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个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...
随机推荐
- 2018-08-15-weekly
Algorithm 5. Longest Palindromic Substring What 给定一个字符串s,找到s中最长的回文子字符串. 给定s的最大长度为1000. How 这是一道比较经典的 ...
- DispatcherServlet的工作原理
下面是DispatcherServlet的工作原理图,图片来源于网络. 下面是我从DispatcherServlet源码层面来分析其工作流程: 1.请求到达后,调用HandlerMapping来查找对 ...
- 一个奇怪的问题:Last_Errno: 1264 Error 'Out of range value for column 0x322E36343030
场景环境: 1. 主从都是:Server version: 5.7.16-log MySQL Community Server (GPL) 2.操作系统:CentOS release 6.7 (Fin ...
- vuex的mapState方法来获取vuex的state对象中属性
有两种写法 1.首先在组件中引入vuex的mapState方法: 首先在组件中引入vuex的mapState方法: import { mapState } from 'vuex' 然后在compute ...
- python基本数据预处理语法函数(1)
numpy包: ####数组###########from numpy import * shape #获取维度 size #获取长度 arange(0,5,1) #生成数组函数,从0到5以1为间隔 ...
- 各种IO之间的区别
- python线程池示例
使用with方式创建线程池,任务执行完毕之后,会自动关闭资源 , 否则就需要手动关闭线程池资源 import threading, time from concurrent.futures impo ...
- php substr()函数 语法
php substr()函数 语法 作用:截取字符串 语法:substr(string,start,length)大理石平台 参数: 参数 描述 string 必需.规定要返回其中一部分的字符串. s ...
- java文件断点上传
1,项目调研 因为需要研究下断点上传的问题.找了很久终于找到一个比较好的项目. 在GoogleCode上面,代码弄下来超级不方便,还是配置hosts才好,把代码重新上传到了github上面. http ...
- 十一、atomic、async深入
一.原子操作 g++; g+=1; g = g+1;//结果不对 一般原子操作针对++,--,+=,&=,|=,^=是支持的,其他的可能不支持 二.std::async深入 用来创建异步任务. ...