Codeforces Round #428 (Div. 2)(A+B)

A. Arya and Bran
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Bran and his older sister Arya are from the same house. Bran like candies so much, so Arya is going to give him some Candies.

At first, Arya and Bran have 0 Candies. There are n days, at the i-th day, Arya finds ai candies in a box, that is given by the Many-Faced God. Every day she can give Bran at most 8 of her candies. If she don’t give him the candies at the same day, they are saved for her and she can give them to him later.

Your task is to find the minimum number of days Arya needs to give Bran k candies before the end of the n-th day. Formally, you need to output the minimum day index to the end of which k candies will be given out (the days are indexed from 1 to n).

Print -1 if she can’t give him k candies during n given days.

Input
The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10000).

The second line contains n integers a1, a2, a3, …, an (1 ≤ ai ≤ 100).

Output
If it is impossible for Arya to give Bran k candies within n days, print -1.

Otherwise print a single integer — the minimum number of days Arya needs to give Bran k candies before the end of the n-th day.

Examples
input
2 3
1 2
output
2
input
3 17
10 10 10
output
3
input
1 9
10
output
-1
Note
In the first sample, Arya can give Bran 3 candies in 2 days.

In the second sample, Arya can give Bran 17 candies in 3 days, because she can give him at most 8 candies per day.

In the third sample, Arya can’t give Bran 9 candies, because she can give him at most 8 candies per day and she must give him the candies within 1 day.
題意:每天會收到ai顆糖果,每天至多分出去8顆,問最少多少天可以分出去k顆。
代碼:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
#include<map>
#define NI  freopen("in.txt","r",stdin);
#define NO  freopen("out.txt","w",stdout);
using namespace std;
typedef long long int ll;
typedef pair<int,int>pa;
const int N=1e5+10;
const int MOD=1e9+7;
const ll INF=1e18;
const int NL=2550;
int read()
{
    int x=0;
    char ch = getchar();
    while('0'>ch||ch>'9')ch=getchar();
    while('0'<=ch&&ch<='9')
    {
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    return x;
}
/************************************************************/
int n,k;
int a[N];
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++) cin>>a[i];
    int tot=0;
    for(int i=1;i<=n;i++)
    {
        tot+=a[i];
        if(tot<8)
        {
            k-=tot;
            tot=0;
        }
        else
        {
            k-=8;
            tot-=8;
        }
        if(k<=0)
        {
            return 0*printf("%d\n",i);
        }
    }
    return 0*printf("-1\n");
}

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
Note
In the first sample, Daenerys can place the soldiers like in the figure below:

In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group.

In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats.

In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).

題意:飛機上有n行座位,k個軍團的人,不同軍團的人座位不能相鄰,問能否坐下。
題解:真是煩,很明顯可以將座位分成兩種,雙人座和4人座,3人或4人坐4人座,1人或2人坐雙人座。注意4人座可以坐一個2和一個1,(3,4,6),一個2可以拆成兩個1。然後分情況討論就行了。
代碼:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<math.h>
#include<algorithm>
#include<map>
#define NI  freopen("in.txt","r",stdin);
#define NO  freopen("out.txt","w",stdout);
using namespace std;
typedef long long int ll;
typedef pair<int,int>pa;
const int N=1e5+10;
const int MOD=1e9+7;
const ll INF=1e18;
const int NL=2550;
int read()
{
    int x=0;
    char ch = getchar();
    while('0'>ch||ch>'9')ch=getchar();
    while('0'<=ch&&ch<='9')
    {
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    return x;
}
/************************************************************/
int n,k;
int a[N];
int main()
{
    cin>>n>>k;
     int tot=n*2;
    int cnt=n;
    for(int i=1; i<=k; i++)
    {
         cin>>a[i];
         cnt-=a[i]/4;
         a[i]%=4;
    }
    sort(a+1,a+1+k);
    reverse(a+1,a+1+k);
    int fl=0;
    int fr=0;
    for(int i=1; i<=k; i++)
    {
        if(a[i]==3)
        {
            cnt--;
        }
        else if(a[i]>0)
        {
            tot--;
            if(tot<0)
            {
            if(a[i]==1) fl++;
            if(a[i]==2) fr++;
            }
        }
    }
    int mx=max(fl,fr);
    int mi=min(fl,fr);
    if((cnt<0&&(tot/2)+cnt>=0)||(tot<0&&cnt-(mi+ceil((fl-mi)/2.0)+(fr-mi)/3*2+(fr-mi)%3)>=0)||(cnt>=0&&tot>=0))
        puts("YES");
    else
        puts("NO");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章