0%

cf-595-div3

A:Yet Another Dividing into Teams

้ข˜็›ฎ๏ผš

You are a coach of a group consisting of ๐‘› students. The ๐‘–-th student has programming skill ๐‘Ž๐‘–. All students have distinct programming skills. You want to divide them into teams in such a way that:

No two students ๐‘– and ๐‘— such that |๐‘Ž๐‘–โˆ’๐‘Ž๐‘—|=1 belong to the same team (i.e. skills of each pair of students in the same team have the difference strictly greater than 1);
the number of teams is the minimum possible.
You have to answer ๐‘ž independent queries.

Input

The first line of the input contains one integer ๐‘ž (1โ‰ค๐‘žโ‰ค100) โ€” the number of queries. Then ๐‘ž queries follow.

The first line of the query contains one integer ๐‘› (1โ‰ค๐‘›โ‰ค100) โ€” the number of students in the query. The second line of the query contains ๐‘› integers ๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘› (1โ‰ค๐‘Ž๐‘–โ‰ค100, all ๐‘Ž๐‘– are distinct), where ๐‘Ž๐‘– is the programming skill of the ๐‘–-th student.

Output

For each query, print the answer on it โ€” the minimum number of teams you can form if no two students ๐‘– and ๐‘— such that |๐‘Ž๐‘–โˆ’๐‘Ž๐‘—|=1 may belong to the same team (i.e. skills of each pair of students in the same team has the difference strictly greater than 1)

Example

input

1
2
3
4
5
6
7
8
9
4
4
2 10 1 20
2
3 6
5
2 3 4 99 100
1
42

output

1
2
3
4
2
1
2
1

้ข˜่งฃ

ๆšดๅŠ›ๅณๅฏ

ไปฃ็ 

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
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
int a[110];
int main()
{
int t;
scanf("%d", &t);
while(t--){
int n;
scanf("%d", &n);
for(int i = 0; i <n; i++)
scanf("%d", &a[i]);
int ans = 1;
sort(a,a +n);
for(int i = 1; i < n;i++)
if(a[i] - a[i-1] == 1){
ans++;
break;
}
printf("%d\n",ans);
}
return 0;
}

B Books Exchange

้ข˜็›ฎ

The only difference between easy and hard versions is constraints.

There are ๐‘› kids, each of them is reading a unique book. At the end of any day, the ๐‘–-th kid will give his book to the ๐‘๐‘–-th kid (in case of ๐‘–=๐‘๐‘– the kid will give his book to himself). It is guaranteed that all values of ๐‘๐‘– are distinct integers from 1 to ๐‘› (i.e. ๐‘ is a permutation). The sequence ๐‘ doesnโ€™t change from day to day, it is fixed.

For example, if ๐‘›=6 and ๐‘=[4,6,1,3,5,2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on.

Your task is to determine the number of the day the book of the ๐‘–-th child is returned back to him for the first time for every ๐‘– from 1 to ๐‘›.

Consider the following example: ๐‘=[5,1,2,4,3]. The book of the 1-st kid will be passed to the following kids:

after the 1-st day it will belong to the 5-th kid,
after the 2-nd day it will belong to the 3-rd kid,
after the 3-rd day it will belong to the 2-nd kid,
after the 4-th day it will belong to the 1-st kid.
So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.

You have to answer ๐‘ž independent queries.

Input

The first line of the input contains one integer ๐‘ž (1โ‰ค๐‘žโ‰ค200) โ€” the number of queries. Then ๐‘ž queries follow.

The first line of the query contains one integer ๐‘› (1โ‰ค๐‘›โ‰ค200) โ€” the number of kids in the query. The second line of the query contains ๐‘› integers ๐‘1,๐‘2,โ€ฆ,๐‘๐‘› (1โ‰ค๐‘๐‘–โ‰ค๐‘›, all ๐‘๐‘– are distinct, i.e. ๐‘ is a permutation), where ๐‘๐‘– is the kid which will get the book of the ๐‘–-th kid.

Output

For each query, print the answer on it: ๐‘› integers ๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘›, where ๐‘Ž๐‘– is the number of the day the book of the ๐‘–-th child is returned back to him for the first time in this query.

Example

input

1
2
3
4
5
6
7
8
9
10
11
12
13
6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3

output

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

้ข˜่งฃ

ๅนถๆŸฅ้›†่ฎฐๅฝ•ๅฑžไบŽๅ“ชไธ€ไธช้›†ๅˆๅณๅฏใ€‚

ไปฃ็ 

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
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
int a[200010];
int p[200010];
int f[200010];
set<int> s;
int main()
{
int t;
scanf("%d", &t);
while(t--){
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
f[i] = i;
}
for(int i = 1; i <= n; i++)
{
if(f[i] != i)
continue;
int ans = 1;
int now = i;
while(a[now] != i){
f[now] = i;
now = a[now];
ans++;
}
p[i] = ans;
}
for(int i = 1; i <= n; i++){
if(f[i] == i)
printf("%d ", p[i]);
else{
printf("%d ",p[f[i]]);
}
}
printf("\n");
}
return 0;
}

