0%

cf-Global Round 5

A:Balanced Rating Changes

้ข˜็›ฎ๏ผš

Another Codeforces Round has just finished! It has gathered ๐‘› participants, and according to the results, the expected rating change of participant ๐‘– is ๐‘Ž๐‘–. These rating changes are perfectly balanced โ€” their sum is equal to 0.

Unfortunately, due to minor technical glitches, the round is declared semi-rated. It means that all rating changes must be divided by two.

There are two conditions though:

For each participant ๐‘–, their modified rating change ๐‘๐‘– must be integer, and as close to ๐‘Ž๐‘–/2 as possible. It means that either ๐‘๐‘–=โŒŠ๐‘Ž๐‘–/2โŒ‹ or ๐‘๐‘–=โŒˆ๐‘Ž๐‘–/2โŒ‰. In particular, if ๐‘Ž๐‘– is even, ๐‘๐‘–=๐‘Ž๐‘–2. Here โŒŠ๐‘ฅโŒ‹ denotes rounding down to the largest integer not greater than ๐‘ฅ, and โŒˆ๐‘ฅโŒ‰ denotes rounding up to the smallest integer not smaller than ๐‘ฅ.
The modified rating changes must be perfectly balanced โ€” their sum must be equal to 0.
Can you help with that?

Input

The first line contains a single integer ๐‘› (2โ‰ค๐‘›โ‰ค13845), denoting the number of participants.

Each of the next ๐‘› lines contains a single integer ๐‘Ž๐‘– (โˆ’336โ‰ค๐‘Ž๐‘–โ‰ค1164), denoting the rating change of the ๐‘–-th participant.

The sum of all ๐‘Ž๐‘– is equal to 0.

Output

Output ๐‘› integers ๐‘๐‘–, each denoting the modified rating change of the ๐‘–-th participant in order of input.

For any ๐‘–, it must be true that either ๐‘๐‘–=โŒŠ๐‘Ž๐‘–/2โŒ‹ or ๐‘๐‘–=โŒˆ๐‘Ž๐‘–/2โŒ‰. The sum of all ๐‘๐‘– must be equal to 0.

If there are multiple solutions, print any. We can show that a solution exists for any valid input.

Examples

input

1
2
3
4
3
10
-5
-5

output

1
2
3
5
-2
-3

input

1
2
3
4
5
6
7
8
7
-7
-29
0
3
24
-29
38

output

1
2
3
4
5
6
7
-3
-15
0
2
12
-15
19

้ข˜่งฃ๏ผš

็ปŸ่ฎกๆญฃๆ•ฐไธŽ่ดŸๆ•ฐ็š„ๅฅ‡ๆ•ฐไน‹ๅทฎ๏ผŒ็„ถๅŽๅœจ่พ“ๅ‡บๆ—ถ่ฟ›่กŒๅค„็†ๅณๅฏใ€‚

ไปฃ็ ๏ผš

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
int a[20010];
int main()
{
int n;
scanf("%d", &n);
int sum1 =0;
int sum2 =0;
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
if(a[i] > 0 && a[i] % 2 == 1)
sum1++;
if(a[i] < 0 && a[i] % 2 == -1)
sum2++;
}
if(sum1 < sum2){
sum1 = (sum2 - sum1) / 2;
for(int i = 0; i < n; i++){
if(a[i] < 0 && a[i]%2 == -1 && sum1 > 0){
printf("%d\n",a[i]/2 - 1);
sum1--;
}
else
{
printf("%d\n",a[i]/2);
}

}
}
else
{
sum1 = (sum1 - sum2) / 2;
for(int i = 0; i < n; i++){
if(a[i] > 0 && a[i]%2 == 1 && sum1 > 0){
printf("%d\n",a[i]/2 + 1);
sum1--;
}
else
{
printf("%d\n",a[i]/2);
}

}
}
return 0;
}

B๏ผš Balanced Tunnel

้ข˜็›ฎ๏ผš

Consider a tunnel on a one-way road. During a particular day, ๐‘› cars numbered from 1 to ๐‘› entered and exited the tunnel exactly once. All the cars passed through the tunnel at constant speeds.

A traffic enforcement camera is mounted at the tunnel entrance. Another traffic enforcement camera is mounted at the tunnel exit. Perfectly balanced.

Thanks to the cameras, the order in which the cars entered and exited the tunnel is known. No two cars entered or exited at the same time.

Traffic regulations prohibit overtaking inside the tunnel. If car ๐‘– overtakes any other car ๐‘— inside the tunnel, car ๐‘– must be fined. However, each car can be fined at most once.

Formally, letโ€™s say that car ๐‘– definitely overtook car ๐‘— if car ๐‘– entered the tunnel later than car ๐‘— and exited the tunnel earlier than car ๐‘—. Then, car ๐‘– must be fined if and only if it definitely overtook at least one other car.

