HDU1789 Doing Homework again 【贪心】
Doing Homework again
his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced
scores.
3
3
3 3 3
10 5 1
3
1 3 1
6 2 3
7
1 4 6 4 2 4 3
3 2 1 7 6 5 4
0
3
5
题意:有n个任务,每一个任务完毕须要一整天。给出每一个任务完毕的期限和完不成的惩处值。求最小惩处值。
题解:看完这题的第一反应是二分图最优匹配。然后就直接上模板了。结果是超时,后来发现这题能够用贪心,每次选择惩处值最大的任务安排,安排的规则为从该任务的期限后面往前选择一个没有安排任务的天数,将该任务安插在这一天。
贪心:
#include <stdio.h>
#include <string.h>
#include <algorithm> #define maxn 1010 struct Node {
int days, penalty;
} arr[maxn];
bool vis[maxn]; bool cmp(Node a, Node b) {
return a.penalty > b.penalty;
} int main() {
// freopen("stdin.txt", "r", stdin);
int t, n, i, j, sum, sum2;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
sum = sum2 = 0;
for(i = 0; i < n; ++i)
scanf("%d", &arr[i].days);
for(i = 0; i < n; ++i) {
scanf("%d", &arr[i].penalty);
sum += arr[i].penalty;
}
std::sort(arr, arr + n, cmp);
memset(vis, 0, sizeof(vis));
for(j = 0; j < n; ++j)
for(i = arr[j].days; i; --i)
if(!vis[i]) {
vis[i] = true;
sum2 += arr[j].penalty;
break;
}
printf("%d\n", sum - sum2);
}
return 0;
}
TLE的KM:
#include <stdio.h>
#include <string.h> #define maxn 1010
#define inf 0x3f3f3f3f int G[maxn][maxn];
int Lx[maxn], Ly[maxn];
int match[maxn];
bool visx[maxn], visy[maxn];
int slack[maxn], n, m, sum; bool DFS(int cur) {
int t, y;
visx[cur] = true;
for(y = 1; y <= n; ++y) {
if(visy[y]) continue;
t = Lx[cur] + Ly[y] - G[cur][y];
if(0 == t) {
visy[y] = true;
if(-1 == match[y] || DFS(match[y])) {
match[y] = cur; return true;
}
} else if(slack[y] > t) slack[y] = t;
}
return false;
} int KM() {
int i, j, x, d, ret = 0;
memset(match, -1, sizeof(int) * (n + 1));
memset(Ly, 0, sizeof(int) * (n + 1));
for(i = 1; i <= n; ++i) {
Lx[i] = -inf;
for(j = 1; j <= n; ++j)
if(G[i][j] > Lx[i]) Lx[i] = G[i][j];
}
for(x = 1; x <= n; ++x) {
memset(slack, 0x3f, sizeof(int) * (n + 1));
while(true) {
memset(visx, 0, sizeof(bool) * (n + 1));
memset(visy, 0, sizeof(bool) * (n + 1));
if(DFS(x)) break;
d = inf;
for(i = 1; i <= n; ++i)
if(!visy[i] && d > slack[i])
d = slack[i];
for(i = 1; i <= n; ++i)
if(visx[i]) Lx[i] -= d;
for(i = 1; i <= n; ++i)
if(visy[i]) Ly[i] += d;
else slack[i] -= d;
}
}
for(i = 1; i <= n; ++i)
if(match[i] > -1)
ret += G[match[i]][i];
return ret;
} void getMap() {
int i, w, j;
sum = 0;
scanf("%d", &n);
memset(G, 0, sizeof(G));
for(i = 1; i <= n; ++i)
scanf("%d", &match[i]); // 暂时表示第i个任务能够匹配的天数
for(i = 1; i <= n; ++i) {
scanf("%d", &w);
sum += w;
for(j = 1; j <= match[i]; ++j)
G[i][j] = w;
}
} void solve() {
printf("%d\n", sum - KM());
} int main()
{
// freopen("stdin.txt", "r", stdin);
int t;
scanf("%d", &t);
while(t--) {
getMap();
solve();
}
}
HDU1789 Doing Homework again 【贪心】的更多相关文章
- HDU-1789 Doing Homework again 贪心问题 有时间限制的最小化惩罚问题
题目链接:https://cn.vjudge.net/problem/HDU-1789 题意 小明有一大堆作业没写,且做一个作业就要花一天时间 给出所有作业的时间限制,和不写作业后要扣的分数 问如何安 ...
- hdu1789 Doing Homework again(贪心+排序)
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu1789 Doing Homework again---(经典贪心)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1789 题目大意: 给出N个作业的截至日期,和N个作业不交所扣掉的分数,要求输出扣除分数做少的方案. ...
- HDU1789 Doing Homework again 做作业【贪心】
题目链接:https://vjudge.net/problem/HDU-1789 题目大意: 给出N个作业的截至日期,和N个作业不交所扣掉的分数,要求输出扣除分数做少的方案. 解析: 与上一道销售商品 ...
- HDU 1789 Doing Homework again(贪心)
Doing Homework again 这只是一道简单的贪心,但想不到的话,真的好难,我就想不到,最后还是看的题解 [题目链接]Doing Homework again [题目类型]贪心 & ...
- hdu--1798--Doing Homework again(贪心)
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 1789 Doing HomeWork Again (贪心算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 /*Doing Homework again Time Limit: 1000/1000 MS ...
- HDU 1789 - Doing Homework again - [贪心+优先队列]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...
- HDOJ.1789 Doing Homework again (贪心)
Doing Homework again 点我挑战题目 题意分析 给出n组数据,每组数据中有每份作业的deadline和score,如果不能按期完成,则要扣相应score,求每组数据最少扣除的scor ...
随机推荐
- jQuery文档处理(增加与删除文档)
1.追加内容
- MFC中GetParent()的作用(转)
原文转自 https://bbs.csdn.net/topics/390540690 1.假如创建一个非模态的窗口,在如下两种做法里任选一种.(1) chatting.m_lpDlg = new CC ...
- javascript验证前端页面
数据表结构 1.html页面 <!DOCTYPE html> <html> <head> <title>注册</title> <met ...
- 自定义validate的效验规则 检验用户名是否存在
<script type="text/javascript"> //自定义效验规则 $.validator.addMethod( //规则名称 "checkU ...
- Android 各种功能代码收集
1.分享图片等文件到单个指定微信好友 /** * 分享信息到朋友 * * @param file * 假如图片的路径为path,那么file = new File(path); */ private ...
- Wannafly挑战赛4 A解方程【二分/set/hash求解方程】
https://www.nowcoder.com/acm/contest/35/A 题目描述 给出n个整数和x,请问这n个整数中是否存在三个数a,b,c使得ax2+bx+c=0,数字可以重复使用. 输 ...
- bzoj 5346: tree (其实是是某次雅礼集训的题)
用prufer序列的公式直接dp,O(n^4)的算法简简单单就写出来了23333. 按理说 O(n^4)是需要优化成O(n^3)才能过的,然鹅我也不知道我怎么过了23333 (那就懒得优化了hhhhh ...
- vue.js 微信浏览器不支持lambda表达式
最近尝试在用vue重构一个微信网页,然后发现在本地测试是可以的,在微信测试工具里也是正常的,然后在手机里有人正常有人不正常,后来发现规律,微信比较新的是不支持的,微信比较旧的是不支持的.然后网上谷歌了 ...
- SPCOMM控件对串口参数的设置
对于串口来说,一般大家都了解波特率,校验码,数据位之类的参数.然而在实际的数据传输中,有些参数也会影响数据的传输.现总结如下,以便大家查询.在对串口进行编程时,可用portman对串口参数进行跟踪,提 ...
- ServicePointManager.ServerCertificateValidationCallback 冲突的解决
ServicePointManager是用于创建. 维护和删除的实例的静态类ServicePoint类. 当应用程序请求对 Internet 资源统一资源标识符 (URI) 的连接通过ServiceP ...