Description

给定 \(n\) 个电视节目和两个参数 \(x,y\)。你想要看全部的电视节目,但是同一个电视机同一个时刻只能播放一个电视节目,所以你只能多租赁电视机。在时间 \([l,r]\) 租赁一台电视机的花费是 \(x~+~y~(r - l)\)。一台电视机不可以在节目没有播放完时中断播放。求最小花费。答案对 \(1e9+7\) 取模

Input

第一行是三个整数 \(n, x, y\)

下面 \(n\) 行每行两个整数 \(l,r\),代表节目时间

Output

一行一个整数代表答案对 \(1e9+7\) 取模的结果

Hint

\(1~\leq~n~\leq~10^5~,~1~\leq~y~<~x~\leq~10^9~,~1~\leq~l~\leq~r~\leq~10^9\)

Solution

考虑贪心,有如下结论:

一、如果必须租赁新的电视机,在结束租赁的时间一定的情况下,租赁时间越晚越好。

这个结论显然,因为租赁时间越晚,租赁时长的代价越低。

二、如果前面有好几台空闲的电视机可以用,在不考虑租赁新的电视机时,一定选择上次结束时间最靠后的一台。

例如:

我们一定选择三号机器而不是一号二号,因为这样“浪费”的时间更少,即从上个节目结束到这个节目开始的时间花费更少。考虑如果后面紧跟着还有一个节目(开始时当前节目未结束),则它会选择一号,与这次选择一号下次选择三号相比,答案不会更劣。

以上是感性的理解结论二,下面给出结论二的证明(参考自官方题解):

先将节目顺序按照左端点排序。因为每个节目时长再乘 \(y\) 是的花费是不变的,因此我们只比较额外的花费。即租赁新电视机的花费和时的花费。下面的花费均值额外花费

考虑我们有两个用过的电视机 \(o1,o2\),其中 \(r_{o1}~<~r_{o2}\), \(r\) 为该电视上个节目的结束时间。当前我们要分配 \(i\) 这个节目。

下面分三种情况:

第一种:

如果后面不存在一个 \(j\) 使得从两个中继承过来比新租一个更划算的话,显然把 \(o2\) 分配给 \(i\) 更优

第二种:

如果后面存在一个 \(j\),使得 \((l_j ~-~r_{o2})~y~\leq~x~~(1)\),但是 \((l_j ~-~r_{o1})~y~>~x~~(2)\),我们如果将 \(o1\) 分配给 \(i\),那么花费是 \((l_j~-~r_{o2})~y~+~(l_i~-~r_{o1})~y~~(3)\)。考虑将 \(o2\) 分配给 \(i\),那么花费是 \((r_l~-~r_{o1})~y~+~x~~(4)\)。给 \((2)\) 式左右同加 \((l_i~-~r_{o2})~y\) 即得 \((l_j ~-~r_{o1})~y~+~(l_i~-~r_{o2})~y>~x~+~(l_i~-~r_{o2})~y\),即 \((3)~>~(4)\),所以讲 \(o2\) 分配给 \(i\) 更优

第三种:

如果后面存在一个 \(j\),使得 \((l_j ~-~r_{o2})~y~\leq~x~~(1)\),并且 \((l_j ~-~r_{o1})~y~\leq~x~~(2)\),那么这两种分配方式分别花费是 \((l_j~-~r_{o2})y~+~(l_i~-~r_{o1})y~=~y(l_j~+~l_i~-~r_{o2}~-~r_{o1})\) 和 \((l_i~-~r_{o2})y~+~(l_j~-~r_{o1})y~=~y(l_j~+~l_i~-~r_{o2}~-~r_{o1})\)。所以上两式是相等的,选择 \(o2\) 给 \(i\) 不会更劣。证毕。

那么我们就有如上两个贪心策略,每次比较贪心策略那个更优即可。即:新选择一个电视机租赁或者选择上次结束时间最靠后的一个电视机。根据结论一,如果选上次租赁过的而不是新租赁一个更好的话,因为新租赁电视机的时间被推迟了,答案一定不会更劣。于是这个贪心是正确的。

Code

#include <cstdio>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long typedef long long int ll; namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
} template <typename T>
inline void qr(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
} template <typename T>
inline void ReadDb(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
} namespace OPT {
char buf[120];
} template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
rg int top=0;
do {OPT::buf[++top] = x % 10 + '0';} while (x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
} const int maxn = 100010;
const int maxm = 200010;
const int MOD = 1000000007; struct M {
int l, r;
inline bool operator<(const M &_others) const {
return this->r < _others.r;
}
};
M MU[maxn]; struct Zay {
int id, pos;
bool Is_Begin;
inline bool operator<(const Zay &_others) const {
if (this->pos != _others.pos) return this->pos < _others.pos;
else if (this->Is_Begin ^ _others.Is_Begin) return this->Is_Begin;
else return this->id < _others.id;
}
};
Zay CU[maxm]; int n, x, y, cnt, scnt, ans;
int stack[maxm]; int main() {
freopen("1.in", "r", stdin);
qr(n); qr(x); qr(y);
for (rg int i = 1; i <= n; ++i) {
qr(MU[i].l); qr(MU[i].r);
Zay &_temp = CU[++cnt];
_temp.id = i; _temp.pos = MU[i].l; _temp.Is_Begin = true;
Zay &_tp = CU[++cnt];
_tp.id = i; _tp.pos = MU[i].r;
}
std::sort(CU + 1, CU + 1 + cnt);
for (rg int i = 1; i <= cnt; ++i) {
if (CU[i].Is_Begin) {
ll payd = x + 1ll * (MU[CU[i].id].r - MU[CU[i].id].l) * y;
if (scnt) {
if ((1ll * (MU[CU[i].id].r - stack[scnt]) * y) < payd) {
payd = 1ll * (MU[CU[i].id].r - stack[scnt]) * y;
--scnt;
}
}
ans = (ans + payd) % MOD;
} else {
stack[++scnt] = CU[i].pos;
}
}
qw(ans, '\n', true);
return 0;
}

