Codeforces Round #464 (Div. 2) ABCDE

A. Love Triangle

As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1 ≤ fi ≤ n and fi ≠ i.

We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.

Input

The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of planes.

The second line contains n integers f1, f2, ..., fn (1 ≤ fi ≤ nfi ≠ i), meaning that the i-th plane likes the fi-th.

Output

Output «YES» if there is a love triangle consisting of planes on Earth. Otherwise, output «NO».

You can output any letter in lower case or in upper case.

Examples
input
Copy
5
2 4 5 1 3
output
Copy
YES
input
Copy
5
5 5 5 5 1
output
Copy
NO
Note

In first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.

In second example there are no love triangles.

是否存在三角关系,简单题。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 5e3+10;
 5 int f[N];
 6 int main() {
 7     int n;
 8     cin >> n;
 9     for(int i = 1; i <= n; i ++) cin >> f[i];
10     for(int i = 1; i <= n; i ++) {
11         if(i == f[f[f[i]]]) return 0*printf("YES\n");
12     }
13     printf("NO\n");
14     return 0;
15 }

B. Hamster Farm

Dima has a hamsters farm. Soon N hamsters will grow up on it and Dima will sell them in a city nearby.

Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters.

Dima can buy boxes at a factory. The factory produces boxes of K kinds, boxes of the i-th kind can contain in themselves ai hamsters. Dima can buy any amount of boxes, but he should buy boxes of only one kind to get a wholesale discount.

Of course, Dima would buy boxes in such a way that each box can be completely filled with hamsters and transported to the city. If there is no place for some hamsters, Dima will leave them on the farm.

Find out how many boxes and of which type should Dima buy to transport maximum number of hamsters.

Input

The first line contains two integers N and K (0 ≤ N ≤ 1018, 1 ≤ K ≤ 105) — the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces.

The second line contains K integers a1, a2, ..., aK (1 ≤ ai ≤ 1018 for all i) — the capacities of boxes.

Output

Output two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to K in the order they are given in input.

If there are many correct answers, output any of them.

Examples
input
Copy
19 3
5 4 10
output
Copy
2 4
input
Copy
28 3
5 6 30
output
Copy
1 5

 求选择哪种类型的箱子可以放最多的仓鼠,依次遍历下就知道了。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 1e5+10;
 5 ll n, k, a[N];
 6 int main() {
 7     cin >> n >> k;
 8     for(int i = 1; i <= k; i ++) cin >> a[i];
 9     ll id, MIN = 1e18;
10     for(int i = 1; i <= k; i ++) {
11         if(n-n/a[i]*a[i] < MIN) {
12             id = i;
13             MIN = n-n/a[i]*a[i];
14         }
15     }
16     cout << id << ' ' << n/a[id] << endl;
17     return 0;
18 }

C. Convenient For Everybody

In distant future on Earth day lasts for n hours and that's why there are n timezones. Local times in adjacent timezones differ by one hour. For describing local time, hours numbers from 1 to n are used, i.e. there is no time "0 hours", instead of it "n hours" is used. When local time in the 1-st timezone is 1 hour, local time in the i-th timezone is i hours.

Some online programming contests platform wants to conduct a contest that lasts for an hour in such a way that its beginning coincides with beginning of some hour (in all time zones). The platform knows, that there are ai people from i-th timezone who want to participate in the contest. Each person will participate if and only if the contest starts no earlier than s hours 00 minutes local time and ends not later than f hours 00 minutes local time. Values s and f are equal for all time zones. If the contest starts at f hours 00 minutes local time, the person won't participate in it.

Help platform select such an hour, that the number of people who will participate in the contest is maximum.

Input

The first line contains a single integer n (2 ≤ n ≤ 100 000) — the number of hours in day.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 10 000), where ai is the number of people in the i-th timezone who want to participate in the contest.

The third line contains two space-separated integers s and f (1 ≤ s < f ≤ n).

Output

Output a single integer — the time of the beginning of the contest (in the first timezone local time), such that the number of participants will be maximum possible. If there are many answers, output the smallest among them.

Examples
input
Copy
3
1 2 3
1 3
output
Copy
3
input
Copy
5
1 2 3 4 1
1 3
output
Copy
4
Note

In the first example, it's optimal to start competition at 3 hours (in first timezone). In this case, it will be 1 hour in the second timezone and 2hours in the third timezone. Only one person from the first timezone won't participate.

In second example only people from the third and the fourth timezones will participate.

 有n个时区,相邻两个时区,比赛在s点开始,f点结束,每个时区都有自己的时间,按第一时区计算,在哪个点开始比赛参加人数最多。

