牛客多校第四场 B题 Interval Revisited

链接:https://www.nowcoder.com/acm/contest/142/B
来源:牛客网
 

题目描述

Chiaki has a long interval [1,m] and n small intervals [l1, r1], [l2,r2], ..., [ln, rn]. Each small interval [li,ri] is associated with a weight wi.
Chiaki would to select some small intervals such that:

  • each integer position x ∈ [1, m] is covered by at least one small interval.
  • let sx be the sum of the weights of all the small intervals covering position x, the maximum value of sx should be minimum.

Chiaki would like to know the minimum value of maximum sx.

输入描述:

There are multiple test cases. The first line of input contains an integer T, indicating the number of test
cases. For each test case:
The first line contains two integers n and m (1 ≤ n, m ≤ 2000) -- the number of small intervals and the length of the long interval.
Each of the next n lines contains three integers li, ri and wi (1 ≤ li ≤ ri ≤ m, 1 ≤ wi ≤ 1000).
It is guaranteed that the sum of all n does not exceed 20000.

输出描述:

For each test case, output an integer denoting the answer, or -1 if Chiaki cannot select such intervals.

示例1

输入

复制

2
2 4
1 2 2
3 4 5
1 4
1 3 1

输出

复制

5
-1

题意,给你一段大区间,多个小区间。 用多个小区间去覆盖大区间的整点数。 求用到的所有小区间中最大的厚度的最小值。

思路: 可以确定的是任意一个整数不可能被三个区间覆盖,也就是最多被两个区间覆盖。

dp[i][j] 表示选择i 这个小区间,从 j 到 a[i].r 只被一个区间覆盖的答案的最小值。

代码:

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const int N =2005;
const int M =2005;

struct node
{
    int l,r;
    ll w;
} a[N];

ll dp[N][M];
ll dd[N][M];

bool cmp(node a,node b)
{
    return a.r<b.r;
}

int main()
{
    int T;
    scanf("%d",&T);
    int n,m;
    while(T--)
    {
        scanf("%d %d",&n,&m);
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=m;j++){
                dp[i][j]=dd[i][j]=inf;
            }
        }
        for(int i=1; i<=n; i++)
        {
            scanf("%d %d %lld",&a[i].l,&a[i].r,&a[i].w);
        }

        sort(a+1,a+n+1,cmp);

        for(int i=1; i<=n; i++)
        {
            if(a[i].l==1)
            {
                for(int j=1; j<=a[i].r; j++) dd[i][j]=a[i].w;
            }
            for(int j=1; j<=a[i].r; j++) dp[i][j]=min(dp[i][j-1],dd[i][j]);
        }

        for(int i=1; i<=n; i++)
        {
            for(int k=1; k<i; k++)
            {
                if(a[k].r+1==a[i].l)
                {
                    dd[i][a[i].l]=min(dd[i][a[i].l],max(dp[k][a[k].r],a[i].w));
                }
                else if(a[k].r>=a[i].l)
                {
                    dd[i][a[k].r+1]=min(dd[i][a[k].r+1],max(dp[k][a[i].l],a[i].w+a[k].w));
                }
            }
            for(int j=1; j<=a[i].r; j++)
            {
                dp[i][j]=min(dp[i][j-1],dd[i][j]);
            }
        }
        ll ans=inf;
        for(int i=1; i<=n; i++)
        {
            ans=min(ans,dp[i][m]);
        }

        if(ans==inf)
        {
            printf("-1\n");
        }
        else printf("%lld\n",ans);

    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值