• 【31.72%】【codeforces 604B】More Cowbell


    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won’t place more than two cowbells into a single box. Since Kevin wishes to minimize expenses, he is curious about the smallest size box he can use to pack his entire collection.

    Kevin is a meticulous cowbell collector and knows that the size of his i-th (1 ≤ i ≤ n) cowbell is an integer si. In fact, he keeps his cowbells sorted by size, so si - 1 ≤ si for any i > 1. Also an expert packer, Kevin can fit one or two cowbells into a box of size s if and only if the sum of their sizes does not exceed s. Given this information, help Kevin determine the smallest s for which it is possible to put all of his cowbells into k boxes of size s.

    Input
    The first line of the input contains two space-separated integers n and k (1 ≤ n ≤ 2·k ≤ 100 000), denoting the number of cowbells and the number of boxes, respectively.

    The next line contains n space-separated integers s1, s2, …, sn (1 ≤ s1 ≤ s2 ≤ … ≤ sn ≤ 1 000 000), the sizes of Kevin’s cowbells. It is guaranteed that the sizes si are given in non-decreasing order.

    Output
    Print a single integer, the smallest s for which it is possible for Kevin to put all of his cowbells into k boxes of size s.

    Examples
    input
    2 1
    2 5
    output
    7
    input
    4 3
    2 3 5 9
    output
    9
    input
    3 2
    3 5 7
    output
    8
    Note
    In the first sample, Kevin must pack his two cowbells into the same box.

    In the second sample, Kevin can pack together the following sets of cowbells: {2, 3}, {5} and {9}.

    In the third sample, the optimal solution is {3, 5} and {7}.

    【题目链接】:http://codeforces.com/contest/604/problem/B

    【题解】

    二分最后的箱子容量;
    左端点应该是最大的cowbell,右端点无限大.
    看看m需要用几个箱子d;
    (如果加上这个数大于m或装了两个就不装了);
    (装的时候,先装大的,大的尝试和当前剩余最小的组合在一起,如果能组合就组合,不能的话大的单独装.);
    if (d <= k)
    ans = m,r = m-1;
    else
    if (d > k)//箱子用多了那就增大箱子容量
    l = m+1;


    【完整代码】

    #include <bits/stdc++.h>
    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
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    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,k;
    int s[MAXN];
    
    int ok(int lim)
    {
        int tot = 0;
        int r = n,l = 1;
        while (l <= r)
        {
            if (l!=r && s[r]+s[l]<=lim)
            {
                r--,l++;
                tot++;
            }
            else
                tot++,r--;
        }
        return tot;
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);rei(k);
        int l = 1,r = 21e8;
        rep1(i,1,n)
            {
                rei(s[i]);
                l = max(s[i],l);
            }
        int ans = -1;
        while (l <= r)
        {
            int m = (l+r)>>1;
            if (ok(m)<=k)
            {
                ans = m;
                r = m-1;
            }
            else
                l = m+1;
        }
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    cmd输入输出重定向
    【转载】标准输入输出、错误输出、重定向标准输出
    cmd 重定向
    Windows下cmd标准输入输出重定向
    Windows 命令行输入输出重定向问题
    MATLAB数值计算与符号运算
    选择排序法 冒泡排序法 本质上是对内存进行整理
    学习笔记:Monkey runner(一)
    Monkey test
    fiddler:快速标识接口
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626811.html
Copyright © 2020-2023  润新知