【贪心+堆】XMU 1584 小明的烦恼

来源:互联网 发布:沪江英语网络课程如何 编辑:程序博客网 时间:2024/06/08 01:40
题目链接:

  http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1584

题目大意

  给n(n<=100 000)个任务的耗时和截至时间,问最少不能完成几个任务。

题目思路:

  【贪心+堆】

  一开始想贪心但是没想到要加个堆,又跪了。

  首先按照结束时间排序,结束时间早的肯定优先考虑。

  如果当前的任务无法完成,就将当前任务和之前已经做了的任务中耗时最长的取消掉,改做当前任务

  (如果当前任务就是耗时最长的则不用加当前任务,因为取消一个换另一个结果不会更差,只会使已经消耗的时间减少)

  所以用一个最大堆记录当前的最大耗时。

 

////by coolxxx//#include<iostream>#include<algorithm>#include<string>#include<iomanip>#include<memory.h>#include<time.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<stdbool.h>#include<math.h>#define min(a,b) ((a)<(b)?(a):(b))#define max(a,b) ((a)>(b)?(a):(b))#define abs(a) ((a)>0?(a):(-(a)))#define lowbit(a) (a&(-a))#define sqr(a) ((a)*(a))#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))#define eps 1e-8#define J 10#define MAX 0x7f7f7f7f#define PI 3.1415926535897#define N 100004using namespace std;int n,m,lll,ans,cas;struct xxx{int c,e;}a[N];int h[N];void weihuup(int h[],int x){int xx=x>>1;if(!xx)return;if(h[x]>h[xx]){swap(h[x],h[xx]);weihuup(h,xx);}}void weihudown(int h[],int x){int xx=x+x,yy=x+x+1,zz;if(xx>h[0])return;if(yy<=h[0]){zz=h[xx]>h[yy]?xx:yy;if(h[x]<h[zz]){swap(h[x],h[zz]);weihudown(h,zz);}}else{if(h[x]<h[xx])swap(h[x],h[xx]);}}bool cmp(xxx aa,xxx bb){if(aa.e!=bb.e)return aa.e<bb.e;return aa.c<bb.c;}int main(){#ifndef ONLINE_JUDGE//freopen("1.txt","r",stdin);//freopen("2.txt","w",stdout);#endifint i,j,k;//while(~scanf("%s",s1))while(~scanf("%d",&n)){for(i=1;i<=n;i++){scanf("%d%d",&a[i].c,&a[i].e);}sort(a+1,a+1+n,cmp);for(i=1,k=0;i<=n;i++){if(k+a[i].c<=a[i].e){h[++h[0]]=a[i].c;weihuup(h,h[0]);k+=a[i].c;}else{if(a[i].c>h[1])continue;k=k-h[1]+a[i].c;h[1]=a[i].c;weihudown(h,1);}}printf("%d\n",n-h[0]);}return 0;}/*////*/


0 0