Topic Link: Click to open the link
Test instructions
Given n-long sequences, M, K
Select some numbers to make the selected number and maximum. Output and.
Limit: The maximum number of k in any interval [I, I+m] is selected.
Ideas:
Petition P367, Interval K coverage problem, an interval as a point, then select a point is equivalent to covering the M range.
#include <iostream> #include <stdio.h> #include <string.h> #include <queue> #include <math.h >using namespace std; #define LL int#define inf 0x3f3f3f3f#define inf 0x3fffffffffffffffll#define N 3000#define M 3000*3 000struct Edge {ll to, cap, cost, NEX; Edge () {} edge (ll to,ll cap,ll cost,ll next): To, Cap (CAP), cost, NEX (next) {}} edge[m];ll Head[n], Edgenum;ll D[n] , A[n], P[n];bool inq[n];void Add (ll from,ll to,ll cap,ll cost) {Edge[edgenum] = Edge (To,cap,cost,head[from]); Head[from] = edgenum++; Edge[edgenum] = Edge (From,0,-cost,head[to]); Head[to] = edgenum++;} BOOL SPFA (ll S, ll T, LL &flow, ll &cost) {for (ll i = 0; I <= t; i++) d[i] = inf; memset (inq, 0, sizeof inq); queue<ll>q; Q.push (s); D[s] = 0; A[s] = inf; while (!q.empty ()) {ll u = Q.front (); Q.pop (); Inq[u] = 0; for (ll i = head[u]; ~i; i = Edge[i].nex) {Edge &e = Edge[i]; if (E.cap && D[e.to] > D[u] + e.cost) {d[e.to] = D[u] + e.cost; P[e.to] = i; A[e.to] = min (A[u], e.cap); if (!inq[e.to]) {inq[e.to]=1; Q.push (e.to);} }}}//if the cost is INF, abort the charge flow if (d[t] = = inf) return false; Cost + = d[t] * A[t]; Flow + = A[t]; ll u = t; while (U! = s) {edge[p[u]].cap-= a[t]; Edge[p[u]^1].cap + = a[t]; u = edge[p[u]^1].to; } return true; ll Mincost (ll S,ll t) {ll flow = 0, cost = 0; while (SPFA (s, t, flow, cost)); return cost;} void Init () {memset (head,-1,sizeof head); edgenum = 0;} int A[n], from, to, N, M, k;void input () {for (int i = 1; i<= N; i++) scanf ("%d", &a[i]); Init (); from = 0; to = n+2; Add (from, 1, K, 0); for (int i = 1; I <= n; i++) {int tmp = MIN (m+i, n+1); Add (i, TMP, 1,-a[i]); Add (i, i+1, K, 0); } Add (N+1, to, K, 0);} int main () {while (~SCANF ("%d%d%d", &n, &amP;m, &k)) {input (); int cost = Mincost (from, to); cout<<-cost<<endl; } return 0;}
HDU 4106 Fruit Ninja interval k coverage problem minimum cost flow