codeforces 811B

原题链接:

  http://codeforces.com/problemset/problem/811/B

题意:

  给你一个序列,截取一段该序列的子序列以升序方式排列,判断重新排序前后某位(该为属于子序列)值是否改变;

思路:

  最开始时只是简单的对子序列sort排序,超时;后来看了大神的博客:数字不变,当且仅当 x 在 [ l , r ] 中的左边比 x 大的数字个数与 x 右边比 x 小的数字个数相等;

  测试数据比较大,不建议流输入,可能会超时(尽管本人没试验);

ps:大神博客链接:http://blog.csdn.net/joovo/article/details/72787899

代码:

 1 #include<cstdio>
 2 #include<string>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     int a[10002];
12     int n,m,l,r,x,i;
13     cin>>n>>m;
14     for(i=0;i<n;i++)
15     {
16         scanf("%d",&a[i]);
17     }
18     while(m--)
19     {
20         int p=0,q=0;
21         scanf("%d%d%d",&l,&r,&x);
22         for(i=l-1;i<r;i++)
23         {
24         //    printf("i=%d,x=%d,a[i]=%d,a[x-1]=%d
",i,x,a[i],a[x-1]);
25             if(i<(x-1)&&a[i]>a[x-1])
26                 p++;
27             else if(i>(x-1)&&a[i]<a[x-1])
28                 q++;
29         }
30     //    printf("p=%d,q=%d
",p,q);
31         if(p==q)
32             printf("Yes
");
33         else
34             printf("No
");
35     }
36     return 0;
37 }
原文地址:https://www.cnblogs.com/x-x-y/p/6917504.html