zoukankan      html  css  js  c++  java
  • 贪心-hdu-1789-Doing Homework again

    题目链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=1789

    题目意思:
    有n个作业,每个作业有一个截止日期,每个作业如果超过截止日期完成的时候有一个惩罚值,问怎样安排作业,使惩罚值最小。

    解题思路:

    贪心。

    先按惩罚值从大到小排序,惩罚值越大,就应该尽量安排改作业在截止日期之前完成,而怎样保证既在截止日期之前完成,又保证其他的作业的惩罚值总和较小呢,应该安排在离该截止日期最近的可安排那个日期,这样就保证了惩罚值最小。

    代码:

    #include<iostream>
    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<set>
    #include<stack>
    #include<list>
    #include<queue>
    #define eps 1e-6
    #define INF 0x1f1f1f1f
    #define PI acos(-1.0)
    #define ll __int64
    #define lson l,m,(rt<<1)
    #define rson m+1,r,(rt<<1)|1
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    using namespace std;
    
    /*
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    */
    struct Inf
    {
       int pun,dl;
    }inf[1100];
    bool vis[1100];
    
    bool cmp(struct Inf a,struct Inf b)
    {
      if(a.pun!=b.pun) //按惩罚值从大到小排序
         return a.pun>b.pun;
      return a.dl<b.dl;  //惩罚值相同的话,截止日期靠前的先安排
    }
    
    int main()
    {
       int n,t;
    
       scanf("%d",&t);
       while(t--)
       {
          scanf("%d",&n);
          for(int i=1;i<=n;i++)
             scanf("%d",&inf[i].dl);
          for(int i=1;i<=n;i++)
             scanf("%d",&inf[i].pun);
    
          sort(inf+1,inf+n+1,cmp);
          memset(vis,false,sizeof(vis));
          int ans=0;
          for(int i=1;i<=n;i++)
          {
             int t=inf[i].dl,j;
             for(j=t;j>=1;j--) //找到最靠近的那天,
             {
                if(!vis[j])
                {
                   vis[j]=true;
                   break;
                }
             }
             if(j<1) //如果前面都放了,只能接受惩罚,此时的惩罚值是比较小的
                ans+=inf[i].pun;
          }
          printf("%d
    ",ans);
    
       }
       return 0;
    }
    




  • 相关阅读:
    宠物店4.0的安装
    《professional asp.net 2.0》读书笔记连载2
    《xhtml 入门系列》之一
    ALinq 让Mysql变得如此简单
    ALinq 入门学习(八)ALinq 对Vs2010 的支持
    教你一款极为简单实用的图表插件
    虚拟机下无法启动 Linux 系统
    怎样去突破文件依赖缓存
    jQuery 表单验证扩展(五)
    Log4Net 全方位跟踪程序运行
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3221906.html
Copyright © 2011-2022 走看看