Codeforces 321E Ciel and Gondolas

传送门:http://codeforces.com/problemset/problem/321/E

【题解】

首先有一个$O(n^2k)$的dp。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + 10, N = 4000 + 5;
const int mod = 1e9+7;
const ll inf = 1e17;

inline int getint() {
    int x = 0; char ch = getchar();
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch)) x = (x<<3) + (x<<1) + ch - ‘0‘, ch = getchar();
    return x;
}

int n, K, a[N][N], d[N][N];
ll f[805][N];

int main() {
    n = getint(), K = getint();
    for (int i=1; i<=n; ++i)
        for (int j=1; j<=n; ++j)
            a[i][j] = a[i-1][j] + a[i][j-1] - a[i-1][j-1] + getint();
    for (int i=1; i<=n; ++i) {
        d[i][i] = 0;
        for (int j=i+1; j<=n; ++j)
            d[i][j] = (a[j][j] - a[j][i-1] - a[i-1][j] + a[i-1][i-1])/2;
    }
    for (int i=1; i<=n; ++i) f[0][i] = inf;
    for (int i=1; i<=K; ++i) {
        for (int j=1; j<=n; ++j) {
            f[i][j] = inf;
            for (int k=0; k<j; ++k)
                f[i][j] = min(f[i][j], f[i-1][k] + d[k+1][j]);
        }
    }
    cout << f[K][n];

    return 0;
}

然后发现每层当n向右移动的时候,决策点一定向右移动

(可能可以观察+证明)

然后用整体二分trick就可以了。复杂度$O(Knlogn)$。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + 10, N = 4000 + 5;
const int mod = 1e9+7;
const ll inf = 1e17;

inline int getint() {
    int x = 0; char ch = getchar();
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch)) x = (x<<3) + (x<<1) + ch - ‘0‘, ch = getchar();
    return x;
}

int n, K, a[N][N], d[N][N];
ll f[805][N], *F, *G;

inline void solve(int l, int r, int al, int ar) {
    if(l > r) return ;
    int mid = l+r>>1; ll mx = inf, t; int pos = 0;
    for (int j=al; j<=ar && j<mid; ++j)
        if((t = G[j] + d[j+1][mid]) < mx) mx = t, pos = j;
    F[mid] = mx;
    solve(l, mid-1, al, pos);
    solve(mid+1, r, pos, ar);
}

int main() {
    n = getint(), K = getint();
    for (int i=1; i<=n; ++i)
        for (int j=1; j<=n; ++j)
            a[i][j] = a[i-1][j] + a[i][j-1] - a[i-1][j-1] + getint();
    for (int i=1; i<=n; ++i) {
        d[i][i] = 0;
        for (int j=i+1; j<=n; ++j)
            d[i][j] = (a[j][j] - a[j][i-1] - a[i-1][j] + a[i-1][i-1])/2;
    }
    for (int i=1; i<=n; ++i) f[0][i] = inf;
    for (int i=1; i<=K; ++i) {
        F = f[i], G = f[i-1];
        solve(1, n, 0, n);
    }
    cout << f[K][n];

    return 0;
}

时间: 2024-08-24 12:21:11

Codeforces 321E Ciel and Gondolas的相关文章

codeforces 321E Ciel and Gondolas 四边形不等式

题目大意:给定n个人,需要分k次过河,两个人i,j如果同乘一条船就会产生ai,j的代价,求最终代价的最小值 这个玩应显然满足四边形不等式(虽然我并不知道这个不等式是啥 然后就是决策单调(虽然我并不知道为何满足四边形不等式一定决策单调 然后就能分治做辣... 定义Solve(l,r,optl,optr)表示当前在处理区间[l,r],最优决策区间为[optl,optr] 然后我们用区间[optl,min(optr,mid?1)]的f值来更新f[mid],并记录最优决策点pos 那么[l,mid?1]

Codeforces G. Ciel the Commander

题目描述: Ciel the Commander time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected by n?-?1 undire

【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 #includ

CodeForces 321C Ciel the Commander

Ciel the Commander Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 321C64-bit integer IO format: %I64d      Java class name: (Any) Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its na

CodeForces 321A Ciel and Robot(数学模拟)

题目链接:http://codeforces.com/problemset/problem/321/A 题意:在一个二维平面中,開始时在(0,0)点,目标点是(a.b),问能不能通过反复操作题目中的指令,从原点移动到目标点. 分析:如果一次完毕全部的命令后.移动到了(xx,yy),而且从(Xi.Yi)反复操作k次指令到达目标点.则能够列出方程 Xi + k * xx = a && Yi + k * yy = b.然后解出k.推断k是否大于等于0就可以. #include <cstdi

Codeforces 321B Ciel and Duel KM

题目链接 题意: 类似于游戏王的卡牌游戏,每个随从都有嘲讽... 输入n m 下面n行给出对手的随从当前状态和强壮值. 下面m行给出自己的随从的强壮值. 表示自己有m个随从,对手有n个随从.每个随从有一个强壮值. 现在是自己进攻的回合,自己的每个随从可以攻击一次(也可以不攻击) 若对手的随从都死光了,那么可以直接攻击玩家,造成与强壮值相等的伤害. 否则就要先攻击对手的随从. 对手的随从有防御状态(Defense) 和 攻击状态(Attack),每个随从也有一个强壮值. 1.若攻击对手的攻击状态随

Codeforces 321C Ciel the Commander 树分治裸题

题目链接 题意: 给定一棵树,要用字母A-Z 填到每个节点上 字母可以无限使用,但A至多只能用一次 目标:对于任意两个相同字母的节点,他们之间的路径上必须有至少一个节点的字母比他们小 例如:在两个C之间至少要有一个A 或者一个B 问: 输出填涂方案. 树分治即可,最多支持2^25个节点,不会无解. #include <iostream> #include <string> #include <vector> #include <cstring> #inclu

Codeforces 321A Ciel and Robot 枚举答案

题目链接 枚举机器人走的最后一步,用终点坐差后计算周期次数 trick:周期次数要>=0 #include <iostream> #include <string> #include <vector> #include <cstring> #include <cstdio> #include <map> #include <queue> #include <algorithm> #include <

Codeforces 321B Ciel and Duel

题意:对方有N张卡牌,每一张卡牌要么是攻击模式要么是防守模式,你有M张卡牌,都是攻击模式 ,每张卡牌都有一个值,你可以进行一下的任意操 1)如果对方牌都被你消灭了,那么你任选一张没有选过的牌是对方遭受牌值的攻击. 2)可以选择一张没有选过值为X的牌,攻击对方一张攻击模式值为Y 的牌  对对方造成(X-Y)的伤害并把牌消灭 X必须 大于等于Y 3)可以选择一张没有选过值为X的牌,攻击对方一张防守模式值为Y 的牌  对对方不造成伤害,把对方牌消灭. X必须大于Y 解题思路:暴力 + DP? 解题代码