A14467 | Minimal Coverage
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given $n$ lengths of segments that need to be placed on an infinite axis with coordinates.
The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate $0$ . Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start.
The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is $d$ and the "end" of the previous one has the coordinate $x$ , the segment can be placed either on the coordinates $[x-d, x]$ , and then the coordinate of its "end" is $x - d$ , or on the coordinates $[x, x+d]$ , in which case its "end" coordinate is $x + d$ .
The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order.
The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate $0$ . Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start.
The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is $d$ and the "end" of the previous one has the coordinate $x$ , the segment can be placed either on the coordinates $[x-d, x]$ , and then the coordinate of its "end" is $x - d$ , or on the coordinates $[x, x+d]$ , in which case its "end" coordinate is $x + d$ .
The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order.
输入格式
The first line contains an integer $t$ ( $1 \leq t \leq 1000$ ) — the number of test cases.
The next $2t$ lines contain descriptions of the test cases.
The first line of each test case description contains an integer $n$ ( $1 \le n \le 10^4$ ) — the number of segments. The second line of the description contains $n$ space-separated integers $a_i$ ( $1 \le a_i \le 1000$ ) — lengths of the segments in the same order they should be placed on the axis.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$ .
The next $2t$ lines contain descriptions of the test cases.
The first line of each test case description contains an integer $n$ ( $1 \le n \le 10^4$ ) — the number of segments. The second line of the description contains $n$ space-separated integers $a_i$ ( $1 \le a_i \le 1000$ ) — lengths of the segments in the same order they should be placed on the axis.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$ .
输出格式
Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible length of the axis coverage.
输入输出样例
输入 #1
6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6
输出 #1
3 3 9 9 7 8
In the third sample test case the segments should be arranged as follows: $[0, 6] \rightarrow [4, 6] \rightarrow [4, 7] \rightarrow [-2, 7]$ . As you can see, the last segment $[-2, 7]$ covers all the previous ones, and the total length of coverage is $9$ .
In the fourth sample test case the segments should be arranged as $[0, 6] \rightarrow [-2, 6] \rightarrow [-2, 2] \rightarrow [2, 7]$ . The union of these segments also occupies the area $[-2, 7]$ and has the length of $9$ .
In the fourth sample test case the segments should be arranged as $[0, 6] \rightarrow [-2, 6] \rightarrow [-2, 2] \rightarrow [2, 7]$ . The union of these segments also occupies the area $[-2, 7]$ and has the length of $9$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted