Topic Links:
C. Bear and Up-down
Time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following and Condi tions is satisfied:
- T I < ti + 1 for each odd i < n;
- T i > ti + 1 for each even i < n.
For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1,, in.) is nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) is not.
Bear Limak have a sequence of positive integers t1, t2, ..., tn. This sequence are not nice now and Limak wants to fix it by a single swap. He is going to choose, indices I < J and swap elements ti and tJ in order to get a nice sequence. Count the number of ways to does so. Ways is considered different if indices of elements chosen for a swap is different.
Input
The first line of the input contains one integer n (2≤ n ≤150)-the length of the Sequenc E.
The second line contains n integers t1, t2, ..., tn (1≤ ti ≤150)-the initial sequence. It's guaranteed that the given sequence are not nice.
Output
Print the number of ways to swap, elements exactly once in order to get a nice sequence.
Examplesinput
5
2 8 4) 7 7
Output
2
Input
4
200 150 100 50
Output
1
Input
10
3 2 1 4 1 4 1 4 1 4
Output
8
Input
9
1 2 3 4 5 6 7 8 9
Output
0
Note
In the first sample, there is ways to get a nice sequence with one swap:
- Swap t2 = 8 with t4 = 7.
- Swap t1 = 2 with t5 = 7.
In the second sample, there are only one way-limak should swap t1 = @ with t4 = 50.
Test instructions: Ask how many exchange methods make the sequence satisfy Nice's condition;
Ideas: The unreasonable position to find out, and then the violent exchange to see how many ways, chaos can be made out, but I messed up a night has been WA, and then well thought of different circumstances before;
AC Code:
Code to write the same as Xiang, he saw the want to vomit;
#include <bits/stdc++.h>using namespacestd;Const intn=15e4+3;intA[n],flag[n],pos[n],cnt,num,n;intSapintXinty) { intt=A[y]; A[y]=A[x]; A[X]=t;}intCheckintv) { /*if (x==3) {for (int j=0;j<x;j++) {cout<<pos[j]<< & <<endl; } } */ intu=v; //cout<<v<< "@" <<pos[v-1]<<endl; for(intj=0; j<u;j++) { //cout<<a[1]<< "*" <<a[4]<<endl; //cout<<pos[j]<< "#" <<j<<endl; if(pos[j]==1) { if(a[pos[j]+1]<=A[POS[J]])return 0; Continue; } if(pos[j]==N) {if(n%2) { if(a[n-1]<=a[n])return 0; Continue; } Else { if(a[n-1]>=a[n])return 0; Continue; } } if(pos[j]%2) { if(a[pos[j]-1]<=a[pos[j]]| | a[pos[j]+1]<=A[POS[J]])return 0; } Else { if(a[pos[j]-1]>=a[pos[j]]| | a[pos[j]+1]>=A[POS[J]])return 0; } } return 1;}intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); } a[0]=10000000; A[n+1]=0; CNT=0, num=0; for(intI=1; i<n;i++) { if(i%2) { if(a[i]>=a[i+1]){ if(!flag[i]) {pos[num++]=i,flag[i]=1;} if(!flag[i+1]) pos[num++]=i+1, flag[i+1]=1; } } Else { if(a[i]<=a[i+1]) { if(!flag[i]) pos[num++]=i,flag[i]=1; if(!flag[i+1]) pos[num++]=i+1, flag[i+1]=1; } } } if(num>=8) {cout<<"0"<<endl;return 0;} intans=0; for(intI=0; i<num;i++) { for(intj=i+1; j<num;j++) {SAP (pos[i],pos[j]); Ans+=check (num); SAP (Pos[j],pos[i]); } } for(intI=0; i<num;i++) { for(intj=1; j<=n;j++) { if(!Flag[j]) {SAP (POS[I],J); Pos[num]=J; Ans+=check (num+1); SAP (J,pos[i]); } }} cout<<ans<<"\ n"; return 0;}
Codeforces 653C C. Bear and Up-down (Doodle)