【Codeforces Round #429 (Div. 2)】 A B C

来源:互联网 发布:js tip提示框 编辑:程序博客网 时间:2024/06/17 05:31

A. Generous Kefa
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
One day Kefa found n baloons. For convenience, we denote color of i-th baloon as si — lowercase letter of the Latin alphabet. Also Kefa has k friends. Friend will be upset, If he get two baloons of the same color. Kefa want to give out all baloons to his friends. Help Kefa to find out, can he give out all his baloons, such that no one of his friens will be upset — print «YES», if he can, and «NO», otherwise. Note, that Kefa’s friend will not upset, if he doesn’t get baloons at all.

Input
The first line contains two integers n and k (1 ≤ n, k ≤ 100) — the number of baloons and friends.

Next line contains string s — colors of baloons.

Output
Answer to the task — «YES» or «NO» in a single line.

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

Examples
input
4 2
aabb
output
YES
input
6 3
aacaab
output
NO
Note
In the first sample Kefa can give 1-st and 3-rd baloon to the first friend, and 2-nd and 4-th to the second.

In the second sample Kefa needs to give to all his friends baloons of color a, but one baloon will stay, thats why answer is «NO».
题意 将n个糖果分给k个朋友,每个朋友得到的糖果中,不会有相同的糖果,问是否可能?
思路一 数据较小,我们可以模拟选的过程,代码
代码

#include<bits/stdc++.h>using namespace std;#define   LL  long longconst int MAXN = 100+10;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;map<char,int >mp[MAXN];int ge[MAXN]={0};char s[MAXN];int main(){ //  freopen("in.txt","r",stdin);//  freopen("out.txt","w",stdout);    int n,k;     while(scanf("%d%d",&n,&k)!=EOF){    scanf("%s",s);     if(k>=n){        puts("YES");                    return 0;    }    int cnt=0;    for(int i=0;s[i];i++) {        for(int j=1;j<=k;j++){            if(mp[j][s[i]]==0) {                 mp[j][s[i]]=1;                  cnt++;                  break;            }            else continue;        }       }    //printf("%d\n",cnt);    if(cnt==n) puts("YES");    else puts("NO");        }    return 0;}

思路二 ,题意只要理解了,其实题目就是说,对于每种的糖果只要不会分给同一个人就可以了,所以这个就是典型的抽屉原理,只要求每种的糖果不超过抽屉个数(朋友个数)就可以。
代码

#include<bits/stdc++.h>using namespace std;#define  LL long longconst int MAXN = 1e5;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;int arr[26];int main(){//   freopen("in.txt","r",stdin);//   freopen("out.txt","w",stdout);    int n,k;scanf("%d%d",&n,&k);    string s;cin>>s;    for(int i=0;s[i];i++) arr[s[i]-'a']++;    for(int i=0;i<26;i++)         if(arr[i]>k) {            puts("NO");            return 0;        }    puts("YES");    return 0;}

B. Godsend
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally?

Input
First line of input data contains single integer n (1 ≤ n ≤ 106) — length of the array.

Next line contains n integers a1, a2, …, an (0 ≤ ai ≤ 109).

Output
Output answer in single line. “First”, if first player wins, and “Second” otherwise (without quotes).

Examples
input
4
1 3 2 3
output
First
input
2
2 2
output
Second
Note
In first sample first player remove whole array in one move and win.

In second sample first player can’t make a move and lose.

博弈题目 。 明显就是对于每个数的奇偶来考虑,所以我们可以分三个方向。
1 有奇数 个奇数,这个时候肯定是先手赢 。
2 有偶数 个奇数,这个时候,只要先手拿走前包含n-1个奇数的序列,最后剩下一个奇数和一些偶数,此时后手肯定只能够拿偶数,下一次先手可以全部拿走,所以这个情况也是先手赢。
3 全是偶数,肯定是后手赢。
综上 。
代码

#include<bits/stdc++.h>using namespace std;#define   LL  long longconst int MAXN = 1e6+10;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;int arr[MAXN];int main(){//  freopen("in.txt","r",stdin);//  freopen("out.txt","w",stdout);    int n;    while(~scanf("%d",&n)){        int a,b; a=b=0;        for(int i=1;i<=n;i++){             scanf("%d",&arr[i]);             if(arr[i]&1) a++;             else b++;        }        if(b==n) puts("Second");        else puts("First") ;        }     return 0;}

C. Leha and Function
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Leha like all kinds of strange things. Recently he liked the function F(n, k). Consider all possible k-element subsets of the set [1, 2, …, n]. For subset find minimal element in it. F(n, k) — mathematical expectation of the minimal element among all k-element subsets.

But only function does not interest him. He wants to do interesting things with it. Mom brought him two arrays A and B, each consists of m integers. For all i, j such that 1 ≤ i, j ≤ m the condition Ai ≥ Bj holds. Help Leha rearrange the numbers in the array A so that the sum is maximally possible, where A’ is already rearranged array.

Input
First line of input data contains single integer m (1 ≤ m ≤ 2·105) — length of arrays A and B.

Next line contains m integers a1, a2, …, am (1 ≤ ai ≤ 109) — array A.

Next line contains m integers b1, b2, …, bm (1 ≤ bi ≤ 109) — array B.

Output
Output m integers a’1, a’2, …, a’m — array A’ which is permutation of the array A.

Examples
input
5
7 3 5 3 4
2 1 3 2 3
output
4 7 3 5 3
input
7
4 6 5 8 8 2 6
2 1 2 2 1 1 2
output
2 6 4 5 8 8 6

看了样例,感觉就是两者差距越大,那么结果就会越大。
事实也就是,我们可以打表,发现其实它是单调的。
代码

#include<bits/stdc++.h>using namespace std;#define   LL  long longconst int MAXN = 2e5+10;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;struct Node{    int val,id,a;}node[MAXN];bool cmp1(Node a,Node b){    return a.val<b.val;}bool cmp2(Node a,Node b){    return a.id<b.id;}priority_queue < int > que;int main(){    int n;scanf("%d",&n);    for(int i=1;i<=n;i++) {        int a;scanf("%d",&a);        que.push(a);    }    for(int i=1;i<=n;i++){        scanf("%d",&node[i].val);        node[i].id=i;    }    sort(node+1,node+1+n,cmp1);    for(int i=1;i<=n;i++) {        node[i].a=que.top();         que.pop();     }     sort(node+1,node+1+n,cmp2);     for(int i=1;i<=n;i++)        printf("%d%s",node[i].a,i==n?"":" ");    puts("");    return 0;}

D 不会,明天补 。

原创粉丝点击