【Codeforces Round #523(Div. 2)】TV Shows(贪心+map)

题目链接

D. TV Shows

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn TV shows you want to watch. Suppose the whole time is split into equal parts called "minutes". The ii-th of the shows is going from lili-th to riri-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][li,ri] and [lj,rj][lj,rj] intersect, then shows ii and jj 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 xx rupees, and charges yy (y<xy<x) rupees for every extra minute you keep the TV. So in order to rent a TV for minutes [a;b][a;b] you will need to pay x+y⋅(b−a)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+7109+7.

Input

The first line contains integers nn, xx and yy (1≤n≤1051≤n≤105, 1≤y<x≤1091≤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 nn lines contains two integers lili and riri (1≤li≤ri≤1091≤li≤ri≤109) denoting the start and the end minute of the ii-th TV show.

Output

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

Examples

input

Copy

5 4 3
1 2
4 10
2 4
10 11
5 9

output

Copy

60

input

Copy

6 3 2
8 20
6 22
4 15
20 28
17 25
20 27

output

Copy

142

input

Copy

2 1000000000 2
1 2
2 3

output

Copy

999999997

Note

In the first example, the optimal strategy would be to rent 33 TVs to watch:

  • Show [1,2][1,2] on the first TV,
  • Show [4,10][4,10] on the second TV,
  • Shows [2,4],[5,9],[10,11][2,4],[5,9],[10,11] on the third TV.

This way the cost for the first TV is 4+3⋅(2−1)=74+3⋅(2−1)=7, for the second is 4+3⋅(10−4)=224+3⋅(10−4)=22 and for the third is 4+3⋅(11−2)=314+3⋅(11−2)=31, which gives 6060 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+7109+7.

【题意】

有n部片子,给出每部片子的起始时间a和结束时间b,一部片子只能在一部TV上看,并且只有在一部片子结束之后才能在这个TV上放另一部片子,已知租用TV的费用为x,每分钟需要增收租金y,可以同时看很多片子,问最少需要的租金。

【解题思路】

首先还是很明确的,先给这n部片子的放映时间排序。用map存储同一结束时间的片子的数量。

用二分查找是否存在比当前片子开始时间小一点的结束时间,如果不存在说明需要再租一台TV。如果存在,就要分两类情况,第一类是(当前片子的开始时间-之前的片子的结束时间)*y<=租金,说明可以用之前的TV继续看,反之需要再租一台TV。

【代码】

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
const LL mod=1e9+7;
struct Node
{
    LL l,r;
}node[maxn];
bool cmp(Node a,Node b)
{
    return (a.l!=b.l)?a.l<b.l:a.r<b.r;
}
int main()
{
    LL n,x,y,ans=0;
    map<LL,int>mp;
    map<LL,int>::iterator it;
    scanf("%lld%lld%lld",&n,&x,&y);
    for(int i=0;i<n;i++)
        scanf("%lld%lld",&node[i].l,&node[i].r);
    sort(node,node+n,cmp);
    mp[node[0].r]++;
    ans=(ans+x+y*(node[0].r-node[0].l)%mod)%mod;
    for(int i=1;i<n;i++)
    {
        it=mp.lower_bound(node[i].l);
        if(it==mp.begin())
        {
            ans=(ans+x+y*(node[i].r-node[i].l)%mod)%mod;
            mp[node[i].r]++;
        }
        else
        {
            it--;
            LL t=it->first;
            if(node[i].l>t && (node[i].l-t)*y<=x)
            {
                ans=(ans+y*(node[i].r-t)%mod)%mod;
                mp[t]--;
                if(mp[t]==0)mp.erase(it);
                mp[node[i].r]++;
            }
            else
            {
                ans=(ans+x+y*(node[i].r-node[i].l)%mod)%mod;
                mp[node[i].r]++;
            }
        }
    }
    printf("%lld\n",ans%mod);
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/84649678