[Codeforces 1579G] Minimal Coverage | dp最小区间覆盖

简介: 题意:给出n个线段,以及一个无限大的坐标轴,第一个线段以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.


Input


The first line contains an integer t ( 1 ≤ t ≤ 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 ≤ n ≤ 1 0 4 )  — the number of segments. The second line of the description contains n space-separated integers a i ( 1 ≤ a i ≤ 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 .


Output


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.


Example


inputCopy


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


outputCopy


3
3
9
9
7
8


Note


In the third sample test case the segments should be arranged as follows: [0,6]→[4,6]→[4,7]→[−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]→[−2,6]→[−2,2]→[2,7]. The union of these segments also occupies the area [−2,7] and has the length of 9.


题意:


给出n个线段,以及一个无限大的坐标轴,第一个线段以0为起点进行放置,后面的线段必须以前一个的终点为起点放置,这就有两种方式,向左向右

用memset会被卡TLE,所以说尽量还是手动f o r forfor来进行初始化

dp[i][j] 是前i 个线段以轴上一点j 为终点的覆盖长度大小

因为可能出现直接向左进行放的情况,所以说左边的点可能会出现负数,这里可以进行偏移一点

枚举n nn个线段,每个线段的长度为a [ i ]

然后枚举终点坐标j{

向左放置:

if(j<=a[i]) dp[i][0] = min(dp[i][0],dp[i-1][j]+a[i]-j);

else dp[i][j-a[i]] = min(dp[i][j-a[i]],dp[i-1][j]

向右放置:

dp[i][j+a[i]] = min( dp[i][j+a[i]],

max(dp[i-1][j],a[i]+j)

)

}

int n;
int a[10007];
int dp[10007][2001];
int main() {
  int _ = read;
  while (_--) {
    n = read;
    for (int i = 1; i <= n; i++) a[i] = read;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= 2000; j++) dp[i][j] = 0x3f3f3f3f;
    }
    dp[0][0] = 0LL;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= 2000; j++) {
        if (j <= a[i])
          dp[i][0] = min(dp[i][0], dp[i - 1][j] + a[i] - j);
        else
          dp[i][j - a[i]] = min(dp[i][j - a[i]], dp[i - 1][j]);
        dp[i][j + a[i]] = min(dp[i][j + a[i]], max(dp[i - 1][j], a[i] + j));
      }
    }
    int ans = 0x3f3f3f3f;
    for (int i = 0; i <= 2000; i++) {
      ans = min(ans, dp[n][i]);
    }
    printf("%d\n", ans);
  }
  return 0;
}


文章知识点与官方知识档案匹配,可进一步学习相关知识

算法技能树leetcode-动态规划22-括号生成8216 人正在系统学习中

目录
相关文章
|
Java API 开发工具
如何用阿里云 oss 下载文件
阿里云对象存储服务(OSS)提供了多种方式下载文件,以下讲解下各种方式的下载方法
9880 2
|
12月前
|
XML 编解码 JavaScript
DOM(文档对象模型)和 BOM(浏览器对象模型)
【10月更文挑战第19天】在前端开发中,理解 DOM(文档对象模型)和 BOM(浏览器对象模型)是至关重要的。它们是 Web 开发的基础,为我们提供了与网页文档和浏览器进行交互的能力。
1330 62
|
9月前
|
JavaScript 前端开发 测试技术
盘点原生JavaScript中直接触发事件的方式
本文全面探讨了原生JavaScript中触发事件的多种方式,包括`dispatchEvent`、`Event`构造函数、`CustomEvent`构造器、直接调用事件处理器以及过时的`createEvent`和`initEvent`方法。通过技术案例分析,如模拟点击事件、派发自定义数据加载事件和实现提示框系统,帮助开发者掌握这些方法在实际开发中的应用,提升灵活性与兼容性。
306 3
|
10月前
|
运维 Kubernetes 调度
阿里云容器服务 ACK One 分布式云容器企业落地实践
阿里云容器服务ACK提供强大的产品能力,支持弹性、调度、可观测、成本治理和安全合规。针对拥有IDC或三方资源的企业,ACK One分布式云容器平台能够有效解决资源管理、多云多集群管理及边缘计算等挑战,实现云上云下统一管理,提升业务效率与稳定性。
|
10月前
|
Web App开发 大数据 应用服务中间件
什么是 HTTP Range请求(范围请求)
HTTP Range 请求是一种非常有用的 HTTP 功能,允许客户端请求资源的特定部分,从而提高传输效率和用户体验。通过合理使用 Range 请求,可以实现断点续传、视频流播放和按需加载等功能。了解并掌握 HTTP Range 请求的工作原理和应用场景,对开发高效的网络应用至关重要。
1127 16
|
人工智能 BI
区间问题之区间覆盖(看一遍就会系列)
区间问题之区间覆盖(看一遍就会系列)
|
安全 Java 测试技术
【Java】已解决Java中的java.util.NoSuchElementException异常
【Java】已解决Java中的java.util.NoSuchElementException异常
928 1
|
SQL 关系型数据库 MySQL
[已解决]com.mysql.cj.jdbc.exceptions. PacketTooBigException: Packet for query is too large (3,456,888
[已解决]com.mysql.cj.jdbc.exceptions. PacketTooBigException: Packet for query is too large (3,456,888
332 0
|
算法 程序员 分布式数据库
分布式一致性必备:一文读懂Raft算法
Raft算法是一种用于分布式系统中复制日志一致性管理的算法。它通过选举领导者来协调日志复制,确保所有节点数据一致。算法包括心跳机制、选举过程、日志复制和一致性保证。当领导者失效时,节点会重新选举,保证高可用性。Raft易于理解和实现,提供强一致性,常用于分布式数据库和协调服务。作者小米分享了相关知识,鼓励对分布式系统感兴趣的读者进一步探索。
2600 1
vue3 键盘事件 回车发送消息,ctrl+回车 内容换行
const textarea = textInput.value.textarea; //获取输入框元素
663 3