Codeforces Round #134 (Div. 2)

A. Mountain Scenery
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Bolek has found a picture with n mountain peaks painted on it. The n painted peaks are represented by a non-closed polyline, consisting of 2n segments. The segments go through 2n + 1 points with coordinates (1, y1), (2, y2), ..., (2n + 1, y2n + 1), with the i-th segment connecting the point (i, yi) and the point (i + 1, yi + 1). For any even i (2 ≤ i ≤ 2n) the following condition holds: yi - 1 < yi and yi > yi + 1.

We shall call a vertex of a polyline with an even x coordinate a mountain peak.

The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, k = 2.

 

Bolek fancied a little mischief. He chose exactly k mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the y coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1, r1), (2, r2), ..., (2n + 1, r2n + 1).

Given Bolek's final picture, restore the initial one.

Input

The first line contains two space-separated integers n and k (1 ≤ k ≤ n ≤ 100). The next line contains 2n + 1 space-separated integers r1, r2, ..., r2n + 1 (0 ≤ ri ≤ 100) — the y coordinates of the polyline vertices on Bolek's picture.

It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks.

Output

Print 2n + 1 integers y1, y2, ..., y2n + 1 — the y coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them.

Sample test(s)
Input
3 2 0 5 3 5 1 5 2
Output
0 5 3 4 1 4 2 
Input
1 1 0 2 0
Output
0 1 0 

这题马虎了一下 边输边判的 忘记a[i+1]还没输进去。。
View Code
 1 #include <iostream>
 2 #include<cstdio>
 3 #include<string.h>
 4 using namespace std;
 5 int a[301];
 6 int main()
 7 {
 8     int i,j,k,n;
 9     scanf("%d %d",&n,&k);
10     for(i = 1; i <= 2*n+1 ; i++)
11     {
12         scanf("%d", &a[i]);
13     }
14     for(i = 1; i <= 2*n+1 ; i++)
15     {
16         if(i%2==0&&k&&((a[i]-1)>a[i-1]&&(a[i]-1)>a[i+1]))
17         {
18             a[i]--;
19             k--;
20         }
21     }
22     for(i = 1; i <= 2*n ;i++)
23     printf("%d ",a[i]);
24     printf("%d\n",a[2*n+1]);
25     return 0;
26 }
B. Airport
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Lolek and Bolek are about to travel abroad by plane. The local airport has a special "Choose Your Plane" offer. The offer's conditions are as follows:

  • it is up to a passenger to choose a plane to fly on;
  • if the chosen plane has x (x > 0) empty seats at the given moment, then the ticket for such a plane costs x zlotys (units of Polish currency).

 

The only ticket office of the airport already has a queue of n passengers in front of it. Lolek and Bolek have not stood in the queue yet, but they are already wondering what is the maximum and the minimum number of zlotys the airport administration can earn if all n passengers buy tickets according to the conditions of this offer?

The passengers buy tickets in turn, the first person in the queue goes first, then goes the second one, and so on up to n-th person.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of passengers in the queue and the number of planes in the airport, correspondingly. The next line contains m integers a1, a2, ..., am (1 ≤ ai ≤ 1000)ai stands for the number of empty seats in the i-th plane before the ticket office starts selling tickets.

The numbers in the lines are separated by a space. It is guaranteed that there are at least n empty seats in total.

Output

Print two integers — the maximum and the minimum number of zlotys that the airport administration can earn, correspondingly.

Sample test(s)
Input
4 3 2 1 1
Output
5 5
Input
4 3 2 2 2
Output
7 6
Note

In the first test sample the number of passengers is equal to the number of empty seats, so regardless of the way the planes are chosen, the administration will earn the same sum.

In the second sample the sum is maximized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person — to the 2-nd plane, the 3-rd person — to the 3-rd plane, the 4-th person — to the 1-st plane. The sum is minimized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person — to the 1-st plane, the 3-rd person — to the 2-nd plane, the 4-th person — to the 2-nd plane.

View Code
 1 #include <iostream>
 2 #include<cstdio>
 3 #include<string.h>
 4 using namespace std;
 5 int main()
 6 {
 7     int i,j,k,n,m,x[1001],y[1001],min = 0,max = 0,a,b,a1,b1;
 8     scanf("%d%d",&n,&m);
 9     for(i = 1; i <= m ; i++)
10     {
11         scanf("%d",&x[i]);
12         y[i] = x[i];
13     }
14     for(i = 1; i <= n ;i++)
15     {
16         a = 0;
17         for(j = 1 ; j <= m ;j++)
18         if(x[j]>a)
19         {
20             a = x[j];
21             a1 = j;
22         }
23         x[a1]--;
24         max+=a;
25     }
26     for(i = 1; i <= n ;i++)
27     {
28         a = 2000;
29         for(j = 1; j <= m ; j++)
30         if(y[j]<a&&y[j]>0)
31         {
32             a1 = j;
33             a = y[j];
34         }
35         y[a1]--;
36         if(a!=2000)
37         min+=a;
38     }
39     printf("%d %d\n",max,min);
40     return 0;
41 }
C. Ice Skating
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.

We assume that Bajtek can only heap up snow drifts at integer coordinates.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift.

Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift's locations are distinct.

Output

Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one.

Sample test(s)
Input
2 2 1 1 2
Output
1
Input
2 2 1 4 1
Output
0

并查集
View Code
 1 #include <iostream>
 2 #include<cstdio>
 3 #include<string.h>
 4 using namespace std;
 5 struct node
 6 {
 7     int x,y;
 8 }q[110];
 9 int father[110];
10 int find(int x)
11 {
12     if(x!=father[x])
13     father[x] = find(father[x]);
14     return father[x];
15 }
16 int main()
17 {
18     int n,i,j,k,num = 0;
19     scanf("%d", &n);
20     for(i = 1; i <= n ;i++)
21     {
22         scanf("%d%d",&q[i].x,&q[i].y);
23     }
24     for(i = 1; i <= n ;i++)
25     father[i] = i;
26     for(i = 1; i < n ; i++)
27     {
28         for(j = i+1; j <= n ;j++)
29         {
30             if(q[j].x==q[i].x||q[j].y==q[i].y)
31             {
32                 int px = find(i);
33                 int py = find(j);
34                 if(px!=py);
35                 father[px] = py;
36             }
37         }
38     }
39     for(i = 1; i <= n ; i++)
40     if(father[i] == i)
41     num++;
42     printf("%d\n",num-1);
43     return 0;
44 }

 

posted @ 2012-08-18 17:40  _雨  阅读(345)  评论(0编辑  收藏  举报