HDU 1789-Doing Homework again

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15369    Accepted Submission(s): 8973


题目链接:点击打开链接


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


题意:
有个人现在有很多作业要做。每个老师都给他交作业的最后期限。如果他在最后期限之后做作业,老师将会降低

他的期末考试的分数。现在我们假设每个家庭作业都需要一天的时间来完成。他现在希望你能帮他安排做作业的

顺序来减少分数的丢失。

输入包含几个测试用例。输入的第一行是一个整数T,这是测试用例的数量。T测试用例。
每个测试用例以一个正整数N(1 <=N<= 1000)开始,这表示作业的数量。2行。第一行包含N个整数,表示该作业的最后期限,下一行包含N个整数,表示如果没在规定的期限里做完作业,需要扣掉的分数值。


分析:
贪心吧!用一个结构体来存每一个作业的期限和扣除的分值,然后用结构体排序,先按扣分情况来安排作业,如果扣分的情况相同,就按期限来排,期限少的先排。(第一次我想都没想,感觉是按时间先排,再考虑分数,结果错了,因为本题让你找到一种选择,使分数少扣一些,所以应该先按分数来排)
在遍历的时候,我们应该从该作业的最后期限往前遍历,尽量为别的作业多留时间,这一点宝宝们应该好好理解一下。


#include <iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;

struct book{
    int day,fa;
}s[1005];

bool cmp(book a,book b)
{
    if(a.fa==b.fa)
        return a.day<b.day;
    return a.fa>b.fa;
}

int main()
{
    int t,n,ans,j;
    int vis[1005];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&s[i].day);
        for(int i=0;i<n;i++)
            scanf("%d",&s[i].fa);
        sort(s,s+n,cmp);
        ans=0;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            for(j=s[i].day;j>=1;j--)
            {
                if(!vis[j])
                {
                    vis[j]=1;
                    break;
                }
            }
            if(j==0)///如果遍历了该作业期限里的所有时间都没有找到空闲的一天,就说明该作业不能按时完成
                ans+=s[i].fa;
        }
        printf("%d\n",ans);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值