Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11234 Accepted Submission(s):
4472
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.
给定一个星象图,请你写一个程序计算各个等级的星星数目。输入的第一行包含星星的总数N (1<=N<=15000)。接下来N行,描述星星的坐标(X,Y)(X和Y用空格分开,0<=X,Y<=32000)。星象图中的每个点处最多只有一颗星星。所有星星按Y坐标升序排列。Y坐标相等的星星按X坐标升序排列。
/* 前面的必定比当前的低 只需要考虑在当前的左边的 */
给出(x,y)只要求1~~x的和即可
1 #include <iostream> 2 #include <algorithm> 3 #include<cstdio> 4 #include<string> 5 #define n 32002 //n=32000时WA了N多次... 6 int c[n + 5]; 7 int total[n + 5];//存该等级有多少颗星星 8 9 void add(int k) 10 { 11 while (k <= n) 12 { 13 c[k] ++; 14 k += (k&(-k)); 15 } 16 } 17 18 int sum(int k)//算它是几等级 19 { 20 int s = 0; 21 while (k > 0)//算前k的和,就是它的等级 22 { 23 s += c[k]; 24 k -=(k&(-k)); 25 } 26 return s; 27 } 28 29 int main() 30 { 31 int i, j, x, y, nn; 32 while (scanf("%d", &nn) != EOF) 33 { 34 memset(c, 0, sizeof(c)); 35 memset(total, 0, sizeof(total)); 36 for (i = 1; i <= nn; i++) 37 { 38 scanf("%d%d", &x, &y);//由于坐标x可能为0,因此输入坐标要+1,不然会超时0&(-0)=0; 39 add(x + 1); 40 total[sum(x + 1) - 1]++;//减回去 41 /*x++; 42 total[sum(x)]++; 43 add(x);*/ 44 } 45 for (i = 0; i < nn; i++) 46 printf("%d\n", total[i]); 47 } 48 return 0; 49 }