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




     
  • 相关阅读:
    c/c++ 传统数组的缺点
    在Eclipse中搭建C/C++环境
    web服务编码设置
    锋利的jQuery学习总结
    SQL调优常用方法
    C#(KeyChar和KeyCord值,KeyDown/KeyPress事件区别)
    winform 屏蔽 空格键
    $.ajax与$.post、$.get的一点区别
    jquery $.ajax $.get $.post的区别?
    $(function() {}),即$(document).ready(function(),什么时候执行?以此为准
  • 原文地址:https://www.cnblogs.com/pangblog/p/3331259.html
Copyright © 2011-2022 走看看