T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in the Arabian theater and led a group of Arab nationals in guerilla strikes against the Ottoman Empire. His primary targets were the railroads. A highly fictionalized version of his exploits was presented in the blockbuster movie, "Lawrence of Arabia".

You are to write a program to help Lawrence figure out how to best use his limited resources. You have some information from British Intelligence. First, the rail line is completely linear---there are no branches, no spurs. Next, British Intelligence has assigned a Strategic Importance to each depot---an integer from 1 to 100. A depot is of no use on its own, it only has value if it is connected to other depots. The Strategic Value of the entire railroad is calculated by adding up the products of the Strategic Values for every pair of depots that are connected, directly or indirectly, by the rail line. Consider this railroad:

Its Strategic Value is 4*5 + 4*1 + 4*2 + 5*1 + 5*2 + 1*2 = 49.

Now, suppose that Lawrence only has enough resources for one attack. He cannot attack the depots themselves---they are too well defended. He must attack the rail line between depots, in the middle of the desert. Consider what would happen if Lawrence attacked this rail line right in the middle:

The Strategic Value of the remaining railroad is 4*5 + 1*2 = 22. But, suppose Lawrence attacks between the 4 and 5 depots:

The Strategic Value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17. This is Lawrence's best option.

Given a description of a railroad and the number of attacks that Lawrence can perform, figure out the smallest Strategic Value that he can achieve for that railroad.

 
Input
There will be several data sets. Each data set will begin with a line with two integers, n and m. n is the number of depots on the railroad (1≤n≤1000), and m is the number of attacks Lawrence has resources for (0≤m<n). On the next line will be n integers, each from 1 to 100, indicating the Strategic Value of each depot in order. End of input will be marked by a line with n=0 and m=0, which should not be processed.
 
Output
For each data set, output a single integer, indicating the smallest Strategic Value for the railroad that Lawrence can achieve with his attacks. Output each integer in its own line.
 
Sample Input
4 1
4 5 1 2
4 2
4 5 1 2
0 0
 
Sample Output
17
2
 
题意:n(1<=n<=1000)个数,将其分成m + 1 (0 <= m < n)组,要求每组数必须是连续的而且要求得到的价值最小。
一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0
思路:可以把题目理解为整数划分类型的题目,关键是打表发现可以用四边形不等式优化
dp[i][j] 前i个数 分成j组  dp[i][j]=min(dp[k][j-1]+(d[i]-(sum[i]-sum[k])*sum[k]-d[k]); d[]表示前缀的任意两点的权值和   sum[]为前缀和
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int a[];
int sum[];
int d[];
int dp[][]; //前i个点分成j组
int s[][];
int main(){
ios::sync_with_stdio(false);
int n,m;
while(cin>>n>>m){
if(!n&&!m) break;
memset(dp,inf,sizeof(dp));
for(int i=;i<=n;i++)
cin>>a[i],sum[i]=sum[i-]+a[i];
for(int i=;i<=n;i++){
d[i]=a[i]*sum[i-]+d[i-];
}
for(int i=;i<=n;i++){
dp[i][]=d[i];
s[i][]=;
}
for(int j=;j<=m+;j++){
s[n+][j]=n;
for(int i=n;i>=j;i--){
for(int k=s[i][j-];k<=s[i+][j];k++){
if(dp[i][j]>dp[k][j-]+d[i]-(sum[i]-sum[k])*sum[k]-d[k]){
dp[i][j]=dp[k][j-]+d[i]-(sum[i]-sum[k])*sum[k]-d[k];
s[i][j]=k;
}
}
}
}
cout<<dp[n][m+]<<endl;
}
return ;
}

最新文章

  1. ASP.NET Core管道深度剖析(3):管道是如何处理HTTP请求的?
  2. Eclipse&#183;如何关联Git库文件和添加JUint库
  3. JavaScript笔记:DOM基础
  4. Python之几个技巧特点
  5. 搭建高性能计算环境(四)、应用软件的安装之VASP
  6. Servlet+Tomcat 界面登录
  7. SpringTest 使用说明 -构建无污染纯绿色事务测试框架 (记录用)
  8. 误删除了Oracle的dbf文件后的解决方法
  9. ajax post data 获取不到数据,注意 content-type的设置
  10. 从头开始-04.C语言中流程控制
  11. hbase1.1.4集群搭建
  12. Docker:macvlan实现容器跨主机通信 [十四]
  13. 北大poj- 1012
  14. HiveServer2的WEB UI界面
  15. mig_7series_v4_0_data_gen_chk
  16. Python建立多线程任务并获取每个线程返回值
  17. Derek解读Bytom源码-protobuf生成比原核心代码
  18. e616. Determining If a Focus Lost Is Temporary or Permanent
  19. C# 关于out和ref的问题
  20. git for c#, commit本地,pushserver

热门文章

  1. 想要在launcher中模拟按home键。
  2. 安卓开发:UI组件-布局管理器和文本显示
  3. Windows应急响应常识
  4. MongoDB 创建索引的语法
  5. ASP.NET中弹出消息框的几种常见方法
  6. Windows10系统无法更新
  7. LVS负载均衡基础介绍及NET、DR模式配置
  8. HTML,CSS---问题记录
  9. There Are Now 3 Apache Spark APIs. Here’s How to Choose the Right One
  10. jquery.amaran jquery提示类使用