Codeforces Round #535 E2-Array and Segments (Hard version)
Codeforces Round #535 E2-Array and Segments (Hard version)
题意:
给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一),
求最后最大值与最小值的差值最大,并输出选择的区间
思路:
在n=300的时候,我们是枚举每个数作为最小值,应用所有覆盖它的区间,并且没
次都更行差值的最大值。
但是这里的n=1e5,所以我们不能用O(n*n*m),但是我们看到这里的m=300
所以可以从m入手,枚举区间,就是记录每个区间的两个端点,利用差分的思想,
来枚举更新最大值
这里说一下为什么枚举最小值,因为如果最大值也在这个区间则抵消,如果没在则
更好
#include<bits/stdc++.h>
using namespace std;
#define MAX 100005
int n,m;
vector<int>add[MAX],sub[MAX],a(MAX);
vector<pair<int,int > >b(MAX); void change(int l,int r,int x)
{
for(int i=l;i<=r;i++)
{
a[i]+=x;
}
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=;i<m;i++) sub[i].clear();
for(int i=;i<m;i++) add[i].clear();
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<m;i++)
{
scanf("%d %d",&b[i].first,&b[i].second);
b[i].first--;
b[i].second--;
sub[b[i].first].push_back(i);
add[b[i].second+].push_back(i);
}
int minn,maxn;
minn=maxn=a[];//注意这里不要习惯把minn=INF,maxn=0,因为a数组元素可能为负数,比如-1,这样ans=1,容易出错
for(int i=;i<n;i++)
{
maxn=max(maxn,a[i]);
minn=min(minn,a[i]);
}
int ans=maxn-minn;
int c=-;
for(int i=;i<n;i++)
{
//应用覆盖的区间
for(int j=;j<sub[i].size();j++)
{
int ind=sub[i][j];
change(b[ind].first,b[ind].second,-);
}
//消除之前的区间
for(int j=;j<add[i].size();j++)
{
int ind=add[i][j];
change(b[ind].first,b[ind].second,);
}
int minn=maxn=a[];
if(add[i].size()||sub[i].size())
{
for(int j=;j<n;j++)
{
maxn=max(maxn,a[j]);
minn=min(minn,a[j]);
}
if(maxn-minn>ans)
{
ans=maxn-minn;
c=i;
}
}
}
int len=;
int mask[MAX];
for(int i=;i<m;i++)
{
if(b[i].first<=c&&c<=b[i].second)
{
mask[len++]=i;
}
}
printf("%d\n",ans);
printf("%d\n",len);
for(int i=;i<len;i++)
{
printf("%d ",mask[i]+);
}
}
return ;
}
Codeforces Round #535 E2-Array and Segments (Hard version)的更多相关文章
- Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】
传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...
- CF #535 (Div. 3) E2 Array and Segments (Hard version) 利用线段树进行区间转移
传送门 题意: 有m个区间,n个a[ i ] , 选择若干个区间,使得整个数组中的最大值和最小值的差值最小.n<=1e5,m<=300; 思路: 可以知道每个i,如果一个区间包含这个 ...
- E1. Array and Segments (Easy version)(暴力) && E2. Array and Segments (Hard version)(线段树维护)
题目链接: E1:http://codeforces.com/contest/1108/problem/E1 E2:http://codeforces.com/contest/1108/problem ...
- CF E2 - Array and Segments (Hard version) (线段树)
题意给定一个长度为n的序列,和m个区间.对一个区间的操作是:对整个区间的数-1可以选择任意个区间(可以为0个.每个区间最多被选择一次)进行操作后,要求最大化的序列极差(极差即最大值 - 最小值).ea ...
- Codeforces Round #535 (Div. 3) 题解
Codeforces Round #535 (Div. 3) 题目总链接:https://codeforces.com/contest/1108 太懒了啊~好久之前的我现在才更新,赶紧补上吧,不能漏掉 ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]
hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...
- Codeforces Round #504 D. Array Restoration
Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...
- Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)
题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...
随机推荐
- wordpress系统网站访问慢的解决方案
从2013年5月底开始,google在中国基本处于无法访问状态,谷歌官网 域名,香港域名均无法访问,就连之前的IP访问方法也都失效,而Google Adsense打不开,恐怕做谷歌联盟的站长也要倒霉了 ...
- STS插件创建springboot项目,pom第一行报unkown错误
Description Resource Path Location TypeUnknown pom.xml /amq-provider line 1 ...
- Elasticsearch Java Low Level REST Client(嗅探器)
https://segmentfault.com/a/1190000016828977?utm_source=tag-newest#articleHeader0 嗅探器 允许从正在运行的Elastic ...
- 关于discuz论坛邮箱配置
Discuz后台可以进行邮件设置,实现网站自动发送邮件给用户的邮箱. 在Discuz邮件设置,经常使用25端口普通发送邮件.为了数据安全,我们也可以使用SSL加密发送,设置方法很简单,只需按照下图进行 ...
- Object中有哪些公用方法?
clone()方法 实现对象的浅复制,只有实现了Cloneable接口才能调用该方法. toString()方法 返回该对象的字符串表示. equals()方法: 在Object中与“==”的定义是一 ...
- Python链接liunx 带尝试
本文实例讲述了python下paramiko模块实现ssh连接登录Linux服务器的方法.分享给大家供大家参考.具体分析如下: python下有个paramiko模块,这个模块可以实现ssh登录lin ...
- xcopy忽略文件 7zip打包
xcopy src target /Y /r /d /k /f /s /exclude:%cd%\xcopy.config xcopy.config\assets\\tet\ 7z.exe a -tz ...
- git log的个性化设置
--date=(relative|local|default|iso|rfc|short|raw) Only takes effect for dates shown in human-readabl ...
- pspice中参数的意义
摘自:http://royroyyy.blog.163.com/blog/static/137650617201102610471196/ 有源器件在符号库中的名称(NAME)通常以关键字开头,后根长 ...
- Queue2链队列
链队列 1 #include <iostream> using namespace std; template <class T> class Queue { private: ...