] [C ++ STL sort () array sorting fragments

topic

There are number N, of the first array  l_{1} to the second  r_{1} number in ascending order of the sort. Then the first array  l_{2} to the second  r_{2} digital sorted in descending order.

Input Format

The first input line of five integers  N, l_{1}, r_{1}, l_{2}, r_{2}, where 0 < l_{1} <  r_{1} < N, 0 < l_{2} <  l_{2} < N, five numbers are not more than 10,000;

The second behavior N integers.

Output Format

N integers line, shows the result after sorting array, separated by spaces between the numbers, the end of the wrapping.

Code

#include<bits/stdc++.h>
using namespace std;
int arr[10010];
bool judge_1(int a, int b){
    return a > b;
}
int main(){
    int N, l1, r1, l2, r2;
    cin >> N >> l1 >> r1 >> l2 >> r2;
    for(int i = 0; i < N; i++){
        cin >> arr[i];
    }
    sort(arr + l1 - 1, arr + r1);
    sort(arr + l2 - 1, arr + r2, judge_1);
    for(int i = 0; i < N; i++){
        cout << arr[i];
        if(i != N - 1)
            cout << " ";
    }
    return 0;
}

This topic has a relatively small error-prone points, using the sort () to sort, we should pay attention to the function parameters:

  • The first is to sort the starting address of the array.
  • The second is the end of the address (the last one to sort of address)
  • The third parameter is the sort of method, but also can be big to small from small to large, you can not write the third parameter, then the default is to sort in ascending order.

Accordingly Here, the start address of the first number K for the ARR K + -. 1 .

Published 62 original articles · won praise 34 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_41960890/article/details/105186981