CodeForces 252B--Unsorting Array
来源:互联网 发布:ubuntu命令行启动软件 编辑:程序博客网 时间:2024/06/07 06:51
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting ofn 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 ofn 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.
The first line contains a single integer n (1 ≤ n ≤ 105). The second line containsn 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 exceed109.
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.
11
-1
21 2
-1
41 2 3 4
1 2
31 1 1
-1
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.
!!! 暴力竟然能过~~
CODE:
#include <iostream>#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;int a[100005],a1[100005],a2[100005];bool cmp(int x,int y){ return x>y;}int check(int *ch_a,int *ch_aa,int nn){ for(int i=1;i<=nn;i++) { if(ch_a[i]!=ch_aa[i]) return 0; } return 1;}int main(){ //freopen("in.in","r",stdin); int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); a1[i]=a[i]; a2[i]=a[i]; } sort(a1+1,a1+n+1); sort(a2+1,a2+n+1,cmp); int ch1=check(a,a1,n),ch2=check(a,a2,n); int t,ok=0; if(ch1==1&&ch2==1) { printf("-1\n"); } else { for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { if(a[i]!=a[j]) { t=a[i];a[i]=a[j];a[j]=t; if((check(a,a1,n)+check(a,a2,n))==0) { printf("%d %d\n",i,j); ok=1; break; } else { t=a[j];a[j]=a[i];a[i]=t; } } } if(ok) break; } if(ok==0) printf("-1\n"); } } return 0;}
- CodeForces 252B--Unsorting Array
- codeforces 252B Unsorting Array 暴力+贪心
- cf 153.div2 B. Unsorting Array
- CodeForces 224B Array
- CodeForces 482 B. Interesting Array
- CodeForces 482B Interesting Array
- Codeforces 623 B. Array GCD
- Codeforces 623B:Array GCD
- codeforces 482B Interesting Array
- Codeforces 84B Magical Array
- CodeForces 623B Array GCD
- Codeforces 220B Little Elephant and Array
- CodeForces 220B Little Elephant and Array
- codeforces 315 B.Sereja and Array
- CodeForces 315B Sereja and Array
- Codeforces Round #138 (Div. 2) B. Array
- Codeforces 451B Sort the Array
- Codeforces 451B Sort the Array(水题)
- int和Integer的不同
- frgtgh
- 黑马程序员_银行业务调度系统
- Eclipse 代码补全快捷键
- 2014年4月操作系统份额:Windows XP仍然在26%以上,Linux微涨
- CodeForces 252B--Unsorting Array
- 数塔
- 逆腹式呼吸法
- Python代码中中文报错问题解决
- java构造方法总结
- Bitmap内存问题
- velocity显示List与Map的方法详细解析
- mybatis generator myeclipse 插件
- 查找算法大总结