zoukankan      html  css  js  c++  java
  • BZOJ 1620 [Usaco2008 Nov]Time Management 时间管理:贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1620

    题意:

      有n个工作,每一个工作完成需要花费的时间为tim[i],完成这项工作的截止日期为dead[i]。

      问你在保证所有工作按时完成的前提下,最晚什么时候开始工作。

      (每天从时刻0开始算。如果无论如何都完成不了,输出-1)

    题解:

      贪心。

      先将所有工作按dead从大到小排序。

      当前开始工作的时间为start(初始为INF)。

      对于每个工作,start一定要满足两个条件:

        start <= dead - tim (最后一刻才完成这项工作的开始时间)

        start + tim <= 原来的start (这项工作做完了之后,才能开始别的工作)

      所以start = min(start - tim, dead - tim)

    AC Code:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <algorithm>
     5 #define MAX_N 1005
     6 #define INF 10000000
     7 
     8 using namespace std;
     9 
    10 struct Work
    11 {
    12     int tim;
    13     int dead;
    14     Work(int _tim,int _dead)
    15     {
    16         tim=_tim;
    17         dead=_dead;
    18     }
    19     Work(){}
    20     friend bool operator < (const Work &a,const Work &b)
    21     {
    22         return a.dead>b.dead;
    23     }
    24     void read_work()
    25     {
    26         cin>>tim>>dead;
    27     }
    28 };
    29 
    30 int n;
    31 int start=INF;
    32 Work work[MAX_N];
    33 
    34 void read()
    35 {
    36     cin>>n;
    37     for(int i=0;i<n;i++)
    38     {
    39         work[i].read_work();
    40     }
    41 }
    42 
    43 void solve()
    44 {
    45     sort(work,work+n);
    46     for(int i=0;i<n;i++)
    47     {
    48         Work now=work[i];
    49         start=min(start-now.tim,now.dead-now.tim);
    50     }
    51 }
    52 
    53 void print()
    54 {
    55     if(start>=0) cout<<start<<endl;
    56     else cout<<-1<<endl;
    57 }
    58 
    59 int main()
    60 {
    61     read();
    62     solve();
    63     print();
    64 }
  • 相关阅读:
    SGU438_The Glorious Karlutka River =)
    SGU326_Perspective
    Problem B. Harvest of Apples(莫队+数学)
    【HDU2019多校】1005
    【HDU2019多校】K
    L
    「2017 山东一轮集训 Day2」Pair (霍尔定理+线段树)
    【2017西安】Sum of xor sum(线段树)
    【2017西安】 XOR (线性基+思维)
    【SPOJ】Lightning Conductor (dp+决策单调性)
  • 原文地址:https://www.cnblogs.com/Leohh/p/7612654.html
Copyright © 2011-2022 走看看