CodeForces 604B More Cowbell

http://codeforces.com/problemset/problem/604/B

题意:n个数字,k个盒子,把n个数放入k个盒子中,每个盒子最多只能放两个数字,问盒子容量的最小值是多少(水题)

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdlib>
#include <algorithm>
using namespace std;

typedef long long LL;
#define N 200000
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define MOD 1000000007
#define met(a, b) memset (a, b, sizeof(a))

const double EPS = 1e-8;

int a[N];

int cmp (const void *a, const void *b)
{
    return (int *)a-(int *)b;
}

int main ()
{
    int n, m;
    while (scanf ("%d %d", &n, &m) != EOF)
    {
        met (a, 0);
        int maxn = 0;

        for (int i=0; i<n; i++)
        {
            scanf ("%d", &a[i]);
            maxn = max (maxn, a[i]);
        }

        int k = n-m;

        qsort (a, n, sizeof(a[0]), cmp);

        for (int i=0,j=k*2-1; i<k*2,j>=0; i++,j--)
            maxn = max (maxn, a[i]+a[j]);

        printf ("%d\n", maxn);
    }
    return 0;
}


你可能感兴趣的:(CodeForces 604B More Cowbell)