Chopsticks Hdu1500

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1500

从大到小搞,到第 i 个人取的对数 不超过 i/3,就保证了对于每一对之前总有比他大的 与他配对。

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <algorithm>

#include <cmath>

#include <stack>

#include <queue>

#include <vector>

#include <map>

#include <string>

#include <iostream>

using
namespace std;

const
int INF=0xfffffff;

int
Min(int
a,int b)

{

    return
a>b? b:a;

}

int
a[5555],dp[5555][1111];

int
main()

{

    int
acase,n,k;

    scanf("%d",&acase);

    while(acase--){

        scanf("%d%d",&k,&n);k+=8;

        for(int
i=n;i>0;i--)

            scanf("%d",&a[i]);

        for(int
i=0;i<=n;i++)

            for(int
j=0;j<=k;j++)

            dp[i][j]=INF;

        for(int
i=0;i<=n;i++)

            dp[i][0]=0;

        for(int
i=1;i<=n;i++)

        for(int
j=1;j<=i/3;j++){

            dp[i][j]=Min(dp[i-1][j],dp[i-2][j-1]+(a[i]-a[i-1])*(a[i]-a[i-1]));

        }

        printf("%d\n",dp[n][k]);

    }

    return
0;

}

  

时间: 2024-10-13 22:49:49

Chopsticks Hdu1500的相关文章

UVA 10271 Chopsticks(线性DP)

In China, people use a pair of chopsticks to get food on the table, but Mr. L is a bit different. He uses a set of three chopsticks – one pair, plus an EXTRA long chopstick to get some big food by piercing it through the food. As you may guess, the l

uva10271 - Chopsticks(递推)

题目:uva10271 - Chopsticks(递推) 题目大意:给出N支筷子,值代表长度,现在要求在这些筷子中选出K对,每对筷子(A,B,C),badness(B- A)^2.要求总的badness最小. 解题思路:选择相邻的筷子来做A和B,这样的badness肯定比较小.但是还要考虑C比较麻烦.最后看了大神的题接,筷子应该从长到短开始考虑,dp[k][j]:前j根筷子凑出了K对,最小的badness. dp[k][j] = Min ((dp[k][j - 1], dp[k - 1][j -

hdu 1500 Chopsticks

http://acm.hdu.edu.cn/showproblem.php?pid=1500 dp[i][j]为第i个人第j个筷子. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 int dp[1011][5011]; 7 int a[5011]; 8 int k,n; 9 int sqr(int x) 10 { 11 return x

hdu 1500 Chopsticks DP

题目链接:HDU - 1500 In China, people use a pair of chopsticks to get food on the table, but Mr. L is a bit different. He uses a set of three chopsticks -- one pair, plus an EXTRA long chopstick to get some big food by piercing it through the food. As you

uva 10271 Chopsticks 【dp】

题目:uva 10271 Chopsticks 题意:从一组数中选出每三个为一组,价值为三个中两个小的差值的平方和,让这个总价值最小. 分析:定义dp[i][j]为从后 i 个中选出 j 对的最小价值. 转移方程:dp[i][j] = min(dp[i-1][j],dp[i+2][j-1]+(a[i]-a[i+1])*(a[i]-a[i+1])) 注意状态转移的条件:就是每组要保留一个最大的值作为第三个值. AC代码: #include <iostream> #include <cstd

UVA10271【Chopsticks】

UVA10271[Chopsticks] Description: In China, people use a pair of chopsticks to get food on the table, but Mr. L is a bit different. He usesa set of three chopsticks – one pair, plus an EXTRA long chopstick to get some big food by piercing it through

UVA-10271 Chopsticks (线性DP)

题目大意:在n个数中,找出k个三元组(a<=b<=c),求最小的(a-b)*(a-b)之和. 题目分析:将所有数从大到小排序,定义dp(i,j)表示前 i 个数中找出 j 个三元组时的最小和,则状态转移方程为dp(i,j)=min(dp(i-1,j),dp(i-2,j-1)),第二种决策是在前i-1个数构成j-1组三元组时必须还要有剩余的数的前提下才能做出.这道题和“搬寝室”和“筷子”类似,同样要填表求解并且注意边界. 代码如下: # include<iostream> # inc

Chopsticks

题意: n个数3个相邻是一组,求选k组使得,各组组内较小的两个数的差之和最小. 分析: 对于每个数选或不选的问题,dp[i][j]表前i个数选了j组得到的最小和. dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+差)选或不选,数应该降序排列. #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include &

【HDOJ】1500 Chopsticks

DP. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <iostream> 6 using namespace std; 7 8 #define MAXN 5005 9 #define MAXK 1005 10 #define INF 0x3f3f3f3f 11 int a[MAXN]; 12 int