time limit per test4 seconds

memory limit per test512 megabytes

inputstandard input

outputstandard output

Fox Ciel is in the Amusement Park. And now she is in a queue in front of the Ferris wheel. There are n people (or foxes more precisely) in the queue: we use first people to refer one at the head of the queue, and n-th people to refer the last one in the queue.

There will be k gondolas, and the way we allocate gondolas looks like this:

When the first gondolas come, the q1 people in head of the queue go into the gondolas.

Then when the second gondolas come, the q2 people in head of the remain queue go into the gondolas.

The remain qk people go into the last (k-th) gondolas.

Note that q1, q2, …, qk must be positive. You can get from the statement that and qi > 0.

You know, people don’t want to stay with strangers in the gondolas, so your task is to find an optimal allocation way (that is find an optimal sequence q) to make people happy. For every pair of people i and j, there exists a value uij denotes a level of unfamiliar. You can assume uij = uji for all i, j (1 ≤ i, j ≤ n) and uii = 0 for all i (1 ≤ i ≤ n). Then an unfamiliar value of a gondolas is the sum of the levels of unfamiliar between any pair of people that is into the gondolas.

A total unfamiliar value is the sum of unfamiliar values for all gondolas. Help Fox Ciel to find the minimal possible total unfamiliar value for some optimal allocation.

Input

The first line contains two integers n and k (1 ≤ n ≤ 4000 and 1 ≤ k ≤ min(n, 800)) — the number of people in the queue and the number of gondolas. Each of the following n lines contains n integers — matrix u, (0 ≤ uij ≤ 9, uij = uji and uii = 0).

Please, use fast input methods (for example, please use BufferedReader instead of Scanner for Java).

Output

Print an integer — the minimal possible total unfamiliar value.

Examples

input

5 2

0 0 1 1 1

0 0 1 1 1

1 1 0 0 0

1 1 0 0 0

1 1 0 0 0

output

0

input

8 3

0 1 1 1 1 1 1 1

1 0 1 1 1 1 1 1

1 1 0 1 1 1 1 1

1 1 1 0 1 1 1 1

1 1 1 1 0 1 1 1

1 1 1 1 1 0 1 1

1 1 1 1 1 1 0 1

1 1 1 1 1 1 1 0

output

7

input

3 2

0 2 0

2 0 3

0 3 0

output

2

Note

In the first example, we can allocate people like this: {1, 2} goes into a gondolas, {3, 4, 5} goes into another gondolas.

In the second example, an optimal solution is : {1, 2, 3} | {4, 5, 6} | {7, 8}.

题解

DP;

设f[i][j]表示前i艘船。装下j个人的最小不友好值。

f[i][j] = min(f[i-1][k]+w[k+1][j]);

其中w[i][j]表示从把第i个人到第j个人放在同一艘船上增加的不友好值。

这个不友好值可以用一个类似”矩阵前缀和”的东西弄出来;具体的看代码;

这里主要是f[i][j]这个转移的优化方法;

用到了四边形不等式;

想看证明的话转到这个地址:

http://www.cnblogs.com/vongang/archive/2013/01/21/2869315.html

大概就是说设s[i][j]为f[i][j]这个状态转移所需要的决策量。

如果满足BALABALBA就有s[i-1][j] < s[i][j] < s[i][j+1];

用这个就能把n^3的复杂度降低到n^2;

如果不用getchar输入会T

代码

#include <cstdio>
#include <cctype>
#include <cstring> const int MAXN = 4010;
const int MAXM = 810; int n, m,a[MAXN][MAXN],b[MAXN][MAXN],w[MAXN][MAXN];
int f[MAXM][MAXN];
int s[MAXM][MAXN]; void input(int &num)
{
num = 0;
char c;
do
{
c = getchar();
} while (!isdigit(c));
while (isdigit(c))
{
num = num * 10 + c - '0';
c = getchar();
}
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input(n); input(m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
input(a[i][j]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)//矩阵前缀和
a[i][j] = a[i][j] + a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++)//后面加上重复减去的部分。除2就是花费
w[i][j] = (a[j][j] - a[i - 1][j] - a[j][i - 1] + a[i - 1][i - 1]) / 2;
memset(f, 127 / 3, sizeof(f));
for (int i = 1; i <= n; i++)
f[1][i] = w[1][i], s[1][n] = 0;
for (int i = 2; i <= m; i++)
{
s[i][n + 1] = n;
for (int j = n; j >= i; j--)
{
for (int k = s[i - 1][j]; k <= s[i][j + 1]; k++)
if (f[i][j] > f[i - 1][k] + w[k + 1][j])
f[i][j] = f[i - 1][k] + w[k + 1][j], s[i][j] = k;//更改决策量。
}
}
printf("%d\n", f[m][n]);
return 0;
}

