【23.58%】【code forces 321E】Ciel and Gondolas

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;
}

转载于:https://www.cnblogs.com/AWCXV/p/7632211.html

### Codeforces Problem 1130C 解析 用户提到的是 **Codeforces Problem 742B** 的相关内容,而问题是希望找到关于 **Problem 1130C** 的解答或解释。以下是针对 **Problem 1130C** 的解析。 #### 题目概述 在 **Codeforces Problem 1130C (Array Beauty)** 中,给定一个数组 `a` 和整数 `k`,定义子序列的美丽值为该子序列中的最小差值。目标是从数组中选取长度至少为 `k` 的子序列,使得其美丽值最大化,并返回这个最大化的美丽值。 --- #### 关键概念算法思路 为了求解此问题,可以采用二分法结合滑动窗口技术来高效解决问题: 1. **二分搜索范围**: 子序列的美丽值可能的最大值是数组中相邻两个元素之间的最小差值,因此可以通过二分搜索的方式,在 `[0, max_diff]` 范围内寻找满足条件的最大美丽值[^5]。 2. **验证函数设计**: 对于每一个候选美丽值 `mid`,通过滑动窗口检查是否存在一个子序列,其中任意两元素之差均不大于 `mid` 并且长度不小于 `k`。如果存在,则说明当前美丽值可行;否则不可行[^6]。 3. **实现细节**: - 使用双指针维护滑动窗口。 - 记录窗口内的元素数量以及它们之间是否满足美丽值约束。 --- #### 实现代码 以下是一个基于 Python 的解决方案: ```python def can_form_subsequence(a, k, mid): count = 0 last = float('-inf') for num in a: if num >= last + mid: count += 1 last = num if count >= k: return True return False def array_beauty(n, k, a): low, high = 0, max(a) - min(a) result = 0 while low <= high: mid = (low + high) // 2 if can_form_subsequence(sorted(a), k, mid): result = mid low = mid + 1 else: high = mid - 1 return result # 输入处理 n, k = map(int, input().split()) a = list(map(int, input().split())) print(array_beauty(n, k, a)) ``` 上述代码实现了二分查找逻辑并配合辅助函数完成验证操作[^7]。 --- #### 测试样例分析 对于输入数据: ``` Input: 5 3 1 3 2 4 5 Output: 2 ``` 程序会按照如下流程执行: - 排序后的数组为 `[1, 2, 3, 4, 5]`。 - 初始二分区间为 `[0, 4]`。 - 经过多次迭代最终得出结果为 `2`,即最长符合条件的子序列美丽值。 --- #### 时间复杂度空间复杂度 - **时间复杂度**: O(N log M),其中 N 是数组大小,M 是数组中最大值减去最小值的结果。 - **空间复杂度**: O(1),除了存储原始数组外无需额外的空间开销[^8]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值