1/21集训二 模拟+贪心
G.(贪心 安排做作业的时间)Doing Homework again(感觉有点像HDU安排电视节目的那题(今年暑假不AC))
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.
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
题意:
一共n个作业 ,完成每个作业都需要一天。但是每个作业有截止日期 ,如果超过了截止日期 就会扣分 。给出 n个作业的截止日期, 和如果超过截止日期要扣的分,现在要计算,怎么设计做作业的顺序扣分最少,计算出这个最少的扣分值
思路:
因为学分的重要度比截止时间重要一些,所以应该按照学分的高低排序,若学分一致,那么deadline靠前的排在前面。
然后从第一个作业开始遍历,将他从它的deadline开始向前查找没有被安排作业的日期,即应该安排在离该截止日期最近的可安排那个日期。这样就保证了在截止日期之前完成,且这门作业的时间又排在比较后面,不会影响前面的更紧急的课程,保证其他的作业的惩罚值总和较小。(要用一个数组来记录某一天是否被安排作业)
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <algorithm>
#include <iostream>
#define MAXN 1005
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
struct School
{
int d, s;
};
bool cmp(School a, School b)
{
if(a.s != b.s)
return a.s > b.s;
else
return a.d < b.d;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
struct School sch[MAXN];
bool vis[MAXN] = {false};
for(int i = 0; i < n; i++)
cin>>sch[i].d;
for(int i = 0; i < n; i++)
cin>>sch[i].s;
sort(sch, sch + n, cmp);
int score = 0;
for(int i = 0; i < n; i++)
{
int j;
for(j = sch[i].d; j >= 1; j--)
{
if(!vis[j])
{
vis[j] = true;
break;
}
}
if(j == 0)
score += sch[i].s;
}
cout<<score<<endl;
}
return 0;
}