题目链接: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)(差分+思维)的更多相关文章

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

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

  2. codeforces#1108E2. Array and Segments (线段树+扫描线)

    题目链接: http://codeforces.com/contest/1108/problem/E2 题意: 给出$n$个数和$m$个操作 每个操作是下标为$l$到$r$的数减一 选出某些操作,使$ ...

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

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

  5. 【Codeforces 1108E1】Array and Segments (Easy version)

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 枚举最大值和最小值在什么地方. 显然,只要包含最小值的区间,都让他减少. 因为就算那个区间包含最大值,也无所谓,因为不会让答案变小. 但是那些 ...

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

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

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

  8. Codeforces 1108E (Array and Segments) 线段树

    题意:给你一个长度为n的序列和m组区间操作,每组区间操作可以把区间[l, r]中的数字都-1,请选择一些操作(可以都不选),使得序列的最大值和最小值的差值尽量的大. 思路:容易发现如果最大值和最小值都 ...

  9. CF1108E2 Array and Segments (Hard version)

    线段树 对于$Easy$ $version$可以枚举极大值和极小值的位置,然后判断即可 但对于$Hard$ $version$明显暴力同时枚举极大值和极小值会超时 那么,考虑只枚举极小值 对于数轴上每 ...

随机推荐

  1. 用v-bind:style时的问题

    今天纠结了挺久一个问题,个人习惯是在HBuilder里先写好前端样式,在放.net去测试数据,但是发现一个问题 就是一个提示框跟随鼠标移动 提示框用v-bind:style绑定一个对象 DIV就是这句 ...

  2. OSS上传文件到阿里云

    最近做项目,需要上传文件,因为上传到项目路径下,感觉有时候也挺不方便的,就试了一下上传文件到阿里云oss上去了, oss的使用网上有很多介绍,都是去配置一下需要的数据,然后直接调用他的api就可以了. ...

  3. 2019/1.7 js面向对象笔记

    面向对象 1.构造函数里的属性怎么看?看this,谁前面有this谁就是属性. num不是属性,是私有作用域下的私有变量. 2.如何查找面向对象中的this 1.构造函数的this指向实例对象 2.如 ...

  4. nginx系列6:nginx的进程结构

    nginx的进程结构 如下图: 通过ps –ef | grep nginx可以看到共有三个进程,一个master进程,两个worker进程. nginx是多进程结构,多进程结构设计是为了保证nginx ...

  5. 折腾Java设计模式之观察者模式

    观察者模式 Define a one-to-many dependency between objects where a state change in one object results in ...

  6. sql 服务启动失败 SQL Server(MSSQLSERVER) 错误码126

    SQL配置管理器-->sql server 网络配置-->mssqlerver的协议-->VIA禁用服务

  7. Django 传递额外参数及 URL别名

    传递额外参数到视图函数中 在 urls.py 文件中添加下面内容 from django.conf.urls import url urlpatterns = [ url(r'index', view ...

  8. 重拾《 两周自制脚本语言 》- Eclipse插件实现语法高亮

    源码库: program-in-chinese/stone-editor-eclipse 参考: FAQ How do I write an editor for my own language? D ...

  9. Dynamics 365 启用跟踪及读取跟踪文件工具

    微软动态CRM专家罗勇 ,回复315或者20190313可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 当根据错误提示排查问 ...

  10. ArcGIS API for JavaScript与 npm

    在4.7版本中,不仅增加了WebGL的渲染支持(渲染前端速度加快,渲染量也加大).增强了ES6中的Promises语法支持,还支持了npm管理及webpack打包,实属喜讯. “意味着可以不经过esr ...