传送门

参考资料:

  [1]:https://blog.csdn.net/weixin_43262291/article/details/90271693

题意:

  给你一个包含 n 个数的序列 a,并且 max{ai} ≤ x;

  定义一个操作 f(L,R) 将序列 a 中  L ≤ ai ≤ R 的数删除;

  问有多少对满足条件的 (L,R) 使得执行完 f(L,R) 操作后的序列非递减;

题解:

  [1]博文看了一晚上,终于理解了;

  枚举左区间 i,找到符合条件的最小的右区间 ki,f(1,k1),f(2,k2),....,f(x,kx);

  如果执行完 f(1,k1) 后序列非递减,那么执行完 f(1,k1+1),f(1,k1+2),....,f(1,x) 后同样会使得序列非递减;

  f(i,ki)同理,那么最终答案就是 (x-k1+1)+(x-k2+1)+......+(x-kx+1);

  如何高效的求解k1,k2,....,kx呢?

  首先看看相关变量解释:

int n,x;///max{a[i]} <= x
int a[maxn];
int l[maxn];///l[i]:数字i第一次出现的位置
int r[maxn];///r[i]:数字i最后一次出现的位置
int L[maxn];///L[i]:数字[i,n]最先出现的位置
int R[maxn];///R[i]:数字[1,i]最后出现的位置

  预处理出l,r,L,R数组:

 mem(l,INF);
mem(r,);
for(int i=;i <= n;++i)
{
l[a[i]]=min(i,l[a[i]]);
r[a[i]]=i;
}
mem(L,INF);
mem(R,);
for(int i=x;i >= ;--i)
L[i]=min(L[i+],l[i]);
for(int i=;i <= x;++i)
R[i]=max(R[i-],r[i]);

  明确一点,k1 ≤ k2 ≤ ...... ≤ kx

  那么,首先求出 k1,然后,递推出 ki

  如何求解k1呢?

  上述查找可转化为找最小的 k1 使得 [k1+1,x] 组成的序列非递减;

int k=x;///如果[k,x]非递减,那么k-1找下一个位置
for(;k > && r[k] <= L[k+];--k);

  如何根据 k1 递推出 ki 呢?

for(int i=;i <= x && R[i-] <= l[i-];++i)
for(;k < i || R[i-] > L[k+];++k);///找到第一个使得R[i-1]>=L[k+1]的k

AC代码:

 #include<bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define INF 0x3f3f3f3f
const int maxn=1e6+; int n,x;///max{a[i]} <= x
int a[maxn];
int l[maxn];///l[i]:数字i第一次出现的位置
int r[maxn];///r[i]:数字i最后一次出现的位置
int L[maxn];///L[i]:数字[i,n]最先出现的位置
int R[maxn];///R[i]:数字[1,i]最后出现的位置 ll Solve()
{
mem(l,INF);
mem(r,);
for(int i=;i <= n;++i)
{
l[a[i]]=min(i,l[a[i]]);
r[a[i]]=i;
}
mem(L,INF);
mem(R,);
for(int i=x;i >= ;--i)
L[i]=min(L[i+],l[i]);
for(int i=;i <= x;++i)
R[i]=max(R[i-],r[i]); int k=x;///如果[k,x]非递减,那么k-1找下一个位置
for(;k > && r[k] <= L[k+];--k); ll ans=x-k+; ///要确保[1,i-1]非递减
///且已知[k+1,x]非递减
for(int i=;i <= x && R[i-] <= l[i-];++i)
{
for(;k < i || R[i-] > L[k+];++k);///找到第一个使得R[i-1]>=L[k+1]的k ans += x-k+;
}
return ans;
}
int main()
{
// freopen("C:\\Users\\hyacinthLJP\\Desktop\\in&&out\\contest","r",stdin);
scanf("%d%d",&n,&x);
for(int i=;i <= n;++i)
scanf("%d",a+i); printf("%lld\n",Solve());
return ;
}

Educational Codeforces Round 65 (Rated for Div. 2) E. Range Deleting(思维+coding)的更多相关文章

  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)][二分]

    https://codeforc.es/contest/1167/problem/E E. Range Deleting time limit per test 2 seconds memory li ...

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

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

  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. POJ 1845 (洛谷 :题目待添加)Sumdiv

    约数和 题目描述 给出a和b求a^b的约数和. 输入格式: 一行两个数a,b. 输出格式: 一个数表示结果对 9901 的模. Input 2 3 Output 15 SB的思路: 这是一道典型的数论 ...

  2. 【滴水石穿】rn

    这个项目还不错,还比较全 先放项目地址:https://github.com/ShionHXC/rn 项目算是一个完整的APP 有用到redux-thunk存储数据,算的上是一个普通的比较完整的APP ...

  3. re模块相关

    一.正则表达式中的转义: "\" 表示转义符 [()+*?/$.] 在字符组中一些特殊的字符会现出原形 所有的\w \d \s (\n,\t) \W \D \S 都表示它原本的意义 ...

  4. 2018-9-2-WPF-开发自动开机启动程序

    title author date CreateTime categories WPF 开发自动开机启动程序 lindexi 2018-09-02 15:10:52 +0800 2018-9-2 14 ...

  5. 网络流24题 负载平衡(DCOJ8013)

    题目描述 G 公司有 n nn 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n nn 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入格式 文件 ...

  6. JavaScript--天猫数量输入框

    <!DOCTYPE html> <head> <meta charset="utf-8" /> <title>无标题文档</t ...

  7. UVA_490:Rotating Sentences

    "R  Ie   n  te  h  iD  ne  kc  ,a   r  tt  he  es  r  eo  fn  oc  re  e   s  Ia   i  ad  m,  .  ...

  8. UVA_401:Palindromes

    AC:Time(29ms)     C++ 4.8.2 #include<stdio.h> #include<string.h> char * mirstr = "A ...

  9. shell学习(15)- eval及shell No such file or directory解决办法

    eval可以读取一连串的参数,然后按照参数特性来执行.参数数目不限,彼此之间用分号隔开. eval会对后面的命令进行两遍扫描,如果第一遍扫描后,命令是个普通命令,则执行此命令:如果命令中含有变量的间接 ...

  10. JavaScript for循环 while循环

    循环可以将代码块执行指定的次数. JavaScript 循环 如果您希望一遍又一遍地运行相同的代码,并且每次的值都不同,那么使用循环是很方便的. 我们可以这样输出数组的值: 一般写法: documen ...