题目链接:https://vjudge.net/problem/HDU-2829

Lawrence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4678    Accepted Submission(s): 2150

Problem Description
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
 
Source

题意:

给出一个序列,定义一个连续段的值为:连续段内每每两个数的积之和。现要求将序列断开m处,即把序列断成m+1断子序列。使得每段的值之和最小。

题解:

1.设dp[i][j]为:第j个数属于第i段时,值之和的最小值。可得:dp[i][j] = min(dp[i-1][k] + cost[k+1][i]) 其中 i-1<=k<=j-1。

2.对于cost[i][j]:

  可知:cost[1][j] = cost[1][k] + cost[k+1][j] + sum[k]*(sum[j] - sum[k]),

  移项:cost[k+1][j] = cost[1][j] - cost[1][k] -  sum[k]*(sum[j] - sum[k]),因而cost数组可改成一维,sum为前缀和。

  因此:dp[i][j] = min(dp[i-1][k] + cost[j] - cost[k] - sum[k]*(sum[j] - sum[k])) 其中 i-1<=k<=j-1。

  此步转化的目的是要分离 i 和 j,使得在使用斜率优化时能够去掉 i ,变成只与 j 、k有关的式子。

3.斜率优化DP,与此题HDU3480 Division的形式一样。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e3+; int sum[MAXN], cost[MAXN], dp[MAXN][MAXN];
int q[MAXN], head, tail; int getUP(int i, int k1, int k2)
{
return (dp[i-][k1] - cost[k1] + sum[k1]*sum[k1])-
(dp[i-][k2] - cost[k2] + sum[k2]*sum[k2]);
} int getDOWN(int k1, int k2)
{
return sum[k1]-sum[k2];
} int getDP(int i, int j, int k)
{
return dp[i-][k] + cost[j] - cost[k] - sum[k]*(sum[j]-sum[k]);
} int main()
{
int n, m;
while(scanf("%d%d", &n,&m)&&(m||n))
{
sum[] = cost[] = ;
for(int i = ; i<=n; i++)
{
int val;
scanf("%d", &val);
sum[i] = sum[i-] + val;
cost[i] = cost[i-] + val*sum[i-];
} for(int i = ; i<=n; i++) //初始化第一阶段
dp[][i] = cost[i];
for(int i = ; i<=m+; i++) //从分成i-1段的状态转移到分成i段的状态
{
head = tail = ;
q[tail++] = i-; //因为分成i-1段最少需要i-1个数,故备选状态从i-1开始
for(int j = i; j<=n; j++) //因为分成i段最少需要i个数,故从i开始
{
while(head+<tail && getUP(i, q[head+],q[head])<=
getDOWN(q[head+],q[head])*sum[j]) head++;
dp[i][j] = getDP(i, j, q[head]); while(head+<tail && getUP(i, j, q[tail-])*getDOWN(q[tail-],q[tail-])<=
getUP(i, q[tail-],q[tail-])*getDOWN(j,q[tail-])) tail--;
q[tail++] = j;
}
}
printf("%d\n", dp[m+][n]);
}
}

最新文章

  1. jQuery笔记总结
  2. Synergy 鼠标和键盘共享软件
  3. Android,配置Activity为启动Activity(AndroidManifest.xml,application,intent-filter,MAIN,LAUNCHER)
  4. DataBase 之 数据库中的系统表
  5. SpringMVC conflicts with existing, non-compatible bean definition of same name and class 的解决办法
  6. W - Bitset(第二季水)
  7. 玩玩微信公众号Java版之准备
  8. Java语言课程设计——博客作业教学数据分析系统(201521123107 张翔)
  9. Mybatis源码分析之缓存
  10. 从Java继承类的重名static函数浅谈解析调用与分派
  11. Vue.js的简介
  12. GAN-生成对抗网络原理
  13. Python学习(二十八)—— Django模板系统
  14. python系列-2 正则表达式资料
  15. [原]单片机/Stm32教程
  16. ActiveMQ 负载均衡与高可用(转载)
  17. 原生JavaScript技巧大收集
  18. mybatis由浅入深day01_4.7根据用户名称模糊查询用户信息_4.8添加用户((非)自增主键返回)
  19. ubuntu 11.04 old sources.list
  20. css3动画,监控动画执行完毕

热门文章

  1. Centos7源码安装MySQL5.7
  2. html的诸多标签
  3. Codeforces Gym - 101147G The Galactic Olympics
  4. Java创建和解析Json数据方法(三)——json-lib包的使用
  5. AnsiString类型定义的时候可以直接指定代码页,比如950繁体字,936日文
  6. MongoDb 出现配置服务不同步的处理
  7. ArcGIS for Android地图控件的5大常见操作转
  8. 【spring data jpa】repository中使用@Query注解使用hql查询,使用@Param引用参数,报错:For queries with named parameters you need to use provide names for method parameters. Use @Param for query method parameters, or when on
  9. poj-3744-Scout YYF I-矩阵乘法
  10. SPFA 求带负权的单源最短路