A. 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头牛的开始结束时间及占有的桶。时间从1-1000分钟。每个开始结束时间不重复。求用最少的桶满足需求。
题目分析:
赛中我模拟了一下过程比较简单。赛后看别人代码发现有更快的方法。
用一个数组a存储每个时间点所需桶数,这样1-1000范围中,每个时间点所对应得桶数已知;
再用ans=max(ans,a[t]);求出所需最小桶数;
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct T{
int l;
int r;
int p;
};
int cmp(const T &x,const T &y)
{
if(x.l==y.l)return x.r<y.r;
return x.l<y.l;
}
T a[1005];
int ans=0;int used=0;int time[1008];
int main()
{
int n;cin>>n;
memset(time,0,sizeof(time));
for(int i=0;i<n;i++)
{
cin>>a[i].l>>a[i].r>>a[i].p;
time[a[i].r]=a[i].p;
}
sort(a,a+n,cmp);
int t=1;ans=0; used=0;
int sum=0;
while(sum!=n)
{
while(t!=a[sum].l){
used+=time[t];
t++;
}
if(used>=a[sum].p)
used-=a[sum].p;
else
{
ans+=a[sum].p-used;
used=0;
}
t++;sum++;
}
cout<<ans<<endl;
return 0;
}
参考别人的代码:
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
int main(){
int N,i,j,s,t,b;
int bucket[1005]={0};
scanf("%d",&N);
for(i=0;i<N;i++){
scanf("%d%d%d",&s,&t,&b);
for(j=s;j<=t;j++) bucket[j]+=b;
}
int ans=0;
for(i=0;i<=1000;i++){
ans=max(ans,bucket[i]);
}
printf("%d\n",ans);
return 0;
}