zoukankan      html  css  js  c++  java
  • IndiaHacks 2016

    C. Bear and Up-Down

    题目连接:

    http://www.codeforces.com/contest/653/problem/C

    Description

    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.

    Sample Input

    5
    2 8 4 7 7

    Sample Output

    2

    Hint

    题意

    一个序列定义为nice的话,就是这个序列满足阶梯状。

    就是如果i是偶数,那么ai>ai-1,ai>ai+1

    如果i是奇数,那么ai<ai+1,ai<ai-1

    现在允许你交换两个数的位置,问你一共有多少种交换方式,使得这个序列变成nice

    保证一开始的序列不是nice的。

    题解:

    我们定义不nice的数就是不满足条件的位置。

    我们可以大胆猜测一发,不nice的数一定不会有很多,因为一次交换最多影响6个数,所以我们把这些不nice的数直接扔到一个数组里面。

    然后暴力去和整个序列去交换就好了。

    然后check也是只用check那些不nice的位置和你交换的那个位置的数。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 2e5+7;
    int n;
    int a[maxn];
    vector<int>tmp;
    long long ans = 0;
    set<pair<int,int> >S;
    bool check()
    {
        for(int i=0;i<tmp.size();i++)
        {
            for(int j=-1;j<=1;j++)
            {
                if(tmp[i]+j==0)continue;
                if(tmp[i]+j==n+1)continue;
                int pos = (tmp[i]+j);
                if(pos%2==1)
                {
                    if(a[pos]>=a[pos+1])return false;
                    if(a[pos]>=a[pos-1])return false;
                }
                else
                {
                    if(a[pos]<=a[pos+1])return false;
                    if(a[pos]<=a[pos-1])return false;
                }
            }
        }
        return true;
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        a[0]=1e9;
        if(n%2==1)a[n+1]=1e9;
        else a[n+1]=-1;
        for(int i=1;i<=n;i++)
        {
        	if( i & 1 ){
    			bool ok = true;
    			if( i + 1 <= n && a[i] >= a[i+1] ) ok = false;
    			if( i - 1 >= 1 && a[i] >= a[i-1] ) ok = false;
    			if( ok == false ) tmp.push_back( i );
    		}else{
    			bool ok = true;
    			if( i + 1 <= n && a[i] <= a[i+1] ) ok = false;
    			if( i - 1 >= 1 && a[i] <= a[i-1] ) ok = false;
    			if( ok == false ) tmp.push_back( i );
    		}
        }
    
        if(tmp.size()>30)
            return puts("0"),0;
    
        for(int i=0;i<tmp.size();i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(tmp[i]==j)continue;
                swap(a[tmp[i]],a[j]);
                bool ok = check();
                if(j%2==1)
                {
                    if(a[j]>=a[j+1]||a[j]>=a[j-1])ok=false;
                }
                if(j%2==0)
                {
                    if(a[j]<=a[j+1]||a[j]<=a[j-1])ok=false;
                }
                if(ok)
                {
                    pair < int , int > SS = make_pair( min( tmp[i] , j ) , max( tmp[i] , j ) );
                    if(!S.count(SS)){
                        S.insert( SS );
                        ans++;
                    }
                }
                swap(a[tmp[i]],a[j]);
            }
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    【干货】Windows系统信息收集篇
    【干货】SIFT-Workstation 下载与安装 不跳过每一个细节部分
    【干货】从没有数据结构的二进制中(内存或者未隐藏的数据块)提取关键信息
    【干货】操纵时间 感受威胁 MAC time时间戳视角
    【干货】证据被毁?数据恢复出来
    【干货】已Window7 系统为例,谈谈boot引导程序-------附带看看数据隐藏
    IDEA创建各种不同的工程的方法
    IDEA全局配置
    IDEA项目添加第三方依赖
    IDEA打开最近打开的项目以及关闭项目
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5296520.html
Copyright © 2011-2022 走看看