1620: [Usaco2008 Nov]Time Management 时间管理

1620: [Usaco2008 Nov]Time Management 时间管理

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 506  Solved: 306
[Submit][Status]

Description

Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. He has N jobs conveniently numbered 1..N (1 <= N <= 1,000) to accomplish (like milking the cows, cleaning the barn, mending the fences, and so on). To manage his time effectively, he has created a list of the jobs that must be finished. Job i requires a certain amount of time T_i (1 <= T_i <= 1,000) to complete and furthermore must be finished by time S_i (1 <= S_i <= 1,000,000). Farmer John starts his day at time t=0 and can only work on one job at a time until it is finished. Even a maturing businessman likes to sleep late; help Farmer John determine the latest he can start working and still finish all the jobs on time.

N个工作,每个工作其所需时间,及完成的Deadline,问要完成所有工作,最迟要什么时候开始.

Input

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains two space-separated integers: T_i and S_i

Output

* Line 1: The latest time Farmer John can start working or -1 if Farmer John cannot finish all the jobs on time.

Sample Input

4
3 5
8 14
5 20
1 16

INPUT DETAILS:

Farmer John has 4 jobs to do, which take 3, 8, 5, and 1 units of
time, respectively, and must be completed by time 5, 14, 20, and
16, respectively.

Sample Output

2

OUTPUT DETAILS:

Farmer John must start the first job at time 2. Then he can do
the second, fourth, and third jobs in that order to finish on time.

HINT

Source

Silver

题解:坑爹啊,这次居然CE,吓我一跳——打开一看,代码没复制全TuT。。。突然觉得其实bzoj上面贪心的题才是最令人不敢下手的,看了半天才尝试性的写了个贪心程序,然后碰运气,有时候AC,有时候直接跪。。。书归正传,这个题其实就是先按照deadline时间排个序,然后不断的往前减当前任务消耗的时间,假如出现了减去后小于0的情况,就出-1,还有注意每次减去后到了下一次,然后要和这个新的任务deadline比较小,假如deadline更小的话,则取deadline再减,否则凉拌。。。

 1 type
 2     arr=array[0..2000] of longint;
 3 var
 4    i,j,k,l,m,n:longint;
 5    a,b:arr;
 6 procedure swap(var x,y:longint);
 7           var z:longint;
 8           begin
 9                z:=x;x:=y;y:=z;
10           end;
11 function min(x,y:longint):longint;
12          begin
13               if x<y then min:=x else min:=y;
14          end;
15 procedure sort(l,r:longint);
16           var
17              i,j,x,y:longint;
18           begin
19                i:=l;j:=r;
20                x:=a[(l+r) div 2];
21                repeat
22                      while a[i]<x do inc(i);
23                      while a[j]>x do dec(j);
24                      if I<=j then
25                         begin
26                              swap(a[i],a[j]);
27                              swap(b[i],b[j]);
28                              inc(i);dec(j);
29                         end;
30                until I>j;
31                if l<j then sort(l,j);
32                if i<r then sort(i,r);
33           end;
34 begin
35      readln(n);
36      for i:=1 to n do
37          readln(b[i],a[i]);
38      sort(1,n);
39      l:=a[n];
40      for i:=n downto 1 do
41          begin
42               l:=min(l,a[i]);
43               l:=l-b[i];
44               if l<0 then
45                  begin
46                       writeln(-1);
47                       halt;
48                  end;
49          end;
50      writeln(l);
51 end.
52                        
时间: 2024-10-19 01:33:36

1620: [Usaco2008 Nov]Time Management 时间管理的相关文章

[BZOJ] 1620: [Usaco2008 Nov]Time Management 时间管理

1620: [Usaco2008 Nov]Time Management 时间管理 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 850  Solved: 539[Submit][Status][Discuss] Description Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. He has N jobs

BZOJ 1620: [Usaco2008 Nov]Time Management 时间管理( 二分答案 )

二分一下答案就好了... ---------------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<cctype> #include<iostream> #define rep( i , n ) for( int i

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

BZOJ——1620: [Usaco2008 Nov]Time Management 时间管理

Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 920  Solved: 569[Submit][Status][Discuss] Description Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. He has N jobs conveniently numbered 1..N (1 <= N <= 1,0

bzoj1620[Usaco2008 Nov]Time Management 时间管理*

bzoj1620[Usaco2008 Nov]Time Management 时间管理 题意: n个任务,每个有一个所需时间和最晚完成时刻,问最晚要从什么时候开始工作.n≤1000 题解: 贪心,按最晚完成时刻从早到晚排序,如果当前任务来不及完成,就将前面的任务往前推,否则累积一个“自由时间”.当推任务时,如果之前有“自由时间”,就用自由时间减往前推的时间,否则用最晚开始时间去减往前推的时间.反思:我开始贪错了,按最晚开始时刻从早到晚排序,结果WA很久.现在还是想不太清楚原因,希望哪位神犇能帮我

bzoj1620:时间管理

1620: [Usaco2008 Nov]Time Management 时间管理 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 571  Solved: 343[Submit][Status][Discuss] Description Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. He has N jobs

bzoj1620 / P2920 [USACO08NOV]时间管理Time Management

P2920 [USACO08NOV]时间管理Time Management 显然的贪心. 按deadline从大到小排序,然后依次填充时间. 最后时间为负的话那么就是无解 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define re register 6 using namespace std; 7 int max(int a,in

session management会话管理的原理

web请求与响应基于http,而http是无状态协议.所以我们为了跨越多个请求保留用户的状态,需要利用某种工具帮助我们记录与识别每一次请求及请求的其他信息.举个栗子,我们在淘宝购物的时候,首先添加了一本<C++ primer>进入购物车,然后我们又继续去搜索<thinking in java>,继续添加购物车,这时购物车应该有两本书.但如果我们不采取session management会话管理的话,基于http无状态协议,我们在第二次向购物车发出添加请求时,他是无法知道我们第一次添

【安全牛学习笔记】MANAGEMENT FRAME 管理帧

MANAGEMENT FRAME 管理帧 MANAGEMENT FRAME                      管理帧 MANAGEMENT FRAME                用于协商和控制STA与AP之间的关系 BEACON FRAMES                             AP发送的广播帧,通告无线网络的存在(BSSID) 发包频率                                      102.4ms (可变)