Codeforces Round #495 (Div. 2) ABCD

A. Sonya and Hotels

Sonya decided that having her own hotel business is the best way of earning money because she can profit and rest wherever she wants.

The country where Sonya lives is an endless line. There is a city in each integer coordinate on this line. She has nn hotels, where the ii-th hotel is located in the city with coordinate xixi. Sonya is a smart girl, so she does not open two or more hotels in the same city.

Sonya understands that her business needs to be expanded by opening new hotels, so she decides to build one more. She wants to make the minimum distance from this hotel to all others to be equal to dd. The girl understands that there are many possible locations to construct such a hotel. Thus she wants to know the number of possible coordinates of the cities where she can build a new hotel.

Because Sonya is lounging in a jacuzzi in one of her hotels, she is asking you to find the number of cities where she can build a new hotel so that the minimum distance from the original nn hotels to the new one is equal to dd.

Input

The first line contains two integers nn and dd (1n1001≤n≤100, 1d1091≤d≤109) — the number of Sonya's hotels and the needed minimum distance from a new hotel to all others.

The second line contains nn different integers in strictly increasing order x1,x2,,xnx1,x2,…,xn (109xi109−109≤xi≤109) — coordinates of Sonya's hotels.

Output

Print the number of cities where Sonya can build a new hotel so that the minimum distance from this hotel to all others is equal to dd.

Examples
input
Copy
4 3
-3 2 9 16
output
Copy
6
input
Copy
5 2
4 8 11 18 19
output
Copy
5
Note

In the first example, there are 66 possible cities where Sonya can build a hotel. These cities have coordinates 6−6, 55, 66, 1212, 1313, and 1919.

In the second example, there are 55 possible cities where Sonya can build a hotel. These cities have coordinates 22, 66, 1313, 1616, and 2121.

求有多少个点与已知的点最小的距离是3

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 110;
 4 int a[N], n, x, d;
 5 int main() {
 6     cin >> n >> d;
 7     for(int i = 0; i < n; i ++) cin >> a[i];
 8     sort(a,a+n);
 9     int ans = 2;
10     for(int i = 0; i < n-1; i ++) {
11         if(a[i+1]-a[i]-2*d > 0) ans += 2;
12         else if(a[i+1]-a[i]-2*d == 0) ans++;
13     }
14     cout << ans << endl;
15     return 0;
16 }

B. Sonya and Exhibition

Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.

There are nn flowers in a row in the exhibition. Sonya can put either a rose or a lily in the ii-th position. Thus each of nn positions should contain exactly one flower: a rose or a lily.

She knows that exactly mm people will visit this exhibition. The ii-th visitor will visit all flowers from lili to riri inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.

Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.

Input

The first line contains two integers nn and mm (1n,m1031≤n,m≤103) — the number of flowers and visitors respectively.

Each of the next mm lines contains two integers lili and riri (1lirin1≤li≤ri≤n), meaning that ii-th visitor will visit all flowers from lili to ririinclusive.

Output

Print the string of nn characters. The ii-th symbol should be «0» if you want to put a rose in the ii-th position, otherwise «1» if you want to put a lily.

If there are multiple answers, print any.

Examples
input
Copy
5 3
1 3
2 4
2 5
output
Copy
01100
input
Copy
6 3
5 6
1 4
4 6
output
Copy
110010
Note

In the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions;

  • in the segment [13][1…3], there are one rose and two lilies, so the beauty is equal to 12=21⋅2=2;
  • in the segment [24][2…4], there are one rose and two lilies, so the beauty is equal to 12=21⋅2=2;
  • in the segment [25][2…5], there are two roses and two lilies, so the beauty is equal to 22=42⋅2=4.

The total beauty is equal to 2+2+4=82+2+4=8.

