Plus
Pin


Problem description Progress
ID 31926. Snowflake classification
Темы: Quick sort   

Stella studies snowflakes, measuring the length of their six rays, and has already collected a lot of data. Now Stella is going to determine how many different types of snowflakes she has found. She considers snowflakes to be the same if the snowflakes can be combined after being rotated and/or flipped.
Write a program to help Stella classify snowflakes.
The first input line contains one integer N (2 ≤ N ≤ 100000). Then N lines follow, each containing 6 integers a1 a2 a3 a4 a 5 a6 from 1 to 109 separated by spaces – lengths of snowflake rays in clockwise order.
Output a single integer – the number of different snowflakes found by Stella.

Enter Output
5
1 2 3 4 5 6
3 4 5 6 1 2
3 2 1 4 5 6
6 5 4 3 2 1
2 3 6 5 4 1
2
Note
Snowflakes 1 and 2 and 4 after rotation or after turning over and snowflakes 3 and 5 after turning and turning coincide.

ID 34879. K-quest
Темы: Binary search    Quick sort   

In one of the computer quest games there is the following task. There are N characters on the map of the game world, each of which the player can meet. From communication with the i-th character, the player's karma changes by the value ai, which can be either positive or negative or even zero.

Initially, the player's karma is zero. In order to pass to the next level, you need to have karma exactly equal to the value of K, while karma can also take both positive and negative values.

The rooms where the characters are located are connected by one-way magical portals, so the player will have to meet the characters in a certain sequence: after the character number i, he gets to the character number i + 1, then to the character number i + 2, and so on. .d. There is no portal to another character in the room of the last character with number N.

To move between characters, you can also use teleportation spells, but unfortunately the hero has only two scrolls with spells left. Therefore, one of these scrolls will have to be used in order to teleport to any of the characters, and the second scroll — to leave the game world after the hero's karma reaches K.

Help the player determine which room to teleport to at the beginning and which room to leave the game world to reach Karma K, or tell them it's not possible.

Input
The first line of the input contains two numbers: the number of characters N and the required level of karma K (|K| ≤ 109, K ≠ 0). The second line contains N space-separated integers a1, a2, ..., aN — values ​​by which the hero's karma changes after communicating with characters with numbers 1, 2, ..., N, respectively.

Imprint
Print the number of the room the player must enter and the number of the room from which the player must exit in order to collect karma K. big number. If it is impossible to achieve karma K by consistently communicating with the characters, then print a single number  - 1.
 

Input Output
5 3
-2 2 -1 2 4
2 4
7 1
1 -1 1 -1 1 -1 2
5 5
4 3
2 2 2 2
-1

 

ID 22008. Big sort
Темы: Quick sort   

Given a number N (\(1<=N<=100000\)) followed by N natural numbers from 1 to 100.
Print N numbers in non-decreasing order.

Input
The first line specifies the number N - the number of array elements (\(1<=N<=100000\)).
Next comes N lines, one number per line - array elements.

Imprint 
Display the sorted array on one line.
 
Examples
# Input Output
1
5
3
1
2
4
2
1 2 2 3 4

ID 31880. Fence painting
Темы: Quick sort   

One day, as punishment for pranks and deceit, Aunt Polly made Tom paint a L yard fence. All of you remember very well that Tom sold (for various goodies) his work to other boys who wanted to whitewash the fence.
By the time Tom ran out of lime, the fence had been painted by N boys. And since Tom didn’t really follow the boys, everyone painted the part of the fence that he liked best. 
Each i-th boy started painting the fence from a vertical plank with coordinate Lefti and painted up to plank with coordinate Righti (the length of the board is considered equal to 1). 
Determine the length of the fence that Tom will need to paint himself. 

 

Input
The first line contains the number L - the length of Aunt Polly's fence. The second line contains the number N, the next N lines - pairs Lefti< /sub> and Righti. All numbers are integers
Restrictions:
\(0 <= L <= 2 \cdot 10^9\);
 \(-10^9 <= Left_i <= Right_i <= 10^9\);
\(1 <= N <= 15 000\).

