Doing Homework again:贪心+结构体sort
2024-08-31 06:34:47
Doing Homework again
Problem Description
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce 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.
Input
The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
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.
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.
Output
For each test case, you should output the smallest total reduced score, one line per test case.
Sample Input
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
Sample Output
0
3
5
题目描述:t组数据,输入n,然后n个数代表要交作业的时间,后边n个数表示作业的分值,做完不扣分,在该交时没交上,要扣对应的分,求最优方案下的扣分。
先按照分数排序,肯定先安排好分大的,然后再安排分小的。
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std; struct node { //t代表时间,a代表分值
int t, a;
}s[1010]; int T, n, cnt;
bool used[1010]; //标记某个时间段是否用了
bool cmp(node x, node y) {
if (x.a == y.a)
return x.t<y.t;
else
return x.a>y.a;
} int main() {
cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> s[i].t;
for (int i = 1; i <= n; i++)
cin >> s[i].a; sort(s + 1, s + n + 1, cmp);
memset(used, false, sizeof(used)); //不要忘了初始化
cnt = 0; for (int i = 1; i <= n; i++) { //枚举,先把分大的安排好
int ans = 1;
for (int j = s[i].t; j>0; j--) { //如果分大的当前时间已经被安排了,那就向前找
if (!used[j]) {
used[j] = true;
ans = 0;
break;
}
}
if (ans)cnt += s[i].a; //找不到,就只能加上
} cout << cnt << "\n";
}
return 0;
}
最新文章
- 子div设置浮动无法把父div撑开。
- TFS修改项目名称
- O(n)线性筛选n以内的素数
- 技术英文单词贴--C
- Leetcode 130 Surrounded Regions DFS
- 一步一步实战扩展 ASP.NET Route,实现小写 URL、个性化 URL
- PHP出错界面详细说明
- unity3d shader之God Ray上帝之光
- eclipse 连接手机的 核心解决办法
- mysql之 MySQL 主从基于 GTID 复制原理概述
- 同样:Hashtable较HashMap也是如此。
- ubuntu1604使用之旅——启动ssh系列
- 深入浅出 JVM GC(1)
- 国产首款5G手机抢先亮相:如此给力的说!
- windows10 hyper-v安装配置centos6.8
- windows使用
- Spring RedisTemplate操作-HyperLogLog操作(7)
- python+stomp+activemq
- opencv 摄像头 线程
- List&;Map&;Set的操作和遍历