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] 现在要求两个组内 [1,5]各个数字出现的次数相同 问最少要交换两个组内的数字多少次.
题解:若某一个数字在两组内出现的次数之和为奇数 则无论怎么交换都无法满足要求,输出“-1” 其他情况下题目都会有解
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 int n;
15 int a[10];
16 int b[10];
17 int c[10];
18 int x;
19 int main()
20 {
21     int ans1=0;
22     scanf("%d",&n);
23     for(int i=1;i<=n;i++)
24     {
25         scanf("%d",&x);
26         a[x]++;
27         c[x]++;
28     }
29      for(int i=1;i<=n;i++)
30     {
31         scanf("%d",&x);
32         b[x]++;
33         c[x]++;
34     }
35     for(int i=1;i<=5;i++)
36     {
37         if(c[i]%2)
38         {
39             printf("-1\n");
40             return 0;
41         }
42         else{
43         if(a[i]>b[i])
44             {
45             ans1+=a[i]-c[i]/2;
46             }
47         }
48     }
49     printf("%d\n",ans1);
50     return 0;
51 }

 

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

Output

Print w — the required minimal number of digits to erase. After removing the appropriate wdigits 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 问题一定有解

 题解:从后向前遍历  记录零与非零的个数zha,ans  直到zha等于k 则输出ans

否则  输出n的长度减一 

0能够除尽任何数

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 ll k;
15 char a[200];
16 int main()
17 {
18     scanf("%s %I64d",a,&k);
19     int ans=0;
20     int len=strlen(a);
21     int zha=0;
22     if(len>=k)
23     {
24         for(int i=len-1;i>=0;i--)
25             {
26                 if(a[i]!='0')
27                  ans++;
28                  else
29                 zha++;
30                 if(zha==k)
31                     {
32                 printf("%d\n",ans);
33                 return 0;
34                 }
35              }
36         printf("%d\n",len-1);
37         return 0;
38     }
39     else
40     {
41      printf("%d\n",len-1);
42     }
43     return 0;
44 }

 

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

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个物品 最少的花费。 

题解:sort

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 int n,k;
15 struct  node
16 {
17     int a,b;
18     int cha;
19 }N[200005];
20 bool cmp(struct node aa,struct node bb)
21 {
22     return aa.cha<bb.cha;
23 }
24 int main()
25 {
26     int ans=0;
27     scanf("%d %d",&n,&k);
28     for(int i=1;i<=n;i++)
29     {
30         scanf("%d",&N[i].a);
31     }
32      for(int i=1;i<=n;i++)
33     {
34         scanf("%d",&N[i].b);
35         N[i].cha=N[i].a-N[i].b;
36     }
37     sort(N+1,N+1+n,cmp);
38     for(int i=1;i<=k;i++)
39     {
40       ans+=N[i].a;
41     }
42     for(int i=k+1;i<=n;i++)
43     {
44         if(N[i].cha<0)
45             ans+=N[i].a;
46         else
47             ans+=N[i].b;
48     }
49     printf("%d\n",ans);
50     return 0;
51 }

 

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 ai are 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.

题意:给你初始字符串 目标字符串 以及对初始字符串执行删除操作的顺序  

若对某个字符删除之后无法到达目标状态  则无法删除 并且结束

问最多能删除多少个字符

题解:二分答案  fun()

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 char a[200005];
15 char b[200005];
16 int c[200005];
17 map<int,int> mp;
18 int len1,len2;
19 bool fun(int x)
20 {
21     mp.clear();
22     for(int i=0;i<=x;i++)
23     {
24         mp[c[i]-1]=1;
25     }
26     int exm=0;
27     int jishu=0;
28     for(int j=0;j<len2;j++)
29     {
30         while(exm<len1)
31       {
32          if(a[exm]==b[j]&&mp[exm]==0){
33             jishu++;
34             exm++;
35             break;
36          }
37          else
38            exm++;
39       }
40     }
41     if(jishu==len2)
42         return true;
43     else
44         return false;
45 }
46 int main()
47 {
48     scanf("%s",a);
49     scanf("%s",b);
50     len1=strlen(a);
51     len2=strlen(b);
52     for(int i=0;i<len1;i++)
53          scanf("%d",&c[i]);
54     int l=0,r=len1-1,mid;
55     while(l<=r)
56     {
57         mid=(l+r)/2;
58         if(fun(mid))
59         {
60            l=mid+1;
61         }
62         else
63         {
64             r=mid-1;
65         }
66     }
67     printf("%d\n",r+1);
68     return 0;
69 }