Nowcoder-2018ACM多校训练营(第四场)B Interval Revisited

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述 

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

题意:有n个线段,区间为[L_{i},R_{i}],权值为W_{i},用这些线段覆盖[1,m]的区间,要求在使用的线段尽量少的前提下,区间上的点的权值的最大值尽可能的小,要求输出最小权值

题解:考虑到每个点最多被2条线段覆盖(如果被3条线段覆盖,则一定可以删除其中一条),我们可以这样做:先将线段按照L升序排序,假设当前使用到了第i条线段,那么是覆盖了[1,R_{i}],假设最后一个被2条线段覆盖的点的位置是j,那么选择下一条线段时,它的左端点L的范围应该是[j + 1,R_{i}],这样就可以进行DP了。

设dp[i][j]为,当前使用到了第i条线段,下一条线段的下标至少从j开始,已经被覆盖的点的最大权值

当R_{i} == m时,维护一下答案即可

#include <bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define x first
#define y second
#define add(x,y) ((x)+(y))%mod
#define sub(x,y) ((x)-(y)+mod)%mod
#define mul(x,y) ((x)%mod)*((y)%mod)%mod
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=a-1;i>=b;--i)
#define fuck(x) cout<<'['<<(x)<<']'<<endl;
using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fll;
const ll mod = 1000000007;
const int MX = 2e3 + 5;

int dp[MX][MX], t[MX];

struct node {
    int l, r, w;
    bool operator< (const node& _A) const {
        return l < _A.l;
    }
} a[MX];

int main() {
    //freopen ("in.txt", "r", stdin);
    int T, n, m; cin >> T;
    while (T--) {
        scanf ("%d%d", &n, &m);
        rep (i, 1, n + 1) scanf ("%d%d%d", &a[i].l, &a[i].r, &a[i].w);
        sort (a + 1, a + n + 1);
        rep (i, 1, n + 1) rep (j, 0, n + 2) dp[i][j] = INF;
        rep (i, 1, n + 1) {
            t[i] = n + 1;
            rep (j, i + 1, n + 1) if (a[j].l > a[i].r) {t[i] = j; break;}
        }
        int ans = INF;
        rep (i, 1, n + 1) {
            if (a[i].l == 1) dp[i][i + 1] = a[i].w;
            rep (j, i + 1, n + 1) {
                dp[i][j + 1] = min (dp[i][j + 1], dp[i][j]);
                if (a[j].l <= a[i].r) {
                    dp[j][t[i]] = min (dp[j][t[i]], max (dp[i][j], a[i].w + a[j].w) );
                }
                if (a[j].l == a[i].r + 1) {
                    dp[j][j + 1] = min (dp[j][j + 1], max (a[j].w, dp[i][j]) );
                }
            }
            if (a[i].r == m) ans = min (ans, dp[i][n + 1]);
        }
        cout << (ans == INF ? -1 : ans) << endl;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值