Stars
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 4508 | Accepted: 1869 |
Description
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 file 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 5 1 1 5 1 7 1 3 3 5 5 Sample Output 1 2 1 1 0 Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. Source
Ural Collegiate Programming Contest 1999 解題方案 典型的樹狀數組問題。根據X坐标建立數組C,每個X坐标對應一個數組元素,記錄X坐标為該坐标的點的個數,共有32000+1個元素。由于輸入是按照Y的升序并且Y相同時X的升序排列的,是以不需要考慮Y坐标。輸入一個X坐标時,求出目前X坐标小于等于輸入X坐标(Y坐标由輸入順序可以保證)的點的個數(sum(x)),然後增加該X坐标對應數組元素的統計值。求和以及增加的操作均是通過樹狀數組完成,sum(x)求count(1)+count(2)+..count(x),增加統計值的操作是modify(x,1)。 看到這道題目的第一感覺就是使用二維樹狀數組,但是根據問題規模,這顯然是不允許的。同時由于輸入的有序性保證了任何新插入的X坐标都是在前面X坐标之上或同一水準線的,是以隻需要記錄X坐标,統計出現在每個位置前面的星星的個數。 這道題目剛開始做的時候,老是TLE。原來以為是輸入輸出問題,找了半天沒有找到答案。後來把題再仔細看了一遍,發現我的測試用例中沒有包括X坐标為0的點。一加上,就死循環了。樹狀數組要求數組下标從1開始,把X坐标做為數組下标肯定要出問題。解決方法是X坐标+1做為數組下标,同時樹狀數組的元素個數等于最大X坐标+1,這樣X坐标等于0的情況就被cover到了。 Code: #include <stdio.h> #define MAXN 32001
#define MAXNUM 15000 static int count[MAXNUM]; static int C[MAXN+1];
static int size; inline int lowbit(int x)
{
return (x & -x);
} inline void modify(int k, int val)
{
while (k <= size)
{
C[k] += val;
k += lowbit(k);
}
} inline int sum(int k)
{
if (k == 0)
return 0; int t = 0;
while (k > 0)
{
t += C[k];
k -= lowbit(k);
}
return t;
} inline int maxFunc(int a, int b)
{
return a>b?a:b;
} inline int toIndex(int x)
{
return x+1;
}
int main(int argc,char **argv)
{
int i,N;
int x[MAXNUM];
int level;
bool ascend = false; scanf("%d",&N); size = 0; for(i=0;i<N;i++)
{
scanf("%d %*d",&x[i]);
size = maxFunc(size,x[i]+1);
} for(i=0;i<N;i++)
{
level = sum(toIndex(x[i]));
count[level]++;
modify(toIndex(x[i]),1);
} for(i=0;i<N;i++)
{
printf("%d/n",count[i]);
} return 0;
}
樹狀數組的典型實作參考文章<<樹狀數組入門>>