题面:
A. The Bucket List
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
Farmer John is considering a change in how he allocates buckets for milking his cows. He thinks this will ultimately allow him to use a small number of total buckets, but he is not sure how many exactly. Please help him out! Farmer John has N cows (1 ≤ N ≤ 100), conveniently numbered 1...N. The ith cow needs to be milked from time si to time ti, and requires bi buckets to be used during the milking process. Several cows might end up being milked at the same time; if so, they cannot use the same buckets. That is, a bucket assigned to cow i′s milking cannot be used for any other cow’s milking between time si and time ti. The bucket can be used for other cows outside this window of time, of course. To simplify his job, FJ has made sure that at any given moment in time, there is at most one cow whose milking is starting or ending (that is, the si’s and ti’s are all distinct).
FJ has a storage room containing buckets that are sequentially numbered with labels 1, 2, 3, and so on. In his current milking strategy, whenever some cow (say, cow i) starts milking (at time si), FJ runs to the storage room and collects the bi buckets with the smallest available labels and allocates these for milking cow i.
Please determine how many total buckets FJ would need to keep in his storage room in order to milk all the cows successfully.
Input
The first line of input contains N. The next N lines each describe one cow, containing the numbers si, ti, and bi, separated by spaces. Both si and ti are integers in the range 1...1000, and bi is an integer in the range 1...10.
Output
Output a single integer telling how many total buckets FJ needs.
Example
Input
3
4 10 1
8 13 3
2 6 2
Output
4
Note
In this example, FJ needs 4 buckets: He uses buckets 1 and 2 for milking cow 3 (starting at time 2). He uses bucket 3 for milking cow 1 (starting at time 4). When cow 2 arrives at time 8, buckets 1 and 2 are now available, but not bucket 3, so he uses buckets 1, 2, and 4.
题目描述:
有N头奶牛,每头奶牛都有对应的时间段。在这个时间段里,奶牛需要b[i]个桶“接奶”。这些桶接完奶后,可以继续用。问:最少需要多少个桶才能接完这些奶牛产出的奶?
题目分析:
我们可以先分析一下样例:
![](https://i-blog.csdnimg.cn/blog_migrate/64b1637f2cc0a4a3b98c801ff2cd353c.png)
首先,在2到6这个时间段,对于第一头奶牛,要用1个桶:
![](https://i-blog.csdnimg.cn/blog_migrate/0062cdeedd7b3582d2bffa29e4b4a946.png)
显然,我们至少要1个桶。
其次,在4到10这个时间段,对于第二头奶牛,要用3个桶:
![](https://i-blog.csdnimg.cn/blog_migrate/bfbd0fa870a1fca2e9ca375911251af3.png)
我们发现,在第二头奶牛开始挤奶的时候,由于第一头奶牛还在接奶,所以我们不能用第一头奶牛用的桶,要另外找3个桶来接第二头奶牛的奶。所以这时至少要4个桶。
最后,在8到13这个时间段,对于第三头奶牛,要用2个桶:
![](https://i-blog.csdnimg.cn/blog_migrate/855278baa35e0c5e210d3b3b2be77d9b.png)
我们发现,在第三头奶牛开始挤奶的时候,只有第一头奶牛的桶才可以用于第三头奶牛挤奶,第二头奶牛的3个桶还不能用。所以这时至少需要5个桶。
看完上面的图,其实只要统计在哪个时间段,需要用的桶最多,就可以知道最少需要多少个桶了。
因为对于这道题,时间在1000以内,最多只有100头奶牛。所以,我们可以直接用一个数组来存这些桶,然后在某个时间段加上这个时间段需要的桶就可以完成这题了。
AC代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 int n, s[105], b[105], t[105]; 6 int bk[1005]; //桶 7 8 int main(){ 9 memset(bk, 0, sizeof(bk)); 10 cin >> n; 11 for(int i = 0; i < n; i++){ 12 cin >> s[i] >> t[i] >> b[i]; 13 } 14 15 for(int i = 0; i < n; i++){ 16 for(int k = s[i]; k <= t[i]; k++){ 17 bk[k] += b[i]; 18 } 19 } 20 21 int maxn = 0; 22 for(int i = 0; i <= 1000; i++){ 23 if(bk[i] > maxn){ 24 maxn = bk[i]; 25 } 26 } 27 28 cout << maxn << endl; 29 return 0; 30 }