传送门

参考资料:

  [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呢?

  首先看看相关变量解释:

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

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

  1. mem(l,INF);
  2. mem(r,);
  3. for(int i=;i <= n;++i)
  4. {
  5. l[a[i]]=min(i,l[a[i]]);
  6. r[a[i]]=i;
  7. }
  8. mem(L,INF);
  9. mem(R,);
  10. for(int i=x;i >= ;--i)
  11. L[i]=min(L[i+],l[i]);
  12. for(int i=;i <= x;++i)
  13. R[i]=max(R[i-],r[i]);

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

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

  如何求解k1呢?

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

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

  如何根据 k1 递推出 ki 呢?

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

AC代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define mem(a,b) memset(a,b,sizeof(a))
  4. #define ll long long
  5. #define INF 0x3f3f3f3f
  6. const int maxn=1e6+;
  7.  
  8. int n,x;///max{a[i]} <= x
  9. int a[maxn];
  10. int l[maxn];///l[i]:数字i第一次出现的位置
  11. int r[maxn];///r[i]:数字i最后一次出现的位置
  12. int L[maxn];///L[i]:数字[i,n]最先出现的位置
  13. int R[maxn];///R[i]:数字[1,i]最后出现的位置
  14.  
  15. ll Solve()
  16. {
  17. mem(l,INF);
  18. mem(r,);
  19. for(int i=;i <= n;++i)
  20. {
  21. l[a[i]]=min(i,l[a[i]]);
  22. r[a[i]]=i;
  23. }
  24. mem(L,INF);
  25. mem(R,);
  26. for(int i=x;i >= ;--i)
  27. L[i]=min(L[i+],l[i]);
  28. for(int i=;i <= x;++i)
  29. R[i]=max(R[i-],r[i]);
  30.  
  31. int k=x;///如果[k,x]非递减,那么k-1找下一个位置
  32. for(;k > && r[k] <= L[k+];--k);
  33.  
  34. ll ans=x-k+;
  35.  
  36. ///要确保[1,i-1]非递减
  37. ///且已知[k+1,x]非递减
  38. for(int i=;i <= x && R[i-] <= l[i-];++i)
  39. {
  40. for(;k < i || R[i-] > L[k+];++k);///找到第一个使得R[i-1]>=L[k+1]的k
  41.  
  42. ans += x-k+;
  43. }
  44. return ans;
  45. }
  46. int main()
  47. {
  48. // freopen("C:\\Users\\hyacinthLJP\\Desktop\\in&&out\\contest","r",stdin);
  49. scanf("%d%d",&n,&x);
  50. for(int i=;i <= n;++i)
  51. scanf("%d",a+i);
  52.  
  53. printf("%lld\n",Solve());
  54. return ;
  55. }

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. 使用fast-json-stringify代替JSON.stringify

    使用JSON.stringify的思考 使用过JSON对象的程序员最常做的一项工作便是,将JSON对象转化为字符串.该字符串的用途很多,例如可以使用在WEB的URL中,在多个页面间进行传递. cons ...

  2. “获取access_token”接口新增IP白名单保护

  3. python 正确地初始化对象

  4. python 模块的导入

  5. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二)

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二) 代码工程地址: https:/ ...

  6. phpcms分类信息地区识别跳转

    <script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"></scri ...

  7. 【NS2】NS2在ubuntu下的安装

    Step1: 更新系统.在终端输入如下命令 sudo apt-get  update #更新源列表sudo apt-get upgrade #更新已经安装的包sudo apt-get dist-upg ...

  8. 巨蟒python全栈开发-第11阶段 ansible3_4

    1.ansible  roles 2.nginx+uwsgi扩展 3.celery异步任务 4.celery延时任务 5.周期任务 6.celery与django结合 7.网络基础 8.celery监 ...

  9. XML内部DTD约束 Day24

    <?xml version="1.0" encoding="UTF-8"?> <!-- 内部DTD --> <!-- XML:ex ...

  10. @总结 - 1@ 多项式乘法 —— FFT

    目录 @0 - 参考资料@ @1 - 一些概念@ @2 - 傅里叶正变换@ @3 - 傅里叶逆变换@ @4 - 迭代实现 FFT@ @5 - 参考代码实现@ @6 - 快速数论变换 NTT@ @7 - ...