题目链接:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied:
- ti < ti + 1 for each odd i < n;
- ti > ti + 1 for each even i < n.
For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not.
Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different.
Input
The first line of the input contains one integer n (2 ≤ n ≤ 150 000) — the length of the sequence.
The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 150 000) — the initial sequence. It‘s guaranteed that the given sequence is not nice.
Output
Print the number of ways to swap two elements exactly once in order to get a nice sequence.
Examples
input
52 8 4 7 7
output
2
input
4200 150 100 50
output
1
input
103 2 1 4 1 4 1 4 1 4
output
8
input
91 2 3 4 5 6 7 8 9
output
0
Note
In the first sample, there are two 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 is only one way — Limak should swap t1 = 200 with t4 = 50.
题意:问有多少种交换方法使序列满足nice的条件;
思路:把不合理的位置找出来,然后暴力交换看有多少种方式,乱搞就能搞出来,然而我乱搞了一夜晚一直wa,后来好好想了不同的情况才过;
AC代码:
//代码写的跟翔一样,自己看了就想吐; #include <bits/stdc++.h> using namespace std; const int N=15e4+3; int a[N],flag[N],pos[N],cnt,num,n; int sap(int x,int y) { int t=a[y]; a[y]=a[x]; a[x]=t; } int check(int v) { /* if(x==3) { for(int j=0;j<x;j++) { cout<<pos[j]<<"&"<<endl; } } */ int u=v; // cout<<v<<"@"<<pos[v-1]<<endl; for(int j=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; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } a[0]=10000000; a[n+1]=0; cnt=0,num=0; for(int i=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;} int ans=0; for(int i=0;i<num;i++) { for(int j=i+1;j<num;j++) { sap(pos[i],pos[j]); ans+=check(num); sap(pos[j],pos[i]); } } for(int i=0;i<num;i++) { for(int j=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; }