1029: [JSOI2007]建筑抢修
Description
小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的
入侵者。但是T部落的基地里已经有N个建筑设施受到了严重的损伤,如果不尽快修复的话,这些建筑设施将会完全
毁坏。现在的情况是:T部落基地里只有一个修理工人,虽然他能瞬间到达任何一个建筑,但是修复每个建筑都需
要一定的时间。同时,修理工人修理完一个建筑才能修理下一个建筑,不能同时修理多个建筑。如果某个建筑在一
段时间之内没有完全修理完毕,这个建筑就报废了。你的任务是帮小刚合理的制订一个修理顺序,以抢修尽可能多
的建筑。
Input
第一行是一个整数N接下来N行每行两个整数T1,T2描述一个建筑:修理这个建筑需要T1秒,如果在T2秒之内还
没有修理完成,这个建筑就报废了。
Output
输出一个整数S,表示最多可以抢修S个建筑.N < 150,000; T1 < T2 < maxlongint
Sample Input
4
100 200
200 1300
1000 1250
2000 3200
100 200
200 1300
1000 1250
2000 3200
Sample Output
3
贪心,先按终点排序,然后从前向后插入,然后判断当前点和之前用时最多的那个点那个大,用更小的值更新。。。
这样就好了。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<cstdio> 6 #include<algorithm> 7 #include<string> 8 #include<map> 9 #include<queue> 10 #include<vector> 11 #include<set> 12 #define inf 1000000000 13 #define maxn 150000+5 14 #define maxm 10000+5 15 #define eps 1e-10 16 #define ll long long 17 #define for0(i,n) for(int i=0;i<=(n);i++) 18 #define for1(i,n) for(int i=1;i<=(n);i++) 19 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 20 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 22 using namespace std; 23 int read(){ 24 int x=0,f=1;char ch=getchar(); 25 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 26 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 27 return x*f; 28 } 29 struct data{ 30 int w,end; 31 }a[maxn]; 32 bool cmp(data a,data b){ 33 return a.end<b.end; 34 } 35 priority_queue<int> f; 36 int main(){ 37 //freopen("input.txt","r",stdin); 38 //freopen("output.txt","w",stdout); 39 int n=read(); 40 for1(i,n){ 41 a[i].w=read();a[i].end=read(); 42 } 43 sort(1+a,1+a+n,cmp); 44 int now=0,ans=0; 45 for1(i,n){ 46 if(now+a[i].w<=a[i].end){ 47 ans++; 48 now+=a[i].w; 49 f.push(a[i].w); 50 } 51 else{ 52 if(a[i].w<f.top()){ 53 now=now+a[i].w-f.top(); 54 f.pop(); 55 f.push(a[i].w); 56 } 57 } 58 } 59 printf("%d",ans); 60 return 0; 61 }