Timus 1028. Stars requires that the level of the stars in the Cartesian coordinate system be calculated.
1028. Stars
Time Limit: 0.25 second
Memory limit: 16 MB
Astmers
Often examine star maps where stars are represented by points on
Plane and each star has Cartesian coordinates. Let the level of a star
Be an amount of the stars that are not higher and not to the right
The given star. astpolicmers want to know the distribution of the levels
Of the stars.
For
Example, look at the map shown on the figure above. Level of the star
Number 5 is equal to 3 (It's formed by three stars with a numbers 1, 2
And 4). and the levels of the stars numbered by 2 and 4 are 1. At this
Map there are only one star of the level 0, two stars of the level 1,
One star of the Level 2, and one star of the Level 3.
You are to write a program that will count the amounts of the stars of each level on a given map.
Input
The first line of the input contains a number of starsN(1 ≤N≤ 15000). The followingNLines describe coordinates of Stars (two integersXAndYPer line separated by a space, 0 ≤X,Y≤ 32000). There can be only one star at one point of the plane. Stars are listed in ascending orderYCoordinate. Stars with equalYCoordinates are listed in ascending orderXCoordinate.
Output
The output shoshould containN
Lines, one number per line. The first line contains amount of stars
The level 0, the second does amount of stars of the level 1 and so on,
The last line contains amount of stars of the levelN−1.
Sample
Input |
Output |
5 1 1 5 1 7 1 3 3 5 5 |
1 2 1 1 0 |
Problem Author:Pavel Zaretsky
Problem Source:Ural Collegiate Programming contest'99
The answer is as follows:
1 using system;
2
3 namespace skyiv. Ben. timus
4 {
5 // http://acm.timus.ru/problem.aspx? Space = 1 & num = 1028
6 Sealed class t1028
7 {
8 Static void main ()
9 {
10 const int limit tlength = 32000 + 1;
11 const int blockbits = 8;
12 const int blocksize = 1 <blockbits;
13 const int blockmask = blocksize-1;
14 const int blocklength = required tlength/blocksize + 1;
15 short [] counts = new short [same tlength];
16 short [] blocks = new short [blocklength];
17 short [] levels = new short [Int. parse (console. Readline ()];
18 For (INT I = levels. length; I> 0; I --)
19 {
20 int x = int. parse (console. Readline (). Split () [0]);
21 int q = x> blockbits;
22 int r = x & blockmask;
23 int level = blocks [Q];
24 For (Int J = x-R + 1; j <= x; j ++) LEVEL + = counts [J];
25 For (Int J = q + (r = 0 )? 0: 1); j <blocklength; j ++) blocks [J] ++;
26 levels [level] ++;
27 counts [x] ++;
28}
29 foreach (short level in levels) console. writeline (level );
30}
31}
32}
The time limit for this question is strict, only 0.25 seconds. According to the question, the star level is defined as the number of stars in the lower left. Because the input is sorted by the first and then the abscissa, the level of the stars is equal to the number of the stars that have been read on the left, and is irrelevant to the ordinate of the stars. Therefore, in this program, the root does not read the Y coordinates of the stars. The key of this program is to use an O (N * logn) algorithm with time complexity. If the common O (n2) algorithm is used, it times out. In this program, the x-axis length is 32001 (blocked tlength), which is divided into 126 (blocklength) areas. Each area includes 256 (blocksize) coordinate points. Then perform the calculation in different regions. The actual running time of this program is 0.14 seconds. The best score for this question is 0.001 seconds, using the C ++ language. I really don't know what its algorithm is, why it can be so fast.