cf题目链接
题意简述:原有一序列p,p中元素为[1,n],通过q[i]=max(p[1],p[2]…,p[i])得到一个新的序列q,现给出q,问满足规则的字典序最大和最小的p序列分别是什么

解法:通过规则,我们可以确定的是在q序列中第一次出现的数x,x在原序列p中也处于该位置(q[i]=max(p[1],p[2]…,p[i]))
字典序最小:我们把1-n中除了出现在q序列的数从小到大填入p序列的空位即可
字典序最大:我们先将确定好的q序列的数填入了p序列,那么我们将小于要填位置之前的第一个非空p序列的数从大到小填入即可

AcCode:

#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<set>#define int long longusing namespace std;const int N = 3e5 + 100;int arr1[N], arr2[N], vis[N];signed main() {ios::sync_with_stdio(false);int t;cin >> t;while (t--) {int n;cin >> n;set<int> st;//记录q序列的数set<int> init;//记录通过q序列确定的位置set<int> num;//记录未填入的1-n的数for (int i = 1; i <= n; i++) {cin >> arr1[i];arr2[i] = arr1[i];if (arr1[i] != arr1[i - 1]) {vis[i] = 1;st.insert(arr1[i]);init.insert(i);}}for (int i = 1; i <= n; i++) if (!st.count(i)) num.insert(i);set<int>::iterator iter;iter = num.begin();for (int i = 1; i <= n && iter != num.end(); i++) {//字典序最小if (vis[i]) continue;arr1[i] = *iter;iter++;}int rem = 0;for (int i = 1; i <= n; i++) {//字典序最大if (vis[i]) {rem = arr2[i];continue;}auto v = lower_bound(num.begin(), num.begin(), rem);iter = num.lower_bound(rem);if (iter != num.begin()) iter--;//得到未填入的符合条件的最大数arr2[i] = *iter;num.erase(iter);}for (int i = 1; i <= n; i++) cout << arr1[i] << " ";cout << endl;for (int i = 1; i <= n; i++) cout << arr2[i] << " ";cout << endl;for (auto v : init) vis[v] = 0;}
}

Restoring the Permutation相关推荐

  1. E. Restoring the Permutation

    E. Restoring the Permutation Example input 4 7 3 3 4 4 7 7 7 4 1 2 3 4 7 3 4 5 5 5 7 7 1 1 output 3 ...

  2. Restoring the Permutation CodeForces - 1506E

    题目链接:E. Restoring the Permutation 题解:按字典序最小的简单一点,字典序大的较为复杂,找出字典序最大的可以通过栈,如果当前的a[i]值和a[i-1]值不相等,说明b[i ...

  3. 【竞赛题解】Codeforces Round #710 (Div. 3)

    B. Partial Replacement 题意:有字符串由.和*组成,可标记其中*,首尾的*必须被标记,使被标记的*之间距离不超过k,求最少的标记量 思路:首先从首尾出发确定首尾*的位置,再由首b ...

  4. Codeforces Round #710 (Div. 3)

    文章目录 A. Strange Table 题意 解题思路 Code B. Partial Replacement 题意 解题思路 Code C. Double-ended Strings 题意 解题 ...

  5. Codeforces Round #710 (Div. 3) ABCDE 题解

    我的博客园传送门,看的方便些 A. Strange Table 签到题,算出对应行列即可. view code #include<iostream> #include<string& ...

  6. Codeforeces #710 div3题解报告

    A. Strange Table 题意:给你一个矩阵(n*m)的.这个矩阵从上到下由1,2,3````n * m填满. 再给你一个数x.问你在另一个矩阵(也是n * m,不过是从水平填起),这个x的位 ...

  7. Codeforces Round #710 (Div. 3)个人题解

    Codeforces Round #710 (Div. 3) 文章目录 [Codeforces Round #710 (Div. 3)](https://codeforces.com/contest/ ...

  8. Restoring Permutation CodeForces - 1315C(思维)

    You are given a sequence b1,b2,-,bn. Find the lexicographically minimal permutation a1,a2,-,a2n such ...

  9. C. Restoring Permutation

    链接:https://codeforces.ml/contest/1315/problem/C You are given a sequence b1,b2,-,bnb1,b2,-,bn. Find ...

最新文章

  1. 如何使用OSI模型排除故障
  2. bubbliiiing/keras-face-recognition
  3. WMI CIM studio无法连接解决 在XP下wmi取不到值可巧用wmic取值
  4. js正则表达式test()和exec()用法
  5. 如何在Ubuntu里安装Helm
  6. php内置函数数组函数,PHP 数组排序内置函数
  7. 分布式Session共享解决方案
  8. TCP协议的三次握手和四次分手
  9. JavaScript从入门到放弃 - ES6中的对象和类
  10. 用户使用报告_【市场】最新手机用户使用报告 8+256GB成首选 小屏党哭了
  11. C#订阅与发布标准实现
  12. 使用Python PyQt5实现一个简单的图像识别软件
  13. 个性时钟屏保=冷高轮时间麻将时钟电脑屏幕保护
  14. Jib使用小结(Maven插件版)
  15. methodinvocationexception: property ‘driverclassname‘ threw exception; nested exception is java.lang
  16. 联想小新air14和联想pro14哪个好
  17. 宠物小精灵之收服 01背包 POJ4102
  18. Ural1671 Anansi's Cobweb
  19. MySQL基础学习(六)——聚合函数
  20. SQL注入攻击以及防护

热门文章

  1. 【愚公系列】2022年11月 微信小程序-优购电商项目-意见反馈页面
  2. 电力巡检无人机助力迎峰度夏,保障夏季电力供应
  3. 光伏整县推进,农网重构进行时——福建低压400V并网,有线2M信号接入调度通信
  4. 英伟达显卡监控工具nvtop使用方法
  5. 音频服务未启动的症状及解决办法
  6. 成都榆熙电子商务有限公司:提升产品关键词质量分的技巧你都知道哪些?
  7. @ExceptionHandler全局异常捕获响应体返回中文乱码
  8. Java8后 日期时间格式化及解析
  9. SX1276/77/78学习笔记5 - sx1278数据包结构
  10. python中常用的加密算法