Codeforces Round #744 (Div. 3) G. Minimal Coverage

Codeforces Round #744 (Div. 3)

Codeforces

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:

输入:
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

输出:
3
3
9
9
7
8

解释:
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个长度的绳子,后面一个绳子起点只能接在上一个绳子的终点,并且向前延伸,或者向后延伸,问最小放下所有绳子的区间长度。

解题思路1:

二分+动态规划
问最小放下所有绳子的区间长度,且所有绳子长度最长为1000,那么最大的区间不会超过2000,因为可以来回放嘛。
那么我们就可以二分这个长度区间,判断是否满足条件,判断是否满足条件时可以用动态规划来完成。

动态规划,三步走:


1.状态表示:
用dp[i][j]表示第i个绳子是否可以以坐标j结束,可以则为1,不可以则为0。


2.初始化:

  • dp[0][j]=1 j为0~len,第0个绳子没有要求,可以放在满足二分长度len的所有点上

3.状态转移:

  • 往前放:
    dp[i][j-a[i]]=1,如果dp[i-1][j]=1 && j-a[i]>=0
  • 往后放:
    dp[i][j+a[i]]=1,如果dp[i-1][j]=1 && j+a[i]<=len

代码如下:

#include<bits/stdc++.h>

using namespace std;

const int MAXN=1e4+5;
int a[MAXN];
int dp[MAXN][2005];

bool check(int n,int len) {
    for(int i=0;i<=n;++i) {
        for(int j=0;j<=len;++j) {
            if(i==0)
                dp[i][j]=1;
            else
                dp[i][j]=0;
        }
    }
    for(int i=1;i<=n;++i) {
        for(int j=0;j<=len;++j) {
            if(dp[i-1][j]) {
                if(j+a[i]<=len)
                    dp[i][j+a[i]]=1;
                if(j-a[i]>=0)
                    dp[i][j-a[i]]=1;
            }
        }
    }
    for(int j=0;j<=len;++j)
        if(dp[n][j])
            return true;
    return false;

}

int main()
{
    int n,t;
    cin>>t;
    while(t--) {
        cin>>n;
        for(int i=1;i<=n;++i)
            cin>>a[i];
        int l=0,r=2000;
        while(l<r) {
            int mid = (l+r)>>1;
            if(check(n,mid))
                r=mid;
            else
                l=mid+1;
        }
        cout<<r<<endl;
    }
    return 0;
}

解题思路2:

在解题思路1的基础上,更改一下dp状态。

动态规划,三步走:


1.状态表示:
dp[i][j]=x表示将第i个线段以坐标j结束的所占最小长度x。


2.初始化:

  • dp[0][j]=j,第0个绳子可以以任意坐标j结束,长度为j
  • else dp[i][j]=INF;

3.状态转移:

  • 往前放:
    dp[i][j-a[i]]=min(dp[i][j-a[i]],dp[i-1][j]);如果j-a[i]>=0
    因为往前放并不会增加区间长度
  • 往后放:
    dp[i][j+a[i]]=min(dp[i][j+a[i]],max(dp[i-1][j],j+a[i]));如果j+a[i]<len,len为可能最大长度2005
    dp[i-1][j]是放完i-1段绳子以坐标j结尾所占区间长度,而j+a[i]就是放当前绳子所占区间长度,求最大值

代码如下:

#include<bits/stdc++.h>

using namespace std;

const int MAXN=1e4+5;
int a[MAXN];
int dp[MAXN][2005]; //dp[i][j]=x表示将第i个线段以坐标j结束的所占最小长度x

int solve(int n) {
    int len=2005;


    for(int i=1;i<=n;++i)
        for(int j=0;j<len;++j)
            dp[i][j]=INT_MAX;

    //初始化一个0线段任意长度都行
    for(int j=0;j<len;++j)
        dp[0][j]=j;


    for(int i=1;i<=n;++i) {
        for(int j=0;j<len;++j) {
            if(j>=a[i])     //往前放
                dp[i][j-a[i]]=min(dp[i][j-a[i]],dp[i-1][j]);
            if(j+a[i]<len)  //往后放
                dp[i][j+a[i]]=min(dp[i][j+a[i]],max(dp[i-1][j],j+a[i]));
        }
    }

    int ans=INT_MAX;
    for(int j=0;j<len;++j)
        ans=min(ans,dp[n][j]);
    return ans;
}

int main()
{
    int n,t;
    cin>>t;
    while(t--) {
        cin>>n;
        for(int i=1;i<=n;++i)
            cin>>a[i];
        cout<<solve(n)<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值