【贪心】【CF1061D】 TV Shows的更多相关文章

  1. CodeForces-1061D TV Shows

    题目链接 https://vjudge.net/problem/CodeForces-1061D 题面 Description There are nn TV shows you want to wa ...

  2. Codeforces Round #523 (Div. 2) D. TV Shows 模拟(多重集 先把所有区间加入多重集合)+贪心+二分

    题意:给出n个电视节目的起始和结束时间  并且租一台电视需要x +y*(b-a)  [a,b]为时段 问完整看完电视节目的最小花费是多少 思路:贪心的思想 情况1 如果新租一台电视的花费<=在空 ...

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

    传送门 https://www.cnblogs.com/violet-acmer/p/10005351.html 题意: 有n个节目,每个节目都有个开始时间和结束时间. 定义x,y分别为租电视需要的花 ...

  4. 【codeforces】【Round#523D】TV shows

    题意:n个节目,每个节目的播放时间为[li,ri],你需要选择一些电视机全部播放这些节目,一台电视机不能同时播放多个节目,选择一个新的电视机代价为x , 如果某台电视机的使用时间为[Li,Ri]需要付 ...

  5. 2015-2016 ACM-ICPC Nordic Collegiate Programming Contest ---E题Entertainment Box(有点变化的贪心)

    提交链接 http://codeforces.com/gym/100781/submit Description: Ada, Bertrand and Charles often argue over ...

  6. 10 Best TV Series Based On Hacking And Technology

    Technology is rapidly becoming the key point in human lives. Here we have discussed top TV shows whi ...

  7. CodeForces - 1097F:Alex and a TV Show (bitset & 莫比乌斯容斥)

    Alex decided to try his luck in TV shows. He once went to the quiz named "What's That Word?!&qu ...

  8. 每日英语:In Digital Era, What Does 'Watching TV' Even Mean?

    We spend a full five hours and 16 minutes a day in front of a screen, and that's without even turnin ...

  9. 每日英语:Why 'The Voice' Is China's No. 1 TV Show

    U.S. fans of the hit talent show 'The Voice' may take for granted that its judges sit with their bac ...

随机推荐

  1. SQL-Server collation, what is it and how to change db/column collation

    The thing about collations is that although database have it's own collation, every table, and every ...

  2. umount命令详解

    基础命令学习目录首页                                    umount 用来卸载设备 -a:卸除/etc/mtab中记录的所有文件系统: -h:显示帮助: -n:卸除 ...

  3. easyui panel异步获取后台数据在前台显示

    我在使用easyui的时候,想做一个向下图所示的效果,这个panel的样式已经做好了,想从后台异步获取json数据,然后填入到文本框中,不知道哪位大神能给点指导?万分感谢! 放入表单中,使用form对 ...

  4. React Native 之 main.jsbundle生成方法

    通过react-native init yooweiProject 生成的RN项目(版本基于0.57),目录结构如下 项目结构: 大家可以发现main.jsbundle 是红色的,不存在的,这个属于正 ...

  5. $.each()用法

    通过它,你可以遍历对象.数组的属性值并进行处理. 使用说明 each函数根据参数的类型实现的效果不完全一致: 1.遍历对象(有附加参数) $.each(Object, function(p1, p2) ...

  6. 团队博客作业Week2 --- 学长学姐访谈录

    ## 团队作业2 ## ### 团队一 ### 这个团队中组员是位研一的学姐,她的软件工程老师是姚淑珍,当时她们团队总共有4个人,而且她们都很努力,但是可能是最后团队的作品不太理想,她们的软件并没有上 ...

  7. C#简单窗体应用程序(三)

    使用C#创建窗体应用程序的基本步骤: (1)创建项目: (2)用户界面设计: (3)属性设置: (4)编写程序代码: (5)保存.调试.运行: 例题:设计歌曲列表界面,效果如下: 第一步:创建项目: ...

  8. windows下的C++ socket服务器(2)

    int main(int ac, char *av[]) { ); ) { exit(); } thread t; ) { int socket_fd = accept(tcp_socket, nul ...

  9. js访问对象属性的两种方法

    var obj={name:'fuuf',age:19} 第一种,用.访问 obj.name 第二种 用[]访问 obj['name']  //此时name是字符串,要加引号 注意事项 使用第二种方法 ...

  10. 配置docker的私有仓库

    1:安装docker-registry包 yum install -y docker-distribution   2:启动docker-distribution,默认监听于TCP/5000端口 sy ...