【t035】收入计划

Time Limit: 1 second
Memory Limit: 32 MB

【问题描述】

高考结束后,同学们大都找到了一份临时工作,渴望挣得一些零用钱。从今天起,Matrix67将连续工作N天(1<=N<=100 000)。每
一天末他可以领取当天及前面若干天里没有领取的工资,但他总共只有M(1<=M<=N)次领取工资的机会。Matrix67已经知道了在接
下来的这N天里每一天他可以赚多少钱。为了避免自己滥用零花钱,他希望知道如何安排领取工资的时间才能使得领到工资最多
的那一次工资数额最小。注意Matrix67必须恰好领工资M次,且需要将所有的工资全部领走(即最后一天末需要领一次工资)。
【输入格式】

第一行输入两个用空格隔开的正整数N和M
以下N行每行一个不超过10000正整数,依次表示每一天的薪水。
【输出格式】

输出领取到的工资的最大值最小是多少。

Sample Input

7 5
100
400
300
100
500
101
400

Sample Output

500

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t035

【题解】

二分领取的工资为多少;
如果能够在m次内领走就缩小答案;否则增大答案;
裸题;

【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

void rel(LL &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t) && t!='-') t = getchar();
    LL sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

void rei(int &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t)&&t!='-') t = getchar();
    int sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

const int MAXN = 1e5+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n,m;
int a[MAXN];

bool ok(int x)
{
    int now = 0,cnt = 1;
    rep1(i,1,n)
        if (now+a[i]>x)
        {
            cnt++;
            if (cnt>m) return false;
            now = a[i];
            if (now > x) return false;
        }
        else
            now+=a[i];
    return true;
}

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    scanf("%d%d",&n,&m);
    int l = 0,r= 0;
    rep1(i,1,n)
        rei(a[i]),r+=a[i];
    int ans = -1;
    while (l <= r)
    {
        int m = (l+r)>>1;
        if (ok(m))
            ans = m,r = m-1;
        else
            l=m+1;
    }
    printf("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626863.html