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)的更多相关文章

  1. 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 ...

  2. CF #535 (Div. 3) E2 Array and Segments (Hard version) 利用线段树进行区间转移

    传送门 题意:    有m个区间,n个a[ i ] , 选择若干个区间,使得整个数组中的最大值和最小值的差值最小.n<=1e5,m<=300; 思路: 可以知道每个i,如果一个区间包含这个 ...

  3. 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 ...

  4. CF E2 - Array and Segments (Hard version) (线段树)

    题意给定一个长度为n的序列,和m个区间.对一个区间的操作是:对整个区间的数-1可以选择任意个区间(可以为0个.每个区间最多被选择一次)进行操作后,要求最大化的序列极差(极差即最大值 - 最小值).ea ...

  5. Codeforces Round #535 (Div. 3) 题解

    Codeforces Round #535 (Div. 3) 题目总链接:https://codeforces.com/contest/1108 太懒了啊~好久之前的我现在才更新,赶紧补上吧,不能漏掉 ...

  6. Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

    Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...

  7. Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]

    hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...

  8. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  9. Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)

    题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...

随机推荐

  1. P.W.N. CTF - Web - Login Sec

    题目 链接:https://ctftime.org/task/6934 题解 Login 1 题目给出了源码 var http = require('http'); const crypto = re ...

  2. (转)nginx+redis实现接入层高性能缓存技术

    转自:https://blog.csdn.net/phil_code/article/details/79154271 1. OpenRestyOpenResty是一个基于 Nginx与 Lua的高性 ...

  3. man VGCREATE

    VGCREATE(8)                                                        VGCREATE(8) NAME/名称       vgcreat ...

  4. Jmeter参数化控件意见收集

    1.可以读取EXCEL,可以自定义SHEET,行和列: 2.数据可以加密传输,加密方式如下: 1)SHA1 2)SHA224 3)SHA256 4)SHA384 5)SHA512 6)MD5 7)Hm ...

  5. php substr()函数 语法

    php substr()函数 语法 作用:截取字符串 语法:substr(string,start,length)大理石平台 参数: 参数 描述 string 必需.规定要返回其中一部分的字符串. s ...

  6. 【2019 Multi-University Training Contest 5】

    01: 02:https://www.cnblogs.com/myx12345/p/11649221.html 03: 04:https://www.cnblogs.com/myx12345/p/11 ...

  7. 如何扩展 Create React App 的 Webpack 配置

    如何扩展 Create React App 的 Webpack 配置  原文地址https://zhaozhiming.github.io/blog/2018/01/08/create-react-a ...

  8. 纯css实现瀑布流(multi-column多列及flex布局)

    瀑布流的布局自我感觉还是很吸引人的,最近又看到实现瀑布流这个做法,在这里记录下,特别的,感觉flex布局实现瀑布流还是有点懵的样子,不过现在就可以明白它的原理了 1.multi-column多列布局实 ...

  9. Centos7开机自动启动服务和联网

    虚拟机设置选择NAT模式,默认情况下,Centos不是自动连接上网的,需要点击右上角,手动连接上网. 可以修改开机启动配置修改: 1. cd 到/etc/sysconfig/network-scrip ...

  10. ruby异常处理

    begin # 这段代码抛出的异常将被下面的 rescue 子句捕获 rescue # 这个块将捕获所有类型的异常 retry # 这将把控制移到 begin 的开头 end