451B - Sort the Array - CodeForces Solution


implementation sortings *1300

Please click on ads to support us..

Python Code:

import sys
input=sys.stdin.readline
n=int(input())
l=list(map(int,input().split()))
a=sorted(l)
i=0
x=0
y=n-1
k=1
while i<n:
    if l[i]==a[i] and k==1:
        i=i+1
    elif l[i]==a[i] and k==0:
        i=i-1
    else:
        if k==1:
            x=i
            k=0
            i=n-1
        else:
            y=i
            break
s=l[x:min(y+1,n)]
v=l[:max(x,0)]+s[::-1]+l[min(y+1,n):]
if a==v :
    print("yes")
    print(x+1,y+1)
elif a==l:
    print("yes")
    print(1,1)
else:
    print("no")

C++ Code:

// Date: 2023-01-31 15:06:19
// Problem: B. Sort the Array
// Contest: Codeforces - Codeforces Round #258 (Div. 2)
// URL: https://codeforces.com/problemset/problem/451/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// --------By WIDA--------

#include <bits/stdc++.h>
using namespace std;
namespace WIDA {
	using LL = long long;
	using PII = pair<LL, LL>;
	using TII = tuple<LL, LL, LL>;
	#define FOR(i,a,b) for (int i = (int)(a); i <= (int)(b); i ++)
	#define FOR2(i,a,b) for (int i = (int)(a); i <= (int)(b); i += 2)
	#define FORD(i,b,a) for (int i = (int)(a); i >= (int)(b); i --)
	#define ALL(a) a.begin(), a.end()
	#define rALL(a) a.rbegin(), a.rend()
	#define pb push_back
	#define fi first
	#define se second
	#define sz size()
	const LL INF = 0x3f3f3f3f3f3f3f3f;
	template <class... Args> void _(Args... args) {
	    auto _ = [&](auto x) { cout << x << " "; };
	    cout << "--->"; int arr[] = {(_(args), 0)...};
	    cout << "\n";
	}
	template <class T> void _i(T args) {
	    cout << "{";
	    for (auto i : args) cout << i << ", "; cout << "}\n";
	}
	template <class T> void _ii(T args) {
	    cout << "{"; 
	    for (auto [i, j] : args) cout << i << " " << j << ", ";
	    cout << "}\n";
	}
	template <class... Args> void __(Args... args) {
	    auto _ = [&](auto x) { cout << x << " "; };
	    int arr[] = {(_(args), 0)...}; cout << "\n";
	}
	#define int long long // Zmod要关闭
	#define endl "\n" // 交互题要关闭
	const int MOD = 998244353; // 看清楚每道题的 MOD
	const int N = 1e6 + 7; // 二维数组要修改 vector<vector<int> > a(n, vector<int> (m, 0));
}
using namespace WIDA;


bool Solve() {
	int n; cin >> n;
	vector<int> a(n + 1);
	for (int i = 1; i <= n; ++ i) cin >> a[i];
	a.push_back(INF);
	
	int l = 1, r = 1;
	for (int i = 1; i <= n; ++ i) {
		if (a[i - 1] < a[i] && a[i] > a[i + 1]) l = i;
		else if (a[i - 1] > a[i] && a[i] < a[i + 1]) r = i;
	}
	reverse(a.begin() + l, a.begin() + r + 1);
	if (is_sorted(a.begin(), a.end())) cout << "yes\n" << l << " " << r;
	else cout << "no";
	
	return 0;
}
signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	
	int Task = 1;
	// cin >> Task;
	for (int i = 1; i <= Task; ++ i) {
		Solve();
		// cout << (Solve() ? "YES" : "NO") << endl;
	}
	
	return 0;
}


Comments

Submit
0 Comments
More Questions

Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes