当前位置:网站首页>Codeforces Round #771 (Div. 2)
Codeforces Round #771 (Div. 2)
2022-07-06 11:25:00 【%xiao Q】
A. Reverse
For this problem, we only need to combine the array and the array arranged in order (1,2,3…n) Just compare , Find the first unequal position l, Then continue to find in the following array l The smallest number that should have been placed in the position ( namely l) The subscript of the number of r, Then flip [l, r], If they are all equal , Then this is the smallest , Direct output
Reference code :
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#include <unordered_map>
#define LL long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define reps(i, a, b) for(int i = a; i < b; i++)
#define pre(i, a, b) for(int i = b; i >= a; i--)
using namespace std;
const int N = 510;
int a[N], b[N];
int main()
{
int T;
cin >> T;
while(T--)
{
int n;
cin >> n;
rep(i, 1, n) cin >> a[i], b[i] = i;
int l = -1, r;
rep(i, 1, n)
if(a[i] != b[i])
{
l = i;
break;
}
if(l == -1)
{
rep(i, 1, n) cout << a[i] << ' ';
cout << endl;
continue;
}
rep(i, 1, n)
if(a[i] == b[l])
{
r = i;
break;
}
rep(i, 1, l - 1) cout << a[i] << ' ';
pre(i, l, r) cout << a[i] << ' ';
rep(i, r + 1, n) cout << a[i] << ' ';
cout << endl;
}
return 0;
}
B. Odd Swap Sort
In this problem, we can divide these numbers into 2 An array , According to the title requirements , Only a[i] + a[i + 1] Only odd numbers can be exchanged , So I want to exchange 2 Adjacent numbers , Then their parity will be different , So we can enumerate in order , Divide these numbers into 2 An array ( Odd and even arrays ), Then judge this 2 Whether there is an array a[i] > a[i + 1], Because parity is the same and cannot be exchanged , Want to exchange big numbers back , So they 2 The number must be exchanged once , If it doesn't exist , Then it can be arranged in order , On the contrary, we can't .
Reference code :
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#include <unordered_map>
#define LL long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define reps(i, a, b) for(int i = a; i < b; i++)
#define pre(i, a, b) for(int i = b; i >= a; i--)
using namespace std;
const int N = 1e5 + 10;
int a[N], b[N];
int main()
{
int T;
cin >> T;
while(T--)
{
int n, n1 = 0, n2 = 0;
cin >> n;
rep(i, 1, n)
{
int x;
cin >> x;
if(x & 1) a[n1++] = x;
else b[n2++] = x;
}
bool flag = true;
rep(i, 0, n1 - 2)
if(a[i] > a[i + 1]) flag = false;
rep(i, 0, n2 - 2)
if(b[i] > b[i + 1]) flag = false;
if(flag) puts("Yes");
else puts("No");
}
return 0;
}
C. Inversion Graph
We can use a monotonically increasing stack to solve this problem , Each element in the stack represents the largest element in a connected block ,
So how do we maintain this stack ?
First, traverse the array in sequence :
- a[i] > q.top(): Because it's a monotonous stack , So it is larger than all the numbers in the stack , Directly into the stack as a new connected block
- a[i] < q.top() : This is a judgment q.top() Whether it can connect with other elements in the stack , That is, the elements in the stack can be combined with a[i] Connected to a ( That is to say q[i] > a[i]), At this time, we just need to put greater than a[i] All the numbers are out of the stack , But the top of the stack cannot be out of the stack .
Reference code :
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <unordered_map>
#define LL long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define reps(i, a, b) for(int i = a; i < b; i++)
#define pre(i, a, b) for(int i = b; i >= a; i--)
using namespace std;
int main()
{
int T;
cin >> T;
while(T--)
{
stack<int> a;
int n;
cin >> n;
rep(i, 1, n)
{
int x;
cin >> x;
if(a.empty())
{
a.push(x);
continue;
}
if(x > a.top()) a.push(x);
else
{
int t = a.top();
while(!a.empty() && a.top() > x) a.pop();
a.push(t);
}
}
cout << a.size() << endl;
}
return 0;
}
边栏推荐
- AcWing 1294. Cherry Blossom explanation
- Tcp/ip protocol (UDP)
- 安全测试涉及的测试对象
- Heating data in data lake?
- Data dictionary in C #
- QT creator specifies dependencies
- Neo4j installation tutorial
- What does usart1 mean
- [ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
- 机器学习笔记-Week02-卷积神经网络
猜你喜欢
Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
02-项目实战之后台员工信息管理
Basic use of redis
Software I2C based on Hal Library
[蓝桥杯2017初赛]方格分割
QT creator shape
学习问题1:127.0.0.1拒绝了我们的访问
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application
人脸识别 face_recognition
一键提取pdf中的表格
随机推荐
Software I2C based on Hal Library
Install mongdb tutorial and redis tutorial under Windows
Why can't I use the @test annotation after introducing JUnit
自动机器学习框架介绍与使用(flaml、h2o)
天梯赛练习集题解LV1(all)
学习问题1:127.0.0.1拒绝了我们的访问
Neo4j installation tutorial
软件测试-面试题分享
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
LeetCode #461 汉明距离
引入了junit为什么还是用不了@Test注解
库函数--(持续更新)
图像识别问题 — pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path
Copie maître - esclave MySQL, séparation lecture - écriture
When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page
Did you forget to register or load this tag 报错解决方法
Punctual atom stm32f103zet6 download serial port pin
PHP - whether the setting error displays -php xxx When PHP executes, there is no code exception prompt
Ansible practical Series II_ Getting started with Playbook
Base de données Advanced Learning Notes - - SQL statements