標籤:stream images ima 分享 color while bsp pll set
http://www.lydsy.com/JudgeOnline/problem.php?id=1029
題意:
思路:
先按T2排序,並維護一個T1的優先隊列,然後對於每個建築,如果當前這個建築能修的話,那麼就修。如果不能修,那麼就從優先隊列中彈出之前的建築中所需修複最長的時間,如果該時間比我當前建築時間還長並且去掉它後就能修理當前建築,那麼我們就放棄那個,改為修理當前建築,因為這樣一來總的時間會更小。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<sstream> 6 #include<vector> 7 #include<stack> 8 #include<queue> 9 #include<cmath>10 #include<map>11 #include<set>12 using namespace std;13 typedef long long ll;14 typedef pair<int,ll> pll;15 const int INF = 0x3f3f3f3f;16 const int maxn=50000+5;17 18 int n;19 20 struct node21 {22 int cost,end;23 }a[150005];24 25 bool cmp(node a, node b)26 {27 return a.end<b.end;28 }29 30 bool operator<(node& a, node& b)31 {32 return a.cost>b.cost;33 }34 35 int main()36 {37 //freopen("in.txt","r",stdin);38 while(~scanf("%d",&n))39 {40 for(int i=1;i<=n;i++) scanf("%d%d",&a[i].cost,&a[i].end);41 sort(a+1,a+1+n,cmp);42 priority_queue<int> Q;43 44 int t=0;45 int ans=0;46 for(int i=1;i<=n;i++)47 {48 if(t+a[i].cost<=a[i].end) {ans++;t+=a[i].cost;Q.push(a[i].cost);}49 else50 {51 if(t-Q.top()+a[i].cost<=a[i].end && Q.top()>=a[i].cost)52 {53 t=t-Q.top()+a[i].cost;54 Q.pop();55 Q.push(a[i].cost);56 }57 }58 }59 printf("%d\n",ans);60 }61 return 0;62 }
BZOJ 1029: [JSOI2007]建築搶修(貪心)