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 }
  • 相关阅读:
    cms建站
    tab切换 原生js
    iOS下JS与原生OC互相调用(总结)
    HBuilder设置沉浸式状态栏显示效果
    JQuery 绑定select标签的onchange事件,弹出选择的值,并实现跳转、传参
    js调用app启动页
    CSS实现单行、多行文本溢出显示省略号(…)
    Java NIO系列教程(七) FileChannel
    Java NIO系列教程(六) Selector
    Java NIO系列教程(五) 通道之间的数据传输
  • 原文地址:https://www.cnblogs.com/acgoto/p/8526303.html
Copyright © 2011-2022 走看看