Test instructions: There is a ring with a length of L, some apples on the ring, a maximum of k apples at a time, ask the shortest distance that all apples are taken.
Idea: Greedy plus DP.
First of all, if the ring from the point of the cut, then only need to greedy on both sides, and now connected together, then there is a possibility, that is, around the ring for a week to get the apple this solution.
It is not difficult to prove that, at most, it is possible to circle only once, because the loop can be divided into one side of the Apple plus a round of this scheme.
Specific implementation: The CNT Apple tree is divided into n each tree has an apple apple tree, record each Apple's location.
For both sides, record the minimum distance required to take M apples, this step can be implemented with DP, i.e. sul[m] = sul[m-k]+posl[m].
Finally, we discuss the problem of circle, the K apples taken around the circle must be taken from the apples near the midpoint, i.e.
ans = min (ans, sul[nl-i]+sur[max (nr-k+i, 0)]+l).
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt #include <string> #include <map> #include <set> #define EPS 1e-6 #define LL Long long using namespace std; const int MAXN = 100000 + 50;const int INF = 0x3f3f3f3f;int L, cnt, K, N, NL, nr; LL SUL[MAXN], Sur[maxn];int POS[MAXN], POSL[MAXN], Posr[maxn];int main () {//freopen ("Input.txt", "R", stdin); int t; CIN &G T;> T;while (t--) {n = nl = NR = 0;scanf ("%d%d%d", &l, &cnt, &k), while (cnt--) {int x, a;scanf ("%d%d", &x , &a); while (a--) pos[++n] = x;} for (int i = 1; I <= n; i++) if (2*pos[i] < L) POSL[++NL] = Pos[i];else Posr[++nr] = L-pos[i];sort (posl+1, POSL+1+NL) ; Sort (posr+1, posr+nr+1); for (int i = 1; I <= nl; i++) if (i > K) sul[i] = Sul[i-k] + posl[i]*2;else sul[i] = posl[i]*2 ; for (int i = 1; I <= nr; i++) if(i > K) sur[i] = Sur[i-k] + posr[i]*2;else sur[i] = posr[i]*2; LL ans = sul[nl] + sur[nr];for (int i = 1; i<=nl && i<k; i++) ans = min (ans, sul[nl-i]+sur[max (nr-k+i, 0)]+l) ; cout << ans << endl;} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Multi-university Training Contest 2 1004 Delicious Apples (greedy)