1. 程式人生 > >Codeforces Round #523 (Div. 2) D. TV Shows

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

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

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

output

60

input

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

output

142

input

2 1000000000 2
1 2
2 3

output

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.

思路:首先排序是肯定可以想到的,但關鍵是用什麼來維護比較你一個節目接在另一個下面的費用小還是直接在開一臺電視的費用小。大佬們又multiset,也有用set加map來維護的。我就使用set加map維護的(注意比較的時候有個二分找結束的時間用set本身的二分函式效率快很多)。用到二分查詢的原因是我們記錄結束的時間那麼新的時間加入就要找離開始時間最近的結束時間。複雜度是nlogn

程式碼:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#define LL long long

using namespace std;
const int maxn=1e5+100;
const int mod=1e9+7;
struct node
{
    LL a,b;
    friend bool operator <(node p,node q)
    {
        if(p.a==q.a)
        {
            return p.b<q.b;
        }
        return p.a<q.a;
    }
};
node c[maxn];
set<LL>st;
map<LL,int>mp;
int main()
{
    int n;
    LL x,y;
    scanf("%d%lld%lld",&n,&x,&y);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld%lld",&c[i].a,&c[i].b);
    }
    sort(c+1,c+1+n);
    LL ans=0;
    st.insert(c[1].b);
    mp[c[1].b]++;
    ans=(ans+x+(c[1].b-c[1].a)*y%mod)%mod;
    set<LL>::iterator it;
    for(int i=2;i<=n;i++)
    {
        it=st.lower_bound(c[i].a);

        if(it==st.begin())
        {
            ans=(ans+x+(c[i].b-c[i].a)*y%mod)%mod;
            mp[c[i].b]++;
        }
        else
        {
            it--;
            LL value=*it;
            if(c[i].a>value&&(c[i].a-value)*y<=x)
            {
                mp[value]--;
                if(mp[value]==0)
                    st.erase(value);
                ans=(ans+(c[i].b-value)*y%mod)%mod;
                mp[c[i].b]++;
            }
            else
            {
                ans=(ans+x+(c[i].b-c[i].a)*y%mod)%mod;
                mp[c[i].b]++;
            }
        }
        st.insert(c[i].b);
    }
    printf("%lld\n",ans);
    return 0;

}

相關推薦

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

D. TV Shows 題目連結:https://codeforc.es/contest/1061/problem/D 題意: 有n個電視節目,每個電視節目都有一定的時間 [li,ri],現在要把每個節目都看了,但是電視需要租,租電視費用為x,之後每一分鐘的費用為y,比如[l,r]的費用就是x+(r-l

Codeforces Round #523 (Div. 2) D - TV Shows ( multiset的使用)

題意:給出 n 個電視節目,在你附近有一家電視出租商店。它以 X 元 租一臺電視機,每一分鐘花費是  y元,如果片段 [li,ri] 和 [lj,rj] 相交,則顯示i和j不能同時在一臺電視上觀看。一旦您開始在某臺電視上觀看一檔節目,直到這個節目結束。 你需要花費最小的花費把

Codeforces Round #523 (Div. 2) D. TV Shows(multiset+思維)

題目連結: D. TV Shows   題意: 有 n 個電視節目,播放的時間區間為 [li,ri] 。同一時間,不同的節目不能在同一臺電視上播放。一個節目必須完整的在一臺電視上播放完。現在租一臺電視需要先付 x 塊錢,之後每分鐘要付 y 塊錢,即租一臺電視從時間區間

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

output standard output There are nn TV shows you want to watch. Suppose the whole time is split into equal parts called "minutes". The i

Codeforces Round #524 (Div. 2)D - TV Shows

int lld 多個 i++ force != namespace 超時 當前 題意是給你n個節目,每次租一臺電視需要消耗x+(r-l)*y元,問你怎麽樣安排才能使得看完所有節目並且消費最少,輸出最少的金額 做法是按節目開始時間l排序,遍歷所有節目,如果該節目不能在已有的

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 The

Codeforces Round #221 (Div. 2) D

cpp 位置 input memset ont code init cal 矩形 有點郁悶的題目,給了2000ms,可是n,m的範圍已經是5000了。5000 * 5000一般在別的OJ已經是超了2000ms,一開始不敢敲。看了下別人有n*m的潛逃循環,原來CF的機子如

Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(離線樹狀數組)

turn string 之前 algorithm printf ace r++ void contest http://codeforces.com/contest/703/problem/D 題意: 給出一行數,有m次查詢,每次查詢輸出區間內出現次數為偶數次的數字的異

Codeforces Round #271 (Div. 2) D. Flowers (遞推 預處理)

int art style eve itl which pop 有一種 esp We saw the little game Marmot made for Mole‘s lunch. Now it‘s Marmot‘s dinner time and, as we

【分類討論】【spfa】【BFS】Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game

邊界情況 code def bfs spa eof scan string amp 那個人第一步肯定要麽能向下走,要麽能向右走。於是一定可以判斷出上下是否對調,或者左右是否對調。 然後他往這個方向再走一走就能發現一定可以再往旁邊走,此時就可以判斷出另一個方向是否對調。 都判

【推導】Codeforces Round #364 (Div. 2) D. As Fast As Possible

std %d while pre 分享 mage oss 時間 http 一種方法是二分總時間,復雜度O(nlogn)。 另外我們可以證明,當所有人同時到達終點的時候,是最優的,因為沒有人的時間“浪費”了。 我們又發現,每個人的運動過程總是兩段,要麽是走路,要麽是坐車。於

Codeforces Round #316 (Div. 2) D. Tree Requests(DFS+狀態壓縮)

back push_back init data cti pragma [0 ack false 題意:給定一棵樹,n個節點。每一個節點處有一個字母,結點的深度定義為節點到根結點1的距離, 有m個詢問(u。v),每次回答以結點u為根的子樹的深度為v的那些節點處的字

Codeforces Round #423 (Div. 2) D. High Load(構造題)

ces lose 最大 int get close fin print 構造 題目鏈接:Codeforces Round #423 (Div. 2) D. High Load 題意: 給你一個數n和k,讓你構造出一顆樹,有k個葉子節點,使得這棵樹的任意兩個點的距離的最大值最

Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground

最大 i++ using urn its -- spa 多少 != 題意:給出 一顆樹,然後q個詢問,每個詢問給出3個點,讓我們自己選擇一個起點一個終點,這條路線上的點標記,問第三個點到終點的最多標記點是多少 思路:第三個點到終點的標記點,肯定是第三個點與起點的最近公共祖先

Codeforces Round #427 (Div. 2) D. Palindromic characteristics(Manacher求回文串)

space truct -- names class none ref ++ += 題目鏈接:Codeforces Round #427 (Div. 2) D. Palindromic characteristics 題意: 給你一個串,定義k-th回文串,讓你求每個k-t

Codeforces Round #426 (Div. 2) D. The Bakery(線段樹維護dp)

src lap codeforce blank com date close scanf logs 題目鏈接: Codeforces Round #426 (Div. 2) D. The Bakery 題意: 給你n個數,劃分為k段,每段的價值為這一段不同的數的個數,問如何

Codeforces Round #263 (Div. 2) D. Appleman and Tree 樹形dp

鏈接 樹形dp targe 代碼 else 多少 color turn def 鏈接: http://codeforces.com/contest/462/problem/D 題意: 給定n個點的樹, 0為根,下面n-1行表示每個點的父節點 最後一行n個數 表示每

(容斥)Codeforces Round #428 (Div. 2) D. Winter is here

who walk ++ 關系 queue 只需要 long iostream scanf D. Winter is here time limit per test 3 seconds memory limit per test 256 megabytes inpu

Codeforces Round #428 (Div. 2) D. Winter is here[數論II][容斥原理]

note efi force its eps page http ref esp 傳送門:http://codeforces.com/contest/839/problem/D Examples input 33 3 1 output 12 inp

【排序】【規律】Codeforces Round #254 (Div. 2) - D. Rooter's Song

names nes rac then represent cin loaded output same D. DZY Loves FFT Source http://codeforces.com/contest/445/problem/D Description Wh