当前位置:网站首页>Pat 1045 quick sort
Pat 1045 quick sort
2022-06-25 05:42:00 【Axyzstra】
The problem is recursion , Use over Array record location i The difference from the maximum value of all previous data ( All the differences in this question refer to the current element minus other values ); After recording , Traverse from back to front , Find out the difference between the current data and the following minimum value ; If the difference between the current number and the front largest element is positive, it means that all the front elements of the current element are smaller than the current element , If the difference between the current element and the following smallest element is negative , Indicates that the current element is larger than all the following elements ; When both are satisfied , This element can be used as a primary element , Record current element position ;
The key to this problem is to find the recurrence relation , The difference between the current element and the previous maximum element can be obtained through analysis
The current element - The former element + The difference between the previous element and the previous largest element or The current element - The former element
The second possibility is when the current element is the maximum ( namely over[i - 1] > 0)
Similar to the difference of the following smallest element ;
#include<cstdio>
const int maxn = 100010;
int over[maxn] = {
0}; // The difference between the current position and the maximum element
int main() {
int n, arr[maxn] = {
0};
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(int i = 0; i < n; i++) {
if(i > 0) {
over[i] = arr[i] - arr[i - 1] + over[i - 1];
}
if(over[i - 1] > 0) {
// If the previous one is the maximum , That is, the difference between the previous maximum value and the previous maximum value is positive
over[i] = arr[i] - arr[i - 1];
}
}
int low = 0, ans[n] = {
-1}, index = 0; // The difference between the current element and the following smallest element
for(int i = n - 1; i > -1; i--) {
if(i == n - 1) {
low = 0;
} else if(low < 0) {
// If the previous one is the minimum
low = arr[i] - arr[i + 1];
} else {
low = arr[i] - arr[i + 1] + low;
}
if(over[i] >= 0 && low <= 0) {
ans[index++] = i;
}
}
printf("%d\n", index);
for(int i = index - 1; i > -1; i--) {
printf("%d", arr[ans[i]]);
if(i > 0) {
printf(" ");
}
}
printf("\n");
return 0;
}
边栏推荐
- [OSPF routing calculation (class I LSA router, class II LSA network, and class III LSA sum net)] -20211228-30
- DOM document object model (I)
- JS handwriting depth clone array and object
- Word quickly makes multiple single-sided table labels, number plates, etc
- How to add an external header file in vs?
- A review of small sample learning
- JSON Library Tutorial from scratch (I): starting to learn and organize notes
- 2022.1.21 diary
- 3.2.3 use tcpdump to observe TCP header information (supplement common knowledge of TCP protocol)
- C language -- Sanzi chess
猜你喜欢

Word quickly makes multiple single-sided table labels, number plates, etc

SSRF-lab

Farewell to Lombok in 996

MySQL operation JSON

Jenkins installation and configuration

Use serialize in egg to read and write split tables

By inserting a section break, the word header, footer, and page number can start from any page

Interface learning

1.6.3 use tcpdump to observe DNS communication process

Double recursion in deep analysis merge sort
随机推荐
Volatile and JMM memory models
Dynamic programming full backpack
Vue uses keep alive to cache page optimization projects
Guava immutable set
Could not find “store“ in the context of “Connect(homePage)
Code learning-cvpr2020 unsupervised domain adaptive semantic segmentation: intra advance
Everything is an object
Uva1103 ancient pictograph recognition
Extend the toolbar of quill editor
C language -- Sanzi chess
Analysis of IM project framework
What is flush software? Is it safe to open an account online?
Go implements LRU cache
Day22(File,DiGui,FileOutputStream)
Use js to simply implement the apply, call and bind methods
[untitled]
16 application problem solving
Two dimensional array and function call cases of C language
Which securities company is good for opening a mobile account? Is it safe to open a mobile account?
Even if you are not good at anything, you are growing a little bit [to your 2021 summary]