HDU-1040 as easy as a+b
2011-08-16 21:24
435 查看
其实如果用堆排序来写,这个题就比a+b难!
/*
* HDU-1040 as easy as a+b
* mike-w
* 2011-8-16
* PS: 练习堆排序的题--当然,如果用堆排序做,这就是练习堆排序的题目:p
*/
#include<stdio.h>
#define MAXN 1010
long heap[MAXN];
int swap(long *t1,long *t2)
{
long t3=*t1;
*t1=*t2;
*t2=t3;
return 0;
}
int insert(long e)
{
int n=++heap[0];
heap
=e;
while(n>1&&heap
<heap[n/2])
swap(heap+n,heap+n/2),n/=2;
return 0;
}
long pop(void)
{
long ret=heap[1];
swap(heap+1,heap+heap[0]);
heap[0]--;
long n=1,t=1;
while((t=n<<1)<=*heap)
if(t+1<=*heap && heap[t+1]<heap[t] && heap[t+1]<heap
)
swap(heap+n,heap+t+1),n=t+1;
else if(heap[t]<heap
)
swap(heap+t,heap+n),n=t;
else
break;
return ret;
}
int main(void)
{
int ncase,nnum,i;
long t;
scanf("%d",&ncase);
while(ncase-->0)
{
scanf("%d",&nnum);
for(i=0;i<nnum;i++)
scanf("%ld",&t),insert(t);
for(i=0;i<nnum;i++)
printf("%ld%c",pop(),(i==nnum-1?'\n':' '));
}
return 0;
}
/*
* HDU-1040 as easy as a+b
* mike-w
* 2011-8-16
* PS: 练习堆排序的题--当然,如果用堆排序做,这就是练习堆排序的题目:p
*/
#include<stdio.h>
#define MAXN 1010
long heap[MAXN];
int swap(long *t1,long *t2)
{
long t3=*t1;
*t1=*t2;
*t2=t3;
return 0;
}
int insert(long e)
{
int n=++heap[0];
heap
=e;
while(n>1&&heap
<heap[n/2])
swap(heap+n,heap+n/2),n/=2;
return 0;
}
long pop(void)
{
long ret=heap[1];
swap(heap+1,heap+heap[0]);
heap[0]--;
long n=1,t=1;
while((t=n<<1)<=*heap)
if(t+1<=*heap && heap[t+1]<heap[t] && heap[t+1]<heap
)
swap(heap+n,heap+t+1),n=t+1;
else if(heap[t]<heap
)
swap(heap+t,heap+n),n=t;
else
break;
return ret;
}
int main(void)
{
int ncase,nnum,i;
long t;
scanf("%d",&ncase);
while(ncase-->0)
{
scanf("%d",&nnum);
for(i=0;i<nnum;i++)
scanf("%ld",&t),insert(t);
for(i=0;i<nnum;i++)
printf("%ld%c",pop(),(i==nnum-1?'\n':' '));
}
return 0;
}
相关文章推荐
- Hdu 1040 - As Easy As A+B
- hdu 1040 As Easy As A+B
- hdu 1040 As Easy As A+B
- HDU1040_As Easy As A+B
- HDU 1040 As Easy As A+B (排序。。。水题)
- hdu 1040 As Easy As A+B (水题)
- HDU 1040 As Easy As A+B
- HDU 1040 As Easy As A+B
- hdu-1040-As Easy As A+B(冒泡排序)
- HDU 1040 As Easy As A+B
- ACM-简单题之As Easy As A+B——hdu1040
- HDU 1040 As Easy As A+B
- hdu 1040 As Easy As A+B
- 【HDU】1040 As Easy As A+B
- hdu-1040-As Easy As A+B
- hdu 1040 As Easy As A+B(排序)
- As Easy As A+B hdu 1040
- HDU's ACM 1040 As Easy As A+B
- HDU 1040 As Easy As A+B(排序)
- HDU 1040 As Easy As A+B(深感hdu水题太多。。。)