Imprint
Print a single number - the length of the fence that Tom needs to finish painting.
 
 
Examples
# Input Output
1
20
1
10 20
10
2 10
1
10 10
10
3 100
2
10 30
20 40
70

ID 37257. Taxi
Темы: Quick sort   

After a long meeting, the director of the company decided to order a taxi to take the employees home. He ordered N cars  – as many as it has employees. However, when they arrived, it turned out that each taxi driver has his own tariff for 1 kilometer.

The director knows which employee how many kilometers from work to home (unfortunately, all employees live in different directions, so you cannot send two employees in one car). Now the director wants to determine which of the employees should take which taxi home so that the total cost of a taxi (and they are borne by the company) is minimal.


Input: The first line of the input contains a natural number N (1 ≤ N< /em> ≤ 1000)  – the number of company employees (coinciding with the number of taxis called). Next,  N numbers are written that specify the distance in kilometers from work to the homes of company employees (the first number  – for the first employee, the second  – for the second, etc.). All distances  – positive integers not exceeding 1000. More N numbers  – fares for one kilometer in a taxi (the first number & ndash; in the first taxi car, the second & nbsp; & ndash; in the second, etc.). Tariffs are expressed as positive integers not exceeding 10000. 

Output: The program should output N numbers. First number – the number of the taxi that the first employee should take, the second number – the number of the taxi in which the second one should sit, etc., so that the total cost of a taxi is minimal. If there are several seating options for employees with minimal costs, print any of them.

Examples
# Input Output
1 3
10 20 30
50 20 30
1 3 2
2 5
10 20 1 30 30
3 3 3 2 3
5 1 3 2 4

ID 37256. Rescuing robots
Темы: Quick sort   

A storm of diamond dust has risen on Shelezyak's planet. As you know, diamond dust causes paralysis in robots. At the beginning of the storm, all the robots were busy working along one straight road. There are m repair shops along the same road. Therefore, it was decided to send each robot to the nearest repair shop to renew their lubricant. 
It is necessary for each robot to determine the nearest repair shop to it.
 

Input 
The first line contains the number n - the number of robots(\(1 <= n <= 100000\)). The second line contains n different integers, the i-th of these numbers specifies the distance from the beginning of the road to the place of work of the i-th robot . The third line of the input contains the number m - the number of repair shops (1 <= m <= 100000). The fourth line contains m various integers, the ith of these numbers specifies the distance from the beginning of the road to the ith repair shop. All distances are positive and do not exceed 109.  The robot and the workshop can be located at the same point.

Imprint
Print n numbers - for each robot, print the number of the nearest repair shop. Repair shops are numbered from 1 to m in the order in which they are given in the input.

 
Examples
# Input Output
1 4
1 2 6 10
2
7 3
2 2 1 1

ID 37258. bomb shelters
Темы: Quick sort   

The Civil Defense Headquarters of the Thirtieth Region decided to update the rescue plan in case of a nuclear attack. It is known that all n villages of the Thirtieth Region are located along one straight road. There are also m bomb shelters along the road, where villagers can take shelter in case of a nuclear attack.

In order for the rescue in the event of a nuclear alarm to be as efficient as possible, it is necessary to determine the nearest bomb shelter for each village.

Input data: In the first line enter the number n - the number of villages (1 <= n <= 100000). The second line contains n different integers, the i-th of these numbers specifies the distance from the beginning of the road to the i-th village. The third line of the input specifies the number m - the number of bomb shelters (1 <= m <= 100000). The fourth line contains m different integers, i-th of these integers specifies the distance from the beginning of the road to i-th bomb shelter. All distances are positive and do not exceed 109. The village and the shelter can be located at the same point.

Output: Print n numbers - for each village print the number of the bomb shelter closest to it. The bomb shelters are numbered from 1 to m in the order in which they are given in the input.

Examples
# Input Output
1 4
1 2 6 10
2
7 3
2 2 1 1

ID 31879. Anti-QuickSort (under development)
Темы: Quick sort   

You are given a number N (1<=N<=1000) and then N natural numbers from the range 1 to 100.
Output a permutation of array elements on which quicksort will perform the maximum number of comparisons, provided that the "reference" there will be an element in the middle. 

