Educational Codeforces Round 35 (Rated for Div. 2)

A. Nearest Minimums

You are given an array of n integer numbers a0, a1, ..., an - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times.

Input

The first line contains positive integer n (2 ≤ n ≤ 105) — size of the given array. The second line contains n integers a0, a1, ..., an - 1 (1 ≤ ai ≤ 109) — elements of the array. It is guaranteed that in the array a minimum occurs at least two times.

Output

Print the only number — distance between two nearest minimums in the array.

Examples
Input
2
3 3
Output
1
Input
3
5 6 5
Output
2
Input
9
2 1 3 5 4 1 2 3 1
Output
3
暴力一波
 1 // ConsoleApplication2.cpp: 定义控制台应用程序的入口点。
 2 //
 3 
 4 //#include "stdafx.h"
 5 #include<cmath>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<iostream>
 9 #include<algorithm>
10 using namespace std;
11 
12 const int INF = 0x3f3f3f3f;
13 const int maxn = (int)3e5;
14 
15 int n;
16 struct node {
17     int num, id;
18     bool operator<(const node& i)const {
19         if (num == i.num) return id < i.id;
20         return num < i.num;
21     }
22 }a[maxn];
23 
24 int main()
25 {
26     cin >> n;
27     for (int i = 1; i <= n; i++) {
28         int temp;
29         cin >> temp;
30         a[i].id = i;
31         a[i].num = temp;
32     }
33     sort(a + 1, a + n + 1);
34     int ans = INF;
35     if (a[1].num == a[2].num) {
36         for (int i = 2; i <= n; i++) {
37             if (a[i].num != a[1].num) break;
38             else ans = min(ans,abs(a[i].id - a[i - 1].id));
39         }
40     }
41     else {
42         for (int i = 2; i <= n; i++) {
43             if (a[i].num != a[2].num) break;
44             else ans = min(ans,abs(a[i].id - a[1].id));
45         }
46     }
47     cout << ans << endl;
48     return 0;
49 }

B. Two Cakes

It's New Year's Eve soon, so Ivan decided it's high time he started setting the table. Ivan has bought two cakes and cut them into pieces: the first cake has been cut into a pieces, and the second one — into b pieces.

Ivan knows that there will be n people at the celebration (including himself), so Ivan has set n plates for the cakes. Now he is thinking about how to distribute the cakes between the plates. Ivan wants to do it in such a way that all following conditions are met:

  1. Each piece of each cake is put on some plate;
  2. Each plate contains at least one piece of cake;
  3. No plate contains pieces of both cakes.

To make his guests happy, Ivan wants to distribute the cakes in such a way that the minimum number of pieces on the plate is maximized. Formally, Ivan wants to know the maximum possible number x such that he can distribute the cakes according to the aforementioned conditions, and each plate will contain at least x pieces of cake.

Help Ivan to calculate this number x!

Input

The first line contains three integers n, a and b (1 ≤ a, b ≤ 100, 2 ≤ n ≤ a + b) — the number of plates, the number of pieces of the first cake, and the number of pieces of the second cake, respectively.

Output

Print the maximum possible number x such that Ivan can distribute the cake in such a way that each plate will contain at least x pieces of cake.

Examples
Input
5 2 3
Output
1
Input
4 7 10
Output
3
Note

In the first example there is only one way to distribute cakes to plates, all of them will have 1 cake on it.

In the second example you can have two plates with 3 and 4 pieces of the first cake and two plates both with 5 pieces of the second cake. Minimal number of pieces is 3.

题解:列两个等式,假设a被分成x块,b被分成y块,最小的数为Xmin,则x*Xmin<=a,y*Xmin<=b。所以可知:Xmin=min(a/x,b/y)(x+y=n),剩下的是枚举块数x。

 1 // ConsoleApplication2.cpp: 定义控制台应用程序的入口点。
 2 //
 3 
 4 //#include "stdafx.h"
 5 #include<cmath>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<iostream>
 9 #include<algorithm>
10 using namespace std;
11 
12 const int INF = 0x3f3f3f3f;
13 const int maxn = (int)3e5;
14 
15 int n, a, b;
16 
17 int main()
18 {
19     while (cin >> n >> a >> b) {
20         int ans = 0;
21         if (n == a + b) ans = 1;
22         else {
23             for (int i = 1; i < n; i++) ans = max(ans, min(a / i, b / (n - i)));
24         }
25         cout << ans << endl;
26     }
27 }

C. Three Garlands

Mishka is decorating the Christmas tree. He has got three garlands, and all of them will be put on the tree. After that Mishka will switch these garlands on.

When a garland is switched on, it periodically changes its state — sometimes it is lit, sometimes not. Formally, if i-th garland is switched on during x-th second, then it is lit only during seconds x, x + ki, x + 2ki, x + 3ki and so on.

