codeforces 839B(模拟)

来源:互联网 发布:c语言easyx 编辑:程序博客网 时间:2024/05/22 03:07

这个人写得很好了

http://blog.sina.com.cn/s/blog_be010f940102x9tv.html

我漏了一个点就是:如果剩下一个人,那么要先选四位的而不是二位的

#include <bits/stdc++.h>using namespace std;const int N = 1e4+100;int a[N];int cmp(int a,int b){    return a>b;}int main(){    int n,k;    scanf("%d%d",&n,&k);    for(int i=1;i<=k;i++)        scanf("%d",&a[i]);    sort(a+1,a+k+1,cmp);    int hh=2*n;    int tot=0,h=0;    for(int i=1;i<=k;i++)    {        int kk=a[i]/4;        if(kk<=n) n-=kk,a[i]%=4;        else        {            a[i]-=4*n;            n=0;        }        if(a[i]==3)        {            if(n) n--,a[i]-=3;            else if(hh>=2)                hh-=2,a[i]-=3;        }         if(a[i])        {            int tt=a[i]/2;            if(tt<=hh)            hh-=tt,a[i]%=2;            else{                a[i]-=hh*2;                if(h>=a[i]/2)                   h-=(a[i]/2),tot-=(a[i]/2),a[i]%=2;                else a[i]-=h*2,h=0,tot-=h;            }        }        if(a[i]==1&&n)        {            a[i]=0;            n--;            h++,tot++;        }        if(a[i]==1&&tot) tot-=1,h--,a[i]-=1;        if(a[i]==1&&hh) hh-=1,a[i]-=1;        if(a[i]==2&&n)         {             if(a[i]==2) tot++;             a[i]=0,n--;         }        if(a[i]&&tot)        {            if(tot>=a[i]) tot-=a[i],a[i]=0;            else a[i]-=tot,tot=0;        }        if(a[i])        {             printf("NO\n");             return 0;        }    }    printf("YES\n");}
原创粉丝点击