Input: number N is given in the first line
The second line contains N numbers - array elements
Output: print the desired permutation of the elements of the original array

Examples

# Input Output
1 5
3 10 1 20 7
3 20 7 1 10

ID 37691. Anti-QuickSort
Темы: Quick sort   

Given a number N (\(1<=N<=1000\)) followed by N natural numbers from 1 to 100.
Output the permutation of the array elements on which quicksort will perform the maximum number of comparisons, provided that "reference" there will be an element in the middle. 

Input  
The first line specifies the number N.

Imprint
Output the desired permutation of numbers from 1 to N, on which quicksort will perform the maximum number of comparisons.
 

Examples
# Input Output
1 5 1 4 5 3 2
 
Explanation
The worst running time is when the array is split so that one part contains n−1 elements and the second — 1. This can be achieved if at each stage of the split there is a maximum element in the middle.
1) 1 4 5 3 2
2) 1 4 2 3 5
3) 1 3 2 4 5
4) 1 2 3 4 5
5) 1 2 3 4 5

 

ID 38920. elves and deer
Темы: Heuristic methods    Binary search by answer    Quick sort    Greedy Algorithm   

The New Year is coming soon and Santa Claus has already started preparing his magical reindeer team, on which he delivers gifts to children. It is known that the team is driven by several magical deer, each of which is ridden by two elves.

But the magical deer – obstinate animals, so not any two elves can ride any deer. Namely, each deer is characterized by some obstinacy ai, and each elf – temperament bi. Two elves j and k can ride the i-th reindeer if and only if either bj < ai < bk or bk < ai < bj.

To make his appearance as spectacular as possible, Santa Claus wants his team to have as many reindeer as possible. About every deer Santa knows his obstinacy, and about every elf – his temperament.

Help Santa figure out the maximum number of reindeer he can include in his team, which reindeer he should choose, and which elves should ride them.

Input
The first line contains two integers m and n – the number of deer and elves, respectively ( 1 ≤ m, n ≤ 100,000).

The second line contains m integers ai – deer obstinacy ( 0 ≤ ai ≤ 109). The third line contains n integers bi – elves temperament ( 0 ≤ bi ≤ 109).

Imprint
In the first line  print a single number k – the maximum number of reindeer that Santa Claus can include in his team. In the next k lines print three integers each: di, ei, 1, ei, 2 – for each reindeer in the team print its number and the numbers of the elves that will ride it. If there are several solutions, print any one.

Both elves and deer are numbered, starting from one, in the order in which they are given in the input.

Examples
# Input Output
1 4 6
2 3 4 5
1 3 2 2 5 2
2
1 1 2
3 4 5

ID 38991. Advertising 2
Темы: Intersection of many    Quick sort   

The supermarket decided to broadcast advertisements for new products from time to time. In order to create the optimal schedule for broadcasting advertising, the supermarket management conducted the following study: during the day, for each customer who visited the supermarket, the time when he arrived at the supermarket and when he left it was recorded.

The advertising manager assumed that such a schedule of arrivals and departures of buyers would continue in the following days. He wants to schedule commercials so that each customer hears at least two advertisements. At the same time, he stipulated that two advertisements should not be broadcast at the same time and, since the sellers had to listen to these advertisements all the time, the total number of advertisements per day was minimal.

Write a program that will create such a schedule for broadcasting commercials. Advertisements can start broadcasting only at whole points in time. Each advertisement is considered to end before the next integer point in time. If an advertisement is broadcast at the moment when the customer enters or leaves the supermarket, the customer has time to hear this advertisement.

Input
The input file contains first the number N — number of shoppers visiting the supermarket per day (1<N<3000). Then comes N pairs of natural numbers Ai, Bi, specifying the time of arrival and departure of customers from the supermarket, respectively (0<Ai<Bi<106).

Imprint
In the output file, first print the number of advertisements that will be broadcast per day. Then output in ascending order the time points at which you want to broadcast advertisements.

If there are several solutions, print any of them.
 

Examples
# Input Output
1 5
1 10
10 12
1 10
1 10
23 24
5
5 10 12 23 24