Topic links
Test instructions: l rings, n trees, backpack capacity K, tells you the ID of K apple tree, and the number of fruit on each tree, once the backpack is full to return to the starting point (id==0)
Empty, ask you at least how many road, can pick up all the apples.
Ideas:
Because it is a ring, so in fact, the farthest point from the beginning should be L/2;
Two ways of picking apples, one starting from the top half, using dp[0][i] to record;
Another, starting from the lower half of the circle, with Dp[1][i] records;
ALLV record the total number of apples, then just find the smallest dp[0][i] + dp[1][all-i] just fine!
The code is as follows:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <map >using namespace Std;typedef long long ll;const int N = 1e5+10;const ll INF = 1e18;ll L, N, K, inf;ll dp[2][n];ll allv; struct Node{ll D, v;bool operator < (const node &RHS) Const{return D < RHS.D;}} Tree[n];int Main () {int t;scanf ("%d", &t), while (t--) {allv = 0;scanf ("%i64d%i64d%i64d", &l, &n, &k); for ( int i = 0; I < n; i++) {scanf ("%i64d%i64d", &TREE[I].D, &TREE[I].V); ALLV + = TREE[I].V;} Sort (tree, tree + n); int cnt = 1;for (int i = 0; i < n; i++) {for (int j = 0; J < tree[i].v; J + +) {int tmp = 0, dis = l; if (cnt > k) {tmp = Cnt-k;} if (2 * tree[i].d < L) dis = 2 * tree[i].d;dp[0][cnt] = dp[0][tmp] + dis;cnt + +;}} CNT = 1;for (int i = n-1; I >= 0; i--) {for (int j = 0; J < tree[i].v; J + +) {int tmp = 0, dis = l;if (cnt > k) {tmp = C Nt-k;} if ((2 * l-2 * TREE[I].D) < L) dis = 2*l-2 * tree[i].d;dp[1][cnt] = dp[1][tmp] + dis;cnt + +;}} ll ans = inf;for (int i = 0; I <= allv; i++) {ans = min (ans, dp[0][i] + dp[1][allv-i]);} printf ("%i64d\n", ans);} 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 (DP)