1028. Stars
Time Limit: 0.25 second
Memory Limit: 16 MB
Astronomers often examine star maps where stars are represented by points on a 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 of the given star. Astronomers 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 stars N (1 ≤ N ≤ 15000). The following N lines describe coordinates of stars (two integers X and Y per 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 order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
Output
The output should contain N lines, one number per line. The first line contains amount of stars of 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 level N−1.
Sample
input | output |
---|---|
5 1 1 5 1 7 1 3 3 5 5 | 1 2 1 1 0 |
Problem Source: Ural Collegiate Programming Contest '99
答案如下:
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 pivotLength = 32000 + 1;
11 const int blockBits = 8;
12 const int blockSize &#61; 1 << blockBits;
13 const int blockMask &#61; blockSize - 1;
14 const int blockLength &#61; pivotLength / blockSize &#43; 1;
15 short[] counts &#61; new short[pivotLength];
16 short[] blocks &#61; new short[blockLength];
17 short[] levels &#61; new short[int.Parse(Console.ReadLine())];
18 for (int i &#61; levels.Length; i > 0; i--)
19 {
20 int x &#61; int.Parse(Console.ReadLine().Split()[0]);
21 int q &#61; x >> blockBits;
22 int r &#61; x & blockMask;
23 int level &#61; blocks[q];
24 for (int j &#61; x - r &#43; 1; j <&#61; x; j&#43;&#43;) level &#43;&#61; counts[j];
25 for (int j &#61; q &#43; ((r &#61;&#61; 0) ? 0 : 1); j < blockLength; j&#43;&#43;) blocks[j]&#43;&#43;;
26 levels[level]&#43;&#43;;
27 counts[x]&#43;&#43;;
28 }
29 foreach (short level in levels) Console.WriteLine(level);
30 }
31 }
32 }
这道题时间限制比较严格&#xff0c;只有 0.25 秒。根据题意&#xff0c;星星的等级定义为其左下方星星的个数。由于输入是按先纵坐标后横坐标排好序的&#xff0c;所以星星的等级等于已经读入的星星在其左方的个数&#xff0c;和星星的纵坐标无关。所以在本程序中根本就没有读入星星的纵坐标。本程序的关键是要使用时间复杂度为 O(N*logN) 的算法。如果使用通常的 O(N2) 算法就会超时。在本程序中&#xff0c;横坐标长度为 32001 (pivotLength)&#xff0c;分为 126 (blockLength) 个区域&#xff0c;每个区域包括 256 (blockSize) 个坐标点。然后分区域进行计算。本程序的实际运行时间为 0.14 秒。这道题的最好成绩是 0.001 秒&#xff0c;使用 C&#43;&#43; 语言。真不知道其算法是什么&#xff0c;为什么能够这么快。