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.
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.
Answer to the task — «YES» or «NO» in a single line.
You can choose the case (lower or upper) for each letter arbitrary.
4 2
aabb
YES
6 3
aacaab
NO
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 个人,每个人可以没有气球但不能分到颜色一样的,要求必须分完所有气球。
将气球按颜色分,因为必须分完且每个人不能分到颜色一样的,根据抽屉原理,一旦有一种颜色的气球个数超过 k 个,那么就不存在分配方案,否则存在。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[30];
char s[200];
int main()
{
memset(a,0,sizeof(a));
int n,k;
bool ok = true;
scanf("%d %d %s",&n,&k,s);
for(int i = 0;s[i] != '\0'; ++i)
{
int x = s[i] - 'a';
a[x]++;
if(a[x] > k) ok = false;
}
if(ok) printf("YES\n");
else printf("NO\n");
return 0;
}
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?
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 answer in single line. "First", if first player wins, and "Second" otherwise (without quotes).
4
1 3 2 3
First
2
2 2
Second
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.
题目大意:给定一个长度为 n 的序列,两人轮流按规则取。第一个人每次可以取长度大于 0 的且和是奇数的一段连续序列,第二个人,每次可以取长度大于 0 的且和是偶数的一段连续序列。轮到谁,谁没的取就输了。
一开始读错题了,血崩,卡了四十多分钟,以为 “non-zero” 意思是不含 0 的序列.。。。观察可以发现只要序列里有奇数,后手必败。然后就很简单了。。。判断有没有奇数就行了。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n,t,ans = 0;
scanf("%d",&n);
for(int i = 0;i < n; ++i)
{
scanf("%d",&t);
if(t & 1) ans = 1;
}
if(ans) printf("First\n");
else printf("Second\n");
return 0;
}
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.
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 m integers a'1, a'2, ..., a'm — array A' which is permutation of the array A.
5
7 3 5 3 4
2 1 3 2 3
4 7 3 5 3
7
4 6 5 8 8 2 6
2 1 2 2 1 1 2
2 6 4 5 8 8 6
题目大意:定义了一个F(n,k),意思是 1 ~ n 里取 k 个数,最小值的期望。然后给定两个序列A、B,且满足 Ai >= Bj 。要求将 A 重新排列,使得F(Ai',Bi)求和的结果尽可能大(i = 1,2,3,……)。
推一推能发现这是一道贪心,对于F(n,k),n 越大 k 越小,F(n,k)就越大(就这么过了。。。证明不会(逃。。),于是贪心的策略就是将最大的 Ai 分配给最小的 Bi,两次排序就能做到。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 2 * 1e5 + 5;
typedef struct node
{
int x,y;
}node;
int a[maxn],c[maxn];
node b[maxn];
bool cmp1(node x,node y)
{
return x.x < y.x;
}
bool cmp2(int x,int y)
{
return x > y;
}
int main()
{
int n;
scanf("%d",&n);
for(int i = 0;i < n; ++i) scanf("%d",&a[i]);
for(int i = 0;i < n; ++i) scanf("%d",&b[i].x),b[i].y = i;
sort(a,a + n,cmp2);
sort(b,b + n,cmp1);
for(int i = 0;i < n; ++i) c[b[i].y] = a[i];
for(int i = 0;i < n; ++i) printf("%d ",c[i]);
return 0;
}