| ||||||||||
| 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 | |||||||||
time limited!晕!大家有什么更好的算法呀#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef struct
{
int w;
int d;
}Order;
Order a[800001];
short flag[800001];
bool cmp(Order x,Order y)
{
return x.d<y.d;
}
int main(void)
{
int num,i,j,k;
int counter,pos,max;
while(scanf("%d",&num)!=EOF)
{
for(i=0;i<num;i++)
scanf("%d%d",&a[i].w,&a[i].d);
memset(flag,0,sizeof(flag));
counter=num;
pos=0;
sort(a,a+num,cmp);
for(i=0;i<num;i++)
{
pos+=a[i].w;
if(pos>a[i].d)
{
max=0;
for(j=0;j<=i;j++)
if((!flag[j])&&a[j].w>max)
{
max=a[j].w;
k=j;
}
flag[k]=1;
pos-=max;
counter--;
}
}
printf("%d\n",counter);
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator