P3545HUR-Warehouse Store&P4053建筑抢修(反悔贪心、堆)

10 篇文章 0 订阅

解析

两道很像的题
不能说相似,只能说是一模一样
感觉有火车载客的影子
想到开个堆来维护
在供不应求时弹出要求最高的即可
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
*/

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值