Codeforces Round #334 (Div. 2) B. More Cowbell

題意:裝盒子。一個盒子最多裝倆。k個相同容量的盒子,求最小容量。

思路:顯然用的盒子越多最小容量越小。所以若n <= k,就用n個盒子;否則就儘量多的一個盒子裝一個,且先挑大的裝。這樣剩下的就是兩個裝一個的。每次挑最大的和最小的裝一個,這樣一定是最優的,可以證出來。不用二分。

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>
using namespace std;

typedef long long LL;
#define mem(a, n) memset(a, n, sizeof(a))
#define ALL(v) v.begin(), v.end()
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pb push_back
#define eps 1e-8
const int inf = 0x3f3f3f3f, N = 1e5 + 5, MOD = 1e9 + 7;

int T, cas = 0;
int n, k, x;
int s[N], ans;

int main(){
#ifdef LOCAL
    freopen("/Users/apple/input.txt", "r", stdin);
//  freopen("/Users/apple/out.txt", "w", stdout);
#endif
    
    ans = 0;
    sii(n, k);
    for(int i = 1; i <= n; i ++) si(s[i]);
    x = 2 * k - n;
    int r = n - x;
    if(r < 0) r = 0;
    for(int i = r + 1; i <= n; i ++) ans = max(ans, s[i]);
    for(int i = 1; i <= r / 2; i ++) ans = max(ans, s[i] + s[r - i + 1]);
    printf("%d\n", ans);
    return 0;
}


發佈了115 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章