Codeforces 653C Bear and Up-Down【暴力+思维】题目还不错~

来源:互联网 发布:stl源码剖析第二章 编辑:程序博客网 时间:2024/06/15 04:26

C. Bear and Up-Down
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 callednice if the following two conditions are satisfied:

  • ti < ti + 1 for each oddi < n;
  • ti > ti + 1 for each eveni < 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 sequenceis not nice now and Limak wants to fix it by a single swap. He is going to choose two indicesi < 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:

  1. Swap t2 = 8 witht4 = 7.
  2. Swap t1 = 2 witht5 = 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);        }    }}







0 0