zoukankan      html  css  js  c++  java
  • hdu1789(贪心)

    Doing Homework again

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


    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
     思路:按照贪心的思想,每次做出最优决策,所以可以将作业按扣分高低排序,每次找到扣分最高的,将它安排进终止日期那一天,如果那天有事,就在往前推,,如果最后安排不下,就扣分。这样循环n次就可以了。
     1 #include <cstdio>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 int T, N, hash[1010];
     6 struct HM
     7 {
     8     int day, score;
     9 } hm[1010];
    10 bool cmp (HM x, HM y )
    11 {
    12     if ( x.score != y.score )
    13         return x.score > y.score;
    14     return x.day > y.day;
    15 }
    16 int main ()
    17 {
    18     scanf ( "%d", &T );
    19     int i,j;
    20     while ( T -- )
    21     {
    22         scanf ( "%d", &N );
    23         int  sum = 0;
    24         for (i=1; i <= N; ++ i )
    25             scanf ( "%d", &hm[i].day );
    26         for (i=1; i <= N; ++ i )
    27             scanf ( "%d", &hm[i].score );
    28         sort ( hm+1, hm+N+1, cmp );
    29         memset ( hash, 0, sizeof ( hash ) );
    30         for (i=1; i<= N; ++i )
    31         {
    32             for ( j = hm[i].day; j > 0; j-- )
    33             {
    34                 if ( !hash[j] )
    35                 {
    36                     hash[j]=1;
    37                     break;
    38                 }
    39             }
    40             if ( !j )
    41                 sum += hm[i].score;
    42         }
    43         printf ( "%d
    ", sum );
    44     }
    45     return 0;
    46 }
    View Code
  • 相关阅读:
    Android相对布局RelativeLayout常用到的属性
    用LinkedList模拟队列(Java容器)
    JAVA数组(一)
    SQL分页查询(转)
    asp.net 子窗体和父窗体交互
    Silverlight加载外部XAP包和页面
    As.net 动态反射程序集里面DLL并创建对象
    Silverlight LIstBox 实现横向排列元素 并且自动换行
    java jdbc 连接SQL数据库
    Silverlight Command的运用
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3914024.html
Copyright © 2011-2022 走看看