Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)
题目链接:Array and Segments (Hard version)
题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大。
题解:利用差分的思想,并且考虑到m比较小,遍历一遍序列,当前点遇到需要改变的时候进行操作,同时更新答案。
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std; #define eps 1e-8
#define pb push_back
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define clr(a,b) memset(a,b,sizeof(a)
#define bugc(_) cerr << (#_) << " = " << (_) << endl
#define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) const int N=1e5+;
typedef long long ll;
typedef unsigned long long ull;
int a[N],l[N],r[N];
vector <int> sub[N],add[N]; int main(){
int mxt=-INF,mit=INF;
int n,m,ans=-INF,ansid=,cnt=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
mit=min(mit,a[i]);mxt=max(mxt,a[i]);
}
ans=mxt-mit;
for(int i=;i<=m;i++){
scanf("%d%d",&l[i],&r[i]);
sub[l[i]].push_back(i);
add[r[i]+].push_back(i);
}
for(int i=;i<=n;i++){
for(int j=;j<sub[i].size();j++){
int id=sub[i][j];
for(int k=l[id];k<=r[id];k++) a[k]--;
}
for(int j=;j<add[i].size();j++){
int id=add[i][j];
for(int k=l[id];k<=r[id];k++) a[k]++;
}
if(sub[i].size()||add[i].size()){
int mi=INF,mx=-INF;
for(int j=;j<=n;j++) mi=min(mi,a[j]),mx=max(mx,a[j]);
if(ans<mx-mi){
ans=mx-mi;
ansid=i;
}
}
}
printf("%d\n",ans);
for(int i=;i<=m;i++){
if(l[i]<=ansid&&r[i]>=ansid) cnt++;
}
printf("%d\n",cnt);
for(int i=;i<=m;i++){
if(l[i]<=ansid&&r[i]>=ansid) printf("%d ",i);
}
return ;
}
Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)的更多相关文章
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- codeforces#1108E2. Array and Segments (线段树+扫描线)
题目链接: http://codeforces.com/contest/1108/problem/E2 题意: 给出$n$个数和$m$个操作 每个操作是下标为$l$到$r$的数减一 选出某些操作,使$ ...
- 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 ...
- Array and Segments (Easy version) CodeForces - 1108E1 (暴力枚举)
The only difference between easy and hard versions is a number of elements in the array. You are giv ...
- 【Codeforces 1108E1】Array and Segments (Easy version)
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 枚举最大值和最小值在什么地方. 显然,只要包含最小值的区间,都让他减少. 因为就算那个区间包含最大值,也无所谓,因为不会让答案变小. 但是那些 ...
- 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 1108E (Array and Segments) 线段树
题意:给你一个长度为n的序列和m组区间操作,每组区间操作可以把区间[l, r]中的数字都-1,请选择一些操作(可以都不选),使得序列的最大值和最小值的差值尽量的大. 思路:容易发现如果最大值和最小值都 ...
- CF1108E2 Array and Segments (Hard version)
线段树 对于$Easy$ $version$可以枚举极大值和极小值的位置,然后判断即可 但对于$Hard$ $version$明显暴力同时枚举极大值和极小值会超时 那么,考虑只枚举极小值 对于数轴上每 ...
随机推荐
- WPF 自定义 ImageButton
控件源码: public class ImageButton : Button { public ImageButton() { } public string No ...
- 成为一名Java架构师的必修课
一.热门框架源码学习 设计模式篇 Spring5源码解读篇 Mybatis篇 SpringBoot2篇 二. 微服务架构 架构设计篇 BAT互联网架构这些年的演进分析 国内外常见分布式系统架构状况介绍 ...
- 中介者模式 调停者 Mediator 行为型 设计模式(二十一)
中介者模式(Mediator) 调度.调停 意图 用一个中介对象(中介者)来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散 而且可以独立地改变它们之间的交互. ...
- 通过JS动态的修改HTML元素的样式和增添标签元素等
一. 通过JS动态的修改HTML元素的样式 1. 要想在js中动态的修改HTML元素的样式,首先需要写document, document我们称之为文档对象,这个对象中保存了当前网页中所有的 ...
- numpy 基础操作
Numpy 基础操作¶ 以numpy的基本数据例子来学习numpy基本数据处理方法 主要内容有: 创建数组 数组维度转换 数据选区和切片 数组数据计算 随机数 数据合并 数据统计计算 In [1]: ...
- ILRuntime入门笔记
基础知识 官方地址:https://github.com/Ourpalm/ILRuntime 官方文档:https://ourpalm.github.io/ILRuntime/ 文档Markdown源 ...
- Scrapy框架-Item Pipeline
目录 1. Item Pipeline 3. 完善之前的案例: 3.1. item写入JSON文件 3.2. 启用一个Item Pipeline组件 3.3. 重新启动爬虫 1. Item Pipel ...
- Storm入门(一)原理介绍
问题导读:1.hadoop有master与slave,Storm与之对应的节点是什么?2.Storm控制节点上面运行一个后台程序被称之为什么?3.Supervisor的作用是什么?4.Topology ...
- 从Python越来越想放弃的Day09
今天在学几个新东东,又向py迈了一大步,依旧是从简单的开始,三元运算,又称三目运算,所谓三目,也就是二郎神,跑题了,简单的格式为v = 前面 if 条件 else 后面,条件为True时,则v = 前 ...
- BAT面试题:请使用递归构建N叉树
题目要求: 现在我们拥有全国的省.市.县.镇的行政信息,比如 浙江省 -> 杭州市 -> 西湖区 --> xx街道,请将这些信息构建成一棵树,根节点为全国,叶子节点为镇. 我的误解: ...