题目大意:n个建筑须要抢修。第i个建筑须要T1时间抢修。必须在T2时间之前抢修完成。求最多能抢修多少建筑

首先我们对T2排序 然后依次修理 可是这样贪心显然是不对的 比方说这组数据:

5

10 10

10 20

2 21

2 21

2 21

贪心仅仅能修理前两个。而实际上最多能够修理4个

于是我们考虑修正贪心算法

比方说这组数据,当我们枚举到3的时候。尽管已经无法修理很多其它了,可是我们能够取消修理建筑1而改修理3。这样尽管不能更新ans 可是能够为后面的建筑节省时间

所以做法就非常明白了

我们维护一个大根堆 每修理一栋建筑 我们就把这栋建筑的T1值增加堆 若当前无法修理 我们推断堆顶是否比这栋建筑的T1大 假设大 取消修理堆顶。改为修理当前建筑

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define M 150100
using namespace std;
struct construction{
int T1,T2;
bool operator < (const construction &x) const
{
return T2 < x.T2;
}
}buildings[M];
int n,ans,now,heap[M],top;
void Insert(int x)
{
heap[++top]=x;
int t=top;
while(t>1&&heap[t]>heap[t>>1])
swap(heap[t],heap[t>>1]),t>>=1;
}
void Pop()
{
heap[1]=heap[top--];
int t=2;
while(t<=top)
{
if(t<top&&heap[t+1]>heap[t])
++t;
if(heap[t]>heap[t>>1])
swap(heap[t],heap[t>>1]),t<<=1;
else
break;
}
}
int main()
{
int i;
cin>>n;
for(i=1;i<=n;i++)
scanf("%d%d",&buildings[i].T1,&buildings[i].T2);
sort(buildings+1,buildings+n+1);
for(i=1;i<=n;i++)
{
if(now+buildings[i].T1<=buildings[i].T2)
{
now+=buildings[i].T1;
++ans;
Insert(buildings[i].T1);
}
else
{
if(!top)
continue;
int temp=heap[1];
if( temp>buildings[i].T1 )
now=now+buildings[i].T1-temp,Pop(),Insert(buildings[i].T1);
}
}
cout<<ans<<endl;
}

最新文章

  1. mybatis配置自带缓存和第三方缓存
  2. Python: Win7 64位如何安装MongoDB?
  3. oracle中merge方法
  4. 【Python③】python基本数据类型,变量和常量
  5. 与Python Falling In Love_Python跨台阶(面向对象)
  6. cf118A(水题)
  7. Swift(三.函数)
  8. 聊聊js运算符 ‘与(&amp;&amp;)’和‘ 或(||)’
  9. 一个js编写全选、弹出对话框、ajax-json的案例
  10. 用 for/in 在 Java 5.0 中增强循环
  11. ios学习之常见问题记录
  12. 如何通过jQuery获取一个没有定高度的元素---------的自适应高度(offsetHeight的正确使用方法)
  13. 文件操作命令(rename)
  14. Beta冲刺(6/7)
  15. k8s namespace/volume
  16. NMAP执行脚本smb-check-vulns.nse出错
  17. Hadoop生态集群hdfs原理(转)
  18. c# 之 事务
  19. HTTP 错误 500.XX - Internal Server Error 解决办法
  20. R语言低级绘图函数-abline

热门文章

  1. 避免闪烁的方法(OnEraseBkgnd)
  2. 安装使用ionic3
  3. 【CSWS2014 Main Conference】Some Posters
  4. quartz实现定时任务调度
  5. android开发中WebView的使用(附完整程序)
  6. C#多线程之 ManualResetEvent和AutoResetEvent
  7. 升级 asp.net core 1.1 到 2.0 preview
  8. 错误号:1364 错误信息:Field &amp;#39;platId&amp;#39; doesn&amp;#39;t have a default value
  9. 【mysql】Innodb三大特性之adaptive hash index
  10. [转载][转]修改/proc目录下的参数优化网络性能