CodeForces - 321E Ciel and Gondolas

Discription

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 q1q2, ..., 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 followingn 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}.

 

 

    典型的一题多解啊23333。

    先来写个最基本的dp方程: 设f[i][j] 为将前j个贞鱼 分到 i组中 可能得到的最小代价和是多少,显然 f[i][j] = min{ f[i-1][k] + cost[k+1][j] , k<j},并且直接转移是 n^2 * k 的。

 

解法1:利用该dp的决策单调性进行分治求解。

     还记得 CF 的某道E题叫 yet another .... problem 来着? 就是那个题的翻版。

    考虑到 cost[i][j] 在i一定的时候,函数的值和斜率都关于j单调不减,这个时候多阶段性dp是有决策单调性的,所以我们对于每个i求f[i][]的时候都采用分治,暴力算出 f[i][mid] 的转移点x,于是 j<mid的转移点都<=x,j>mid的转移点都>=x。

     这个算法的理论复杂度是 O(n * k * log(n)) 的,刚刚好卡着过这个题hhhh(不过标算就是这个算法?)

 

解法2:利用四边形不等式优化转移过程。

     这个解法来的更加自然一些。 让我们设 pos[i][j] 为 f[i][j] 是从 f[i-1][pos[i][j]] 转移过来的,那么因为cost[][]满足四边形不等式,(也就是 cost[i-1][j+1] + cost[i][j] > cost[i-1][j] + cost[i][j+1]),所以 f[][] 也是满足四边形不等式的,所以可以得到 pos[i-1][j] <= pos[i][j] <= pos[i][j+1],所以根据这个限制直接dp,随便算一算复杂度(不等号隔开的三块都求个和),发现是O(N^2),也可以过本题。

 

解法3:利用dp的凸优化进行二分

    虽然没有具体想这个做法怎么做,但是显然题目限制的必须分出k组,是可以进行dp凸优化的,通过二分一个每分出就要付出的代价,可以最后得到最优解。。。

(因为我懒得仔细想了所以就这样吧23333)

 

    写了前两种,拍了一下发现拍上了(美滋滋),一交都A了2333

 

解法1:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=4005;
int Q[maxn][maxn],n,m,now,F[maxn],G[maxn],cost[maxn][maxn];
 
inline int read(){
    int x=0; char ch=getchar();
    for(;ch<'0'||ch>'9';ch=getchar());
    for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
    return x;
}
 
void dp(int l,int r,int L,int R){
    if(l>r||L>R) return;
    int mid=l+r>>1,pos=L;
     
    for(int i=min(mid,R+1);i>L;i--){
        now=cost[i][mid];
        if(G[i-1]+now<F[mid]) F[mid]=G[i-1]+now,pos=i-1;
    }
     
    dp(l,mid-1,L,pos);
    dp(mid+1,r,pos,R);
}
 
inline void solve(){
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++) cost[i][j]=cost[i][j-1]+Q[j][j]-Q[j][i-1];
    for(int i=1;i<=n;i++) F[i]=cost[1][i];
     
    for(int i=2;i<=m;i++){
        memcpy(G,F,sizeof(F));
        memset(F,0x3f,sizeof(F));
        dp(i,n,i-1,n-1);
    }
}
 
int main(){
//	freopen("data.in","r",stdin);
//	freopen("ans.out","w",stdout);
	
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++) Q[i][j]=read()+Q[i][j-1];
    solve(),printf("%d\n",F[n]);
     
    /*
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++) printf("%d %d -> %d\n",i,j,cost[i][j]);
    */
    return 0;
}

  解法2:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=4003;
int cost[maxn][maxn],f[803][maxn];
int p[803][maxn],n,k;

inline int read(){
	int x=0; char ch=getchar();
	for(;!isdigit(ch);ch=getchar());
	for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
	return x;
}

inline void solve(){
	memset(f,0x3f,sizeof(f));
	for(int i=1;i<=n;i++) f[1][i]=cost[1][i],p[1][i]=0;
	
	for(int i=2;i<=k;i++){
		p[i][n+1]=n-1;
		for(int j=n,T;j;j--){
			T=p[i][j+1];
		    for(int l=p[i-1][j];l<=T;l++) if(f[i-1][l]+cost[l+1][j]<=f[i][j]){
		    	f[i][j]=f[i-1][l]+cost[l+1][j];
		    	p[i][j]=l;
			}
		}
	}
}

int main(){
//	freopen("data.in","r",stdin);
//	freopen("data.out","w",stdout);
	
	n=read(),k=read();
	for(int i=1;i<=n;i++)
	    for(int j=1;j<=n;j++){
	    	cost[i][j]=read()+cost[i][j-1];
		}
	for(int i=n;i;i--){
		int now=cost[i][i];
	    for(int j=i+1;j<=n;j++) cost[i][j]+=cost[i+1][j]-now;
	    
	    fill(cost[i]+1,cost[i]+i+1,0);
    }
    
    solve();    
	
	printf("%d\n",f[k][n]);
	
	return 0;
}

  

posted @ 2018-05-23 11:02  蒟蒻JHY  阅读(505)  评论(0编辑  收藏  举报