Doing Homework again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13805 Accepted Submission(s): 8008
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.
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
Author
lcy
解题思路:
这道题是一道贪心题,我采用的是按照日期从小到大排序,当日期相等时,按照分数从大到小排序,刚开始的想法比较简单,结果答案错误,后来看了别人的一眼,发现当这个数不满足条件时需要判断一下前面有没有比这个数分数更小的,如果过,就类似替换一下。
当然按照分数从大到小排序,分数相等时日期从小到大,还要注意的是让他们的时间尽量往后拖
解题代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct node
{
int deadline;
int score;
}ch[1005];
bool cmp(node a,node b)
{
if(a.deadline==b.deadline)
return a.score>b.score;
return a.deadline<b.deadline;
}
int main()
{
int vis[1005];
int T;
scanf("%d",&T);
while(T--)
{
memset(vis,0,sizeof(vis));
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&ch[i].deadline);
for(int i=1;i<=n;i++)
scanf("%d",&ch[i].score);
sort(ch+1,ch+1+n,cmp);//日期从小到大排序,当日期相等时,按照分数从大到小排序
int day=0;
int sum=0;
for(int i=1;i<=n;i++)
{
if(day<ch[i].deadline)
day++;
else//当这天日期超过时,要查找前面是否有分数较小,进行替换
{
int mindis=ch[i].score;
//cout<<mindis<<endl;
int k=i;
for(int j=1;j<i;j++)
{
if(ch[j].score<mindis&&vis[j]==0)
{
k=j;
mindis=ch[j].score;
}
}
vis[k]=1;//要标记一下,这个数已经用过了
sum+=mindis;
//cout<<mindis<<endl;
}
}
cout<<sum<<endl;
}
return 0;
}
探讨了一道经典的贪心算法题目,通过合理安排作业完成顺序来最小化因逾期而产生的扣分。介绍了输入输出样例及解题思路,包括日期和分数的排序策略。
290

被折叠的 条评论
为什么被折叠?



