Day 8 B. Unsorting Array

Problem:
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 ≤ 10^5). 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 10^9.

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
1
output
-1

input
2
1 2
output
-1

input
4
1 2 3 4
output
1 2

input
3
1 1 1
output
-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.

题目大致意思就是:给你个数组 交换其中两个元素的值 让数组变成无序 如果有 就输出这两个元素所对应的位置 没有输出-1

代码如下:

#include<iostream>
#include<vector>
#include<algorithm>
#define ll long long 
using namespace std;
int main()
{
    vector <int> a, b, c;//定义三个vector 分别用来存放原数组 存放正序数组 存放倒序数组
    int n;
    int x;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> x;
        a.push_back(x);
        b.push_back(x);
        c.push_back(x);
    }
    sort(b.begin(), b.end());//正序
    sort(c.rbegin(), c.rend());//倒序
    for (int i = 1; i < n; i++)
    {
        if (a[i] != a[i - 1])//如果前后元素不相同
        {
            swap(a[i], a[i - 1]);//交换两者位置
            if (a != b && c != a)//判断是否与正序或是倒序数组相同 不相同输出元素位置
            {
                cout << i  << " " << i+1 << endl;
                return 0;
            }
            swap(a[i], a[i - 1]);//再交换回来 不改变原数组
        }
    }
    cout << -1 << endl;//如果找不到或是符合题目某种特殊条件  
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值