Codeforces Round (Div. 2) 839B. Game of the Rows (Thinking + Greed)

Keywords: P4 REST

Written after newbee was 3:0 by Li Kui.
Turn off live broadcasting, change a place, just a, maybe I don't want too many irrelevant things.
I think it's hard to tell the truth. If I don't see the data on cf, it may be a long time before I can get a.
My idea is to divide each regiment into three groups, four people in a group, two people in a group, one person in a group. In that case, two people and one person in a group, each regiment has at most one. (Key)
Then there's the greedy strategy. The seats in the middle of the plane must be seated by four people, because it won't waste space. Then if the four seats can't be seated by four people, the four people will take apart and sit on both sides.
Then deal with the two-person group. Similarly, the two-person group should have priority on both sides.
Then one person, if there's any left on either side, let them sit over.
Finally, we only have four seats in the middle, right? Because this place, a row can sit three people at most, so special consideration should be given to the remaining two groups, which occupy the same side at most, and the rest can be given to one person, but note that the two groups can also be split up, so we need to add another layer of judgment.
See the code for details.

/*  xzppp  */
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <list>
#include <math.h>
#include <iomanip>
using namespace std;
#define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mp make_pair
typedef long long  LL;
typedef unsigned long long ULL;
const int MAXN = 10000;
const int  INF = 0x7fffffff;
const int MOD = 1e9+7;
struct 
{
    int p1,p2,p4;
}all[MAXN+17];
int main()
{
    //FFF
    int n,k;
    cin>>n>>k;
    int h4 = n,h2 = 2*n;
    for (int i = 0; i < k; ++i)
    {
        int temp;
        cin>>temp;
        all[i].p4 = temp/4;
        temp -= all[i].p4*4;
        all[i].p2 = temp/2;
        temp -= all[i].p2*2;
        all[i].p1 = temp;
    }
    bool can = true;
    int r2=0,r1=0;
    for (int i = 0; i < k; ++i)
    {
        if(h4>=all[i].p4)
            h4 -= all[i].p4;
        else
        {
            all[i].p4 -= h4;
            h4 = 0;
            if(2*h2>=all[i].p4)
                h2 -= 2*all[i].p4;
            else
                can = false;
        }
        r2 += all[i].p2;
        r1 += all[i].p1;
    }
    if(h2>=r2)
    {
        h2 -= r2;
        r2 = 0;
    }
    else
    {
        r2 -= h2;
        h2 = 0;
    }
    if(h2>=r1)
    {
        h2 -= r1;
        r1 = 0;
    }
    else
    {
        r2 -= h2;
        h2 = 0;
    }
    if(!(h4>=r2&&r1+r2<=2*h4))
    {
        if(!(r2>h4&&(r2-h4)*2+r1<=h4))
            can = false;
    }
    if(can)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    return 0;
}

Posted by wyred on Wed, 13 Feb 2019 03:21:18 -0800