二分法(二)

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.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 100001
#define MOD 123
#define E 1e-6
using namespace std;
int n,m;
int a[N];
bool judge(int value)//判断当前花费可把n分成几组
{
    int sum=0;//花费总和
    int cnt=1;//初始时只有一组
    for(int i=0;i<n;i++)//枚举每天花费
    {
        if(sum+a[i]<=value)
            sum+=a[i];
        else
        {
            sum=a[i];
            cnt++;
        }
    }
    if(cnt>m)//若分组数比规定数要多,则mid偏小,需调整左值
        return false;
    else//若分组数比规定数要少,则mid偏大,需调整右值
        return true;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int left=0,right=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            left=max(left,a[i]);
            right+=a[i];
        }
        int mid;
        while(left<=right)
        {
            mid=(left+right)/2;
            if(judge(mid))
                right=mid-1;
            else
                left=mid+1;
        }
        printf("%d\n",mid);
    }
    return 0;
}

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,…,xN (0 <= xi <= 1,000,000,000).

His C (2 <= C <= N) cows don’t like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?
Input

  • Line 1: Two space-separated integers: N and C

  • Lines 2…N+1: Line i+1 contains an integer stall location, xi
    Output

  • Line 1: One integer: the largest minimum distance
    Sample Input
    5 3
    1
    2
    8
    4
    9
    Sample Output
    3
    Hint
    OUTPUT DETAILS:

FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.

Huge input data,scanf is recommended.

#include <iostream>
#include <algorithm>
#define N 100000
 
bool judge(int arr[], int n, int c,int distance) {
	int x = arr[0];
	int count = 1;
	for (int i = 1; i < n; i++) {
		if (arr[i] - x >= distance) {
			x = arr[i];
			count++;
		}
	}
	
	return count >= c;
	
}
 
int main() 
{
	int arr[N];
	int n, c;
	std::cin >> n >> c;
	for (int i = 0; i < n; i++)
		std::cin >> arr[i];
	std::sort(arr, arr + n);
	int left = 1;
	int right = arr[n - 1] - arr[0];
	int mid;
	while (left <= right) {
		mid = left + (right - left) / 2;
		if (judge(arr, n, c, mid)) {
			left = mid + 1;
		}
		else
			right = mid - 1;
	}
 
	std::cout << right << std::endl;
}

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).

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define pb push_back
#include<vector>
typedef long long ll;
typedef long double ld;
const ll MOD=1e9+7;
using namespace std;
ll a[50005];
ll l,n,num;
bool judge(ll mid)
{
	int bits=0;
	for(int i=0;i<n;i++)
	{
		int ans=0;
		for(int j=1;j<=n-i;j++)
		{
			if(a[i+j]-a[i]<mid)
			{
				ans++;
				bits++;
			}else
			break;
		}
		i+=ans;
		if(bits>num)
		{
			return true;
		}
	}
	return false;
}
int main()
{
	
	a[0]=0;
	cin>>l>>n>>num;
	for(int i=1;i<=n;i++)
	cin>>a[i];
	a[n+1]=l;
	n++;
	sort(a,a+n+1);
	ll left=0,right=l,mid=(left+right)/2;
	while(right-left>1)
	{
		
		if(judge(mid))
		{
			right=mid;
		}else
		{
			left=mid;
		}
		mid=(left+right)/2;
	}
	while(!judge(mid))mid++;
	cout<<mid-1;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值