Mishka wants to switch on the garlands in such a way that during each second after switching the garlands on there would be at least one lit garland. Formally, Mishka wants to choose three integers x1, x2 and x3 (not necessarily distinct) so that he will switch on the first garland during x1-th second, the second one — during x2-th second, and the third one — during x3-th second, respectively, and during each second starting from max(x1, x2, x3) at least one garland will be lit.

Help Mishka by telling him if it is possible to do this!

Input

The first line contains three integers k1, k2 and k3 (1 ≤ ki ≤ 1500) — time intervals of the garlands.

Output

If Mishka can choose moments of time to switch on the garlands in such a way that each second after switching the garlands on at least one garland will be lit, print YES.

Otherwise, print NO.

Examples
Input
2 2 3
Output
YES
Input
4 2 3
Output
NO
Note

In the first example Mishka can choose x1 = 1, x2 = 2, x3 = 1. The first garland will be lit during seconds 1, 3, 5, 7, ..., the second — 2, 4, 6, 8, ..., which already cover all the seconds after the 2-nd one. It doesn't even matter what x3 is chosen. Our choice will lead third to be lit during seconds 1, 4, 7, 10, ..., though.

In the second example there is no way to choose such moments of time, there always be some seconds when no garland is lit.

题解:靠一种感觉,只有数字1,2,3有用。cnt_1>0||cnt_2>1||cnt_3=3。但是还有一个4、4、2也是符合的。这组值我遗漏了!

 1 // ConsoleApplication2.cpp: 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include<cmath>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<iostream>
 9 #include<algorithm>
10 using namespace std;
11 
12 const int INF = 0x3f3f3f3f;
13 const int maxn = (int)3e5;
14 
15 int main()
16 {
17     int d[3], cnt1 = 0, cnt2 = 0, cnt3 = 0;
18     for (int i = 0; i < 3; i++) cin >> d[i];
19     for (int i = 0; i < 3; i++) {
20         if (d[i] == 2) cnt1++;
21         if (d[i] == 3) cnt2++;
22         if (d[i] == 1) cnt3++;
23     }
24     sort(d, d + 3);
25     if (d[0] == 2 && d[1] == 4 && d[2] == 4) cout << "YES" << endl;
26     else if (cnt1 >= 2 || cnt2 == 3 || cnt3 > 0) cout << "YES" << endl;
27     else cout << "NO" << endl;
28 }

 D. Inversion Counting

A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i, j) such that i > j and ai < aj. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3).

You are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2, r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].

After each query you have to determine whether the number of inversions is odd or even.

Input

The first line contains one integer n (1 ≤ n ≤ 1500) — the size of the permutation.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of the permutation. These integers are pairwise distinct.

The third line contains one integer m (1 ≤ m ≤ 2·105) — the number of queries to process.

Then m lines follow, i-th line containing two integers li, ri (1 ≤ li ≤ ri ≤ n) denoting that i-th query is to reverse a segment [li, ri] of the permutation. All queries are performed one after another.

Output

Print m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and even otherwise.

Examples
Input
3
1 2 3
2
1 2
2 3
Output
odd
even
Input
4
1 2 4 3
4
1 1
1 4
1 4
2 3
Output
odd
odd
odd
even
Note

The first example:

  1. after the first query a = [2, 1, 3], inversion: (2, 1);
  2. after the second query a = [2, 3, 1], inversions: (3, 1), (3, 2).

题解:学过线性代数的童鞋是否记得一个定理:一个排列中,任意两个元素对换,排列改变奇偶性。具体证明我就不说了。这题完全是这个定理的模板题啊。。。

 1 // ConsoleApplication2.cpp: 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include<cmath>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<iostream>
 9 #include<algorithm>
10 using namespace std;
11 
12 const int INF = 0x3f3f3f3f;
13 const int maxn = 2000;
14 
15 int n, m;
16 int a[maxn];
17 
18 int main()
19 {
20     while (cin >> n) {
21         int cnt = 0, ans = 0;
22         for (int i = 0; i < n; i++) cin >> a[i];
23         for (int i = 1; i < n; i++)
24             for (int j = 0; j < i; j++) if (a[j] > a[i]) cnt++;
25         bool flag = false;
26         if (cnt % 2) flag = true;
27 
28         cin >> m;
29         while(m--){
30             int l, r;
31             cin >> l >> r;
32             ans += (r - l + 1)/2;
33             //cout << ans << endl;
34             if (ans % 2) {
35                 if (flag) cout << "even" << endl;
36                 else cout << "odd" << endl;
37             }
38             else {
39                 if (flag) cout << "odd" << endl;
40                 else cout << "even" << endl;
41             }
42         }
43     }
44     
45 }

 

posted @ 2017-12-29 23:14  天之道,利而不害  阅读(475)  评论(0编辑  收藏  举报