天天看點

Solution -「ARC 125E」Snack

\(\mathcal{Description}\)

  Link.

  把 \(n\) 種零食分給 \(m\) 個人,第 \(i\) 種零食有 \(a_i\) 個;第 \(i\) 個人得到同種零食數量不超過 \(b_i\),總數量不超過 \(c_i\),求最多分出的零食數量。

  \(n,m\le2\times10^5\)。

\(\mathcal{Solution}\)

  很容易看出這是網絡流模型:

  • 源點 \(S\) 連向每種零食 \(i\),容量 \(a_i\);
  • 零食 \(i\) 連向人 \(j\),容量 \(b_j\);
  • 人 \(j\) 連向彙點 \(T\),容量 \(c_j\)。

\(\mathcal{Code}\)

/*~Rainybunny~*/

#include <bits/stdc++.h>

#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i )

typedef long long LL;

inline void chkmin( LL& a, const LL b ) { b < a && ( a = b ); }

const int MAXN = 2e5;
int n, m, b[MAXN + 5], ord[MAXN + 5];
LL a[MAXN + 5], c[MAXN + 5];

int main() {
    scanf( "%d %d", &n, &m );
    rep ( i, 1, n ) scanf( "%lld", &a[i] );
    rep ( i, 1, m ) scanf( "%d", &b[i] ), ord[i] = i;
    rep ( i, 1, m ) scanf( "%lld", &c[i] );

    std::sort( a + 1, a + n + 1,
      []( const LL u, const LL v ) { return u > v; } );
    std::sort( ord + 1, ord + m + 1, []( const int u, const int v )
      { return 1ull * c[u] * b[v] < 1ull * c[v] * b[u]; } );

    LL sa = 0, sb = 0, sc = 0, ans = 1ll << 60;
    rep ( i, 1, n ) sa += a[i];
    rep ( i, 1, m ) sb += b[i];
    for ( int i = 0, j = 1; i <= n; ++i ) {
        sa -= a[i];
        for ( ; j <= m && 1ll * i * b[ord[j]] > c[ord[j]];
          sb -= b[ord[j]], sc += c[ord[j++]] );
        chkmin( ans, sa + i * sb + sc );
    }
    printf( "%lld\n", ans );
    return 0;
}

           

繼續閱讀