http://codeforces.com/contest/802/problem/N
【题解】
方法一:
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 6 #define N 200020 7 8 int to[N], head[N], nxt[N], cost[N], cap[N], cnt; 9 10 void init(){ memset(head, -1, sizeof head); } 11 12 void add_Edge(int S, int T, int c, int w){ 13 nxt[cnt] = head[S], cap[cnt] = c, cost[cnt] = w, to[cnt] = T, head[S] = cnt ++; 14 nxt[cnt] = head[T], cap[cnt] = 0, cost[cnt] = -w, to[cnt] = S, head[T] = cnt ++; 15 } 16 17 const LL INF = 1e14; 18 19 LL dist[N]; 20 int prv[N]; 21 bool vis[N]; 22 23 LL SPFA(int S, int T, int vet){ 24 queue <int> Q; 25 fill(prv, prv + vet, -1); 26 fill(dist, dist + vet, INF); 27 Q.push(S); dist[S] = 0, vis[S] = true; 28 while(!Q.empty()){ 29 int x = Q.front(); Q.pop(), vis[x] = false; 30 for(int id = head[x]; ~id; id = nxt[id]) if( cap[id] ){ 31 int y = to[id]; 32 if(dist[y] > dist[x] + cost[id]){ 33 dist[y] = dist[x] + cost[id]; 34 prv[y] = id; 35 if(!vis[y]) Q.push(y), vis[y] = true; 36 } 37 } 38 } 39 if(!~prv[T]) { return INF; } 40 41 for(int cur = T; cur != S; cur = to[cur^1]){ 42 cur = prv[cur]; 43 cap[cur] --; 44 cap[cur ^ 1] ++; 45 } 46 return dist[T]; 47 } 48 49 int a[N], b[N], n, k; 50 51 int main(){ 52 //freopen("in.txt", "r", stdin); 53 scanf("%d %d", &n, &k); 54 for(int i = 1; i <= n; i ++) scanf("%d", a + i); 55 for(int i = 1; i <= n; i ++) scanf("%d", b + i); 56 57 int S = 0, T = 2 * n + 1; 58 init(); 59 for(int i = 1; i <= n; i ++){ 60 add_Edge(S, i, 1, a[i]); 61 add_Edge(i + n, T, 1, b[i]); 62 add_Edge(i, i + n, n, 0); 63 if(i < n) add_Edge(i + n, i + n + 1, n - i, 0); 64 } 65 LL ans = 0; 66 for(int i = 1; i <= k; i ++) ans += SPFA(S, T, T + 1); 67 cout << ans << endl; 68 }
方法二:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define oo 0x3f3f3f3fLL 5 #define pb push_back 6 #define mp make_pair 7 #define fi first 8 #define se second 9 #define SUM(x,y) accumulate(x,y,0) 10 #define gcd(x,y) (__gcd(x,y)) 11 #define LOWBIT(x) (x&(-x)) 12 #define CLR(x,y) memset(x,y,sizeof(x)) 13 #define PD(x) printf("%d\n",(x)) 14 #define PF(f) printf("%lf\n",(f)) 15 #define PS(s) puts(s) 16 17 typedef pair<int, int> pii; 18 typedef long long LL; 19 20 template <class T> inline bool chkmin(T& x, T y) { return x > y ? x = y, true : false; } 21 template <class T> inline bool chkmax(T& x, T y) { return x < y ? x = y, true : false; } 22 23 template <class T> T reads() { 24 T x = 0; 25 static int f; 26 static char c; 27 for (f = 1; !isdigit(c = getchar()); ) if (c == '-') f = -1; 28 for (x = 0; isdigit(c); c = getchar()) x = x * 10 + c - 48; 29 return x * f; 30 } 31 #define read reads<int> 32 #define readll reads<LL> 33 34 int n, k, v, a[2205], b[2205]; 35 LL l, r, res, m, ans; 36 37 int main(void) { 38 scanf("%d%d", &n, &k); 39 for (int i = 0; i < n; ++i) a[i] = read(); 40 for (int i = 0; i < n; ++i) b[i] = read(); 41 for(int i=0;i<n;i++) 42 { 43 cout<<a[i]<<" "; 44 } 45 cout<<endl; 46 for(int i=0;i<n;i++) 47 { 48 cout<<b[i]<<" "; 49 } 50 cout<<endl; 51 l = 0; r = 1e10; res = 1e18; 52 while (l < r) { 53 ans = 0; m = (l + r) >> 1; v = 0; 54 priority_queue<LL> qa, qb; 55 56 for (int i = 0; i < n; ++i) { 57 qa.push(-a[i]); 58 59 LL ga = -qa.top() + b[i] - m; 60 LL gb = qb.empty() ? LL(1e18) : b[i] - qb.top(); 61 if (ga <= gb && ga <= 0) { 62 ans += ga; v++; 63 qa.pop(); qb.push(b[i]); 64 } else if (ga > gb && gb < 0) { 65 ans += gb; 66 qb.pop(); qb.push(b[i]); 67 } 68 } 69 70 if (v >= k) { r = m - 1; res = ans + k * m; } 71 else l = m + 1; 72 } 73 return cout << res << endl, 0; 74 }