G. Minimal Coverage

刷题记录
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≤104) — the number of segments. The second line of the description contains n space-separated integers ai (1≤ai≤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 104.

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.

#include<bits/stdc++.h>
using namespace std;
int dp[10100][1000<<2];//dp[i][j]表示走到第i步时,在j位置处时的最右端点,规定最左端点为0。1000<<2大于1e7。
int t,n,a[10100];
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		for(int i=1;i<=n;i++){
			for(int j=0;j<=3000;j++){
				dp[i][j]=1e7;
			}
		}
		for(int i=a[1];i<=2000;i++){
			dp[1][i]=i;//每一个j的最左端点都为0
		}
		for(int i=2;i<=n;i++){
			for(int j=0;j<=2000;j++){
				if(j>=a[i])//只能向右走最优,例如6 2 3 9为[0,6]→[4,6]→[4,7]→[−2,7].     [4,6]->[4,7]向右走了距离3
				dp[i][j]=max(dp[i-1][j-a[i]],j);
				if(j+a[i]<=2000)//向左走取最优
				dp[i][j]=min(dp[i-1][j+a[i]],dp[i][j]);
			}
		}
		int answer=1e9;
		for(int i=0;i<=2000;i++){
			answer=min(answer,dp[n][i]);
		}//走到第n步时,因为不能确定走到哪,故在[-1000,1000]范围内逐一比较,因为确定了最左端点为0,故范围为[0,2000],且题目要求最小值
		cout<<answer<<endl;
		//cout<<"--------------"<<endl;
	}
	return 0;
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值