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 = 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++ 语言。真不知道其算法是什么,为什么能够这么快。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述