Multi-university Training Contest 2 1004 Delicious Apples (DP)

Source: Internet
Author: User

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 &AMP;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", &AMP;TREE[I].D, &AMP;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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.