Codeforces Round #436 (Div. 2) ABCDEF

来源:互联网 发布:数据分析属于什么行业 编辑:程序博客网 时间:2024/06/05 02:17

水题场。。。


A. Fair Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 (differentfrom 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
411272711
output
YES11 27
input
266
output
NO
input
6102030201020
output
NO
input
6112233
output
NO
Note

In the first example the game will be fair if, for example, Petya chooses number 11, and Vasya chooses number 27. Then the will take all cards — Petya will take cards 1 and 4, and Vasya will take cards 2 and 3. Thus, each of them will take exactly two cards.

In the second example fair game is impossible because the numbers written on the cards are equal, but the numbers that Petya and Vasya should choose should be distinct.

In the third example it is impossible to take all cards. Petya and Vasya can take at most five cards — for example, Petya can choose number 10 and Vasya can choose number 20. But for the game to be fair it is necessary to take 6 cards.


A,水


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#include <iomanip>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=100005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);int a[maxn];int main() {int n,i,x;cin >> n;int k,l;mem0(a);for (i=1;i<=n;i++) {cin >> x;a[x]++;}k=l=-1;int sum=0;for (i=1;i<=100;i++) {if (a[i]) {sum++;if (k==-1) k=i; else l=i;}}if (sum==2) {if (a[k]==a[l]) cout << "YES" << "\n" << k << ' ' << l; else cout << "NO";} else cout << "NO";return 0;}

B. Polycarp and Letters
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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
11aaaaBaabAbA
output
2
input
12zACaAbbaazzC
output
3
input
3ABC
output
0
Note

In the first example the desired positions might be 6 and 8 or 7 and 8. Positions 6 and 7 contain letters 'a', position 8 contains letter 'b'. The pair of positions 1 and 8 is not suitable because there is an uppercase letter 'B' between these position.

In the second example desired positions can be 78 and 11. There are other ways to choose pretty set consisting of three elements.

In the third example the given string s does not contain any lowercase letters, so the answer is 0.



B,水


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#include <iomanip>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=100005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);char s[maxn];int p[129];int main() {int len,ans,i,cnt;scanf("%d",&len);scanf("%s",s+1);ans=cnt=0;for (i=1;i<=len;i++) {if (s[i]>='A'&&s[i]<='Z') {cnt=0;mem0(p);} else {if (p[s[i]]==0) {p[s[i]]=1;cnt++;}}ans=max(ans,cnt);}cout << ans;return 0;}

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

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 abfk (0 < f < a ≤ 1061 ≤ b ≤ 1091 ≤ 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
input
6 10 2 4
output
2
input
6 5 4 3
output
-1
Note

In the first example the bus needs to refuel during each journey.

In the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4 units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty.

In the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.




一个汽车在数轴上来回从0到a走k次,一个来回算两次。油箱有b升油,一升开一公里,加油站位置在f,求最少的充油次数。


数学题,求出每次加油之后最多能经过加油站 f 多少次,来回k次则最多加k次油,模拟K次即可判断。


一定要特判 k=1,2,3 !!!!!!


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#include <iomanip>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=20005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);ll a,b,f,k;ll t[maxn];int main() {cin >> a >> b >> f >> k;ll l,r,mid,p=-1,e;int i;l=1;r=1e9;while (l<=r) {mid=(l+r)/2;if (mid%2==1) {if ((mid-1)*a+f<=b) p=mid,l=mid+1; else r=mid-1;} else {if (mid*a-f<=b) p=mid,l=mid+1; else r=mid-1;}}if (b>=a*k) {cout << 0;return 0; }if (b<f||b<a-f||(b<2*(a-f)&&k>=2)||(b<2*f&&k>=3)) {cout << -1;return 0; }l=1;r=1e9;e=0;while (l<=r) {mid=(l+r)/2;if (mid*a*2<=b) e=mid,l=mid+1; else r=mid-1;}t[1]=p;if (t[1]>=k) {cout << 1; return 0;}if (t[1]%2==1) {if ((k-t[1])*a+a-f<=b) {cout << 1; return 0;}} else {if ((k-t[1])*a+f<=b) {cout << 1; return 0;}}for (i=2;i<=k;i++) {if (t[i-1]%2==1) {if (e*a*2+2*(a-f)<=b) t[i]=t[i-1]+e*2+1; else t[i]=t[i-1]+e*2;} else {if (e*a*2+2*f<=b) t[i]=t[i-1]+e*2+1; else t[i]=t[i-1]+e*2;}if (t[i]>=k) {cout << i;return 0;}if (t[i]%2==1) {if ((k-t[i])*a+a-f<=b) {cout << i; return 0;}} else {if ((k-t[i])*a+f<=b) {cout << i; return 0;}}}cout << -1;return 0;}

D. Make a Permutation!
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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
43 2 2 3
output
21 2 4 3 
input
64 5 6 3 2 1
output
04 5 6 3 2 1 
input
106 8 4 6 7 1 6 3 4 5
output
32 8 4 6 7 1 9 3 10 5 
Note

In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable.

In the second example Ivan does not need to change anything because his array already is a permutation.



D题,让你把一个长度为N的序列在最少的次数之内修改成n的全排列,在修改最少的前提下,输出字典序最小的解法。


很明显的一个贪心,出现一次的数字肯定不能改,我们只要把出现多次的改成没有出现过的。

对于这些位置,如果当前没有用过的最小的数字比当前数字小,或者当前数字已经被保留了至少一次,则替换掉,否则不替换。

若某个数字一共出现过n次,而之前n-1次出现都被替换,则最后一次必须不被替换。


复杂度O(n)


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#include <deque>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=200005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);int a[maxn],cnt[maxn],ch[maxn],st[maxn];int main() {int n,i,s,l;s=0;scanf("%d",&n);mem0(cnt);for (i=1;i<=n;i++) {scanf("%d",&a[i]);if (cnt[a[i]]!=0) s++; cnt[a[i]]++;ch[i]=st[i]=0;}printf("%d\n",s);queue<int> q;for (i=1;i<=n;i++) {if (cnt[i]==0) q.push(i);if (cnt[i]>1) ch[i]=1;}for (i=1;i<=n;i++) {if (ch[a[i]]) {cnt[a[i]]--;if (cnt[a[i]]==0&&!st[a[i]]) continue;if (a[i]>q.front()||st[a[i]]) {a[i]=q.front();q.pop();} else {st[a[i]]=1;}}}for (i=1;i<=n;i++) {printf("%d ",a[i]);}return 0;}

E. Fire
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 + tbseconds 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 ≤ 201 ≤ di ≤ 2 0001 ≤ 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
33 7 42 6 53 7 6
output
1122 3 
input
25 6 13 3 5
output
111 
Note

In the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2 seconds. Thus, the total value of the saved items will be 6 + 5 = 11.

In the second example Polycarp can save only the first item, since even if he immediately starts saving the second item, he can save it in 3 seconds, but this item will already be completely burned by this time.



E题让你求个01背包并输出路径顺序,状态压缩即可。详见  这里



F. Cities Excursions
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n cities in Berland. Some pairs of them are connected with m directed roads. One can use only these roads to move from one city to another. There are no roads that connect a city to itself. For each pair of cities (x, y) there is at most one road from x to y.

A path from city s to city t is a sequence of cities p1p2, ... , pk, where p1 = spk = t, and there is a road from city pi to city pi + 1 for each i from 1 to k - 1. The path can pass multiple times through each city except t. It can't pass through t more than once.

A path p from s to t is ideal if it is the lexicographically minimal such path. In other words, p is ideal path from s to t if for any other path q from s to t pi < qi, where i is the minimum integer such that pi ≠ qi.

There is a tourist agency in the country that offers q unusual excursions: the j-th excursion starts at city sjand ends in city tj.

For each pair sjtj help the agency to study the ideal path from sj to tj. Note that it is possible that there is no ideal path from sj to tj. This is possible due to two reasons:

  • there is no path from sj to tj;
  • there are paths from sj to tj, but for every such path p there is another path q from sj to tj, such that pi > qi, where i is the minimum integer for which pi ≠ qi.

The agency would like to know for the ideal path from sj to tj the kj-th city in that path (on the way from sjto tj).

For each triple sjtjkj (1 ≤ j ≤ q) find if there is an ideal path from sj to tj and print the kj-th city in that path, if there is any.

Input

The first line contains three integers nm and q (2 ≤ n ≤ 3000,0 ≤ m ≤ 30001 ≤ q ≤ 4·105) — the number of cities, the number of roads and the number of excursions.

Each of the next m lines contains two integers xi and yi (1 ≤ xi, yi ≤ nxi ≠ yi), denoting that the i-th road goes from city xi to city yi. All roads are one-directional. There can't be more than one road in each direction between two cities.

Each of the next q lines contains three integers sjtj and kj (1 ≤ sj, tj ≤ nsj ≠ tj1 ≤ kj ≤ 3000).

Output

In the j-th line print the city that is the kj-th in the ideal path from sj to tj. If there is no ideal path from sj to tj, or the integer kj is greater than the length of this path, print the string '-1' (without quotes) in the j-th line.

Example
input
7 7 51 22 31 33 44 55 34 61 4 22 6 11 7 31 3 21 3 5
output
2-1-12-1

对tarjan进行改造。详见 这里

官方题解用了一个叫做outgoing tree的东西,有时间去学学。

原创粉丝点击