貪心+思維策略 - 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

最後判斷是否有團體的人數大於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;
}

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章