C Good Numbers

้ข˜็›ฎ:

The only difference between easy and hard versions is the maximum value of ๐‘›.

You are given a positive integer number ๐‘›. You really love good numbers so you want to find the smallest good number greater than or equal to ๐‘›.

The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).

For example:

30 is a good number: 30=33+31,
1 is a good number: 1=30,
12 is a good number: 12=32+31,
but 2 is not a good number: you canโ€™t represent it as a sum of distinct powers of 3 (2=30+30),
19 is not a good number: you canโ€™t represent it as a sum of distinct powers of 3 (for example, the representations 19=32+32+30=32+31+31+31+30 are invalid),
20 is also not a good number: you canโ€™t represent it as a sum of distinct powers of 3 (for example, the representation 20=32+32+30+30 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.

For the given positive integer ๐‘› find such smallest ๐‘š (๐‘›โ‰ค๐‘š) that ๐‘š is a good number.

You have to answer ๐‘ž independent queries.

Input

The first line of the input contains one integer ๐‘ž (1โ‰ค๐‘žโ‰ค500) โ€” the number of queries. Then ๐‘ž queries follow.

The only line of the query contains one integer ๐‘› (1โ‰ค๐‘›โ‰ค1018).

Output

For each query, print such smallest integer ๐‘š (where ๐‘›โ‰ค๐‘š) that ๐‘š is a good number.

Example

input

1
2
3
4
5
6
7
8
9
8
1
2
6
13
14
3620
10000
1000000000000000000

output

1
2
3
4
5
6
7
8
1
3
9
13
27
6561
19683
1350851717672992089

้ข˜่งฃ๏ผš

่ฝฌๆขไธบไธ‰่ฟ›ๅˆถ๏ผŒๆ‰พๅˆฐ็ฌฌไธ€ไธชไธบ2็š„ไฝ็ฝฎ๏ผŒ็„ถๅŽๅœจ2ไน‹ๅ‰ๆ‰พๅˆฐ็ฌฌไธ€ไธชไธบ0็š„ไฝ็ฝฎ๏ผŒ็„ถๅŽ็ฝฎไธบ1๏ผŒๅŽ้ข็š„ๅ…จ้ƒจ็ฝฎไธบ0

ไปฃ็ ๏ผš

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
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
int a[100];
long long POW(long long x, long long z)
{
long long ans = 1;
long long now = x;
while(z){
if(z&1)
ans *= now;
now = now * now;
z >>= 1;
}
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while(t--){
long long n;
scanf("%lld", &n);
long long p = n;
int len = 0;
while(n){
a[len] = n %3;
n /= 3;
len++;
}
int sta = -1;
for(int i = 0; i < len; i++)
{
if(a[i] == 2)
sta = i;
}
if(sta == -1){
printf("%lld\n",p);
}
else{
int t = 0,i;
for(i = sta + 1; i < len; i++){
if(a[i] == 0){
t = i;
a[i] = 1;
break;
}
}
if(i == len)
{
printf("%lld\n", POW(3,i));
}
else{
long long ans = 0;
for(int j = t; j < len; j++){
if(a[j] == 1)
{
ans += POW(3,j);
}
}
printf("%lld\n", ans);
}
}
}
return 0;
}

D Too Many Segments

้ข˜็›ฎ๏ผš

The only difference between easy and hard versions is constraints.

You are given ๐‘› segments on the coordinate axis ๐‘‚๐‘‹. Segments can intersect, lie inside each other and even coincide. The ๐‘–-th segment is [๐‘™๐‘–;๐‘Ÿ๐‘–] (๐‘™๐‘–โ‰ค๐‘Ÿ๐‘–) and it covers all integer points ๐‘— such that ๐‘™๐‘–โ‰ค๐‘—โ‰ค๐‘Ÿ๐‘–.

The integer point is called bad if it is covered by strictly more than ๐‘˜ segments.

Your task is to remove the minimum number of segments so that there are no bad points at all.

Input

The first line of the input contains two integers ๐‘› and ๐‘˜ (1โ‰ค๐‘˜โ‰ค๐‘›โ‰ค200) โ€” the number of segments and the maximum number of segments by which each integer point can be covered.

The next ๐‘› lines contain segments. The ๐‘–-th line contains two integers ๐‘™๐‘– and ๐‘Ÿ๐‘– (1โ‰ค๐‘™๐‘–โ‰ค๐‘Ÿ๐‘–โ‰ค200) โ€” the endpoints of the ๐‘–-th segment.

Output

In the first line print one integer ๐‘š (0โ‰ค๐‘šโ‰ค๐‘›) โ€” the minimum number of segments you need to remove so that there are no bad points.

In the second line print ๐‘š distinct integers ๐‘1,๐‘2,โ€ฆ,๐‘๐‘š (1โ‰ค๐‘๐‘–โ‰ค๐‘›) โ€” indices of segments you remove in any order. If there are multiple answers, you can print any of them.

Examples

input

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

output

1
2
3
1 4 7

input

1
2
3
4
5
6
5 1
29 30
30 30
29 29
28 30
30 30

output

1
2
3
3
1 2 4

input

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

output

1
2
4
1 3 5 6

้ข˜่งฃ๏ผš

่ดชๅฟƒๆŒ‰็…งlrๅขžๅบ่ฟ›่กŒๆŽ’ๅบ๏ผŒ็„ถๅŽ่ฎฐๅฝ•ๆœ€ๅคง็š„ๅ‡บ็Žฐๆฌกๆ•ฐไธบkๆฌก็š„ไฝ็ฝฎ๏ผŒๅช้€š่ฟ‡ไบ†easy็‰ˆๆœฌ๏ผŒๆšดๅŠ›่ฟ›่กŒ็ปŸ่ฎก็š„๏ผŒhard็‰ˆๆœฌๅฏไปฅ็”จ็บฟๆฎตๆ ‘ๆˆ–ไผ˜ๅ…ˆ้˜Ÿๅˆ—A๏ผŒๅพ…่กฅ้ข˜ใ€‚

ไปฃ็ ๏ผš

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
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
struct point{
int l,r;
int status;
int sum;
};
point p[200010];
bool cmp(point a, point b){
if(a.r != b.r)
return a.r < b.r;
else{
return a.l < b.l;
}
}
set<int> s;
int main()
{
int n,k;
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++){
scanf("%d%d", &p[i].l, &p[i].r);
p[i].status = i;
}
sort(p + 1, p + n + 1, cmp);
int now = 0;
int ans = 0;
int end = 0;
for(int i = 1; i <= n; i++){
// printf("l %d r %d\n", p[i].l, p[i].r);
if(p[i].l <= now)
{
ans++;
s.insert(p[i].status);
// printf("insert\n");
}
else{
for(int j = i - 1; j > end; j--){
if(p[j].r >= p[i].l){
p[j].sum++;
if(p[j].sum == k){
now = p[j].r;
end = j;
break;
}
}
}
p[i].sum++;
if(p[i].sum == k){
now = p[i].r;
end = i;
}
}
// printf("now %d\n", now);
}
set<int>::iterator it;
printf("%d\n", ans);
for(it = s.begin(); it != s.end(); it++)
printf("%d ", *it);
return 0;
}

