[백준 10855] Extreme Sort (C#, C++) - soo:bak
작성일 :
문제 링크
설명
수열이 주어질 때, 앞에 있는 값이 뒤에 있는 값보다 항상 작거나 같은지 확인하는 문제입니다.
조건을 만족하면 yes, 아니면 no를 출력합니다.
접근법
수열이 비내림차순이면 조건을 만족합니다.
인접한 두 값을 비교해서 앞 값이 뒤 값보다 큰 경우가 하나라도 있으면 no, 없으면 yes를 출력합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
class Program {
static void Main() {
var parts = Console.In.ReadToEnd().Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
var idx = 0;
var n = int.Parse(parts[idx++]);
var ok = true;
var prev = int.Parse(parts[idx++]);
for (var i = 1; i < n; i++) {
var cur = int.Parse(parts[idx++]);
if (cur < prev) ok = false;
prev = cur;
}
Console.WriteLine(ok ? "yes" : "no");
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vi a(n);
for (int i = 0; i < n; i++) cin >> a[i];
bool ok = true;
for (int i = 1; i < n; i++) {
if (a[i] < a[i - 1]) ok = false;
}
cout << (ok ? "yes" : "no") << "\n";
return 0;
}