【Codeforces Round #402 (Div. 2) 】(A,B,C,D )

A. Pupils Redistribution
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

Print the required minimum number of exchanges or -1, if the desired distribution of students can not be obtained.

Examples
input
4
5 4 4 4
5 5 4 5
output
1
input
6
1 1 1 1 1 1
5 5 5 5 5 5
output
3
input
1
5
3
output
-1
input
9
3 2 5 5 2 3 3 3 2
4 1 4 1 1 2 4 4 1
output

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("%d\n",s/2);
		else
		   printf("-1\n");
	}
	return 0;
}

B. Weird Rounding
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The only line of the input contains two integer numbers n and k (0 ≤ n ≤ 2 000 000 0001 ≤ 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.

Output

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).

Examples
input
30020 3
output
1
input
100 9
output
2
input
10203049 2
output
3
Note

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("0\n");
			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("0\n");
			else if(ss < k)	
			  printf("%d\n",l + ss - 1);
			else
			  printf("%d\n",l);
		}
		else
		{
		    int s = 0;
			while(n){
				n = n / 10;
				s++;
			}
			printf("%d\n",s - 1);
		}
	}
	return 0;
}

C. Dishonest Sellers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

In the first line there are two positive integer numbers n and k (1 ≤ n ≤ 2·1050 ≤ 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).

Output

Print the minimal amount of money Igor will spend to buy all n items. Remember, he should buy at least k items right now.

Examples
input
3 1
5 4 6
3 1 5
output
10
input
5 3
3 4 7 10 3
4 5 5 12 5
output
25
Note

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.

题意:一共有n个物品,给你两组数据,第一组代表当前每个物品的价格,第二组代表一周后每个物品的价格,在现在必须买够k个物品,问买n个物品最少要花费多少钱。

思路:一个简单的贪心思想。。首先选择在一周后涨价的物品,如果涨价的物品数量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("%d\n",sum);
	}
	return 0;
}

D. String Game
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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 ta1... 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.

Input

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).

Output

Print a single integer number, the maximum number of letters that Nastya can remove.

Examples
input
ababcba
abb
5 3 4 1 7 6 2
output
3
input
bbbabb
bb
1 6 3 4 2 5
output
4
Note

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.

题意:问随着数组a所给出的数据删除第一个字符串的第ai个字符,最多可以删除多少个字符使得第一个字符串中存在第二个字符串

思路:二分

#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("%d\n",ans);
	 }
	 return 0;
 } 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值