In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions;

  • in the segment [56][5…6], there are one rose and one lily, so the beauty is equal to 11=11⋅1=1;
  • in the segment [14][1…4], there are two roses and two lilies, so the beauty is equal to 22=42⋅2=4;
  • in the segment [46][4…6], there are two roses and one lily, so the beauty is equal to 21=22⋅1=2.

The total beauty is equal to 1+4+2=71+4+2=7.

有m个区间,每个区间的0的个数乘以1的个数,求总和最大时,这个区间是怎么排列的。

全是010101...这样的排列就行。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main() {
 5     int n, m, l, r;
 6     cin >> n >> m;
 7     for(int i = 1; i <= m; i ++) {
 8         cin >> l >> r;
 9     }
10     for(int i = 0; i < n; i ++) {
11         if(i&1)printf("0");
12         else printf("1");
13     }
14     printf("\n");
15     return 0;
16 }

C. Sonya and Robots

Since Sonya is interested in robotics too, she decided to construct robots that will read and recognize numbers.

Sonya has drawn nn numbers in a row, aiai is located in the ii-th position. She also has put a robot at each end of the row (to the left of the first number and to the right of the last number). Sonya will give a number to each robot (they can be either same or different) and run them. When a robot is running, it is moving toward to another robot, reading numbers in the row. When a robot is reading a number that is equal to the number that was given to that robot, it will turn off and stay in the same position.

Sonya does not want robots to break, so she will give such numbers that robots will stop before they meet. That is, the girl wants them to stop at different positions so that the first robot is to the left of the second one.

For example, if the numbers [1,5,4,1,3][1,5,4,1,3] are written, and Sonya gives the number 11 to the first robot and the number 44 to the second one, the first robot will stop in the 11-st position while the second one in the 33-rd position. In that case, robots will not meet each other. As a result, robots will not be broken. But if Sonya gives the number 44 to the first robot and the number 55 to the second one, they will meet since the first robot will stop in the 33-rd position while the second one is in the 22-nd position.

Sonya understands that it does not make sense to give a number that is not written in the row because a robot will not find this number and will meet the other robot.

Sonya is now interested in finding the number of different pairs that she can give to robots so that they will not meet. In other words, she wants to know the number of pairs (pp, qq), where she will give pp to the first robot and qq to the second one. Pairs (pipi, qiqi) and (pjpj, qjqj) are different if pipjpi≠pj or qiqjqi≠qj.

Unfortunately, Sonya is busy fixing robots that broke after a failed launch. That is why she is asking you to find the number of pairs that she can give to robots so that they will not meet.

Input

The first line contains a single integer nn (1n1051≤n≤105) — the number of numbers in a row.

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1051≤ai≤105) — the numbers in a row.

Output

Print one number — the number of possible pairs that Sonya can give to robots so that they will not meet.

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

In the first example, Sonya can give pairs (11, 11), (11, 33), (11, 44), (11, 55), (44, 11), (44, 33), (55, 11), (55, 33), and (55, 44).

In the second example, Sonya can give pairs (11, 11), (11, 22), (11, 33), (22, 11), (22, 22), (22, 33), and (33, 22).

求有多少个p,q  使的从左边遇见第一个p,从右边遇见第一个q没有交叉。

从左到右遍历一遍,当前这个数没有使用时就计算下右边有多少个不重复的数就行。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 1e5+10;
 5 int a[N], n;
 6 map<int,int> mp;
 7 bool vis[N];
 8 int main() {
 9     cin >> n;
10     int cnt = 0;
11     for(int i = 0; i < n; i ++) {
12         cin >> a[i];
13         if(!mp.count(a[i])) {
14             cnt ++;
15         }
16         mp[a[i]] ++;
17     }
18     ll ans = 0;
19     for(int i = 0; i < n; i ++) {
20         mp[a[i]]--;
21         if(mp[a[i]] == 0) cnt--;
22         if(!vis[a[i]]) {
23             ans += 1LL*cnt;
24             vis[a[i]] = 1;
25         }
26     }
27     cout << ans << endl;
28     return 0;
29 }

