DP优化/四边形不等式


  这题……跟邮局那题简直一模一样吧……好水的E题……

  设dp[i][j]表示前 i 艘“gondola”坐了前 j 个人,那么方程即为$dp(i,j)=min\{ dp[i-1][k]+w[k][j] \} (i\leq k\leq j)$

  很明显$w(l,r)=\sum_{i=l}^r \sum_{j=l}^r u(i,j) /2$是满足四边形不等式的……那么根据决策单调性直接搞就行了……

 //CF 321E
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std;
//#define debug
int getint(){
int v=,sign=; char ch=getchar();
while(ch<''||ch>'') {if (ch=='-') sign=-; ch=getchar();}
while(ch>=''&&ch<='') {v=v*+ch-''; ch=getchar();}
return v*sign;
}
typedef long long LL;
const int N=,INF=~0u>>;
/*******************tamplate********************/
LL u[][],n,m,w[][],dp[][];
int s[][];
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
n=getint(); m=getint();
F(i,,n) F(j,,n) u[i][j]=getint();
F(i,,n) F(j,,n) u[i][j]+=u[i-][j]+u[i][j-]-u[i-][j-]; F(i,,n) F(j,i+,n) w[i][j]=u[j][j]-u[i-][j]-u[j][i-]+u[i-][i-]; #ifdef debug
F(i,,n) {F(j,,n) printf("%3d",w[i][j]);puts("");}
#endif
F(i,,m) F(j,,n) dp[i][j]=INF;
F(i,,n){
dp[][i]=w[][i];
s[][i]=;
}
F(i,,m){
s[i][n+]=n;
D(j,n,i)
F(k,s[i-][j],s[i][j+])
if (dp[i-][k]+w[k+][j]<dp[i][j]){
s[i][j]=k;
dp[i][j]=dp[i-][k]+w[k+][j];
}
}
printf("%I64d\n",dp[m][n]/);
return ;
}

【CodeForces】【321E】Ciel and Gondolas的更多相关文章

  1. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  2. 【Codeforces Round #438 C】 Qualification Rounds

    [链接]h在这里写链接 [题意] 给你n个问题,每个人都知道一些问题. 然后让你选择一些问题,使得每个人知道的问题的数量,不超过这些问题的数量的一半. [题解] 想法题. 只要有两个问题. 这两个问题 ...

  3. 【Codeforces Round #438 B】Race Against Time

    [链接]h在这里写链接 [题意] 时针.分钟.秒针走不过去. 问你从t1时刻能不能走到t2时刻 [题解] 看看时针.分钟.秒针的影响就好. 看看是不是在整时的位置就好. 然后看看影响到x不能到y; 然 ...

  4. 【Codeforces Round #438 A】Bark to Unlock

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 枚举它是在连接处,还是就是整个字符串就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc+ ...

  5. 【codeforces 718 C&D】C. Sasha and Array&D. Andrew and Chemistry

    C. Sasha and Array 题目大意&题目链接: http://codeforces.com/problemset/problem/718/C 长度为n的正整数数列,有m次操作,$o ...

  6. 【codeforces contest 1119 F】Niyaz and Small Degrees

    题目 描述 \(n\) 个点的树,每条边有一个边权: 对于一个 \(X\) ,求删去一些边后使得每个点的度数 \(d_i\) 均不超过 \(X\) 的最小代价: 你需要依次输出 \(X=0 \to n ...

  7. 【codeforces #282(div 1)】AB题解

    A. Treasure time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

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

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

  9. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  10. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

随机推荐

  1. 使用DBCP时发生AbstractMethodError异常

    使用DBCP时发生AbstractMethodError异常,错误描述: Exception in thread "main" java.lang.AbstractMethodEr ...

  2. struts2集成javamail发邮件(带附件)实践记录

    一.代码预览 这两天在做struts2上的邮件发送.以前的项目有用到spring,用spring提供的邮件支持类很方便可以完成这个功能,但是现在只用struts2的话,就碰到了一系列的问题. 代码是从 ...

  3. 通过messenger实现activity与service的相互通信

    布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...

  4. UltraEdit中使用正则表达式

    正则表达式 (UltraEdit Syntax): % 匹配行首 - 表明要搜索的字符串一定在行首. $ 匹配行尾 - 表明要搜索的字符串一定在行尾 ? 匹配除换行符外的任一单个字符. * 匹配任意个 ...

  5. c++ 类与函数中static变量初始化问题(转)

    首先static变量只有一次初始化,不管在类中还是在函数中..有这样一个函数: void Foo() { ; // initialize std::cout << a; a++; } 里的 ...

  6. openSUSE13.1无法打开Yast的安装/移除软件管理软件的解决办法·(未解决,临时方法) 收获:有问题,读日志

    看了日志发现错误在于Ruby,新的YaST是基于Ruby的,而我用rvm安了新版本Ruby,日志里这么出错: 2014-08-22 20:20:57 <3> linux-vfpp.site ...

  7. 排序 选择排序&&堆排序

    选择排序&&堆排序 1.选择排序: 介绍:选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始 ...

  8. .NET开源工作流RoadFlow-表单设计-文本框

    点击表单设计器工具栏上的文本框按钮,会弹出文本框属性对话框: 绑定字段:该文本框与表单属性设置中选择的表的某个字段绑定(该文本框中的值将会保存到该字段中). 默认值:该文本框的初始化值. 宽度:文本框 ...

  9. JS对文本框值的判断

    JS判断只能是数字和小数点(摘录自其它资料,在此发表只为个人以后使用查找方便) 1.文本框只能输入数字(不包括小数点) <input onkeyup="this.value=this. ...

  10. hdu 5427 A problem of sorting

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5427 A problem of sorting Description There are many ...