Codeforces Round #436 (Div. 2)(ABCDE)

A. Fair Game

Petya and Vasya decided to play a game. They have n cards (n is an even number). A single integer is written on each card.

Before the game Petya will choose an integer and after that Vasya will choose another integer (different from the number that Petya chose). During the game each player takes all the cards with number he chose. For example, if Petya chose number 5 before the game he will take all cards on which 5 is written and if Vasya chose number 10 before the game he will take all cards on which 10 is written.

The game is considered fair if Petya and Vasya can take all n cards, and the number of cards each player gets is the same.

Determine whether Petya and Vasya can choose integer numbers before the game so that the game is fair.

Input

The first line contains a single integer n (2 ≤ n ≤ 100) — number of cards. It is guaranteed that n is an even number.

The following n lines contain a sequence of integers a1, a2, ..., an (one integer per line, 1 ≤ ai ≤ 100) — numbers written on the n cards.

Output

If it is impossible for Petya and Vasya to choose numbers in such a way that the game will be fair, print "NO" (without quotes) in the first line. In this case you should not print anything more.

In the other case print "YES" (without quotes) in the first line. In the second line print two distinct integers — number that Petya should choose and the number that Vasya should choose to make the game fair. If there are several solutions, print any of them.

Examples
Input
4
11
27
27
11
Output
YES
11 27
求是否有两个数并相同

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int a[110];
 4 map<int, int> mp;
 5 int main() {
 6     int n, x;
 7     cin >> n;
 8     for(int i = 1; i <= n; i ++) {
 9         cin >> a[i];
10         mp[a[i]] ++;
11     }
12     sort(a+1, a+1+n);
13     if(mp.size() == 2 && a[n/2] != a[n/2+1]) printf("Yes\n%d %d\n",a[1],a[n]);
14     else printf("No\n");
15     return 0;
16 }

 

 

B. Polycarp and Letters

Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s consisting only of lowercase and uppercase Latin letters.

Let A be a set of positions in the string. Let's call it pretty if following conditions are met:

  • letters on positions from A in the string are all distinct and lowercase;
  • there are no uppercase letters in the string which are situated between positions from A (i.e. there is no such j that s[j] is an uppercase letter, and a1 < j < a2 for some a1 and a2 from A).

Write a program that will determine the maximum number of elements in a pretty set of positions.

Input

The first line contains a single integer n (1 ≤ n ≤ 200) — length of string s.

The second line contains a string s consisting of lowercase and uppercase Latin letters.

Output

Print maximum number of elements in pretty set of positions for string s.

Examples
Input
11
aaaaBaabAbA
Output
2
求在大写字母之间最多有多少个不重复的小写字母。循环一下,暴力解决。
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 220;
 4 char str[N];
 5 int vis[30];
 6 int check() {
 7     int ans = 0;
 8     for(int i = 0; i < 26; i ++) {
 9         if(vis[i]) ans++;
10     }
11     return ans;
12 }
13 int main() {
14     int n, ans = 0;
15     cin >> n >>str;
16     memset(vis, 0, sizeof(vis));
17     for(int i = 0; i < n; i ++) {
18         if(str[i] >= 'a' && str[i] <= 'z') {
19             vis[str[i] - 'a'] ++;
20         } else {
21             int Max = check();
22             if(ans < Max) ans = Max;
23             memset(vis, 0, sizeof(vis));
24         }
25     }
26     int Max = check();
27     if(ans < Max) ans = Max;
28     printf("%d\n",ans);
29     return 0;
30 }

 

 

C. Bus

A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the point x = a, immediately turns back and then moves to the point x = 0. After returning to the point x = 0 it immediately goes back to the point x = a and so on. Thus, the bus moves from x = 0 to x = a and back. Moving from the point x = 0 to x = a or from the point x = a to x = 0 is called a bus journey. In total, the bus must make k journeys.

The petrol tank of the bus can hold b liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank.

There is a gas station in point x = f. This point is between points x = 0 and x = a. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain b liters of gasoline.

What is the minimum number of times the bus needs to refuel at the point x = f to make k journeys? The first journey starts in the point x = 0.

Input

The first line contains four integers a, b, f, k (0 < f < a ≤ 106, 1 ≤ b ≤ 109, 1 ≤ k ≤ 104) — the endpoint of the first bus journey, the capacity of the fuel tank of the bus, the point where the gas station is located, and the required number of journeys.

Output

Print the minimum number of times the bus needs to refuel to make k journeys. If it is impossible for the bus to make k journeys, print -1.

Examples
Input
6 9 2 4
Output
4


0-a之间反复往返,往返k次在x=f可以把油加满,求最少加多少次油,先考虑-1的情况 ,其它就都可以往返k次,然后循环,0——f——a 可以变成a——f——0每次在0或者a时判断下a+(a-f)或a+f油是否够了。
 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 
 5 int main() {
 6     int a, b, f, k, cnt = 0, ans = 0;
 7     cin >> a >> b >> f >> k;
 8     if(k == 1 && (f > b || (a-f) > b)) return 0*printf("-1\n");
 9     else if(k == 2 && (f > b || 2*(a-f) > b)) return 0*printf("-1\n");
10     else if(k >= 3 && (2*f > b || 2*(a-f) > b)) return 0*printf("-1\n");
11     for(cnt = b; k > 0; f = a - f, --k) {
12         if(f > b || (a-f) > b) return 0*printf("-1\n");
13         else if(cnt >= a + (k != 1 ?a-f:0)) cnt -= a;
14         else cnt = b - (a - f), ans ++;
15     }
16     printf("%d\n",ans);
17     return 0;
18 }

 

