A14067 | Minimum Grid Path
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Let's say you are standing on the $XY$ -plane at point $(0, 0)$ and you want to reach point $(n, n)$ .
You can move only in two directions:
- to the right, i. e. horizontally and in the direction that increase your $x$ coordinate,
- or up, i. e. vertically and in the direction that increase your $y$ coordinate.
In other words, your path will have the following structure:
- initially, you choose to go to the right or up;
- then you go some positive integer distance in the chosen direction (distances can be chosen independently);
- after that you change your direction (from right to up, or from up to right) and repeat the process.
You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes.
As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$ , consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate.
Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$ -th segment.
Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ( $k \le n$ ), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path).
Find the path of the minimum cost and print its cost.
You can move only in two directions:
- to the right, i. e. horizontally and in the direction that increase your $x$ coordinate,
- or up, i. e. vertically and in the direction that increase your $y$ coordinate.
In other words, your path will have the following structure:
- initially, you choose to go to the right or up;
- then you go some positive integer distance in the chosen direction (distances can be chosen independently);
- after that you change your direction (from right to up, or from up to right) and repeat the process.
You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes.
As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$ , consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate.
Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$ -th segment.
Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ( $k \le n$ ), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path).
Find the path of the minimum cost and print its cost.
输入格式
The first line contains the single integer $t$ ( $1 \le t \le 1000$ ) — the number of test cases.
The first line of each test case contains the single integer $n$ ( $2 \le n \le 10^5$ ).
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ( $1 \le c_i \le 10^9$ ) — the costs of each segment.
It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ .
The first line of each test case contains the single integer $n$ ( $2 \le n \le 10^5$ ).
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ( $1 \le c_i \le 10^9$ ) — the costs of each segment.
It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ .
输出格式
For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments.
输入输出样例
输入 #1
3 2 13 88 3 2 3 1 5 4 3 2 1 4
输出 #1
202 13 19
In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$ . The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$ .
In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$ , the second segment of length $3$ and the third segment of length $2$ .
The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$ .
In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$ , the second one — $1$ , the third one — $4$ , the fourth one — $4$ . The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$ .
In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$ , the second segment of length $3$ and the third segment of length $2$ .
The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$ .
In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$ , the second one — $1$ , the third one — $4$ , the fourth one — $4$ . The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted