Codeforces Round #429 (Div. 2) A-C题解

来源:互联网 发布:淘宝店铺支付宝限额 编辑:程序博客网 时间:2024/06/17 10:09

传送门:点击打开链接

A. Generous Kefa
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 2aabb
output
YES
input
6 3aacaab
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».


题目意思是每人k个气球,如果有颜色重复的,就输出 NO ,否则YES。

签到题,map跟普通数组都能实现。计算每个字母出现的次数,大于k就输出 NO。

代码实现:

#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<cstdio>#include<map>#define ll long long#define mset(a,x) memset(a,x,sizeof(a))using namespace std;const double PI=acos(-1);const int inf=0x3f3f3f3f;const double esp=1e-6;const int maxn=100005;const int mod=1e9+7;int dir[4][2]={0,1,1,0,0,-1,-1,0};int main(){map <char,int> mp;int n,k,i;char ch;while(cin>>n>>k){mp.clear();for(i=0;i<n;i++){cin>>ch;mp[ch]++;}map<char,int>::iterator it;int flag=1;for(it=mp.begin();it!=mp.end();it++){if(it->second>k){flag=0;break;}}if(flag)cout<<"YES"<<endl;elsecout<<"NO"<<endl;}return 0;}


#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<cstdio>#define ll long long#define mset(a,x) memset(a,x,sizeof(a))using namespace std;const double PI=acos(-1);const int inf=0x3f3f3f3f;const double esp=1e-6;const int maxn=100005;const int mod=1e9+7;int dir[4][2]={0,1,1,0,0,-1,-1,0};int ans[30];int main(){int n,k,i,j;char ch;while(cin>>n>>k){mset(ans,0);for(i=0;i<n;i++){cin>>ch;ans[ch-'a']++;}int flag=1;for(i=0;i<30;i++){if(ans[i]>k){flag=0;break;}}if(flag)cout<<"YES"<<endl;elsecout<<"NO"<<endl;}return 0;}


B. Godsend
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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
41 3 2 3
output
First
input
22 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.


一堆数,第一个人取数的和只能取奇数,第二个人只能是偶数,问谁能赢。
首先和是奇数的话,第一个肯定能赢,数组如果全是偶数,那么第二个人赢,如果现奇数,第一个人肯定能赢(前提是和是偶数)。

代码实现:

#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<cstdio>#define ll long long#define mset(a,x) memset(a,x,sizeof(a))using namespace std;const double PI=acos(-1);const int inf=0x3f3f3f3f;const double esp=1e-6;const int maxn=1e6+5;const int mod=1e9+7;int dir[4][2]={0,1,1,0,0,-1,-1,0};int map[maxn];int main(){int n,i,ans,sum;while(cin>>n){ans=sum=0;for(i=0;i<n;i++){cin>>map[i];if(map[i]%2)ans++;sum+=map[i];}if(sum%2){cout<<"First"<<endl;continue;}if(ans==0){cout<<"Second"<<endl;}else{cout<<"First"<<endl;}}return 0;}

C. Leha and Function
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 ofm 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
57 3 5 3 42 1 3 2 3
output
4 7 3 5 3
input
74 6 5 8 8 2 62 1 2 2 1 1 2
output
2 6 4 5 8 8 6

把a重新排成一个序列,要求期望最高。期望就是任意取两个数,最小的数的和除以总数得到期望。怎么也想不透,别人的博客说b中小的数对应a中大的数的位置,按思路写了,不出示例结果,很费解,就去看别人代码,也没差错,就运行别人代码,果然结果不同,应该是sj,哎。。。。

代码实现:

#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<cstdio>#define ll long long#define mset(a,x) memset(a,x,sizeof(a))using namespace std;const double PI=acos(-1);const int inf=0x3f3f3f3f;const double esp=1e-6;const int maxn=2*1e5+5;const int mod=1e9+7;int dir[4][2]={0,1,1,0,0,-1,-1,0};struct node{int num;int index;}b[maxn];int a[maxn],ans[maxn];int cmp1(int x,int y){return x>y;}int cmp2(node a,node b){return a.num<b.num;}int main(){int n,i,j,k;while(cin>>n){mset(b,0);mset(a,0);mset(ans,0);for(i=0;i<n;i++){scanf("%d",&a[i]);}for(i=0;i<n;i++){scanf("%d",&b[i].num);b[i].index=i;}sort(a,a+n,cmp1);sort(b,b+n,cmp2);for(i=0;i<n;i++)ans[b[i].index]=a[i];for(i=0;i<n;i++){if(i)cout<<' '<<ans[i];elsecout<<ans[i];}cout<<endl;}return 0;}




原创粉丝点击