bzoj1303

数学题

bzoj1303
 1 #include<cstdio>

 2 #include<cstring>

 3 #include<cmath>

 4 #include<ctime>

 5 #include<cstdlib>

 6 #include<iostream>

 7 #include<algorithm>

 8 #define clr(a,x) memset(a,x,sizeof(a))

 9 #define rep(i,l,r) for(int i=l;i<r;i++)

10 #define down(i,r,l) for(int i=r;i>=l;i--)

11 typedef long long ll;

12 using namespace std;

13 int read()

14 {

15     char c=getchar();

16     int ans=0,f=1;

17     while(!isdigit(c)){

18         if(c=='-') f=-1;

19         c=getchar();

20     }

21     while(isdigit(c)){

22         ans=ans*10+c-'0';

23         c=getchar();

24     }

25     return ans*f;

26 }

27 const int maxn=100005;

28 int a[maxn],l[maxn<<1],r[maxn<<1],s[maxn],pos;

29 int main()

30 {

31     int n=read(),b=read();

32     rep(i,0,n){

33         int t=read();

34         if(t>b) a[i]=1;

35         else if(t<b) a[i]=-1;

36         else {

37             a[i]=0;

38             pos=i;

39         }

40     }

41     l[n]=r[n]=1;

42     down(i,pos-1,0){

43         s[i]=s[i+1]+a[i];

44         ++l[n+s[i]];

45     }

46     rep(i,pos+1,n){

47         s[i]=s[i-1]+a[i];

48         ++r[n+s[i]];

49     }

50     ll ans=0;

51     rep(i,0,n<<1){

52         ans+=l[i]*r[(n<<1)-i];

53     }

54     printf("%lld\n",ans);

55     return 0;

56 }
View Code

1303: [CQOI2009]中位数图

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 1627  Solved: 1060
[Submit][Status][Discuss]

Description

给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b。中位数是指把所有元素从小到大排列后,位于中间的数。

Input

第一行为两个正整数n和b ,第二行为1~n 的排列。

Output

输出一个整数,即中位数为b的连续子序列个数。

Sample Input

7 4
5 7 2 4 3 1 6

Sample Output

4

HINT

第三个样例解释:{4}, {7,2,4}, {5,7,2,4,3}和{5,7,2,4,3,1,6}
N<=100000

Source

 
[ Submit][ Status][ Discuss]

你可能感兴趣的:(ZOJ)