这个贪心很显然跟t1有关 跟t2也有关,
但可以确定的是 报废时间长 的一定排在后面
但 报废时间都很长的时候 我们需要决策
比如 50/100 和2/101 这个时候我们显然选后者
这个用一个简单的堆维护就好了
#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <cstdio>
#include <bitset>
#include <vector>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define UpMing main
#define re register
#pragma GCC optimize(2)
#define Accept return 0;
#define lowbit(x) ((x)&(-(x)))
#define mst(x, a) memset( x,a,sizeof(x) )
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
const int inf =0x3f3f3f3f;
const int maxn=2e5+7;
const ll mod = 1e9+7;
const int N =1e6+7;
inline ll read() {
ll x=0;
bool f=0;
char ch=getchar();
while (ch<'0'||'9'<ch) f|=ch=='-', ch=getchar();
while ('0'<=ch && ch<='9')
x=x*10+ch-'0',ch=getchar();
return f?-x:x;
}
void out(ll x) {
int stackk[20];
if(x<0) {
putchar('-');
x=-x;
}
if(!x) {
putchar('0');
return;
}
int top=0;
while(x) stackk[++top]=x%10,x/=10;
while(top) putchar(stackk[top--]+'0');
}
ll n,ans,b[maxn];
struct wmy {
ll t1,t2;
} a[maxn];
bool cmp(wmy x,wmy y) {
if(x.t2!=y.t2)
return x.t2<y.t2;
return x.t1<y.t1;
}
priority_queue<ll>q;
int UpMing() {
n=read();
for(int i=1 ; i<=n ; i++) {
a[i].t1=read();
a[i].t2=read();
}
sort(a+1,a+1+n,cmp);
ll t=0;
for(int i=1 ; i<=n ; i++) {
if(t+a[i].t1<=a[i].t2)
q.push(a[i].t1),ans++,t+=a[i].t1;
else {
if(a[i].t1<q.top())
t=t-q.top()+a[i].t1, q.pop(),q.push(a[i].t1);
}
}
out(ans);
Accept;
} 
京公网安备 11010502036488号