UVA 10688:the Poor Giant (interval dp)

Source: Internet
Author: User
Tags cas min

Topic Link: uva-10688

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=514&page=show_ problem&problem=1629

Meaning

There are n apples, and a number k, the weight of the first Apple is K+i (1<=i<=n). Only one of the apples is known to be sweet,

All that is lighter than it is bitter, heavier than it is sour.

In order to find the sweet apple, it is necessary to eat it one by one, and eat and bite the apple must eat it, no matter whether the apple is bitter or sour.

We can choose the best strategy, in order to find the sweet apples to eat the least total weight.

Assuming n=4, k=0, then the weight of 4 apples is 1,2,3,4, let's say you eat #2个苹果,

If the # is sweet, then if you eat 2 is sour, then you can determine that 1 is sweet, a total of eating weight =2

If the =2 is sweet, then the weight of the food

If the #3 is sweet, then 2 is sour, it can be speculated that the sweet one in (3,4), then eat 3, you can determine which is sweet, eat weight =2+3=5

If the #4 is sweet, then the solution is the same as the above, to eat the weight =5

In fact, it is similar to the Second Division method.

Total weight = 2+2+5+5 = 14

(The following is the correct description of the original question.)

But this is not the best solution, because there can be less total weight.

For example, eat First,

If the # # is sweet, it costs 1

If it is sweet, then choose to eat #3, no matter what #3 taste, can speculate the taste of the #4, and then spend 1+3

If the #3 is sweet, the second choice eats #3, altogether spends 1+3 = 4

If the #5 is sweet, the solution is the same as above, costing 1+3 = 4

Total 1+4+4+4 = 13, this scheme is better.

Given N and K, what is the minimum total weight to eat?

Ideas

The obvious interval DP, but because the topic description has the mistake place (also blames oneself not to look carefully), the result has not understood the question for a long time.

F (I, j) represents the total weight under the best scheme of the first I to J.

For f (i, j), you can select one of the mid i~j to eat first, and then determine the interval between the left and right intervals of mid (I, mid-1) and (Mid+1, J).

If you eat mid first, then each apple in the left and right range will increase the weight of the first mid Apple, so:

F (i, j) = min{f (i, mid-1) +f (mid+1, J) + weight[mid]* (j-i+1) | i<=mid<=j}

Code

/**===================================================== * This are a solution for ACM/ICPC problem * * @source: uva-1068 8 The Poor Giant * @description: Interval DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: ZENGSHUANGDE@GM
ail.com * Copyright (C) 2013/09/13 12:23 All rights reserved. *======================================================*/#include <iostream> #include <cstdio> #
  
Include <algorithm> #include <cmath> #include <cstring> #define MP make_pair using namespace std;
typedef pair<int, int >PII;
typedef long long Int64;
Const double PI = ACOs (-1.0);
const int INF = 0X3F3F3F3F;
  
const int MAXN = 610;
int F[MAXN][MAXN];
int VAL[MAXN];
  
  
int n, K;
  
int main () {int ncase, Cas=1 scanf ("%d", &ncase);
  
while (ncase--) {scanf ("%d%d", &n, &k);
  
for (int i = 1; I <= n; ++i) Val[i] = k + i;
  
memset (f, 0, sizeof (f)); for (int d = 2; d <= N; ++d) {for (int l = 1; l + d-1 <= N; ++l) {intR = L + d-1;
int& ans = f[l][r] = INF;
for (int mid = L; mid <= R ++mid) {ans = min (ans, f[l][mid-1] + val[mid] * d + f[mid+1][r]);}
printf ("Case%d:%d\n", cas++, F[1][n]);
return 0; }

This column more highlights: http://www.bianceng.cn/Programming/sjjg/

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.