CodeForces - 839B Game of the Rows

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Seeyouer/article/details/78662809


题意:输入座位行数n和部队数k,第二行输入k个军队的人数,让你求是否每个军队的士兵可以不相邻。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
//int a[4]={4,2,2},b[5]={2,2,2,1};
//int army[120];
bool cmp(int x,int y){
    return x>y;
}
int main()
{
    int n,k;
    while(~scanf("%d",&n)){
        cin>>k;int m4=0,m2=0,m1=0;
        while(k--){
            int x;
            cin>>x;
            if(x>=4){
                m4+=x/4;
                x%=4;
            }
            if(x>=2){
                m2+=x/2;
                x%=2;
            }
            if(x){
                m1++;
            }

        }
        int y=8*n-(m4*4+m2*2+m1*2);
        if(y>=0&&m2<=3*n+n/2&&m1<=4*n&&m4<=2*n){
            if(m2==3*n+n/2){//如果两个人的军队正好把座位坐满
                if(((n%2)&&m1<=1)||(n%2==0&&m1==0))
                    cout<<"YES\n";//奇数个行还可以剩下两个空位做一个人;偶数行全坐满,不能再做
                else
                    cout<<"NO\n";
            }
            else
            cout<<"YES\n";
        }
        else cout<<"NO\n";
    }


    return 0;
}

猜你喜欢

转载自blog.csdn.net/Seeyouer/article/details/78662809