E. Range Deleting
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array consisting of nn integers a1,a2,…,ana1,a2,…,an and an integer xx. It is guaranteed that for every ii, 1≤ai≤x1≤ai≤x.

Let's denote a function f(l,r)f(l,r) which erases all values such that l≤ai≤rl≤ai≤r from the array aa and returns the resulting array. For example, if a=[4,1,1,4,5,2,4,3]a=[4,1,1,4,5,2,4,3], then f(2,4)=[1,1,5]f(2,4)=[1,1,5].

Your task is to calculate the number of pairs (l,r)(l,r) such that 1≤l≤r≤x1≤l≤r≤x and f(l,r)f(l,r) is sorted in non-descending order. Note that the empty array is also considered sorted.

Input

The first line contains two integers nn and xx (1≤n,x≤1061≤n,x≤106) — the length of array aa and the upper limit for its elements, respectively.

The second line contains nn integers a1,a2,…ana1,a2,…an (1≤ai≤x1≤ai≤x).

Output

Print the number of pairs 1≤l≤r≤x1≤l≤r≤x such that f(l,r)f(l,r) is sorted in non-descending order.

Examples
input

Copy
3 3
2 3 1
output

Copy
4
input

Copy
7 4
1 3 1 2 2 4 3
output

Copy
6
Note

In the first test case correct pairs are (1,1)(1,1), (1,2)(1,2), (1,3)(1,3) and (2,3)(2,3).

In the second test case correct pairs are (1,3)(1,3), (1,4)(1,4), (2,3)(2,3), (2,4)(2,4), (3,3)(3,3) and (3,4)(3,4).

题意:有一个含有n个元素的数组,数组元素的范围是 1<=ai<=x,你可以删除值在(l,r)的元素,问有多少种方案使删除后数组成为非严格单调递增数组

题解:固定左边界L,则右边界呈现单调性,所以可以二分。判断条件为考虑删除(L,R)之后,(1)L左边的呈现非严格单调递增,(2)R右边呈现非严格单调递增,(3)并且原数组中比L左边某一元素大并且在该元素左边的最大值应<=R,预处理一下即可二分

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<"["<<#x<<"]"<<x<<endl;
const int maxn=1e6+;
const int inf=1e8;
int a[maxn],b[maxn],maxx2;
bool check(ll x0,ll len){
if(b[x0-]>x0+len-)return false;
if(x0+len-<maxx2)return false;
return true;
}
int main() {
int n,x;
scanf("%d%d",&n,&x);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
int maxx=a[n];
int minn=x;
for(int i=n-;i>=;i--){
if(maxx>=a[i]){
maxx=a[i];
}
else{
minn=min(a[i],minn);
}
}
maxx=a[];
for(int i=;i<=n;i++){
if(maxx>a[i]){
b[a[i]]=max(b[a[i]],maxx);
maxx2=max(maxx2,a[i]);
}
else{
maxx=a[i];
}
}
for(int i=;i<=x;i++){
b[i]=max(b[i],b[i-]);
}
ll aans=;
for(int i=;i<=minn;i++){
ll l=;
ll r=x-i+;
ll ans=-;
while(l<=r){
ll mid=(l+r)/;
if(check(i,mid)){
ans=mid;
r=mid-;
}
else{
l=mid+;
}
}
if(ans!=-){
aans+=x-(i+ans-)+;
}
}
printf("%lld\n",aans);
return ;
} /*
*/

[ Educational Codeforces Round 65 (Rated for Div. 2)][二分]的更多相关文章

  1. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  2. Educational Codeforces Round 65 (Rated for Div. 2) D. Bicolored RBS

    链接:https://codeforces.com/contest/1167/problem/D 题意: A string is called bracket sequence if it does ...

  3. Educational Codeforces Round 65 (Rated for Div. 2) C. News Distribution

    链接:https://codeforces.com/contest/1167/problem/C 题意: In some social network, there are nn users comm ...

  4. Educational Codeforces Round 65 (Rated for Div. 2) B. Lost Numbers

    链接:https://codeforces.com/contest/1167/problem/B 题意: This is an interactive problem. Remember to flu ...

  5. Educational Codeforces Round 65 (Rated for Div. 2) A. Telephone Number

    链接:https://codeforces.com/contest/1167/problem/A 题意: A telephone number is a sequence of exactly 11  ...

  6. Educational Codeforces Round 65 (Rated for Div. 2)B. Lost Numbers(交互)

    This is an interactive problem. Remember to flush your output while communicating with the testing p ...

  7. Educational Codeforces Round 65 (Rated for Div. 2)

    A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 ...

  8. Educational Codeforces Round 65 (Rated for Div. 2) E. Range Deleting(思维+coding)

    传送门 参考资料: [1]:https://blog.csdn.net/weixin_43262291/article/details/90271693 题意: 给你一个包含 n 个数的序列 a,并且 ...

  9. Educational Codeforces Round 65 (Rated for Div. 2)(ACD)B是交互题,不怎么会

    A. Telephone Number A telephone number is a sequence of exactly 11 digits, where the first digit is  ...

随机推荐

  1. LeetCode 279. 完全平方数(Perfect Squares) 7

    279. 完全平方数 279. Perfect Squares 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数 ...

  2. Mybatis应用入门

    mybatis简介 Mybatis是在jdbc的基础之上封装而成的持久层框架. Mybatis是一个ORM框架.ORM(object relational mapping):对象关系型映射 搭建myb ...

  3. 如何将本地的项目添加到github上

    参考链接:http://note.youdao.com/noteshare?id=d0b7990a83b024b0172b6d5c5617a8d0&sub=659F216B9046420991 ...

  4. Golang的安装与环境配置(包括Go lint、Go imports、Go fmt)

    Golang安装 下载地址:https://studygolang.com/dl Go语言中文网 下载后安装,win10系统中会自动配置大部分设置,linux系统请参照网上教程 GO环境变量配置: $ ...

  5. session和cookie有什么区别?

    1.存储位置不同 cookie的数据信息存放在客户端浏览器上. session的数据信息存放在服务器上. 2.存储容量不同 单个cookie保存的数据<=4KB,一个站点最多保存20个Cooki ...

  6. 如何加入 Skype for Business 会议?

    参加一个线上培训,收到了Skype的参会地址,是这个样子的 然后就是一脸懵逼的不知道怎么参加会议了.找了半天终于在同事的帮助下参加成功. 我的参加方法:在Window上用Skype for Busin ...

  7. hdu 6143第二类striling

    题意:有m种字符,要求构造两段长度为n的字符串,其中这两段不能有相同的字符 枚举左边选了i种字符,右边可以选1,2....min(n,m-i)种字符 这样就把问题转化为用k种字符构造n长度的字符串的种 ...

  8. C#正则表达式根据分组命名取值

    string[] regexList = new string[] { @"^(?<TickerPart1>[0-9A-Z])[ 0_]?(?<TickerPart2> ...

  9. HTML的发展历史

    HTML是Web统一语言,这些容纳在尖括号里的简单标签,构成了如今的Web,1991年,Tim Berners-Lee编写了一份叫做“HTML标签”的文档,里面包含了大约20个用来标记网页的HTML标 ...

  10. 30分钟用Restful ABAP Programming模型开发一个支持增删改查的Fiori应用

    2016年时,Jerry曾经写过一系列关于SAP Fiori Smart Template(现在更名为Fiori Elements了)的博客,介绍了所谓的MDD开发方法论 - Metadata Dri ...