Unsorting Array(CF-252B)
Problem Description
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
- a1 ≤ a2 ≤ ... ≤ an;
- a1 ≥ a2 ≥ ... ≥ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 ≤ n ≤ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an — the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1Output
-1
Input
2
1 2Output
-1
Input
4
1 2 3 4Output
1 2
Input
3
1 1 1Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted.
题意:给出一个长度为 n 的序列,要求必须交换两个整数一次,问交换后数组能否变为不是有序的,如果交换后有序,那么输出 -1,如果交换后无序,输出交换的元素的位置
思路:建一个复制数组,将排序后与原数组暴力对比即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int a[N];
int b[N];
int main() {
int n;
scanf("%d",&n);
for(int i=0; i<n; i++) {
scanf("%d",&a[i]);
b[i]=a[i];
}
if(n<=2) {
puts("-1");
return 0;
}
sort(b, b+n);
for(int i=0; i<n-1; i++) {
if(a[i]==a[i+1])
continue;
swap(a[i], a[i+1]);
int j=0;
while(j<n&&a[j]==b[j])
j++;
if(j==n) {
swap(a[i], a[i+1]);
continue;
}
j=0;
while(j<n&&a[j]==b[n-j-1])
j++;
if(j==n) {
swap(a[i], a[i+1]);
continue;
}
printf("%d %d\n", i+1, i+2);
return 0;
}
printf("-1\n");
return 0;
}

浙公网安备 33010602011771号