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.
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.
Print the number of ways to swap two elements exactly once in order to get a nice sequence.
5 2 8 4 7 7
2
4 200 150 100 50
1
10 3 2 1 4 1 4 1 4 1 4
8
9 1 2 3 4 5 6 7 8 9
0
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.
题目大意:
需要保证最终序列是这样的:
- ti < ti + 1 for each odd i < n;
- ti > ti + 1 for each even i < n.
思路:
1、明显,如果有错误的位子超过了5个,那么肯定就没有解了。
2、如果有错误的位子小于等于4个,那么对应我们将有错误的点的周围几个点都标记上,对于这些点,我们可以选择其与其他N-1个点进行交换,并且判断是否序列满足最终的要求。
时间复杂度O(n*错误点的个数以及周围点的个数和);
所以我们判断序列是否合法的时候,只要对特殊的点:
①交换点的周围几个点。
②有错误的点和周围几个点。
进行判断是否合法即可。
时间复杂度O(需要判断的点的个数*n*错误点的个数以及周围点的个数和);
因为这些点并不多,所以我们可以暴力来搞。
只要维护好需要判断的位子点,以及需要交换的位子点即可。
3、需要谨慎度相对较高..........大家谨慎点写代码....
Ac代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int a[150500];
int vis[150500];
int tot;
int num[150500];
int n;
int judge(int x,int y)
{
num[tot++]=x-5;
num[tot++]=x-4;
num[tot++]=x-3;
num[tot++]=x-2;
num[tot++]=x-1;
num[tot++]=x;
num[tot++]=x+1;
num[tot++]=x+2;
num[tot++]=x+3;
num[tot++]=x+4;
num[tot++]=x+5;
num[tot++]=y-5;
num[tot++]=y-4;
num[tot++]=y-3;
num[tot++]=y-2;
num[tot++]=y-1;
num[tot++]=y;
num[tot++]=y+1;
num[tot++]=y+2;
num[tot++]=y+3;
num[tot++]=y+4;
num[tot++]=y+5;
for(int j=0; j<tot; j++)
{
int v=num[j];
if(v<0||v>=n)continue;
if(v%2==0)
{
if(v-1>=0)
{
if(a[v]<a[v-1])continue;
else return 0;
}
if(v+1<n)
{
if(a[v]<a[v+1])continue;
else return 0;
}
}
else
{
if(v-1>=0)
{
if(a[v]>a[v-1])continue;
else return 0;
}
if(v+1<n)
{
if(a[v]>a[v+1])continue;
else return 0;
}
}
}
return 1;
}
int main()
{
while(~scanf("%d",&n))
{
memset(vis,0,sizeof(vis));
int cnt=0;
for(int i=0; i<n; i++)scanf("%d",&a[i]);
for(int i=1; i<n; i++)
{
if(i%2==1)
{
if(a[i]>a[i-1])continue;
else
{
cnt++;
for(int j=-2; j<=2; j++)
{
if(i+j<n&&i+j>=0)vis[i+j]=1;
}
}
}
else
{
if(a[i]<a[i-1])continue;
else
{
cnt++;
for(int j=-5; j<=5; j++)
{
if(i+j<n&&i+j>=0)vis[i+j]=1;
}
}
}
}
if(cnt>=5)
{
printf("0\n");
}
else
{
__int64 contz=0;
__int64 output=0;
for(int i=0;i<n;i++)
{
if(vis[i]==1)num[tot++]=i;
}
for(int i=0; i<n; i++)
{
if(vis[i]==1)
{
for(int j=0; j<n; j++)
{
if(i==j)continue;
swap(a[i],a[j]);
if(judge(i,j)==1)
{
output++;
if(vis[j]==1)contz++;
}
tot-=22;
swap(a[i],a[j]);
}
}
}
output-=contz/2;
printf("%I64d\n",output);
}
}
}