zoukankan      html  css  js  c++  java
  • 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): 10705    Accepted Submission(s): 6299


    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
     
    之前做doing homework是用的dp,这道题一来也想用dp,没想到思路,想了好久,然后搜了题解,发现是用的贪心,然后又不知道怎么贪。。。
    最后用了题解的思路,按罚分由大到小排序,deadline之前只要有空着的天,那么就在最晚的那一天完成这项任务,否则,无空闲天,则会被罚分。
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    
    struct Node
    {
        int dead;
        int sco;
    } node[1005];
    int dead[1005];
    int sco[1005];
    
    bool cmp(Node a,Node b)
    {
        if(a.sco>b.sco)
            return 1;
        return 0;
    }
    
    int vis[1005];
    
    int main()
    {
        int t,n;
        scanf("%d",&t);
        while(t--)
        {
            memset(vis,0,sizeof(vis));
            scanf("%d",&n);
            for(int i=0; i<n; i++)
                scanf("%d",&dead[i]);
            for(int i=0; i<n; i++)
                scanf("%d",&sco[i]);
            for(int i=0; i<n; i++)
            {
                node[i].dead=dead[i];
                node[i].sco=sco[i];
            }
            sort(node,node+n,cmp);
            int day=1,spent=0;
            for(int i=0; i<n; i++)
            {
                int j;
                for( j=node[i].dead; j>=1; j--)
                {
                    if(vis[j]==0)
                    {
                        vis[j]=1;
                        break;
                    }
                }
                if(j==0)
                    spent+=node[i].sco;
            }
            printf("%d
    ",spent);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    万能五笔的广告怎么去掉
    JS版日期格式化和解析工具类,毫秒级
    Toast信息提示:下拉收起(基于jQuery)(app信息提示更新)
    MUI右滑关闭窗口用Webview的drag实现
    mui 列表项左右滑删除功能升级(仿微信左滑 点击删除后出现确认删除)
    修正 Mui 下拉上拉刷新功能
    javascript中new Date()的浏览器兼容性问题
    Mui 下拉刷新,刷新完成功能实现
    mysql 时间类型精确到毫秒、微秒及其处理
    Windows 上如何安装Sqlite
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/5489472.html
Copyright © 2011-2022 走看看