codeforces 653C C. Bear and Up-Down

11958 단어
제목 링크:
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 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
    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 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의 조건을 충족시킬 수 있는 몇 가지 교환 방법이 있는지 묻기;
    사고방식: 불합리한 위치를 찾아내고 폭력 교환을 해서 몇 가지 방식이 있는지 보면 마구잡이로 할 수 있다. 그러나 나는 밤새도록 마구잡이로 놀다가 다른 상황을 잘 생각해서 보냈다.
    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<<"
    "; return 0; }

    좋은 웹페이지 즐겨찾기