Peck Chen

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

字典树?!...不会用

排序+筛选A了, 开始没判断那一个人是否被筛掉, 也错了

真是太不小心了...hehe

 

2010-11-20 17:24:45    Accepted    1800    343MS    220K    1803 B    C    Y

 

 

代码
#include <stdio.h>
#include
<stdlib.h>
#include
<string.h>
#define MAX 3001

int elem[MAX];
char *hash;

/* 调整顶为top的m个元素的堆 */
void adjust_heap(int *elem, int top, int m)
{
int i;
int index; /* 保存当前小堆顶元素 */

i
= top * 2;
index
= elem[top-1];
while (i <= m)
{
if (i < m && elem[i-1] < elem[i])
{
i
++; /* 两个子元素之间的大小比较 */
}
if (index >= elem[i-1]) /* 堆已调整完闭 */
{
break;
}

elem[top
-1] = elem[i-1]; /* 调整堆 */
top
= i; /* 修改堆指针 */

i
*= 2; /* 下一个循环条件 */
}
elem[top
-1] = index;
}
/* heap_sort(), adjust_heap(); */
/* 堆排序 */
void heap_sort(int *elem, int len)
{
int i;
int tmp;

for (i = len / 2; i > 0; i--)
{
adjust_heap(elem, i, len);
}

for (i = len; i > 1; i--)
{
/* 每次都与堆顶元素交換 */
tmp
= elem[0];
elem[
0] = elem[i-1];
elem[i
-1] = tmp;

adjust_heap(elem,
1, i - 1); /* 改变堆顶元素后要重新整理堆 */
}
}
/* heap_sort(), adjust_heap() */
/* 求取结果 */
void get_result(int *elem, int len)
{
int i, j, k, cnt;

if ((hash=(char *) calloc (MAX, sizeof(char))) == NULL) {
printf(
"空间分配失败!\n");
exit(
-1);
}
memset(hash,
'0', MAX);
cnt
= 0;
for (i = 0; i < len; i++)
{
if ( hash[i] == '0' ) /* 此人还没有分组, 开始筛选 */
{
cnt
++;
k
= elem[i];
for (j = i; j < len; j++)
{
if (hash[j] == '0' && elem[j] > k) /* 一定要判断此人是否分组了 */
{
k
= elem[j];
hash[j]
= '1';
}
}
}
}

printf(
"%d\n", cnt);
free( hash );
hash
= NULL;
}

int main()
{
int i, N, num;

while (scanf("%d", &N) == 1)
{
i
= 0;
while ( i < N )
{
scanf(
"%d", &elem[i++]);
}

heap_sort(elem, N);
get_result(elem, N);
}

return 0;
}

 

 

 

 

/*
Input
Input file contains multiple test cases.
In a test case,the first line contains a single positive number N indicating
the number of soldiers.(0<=N<=3000)Next N lines :There is only one nonnegative
integer on each line , indicating the level number for each soldier.
( less than 30 digits);

 

Output
For each case, output the minimum number of broomsticks on a single line.

Sample Input
4
10 20 30 04
5

2 3 4 3 4
*/

2010-11-20 17:24:45    Accepted    1800    343MS    220K    1803 B    C    Y
posted on 2010-11-20 17:26  PeckChen  阅读(454)  评论(0编辑  收藏  举报