Codeforces Contest 1061 problem D TV Shows——有约束的情况找区间长度最小(按开头排序的情况)

There are n TV shows you want to watch. Suppose the whole time is split into equal parts called “minutes”. The i-th of the shows is going from li-th to ri-th minute, both ends inclusive.

You need a TV to watch a TV show and you can’t watch two TV shows which air at the same time on the same TV, so it is possible you will need multiple TVs in some minutes. For example, if segments [li,ri] and [lj,rj] intersect, then shows i and j can’t be watched simultaneously on one TV.

Once you start watching a show on some TV it is not possible to “move” it to another TV (since it would be too distracting), or to watch another show on the same TV until this show ends.

There is a TV Rental shop near you. It rents a TV for x rupees, and charges y (y<x) rupees for every extra minute you keep the TV. So in order to rent a TV for minutes [a;b] you will need to pay x+y⋅(b−a).

You can assume, that taking and returning of the TV doesn’t take any time and doesn’t distract from watching other TV shows. Find the minimum possible cost to view all shows. Since this value could be too large, print it modulo 109+7.

Input
The first line contains integers n, x and y (1≤n≤105, 1≤y<x≤109) — the number of TV shows, the cost to rent a TV for the first minute and the cost to rent a TV for every subsequent minute.

Each of the next n lines contains two integers li and ri (1≤li≤ri≤109) denoting the start and the end minute of the i-th TV show.

Output
Print exactly one integer — the minimum cost to view all the shows taken modulo 109+7.

Examples
inputCopy
5 4 3
1 2
4 10
2 4
10 11
5 9
outputCopy
60
inputCopy
6 3 2
8 20
6 22
4 15
20 28
17 25
20 27
outputCopy
142
inputCopy
2 1000000000 2
1 2
2 3
outputCopy
999999997
Note
In the first example, the optimal strategy would be to rent 3 TVs to watch:

Show [1,2] on the first TV,
Show [4,10] on the second TV,
Shows [2,4],[5,9],[10,11] on the third TV.
This way the cost for the first TV is 4+3⋅(2−1)=7, for the second is 4+3⋅(10−4)=22 and for the third is 4+3⋅(11−2)=31, which gives 60 int total.

In the second example, it is optimal watch each show on a new TV.

In third example, it is optimal to watch both shows on a new TV. Note that the answer is to be printed modulo 109+7.

题意:

给你一些电视节目的播出时间,一个电视机只能同时看一个节目,告诉你组一个电视机第一分钟要多少钱,之后的每分钟要多少钱,问你要看所有的节目花掉的钱最少是多少。

题解:

我们对每一个节目只需要找到在他前面离他开头最近的结尾即可,但是这道题不能按结尾排序,我一开始按结尾排序写了好久wa了,后来才发现会有这么一个问题,对于下面这个区间:
在这里插入图片描述
我们可能会把1和2搭配,3单独一个:
在这里插入图片描述
线是尴尬点,将就着看。。但其实应该是1和3搭配,2单独一个
在这里插入图片描述
蓝色的就是他们不相同的一块。
接下来我们验证按开头拍的正确性:因为是按照开头排序,所以前面的结尾对于这个开头来说一定小于等于别的开头,也就是说每个线段都是一定要取的,现在我们要做的就是让线段之间的空尽量小,但是如果这个空乘上money大于重新租一个电视的钱的话,那就直接租一个,这道题很多容器都可以选。

#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long
const int N=1e5+5;
const ll mod=1e9+7;
struct node
{
    ll s,f;
    bool operator< (const node& a)const
    {
        if(s!=a.s)
            return s<a.s;
        return f<a.f;
    }
}a[N];
multiset<ll>s;
int main()
{
    int n;
    ll x,y;
    scanf("%d%lld%lld",&n,&x,&y);
    ll ans=0;
    for(int i=1;i<=n;i++)
        scanf("%lld%lld",&a[i].s,&a[i].f),(ans+=(a[i].f-a[i].s)*y)%=mod;
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)
    {
        multiset<ll>::iterator pos=s.lower_bound(a[i].s);
        if(pos==s.begin()||(a[i].s-*(--pos))*y>x)
            (ans+=x)%=mod,s.insert(a[i].f);
        else
        {
            (ans+=(a[i].s-*pos)*y)%=mod;
            s.erase(pos);
            s.insert(a[i].f);
        }
    }
    printf("%lld\n",ans);
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值