
思路:用set或者map去重, 遍历数组,将出现过的数字存入set,set里的元素是唯一且按递增顺序排列的。
当set的长度>3时,是不可能变成相等序列的。当set的长度=3时,判断是否为等差数列,是则可变成相等序列。
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
string MiniumM(vector<int> A, int N)
{
if (N <= 2) return "YES";
else{
set<int> B;
vector<int> C;
for (int i = 0; i < N; i++)
B.insert(A[i]);
if (B.size() <= 2) return "YES";
if (B.size()>3) return "NO";
else{
set<int>::iterator p;
for (p = B.begin(); p != B.end(); p++)
C.push_back(*p);
if ((C[2] - C[1]) == (C[1] - C[0]))
return "YES";
else return "NO";
}
}
}
int main()
{
int k, n,m;
cin >> k;
for (int i = 0; i < k; i++)
{
vector<int> A;
cin >> n;
for (int j = 0; j<n; j++)
{
cin >> m;
A.push_back(m);
}
cout << MiniumM(A, n) << endl;
}
system("pause");
return 0;
}
博客介绍了判断数组是否可变成相等序列的思路。使用set或map对数组去重,遍历数组将数字存入set,其元素唯一且递增。当set长度>3,无法变成相等序列;长度=3时,判断是否为等差数列,若是则可变成相等序列。
584

被折叠的 条评论
为什么被折叠?



