A33 贪心算法 P4053 [JSOI2007] 建筑抢修

视频链接:83 贪心算法 [JSOI2007] 建筑抢修_哔哩哔哩_bilibili

 

 

Luogu P4053 [JSOI2007] 建筑抢修

复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

struct node{
  int a,b; //修理时间,报废时间
  bool operator<(node &x){
    return b<x.b;
  }
}t[150005];
priority_queue<int> q;//维护修理时间

int main(){
  int n; scanf("%d",&n);
  for(int i=1; i<=n; i++)
    scanf("%d%d",&t[i].a,&t[i].b);
  sort(t+1, t+1+n);
  
  long long sum=0; //修理时间之和
  int ans=0;
  for(int i=1; i<=n; i++){
    sum+=t[i].a;
    q.push(t[i].a);
    if(sum<=t[i].b) //能修,则ans+1
      ans++;
    else //不能修,则抛弃最长修理时间
      sum-=q.top(),q.pop();
  }
  printf("%d\n",ans);
  return 0;
}
复制代码

 

posted @   董晓  阅读(308)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示