原题链接: https://codeforces.com/contest/1401/problem/C
测试样例:
input
4
1
8
6
4 3 6 6 2 9
4
4 5 6 7
5
7 5 2 2 4
output
YES
YES
YES
NO
样例解释:
In the first and third sample, the array is already non-decreasing.
In the second sample, we can swap and first, and swap and second to make the array non-decreasing.
In the forth sample, we cannot the array non-decreasing using the operation.
题意: 给你一个整数序列,其中你可以选择下标为和,如果的值为整数序列中数值最小的元素,那么就可以交换和。问你能否经过若干次这样的操作使得整数序列变为非递减序列。
解题思路: 我们的最终目的就是要将整数序列变为非递减序列。那么我们拿这个最终序列和原序列进行对比,位置上数值不对的自然是需要靠交换来改变的。而交换的条件则是上述,那么我们如果每一个需要交换的数它们的因子都包含这个最小的数,那么我们就可以通过用这个最小的数来进行交换而不用考虑其他的。假设最小的数为,即如果我们要交换的数满足,那么我们就是可以通过这个最小的数作为媒介来进行转移,如果不行,说明这个要交换的数不能被转移,则说明不行。OK,我们具体看代码。
AC代码:
/*
*
*/
//POJ不支持
//i为循环变量,a为初始值,n为界限值,递增
//i为循环变量, a为初始值,n为界限值,递减。
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//
int t,n,a[maxn],b[maxn];
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>n;
rep(i,0,n-1){
cin>>a[i];
b[i]=a[i];
}
sort(b,b+n);//开始对比。
int minn=b[0];
int flag=false;
rep(i,0,n-1){
if(a[i]!=b[i]&&a[i]%minn!=0){
flag=true;
break;
}
}
if(flag)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
}
return 0;
}