uvalive 4850(贪心)
来源:互联网 发布:人工智能 招聘 深圳 编辑:程序博客网 时间:2024/06/05 02:50
题意:有n个任务,每个任务都有完成时间和完成期限,惩罚值就是超出期限的时间,问所有任务完成的最大和第二大惩罚值之和最小是多少。
题解:先排序,期限越小的排前面,否则就时间短的先完成,但只这样不一定是最优解,可以牺牲某个任务使最终解更优,所以将牺牲掉任务的位置放到之前得到的第二大惩罚值的前面,这样可以使最大惩罚值减小,遍历之前的所有任务,更新最小值。
#include <stdio.h>#include <algorithm>using namespace std;const int N = 505;struct Work {int s, d;}w[N];int n, pos;int cmp(Work a, Work b) {if (a.d != b.d)return a.d < b.d;return a.s < b.s;}int solve(int x) {int max1 = 0, max2 = 0, t = 0;for (int i = 0; i <= pos; i++) {if (i == x)continue;t += w[i].s;int k = max(0, t - w[i].d);max2 = max(max2, k);if (max2 > max1)swap(max1, max2);}t += w[x].s;int k = max(0, t - w[x].d);max2 = max(max2, k);return max1 + max2;}int main() {int t;scanf("%d", &t);while (t--) {scanf("%d", &n);for (int i = 0; i < n; i++)scanf("%d%d", &w[i].s, &w[i].d);sort(w, w + n, cmp);int max1 = 0, max2 = 0, t = 0;for (int i = 0; i < n; i++) {t += w[i].s;if (t - w[i].d >= max2) {pos = i;max2 = t - w[i].d;}if (max2 > max1)swap(max1, max2);}int res = max1 + max2;for (int i = 0; i < pos; i++)res = min(res, solve(i));printf("%d\n", res);}return 0;}
0 0
- uvalive 4850(贪心)
- UVALive - 4850 Installations 贪心+枚举
- UVAlive 2911 Maximum(贪心)
- uvalive 2322(贪心)
- uvalive 2326(贪心)
- uvalive 2519(贪心)
- uvalive 3971(贪心)
- uvalive 3635(贪心)
- uvalive 2911(贪心)
- uvalive 2949(二分+贪心)
- uvalive 4725(贪心 + 二分)
- uvalive 3266(贪心)
- uvalive 4094(贪心)
- uvalive 2757(贪心)
- uvalive 4328(贪心)
- UVALive - 5848 Soju 贪心
- UVALive 7416 贪心 multiset
- UVALive 3177-贪心+二分
- 计算两点之间的距离(笔记)
- Android中的动画效果
- IBM supervessel power云平台 之 crontab定时任务篇
- 数据结构之程序性能检测(一):三种排序算法·对比
- 指针函数与函数指针的区别
- uvalive 4850(贪心)
- 陀螺仪
- Android Studio功能
- Android开发之性能调优工具
- leetcode:Valid Palindrome 菜鸟解法
- 120 - Stacks of Flapjacks
- hibernate:java.math.BigInteger cannot be cast to java.lang.Long
- 714 - Copying Books
- 1152 - 4 Values whose Sum is 0