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 | 4 |
output
1 | 2 |
้ข่งฃ
ๆดๅๅณๅฏ
ไปฃ็
1 | #include<stdio.h> |
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 | 6 |
output
1 | 1 1 1 1 1 |
้ข่งฃ
ๅนถๆฅ้่ฎฐๅฝๅฑไบๅชไธไธช้ๅๅณๅฏใ
ไปฃ็
1 | #include<stdio.h> |
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 | 8 |
output
1 | 1 |
้ข่งฃ๏ผ
่ฝฌๆขไธบไธ่ฟๅถ๏ผๆพๅฐ็ฌฌไธไธชไธบ2็ไฝ็ฝฎ๏ผ็ถๅๅจ2ไนๅๆพๅฐ็ฌฌไธไธชไธบ0็ไฝ็ฝฎ๏ผ็ถๅ็ฝฎไธบ1๏ผๅ้ข็ๅ จ้จ็ฝฎไธบ0
ไปฃ็ ๏ผ
1 | #include<stdio.h> |
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 | 7 2 |
output
1 | 3 |
input
1 | 5 1 |
output
1 | 3 |
input
1 | 6 1 |
output
1 | 4 |
้ข่งฃ๏ผ
่ดชๅฟๆ็ งlrๅขๅบ่ฟ่กๆๅบ๏ผ็ถๅ่ฎฐๅฝๆๅคง็ๅบ็ฐๆฌกๆฐไธบkๆฌก็ไฝ็ฝฎ๏ผๅช้่ฟไบeasy็ๆฌ๏ผๆดๅ่ฟ่ก็ป่ฎก็๏ผhard็ๆฌๅฏไปฅ็จ็บฟๆฎตๆ ๆไผๅ ้ๅA๏ผๅพ ่กฅ้ขใ
ไปฃ็ ๏ผ
1 | #include<stdio.h> |
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 | 10 2 |
output
1 | 0 7 13 18 24 35 36 37 40 45 |
input
1 | 10 1 |
output
1 | 0 2 4 7 8 11 13 14 16 17 |
้ข่งฃ
dp ็ถๆ่ฝฌ็งป็ไปฃ็
ไปฃ็ ๏ผ
1 | #include<stdio.h> |