Codeforces Round #523 (Div. 2) D. TV Shows
https://www.cnblogs.com/violet-acmer/p/10005351.html
•题意
有n个节目,每个节目都有个开始时间和结束时间。
定义 x,y 分别为租电视需要的花费和看电视需要的花费(应该是花的电费,哈哈)。
假如某电视节目的播放区间是[a,b],那么看完这个节目需要的总花费为x+(b-a)*y。
如果有其他节目的播放区间是[b,c],那么便不能和[a,b]的节目用同一个电视看。
求看完所有电视节目所需要的最少花费。
•题解
对于每个节目的播放区间[a,b],都会有个(b-a)*y 的花费;
能省钱的地方在“节目 i 是选择和之前的某个节目 j 共用一个电视,还是选择新租一个电视”;
这个只需要寻找一下有没有某个节目 j ,使得节目 i 和 j 共用一个电视要比 i 新租一个电视省钱。
(1):按节目开始时间从小到大排序,对于相同的开始时间,谁在前谁在后都可以。
(2):排序后,从第一个节目开始开始遍历。
假设来到第 i 个节目,查找节目 j,满足节目 j 的结束时间早于节目 i 的开始时间,并且节目 j 的结束时间尽可能晚;
判断节目 i 是否可以和节目 j 共用一个电视;
•贪心策略的证明(个人理解)
假设有①②③④节目,排好序后如下所示:
对于排序策略“相同的开始时间,谁在前谁在后都可以”的理解:
②③节目开始时间相同,假设 x > y,那么①②③节目的总花费有两种不同的策略,1.(①,②)+③ 和 2.(①,③)+②
方案1的总花费为 (x+(9-1)*y) + (x+(11-5)*y) = 2*x+14*y.
方案2的总花费为 (x+(11-1)*y) + (x+(9-5)*y) = 2*x+14*y.
显然,这两种方案所花费的价钱是相同的。
此题的难点在于“如何高效查找节目 j";
一开始本来打算开两个数组,一个数组存储节目的开始时间,一个数组存储节目的结束时间;
并用map存储结束时间相同的总节目数,然后,遍历的时候在结束时间的数组中二分查找;
实现了一下,又仔细想了想,感觉不行,还是太菜了。。。。。
没办法了,只好求助了,翻了翻大神博客,发现,差不多都用到了multiset这个容器,然后,补了补multiset;(tql)
•Code
#include<iostream>
#include<set>
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll __int64
const int MOD=1e9+;
const int maxn=1e5+; int n,x,y;
int res;
struct Node
{
int l,r;
Node(int _a=,int _b=):l(_a),r(_b){}
}show[maxn];
multiset<int >_set; bool cmp(Node _a,Node _b)
{
return _a.l < _b.l;
}
int Solve()
{
sort(show+,show+n+,cmp);
for(int i=;i <= n;++i)
{
multiset<int >::iterator it;
it=_set.lower_bound(show[i].l);//二分查找
//it=lower_bound(_set.begin(),_set.end(),show[i].l);
if(it == _set.begin())//如果it == _set.begin(),说明 i 之前没有结束时间比其小的电视节目
{
res += x;
res %= MOD;
_set.insert(show[i].r);
}
else
{
it--;//当前的it指向的时间是第一个 >= show[i].l,所以,要it--才是最靠近show[i].l的结束时间
ll cost=1ll*(show[i].l-*it)*y;
if(cost <= x)
{
res += cost;
_set.erase(it);
}
else
res += x;
res %= MOD;
_set.insert(show[i].r);
}
}
return res%MOD;
}
int main()
{
scanf("%d%d%d",&n,&x,&y);
for(int i=;i <= n;++i)
{
int l,r;
scanf("%d%d",&l,&r);
show[i]=Node(l,r);
res += 1ll*(r-l)*y%MOD;//记得加 1ll 防止爆int
res %= MOD;
}
printf("%d\n",Solve());
}
分割线:2019.7.2
•Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MOD=1e9+;
const int maxn=1e5+; int n;
ll x,y;
struct Data
{
int l,r;
bool operator < (const Data &obj) const
{
return l < obj.l;
}
}tv[maxn];
multiset<int >_set;
multiset<int >::iterator it; ll Solve()
{
sort(tv+,tv+n+); ll ans=;
for(int i=;i <= n;++i)///求出每个节目都需要一台电视的总花费
ans=(ans+x+(tv[i].r-tv[i].l)*y)%MOD; _set.clear();
for(int i=;i <= n;++i)
{
it=_set.lower_bound(tv[i].l);
if(it != _set.begin())
{
--it;
ll cur=(tv[i].l-*it)*y;
if(cur <= x)///如果可以共用一台电视
{
ans=(ans+cur-x+MOD)%MOD;
_set.erase(it);
}
}
_set.insert(tv[i].r);
}
return ans%MOD;
}
int main()
{
// freopen("C:\\Users\\hyacinthLJP\\Desktop\\in&&out\\contest","r",stdin);
scanf("%d%lld%lld",&n,&x,&y);
for(int i=;i <= n;++i)
{
int l,r;
scanf("%d%d",&l,&r);
tv[i]=Data{l,r};
}
printf("%lld\n",Solve()); return ;
}
Codeforces Round #523 (Div. 2) D. TV Shows的更多相关文章
- Codeforces Round #523 (Div. 2) D. TV Shows 模拟(多重集 先把所有区间加入多重集合)+贪心+二分
题意:给出n个电视节目的起始和结束时间 并且租一台电视需要x +y*(b-a) [a,b]为时段 问完整看完电视节目的最小花费是多少 思路:贪心的思想 情况1 如果新租一台电视的花费<=在空 ...
- Codeforces Round #523 (Div. 2)
Codeforces Round #523 (Div. 2) 题目一览表 来源 考察知识点 完成时间 A Coins cf 贪心(签到题) 2018.11.23 B Views Matter cf 思 ...
- Codeforces Round #523 (Div. 2) Solution
A. Coins Water. #include <bits/stdc++.h> using namespace std; int n, s; int main() { while (sc ...
- Codeforces Round #523 (Div. 2) E. Politics(最小费+思维建图)
https://codeforces.com/contest/1061/problem/E 题意 有n个点(<=500),标记第i个点的代价a[i],然后分别在这n个点建两棵树,对于每颗树的每个 ...
- Codeforces Round #523 (Div. 2) F. Katya and Segments Sets (交互题+思维)
https://codeforces.com/contest/1061/problem/F 题意 假设存在一颗完全k叉树(n<=1e5),允许你进行最多(n*60)次询问,然后输出这棵树的根,每 ...
- Codeforces Round #523 (Div. 2) Cdp
题:https://codeforces.com/contest/1061/problem/C 题意:给你一个序列,我们求他们子序列的个数,这个子序列有个限制就是每一个子序列上的值都必须是能整除他的下 ...
- Codeforces Round #523 (Div. 2) B Views Matter
传送门 https://www.cnblogs.com/violet-acmer/p/10005351.html 这是一道贪心题么???? 题意: 某展览馆展览一个物品,此物品有n堆,第 i 堆有a[ ...
- Codeforces Round #523 (Div. 2) C Multiplicity (DP)
传送门 https://www.cnblogs.com/violet-acmer/p/10005351.html 题意: 给定一数组a[],从a[ ]中除去任意个元素得到b[ ],求能形成多少“好序列 ...
- Codeforces Round #523 (Div. 2) C. Multiplicity
C. Multiplicity 题目链接:https://codeforc.es/contest/1061/problem/C 题意: 给出一串数,问它的“好序列“有多少.好序列的定义是,首先是一个子 ...
随机推荐
- 在delphi中生成GUID
什么是 GUID ? 全球唯一标识符 (GUID) 是一个字母数字标识符,用于指示产品的唯一性安装.在许多流行软件应用程序(例如 Web 浏览器和媒体播放器)中,都使用 GUID. GUID 的格式为 ...
- NPOI 上传Excel功能(二)
3.上传文件,写入log using DC.BE.Business.SYS; using DC.BE.Entity.ERP; using DC.BE.Entity.SAS; using DC.BE.E ...
- 使用Golang打造自己的http服务器 1.0版本
package main import ( "io/ioutil" "net/http" "os" ) func main() { args ...
- BZOJ5037[Jsoi2014]电信网络——最大权闭合子图
题目描述 JYY创建的电信公司,垄断着整个JSOI王国的电信网络.JYY在JSOI王国里建造了很多的通信基站.目前所有的基站 都是使用2G网络系统的.而现在3G时代已经到来了,JYY在思考,要不要把一 ...
- UESTC1013-我的魔法栈-模拟/排列组合
有一个串,有黑色和白色两种元素.一次操作可以把最上面的白色元素变成黑色,同时把这个元素上面的所有元素变成白色. 给你一个30以内的串,计算变成全黑时,元素变化的总和. 我用的方法比较笨,打表处理了1- ...
- Quartus prime16.0 与modelsim ae 联调
前言 quartus和modelsim联调对仿真还是很方便的,当然最好是quartus干综合到烧录的活,modelsim单独仿真.而且ae版的性能比se版差. 流程: 1.配置modelsim ae路 ...
- 【XSY1552】自动机 构造
题目大意 给你一个自动机,包含\(n\)个状态,指令集为前\(m\)个小写字母,对于每个状态\(s\)和每个指令\(i\),自动机均有后继\(T(s,i)\).请你求出一个长度不超过\(2^{20}\ ...
- ans Single VIP LLB and SLB config
ans Single VIP LLB and SLB config 配置命令: # 配置设备工作模式和开启的功能 > enable ans mode FR MBF Edge USNIP L3 P ...
- git errot
常用 git 基础命令 1.错误信息 使用TortoiseGit执行pull命令时显示 git.exe pull --progress --no-rebase -v "origin" ...
- 爬虫_中国天气网_文字天气预报(xpath)
import requests from lxml import etree headers = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/5 ...
