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. 关于Maven的一点理解

    maven是一个项目管理工具,主要作用是: 1.依赖管理(jar包,工程之间); 2.统一开发规范和工具.完成项目的一步构建 3.工程聚合.继承.依赖 其核心配置文件就是pom.xml:pom即Pro ...

  2. [shell] 一次性赋值多个变量

    管道符是fork子进程,子进程的变量无法传回父进程 [root@XM-v106 ~]# echo "1 2 3" | read a b c;echo $a [root@XM-v10 ...

  3. Python之并发编程-concurrent

    方法介绍 #1 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 ProcessPoolExecutor: 进程池 ...

  4. C++:类中两个易被忽略的默认函数

    C++的自定义类中有六个默认的函数,即如果用户没有显式定义这些函数时,C++编译器会类中生成这些函数的默认形式.除了大家所熟知的构造函数.拷贝构造函数.赋值函数和析构函数外,C++为自定义类 还提供了 ...

  5. "重力锁屏"Beta版使用说明

    一.产品介绍 重力锁屏是基于android系统的一款锁屏软件.它利用重力感应器来判断用户的动作从而自动锁屏亮屏,是锁屏软件的一大创新.相比传统的锁屏软件,“重力锁屏”从可操作性.方便性.功能全面性都有 ...

  6. 学习率(Learning rate)的理解以及如何调整学习率

    1. 什么是学习率(Learning rate)?   学习率(Learning rate)作为监督学习以及深度学习中重要的超参,其决定着目标函数能否收敛到局部最小值以及何时收敛到最小值.合适的学习率 ...

  7. delphi 删除字符串的回车、空格、Tab键

    myStr:=StringReplace(myStr, chr(13)+chr(10), '', [rfReplaceAll]);//删除回车      myStr:=StringReplace(my ...

  8. Js获取上一月份

    new Date(new Date().setMonth(new Date().getMonth() - 1))

  9. L2 L3 L4

    第二层交换机,是根据第二层数据链路层的MAC地址和通过站表选择路由来完成端到端的数据交换的.因为站表的建立与维护是由交换机自动完成,而路由器又是属于第三层设备,其寻址过程是根据IP地址寻址和通过路由表 ...

  10. Intellij IDEA Scala开发环境搭建

    1,在intellij 官网上下载IDEA 2,在scala官网上下载最新版scala 3,安装两个下载的文件 4,打开intellij,菜单栏help->find action   输入plu ...