题解:贪心思想,按结束时间从小到大排序,选花费时间尽量短的建筑维修,用堆维护。
1 #include<stdio.h> 2 #include<queue> 3 #include<algorithm> 4 using namespace std; 5 const int N = 150001; 6 struct node{ 7 int t1, t2; 8 }s[N]; 9 10 int cmp(node a, node b){ 11 return a.t2 < b.t2; 12 } 13 14 int main(){ 15 int n, t1, t2, i, cur; 16 scanf("%d", &n); 17 for(i = 0; i < n; ++i) 18 scanf("%d %d", &s[i].t1, &s[i].t2); 19 sort(s, s+n, cmp); 20 priority_queue<int>q; 21 for(cur = i = 0; i < n; ++i){ 22 if(cur + s[i].t1 < s[i].t2){ 23 q.push(s[i].t1); 24 cur += s[i].t1; 25 } 26 else if(!q.empty()&& s[i].t1 < q.top()){ 27 cur = cur -q.top() + s[i].t1; 28 q.pop(); 29 q.push(s[i].t1); 30 } 31 } 32 printf("%d\n", q.size()); 33 return 0; 34 }
时间: 2024-10-14 10:57:28