Codeforces 428(div 2) B. Game of the Rows(사고 문제)

5099 단어 독학 연습
제목.
B. Game of the Rows
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard 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.
각각 각 조의 입력을 4, 2, 1의 인원수에 따라 나누면 x4, x2, x1, x1이 두 개를 차지해야 하기 때문에 총 인원수sum+x1>n*8이 성립되지 않으면 NO를 출력한다. 그렇지 않으면 x4+x1>=n이 성립된다(이때 3, 4는 x2, 5공, 6은 x1을 넣거나 x1은 중간 간격으로 놓는다. 이때sum+x1<=n*8이기 때문에 반드시 모든 사람을 내려놓을 수 있다). 그렇지 않으면 x2n*2+n-x4로 채운다.나머지 부분은 한 사람으로 분해한 후 n-x4-x1을 채웁니다. 만약에 남은 사람이 있으면 성립되지 않습니다. 출력 NO, 그렇지 않으면 출력 YES
4, 5도 인접석이다
코드는 다음과 같습니다.
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAXN=10010;
int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        int x1=0,x2=0,x4=0;
        int sum=0;
        for(int i=0;iint num;
            scanf("%d",&num);
            sum+=num;
            x4+=num/4;
            num%=4;
            x2+=num/2;
            x1+=num%2;
        }
        if(sum+x1>n*8)
        {
            cout<<"NO"<continue;
        }
        if(x4+x1>=n)
        {
            cout<<"YES"<continue;
        }
        int temp=n-x1-x4;
        if(x2<=n-x4+n*2)
        {
            cout<<"YES"<continue;
        }
        x2-=n-x4+n*2;
        x2*=2;
        if(temp>=x2)
        {
            cout<<"YES"<continue;
        }
        else 
        {
            cout<<"NO"<continue;
        }

    }
    return 0;
}

좋은 웹페이지 즐겨찾기