Find the number of cars that must be fined.

Input

The first line contains a single integer ๐‘› (2โ‰ค๐‘›โ‰ค105), denoting the number of cars.

The second line contains ๐‘› integers ๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘› (1โ‰ค๐‘Ž๐‘–โ‰ค๐‘›), denoting the ids of cars in order of entering the tunnel. All ๐‘Ž๐‘– are pairwise distinct.

The third line contains ๐‘› integers ๐‘1,๐‘2,โ€ฆ,๐‘๐‘› (1โ‰ค๐‘๐‘–โ‰ค๐‘›), denoting the ids of cars in order of exiting the tunnel. All ๐‘๐‘– are pairwise distinct.

Output

Output the number of cars to be fined.

Examples

input

1
2
3
5
3 5 2 1 4
4 3 2 5 1

output

1
2

input

1
2
3
7
5 2 3 6 7 1 4
2 3 6 7 1 4 5

output

1
6

input

1
2
3
2
1 2
1 2

output

1
0

้ข˜่งฃ๏ผš

ๆญคไปฃ็ ็”จ็š„ๅฝ’ๅนถๆŽ’ๅบ่ฟ›่กŒๅค„็†๏ผŒๅคๆ‚ๅบฆไธบnlogn ๆœ‰ๅคๆ‚ๅบฆไธบn็š„ๆ–นๆณ•๏ผŒไปฅๅŽ่กฅไธŠใ€‚่ฟ›่กŒๅฝ’ๅนถๆŽ’ๅธƒๆ—ถ๏ผŒๅ‡บ็Žฐ้€†ๅบๆ—ถ๏ผŒๅฐ†ๅๅคง็š„ๆ•ฐๅญ—่ฟ›่กŒ่ฎฐๅฝ•ๅฐฑๅฏใ€‚

ไปฃ็ :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
int p[100010];
int t[100010];
int a[100010];
int used[100010];
int sum=0;
void solve(int l,int r)
{
//printf("%d %d\n",l,r);
if(r-l<2)
{
if(r-l == 1 && a[l] > a[r]){
if(!used[a[l]]){
// printf("%d %d\n",a[l],a[r]);
used[a[l]] = 1;
// printf("2 used %d = true\n",a[l]);
sum++;
}
swap(a[l], a[r]);
}
return ;
}
int m = (l + r) >> 1;
solve(l , m);
solve(m+1 , r);
int now = m-l+1;
int sz = l, sy = m+1;
/* for(int i = l; i <= r;i++)
printf("%d ", a[i]);
printf("\n%d\n",used[6]);
printf("\n");*/
for(int i = 0; i < r - l + 1; i++)
{
// printf(" i:%d used %d %d\n",i,a[sz],used[a[sz]]);
if(sz <= m && sy <= r)
{
if(a[sz] > a[sy]){
t[i] = a[sy];
sy++;
if(!used[a[sz]]){
//printf("%d %d\n",a[sz],a[sy-1]);
sum++;
used[a[sz]] = 1;
// printf("used %d %d\n",a[sz],used[a[sz]]);
// printf("sy r %d %d\n", sy,r);
}
}
else
{
t[i] = a[sz];
if(sy != m + 1 && !used[a[sz]])
{
sum++;
used[a[sz]] = 1;
}
sz++;
now--;
}
}
else if(sz == m+1)
{
while(sy <= r)
{
t[i] = a[sy];
sy++; i++;
}
}
else
{
// printf("sz %d\n",a[sz]);
// printf("used %d %d \n",a[sz],used[a[sz]]);
while(sz <= m)
{
t[i] = a[sz];
// printf("sz %d\n",a[sz]);
// printf("used %d %d \n",a[sz],used[a[sz]]);
if(used[a[sz]] == 0){
sum++;
used[a[sz]] = 1;
}
sz++;
i++;
}
}
}
for(int i= l ; i <= r; i++)
a[i] = t[i-l];

}
int main()
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++){
int now ;
scanf("%d", &now);
p[now] = i;
}
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
a[i] = p[a[i]];
}
solve(1,n);
printf("%d\n", sum);
return 0;
}

C๏ผšBalanced Removals (Easier)

้ข˜็›ฎ๏ผš

This is an easier version of the problem. In this version, ๐‘›โ‰ค2000.

There are ๐‘› distinct points in three-dimensional space numbered from 1 to ๐‘›. The ๐‘–-th point has coordinates (๐‘ฅ๐‘–,๐‘ฆ๐‘–,๐‘ง๐‘–). The number of points ๐‘› is even.

