A. The Bucket List

题目链接

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;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值