UVA 10271 Chopsticks

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 100000007
const int maxn = 5009;
int T;
int n,k;

int a[maxn];
int dp[maxn][3100];

bool cmp(int x,int y)
{
return x > y;
}

void init()
{
cin>>k>>n;
k = k + 8;
rep(i,1,n) scanf("%d",&a[i]);
sort(a+1,a+1+n,cmp);
Clean(dp,0x3f);
rep(i,0,n) dp[i][0] = 0;
}

int cal( int x , int y )
{
return (x-y)*(x-y);
}

void solve()
{
rep(i,3,n)
{
int uplim = min( k , i / 3 );
rep(j,1,uplim)
{
dp[i][j] = min( dp[i-1][j] , dp[i][j] );
if( j * 3 <= i )
dp[i][j] = min( dp[i][j] , dp[i-2][j-1] + cal( a[i-1] , a[i] ) );
}
}
cout<<dp[n][k]<<endl;
}

int main()
{
cin>>T;
while(T--)
{
init();
solve();
}
return 0;
}



【DP】UVA 10271 Chopsticks

2014-11-30 12:00:44

uva 10271 - Chopsticks

2014-08-04 17:40:01

uva 10271 Chopsticks （DP）

2015-04-08 18:50:10

UVA - 10271 Chopsticks DP

2015-08-31 09:24:26

UVA 10271 Chopsticks（dp）

2015-01-29 17:20:42

UVa 10271 - Chopsticks dp

2013-05-02 19:36:17

UVA 10271 Chopsticks（线性DP)

2015-05-12 22:53:54

UVa 10271 - Chopsticks（DP）

2015-07-26 17:35:41

uva 10271 Chopsticks

2011-11-30 11:16:32

UVA - 10271 Chopsticks 经典DP

2014-11-15 00:46:31