找到f-s长度的区间,使的和最大,那么第一个位置就是他所在时区的s点,转换成第一时区的时间就行。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 1e5+10;
 5 ll a[N], MAX = -1;
 6 int n, s, f, ans;
 7 int main() {
 8     cin >> n;
 9     for(int i = 1; i <= n; i ++) {
10         cin >> a[i];
11         a[i] += a[i-1];
12     }
13     cin >> s >> f;
14     int tmp = f-s;
15     for(int i = 1; i <= n; i ++) {
16         if(i+tmp-1 <= n) {
17             if(a[i+tmp-1]-a[i-1] > MAX) {
18                 MAX = a[i+tmp-1]-a[i-1];
19                 ans = s - i + 1;
20                 if(ans <= 0) ans += n;
21             } else if(a[i+tmp-1]-a[i-1] == MAX) {
22                 int ans1 = s - i + 1;
23                 if(ans1 <= 0) ans1 += n;
24                 ans = min(ans, ans1);
25             }
26 
27         } else{
28             if(a[n]-a[i-1]+a[tmp-(n-i+1)] > MAX) {
29                 MAX = a[n]-a[i-1]+a[tmp-(n-i+1)];
30                 ans = s - i + 1;
31                 if(ans <= 0) ans += n;
32             } else if(a[n]-a[i-1]+a[tmp-(n-i+1)] == MAX) {
33                 int ans1 = s - i + 1;
34                 if(ans1 <= 0) ans1 += n;
35                 ans = min(ans, ans1);
36             }
37         }
38     }
39     cout << ans << endl;
40     return 0;
41 }

D. Love Rescue

Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.

This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.

More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.

The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.

The third line contains the lettering on Tolya's t-shirt in the same format.

Output

In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.

In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.

If there are many optimal answers, output any.

Examples
input
Copy
3
abb
dad
output
Copy
2
a d
b a
input
Copy
8
drpepper
cocacola
output
Copy
7
l e
e d
d c
c p
p o
o r
r a
Note

In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.

 把两个字符串变成相同的字符串购买多少魔法。

并查集问题。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1e5+10;
 4 char str1[N], str2[N];
 5 int fa[30], ll[30], rr[30];
 6 int find(int x) {
 7     return fa[x] == x ? x: find(fa[x]);
 8 }
 9 int main() {
10     int n, ans = 0;
11     for(int i = 0; i < 26; i ++) fa[i] = i;
12     cin >> n >> str1 >> str2;
13     for(int i = 0; i < n; i ++) {
14         int l = str1[i]-'a', r = str2[i] - 'a';
15         l = find(l), r = find(r);
16         if(l != r) {
17             ans ++;
18             fa[l] = r;
19             ll[ans] = l;
20             rr[ans] = r;
21         }
22     }
23     cout << ans << endl;
24     for(int i = 1; i <= ans; i ++) {
25         printf("%c %c\n",ll[i]+'a',rr[i]+'a');
26     }
27     return 0;
28 }

E. Maximize!

You are given a multiset S consisting of positive integers (initially empty). There are two kind of queries:

  1. Add a positive integer to S, the newly added integer is not less than any number in it.
  2. Find a subset s of the set S such that the value  is maximum possible. Here max(s) means maximum value of elements in s — the average value of numbers in s. Output this maximum possible value of .
Input

The first line contains a single integer Q (1 ≤ Q ≤ 5·105) — the number of queries.

Each of the next Q lines contains a description of query. For queries of type 1 two integers 1 and x are given, where x (1 ≤ x ≤ 109) is a number that you should add to S. It's guaranteed that x is not less than any number in S. For queries of type 2, a single integer 2 is given.

It's guaranteed that the first query has type 1, i. e. S is not empty when a query of type 2 comes.

Output

Output the answer for each query of the second type in the order these queries are given in input. Each number should be printed in separate line.

Your answer is considered correct, if each of your answers has absolute or relative error not greater than 10 - 6.

Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .

Examples
input
Copy
6
1 3
2
1 4
2
1 8
2
output
Copy
0.0000000000
0.5000000000
3.0000000000
input
Copy
4
1 1
1 4
1 5
2
output
Copy
2.0000000000

 首先是空集,1表示往集合添加x,否则查询max(s)-mean(s)的最大值,s是S的一个子集。

因为每次添加的值都是最大的。所以查询时最大值一定去最近添加进去的,平均值就请最小的那些,所以是去前k个数。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 5e5+10;
 5 ll a[N], sum[N];
 6 int main() {
 7     int q, n = 0, t, k = 1;
 8     cin >> q;
 9     while(q--) {
10         cin >> t;
11         if(t == 1) {
12             cin >> a[++n];
13             sum[n] = sum[n-1] + a[n];
14         }else {
15             while(k<n && (a[n]+sum[k+1])*(k+1)<(k+2)*(a[n]+sum[k])) k++;
16             printf("%.10lf\n",a[n]-1.0*(a[n]+sum[k])/(k+1));
17         }
18     }
19     return 0;
20 }

猜你喜欢

转载自www.cnblogs.com/xingkongyihao/p/8955375.html