UVA 1153(p255)----Keep the Customer Satisfied

来源:互联网 发布:机锋网推荐的淘宝商家 编辑:程序博客网 时间:2024/06/07 08:22
#include<iostream>#include<cstdio>#include<queue>#include<algorithm>using namespace std;const int maxn=1e6;struct point{    int need,d;    bool operator <(const point& rhs) const    {      return need<rhs.need;    }};int n;point a[maxn];priority_queue<point> q;int cmp(point a,point b){    if(a.d==b.d) return a.need<b.need;    else return a.d<b.d;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        while(!q.empty()) q.pop();        scanf("%d",&n);        for(int i=0;i<n;i++)            scanf("%d%d",&a[i].need,&a[i].d);        sort(a,a+n,cmp);        int now=0;        for(int i=0;i<n;i++)        {            if(now+a[i].need<=a[i].d){q.push(a[i]);now+=a[i].need;}            else            {                point tmp=q.top();                if(!q.empty()&&a[i].need<tmp.need)                {                    now=now-tmp.need+a[i].need;                    q.pop();                    q.push(a[i]);                }            }        }      printf("%d\n",q.size());      if(t) printf("\n");    }    return 0;}

0 0
原创粉丝点击