贪心+思维策略 - codeforce 839B Game of the Rows

B. Game of the Rows


题意:座位安排如下: 12  3456  78,每行8个位置。先给定n行座位,k个团体 a1,a2....ak,不是同一个团体的人不能坐在一块。m个团体是否都能坐下?


数据范围

n and k (1 ≤ n ≤ 10000,1 ≤ k ≤ 100)

a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000),  a1 + a2 + ... + ak ≤ 8·n.


分析

采取贪心策略,是尽可能多的热能够坐得下。但是这里有两种座位,两种座位能够使团体最终剩余的人数为1,2,3。

4个连着的座位分配完,有剩余可以分配为两个两个连着的座位和一个单独的座位。4 -> 2+1

然后分配两个连着的座位,分配完有剩余就只能分配为一个单独的座位。2 -> 1

扫描二维码关注公众号,回复: 999981 查看本文章

最后判断是否有团体的人数大于0,输出结果。


代码

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int a[105];

int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        int cc=0;
        for(int i=1; i<=k; ++i)
        {
            scanf("%d",&a[i]);
            cc+=a[i];
        }

        int ot=2*n,of=n,oo=0;
        int t;
        for(int i=1; i<=k; ++i)
        {
            t=min(of,a[i]/4);
            a[i]-=t*4;
            of-=t;
            if(of<=0) break;
        }
        oo+=of;
        ot+=of;

        for(int i=1; i<=k; ++i)
        {
            t=min(ot,a[i]/2);
            a[i]-=t*2;
            ot-=t;
            if(ot<=0) break;
        }
        oo+=ot;

        bool fg=true;
        for(int i=1; i<=k; ++i)
        {
            t=min(oo,a[i]);
            a[i]-=t;
            oo-=t;
            if(a[i]>0)
            {
                fg=false;
                break;
            }
        }

        if(fg) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}




猜你喜欢

转载自blog.csdn.net/exchan/article/details/77148116