1. 程式人生 > >Bzoj1899: [Zjoi2004]Lunch 午餐

Bzoj1899: [Zjoi2004]Lunch 午餐

mem esp post ble clu ans define zoj fin

題面

傳送門

Sol

首先顯然吃飯久的要排在前面
之後再來分配隊伍,設\(f[i][j]\)表示到第\(i\)個人,\(A\)隊伍要等\(j\)的最小吃完飯時間
那麽就是一個簡單的背包吧。。。

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
# define Sqr(x) ((x) * (x))
using namespace std;
typedef long long ll;
const int _(205);

IL ll Read(){
    RG ll x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, a[_], b[_], sum[_], f[_][_ * _], ans = 2e9, id[_];

IL bool Cmp(RG int x, RG int y){  return b[x] > b[y];  }

int main(RG int argc, RG char* argv[]){
    n = Read();
    for(RG int i = 1; i <= n; ++i) a[i] = Read(), b[i] = Read(), id[i] = i;
    sort(id + 1, id + n + 1, Cmp);
    for(RG int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[id[i]];
    Fill(f, 63); f[0][0] = 0;
    for(RG int i = 1; i <= n; ++i)
        for(RG int j = 0; j <= sum[i]; ++j){
            f[i][j] = max(f[i - 1][j], (sum[i] - j) + b[id[i]]);
            if(j >= a[id[i]]) f[i][j] = min(f[i][j], max(f[i - 1][j - a[id[i]]], j + b[id[i]]));
        }
    for(RG int i = 0; i <= sum[n]; ++i) ans = min(ans, f[n][i]);
    printf("%d\n", ans);
    return 0;
}

Bzoj1899: [Zjoi2004]Lunch 午餐