1. 程式人生 > >bzoj1029: [JSOI2007]建築搶修

bzoj1029: [JSOI2007]建築搶修

zoj tdi class cmp pac col printf scanf space

今天是真的萎,切不動題,瞎寫陳年老題找自信。。。

強行一波貪心猛如虎

先按毀壞時間排序,枚舉,能修的就修,修不了就把前面耗時最長的拿出來和當前比較,假如現在需要時間更短就換

總之就是維護修了ans個的最快時間

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;

struct node { int t,ed; }a[210000]; bool cmp(node n1,node n2){return n1.ed<n2.ed;} multiset<int> s; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d%d",&a[i].t,&a[i].ed); sort(a+1,a+n+1,cmp); int now=0,ans=0; for(int i=1
;i<=n;i++) { if(a[i].t+now<=a[i].ed) { now+=a[i].t; ans++; s.insert(a[i].t); } else { int q=*--s.end(); if(a[i].t<q) { int sum=s.count(q); s.erase(q);
for(int j=1;j<sum;j++)s.insert(q); s.insert(a[i].t); now+=a[i].t-q; } } } printf("%d\n",ans); return 0; }

bzoj1029: [JSOI2007]建築搶修