题目:
样例:
|
2 1 2 4 |
解题思路:
这道题很好玩,单纯思维题,我们找一下规律,题目要求咱们 通过删除 两个元素 来达到符合 不能有 i < j && a[i] == a[j] 并且选择删除的时候,尽量使得数组最长
在这里我们看一下规律,我们可以发现,通过删除之后,剩下的元素一定是 单独唯一的,并且删除的时候,我们的删除次数一定是 偶数
代码详解如下:
#include <iostream>
#include <cstring>
#include <unordered_map>
#define endl '\n'
#define umap unordered_map
#pragma GCC optimize(3,"Ofast","inline")
#define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e6 + 10;
inline void solve()
{
bool r[N]; // 用于记录我们 元素是否出现过
memset(r, false, sizeof r); // 初始化
int n ;
cin >> n;
int ans = n; // 假定结果数组长度最长 为初始长度
int sum = 0; // 记录删除次数
for (int i = 0, x; i < n; ++i)
{
cin >> x;
if (!r[x]) // 如果没出现过
{
r[x] = true; // 标记出现
}
else
{
ans--; // 删除该重复元素
sum++; // 累加删除次数
}
}
if (sum % 2)
ans--; // 如果删除次数为 单数,那么我们任意删除其中一个
//输出答案
cout << ans << endl;
}
int main()
{
// freopen("a.txt", "r", stdin);
___G;
int _t = 1;
cin >> _t;
while (_t--)
{
solve();
}
return 0;
}