Codeforces Round #523 (Div. 2) D. TV Shows

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(ba)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 nn , xx and yy (1n1051≤n≤105 , 1y<x1091≤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 (1liri1091≤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+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 3 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(21)=74+3⋅(2−1)=7 , for the second is 4+3(104)=22 4+3⋅(10−4)=22 and for the third is 4+3(112)=31 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

题意

  有n个电视节目,给出开始时间和结束时间,组一个电视花费为(b-a)*y+x,a,b分别代表开始租用时间和结束租用时间,要全部播放这n个节目,最少花费为多少。

分析

  先按开始时间排序,相同的按结束时间排序。利用multiset来存放目前已经被租用的电视的结束时间。每次二分查找小于e[i].l记为now,贪心取(e[i].r-now)*y和(e[i].r-e[i].l)*y+x的最小花费。

///  author:Kissheart  ///
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const long long int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
struct node
{
    ll l,r;
}e[MAX];
int cmp(node a,node b)
{
    if(a.l!=b.l)
        return a.l<b.l;
    return a.r<b.r;
}
ll n,ans;
ll x,y;
multiset<ll>s;
int main()
{
    scanf("%lld%lld%lld",&n,&x,&y);
    for(ll i=1;i<=n;i++) scanf("%lld%lld",&e[i].l,&e[i].r);
    sort(e+1,e+1+n,cmp);
    s.clear();
    s.insert(mod);
    s.insert(-mod);

    for(ll i=1;i<=n;i++)
    {
        auto it=(--s.lower_bound(e[i].l));
        ll now=*it;
        //printf("%lld
",now);
        if( it==s.begin() || ((e[i].r-now)*y >=(e[i].r-e[i].l)*y+x && e[i].l!=now))
        {
            //printf("%lld
",now);
            ans=ans+(e[i].r-e[i].l)*y+x;
            //s.insert(e[i].r);
        }
        else
        {
            ans=ans+(e[i].r-now)*y;
            s.erase(it);
        }
        s.insert(e[i].r);
        ans%=mod;
    }
    printf("%lld
",ans);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/Kissheart/p/10146025.html