B - Game of the Rows

54 篇文章 0 订阅
20 篇文章 0 订阅

B. Game of the Rows
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}.

A row in the airplane
Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats.

Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied.

Input
The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively.

The second line contains k integers a1, a2, a3, …, ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group.

It is guaranteed that a1 + a2 + … + ak ≤ 8·n.

Output
If we can place the soldiers in the airplane print “YES” (without quotes). Otherwise print “NO” (without quotes).

You can choose the case (lower or upper) for each letter arbitrary.

Examples
input
2 2
5 8
output
YES
input
1 2
7 1
output
NO
input
1 2
4 4
output
YES
input
1 4
2 2 1 2
output
YES
这个题需要仔细分析,多动脑才能少动手。
开始我们先把4个座位的给占满就可以。也就是说先把大于等于4的那些集合填上4,而余数1,2,3我们先不要去管。在这期间如果4连座用完了,那么我们用2连座顶替,余数1我们依旧先不要管,这样我们在大于等于4的集合处理完之后,依旧只剩下4连座和2连座。然后我们考虑3,2,1的情况。
3的情况很好说,有4用4,没有用两个2.(为什么不先用2个2,贪心思想, 1个3会占用1个4连座或者两个2连座,但是2个2连座可以做两个2,4连坐却不能)。
2的情况还是先4连座为主,这时候会剩下一个可以单独坐一个人的位置(很重要,不要忘记加)。没有了用一个2连座。这时候顺序就无所谓了。
1的情况就是随便了能做就做。

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int MAXN = 1e4+7;
const int inf = 1e9;
int n,k;
int num[MAXN];

int main()
{
    scanf("%d%d",&n,&k);
    int x;
    priority_queue<int>q;
    for(int i = 0; i < k; ++i)
    {
        scanf("%d",&x);
        q.push(x);
    }
    //x1表示4连座,x2表示2连座,x3表示4连座已经坐了两个了(还能坐1个的)
    int x1 = n,x2 = n*2,x3 = 0;
    while(!q.empty())
    {
        x = q.top();
        q.pop();
        if(x >= 4)
        {
            int d = x/4;
            if(x1 >= d)
            {
                x1-=d;
                if(x%4)q.push(x%4);
            }
            else
            {
                x -= x1*4;
                x1 = 0;
                x2 -= (x+1)/2;
            }
        }
        else if(x == 3)
        {
            if(x1)x1--;
            else x2-=2;
        }
        else if(x == 2)
        {
            if(x1)x1--,x3++;
            else if(x2)x2--;
            else x3-=2;
        }
        else
        {
            if(x3)x3--;
            else if(x2)x2--;
            else x1--,x2++;
        }
        if(x1 < 0 || x2 < 0 || x3 < 0)return 0*puts("NO");
    }
    puts("YES");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值