R1_A_The Bucket List

The Bucket List

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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)(1≤N≤100), conveniently numbered 1…N1…N. The ithith cow needs to be milked from time sisi to time titi, and requires bibi 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′si′smilking cannot be used for any other cow’s milking between time sisi and time titi. 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 sisi’s and ti’s are all distinct).

FJ has a storage room containing buckets that are sequentially numbered with labels 11, 22, 33, and so on. In his current milking strategy, whenever some cow (say, cow ii) starts milking (at time sisi), FJ runs to the storage room and collects the bi buckets with the smallest available labels and allocates these for milking cow ii.

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 NN. The next NN lines each describe one cow, containing the numbers sisi, titi, and bibi, separated by spaces. Both sisi and titi are integers in the range 1…10001…1000, and bibi is an integer in the range 1…101…10.

Output

Output a single integer telling how many total buckets FJ needs.

Example

input

Copy

3
4 10 1
8 13 3
2 6 2

output

Copy

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头牛,每头牛都有s,t,b。s是开始喝奶的时间,t是结束喝奶的时间,b是喝奶的时候占用的桶。问这n头牛要喝奶的最小桶数。桶可重复利用

题目分析

模拟题。我们先按照喝奶开始的时间来对牛进行排序。然后对时间开桶(最大1000不大),记录下,牛喝奶后在什么时间归还。另外再开一个桶来记录现有的桶。对时间进行遍历,当遇上开始的时间的时候,就对现有的桶减去所需要的桶。当现在的桶为负数时,将答案加上透支的桶,然后现有的桶归0。再记录一下归还的时间就可以了。就是一道模拟水题。

代码

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 10000;
typedef struct{
  int s, t, b;
}node;

node a[maxn];
int add[maxn];
bool cmp(node x, node y){
  return x.s < y.s;
}
int main(int argc, char const *argv[]) {
  int n;
  scanf("%d", &n);
  int maxt = -1;
  for(int i = 0; i < n; i++){
    scanf("%d%d%d", &a[i].s, &a[i].t, &a[i].b);
    maxt = max(a[i].t, maxt);
  }
  sort(a, a + n, cmp);
  int ans = 0, now = 0;
  for(int i = 1; i <= maxt; i++){
    now += add[i];
    for(int j = 0; j < n && i >= a[j].s; j++){
      if(a[j].s == i){
        now -= a[j].b;
        if(now < 0){
           ans -= now;
           now = 0;
        }
        add[a[j].t] += a[j].b;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}

猜你喜欢

转载自blog.csdn.net/IT_w_TI/article/details/88555841
今日推荐