HDU1789Doing Homework again(贪婪)

时间:2023-03-08 22:20:28

HDU1789Doing Homework again(贪心)

题目链接

题目大意:给你n们作业的最后期限和过了这个期限没做须要扣的分数。问如何安排能够使得扣分最少。

解题思路:贪心,将扣分多的作业排在前面,扣分同样的依照最后期限前的排前面,然后用一个数组来表示第i天是否有安排。每次都将第i个作业放到它的最后期限的那天完毕,但假设这一天被占了,那么就仅仅能往前移动,找空暇的天。假设一直找到了0。那么说明这个作业是无法按时完毕了,就加上分数。假设某项作业完毕的最后期限比n还大,那么这个作业一定是能够及时完毕的,那么就能够无论它了。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn = 1e3 + 5;
int vis[maxn]; int n;
struct homework { int deadt, score;
}h[maxn]; int cmp (const homework &a, const homework &b) { if (a.score != b.score)
return a.score > b.score;
return a.deadt < b.deadt;
} int solve () { sort(h, h + n, cmp);
memset (vis, -1, sizeof (vis)); int ans = 0, time;
for (int i = 0; i < n; i++) { time = h[i].deadt - 1;
if (time >= n)
continue;
while (time >= 0 && vis[time] != -1) {
time--;
} if (time >= 0)
vis[time] = 1;
else
ans += h[i].score;
}
return ans;
} int main () { int T;
scanf ("%d", &T);
while (T--) { scanf ("%d", &n);
for (int i = 0; i < n; i++)
scanf ("%d", &h[i].deadt);
for (int i = 0; i < n; i++)
scanf ("%d", &h[i].score); printf ("%d\n", solve());
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。