这个周还是继续学习了二分法的有关知识有另外参加了两场dive2的比赛,在做题的时候出现了一个mid取值错误的情况,他如果写成mid=(left+right)/2时,如果left和right比较大的时候,可以改成计算机位运算较除法更快,即mid=(left+right)>>1。另外我看了之前codeforces比赛时没有提交成功的题的题解,感觉略微找到了一些打比赛的技巧。
题目:
1.Farmer John
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.
FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called “fajomonths”. Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.
FJ’s goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.
Input
Line 1: Two space-separated integers: N and M
Lines 2… N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Line 1: The smallest possible monthly limit Farmer John can afford to live with.
Sample Input
7 5
100
400
300
100
500
101
400
Sample Output
500
Hint
If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.
题意:给出n天每天的花费,将n天分成m组,且分组的天数是连续的,要求分完各组的花费和尽量小,输出各组花费和的最大值。
这道题基本上是套用了二分法的套路,通过solve函数判断可以将数据分为几组,然后和规定的分组数进行比较,如果分成的组数偏多即mid偏小,则调整left的取值,组数偏少即mid偏大,则调整right的取值。
ac代码:
#include<iostream>
#include<cmath>
#include<algorithm>
#define M 100001
using namespace std;
int n,m;
int a[M];
bool solve(int mid)
{int sum=0;int num=1;for(int i=0;i<n;i++){if(sum+a[i]<=mid)sum+=a[i];else{sum=a[i];num++;}}if(num>m)return false;elsereturn true;
}
int main()
{while(cin>>n>>m){int left=0,right=0;for(int i=0;i<n;i++){cin>>a[i];left=max(left,a[i]);right+=a[i];}int mid;while(left<=right){mid=(left+right)/2;if(solve(mid))right=mid-1;elseleft=mid+1;}cout<<mid<<endl;}return 0;
}
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.
Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest distance before he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.
Input
Line 1: Three space-separated integers: L, N, and M
Lines 2… N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output
Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks
Sample Input
25 5 2
2
14
11
21
17
Sample Output
4
Hint
Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
题意:有一条河,长度已知,河中间有石头且数量已知,相邻两块石头之间距离也知道,现在可以移除一些石头问移除m块石头后相邻两块石头的最大距离。
通过二分法枚举答案,首先确定left=0,right=河的长度,当两块石头的距离小于mid时,就移走此时这块,并记录移走石头的数目,当大于mid时,更新前一块石头的位置。
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
#define M 100001
bool cmp(int a,int b)
{return a<b;
}
ll a[M];
int main()
{ll len;int n,m;while(cin>>len>>n>>m){a[0]=0;a[n+1]=len;for(int i=1;i<=n;i++)cin>>a[i];sort(a,a+n+2,cmp);int left=1,right=len;ll mid;while(left<=right){int num=0;mid=(left+right)/2;int last=0;for(int i=1;i<=n;i++){if(a[i]-last<mid)num++;elselast=a[i];}if(num<=m)left=mid+1;elseright=mid-1;}cout<<right<<endl;}return 0;
}
生死看淡,不服就干
本文发布于:2024-01-30 13:05:44,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170659114820210.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |