time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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), 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.
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 a single integer telling how many total buckets FJ needs.
3
4 10 1
8 13 3
2 6 2
4
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.
给你奶牛的挤奶的开始和结束的时间,然后判断最多要用同时用几个桶。
题目数据范围很小,我们可以直接用桶储存看在一分钟要挤几个奶,第二分钟。。。暴力到1000分钟,然后搜索出最大值即可。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <utility>
#define mod 1000000007
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
struct node{
int s,e,v;
} t[105];
int bt[1005];
int yt[1005];
int main(){
memset(yt,0,sizeof(yt));
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d %d %d",&t[i].s,&t[i].e,&t[i].v);
}
for(int i=1;i<=1000;i++){
for(int j=0;j<n;j++){
if(i>=t[j].s&&i<=t[j].e){
yt[i]+=t[j].v;
}
}
}
int ans=-1;
for(int i=0;i<=1000;i++){
ans=max(ans,yt[i]);
}
cout<<ans<<endl;
}
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
5
25 3
105 30
20 50
10 17
100 10
10
这道题需要学会用STL里的优先队列。
下面这篇博客有关于优先队列很详细的介绍,大家可以看一下:
https://www.cnblogs.com/xzxl/p/7266404.html
这道题只需对结构体排个序,按时间顺序push入队,然后价值大的先出队,每次入队时都比较一下出队的所留时长与max,最后得到的便是结果。
#include <bits/stdc++.h>
#define maxn 10005
using namespace std;
struct node{
int s,v,id;
friend bool operator<(node a,node b){
return a.id>b.id;
}
}a[100005];
bool cmp(node x,node y){
if(x.s==y.s)
return x.v<y.v;
return x.s<y.s;
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].s>>a[i].v;
a[i].id=i;
}
sort(a+1,a+n+1,cmp);
priority_queue<node>que;
int time=0,ans=0,k;
for(int i=1;i<=n;i++){
if(que.empty()){
time=a[i].s+a[i].v;
for(k=i+1;k<=n;k++){
if(a[k].s<=time)
que.push(a[k]);
else break;
}
}
else{
node tmp=que.top();
que.pop();
ans=max(ans,time-tmp.s);
time+=tmp.v;
for(;k<=n;k++){
if(a[k].s<=time)
que.push(a[k]);
else break;
}
}
}
cout<<ans<<endl;
}
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
6 3 2
1 1 10 14 4 3
4
这道题当时比赛的时候想不出来要怎么做QAQ,后来看了题解才恍然大悟。我们可以不去想要怎么求出最小的时间,而是去想满足题目的时间的最小值即可。所以这里便可以用二分不断搜索满足的条件,直到找到最小满足的时间就是答案了。
AC代码如下:
#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
int n,m,c;
int t[maxn];
bool judge(int x){
int l=0,r=0;
int ans=0;
while(l<n&&r<n){
while(r-l+1<=c&&t[r]-t[l]<=x&&r<n){
r++;
}
ans++;
l=r;
}
if(ans>m) return false;
else return true;
}
int main(){
cin>>n>>m>>c;
for(int i=0;i<n;i++){
scanf("%d",&t[i]);
}
sort(t,t+n);
int l=0,r=1000000000;
while(l<r){
int mid=(l+r)>>1;
if(!judge(mid)) l=mid+1;
else r=mid;
}
cout<<r<<endl;
}
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
7 3
1
15
7
9
2
5
10
84
这题有一个坑人的地方,就是上文有提到奶牛是一个consecutive set连续集,所以这里我们就不能给全部奶牛排序了。
这时候,想要怎样才能求到连续的最优解,我们应该第一反应就是动态规划(DP),通过不断求出局部最优,得到全局最优。
我们便可以写出
题目样例的dp推算:
0 1 30 0 0 0 0 0
0 1 30 37 0 0 0 0
0 1 30 45 0 0 0 0
0 1 30 45 54 0 0 0
0 1 30 45 54 0 0 0
0 1 30 45 54 63 0 0
0 1 30 45 54 63 0 0
0 1 30 45 54 63 68 0
0 1 30 45 54 63 72 0
0 1 30 45 54 63 72 83
0 1 30 45 54 63 72 84
将此写为代码即可
AC代码如下:
#include <bits/stdc++.h>
#define maxn 10005
using namespace std;
int dp[maxn];
int a[maxn];
int n,k;
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
}
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++){
int maxx=a[i];
dp[i]=dp[i-1]+a[i];
for(int j=1;j<k;j++){
if(i-j<=0) break;
maxx=max(maxx,a[i-j]);
dp[i]=max(dp[i],dp[i-j-1]+maxx*(j+1));
/*for(int i=0;i<=n;i++){
printf("%d ",dp[i]);
}
printf("\n");*/
}
}
cout<<dp[n]<<endl;
}
本人蒟蒻一枚,大家有问题欢迎提出Qrz~