[CF542A]Place Your Ad Here
[CF542A]Place Your Ad Here
题目大意:
有\(n(n\le2\times10^5)\)个广告和\(m(m\le2\times10^5)\)个电视台,第\(i\)个广告只能在\([l_i,r_i]\)内播放,第\(j\)个电视台会在时间段\([a_j,b_j]\)播出,并且有\(c_j\)个人收看。选择第\(x\)个广告和第\(y\)个电视台的收益为\((v-u)c_y\),其中\([u,v]=[l_x,r_x]\cap[a_y,b_y]\)。
从中选取一个广告和一个电视台,使收益最大。求最大收益,并输出任意一种方案。
思路:
将广告拆成两个端点排序,将电视台按照右端点排序。
枚举每个电视台\([l,r]\),若一个广告只有左端点在\(r\)前,那么我们只关心最左的左端点;若一个广告左右端点均在\([l,r]\)内,那么我们只关心其长度;若一个广告左端点在\(l\)前,右端点在\([l,r]\)内,那么我们只关心其右端点的位置。
对于第一种情况,用set维护即可;后两种情况只需用线段树维护区间最大值。
时间复杂度\(\mathcal O(n\log n)\)。
源代码:
#include<set>
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
struct Node {
int p,id;
bool operator < (const Node &rhs) const {
return p<rhs.p;
}
};
struct Seg {
int l,r,w,id;
bool operator < (const Seg &rhs) const {
return r<rhs.r;
}
};
const int N=2e5+1,K=8e5+1;
Node a[N],b[N];
Seg c[N];
int rnk[N],tmp[K];
class SegmentTree {
#define _left <<1
#define _right <<1|1
#define mid ((b+e)>>1)
private:
std::pair<int,int> val[K<<2];
void push_up(const int &p) {
val[p]=std::max(val[p _left],val[p _right]);
}
public:
void modify(const int &p,const int &b,const int &e,const int &x,const std::pair<int,int> &v) {
if(b==e) {
val[p]=std::max(val[p],v);
return;
}
if(x<=mid) modify(p _left,b,mid,x,v);
if(x>mid) modify(p _right,mid+1,e,x,v);
push_up(p);
}
std::pair<int,int> query(const int &p,const int &b,const int &e,const int &l,const int &r) const {
if(b==l&&e==r) return val[p];
std::pair<int,int> ret(0,0);
if(l<=mid) ret=std::max(ret,query(p _left,b,mid,l,std::min(mid,r)));
if(r>mid) ret=std::max(ret,query(p _right,mid+1,e,std::max(mid+1,l),r));
return ret;
}
#undef _left
#undef _right
#undef mid
};
SegmentTree t1,t2;
std::set<std::pair<int,int> > set;
int main() {
const int n=getint(),m=getint();
int tot=0;
for(register int i=1;i<=n;i++) {
const int l=getint(),r=getint();
a[i]=(Node){l,i};
b[i]=(Node){r,i};
tmp[++tot]=l;
tmp[++tot]=r;
}
for(register int i=1;i<=m;i++) {
tmp[++tot]=c[i].l=getint();
tmp[++tot]=c[i].r=getint();
c[i].w=getint();
c[i].id=i;
}
std::sort(&tmp[1],&tmp[tot]+1);
for(register int i=1;i<=n;i++) {
a[i].p=std::lower_bound(&tmp[1],&tmp[tot]+1,a[i].p)-tmp;
b[i].p=std::lower_bound(&tmp[1],&tmp[tot]+1,b[i].p)-tmp;
}
for(register int i=1;i<=m;i++) {
c[i].l=std::lower_bound(&tmp[1],&tmp[tot]+1,c[i].l)-tmp;
c[i].r=std::lower_bound(&tmp[1],&tmp[tot]+1,c[i].r)-tmp;
}
std::sort(&a[1],&a[n]+1);
std::sort(&b[1],&b[n]+1);
std::sort(&c[1],&c[m]+1);
for(register int i=1;i<=n;i++) {
rnk[a[i].id]=i;
}
int64 ans=0;
int x,y;
for(register int i=1,j=1,k=1;i<=m;i++) {
for(;j<=n&&a[j].p<=c[i].r;j++) {
set.insert(std::make_pair(tmp[a[j].p],a[j].id));
}
for(;k<=n&&b[k].p<=c[i].r;k++) {
const int j=rnk[b[k].id];
set.erase({tmp[a[j].p],a[j].id});
t1.modify(1,1,tot,a[j].p,{tmp[b[k].p]-tmp[a[j].p],a[j].id});
t2.modify(1,1,tot,a[j].p,{tmp[b[k].p],a[j].id});
}
//-: ad
//=: tv
int len=0,z=0;
if(!set.empty()) {
// ------
//======
const int t=tmp[c[i].r]-std::max(tmp[c[i].l],set.begin()->first);
if(t>len) {
len=t;
z=set.begin()->second;
}
}
{
// --
//======
const auto p=t1.query(1,1,tot,c[i].l,c[i].r);
if(p.first>len) {
len=p.first;
z=p.second;
}
}
{
//------
// ======
const auto p=t2.query(1,1,tot,1,c[i].l);
if(p.first-tmp[c[i].l]>len) {
len=p.first-tmp[c[i].l];
z=p.second;
}
}
if(1ll*len*c[i].w>ans) {
ans=1ll*len*c[i].w;
x=z,y=c[i].id;
}
}
printf("%lld\n",ans);
if(ans) printf("%d %d\n",x,y);
return 0;
}
[CF542A]Place Your Ad Here的更多相关文章
- Sharepoint2013 AD组用户不同步
背景: SP2013列表库使用AD安全组授权访问,向AD安全组添加一个用户A,在Sharepoint AD同步(增量和完全)后,用户A仍然无法访问列表库:原因: 参考:安全令牌上的缓存 SP2013 ...
- freeradius整合AD域作anyconncet认证服务器
一.服务器要求 Radius服务器:centos6.6.hostname.selinux disabled.stop iptables AD域服务器:Windows Server 2008 R2 E ...
- 讲座:Modeling User Engagement for Ad and Search
讲座:http://bdai.ruc.edu.cn/?p=118 Modeling User Engagement for Ad and Search ppt 链接: Dr. Ke(Adam) Zho ...
- Azure AD Connect 手动同步
我们目前采用工具Azure AD Connect 目录同步工具将本地域控制器的用户信息同步至office365和Azure 在之前目录同步工具中使用Windows 任务计划程序或单独的 Windows ...
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问
delphi ado 跨数据库访问 语句如下 ' and db = '帐套1' 报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATE ...
- 如何查看/统计当前AD域控制器的活动用户?
最近公司想知道某台AD域控制器上当前连接了多少活动用户? 此前个人只知道以下不是非常完善且统计起来比较麻烦的方法: 方法1:查看共享会话数.(不完全准确) 方法2:查看当前的DNS记录.(这种方法统计 ...
- AD域-让共享目录只显示用户有权限访问的文件夹
问题: 在AD域中,我们一般都会用到共享,如果有很多部门,我们可能还会按部门.职位配置权限.比如CSD,IT,PA等,但文件夹一多,用户看着就头大,而且用户没权限访问的文件夹误点击进去还会提示无权限访 ...
- AD域的安装(在Windows Server 2003中安装Active Directory)
在Active Directory中提供了一组服务器作为身份验证服务器或登录服务器,这类服务器被称作域控制器(Domain Controller,简称DC).建立一个AD域的过程实际就是在一台运行Wi ...
- 使用Ruby来实现批量更新AD中字段
准备工作 安装需要用到的gem gem install net-ldap gem install roo 准备好要更新的数据,比如exel表: /root/account.xlsx,内容如下 姓名 性 ...
随机推荐
- Day039--HTML
HTML小马哥博客 HTML CSS + DIV实现整体布局 1. HTML 超文本标记语言 对换行不敏感 空白折叠现象 标签要严格密封 新建HTML文件,输入 html:5,按tab键后,自动生成的 ...
- WinForm文件说明
以上位置,双击即可. 界面可以通过拖动控件,也可以通过背后的界面代码去布局. 如果删除了事件代码,界面可能报错,因为界面代码中有未删除的残余(波浪线提示处代码,直接删除即可). 对于多个窗体,Prog ...
- codeforces-1141 (div3)
A.算2,3的因子个数即可 #include <map> #include <set> #include <ctime> #include <cmath> ...
- restful设计规范
什么是restful? REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角度类 ...
- C++回顾day03---<异常>
一:传统错误处理机制(C中通过函数返回来处理) int CalcRes(int n, int m, char ch, int& res) { ; switch (ch) { case '+': ...
- JavaScript 基本包装类型,包装对象
前言 javascript对象是一种复合值,它是属性或已命名值的集合.通过'.'符号来引用属性值.当属性值是一个函数时,称其为方法.通过o.m()来调用对象o中的方法.我们发现,字符串也同样具有属性和 ...
- LINUX涉及网络相关知识
才接触到网络的老铁,是否比较晕呢? 简单记录一下网络相关知识吧(IPV4)! A0. 网络号.主机号 A1.网络地址分类: A2. 保留地址: A3. 子网掩码作用:(子网掩码.IPV4地址做“与”运 ...
- javascript通过navigator.userAgent识别各种浏览器
识别各种浏览器的实现原理是根据navigator.userAgent返回值识别: 实现: unction validBrowser(){ var u_agent = navigator.userAge ...
- Java并发之Thread类的使用
一.线程的几种状态 线程从创建到最终的消亡,要经历若干个状态.一般来说,线程包括以下这几个状态:创建(new).就绪(runnable).运行(running).阻塞(blocked).time wa ...
- 分布式系列五: RMI通信
RPC(Remote Procedure Call)协议 RPC协议是一种通过网络从远程计算机上请求服务, 而不需要了解底层网络技术的协议, 在OSI模型中处在应用层和网络层. 作为一个规范, 使用R ...