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;
    }




     
  • 相关阅读:
    01_javaSE面试题:自增变量
    SpringBoot(二十)_404返回统一异常处理结果
    MD5加密工具代码
    SpringBoot(十九)_spring.profiles.active=@profiles.active@ 的使用
    读取本地文件转化成MultipartFile
    remote: http basic: access denied fatal: authentication failed for '‘解决办法
    git报错_you are not allowed to push code to protected branches on this project
    SpringBoot(十八)_springboot打成war包部署
    sql优化问题笔记(mysql)
    gitbook 简单使用
  • 原文地址:https://www.cnblogs.com/pangblog/p/3331259.html
Copyright © 2011-2022 走看看