CF_604B(二分+贪心)

时间:2021-08-21 10:40:25


#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<stdio.h>
#include<math.h>
using namespace std;
int bell[1000000+100];
int n;
bool kefou(int rong,int tou,int wei,int k)
{
    int i,q,w;
    int ge=0;
    for(w=wei,i=1;;)
    {
        if(w<i)break;
        if(bell[w]+bell[i]<=rong)
        {
            w-=1;
            i+=1;
            ge+=1;
            continue;
        }
        else
        {
            w-=1;
            ge+=1;
            continue;
        }
    }
    if(ge<=k)return 1;
    return 0;
}

int binary(int k)
{
    int high=0xfffffff,low=bell[n];
    int midle;
    while(high>=low)
    {
        midle=(high+low)/2;
        if(kefou(midle,1,n,k)==1&&kefou(midle-1,1,n,k)==0)break;
        if(kefou(midle,1,n,k))high=midle-1;
        else low=midle+1;
    }
    return midle;
}

int main()
{
    int k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        int i,q,w;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&bell[i]);
        }

        int result=binary(k);
        printf("%d\n",result);
    }
    return 0;
}