D. Sonya and Matrix

Since Sonya has just learned the basics of matrices, she decided to play with them a little bit.

Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so.

The Manhattan distance between two cells (x1x1, y1y1) and (x2x2, y2y2) is defined as |x1x2|+|y1y2||x1−x2|+|y1−y2|. For example, the Manhattan distance between the cells (5,2)(5,2) and (7,1)(7,1) equals to |57|+|21|=3|5−7|+|2−1|=3.

Example of a rhombic matrix.

Note that rhombic matrices are uniquely defined by nn, mm, and the coordinates of the cell containing the zero.

She drew a n×mn×m rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of nmn⋅m numbers). Note that Sonya will not give you nn and mm, so only the sequence of numbers in this matrix will be at your disposal.

Write a program that finds such an n×mn×m rhombic matrix whose elements are the same as the elements in the sequence in some order.

Input

The first line contains a single integer tt (1t1061≤t≤106) — the number of cells in the matrix.

The second line contains tt integers a1,a2,,ata1,a2,…,at (0ai<t0≤ai<t) — the values in the cells in arbitrary order.

Output

In the first line, print two positive integers nn and mm (n×m=tn×m=t) — the size of the matrix.

In the second line, print two integers xx and yy (1xn1≤x≤n, 1ym1≤y≤m) — the row number and the column number where the cell with 00 is located.

If there are multiple possible answers, print any of them. If there is no solution, print the single integer 1−1.

Examples
input
Copy
20
1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4
output
Copy
4 5
2 2
input
Copy
18
2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1
output
Copy
3 6
2 3
input
Copy
6
2 1 0 2 1 2
output
Copy
-1
Note

You can see the solution to the first example in the legend. You also can choose the cell (2,2)(2,2) for the cell where 00 is located. You also can choose a 5×45×4 matrix with zero at (4,2)(4,2).

In the second example, there is a 3×63×6 matrix, where the zero is located at (2,3)(2,3) there.

In the third example, a solution does not exist.

给定n个数,求是否存在n*m的矩阵,使的给定的条件成立,并求出0的坐标。一个矩阵,旋转180还是类似的。

所以假设0的坐标为(x , y),到(1, 1)的距离是a, 到(n , m)的距离是b,其中b是给定的最大的数字。并且有:

x-1+y-1 = a      n-x+m-y = b

所以有n+m = a + b + 2

并且可以得到y = n+m-b-x

b和x是已知的,然后遍历所以存在的n,m就行。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1e6+10;
 4 int a[N], n, x, c[N], t;
 5 int main() {
 6     cin >> t;
 7     int b = 0, m, xx, y;
 8     for(int i = 0; i < t; i ++) {
 9         scanf("%d", &x);
10         a[x]++;
11         b = max(b, x);
12     }
13     xx = 1;
14     for(int i = 1; a[i] ; i ++) {
15         if(a[i] != 4*i) {
16             xx = i;
17             break;
18         }
19     }
20     for(int n = 1; n <= t; n ++) {
21         if(t%n == 0) {
22             memset(c, 0, sizeof(c));
23             m = t/n;
24             y = n+m-b-xx;
25             for(int i = 1; i <= n; i ++) {
26                 for(int j = 1; j <= m; j ++) {
27                     c[abs(i-xx)+abs(j-y)]++;
28                 }
29             }
30             bool flag = 1;
31             for(int i = 0; i <= b; i ++) {
32                 if(a[i] != c[i]) {
33                     flag = 0;
34                     break;
35                 }
36             }
37             if(flag) {
38                 return 0*printf("%d %d\n%d %d\n",n,m,xx,y);
39             }
40         }
41     }
42     printf("-1\n");
43     return 0;
44 }

 

 

 

posted @ 2018-07-13 14:24  starry_sky  阅读(321)  评论(0编辑  收藏  举报