银河

SKYIV STUDIO

  博客园 :: 首页 :: 博问 :: 闪存 :: :: :: 订阅 订阅 :: 管理 ::
Timus 1028. Stars 要求计算直角坐标系中星星的等级。

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.

Problem illustration

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

inputoutput
5
1 1
5 1
7 1
3 3
5 5
1
2
1
1
0
Problem Author: Pavel Zaletsky
Problem Source: Ural Collegiate Programming Contest '99

答案如下:

 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 pivotLength = 32000 + 1;
11       const int blockBits = 8;
12       const int blockSize = 1 << blockBits;
13       const int blockMask = blockSize - 1;
14       const int blockLength = pivotLength / blockSize + 1;
15       short[] counts = new short[pivotLength];
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 }

这道题时间限制比较严格,只有 0.25 秒。根据题意,星星的等级定义为其左下方星星的个数。由于输入是按先纵坐标后横坐标排好序的,所以星星的等级等于已经读入的星星在其左方的个数,和星星的纵坐标无关。所以在本程序中根本就没有读入星星的纵坐标。本程序的关键是要使用时间复杂度为 O(N*logN) 的算法。如果使用通常的 O(N2) 算法就会超时。在本程序中,横坐标长度为 32001 (pivotLength),分为 126 (blockLength) 个区域,每个区域包括 256 (blockSize) 个坐标点。然后分区域进行计算。本程序的实际运行时间为 0.14 秒。这道题的最好成绩是 0.001 秒,使用 C++ 语言。真不知道其算法是什么,为什么能够这么快。

posted on 2008-06-22 10:08  银河  阅读(759)  评论(0编辑  收藏  举报