Time Limit: 4 Sec Memory Limit: 162 MB
Submit: 5866 Solved: 2634
[Submit][Status][Discuss]
Descriptionios
小剛在玩JSOI提供的一個稱之爲「建築搶修」的電腦遊戲:通過了一場激烈的戰鬥,T部落消滅了全部z部落的
入侵者。可是T部落的基地裏已經有N個建築設施受到了嚴重的損傷,若是不盡快修復的話,這些建築設施將會徹底
毀壞。如今的狀況是:T部落基地裏只有一個修理工人,雖然他能瞬間到達任何一個建築,可是修復每一個建築都需
要必定的時間。同時,修理工人修理完一個建築才能修理下一個建築,不能同時修理多個建築。若是某個建築在一
段時間以內沒有徹底修理完畢,這個建築就報廢了。你的任務是幫小剛合理的制訂一個修理順序,以搶修儘量多
的建築。
Inputgit
第一行是一個整數N接下來N行每行兩個整數T1,T2描述一個建築:修理這個建築須要T1秒,若是在T2秒以內還
沒有修理完成,這個建築就報廢了。
Outputweb
輸出一個整數S,表示最多能夠搶修S個建築.N < 150,000; T1 < T2 < maxlongint
Sample Input
4svg
100 200ui
200 1300spa
1000 1250code
2000 3200
Sample Output
3 xml
思路題,首先按截止時間排序。可是若是直接按截止時間作顯然錯誤,因此咱們思考持續時間。咱們先貪心的選取截止時間考前的,若是發現一個選不了的,就從已經選了的挑出持續時間最長的,若是大於此時枚舉的,就替換,這裏用優先隊列維護。排序
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#define LL long long
using namespace std;
const int MAXN = 150005;
inline LL rd(){
LL x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-48;ch=getchar();}
return x*f;
}
int n;
LL ans;
priority_queue<int> q;
struct BUILD{
LL st,ed;
}build[MAXN];
inline bool cmp(BUILD A,BUILD B){
if(A.ed==B.ed) return A.st<B.st;
return A.ed<B.ed;
}
//inline void dfs(int x,int last,int sum){
// if(x==n+1) {ans=max(ans,sum);return;}
// if(sum+(n-x)<=ans) return;
// if(last+build[x].st<=build[x].ed) dfs(x+1,last+build[x].st,sum+1);
// dfs(x+1,last,sum);
//}
int main(){
n=rd();
for(register int i=1;i<=n;i++){
int x=rd();
build[i].ed=rd();
build[i].st=x;
}
sort(build+1,build+1+n,cmp);
// dfs(1,0,0);
LL now=0;
for(register int i=1;i<=n;i++){
if(now+build[i].st<=build[i].ed){
ans++;
q.push(build[i].st);
now+=build[i].st;
}
else{
LL x=q.top();
if(x>build[i].st) {
q.pop();
now-=x;
now+=build[i].st;
q.push(build[i].st);
}
}
}
printf("%lld",ans);
return 0;
}