zoukankan      html  css  js  c++  java
  • 题解报告:hdu 1789 Doing Homework again(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789

    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

    解题思路:典型的贪心策略。因为要使得扣分最少,所以要先排个序,规则是:如果期限相同,对扣分多的从大到小排列,如果扣分相同,则将期限从小到大排列。最优策略:每门功课最好在给定的deadline当天就完成,如不能完成,只能往前找哪一天还没使用,尽量使得做这门功课的日期越大越好,即从其截止日期到第一天,如果一路遍历都已经被标记过了,到最后j==0说明已经没有足够的一天时间给他做这门功课,那么就将这门功课扣的分数加到ans中,最终的ans即为最小扣分值。

    AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 struct NODE
     4 {
     5     int deadline,reduce;
     6 }node[1005];
     7 bool cmp(NODE a,NODE b)
     8 {
     9     if(a.reduce!=b.reduce)return a.reduce>b.reduce;//reduce越多的越靠前,先解决扣分多的
    10     return a.deadline<b.deadline;//reduce相同时,deadline越早越靠前,从小到大
    11 }
    12 bool vis[2010];//如果当天没用过,值为false;否则为true
    13 int main()
    14 {
    15     int T,N,ans,j;//ans是保存减少的reduce
    16     cin>>T;
    17     while(T--){
    18         memset(vis,false,sizeof(vis));
    19         cin>>N;
    20         for(int i=0;i<N;i++)cin>>node[i].deadline;
    21         for(int i=0;i<N;i++)cin>>node[i].reduce;
    22         sort(node,node+N,cmp);//按规则排序
    23         ans=0;
    24         for(int i=0;i<N;i++){
    25             for(j=node[i].deadline;j>0;j--)//从截止时间开始往前推,如果有一天没用过,这一天就做这一门课,那么这门课不扣分
    26                 if(!vis[j]){vis[j]=true;break;}
    27             if(j==0)ans+=node[i].reduce;//如果j=0,表明从deadline往前的每一天都被占用了,这门课完不成
    28         }
    29         cout<<ans<<endl;
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    注解实现SpringCache自定义失效时间(升级版)
    表白小游戏之——制作一个小游戏给喜欢的人(Cocos Creator入门小案例)
    3.python编程与计算机的关系,如何执行python文件
    如何临时发布部署Cocos小游戏到Linux服务器,让别人能在微信打开
    当互联网公司换上东京奥运会图标
    灵魂画手的零基础python教程1:关于Python学习的误区、python的优缺点、前景
    聊一聊关于聊天记录的存储
    【爬虫系列】1. 无事,Python验证码识别入门
    【爬虫系列】0. 无内鬼,破解前端JS参数签名
    JPA
  • 原文地址:https://www.cnblogs.com/acgoto/p/8526303.html
Copyright © 2011-2022 走看看