E By Elevator or Stairs

้ข˜็›ฎ

You are planning to buy an apartment in a ๐‘›-floor building. The floors are numbered from 1 to ๐‘› from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.

Let:

๐‘Ž๐‘– for all ๐‘– from 1 to ๐‘›โˆ’1 be the time required to go from the ๐‘–-th floor to the (๐‘–+1)-th one (and from the (๐‘–+1)-th to the ๐‘–-th as well) using the stairs;
๐‘๐‘– for all ๐‘– from 1 to ๐‘›โˆ’1 be the time required to go from the ๐‘–-th floor to the (๐‘–+1)-th one (and from the (๐‘–+1)-th to the ๐‘–-th as well) using the elevator, also there is a value ๐‘ โ€” time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at ๐‘ฅ to any floor ๐‘ฆ (๐‘ฅโ‰ ๐‘ฆ) in two different ways:

If you are using the stairs, just sum up the corresponding values of ๐‘Ž๐‘–. Formally, it will take โˆ‘๐‘–=๐‘š๐‘–๐‘›(๐‘ฅ,๐‘ฆ)๐‘š๐‘Ž๐‘ฅ(๐‘ฅ,๐‘ฆ)โˆ’1๐‘Ž๐‘– time units.
If you are using the elevator, just sum up ๐‘ and the corresponding values of ๐‘๐‘–. Formally, it will take ๐‘+โˆ‘๐‘–=๐‘š๐‘–๐‘›(๐‘ฅ,๐‘ฆ)๐‘š๐‘Ž๐‘ฅ(๐‘ฅ,๐‘ฆ)โˆ’1๐‘๐‘– time units.
You can perform as many moves as you want (possibly zero).

