Bzoj_1620_[usaco2008_nov]_time_management_ Time Management _ (two points + greedy)

Source: Internet
Author: User

Describe

http://www.lydsy.com/JudgeOnline/problem.php?id=1620

n work, each work it takes time, and complete deadline, ask to complete all work, at the latest when to start.

Analysis

We can think of the time of the two-minute start. For a given time, determine whether it can be done.

How to judge it?

We assume that there are \ (a,b\) two tasks and \ (Deadline (a) <deadline (b) \).

If \ (b\) is completed first, then \ (Time[b]+time[a]<deadline (a) \) is required.

If you finish \ (a\) First, then ask \ (Time[a]<deadline (a), time[a]+time[b]<deadline (b) \).

Obviously the second scenario is more likely to be completed, so our greedy choice is to complete the small task first (deadline\) at a time.

1#include <bits/stdc++.h>2 #defineFST First3 #defineSCD second4 using namespacestd;5 6typedef pair <int,int>P;7 Const intmaxn= ++5;8 intn,max=~0u>>1;9 P A[MAXN];TenInlineintReadint&AMP;X) {x=0;intk=1;CharC for(C=getchar ();c<'0'|| C>'9'; C=getchar ())if(c=='-') k=-1; for(; c>='0'&&c<='9'; C=getchar ()) x=x*Ten+c-'0';returnx*=K;} OneInlineBOOLCMP (P a,p b) {returna.scd<B.SCD;} AInlineBOOLCintx) { -      for(intI=1, t=x-1; i<=n;i++){ -t+=a[i].fst; the         if(T&GT;=A[I].SCD)return false; -     } -     return true; - } +InlineintBsearch (intLintR) { -      while(l<R) { +         intmid=l+ (r-l+1)/2; A         if(C (mid)) l=mid; at         Elser=mid-1; -     } -     returnl; - } -Inlinevoidinit () { - read (n); in      for(intI=1; i<=n;i++) Read (A[I].FST), read (A[I].SCD), Max=min (max,a[i].scd-a[i].fst); -Sort (A +1, a+n+1, CMP); to } + intMain () { - init (); the     intAns=bsearch (0, Max); *     if(ans==0) ans=-1; $printf"%d\n", ans);Panax Notoginseng     return 0; -}
View Code

Bzoj_1620_[usaco2008_nov]_time_management_ Time Management _ (two points + greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.