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): 4762    Accepted Submission(s): 2798


    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
     
    Author
    lcy
     
    Source


    思路:
    先按照期限(de)从小到大、罚分(re)从大到小排序,然后依次扫描过去,发现de<t(当前天数),则该作业肯定不能完成,但是不能就直接加上它的re,因为可以将前面的某个作业不做(则空出来一天)来做这个作业,所以再扫描前面做过的作业,找到最小的re,如果re<当前re的,则将那个作业不做来做当前这个作业。这样贪心就能保证得到最优解了。

    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <vector>
    #include <set>
    #include <queue>
    #define maxn 1005
    #define mod 1000000007
    #define INF 0x3f3f3f3f
    using namespace std;
    
    int n,m,ans;
    int vis[maxn];
    struct Node
    {
        int de,re;
    }p[maxn];
    
    bool cmp(Node xx,Node yy)
    {
        if(xx.de!=yy.de) return xx.de<yy.de;
        return xx.re>yy.re;
    }
    void solve()
    {
        int i,j,t=1,flag,mi,k;
        ans=0;
        memset(vis,0,sizeof(vis));
        for(i=1;i<=n;i++)
        {
            if(p[i].de<t)
            {
                mi=p[i].re;
                flag=0;
                for(j=1;j<i;j++)
                {
                    if(!vis[j]) continue ;
                    if(mi>p[j].re)
                    {
                        flag=1;
                        mi=p[j].re;
                        k=j;
                    }
                }
                if(flag)
                {
                    vis[k]=0;
                    vis[i]=1;
                    ans+=p[k].re;
                }
                else ans+=p[i].re;
            }
            else t++,vis[i]=1;
        }
    }
    int main()
    {
        int i,j,t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(i=1;i<=n;i++)
            {
                scanf("%d",&p[i].de);
            }
            for(i=1;i<=n;i++)
            {
                scanf("%d",&p[i].re);
            }
            sort(p+1,p+n+1,cmp);
            solve();
            printf("%d
    ",ans);
        }
        return 0;
    }




     
  • 相关阅读:
    线程安全(上)--彻底搞懂volatile关键字
    数据库设计三大范式
    1、框架及环境搭建
    约瑟夫问题
    链表(上):如何实现LRU缓存淘汰算法?
    为什么很多编程语言中数组都是从 0 开始编号?
    mac 终端命令小结
    复杂度分析(下):浅析最好、最坏、平均、均摊时间复杂度
    复杂度分析(上):如何分析、统计算法的执行效率和资源消耗?
    程序员少走弯路的10条忠告和成就一生的10个经典故事
  • 原文地址:https://www.cnblogs.com/pangblog/p/3331259.html
Copyright © 2011-2022 走看看