zl程序教程

您现在的位置是:首页 >  其它

当前栏目

Why Did the Cow Cross the Road III (B)

The why III cross Cow did
2023-09-27 14:28:26 时间

Why Did the Cow Cross the Road III (B)

题目链接:nowcoder 24080

题目

在这里插入图片描述

题目大意

有一些奶牛要入场,它们每个人都要到达时间和入场要的时间。
同一个时间内只能有一个奶牛在入场。
然后问你全部入场完要多久。

样例输入

3
2 1
8 3
5 7

样例输出

15

样例解释

在这里,第一头母牛在时间2到达并得到快速处理。登机口暂时保持闲置状态,直到第三头母牛在时间5到达并开始处理。然后,第二头母牛在时间8到达,等到时间5 + 7 = 12开始回答问题,在时间12 + 3 = 15结束。

思路

这道题就是一道模拟。

就把奶牛按到达时间从小到大排序,一直记录到现在为止最后一个奶牛搞完要多少时间,然后每次多一个奶牛就分两种情况:
如果还有奶牛在处理,就等所有的奶牛都搞好了再搞。如果前面没有,就直接开搞。

代码

#include<cstdio>
#include<algorithm>

using namespace std;

struct node {
	int arrive, time;
}cow[101];
int n, ans;

bool cmp(node x, node y) {
	return x.arrive < y.arrive;
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d %d", &cow[i].arrive, &cow[i].time);
	
	sort(cow + 1, cow + n + 1, cmp);
	
	for (int i = 1; i <= n; i++) {
		if (cow[i].arrive > ans) ans = cow[i].arrive + cow[i].time;//前面有奶牛在检测,时间要推迟
			else ans += cow[i].time;//可以直接开始检测
	}
	
	printf("%d", ans);
	
	return 0;
}