| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
WA了十几次了,实在找不出错误,恳请高人指点一下!!以下是代码#include <stdio.h>
#include <stdlib.h>
#define SIZE 800000
struct info{
int q;
long end;
} order[SIZE];
int heap[SIZE];
void insert(int heap[],long n,int x);
int Delete(int heap[],long n);
int comp(const void *p, const void *q);
int main()
{
long m,i,N,end,max;
while(scanf("%ld",&N) == 1)
{
for(i = 0; i < N; i++)
scanf("%d %ld",&order[i].q,&order[i].end);
/* 以end为标准对order由小到大排序 */
qsort((struct info*)order,N,sizeof(order[0]),comp);
end = max = m = 0;
for(i = 0; i < N; i++)
{
if(order[i].q > order[i].end)
continue;
end += order[i].q;
insert(heap,++m,order[i].q); /* 将order[i].q编入最大堆中 */
max++;
if(order[i].end < end) /* 成立则取出存入的最大q,减去 */
{
end -= Delete(heap,m);
max--;
m--;
}
}
printf("%ld\n",max);
}
return 0;
}
int comp(const void *p, const void *q)
{
struct info px = * (struct info *)p;
struct info qx = * (struct info *)q;
return px.end - qx.end;
}
/* 将x编入最大堆中,x加入后堆中有n个节点 */
void insert(int heap[],long n,int x)
{
long i,j,temp;
i = n;
j = n / 2;
heap[n] = x;
while(j >= 1 && heap[j] < x)
{
temp = heap[j];
heap[j] = heap[i];
heap[i] = temp;
i = j;
j = i / 2;
}
}
/* 返回大小为n的堆的根节点,并且维护堆 */
int Delete(int heap[],long n)
{
long i,j,max,temp;
max = heap[1];
heap[1] = heap[n];
i = 1;
j = 2 * i;
while(j < n - 1 && (heap[i] < heap[j] || heap[i] < heap[j+1]))
{
if(heap[j] >= heap[j+1])
{
temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}
else
{
temp = heap[i];
heap[i] = heap[j+1];
heap[j+1] = temp;
}
i = j;
j = 2 * i;
}
return max;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator