UVA 1153 Keep the Customer Satisfied(贪心2.0)
2016-10-01 17:24
302 查看
题意
给出每个工作的耗费时间(q)和截止日期(d),一个时间只能完成一个工作,问:最多可以完成几个工作。分析
贪心的想法先按照 截止日期进行排序。
如果 t时间内完成了 k 个任务,那么如何去完成 k+1个任务呢?
如果 c[k+1].q + t <= c[k+1].很好加入任务中,
否则 如果 任务列表中存在比c[k+1].q 还要耗时的,就 两者交换,这样 t 的值就减小了。
这里可以反证一下,如果只是 在不满足的时候就continue的话,任务数量和时间都是没有改变的。相比而言,上个情况会更优。
这里交的时候 re了 两发。刚开始还以为 数组开的小 或者 中间变量溢出了。
后来 才知道是 优先队列 取值时,没有判读是否非空。。。
这里 备注一下,re 的几种情况:
越栈了,比如数组开的不够大
编译错误时Compilation Error
Runtime Error (RE) : 运行时错误,这个一般是程序在运行期间执行了非法的操作造成的。以下列出常见的错误类型:
ACCESS_VIOLATION 您的程序想从一些非法的地址空间读取或向其中写入内容。一般例如指针、数组下标越界都会造成这个错误的。
ARRAY_BOUNDS_EXCEEDED 您的程序试图访问一个超出硬件支持范围的数组单元。
FLOAT_DENORMAL_OPERAND 进行了一个非正常的浮点操作。一般是由于一个非正常的浮点数参与了浮点操作所引起的,比如这个数的浮点格式不正确。
FLOAT_DIVIDE_BY_ZERO 浮点数除法出现除数为零的异常。
FLOAT_OVERFLOW 浮点溢出。要表示的数太大,超出了浮点数的表示范围。
FLOAT_UNDERFLOW 浮点下溢。要表示的数太小,超出了浮点数的表示范围。
INTEGER_DIVIDE_BY_ZERO 在进行整数除法的时候出现了除数为零的异常。
INTEGER_OVERFLOW 整数溢出。要表示的数值太大,超出了整数变量的范围。
STACK_OVERFLOW 栈溢出。一般是由于无限递归或者在函数里使用了太大的数组变量的原因。
…… 其他错误,包括C++标准库/STL运行时库错误等,这里不再举例。
总结一下,就是 代码出现了 越栈 ,索引越界,类型溢出, 除以0.
#include <bits/stdc++.h> using namespace std; const int maxn = 8e5+1000; int n; struct cc { int l, r; }; cc c[maxn]; bool cmp(cc x, cc y) { if(x.r != y.r) return x.r < y.r; return x.l < y.l; } int solve() { priority_queue<int> pq; long long t = 0; for(int i = 0; i < n; i++) { if(c[i].l + t <= c[i].r) { pq.push(c[i].l); t += c[i].l; } else if(!pq.empty()) { int m = pq.top(); if(m > c[i].l) { pq.pop(); pq.push(c[i].l); t = t - m + c[i].l; } } } return pq.size(); } int main() { //freopen("in.txt","r",stdin); int t; cin >> t; while(t--) { cin >> n; for(int i = 0; i < n; i++) { cin >> c[i].l >> c[i].r; } sort(c,c+n,cmp); cout << solve() << endl; if(t) cout << endl; } return 0; }
相关文章推荐
- 习题8-12 UVA - 1153 Keep the Customer Satisfied 顾客是上帝(优先队列贪心)
- uva 1153 - Keep the Customer Satisfied(贪心+优先队列)
- UVA 1153 - Keep the Customer Satisfied(贪心)
- UVa 1153 Keep the Customer Satisfied 【贪心 优先队列】
- UVA 1153 Keep the Customer Satisfied 顾客是上帝(贪心)
- UVA1153-Keep the Customer Satisfied(贪心+优先队列)
- UVa 1153 - Keep the Customer Satisfied (贪心 + 优先队列)
- UVA 1153 Keep the Customer Satisfied(贪心+优先队列)
- UVa 1153 Keep the Customer Satisfied 解题报告(贪心)
- Uva 1153 Keep the Customer Satisfied(贪心)
- UVA 1153 Keep the Customer Satisfied 顾客是上帝 经典贪心 (SWE 2005)
- UVa 1153 - Keep the Customer Satisfied(贪心)
- UVA-1153 Keep the Customer Satisfied (贪心)
- UVa 1153 Keep the Customer Satisfied (贪心+优先队列)
- UVA - 1153 Keep the Customer Satisfied(贪心)
- UVa 1153 Keep the Customer Satisfied (贪心+优先队列)
- UVA 1153 - Keep the Customer Satisfied
- UVALive - 3507 Keep the Customer Satisfied 贪心
- uva 1153—— Keep the Customer Satisfied
- UVALive 3507:Keep the Customer Satisfied(贪心 Grade C)