当前位置:网站首页>Blue Bridge Cup group 2021a - two way sorting
Blue Bridge Cup group 2021a - two way sorting
2022-06-13 09:40:00 【Python's path to becoming a God】


analysis
Want to get full marks , We must not adopt conventional ideas , The problem needs to be simplified , Otherwise, the time limit will be exceeded
Simplification :
- The first time must be “0”, Otherwise, it doesn't make sense
- Two consecutive identical operations , Choose a wide range of
- It can be seen from the above that , Operation must be “0101” Alternate , If the current operation is larger than the previous same operation , Then the previous two operations will be invalid ,eg:“101”->“0”
Thus, the partial boundary can be determined according to each operation , In ascending order, the left end is determined , In descending order, the right end is determined , Finally, determine the middle part according to the number of operations .
Code
#include <iostream>
using namespace std;
const int N = 100010;
pair<int, int> stk[N];
int ans[N];
int main() {
int n, m;
int p, q;
int top = 0;//stk Top pointer of stack
cin >> n >> m;
// Compression adjustment times
while (m--) {
cin >> p >> q;
if (p == 0) {
if (top && stk[top].first == 0) {
// Simplified twice is 0 The situation of
//00
q = max(q, stk[top--].second);
}
while (top >= 2 && stk[top-1].second <= q) {
// If the current operation is larger than the previous same operation , Then the previous two operations will be invalid
//010
top -= 2;
}
stk[++top] = {
0, q}; // Save operation
}
else if (top) {
if (top && stk[top].first == 1) {
// Simplified twice is 0 The situation of
//11
q = min(q, stk[top--].second);
}
while (top >= 2 && stk[top-1].second >= q) {
// If the current operation is larger than the previous same operation , Then the previous two operations will be invalid
//010
top -= 2;
}
stk[++top] = {
1, q}; // Save operation
}
}
int left = 1, right = n, k = n;
for (int i = 1; i <= top; i++) {
// Determine both ends
if (stk[i].first == 0) {
while (right > stk[i].second && left <= right) {
ans[right--] = k--;
}
}
else {
while (left < stk[i].second && left <= right) {
ans[left++] = k--;
}
}
if (left > right) {
break;
}
}
// Deal with the intermediate
if (top % 2) {
// The last sort is ascending
while (left <= right) {
ans[left++] = k--;
}
}
else {
// The last sort is descending
while (left <= right) {
ans[right--] = k--;
}
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
return 0;
}
reference :https://www.bilibili.com/video/BV1pY41187dZ?spm_id_from=333.1007.top_right_bar_window_view_later.content.click
边栏推荐
猜你喜欢

Consolas-with-Yahei

(dfs+ tree DP) acwing 846 Center of gravity of tree
![[51nod p3216] Award [bit operation]](/img/f2/109fb126d026951ffd766781223052.jpg)
[51nod p3216] Award [bit operation]
![[51nod P3210] binary statistics](/img/a0/3fd197107336b10ea0a996f6b6ab58.jpg)
[51nod P3210] binary statistics

(Dijkstra + shortest circuit + by point n^2) acwing 849 Dijkstra finding the shortest path I
![[ssl1280] full arrangement](/img/58/85c456127a406bf5b30ee1d204981d.jpg)
[ssl1280] full arrangement

云计算企业崛起 甲骨文数据库市场主导地位动摇

Figure: concept of figure

acwing 788. Number of pairs in reverse order

简述请求过程
随机推荐
[51nod p3047] displacement operation [bit operation]
多线程 从UE4的无锁队列开始 (线程安全)
Pxxx local socket communication
虚拟机内存结构简述
acwing 788. Number of pairs in reverse order
[51nod p2527] or and sum [bit operation]
LeetCode 6098. Count the number of subarrays whose score is less than K (prefix and + binary search)
List list
matlab轮毂电机分析模糊pid控制垂向振动分析
SpEL表达式 简单使用
Standard template library (STL)
Learning makefile with me
Zigzag transformation
VDD,DVDD,AVDD,VCC,AFVDD,DOVDD,IOVDD
[51nod p3395] n-bit gray code [bit operation]
acwing 789. Range of numbers (dichotomy + suitable for understanding dichotomy boundary)
大O记法解释
Exception handling operation
Timestamp to localdate
第一章 第一节