欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1941E.Rudolf and k Bridges
Rudolf and k Bridges
CF1941E · Rudolf and k Bridges
- 难度:1600
- 标签:binary search、data structures、dp、two pointers
- 链接:https://codeforces.com/problemset/problem/1941/E
- 时间限制:2 seconds 内存限制:256 megabytes
- 出现位置:Day25-二分-DP-数据结构综合
英文原题面
Statement
Bernard loves visiting Rudolf, but he is always running late. The problem is that Bernard has to cross the river on a ferry. Rudolf decided to help his friend solve this problem. The river is a grid of rows and columns. The intersection of the -th row and the -th column contains the number — the depth in the corresponding cell. All cells in the first and last columns correspond to the river banks, so the depth for them is . The river may look like this. Rudolf can choose the row and build a bridge over it. In each cell of the row, he can install a support for the bridge. The cost of installing a support in the cell is . Supports must be installed so that the following conditions are met:
- A support must be installed in cell ;
- A support must be installed in cell ;
- The distance between any pair of adjacent supports must be no more than . The distance between supports and is .
Building just one bridge is boring. Therefore, Rudolf decided to build bridges on consecutive rows of the river, that is, to choose some () and independently build a bridge on each of the rows . Help Rudolf minimize the total cost of installing supports.
Input
The first line contains a single integer — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains four integers , , , and (, , ) — the number of rows and columns of the field, the number of bridges, and the maximum distance between supports. Then follow lines, -th line contains positive integers (, ) — the depths of the river cells. It is guaranteed that the sum of for all sets of input data does not exceed .
Output
For each test case, output a single number — the minimum total cost of supports installation.
样例
样例 1
输入:
5
3 11 1 4
0 1 2 3 4 5 4 3 2 1 0
0 1 2 3 2 1 2 3 3 2 0
0 1 2 3 5 5 5 5 5 2 0
4 4 2 1
0 3 3 0
0 2 1 0
0 1 2 0
0 3 3 0
4 5 2 5
0 1 1 1 0
0 2 2 2 0
0 2 1 1 0
0 3 2 1 0
1 8 1 1
0 10 4 8 4 4 2 0
4 5 3 2
0 8 4 4 0
0 3 4 8 0
0 8 1 10 0
0 10 1 5 0
输出:
4
8
4
15
14
样例解释(英文原文)
In the first test case, it is most profitable to build a bridge on the second row. It is not a top view, but side view: gray cells — bridge itself, white cells are empty, black cells — supports, blue cells — water, brown cells — river bottom. In the second test case, it is most profitable to build bridges on the second and third rows. The supports will be placed in cells , , and on the river banks. In the third test case the supports can be placed along the river banks.
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |