D - TV Shows CodeForces - 1061D(贪心+排序+multiset二分)

 TV Shows
Time limit 2000 ms Memory limit 262144 kB
 

Problem Description

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
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 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.

 

Solution:

首先,新电视的代价x,每分钟持续y(y<x),可以买无限个电视,求最小代价

贪心的思考下,既然y<x,如果让节目都连续一起看的话,肯定代价最低,但数据不可能出得这么良心,所以当要播放新节目的时候就要取舍下,到底直接买台新电视好呢,还是让最近的旧电视等等(dmin)比较划算。

然后还要解决的问题是,怎么安排节目的出现比较好,一般是对l排序,或者r排序,这里根据我们的贪心选择,对于一个准备播放的节目来说,仅有两种选择,选择新电视的代价是一样的,关键是怎么让旧电视等待时间(d min)变短。

 讲l,r分开来看的话,起点是固定的,终点也是固定的,而新的一台电视如果选择旧电视,肯定看终点,离自己越近越好(d越小),但终点是固定的,唯有自己往前挪咯,综上排L合理。(由于这里有多个终点,要用multiset?)

 

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <map>
 4 #include <set>
 5 
 6 using namespace std;
 7 
 8 typedef long long ll;
 9 
10 const ll mod = 1e9 + 7;
11 
12 struct A {
13     ll l, r;
14     bool operator<(A b) const {
15         return l < b.l;
16     }
17 }seg[100010];
18 
19 multiset<ll>s;
20 
21 int main() {
22     ll n,d, x, y,ans=0;
23     scanf("%lld%lld%lld", &n, &x, &y);
24     for (int i = 1; i <= n; ++i) {
25         ll l, r;
26         scanf("%lld%lld", &l, &r);
27         seg[i].l = l;
28         seg[i].r = r;
29         ans = (ans + (r - l)*y%mod) % mod;
30     }
31 
32     sort(seg + 1, seg + 1 + n);
33     
34     for (int i = 1; i <= n; ++i) {
35         auto k = s.lower_bound(seg[i].l);
36         if (k==s.begin() || (seg[i].l-*(--k))*y>=x ) {
37             ans = (ans + x%mod) % mod;
38         }
39         else {
40             ans = (ans + (seg[i].l - (*k))*y%mod) % mod;
41             s.erase(k);
42         }
43         s.insert(seg[i].r);
44     }
45     printf("%lld\n", ans);
46     return 0;
47 }

 

转载于:https://www.cnblogs.com/SayGB/p/10397994.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值