D. Make a Permutation!

Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.

Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.

Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.

In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.

Determine the array Ivan will obtain after performing all the changes.

Input

The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan's array.

The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array.

Output

In the first line print q — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.

Examples
Input
4
3 2 2 3
Output
2
1 2 4 3
Input
6
4 5 6 3 2 1
Output
0
4 5 6 3 2 1

把n个数变成1-n不同的n个数,使的字典序最小,所以只要改变那些出现次数超过2次的就可以了,并变成成没有出现过一次的数,当然改变前要考虑下改变后是否数字更大,如果更大的话第一个出现的就不改变,让后面的全部改变。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 2e5+10;
 4 int a[N], b[N];
 5 map<int,int> mp;
 6 set<int> st;
 7 int main()  {
 8     int n, ans = 0;
 9     scanf("%d", &n);
10     for(int i = 1; i <= n; i ++) {
11         scanf("%d", &a[i]);
12         mp[a[i]] ++;
13     }
14     int cnt = 1;
15     for(int i = 1; i <= n; i ++) {
16         if(mp[a[i]] == 1) {
17             b[i] = a[i];
18         } else {
19             while(mp.count(cnt)) cnt++;
20             if(cnt > a[i]) {
21                 if(st.count(a[i])) {
22                     b[i] = cnt;
23                     cnt++;
24                     ans++;
25                 } else {
26                     st.insert(a[i]);
27                     b[i] = a[i];
28                 }
29             } else {
30                 b[i] = cnt;
31                 mp[a[i]] --;
32                 cnt++;
33                 ans++;
34             }
35         }
36     }
37     printf("%d\n",ans);
38     for(int i = 1; i <= n; i ++) printf("%d%c",b[i],i==n?'\n':' ');
39     return 0;
40 }

 

E. Fire

Polycarp is in really serious trouble — his house is on fire! It's time to save the most valuable items. Polycarp estimated that it would take ti seconds to save i-th item. In addition, for each item, he estimated the value of di — the moment after which the item i will be completely burned and will no longer be valuable for him at all. In particular, if ti ≥ di, then i-th item cannot be saved.

Given the values pi for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item a first, and then item b, then the item a will be saved in ta seconds, and the item b — in ta + tb seconds after fire started.

Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of items in Polycarp's house.

Each of the following n lines contains three integers ti, di, pi (1 ≤ ti ≤ 20, 1 ≤ di ≤ 2 000, 1 ≤ pi ≤ 20) — the time needed to save the item i, the time after which the item i will burn completely and the value of item i.

Output

In the first line print the maximum possible total value of the set of saved items. In the second line print one integer m — the number of items in the desired set. In the third line print m distinct integers — numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them.

Examples
Input
3
3 7 4
2 6 5
3 7 6
Output
11
2
2 3


和今年百度之星资格赛的第四题很相似,直接写了,错在第三组数据,睡个午觉后发现是要排下序,从d最小的开始,这样就不会出错了。
01背包问题。
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 2e3+10;
 4 struct Nod{
 5     int t, d, p, id;
 6 }a[N];
 7 bool cmp(Nod a, Nod b) {
 8     return a.d < b.d;
 9 }
10 int dp[N];
11 vector<int> vs[N];
12 int main() {
13     int n;
14     cin >> n;
15     for(int i = 1; i <= n; i ++) {
16         scanf("%d %d %d", &a[i].t,&a[i].d,&a[i].p);
17         a[i].id = i;
18     }
19     sort(a+1, a+1+n, cmp);
20     // for(int i = 1; i <= n; i ++) {
21     //     printf("%d %d %d %d\n",a[i].t,a[i].d,a[i].p,a[i].id );
22     // }
23     for(int i = 1; i <= n; i ++) {
24         for(int j = a[i].d-1; j >= a[i].t; j --) {
25             if(dp[j-a[i].t] + a[i].p > dp[j]) {
26                 dp[j] = dp[j-a[i].t] + a[i].p;
27                 vs[j].clear();
28                 for(int ll = 0; ll < vs[j-a[i].t].size(); ll ++) {
29                     vs[j].push_back(vs[j-a[i].t][ll]);
30                 }
31                 vs[j].push_back(a[i].id);
32             }
33         }
34         // for(int j = 21; j <= 30; j ++)
35         // printf("__%d ",dp[j]);
36     }
37     int ans = 0, MAX = 0;
38     for(int i = 1; i <= 2000; i ++) {
39         if(MAX < dp[i]) {
40             MAX = dp[i];
41             ans = i;
42         }
43     }
44     printf("%d\n%d\n",MAX,vs[ans].size());
45     for(int i = 0; i < vs[ans].size(); i ++) printf("%d%c",vs[ans][i] , i == vs[ans].size()-1?'\n':' ');
46     return 0;
47 }

 

posted @ 2017-09-26 16:17  starry_sky  阅读(236)  评论(0编辑  收藏  举报