SDUT-2169-Sequence(DP)
2016-05-25 19:13
330 查看
题目链接:http://www.sdutacm.org/sdutoj/problem.php?action=showproblem&problemid=2169
Problem Description
Given an integer number sequence A of length N (1<=N<=1000), we define f(i,j)=(A[i]+A[i+1]+...+A[j])^2 (i<=j). Now you can split the sequence into exactly M (1<=M<= N) succesive parts, and the cost of a part from A[i] to A[j] is f(i,j). The totle cost is the
sum of the cost of each part. Please split the sequence with the minimal cost.
Input
At the first of the input comes an integer t indicates the number of cases to follow. Every case starts with a line containing N ans M. The following N lines are A[1], A[2]...A
, respectively. 0<=A[i]<=100 for every 1<=i<=N.
Output
For each testcase, output one line containing an integer number denoting the minimal cost of splitting the sequence into exactly M succesive parts.
Sample Input
1
5 2
1 3 2 4 5
Sample Output
117
//#include <bits/stdc++.h>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int Scan()
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')flag=1;
else if(ch>='0'&&ch<='9')res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';
return flag?-res:res;
}
void Out(int a)
{
if(a>9)Out(a/10);
putchar(a%10+'0');
}
#define INF 0x3f3f3f3f
#define LL long long
#define bug cout<<"bug"<<endl
const int MAXM = 2e4+7;
const int MAXN = 1007;
long long dp[MAXN][MAXN];
long long sum[MAXN];
int main()
{
int T,n,m,q,a;
T=Scan();
while(T--)
{
scanf("%d%d",&n,&m);
sum[0]=0;
memset(dp,INF,sizeof(dp));
for(int i=1; i<=n; ++i)
{
scanf("%d",&a);
sum[i]=sum[i-1]+a;
dp[1][i]=sum[i]*sum[i];
}
for(int i=2; i<=m; ++i)
for(int j=i; j<=n-m+i; ++j)
for(int k=i-1; k<=j; ++k)
dp[i][j]=min(dp[i][j],dp[i-1][k-1]+(sum[j]-sum[k-1])*(sum[j]-sum[k-1]));
printf("%lld\n",dp[m]
);
}
return 0;
}
/*
1
5 3
1 3 2 4 5
*/
Problem Description
Given an integer number sequence A of length N (1<=N<=1000), we define f(i,j)=(A[i]+A[i+1]+...+A[j])^2 (i<=j). Now you can split the sequence into exactly M (1<=M<= N) succesive parts, and the cost of a part from A[i] to A[j] is f(i,j). The totle cost is the
sum of the cost of each part. Please split the sequence with the minimal cost.
Input
At the first of the input comes an integer t indicates the number of cases to follow. Every case starts with a line containing N ans M. The following N lines are A[1], A[2]...A
, respectively. 0<=A[i]<=100 for every 1<=i<=N.
Output
For each testcase, output one line containing an integer number denoting the minimal cost of splitting the sequence into exactly M succesive parts.
Sample Input
1
5 2
1 3 2 4 5
Sample Output
117
//#include <bits/stdc++.h>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int Scan()
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')flag=1;
else if(ch>='0'&&ch<='9')res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';
return flag?-res:res;
}
void Out(int a)
{
if(a>9)Out(a/10);
putchar(a%10+'0');
}
#define INF 0x3f3f3f3f
#define LL long long
#define bug cout<<"bug"<<endl
const int MAXM = 2e4+7;
const int MAXN = 1007;
long long dp[MAXN][MAXN];
long long sum[MAXN];
int main()
{
int T,n,m,q,a;
T=Scan();
while(T--)
{
scanf("%d%d",&n,&m);
sum[0]=0;
memset(dp,INF,sizeof(dp));
for(int i=1; i<=n; ++i)
{
scanf("%d",&a);
sum[i]=sum[i-1]+a;
dp[1][i]=sum[i]*sum[i];
}
for(int i=2; i<=m; ++i)
for(int j=i; j<=n-m+i; ++j)
for(int k=i-1; k<=j; ++k)
dp[i][j]=min(dp[i][j],dp[i-1][k-1]+(sum[j]-sum[k-1])*(sum[j]-sum[k-1]));
printf("%lld\n",dp[m]
);
}
return 0;
}
/*
1
5 3
1 3 2 4 5
*/
相关文章推荐
- find,vim的编程设置,SUID,SGID,SBIT,软.硬链接,重定向
- UILable 设置多种字体颜色
- iOS开发UI篇—核心动画(转场动画和组动画)
- 对AbstractQueuedSychronizer的粗略理解
- Java中String、StringBuffer和StringBuilder的区别和堆栈内存分配
- Helper2416开发板移植minigui3.0.12之二:移植到开发板
- Vue.js-----轻量高效的MVVM框架(九、组件利用Props传递数据)
- IOS音频1:之采用四种方式播放音频文件(一)AudioToolbox AVFoundation OpenAL AUDIO QUEUE
- Arduino入门(二)
- iOS开发UI篇—iPad开发中得modal介绍
- Codeforces Round #353 (Div. 2) A. Infinite Sequence
- serialVersionUID的作用
- Material Design UI Widgets ——CoordinatorLayout (协调者布局)
- 一个简单的UI动态查询报表例子
- priority_queue&queue使用实例
- MYSQL CLUSTER questions
- #UITableView滑动卡顿优化
- UITextView动态调整高度
- iOS开发之高级视图—— UITableView(五)表视图索引
- NSOperationQueue