MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
题目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=1541
题目描述:
Stars
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 857 Accepted Submission(s): 320
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.
5 1 1 5 1 7 1 3 3 5 5
1 2 1 1 0
题目的意思就是计算 星星的左下角 ( 包括 正左, 正下 ) 有多少颗其他的星星, 左下角星星的个数就是这个星星的等级, 题目要求输出每个等级星星的个数.
具体做法: 另外开个数组存储星星的等级,比他小的数的个数作为下标,即等级,每出现一次,数组加一,com数组的大小即为它存储数据的最大值。
代码如下 :
/*
MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
http://www.cnblog.com/MiYu
Author By : MiYu
Test : 1
Program : 1541
*/
#include <iostream>
using namespace std;
const int MAX = 32005;
#define lowbit(x) ((x)&(-x))
int com[MAX], res[MAX], N;
void modify ( int pos, int val ){
while ( pos <= MAX ){
com[pos] += val;
pos += lowbit(pos);
}
}
int quy ( int x ){
int sum = 0;
while ( x > 0 ){
sum += com[x];
x -= lowbit(x);
}
return sum;
}
int main (int x)
{
while ( scanf ( "%d",&N ) != EOF ){
memset ( com,0,sizeof (com) ); memset ( res,0,sizeof (res) );
for ( int i = 1; i <= N; ++ i ){
scanf ( "%d%*d", &x );
x++;
res[ quy(x) ] ++; modify ( x,1 );
}
for ( int i = 0; i < N; ++ i )
printf ( "%d\n",res[i] );
}
return 0;
}