Codeforces 779

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


/*
  题意很简单。
  给你两个团队 分别都有n个人
  每个人都给了一个评分 1~5 然后要进行重新分成两组 
  要满足每个组的实力相当 A有多少个实力为ai的 B也要有多少个实力为ai的 (ai为1~5)
  如果评分1~5的分别人数不能被2乘除 则一定是不能平均分成两组的 直接输出-1
  求出temp=每种评分总的人数除以2 
  然后for一遍与temp做差取平均值 因为会有重复操作 所以除以二即可
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
using namespace std;
int a[110],b[110];
int mark1[10],mark[10],mark2[10];
int main(void)
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(mark,0,sizeof(mark));
        memset(mark1,0,sizeof(mark1));
        memset(mark2,0,sizeof(mark2));
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            mark1[a[i]]++;
        }
        for(int i=0;i<n;i++)
        {
            scanf("%d",&b[i]);
            mark2[b[i]]++;
        }
        int flag=0;
        for(int i=1;i<=5;i++)
        {
            mark[i]=mark1[i]+mark2[i];
            if((mark1[i]+mark2[i])%2!=0)
            {
                flag=1;
                break;
            }
        }
        if(flag)
        {
            printf("-1\n");
            continue;
        }
        int sum=0;
        for(int i=1;i<=5;i++)
            sum+=fabs(mark1[i]-mark[i]/2);
        printf("%d\n",sum/2);
    }
    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 by10k. 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 和 k
  至少删除n中多少位数 可以满足能被10的k次方整除
  0 which is divisible by any number.
  不可以有前导零。
  输入已经保证满足没有前导零了。
  然后当做字符串输入 从后往前扫一遍即可。
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
using namespace std;
char s[100];
int main(void)
{
    int k;
    while(scanf("%s %d",s,&k)!=EOF)
    {
        int len=strlen(s);
        if(len<=k)
        {
            printf("%d\n",len-1);
            continue;
        }
        int zero=0;
        int num=0;
        for(int i=len-1;i>=0;i--)
        {
            if(s[i]=='0') zero++;
            else num++;
            if(zero==k) break;
        }
        if(zero<k) num=len-1;
        printf("%d\n",num);
    }
    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.



/*
  水题。
  Igor 现在去超市买东西 突然发现一周有打折
  给你n个他需要购买的物品 和 现在必须要购买的物品个数为k。
  然后给你n个物品分别打折前的价格和打折后的价格。
  一周后打折后的价格可能比现在还高。
  但是现在又不得不购买k件商品
  让你帮忙求最少需要花多少钱买这n个物品
  首先利用结构体存储每个商品打折前后的价格 求出差价进行排个序
  然后for一遍即可。
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
using namespace std;
const int maxn = 2*100000;
struct node
{long long int a,b,c;
}p[maxn+10];
bool cmp(const node a,const node b)
{
    return a.c<b.c;
}
int main(void)
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        for(int i=0;i<n;i++)
            scanf("%I64d",&p[i].a);
        for(int i=0;i<n;i++)
            scanf("%I64d",&p[i].b);
        for(int i=0;i<n;i++)
            p[i].c=p[i].a-p[i].b;
        long long int sum=0;
        sort(p,p+n,cmp);
        for(int i=0;i<n;i++)
        {
            if(k!=0)
            {
                sum+=p[i].a;
                k--;
            }
            else if(k==0)
            {
                if(p[i].c>0) sum+=p[i].b;
                else sum+=p[i].a;
            }
        }
        printf("%I64d\n",sum);
    }
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值