解析
两道很像的题
不能说相似,只能说是一模一样
感觉有火车载客的影子
想到开个堆来维护
在供不应求时弹出要求最高的即可
T1还得输出方案针麻烦
代码
你会发现几乎一毛一样
T1
#include<bits/stdc++.h>
const int N=3e5+100;
const int M=2e3+100;
const int mod=1e9+7;
#define ll long long
#define ull unsigned long long
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
inline ll read() {
ll x(0),f(1);char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-1;c=getchar();}
while(isdigit(c)) {x=(x<<1)+(x<<3)+c-'0';c=getchar();}
return x*f;
}
int n,m;
#define pr pair<int,int>
#define mkp make_pair
priority_queue<pr>q;
int ans;
ll now,a[N],b[N],tot;
bool vis[N];
int main(){
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
n=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=1;i<=n;i++) b[i]=read();
for(int i=1;i<=n;i++){
ans++;q.push(mkp(b[i],i));tot+=a[i];now+=b[i];
vis[i]=1;
while(now>tot){
pr o=q.top();q.pop();
now-=o.first;ans--;
vis[o.second]=0;
}
}
printf("%d\n",ans);
for(int i=1;i<=n;i++){
if(vis[i]) printf("%d ",i);
}
return 0;
}
/*
5 3
7 1 4 1 9
1 3 5 3
1 1 4 2
2 3 5
*/
T2
#include<bits/stdc++.h>
const int N=2e5+100;
const int M=2e3+100;
const int mod=1e9+7;
#define ll long long
#define ull unsigned long long
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
inline ll read() {
ll x(0),f(1);char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-1;c=getchar();}
while(isdigit(c)) {x=(x<<1)+(x<<3)+c-'0';c=getchar();}
return x*f;
}
int n,m;
priority_queue<ll>q;
struct node{
ll cost,ed;
bool operator < (const node o)const{return ed<o.ed;}
}p[N];
int main(){
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
n=read();
for(int i=1;i<=n;i++) p[i]=(node){read(),read()};
sort(p+1,p+1+n);
int ans(0);ll sum(0);
for(int i=1;i<=n;i++){
sum+=p[i].cost;q.push(p[i].cost);ans++;
while(sum>p[i].ed){
sum-=q.top();q.pop();ans--;
}
}
printf("%d\n",ans);
return 0;
}
/*
5 3
7 1 4 1 9
1 3 5 3
1 1 4 2
2 3 5
*/