zoukankan      html  css  js  c++  java
  • HDU 4221 Greedy?(贪心)

    Greedy?

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 908    Accepted Submission(s): 284


    Problem Description
    iSea is going to be CRAZY! Recently, he was assigned a lot of works to do, so many that you can't imagine. Each task costs Ci time as least, and the worst news is, he must do this work no later than time Di!
    OMG, how could it be conceivable! After simple estimation, he discovers a fact that if a work is finished after Di, says Ti, he will get a penalty Ti - Di. Though it may be impossible for him to finish every task before its deadline, he wants the maximum penalty of all the tasks to be as small as possible. He can finish those tasks at any order, and once a task begins, it can't be interrupted. All tasks should begin at integral times, and time begins from 0.
     
    Input
    The first line contains a single integer T, indicating the number of test cases.
    Each test case includes an integer N. Then N lines following, each line contains two integers Ci and Di.

    Technical Specification
    1. 1 <= T <= 100
    2. 1 <= N <= 100 000
    3. 1 <= Ci, Di <= 1 000 000 000
     
    Output
    For each test case, output the case number first, then the smallest maximum penalty.
     
    Sample Input
    2 2 3 4 2 2 4 3 6 2 7 4 5 3 9
     
    Sample Output
    Case 1: 1 Case 2: 3
     
    Author
    iSea@WHU
     
    Source
     
    Recommend
    lcy
     
     
     
    贪心。
    就是按照结束时间从小到大排序。之后按照这个顺序去求结果就可以了。
    至于为什么这样贪心是正确的,我也还没有想明白。
    #include<stdio.h>
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    const int MAXN=100010;
    struct Node
    {
        int c,d;
    }node[MAXN];
    bool cmp(Node a,Node b)
    {
        return a.d<b.d;
    }
    int main()
    {
        int T;
        int iCase=0;
        scanf("%d",&T);
        int n;
        while(T--)
        {
            iCase++;
            scanf("%d",&n);
            for(int i=0;i<n;i++)
              scanf("%d%d",&node[i].c,&node[i].d);
            sort(node,node+n,cmp);
            long long sum=0;
            long long ans=0;
            for(int i=0;i<n;i++)
            {
                sum+=node[i].c;
                if(sum-node[i].d>ans)ans=sum-node[i].d;
            }
            printf("Case %d: %I64d\n",iCase,ans);
        }
        return 0;
    }
  • 相关阅读:
    PHP ffmpeg详解简单上手 window64 音频amr转mp3
    PHP强制转换类型
    PHP使用array_unique对二维数组去重处理
    发布包
    CSS用户界面样式
    数组
    结构类型(枚举,结构,联合)
    循环
    程序结构
    文件
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2672698.html
Copyright © 2011-2022 走看看