POJ3273+POJ3258 两道二分

3 篇文章 0 订阅
Monthly Expense
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 13851 Accepted: 5567

Description

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 nextN (1 ≤ N ≤ 100,000) days.

FJ wants to create a budget for a sequential set of exactly M (1 ≤ MN) 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天,让你分成M组,求满足条件的M组里的总和最小的值,对总和进行二分,让N天总和作为上界,N天里的最大值作为下界,进行二分,不知道为什么不能写LOW<=HIGH,去了等号就过了。。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int n,m;
int a[100005];

bool C(int cost)
{
    int sum=0;
    int res=1;
    for(int i=0;i<n;i++)
    {
        if(sum+a[i]<cost)
        {
            sum+=a[i];
        }
        else
        {
            sum=a[i];
            res++;
        }
    }
    if(res>m)
        return false;
    else
        return true;
}
int main()
{
    while(cin>>n>>m)
    {
        int low=0,high=0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            high+=a[i];
            if(low<a[i])
                low=a[i];
        }
        int mid=(low+high)/2;
        while(low<high)
        {

            if(C(mid)==0)
            {
                low=mid+1;
            }
            else
                high=mid-1;
            mid=(low+high)/2;
        }
        cout<<mid<<endl;

    }
    return 0;
}
River Hopscotch
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6488 Accepted: 2812

Description

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 distanceDi 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 toM rocks (0 ≤ MN).

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

Source

给你河的长度L,以及路上的N块石头,允许你搬出M块石头,然后让剩下的石头间距的最小值最大。明显的最大值最小化问题,将距离起点的值升序排序,然后二分可以跳的距离即可,当然起点和终点是不能移动的。。。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int n,m,dis[50005],l;

bool C(int len)
{
    int res=0,pre=0;
    for(int i=0;i<=n;i++)
    {
        if(dis[i]-pre<len)
        {
            res++;
        }
        else
            pre=dis[i];
    }
    return res<=m;
}

int main()
{
    while(cin>>l>>n>>m)
    {
        for(int i=0;i<n;i++)
        {
            cin>>dis[i];
        }
        dis[n]=l;
        sort(dis,dis+n+1);
        int low=0;
        int high=l;
        int ans;
        while(low<=high)
        {
            int mid=(low+high)/2;
            if(C(mid))
            {
                ans=mid;
                low=mid+1;
            }
            else
            high=mid-1;

        }
        cout<<ans<<endl;
    }
    return 0;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值