Youโ€™d like to remove all ๐‘› points using a sequence of ๐‘›2 snaps. In one snap, you can remove any two points ๐‘Ž and ๐‘ that have not been removed yet and form a perfectly balanced pair. A pair of points ๐‘Ž and ๐‘ is perfectly balanced if no other point ๐‘ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points ๐‘Ž and ๐‘.

Formally, point ๐‘ lies within the axis-aligned minimum bounding box of points ๐‘Ž and ๐‘ if and only if min(๐‘ฅ๐‘Ž,๐‘ฅ๐‘)โ‰ค๐‘ฅ๐‘โ‰คmax(๐‘ฅ๐‘Ž,๐‘ฅ๐‘), min(๐‘ฆ๐‘Ž,๐‘ฆ๐‘)โ‰ค๐‘ฆ๐‘โ‰คmax(๐‘ฆ๐‘Ž,๐‘ฆ๐‘), and min(๐‘ง๐‘Ž,๐‘ง๐‘)โ‰ค๐‘ง๐‘โ‰คmax(๐‘ง๐‘Ž,๐‘ง๐‘). Note that the bounding box might be degenerate.

Find a way to remove all points in ๐‘›2 snaps.

Input

The first line contains a single integer ๐‘› (2โ‰ค๐‘›โ‰ค2000; ๐‘› is even), denoting the number of points.

Each of the next ๐‘› lines contains three integers ๐‘ฅ๐‘–, ๐‘ฆ๐‘–, ๐‘ง๐‘– (โˆ’108โ‰ค๐‘ฅ๐‘–,๐‘ฆ๐‘–,๐‘ง๐‘–โ‰ค108), denoting the coordinates of the ๐‘–-th point.

No two points coincide.

Output

Output ๐‘›2 pairs of integers ๐‘Ž๐‘–,๐‘๐‘– (1โ‰ค๐‘Ž๐‘–,๐‘๐‘–โ‰ค๐‘›), denoting the indices of points removed on snap ๐‘–. Every integer between 1 and ๐‘›, inclusive, must appear in your output exactly once.

We can show that it is always possible to remove all points. If there are many solutions, output any of them.

Examples

input

1
2
3
4
5
6
7
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0

output

1
2
3
3 6
5 1
2 4

input

1
2
3
4
5
6
7
8
9
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3

output

1
2
3
4
4 5
1 6
2 7
3 8

้ข˜่งฃ๏ผš

ๆŒ‰็บฟ้ขไฝ“็š„้กบๅบ่ฟ›่กŒไพๆฌกๅˆ ้™คใ€‚ๅณๆŒ‰xyz้กบๅบ่ฟ›่กŒๆŽ’ๅบ๏ผŒๅ…ˆๅˆ ้™คxy็›ธๅŒ็š„๏ผŒๅ†ๅˆ ้™คx็›ธๅŒ็š„๏ผŒ็„ถๅŽๅ†ไพๆฌกๅˆ ้™ค๏ผŒๆณจๆ„ๆฏๆฌกๅˆ ้™คๅŽ้ƒฝ่ฆๆŽ’ๅบไธ€ๆฌกใ€‚

ไปฃ็ ๏ผš

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
struct point{
int x,y,z;
int index;
};
point p[2010];
point tmp[2010];
bool cmp(point a, point b){
if(a.x != b.x){
return a.x < b.x;
}
else if(a.y != b.y){
return a.y < b.y;
}
else{
return a.z < b.z;
}
}
bool used[2010];
int main()
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d%d%d",&p[i].x, &p[i].y, &p[i].z);
p[i].index = i;
}
sort(p + 1, p + n + 1, cmp);
for(int i = 2 ; i <= n; i++)
{
if(p[i].x == p[i - 1].x && p[i].y == p[i - 1].y){
printf("%d %d\n",p[i].index, p[i - 1].index);
used[p[i].index] = used[p[i - 1].index] = true;
i++;
}
}
int now = 1;
for(int i = 1; i <= n; i++)
{
if(!used[p[i].index])
tmp[now++] = p[i];
}
now--;
for(int i = 1; i <= now; i++)
p[i] = tmp[i];
sort(p + 1, p + 1 + now,cmp);
// printf("now %d\n",now);
for(int i = 2 ; i <= now; i++)
{
if(p[i].x == p[i - 1].x){
printf("%d %d\n",p[i].index, p[i - 1].index);
used[p[i].index] = used[p[i - 1].index] = true;
i++;
}
}
int o = 1;
for(int i = 1; i <= now; i++)
{
if(!used[p[i].index])
tmp[o++] = p[i];
}
o--;
sort(tmp + 1, tmp + 1 + o,cmp);
//printf("o %d\n",o);
for(int i = 1; i <= o; i++){
printf("%d %d\n",tmp[i].index,tmp[i + 1].index);
i++;
}
return 0;

}