codeforces 839B(模拟)

这个人写得很好了

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");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值