二分搜索POJ

POJ1064  求最大可行解

You are to help the Cable Master, by writing a program that will determine the maximal possible length of a cable piece that can be cut from the cables in the stock, to get the specified number of pieces.

Input

The first line of the input file contains two integer numbers N and K, separated by a space. N (1 = N = 10000) is the number of cables in the stock, and K (1 = K = 10000) is the number of requested pieces. The first line is followed by N lines with one number per line, that specify the length of each cable in the stock in meters. All cables are at least 1 meter and at most 100 kilometers in length. All lengths in the input file are written with a centimeter precision, with exactly two digits after a decimal point.

Output

Write to the output file the maximal length (in meters) of the pieces that Cable Master may cut from the cables in the stock to get the requested number of pieces. The number must be written with a centimeter precision, with exactly two digits after a decimal point. 
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output file must contain the single number "0.00" (without quotes).

#include<bits/stdc++.h>
using namespace std;
double L[10005];int n,k;
const double INF=100005;

bool judge(double len){
    int cnt=0;
    for(int i=0;i<n;i++){
        cnt+=(int)(L[i]/len);
    }
    return cnt>=k;
}
int main()
{
  //  freopen("datain.txt","r",stdin);
    while(~scanf("%d%d",&n,&k)){
        for(int i=0;i<n;i++)
            scanf("%lf",&L[i]);
        double lb=0,ub=INF;
        while(ub-lb>0.001){ //指定EPS是0.001
            double mid=(ub+lb)/2;
            if(judge(mid))lb=mid;
        else ub=mid;
        }
        printf("%.2f\n",floor(ub*100)/100);//向下取整
    }
}

POJ2456 最大化最小值

armer 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

#include<algorithm>
#include<cstdio>

using namespace std;
int x[100005];
int n,c;//n stalls, c cows
const int INF=1e+009;

bool judge(int d)
{
    int last=0;
//第一头牛的坐标是0
    for(int i=1; i<c; i++)
    {
        int crt=last+1;
        while(crt<n&&x[crt]-x[last]<d)
        {
            crt++;
        }
        if(crt==n)return false;
//取完了所有牛屋,则说明当前的距离d不可行
        last=crt;
//更新最近一头牛的坐标
    }
    return true;
}
int main()
{
    //    freopen("datain.txt","r",stdin);
    while(~scanf("%d%d",&n,&c))
    {
        for(int i=0; i<n; i++)
            scanf("%d",&x[i]);
        sort(x,x+n);
//需要把每个牛屋按照坐标排序
        int lb=0,ub=INF;
        while(ub-lb>1)  //指定EPS是1
        {
            int mid=(ub+lb)/2;
            if(judge(mid))lb=mid;
            else ub=mid;
        }
        printf("%d\n",lb);
    }
}

 

POJ3061

Description

A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.

Input

The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

Output

For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

 

尺取法:

#include<cstdio>
#include<algorithm>
using namespace std;
int n,S; int N[100000];
int main()
{
    // freopen("datain.txt","r",stdin);
     int c;
     scanf("%d",&c);
     while(c--){
        scanf("%d%d",&n,&S);
        for(int i=0;i<n;i++){
            scanf("%d",&N[i]);
        }
        int t=0,s=0,sum=0,res=n+1;
        for(;;){
            while(t<n&&sum<S){
                sum+=N[t++];
            }
            if(sum<S)break;
            res=min(res,t-s);
            sum-=N[s++];
        }
        if(res>n)res=0;
        printf("%d\n",res);
     }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值