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. web14 validation.xml配置 登录验证文件配置

    电影网站:www.aikan66.com 项目网站:www.aikan66.com 游戏网站:www.aikan66.com 图片网站:www.aikan66.com 书籍网站:www.aikan66 ...

  2. struts2封装请求参数

    利用struts2框架进行将页面请求的参数封装有三种方法 第一种(不推荐) 就是动作类和bean中结合在一起,这样方法比较简单,但是很混乱. 代码: package com.example.actio ...

  3. MapReduce编程之Map Join多种应用场景与使用

    Map Join 实现方式一:分布式缓存 ● 使用场景:一张表十分小.一张表很大. ● 用法: 在提交作业的时候先将小表文件放到该作业的DistributedCache中,然后从DistributeC ...

  4. js如何判断一个值是不是Array类型

    本来判断一个对象类型用typeof是最好的,不过对于Array类型是不适用的可以使用 instanceof操作符var arrayStr=new Array("1","2 ...

  5. 二叉查找树ADT--C语言描述

    首先给出此ADT的声明: struct TreeNode; typedef struct TreeNode *Position; typedef struct TreeNode *SearchTree ...

  6. svmtrain和svmpredict简介

    转自:http://blog.sina.com.cn/s/blog_4d7c97a00101bwz1.html 本文主要介绍了SVM工具箱中svmtrain和svmpredict两个主要函数: (1) ...

  7. 学习laravel源码之中间件原理

    刨析laravel源码之中间件原理 在看了laravel关于中间件的源码和参考了相关的书籍之后,写了一个比较简陋的管道和闭包实现,代码比较简单,但是却不好理解所以还是需要多写多思考才能想明白其中的意义 ...

  8. strtr、str_replace()、substr_replace、preg_replace之间的区别

    strtr(string, from, to): 逐个字符开始替换,以from跟to中长度较较短的一个为准,例如: strtr("aidengni","ai", ...

  9. Workstation和Virtualbox的虚拟机磁盘扩容方式.

    1. 虚拟机磁盘管理, 更改磁盘格式是一个场景 还有一个场景是 硬盘空间不够了 需要扩充. 方法主要有两个. 如果是workstation的的虚拟机. 并且没有快照 可以直接GUI操作 如下图: 虚拟 ...

  10. [转帖]认识固态:SSD硬盘内外结构解析

    认识固态:SSD硬盘内外结构解析 来自: 中关村在线 收藏 分享 邀请 固态硬盘(Solid State Drive),简称固态盘(SSD),是用固态电子存储芯片阵列而制成的硬盘,由控制单元和存储单元 ...