본문 바로가기

백준

20127 Y-수열

뭐가문제야!!!!!

 

 

고민하다가 어떻게 동작하는지 적어 봤는데 그러다가 위에 적어둔 거 보고 알았다.

원소 옮겼을 때 처음과 끝을 연결할 수 있는지 살펴봐야 하는데 그 부분을 빼먹었다. ㅎㅎ 왜 기억 안 났지

 

이거 고치고도 더 틀렸는데 1 2 3이런 경우는 처음과 끝을 확인 안 해줘도 된다

 

수정하고 드디어 맞았음

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <bitset>

#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()

using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;
using iii = tuple<int, int, int>;



int     main()
{
	int n;
	scanf("%d", &n);

	vector<int> v(n);
	for (int i = 0; i < n; i++)
		scanf("%d", &v[i]);

	int idx1 = 0;
	bool check = true;
	bool false1 = false;
	for (int i = 1; i < v.size(); i++)
	{
		if (v[i - 1] > v[i] && check)
		{
			check = false;
			idx1 = i;
		}
		else if (v[i - 1] > v[i] && !check)
		{
			false1 = true;
			break;
		}
	}

	if (v[n-1] > v[0] && !check)
		false1 = true;

	int idx2 = 0;
	bool false2 = false;
	check = true;
	for (int i = 1; i < v.size(); i++)
	{
		if (v[i - 1] < v[i] && check)
		{
			check = false;
			idx2 = i;
		}
		else if (v[i - 1] < v[i] && !check)
		{
			false2 = true;
			break;
		}
	}

	if (v[n - 1] < v[0] && !check)
		false2 = true;

	if (false1 && false2)
		printf("-1\n");
	else if (false1)
		printf("%d\n", idx2);
	else if (false2)
		printf("%d\n", idx1);
	else
		printf("%d\n", min(idx1, idx2));

	return 0;
}

'백준' 카테고리의 다른 글

2607 비슷한 단어  (0) 2020.12.01
11660 구간 합 구하기 5  (0) 2020.11.24
10816 숫자 카드 2  (0) 2020.11.10
14729 칠무해  (0) 2020.11.10
2688 줄어들지 않아  (0) 2020.11.06