Problem:
Score : 200 points

Problem Statement

We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.

You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.

Print YES if you can sort p in ascending order in this way, and NO otherwise.

Constraints

All values in input are integers.

2 \leq N \leq 50

p is a permutation of {1,\ 2,\ ...,\ N}.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

You can sort p in ascending order by swapping p_1 and p_5.

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

In this case, swapping any two elements does not sort p in ascending order.

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

p is already sorted in ascending order, so no operation is needed.

Solution in C (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdbool.h>

void	swap(int *a, int *b)
{
	int	tmp;

	tmp = *a;
	*a = *b;
	*b = tmp;
}

int	is_sort(int	*tab, int size)
{
	int	flag;
	int	i;

	flag = 0b00;
	for (i = 0; i < size - 1; i++)
	{
		if (tab[i] < tab[i + 1])
			flag |= 0b01;
		if (tab[i] > tab[i + 1])
			flag |= 0b10;
	}
	if (flag == 0b11)
		return (false);
	return (true);
}

int	is_ascend(int *tab, int size)
{
	int	i, j;
	int	tmp[2];

	j = 0;
	for (i = 0; i < size; i++)
		if (i + 1 != tab[i] && j < 2)
			tmp[j++] = i;
	swap(&tab[tmp[0]], &tab[tmp[1]]);
	if (is_sort(tab, size))
		return (true);
	return (false);
}

int	is_descend(int *tab, int size)
{
	int	i, j;
	int	tmp[2];

	j = 0;
	for (i = size - 1; i > 0; i--)
		if (size - i != tab[i] && j < 2)
			tmp[j++] = i;
	swap(&tab[tmp[0]], &tab[tmp[1]]);
	if (is_sort(tab, size))
		return (true);
	return (false);
}

int	main(void)
{
	int	N;
	int	i;
	int	tab[64], copy[64];
	int	swap[2];

	scanf("%d", &N);
	for (i = 0; i < N; i++)
		scanf("%d", &tab[i]);
	if (is_sort(tab, N))
	{
		printf("YES\n");
		return (0);
	}
	for (i = 0; i < N; i++)
		copy[i] = tab[i];
	if (is_ascend(copy, N))
		printf("YES\n");
	else if (is_descend(tab, N))
		printf("YES\n");
	else
		printf("NO\n");
	return (0);
}