【CodeForces】【321E】Ciel and Gondolas

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$是满足四边形不等式的……那么根据决策单调性直接搞就行了……

 1 //CF 321E
 2 #include<vector>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 #define rep(i,n) for(int i=0;i<n;++i)
 9 #define F(i,j,n) for(int i=j;i<=n;++i)
10 #define D(i,j,n) for(int i=j;i>=n;--i)
11 using namespace std;
12 //#define debug
13 int getint(){
14     int v=0,sign=1; char ch=getchar();
15     while(ch<‘0‘||ch>‘9‘) {if (ch==‘-‘) sign=-1; ch=getchar();}
16     while(ch>=‘0‘&&ch<=‘9‘) {v=v*10+ch-‘0‘; ch=getchar();}
17     return v*sign;
18 }
19 typedef long long LL;
20 const int N=100010,INF=~0u>>2;
21 /*******************tamplate********************/
22 LL u[4001][4001],n,m,w[4001][4001],dp[801][4001];
23 int s[801][4001];
24 int main(){
25 #ifndef ONLINE_JUDGE
26     freopen("input.txt","r",stdin);
27 //    freopen("output.txt","w",stdout);
28 #endif
29     n=getint(); m=getint();
30     F(i,1,n) F(j,1,n) u[i][j]=getint();
31     F(i,1,n) F(j,1,n) u[i][j]+=u[i-1][j]+u[i][j-1]-u[i-1][j-1];
32
33     F(i,1,n) F(j,i+1,n) w[i][j]=u[j][j]-u[i-1][j]-u[j][i-1]+u[i-1][i-1];
34
35     #ifdef debug
36     F(i,1,n) {F(j,1,n) printf("%3d",w[i][j]);puts("");}
37     #endif
38     F(i,1,m) F(j,1,n) dp[i][j]=INF;
39     F(i,1,n){
40         dp[1][i]=w[1][i];
41         s[1][i]=0;
42     }
43     F(i,2,m){
44         s[i][n+1]=n;
45         D(j,n,i)
46             F(k,s[i-1][j],s[i][j+1])
47                 if (dp[i-1][k]+w[k+1][j]<dp[i][j]){
48                     s[i][j]=k;
49                     dp[i][j]=dp[i-1][k]+w[k+1][j];
50                 }
51     }
52     printf("%I64d\n",dp[m][n]/2);
53     return 0;
54 }

时间: 2024-12-29 11:11:32

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

【codeforces #299(div 1)】ABC题解

A. Tavas and Karafs time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Karafs is some kind of vegetable in shape of an 1?×?h rectangle. Tavaspolis people love Karafs and they use Karafs in almost

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

C. Sasha and Array 题目大意&题目链接: http://codeforces.com/problemset/problem/718/C 长度为n的正整数数列,有m次操作,$opt==1$时,对$[L,R]$全部加x,$opt==2$时,对$[L,R]$求$\sum_{i=L}^{R}Fibonacc(a_{i})$. 题解: 线段树+矩阵快速幂. 在每个线段树存一个转移矩阵,然后YY即可. 代码: 1 #include<cstdio> 2 #include<cs

【codeforces #283(div 1)】ABC题解

A. Removing Columns time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given an n?×?m rectangular table consisting of lower case English letters. In one operation you can completely r

【codeforces #284(div 1)】ABC题解

A. Crazy Town time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix?+?biy?+?ci?=?0, where 

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

A. Treasure time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s writte

【codeforces #292(div 1)】ABC题解

A. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Drazil is playing a math game with Varda. Let's define  for positive integer x as a product of factorials of its dig

【codeforces #296(div 1)】ABD题解

A. Glass Carving time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectan

【codeforces #278(div 1)】ABCD题解

A. Fight the Monster time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang and the monster each hav

【codeforces #285(div 1)】

打完这场,从紫名回到蓝名了 A. Misha and Forest time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Mish

【codeforces #286(div 2)】ABCD题解

这次rank23~又回到紫名啦~ A.枚举插入的位置和插入的字符,暴力判断即可. #include <iostream> #include <cmath> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <string> using namespace std; string S; int s[20]