概述
In Berland each high school student is characterized by academic performance — integer value between 1 and 5.
In high school 0xFF there are two groups of pupils: the group A and the group B. Each group consists of exactly n students. An academic performance of each student is known — integer value between 1 and 5.
The school director wants to redistribute students between groups so that each of the two groups has the same number of students whose academic performance is equal to 1, the same number of students whose academic performance is 2 and so on. In other words, the purpose of the school director is to change the composition of groups, so that for each value of academic performance the numbers of students in both groups are equal.
To achieve this, there is a plan to produce a series of exchanges of students between groups. During the single exchange the director selects one student from the class A and one student of class B. After that, they both change their groups.
Print the least number of exchanges, in order to achieve the desired equal numbers of students for each academic performance.
The first line of the input contains integer number n (1 ≤ n ≤ 100) — number of students in both groups.
The second line contains sequence of integer numbers a1, a2, ..., an (1 ≤ ai ≤ 5), where ai is academic performance of the i-th student of the group A.
The third line contains sequence of integer numbers b1, b2, ..., bn (1 ≤ bi ≤ 5), where bi is academic performance of the i-th student of the group B.
Print the required minimum number of exchanges or -1, if the desired distribution of students can not be obtained.
4 5 4 4 4 5 5 4 5
1
6 1 1 1 1 1 1 5 5 5 5 5 5
3
1 5 3
-1
9 3 2 5 5 2 3 3 3 2 4 1 4 1 1 2 4 4 1
4
题意:求最少次交换使得两个组每个分数所对应的人数相等
AC代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
int a[110], aa[110], b[110], bb[110];
int n;
while(~scanf("%d",&n))
{
memset(aa,0,sizeof(aa));
memset(bb,0,sizeof(bb));
for(int i = 0; i < n; i++)
scanf("%d",&a[i]);
for(int i = 0; i < n; i++){
scanf("%d",&b[i]);
}
for(int i = 0; i < n; i++){
aa[a[i]]++;
bb[b[i]]++;
}
int s = 0;
bool F = false;
for(int i = 1; i <= 5; i++){
if(abs(aa[i] - bb[i]) % 2 == 0)
s += abs(aa[i] - bb[i]) / 2;
else
{
F = true;
break;
}
}
if(!F)
printf("%dn",s/2);
else
printf("-1n");
}
return 0;
}
Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k.
In the given number of n Polycarp wants to remove the least number of digits to get a number that is divisible by 10k. For example, if k = 3, in the number 30020 it is enough to delete a single digit (2). In this case, the result is 3000 that is divisible by 103 = 1000.
Write a program that prints the minimum number of digits to be deleted from the given integer number n, so that the result is divisible by 10k. The result should not start with the unnecessary leading zero (i.e., zero can start only the number 0, which is required to be written as exactly one digit).
It is guaranteed that the answer exists.
The only line of the input contains two integer numbers n and k (0 ≤ n ≤ 2 000 000 000, 1 ≤ k ≤ 9).
It is guaranteed that the answer exists. All numbers in the input are written in traditional notation of integers, that is, without any extra leading zeros.
Print w — the required minimal number of digits to erase. After removing the appropriate w digits from the number n, the result should have a value that is divisible by 10k. The result can start with digit 0 in the single case (the result is zero and written by exactly the only digit 0).
30020 3
1
100 9
2
10203049 2
3
In the example 2 you can remove two digits: 1 and any 0. The result is number 0 which is divisible by any number.
题意:至少删除数n中多少位数使得到的数能够整除10^k
思路:从n的个位数字开始计数有多少个0,将不是0的数删除掉,当0的个数等于k时得到答案。值得注意的是当n中的0的个数小于k时,需要删除到只剩一个0为止
AC代码:
#include<cstdio>
#include<cmath>
#define LL __int64
int main()
{
LL n;
int k;
while(~scanf("%I64d%d",&n,&k)){
if(n == 0)
{
printf("0n");
continue;
}
LL p = pow(10,k);
if(n / p >= 1){
int ss = 0;
int l = 0;
while(n)
{
int a = n % 10;
n /= 10;
if(a == 0)
ss++;
else
l++;
if(ss == k)
break;
}
if(ss == 0)
printf("0n");
else if(ss < k)
printf("%dn",l + ss - 1);
else
printf("%dn",l);
}
else
{
int s = 0;
while(n){
n = n / 10;
s++;
}
printf("%dn",s - 1);
}
}
return 0;
}
Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is ai, and after a week of discounts its price will be bi.
Not all of sellers are honest, so now some products could be more expensive than after a week of discounts.
Igor decided that buy at least k of items now, but wait with the rest of the week in order to save money as much as possible. Your task is to determine the minimum money that Igor can spend to buy all n items.
In the first line there are two positive integer numbers n and k (1 ≤ n ≤ 2·105, 0 ≤ k ≤ n) — total number of items to buy and minimal number of items Igor wants to by right now.
The second line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 104) — prices of items during discounts (i.e. right now).
The third line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104) — prices of items after discounts (i.e. after a week).
Print the minimal amount of money Igor will spend to buy all n items. Remember, he should buy at least k items right now.
3 1 5 4 6 3 1 5
10
5 3 3 4 7 10 3 4 5 5 12 5
25
In the first example Igor should buy item 3 paying 6. But items 1 and 2 he should buy after a week. He will pay 3 and 1 for them. So in total he will pay 6 + 3 + 1 = 10.
In the second example Igor should buy right now items 1, 2, 4 and 5, paying for them 3, 4, 10 and 3, respectively. Item 3 he should buy after a week of discounts, he will pay 5 for it. In total he will spend 3 + 4 + 10 + 3 + 5 = 25.
思路:一个简单的贪心思想。。首先选择在一周后涨价的物品,如果涨价的物品数量s不够k个,再买降价降得最少的物品凑够k个,如果s大于等于k个的话,后边再买n-s个一周后的没买的物品
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 2 * 1e5 + 10;
struct Node{
int cha;
int k;
}node[maxn];
bool cmp(Node a, Node b)
{
return a.cha < b.cha;
}
int main()
{
int n,m;
int a[maxn];
int b[maxn];
while(~scanf("%d%d",&n,&m)){
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
for(int j = 1; j <= n; j++)
scanf("%d",&b[j]);
for(int i = 1; i <= n; i++)
{
node[i].cha = a[i] - b[i];
node[i].k = i;
}
sort(node+1,node+1+n,cmp);
int s = 0;
int sum = 0;
for(int i = 1; i <= n; i++)
{
if(node[i].cha <= 0)
{
sum += a[node[i].k];
s++;
}
else
break;
}
if(s < m)
{
for(int i = s + 1; i <= m; i++)
sum += a[node[i].k];
for(int j = m + 1; j <= n; j++)
sum += b[node[j].k];
}
else
{
for(int i = s + 1; i <= n; i++)
sum += b[node[i].k];
}
printf("%dn",sum);
}
return 0;
}
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" "nastya" "nastya" "nastya" "nastya" "nastya" "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all aiare distinct).
Print a single integer number, the maximum number of letters that Nastya can remove.
ababcba abb 5 3 4 1 7 6 2
3
bbbabb bb 1 6 3 4 2 5
4
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" "ababcba" "ababcba" "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters.
思路:二分
#include<cstdio>
#include<cstring>
const int maxn = 2e6 + 10;
char a[maxn];
char b[maxn];
int k[maxn];
int aa[maxn];
int n,m;
bool chake(int mid)
{
for(int i = 0; i <= n; i++)
aa[i] = 0;
for(int j = mid + 1; j <= n; j++)
aa[k[j]] = 1;
int cnt = 1;
for(int i = 1; i <= n; i++)
{
if(aa[i]){
if(a[i] == b[cnt])
cnt++;
}
}
if(cnt <= m) return false;
else
return true;
}
int main()
{
while(~scanf("%s%s",a+1,b+1)){
n = strlen(a+1);
m = strlen(b+1);
for(int i = 1; i <= n; i++)
scanf("%d",&k[i]);
int l = 0, r = n - 1, mid , ans;
while(l <= r){
mid = (l + r) / 2;
if(chake(mid)) l = mid + 1, ans = mid;
else
r = mid - 1;
}
printf("%dn",ans);
}
return 0;
}
最后
以上就是平淡白羊为你收集整理的【Codeforces Round #402 (Div. 2) 】(A,B,C,D )的全部内容,希望文章能够帮你解决【Codeforces Round #402 (Div. 2) 】(A,B,C,D )所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复