【23.58%】【code forces 321E】Ciel and Gondolas的更多相关文章

  1. 【machine learning通俗讲解code逐行注释】之线性回归实现

    现在机器学习算法在分类.回归.数据挖掘等问题上运用的十分广泛,对于初学者来说,可能一听到'算法'或其他的专属名词都感觉高深莫测,以致很多人望而却步,这让很多人在处理很多问题上失去了一个很有用的工具.机 ...

  2. CodeForces - 321E:Ciel and Gondolas (四边形不等式优化DP)

    题意:N个人排成一行,分成K组,要求每组的不和谐值之和最小. 思路:开始以为是斜率优化DP,但是每个区间的值其实已经知道了,即是没有和下标有关的未知数了,所以没必要用斜率. 四边形优化. dp[i][ ...

  3. 【极力分享】[C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例【转载自https://segmentfault.com/a/1190000004152660】

      [C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例 本文我们来学习一下在Entity Framework中使用Cont ...

  4. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...

  5. 【BZOJ3052】【UOJ#58】【WC2013】糖果公园(树上莫队)

    [BZOJ3052][UOJ#58][WC2013]糖果公园(树上莫队) 题面 UOJ 洛谷 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引 ...

  6. 剑指Offer:二叉树打印成多行【23】

    剑指Offer:二叉树打印成多行[23] 题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 题目分析 Java题解 package tree; import java.uti ...

  7. 剑指Offer:链表中环的入口节点【23】

    剑指Offer:链表中环的入口节点[23] 题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目分析 第一步确定链表中是否包含环,怎么确定呢?我们定义两个指针橙和 ...

  8. 【剑指Offer学习】【全部面试题汇总】

    剑指Offer学习 剑指Offer这本书已经学习完了.从中也学习到了不少的东西,如今做一个总的文件夹.供自已和大家一起參考.学如逆水行舟.不进则退.仅仅有不断地学习才干跟上时候.跟得上技术的潮流! 全 ...

  9. 【OCP、OCM、高可用等】小麦苗课堂网络班招生简章(从入门到专家)--课程大纲

    [OCP.OCM.高可用等]小麦苗课堂网络班招生简章(从入门到专家)--课程大纲 小麦苗信息 我的个人信息 网名:小麦苗 QQ:646634621 QQ群:618766405 我的博客:http:// ...

随机推荐

  1. 内存卡(TF或其它)的标准

    内存卡(TF或其它)的标准 来源 https://zh.wikipedia.org/wiki/SDXC 一般的内存卡是SD2.0的规范的,就是 Class10(10M/sec)或低于 Class10 ...

  2. dump var_dump print print_r的区别

    dump var_dump print print_r的区别 一.总结 用dump()来打印就对了 1.echo和print:不能打印复合型和资源型数据: 2.var_dump()和print_r() ...

  3. php数组时按值传递还是按地址传递

    php数组时按值传递还是按地址传递 一.总结 1.数组都是按值:php普通变量和数组的赋值(=)是按值传递,对象的赋值(=)是按址传递 2.对象和按值和按址:对象的clone(用clone关键字)是按 ...

  4. WSDL生成dll

    --生成代理类wsdl /l:cs /n:OAWebService /out:D:OAWebService.cs D:\OAWebService.WSDL--生成dllcsc /out:D:OAWeb ...

  5. 1.20 Python基础知识 - python常用模块-1

    一.time和datetime 1.time模块 1)time.process_time() >>> import time >>> time.process_ti ...

  6. Java 大数

    How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. Flask项目之手机端租房网站功能测试(完结)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶注册和登录以及用户退出功能 二丶上传头像功能和修改用户名功能测试 三丶发布房源以及实名认证功能测试 四丶网站房屋搜索功能 ...

  8. ASP.Net中页面传值的几种方式

    开篇概述 对于任何一个初学者来说,页面之间传值可谓是必经之路,却又是他们的难点.其实,对大部分高手来说,未必不是难点. 回想2016年面试的将近300人中,有实习生,有应届毕业生,有1-3年经验的,有 ...

  9. menuconfig_kconfig

    这一节的主要内容: Menuconfig的操作 Kconfig和.config文件 Linux内核配置裁剪实验 linux编译器通过.config文件确认哪些代码编译进内核,哪些被裁减掉 menuco ...

  10. 洛谷 P2025 脑力大人之监听电话

    P2025 脑力大人之监听电话 题目背景 画外音: (声明:不要管前面那个,纯属意外,现已经重新编题,绝对原创) 上次海选,我们选出了参赛者中的20%参加本次比赛,现在我们将进行第二轮的筛选,这次的比 ...