NC24080. [USACO 2017 Feb B]Why Did the Cow Cross the Road III
描述
Neighboring cows that still have the ability to enter Farmer John's property find the process has become more arduous, as they can enter only through a single gate where each cow is subject to intense questioning, often causing the cows to queue up in a long line.
For each of the N cows visiting the farm, you are told the time she arrives at the gate and the duration of time required for her to answer her entry questions. Only one cow can be undergoing questioning at any given time, so if many cows arrive near the same time, they will likely need to wait in line to be processed one by one. For example, if a cow arrives at time 5 and answers questions for 7 units of time, another cow arriving at time 8 would need to wait until time 12 to start answering questions herself.
Please determine the earliest possible time by which all cows are able to enter the farm.
输入描述
The first line of input contains N, a positive integer at most 100. Each of the next N lines describes one cow, giving the time it arrives and the time it requires for questioning; each of these numbers are positive integers at most 1,000,000.
输出描述
Please determine the minimum possible time at which all the cows could have completed processing.
示例1
输入:
3 2 1 8 3 5 7
输出:
15
说明:
Here, first cow arrives at time 2 and is quickly processed. The gate remains briefly idle until the third cow arrives at time 5, and begins processing. The second cow then arrives at time 8 and waits until time 5+7=12 to start answering questions, finishing at time 12+3 = 15.Go(1.14.4) 解法, 执行用时: 9ms, 内存消耗: 868K, 提交时间: 2020-11-30 00:07:34
package main import "fmt" var a,b [100000 + 10]int var pos [100000 + 10]int func main(){ var n int fmt.Scanf("%d",&n) for i:= 0 ; i < n ;i ++{ fmt.Scanf("%d %d",&a[i],&b[i]) } var num int num = 0; for i:=0;i < n ; i ++{ var s int s = -1 for j:=0;j < n; j ++{ if pos[j] == 0 && (s == -1 || a[j] < a[s]){ s = j } } pos[s] = 1; if a[s] > num{ num = a[s] + b[s] }else{ num = num + b[s] } } fmt.Println(num) }
C++(clang++11) 解法, 执行用时: 3ms, 内存消耗: 388K, 提交时间: 2020-11-28 17:01:22
#include<bits/stdc++.h> using namespace std; int n,ans; struct xy{ int x,y; }d[1000005]; bool cmp(xy a,xy b){ return a.x<b.x; } int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>d[i].x>>d[i].y; } sort(d+1,d+1+n,cmp); for(int i=1;i<=n;i++){ if(ans<d[i].x)ans=d[i].x; ans+=d[i].y; } cout<<ans; }
Python3(3.9) 解法, 执行用时: 19ms, 内存消耗: 2876K, 提交时间: 2020-11-28 14:02:36
import heapq N = int(input()) h = [] heapq.heapify(h) while N > 0: N -= 1 arrive, time = map(int, input().split()) heapq.heappush(h, (arrive, time)) t = 0 while h: arrive, time = heapq.heappop(h) t = max(t, arrive) t += time print(t)