CodeForces - 1265D(贪心+暴力)

题意

https://vjudge.net/problem/CodeForces-1265D

a个0,b个1,c个2,d个3,问是否存在一种排列方案使得任意相邻两数之差==1

思路

分类讨论太麻烦了,直接暴力搞!

枚举0123每个数作为第一个数,然后优先看这个数-1还有没有,有的话就放进去,没有就看这个数+1,如果两个都没有了,那就break,最后判断0123的数量是否都为0即可。

为什么要先放x-1?这是一种贪心的思路,放了x+1的话那么x-1在后面可能就放不了了,所以优先放x-1。

代码

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
ll p[N],q[N];
vector<int> ans;
int main()
{
    std::ios::sync_with_stdio(false);
    cin>>p[0]>>p[1]>>p[2]>>p[3];
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<4; j++)
            q[j]=p[j];
        int x=i;
        if(!q[x]) continue;
        ans.clear();
        q[x]--;
        ans.push_back(x);
        while(1)
        {
            if(x&&q[x-1])
                --x,q[x]--,ans.push_back(x);
            else if(x!=3&&q[x+1])
                ++x,q[x]--,ans.push_back(x);
            else
                break;
        }
        if(!q[0]&&!q[1]&&!q[2]&&!q[3])
        {
            cout<<"YES"<<endl;
            for(int it:ans)
            {
                cout<<it<<" ";
            }
            cout<<endl;
            return 0;
        }
    }
    cout<<"NO"<<endl;
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/mcq1999/p/12029011.html