So your task is for each ๐‘– to determine the minimum total time it takes to reach the ๐‘–-th floor from the 1-st (bottom) floor.

Input

The first line of the input contains two integers ๐‘› and ๐‘ (2โ‰ค๐‘›โ‰ค2โ‹…105,1โ‰ค๐‘โ‰ค1000) โ€” the number of floors in the building and the time overhead for the elevator rides.

The second line of the input contains ๐‘›โˆ’1 integers ๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘›โˆ’1 (1โ‰ค๐‘Ž๐‘–โ‰ค1000), where ๐‘Ž๐‘– is the time required to go from the ๐‘–-th floor to the (๐‘–+1)-th one (and from the (๐‘–+1)-th to the ๐‘–-th as well) using the stairs.

The third line of the input contains ๐‘›โˆ’1 integers ๐‘1,๐‘2,โ€ฆ,๐‘๐‘›โˆ’1 (1โ‰ค๐‘๐‘–โ‰ค1000), where ๐‘๐‘– is the time required to go from the ๐‘–-th floor to the (๐‘–+1)-th one (and from the (๐‘–+1)-th to the ๐‘–-th as well) using the elevator.

Output

Print ๐‘› integers ๐‘ก1,๐‘ก2,โ€ฆ,๐‘ก๐‘›, where ๐‘ก๐‘– is the minimum total time to reach the ๐‘–-th floor from the first floor if you can perform as many moves as you want.

Examples

input

1
2
3
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5

output

1
0 7 13 18 24 35 36 37 40 45 

input

1
2
3
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3

output

1
0 2 4 7 8 11 13 14 16 17 

้ข˜่งฃ

dp ็Šถๆ€่ฝฌ็งป็œ‹ไปฃ็ 

ไปฃ็ ๏ผš

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
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
int a[200010];
int b[200010];
int dp[200010][2];
int main()
{
int n,c;
scanf("%d%d", &n,&c);
for(int i = 1; i < n; i++)
scanf("%d", &a[i]);
for(int i = 1; i < n; i++)
scanf("%d", &b[i]);
dp[0][1] = c;
for(int i = 1; i < n; i++){
dp[i][0] = min(dp[i - 1][0] + a[i], dp[i-1][1] + a[i]);
dp[i][1] = min(dp[i - 1][0] + b[i] + c, dp[i - 1][1] + b[i]);
}
for(int i = 0; i < n;i++)
printf("%d ",min(dp[i][0],dp[i][1]));
return 0;
}