当前位置:网站首页>B. All Distinct
B. All Distinct
2022-07-20 05:29:00 【冷颕】
(大家好,拖更了这么久,本博主又回来了)。
B. All Distinct
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Sho has an array aa consisting of nn integers. An operation consists of choosing two distinct indices ii and jj and removing aiai and ajaj from the array.
For example, for the array [2,3,4,2,5] Sho can choose to remove indices 1 and 3. After this operation, the array becomes [3,2,5]. Note that after any operation, the length of the array is reduced by two.
After he made some operations, Sho has an array that has only distinct elements. In addition, he made operations such that the resulting array is the longest possible.
More formally, the array after Sho has made his operations respects these criteria:
- No pairs such that (i<j) and ai=aj exist.
- The length of aa is maximized.
Output the length of the final array.
Input
The first line contains a single integer tt (1≤t≤103) — the number of test cases.
The first line of each test case contains a single integer nn (1≤n≤50) — the length of the array.
The second line of each test case contains nn integers aiai (1≤ai≤104) — the elements of the array.
Output
For each test case, output a single integer — the length of the final array. Remember that in the final array, all elements are different, and its length is maximum.
Example
input
Copy
4 6 2 2 2 3 3 3 5 9 1 9 9 1 4 15 16 16 15 4 10 100 1000 10000
output
Copy
2 1 2 4
Note
For the first test case Sho can perform operations as follows:
- Choose indices 1 and 5 to remove. The array becomes [2,2,2,3,3,3]→[2,2,3,3].
- Choose indices 1 and 4 to remove. The array becomes [2,2,3,3]→[2,3].
The final array has a length of 22, so the answer is 22. It can be proven that Sho cannot obtain an array with a longer length.
For the second test case Sho can perform operations as follows:
- Choose indices 3 and 4 to remove. The array becomes [9,1,9,9,1]→[9,1,1].
- Choose indices 1 and 3 to remove. The array becomes [9,1,1]→[1].
The final array has a length of 1, so the answer is 1. It can be proven that Sho cannot obtain an array with a longer length.
首先我们要知道,我们需要找到一个最大长度且数组元素不重复的数组。
接着我们把数组分区,将每个数组元素的个数重建一个数组。
如果该数组元素的数量是奇数个,我们就置位1
如果该数组元素的数量是偶数个,我们就置位2
那么我就开始讨论数组中元素的等可能的情况。
(注讨论的情况,重复是指该数组值出现等于两次,数组数量为1的情况,我们直接加就完事了)
长度默认为0
1.数组中元素全不重复
2数组中元素有偶数个重复
例如 1 1 2 2
等于 1 2
长度加有多少个重复
3.数组中元素有奇数个重复
例如 1 1 2 2 3 3
等于 1 2
长度加有多少个重复-1
思路已经分析完了,接下来我直接贴一下代码。
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int t;
cin >> t;
while (t > 0)
{
int n, index1 = 0, sum = 0;
cin >> n;
int data1[10000];
int qu[20000];
for (int i = 0; i < n; i++)
{
cin >> data1[i];
}
sort(data1, data1 + n);
for (int i = 0; i <= data1[n - 1]; i++)
{
qu[i] = 0;
}
for (int i = 0; i < n; i++)
{
qu[data1[i]]++;
}
for (int i = 0; i <= data1[n - 1]; i++)
{
if (qu[i] != 0)
{
if (qu[i] % 2 == 0)
{
qu[i] = 2;
}
else
{
qu[i] = 1;
}
}
}
for (int i = 1; i <= data1[n - 1]; i++)
{
if (qu[i] == 2)
{
index1++;
}
}
if (index1 != 0)
{
if (index1 % 2 == 0)
{
sum = index1;
}
else
{
index1 = index1 - 1;
sum = index1;
}
for (int i = 1; i <= data1[n - 1]; i++)
{
if (qu[i] == 1)
{
sum++;
}
}
cout << sum << endl;
}
else if (index1 == 0)
{
for (int i = 1; i <= data1[n - 1]; i++)
{
if (qu[i] == 1)
{
sum++;
}
}
cout << sum << endl;
}
t--;
}
return 0;
}
边栏推荐
- Apache Doris ODBC appearance database mainstream version and its ODBC version correspondence
- sql优化(十):关联更新
- Swagger重点配置项
- 请问一下老师们,flink SQL kafka connector中的startup mode 选择
- Websocket server code protocol analysis, learn to do their own protocol ideas.
- 2022-07-19-利用shell去激活conda环境
- [sort] bucket sort and cardinal sort
- 【科学文献计量】中英文文献标题及摘要分词字数与频数统计与可视化
- [AD learning record] copper clad
- [wechat applet]: paging request data, pull-up load, pull-down refresh.
猜你喜欢
[AD learning record] Why are schematic diagrams and PCBs in the same folder, and PCB cannot be generated?
ES6中Symbol、迭代器和生成器基本语法
win10基于IDEA,搭建Presto开发环境
MATLAB数字图像处理 实验五:形态学图像处理
消息队列(MQ)
Flag signal
Lombok详细介绍
Websocket server code protocol analysis, learn to do their own protocol ideas.
DNP3 模拟器使用教程
[note] logstash environment setup and installation configuration
随机推荐
请问一下老师们,flink SQL kafka connector中的startup mode 选择
2022-7-19 Gu Yujia's learning notes of group 8 (this keyword and packaging)
消息队列(MQ)
Websocket服务器代码协议解析,学会自己做协议思路。
Apache Doris Oracle ODBC appearance User Guide
30 spark introduction: Spark Technology stack explanation, partition, system architecture, operator and task submission method
win10如何实现电脑上文件共享访问
[scientific literature measurement] keyword mining and visualization
Matlab digital image processing experiment 5: morphological image processing
牛客BM6 判断链表中是否有环
Usage and configuration of Apache Doris ODBC MySQL appearance under Ubuntu
Silicon Valley classroom notes (Part 1)
STL list构造函数、大小
[wechat applet]: paging request data, pull-up load, pull-down refresh.
Event object of DOM
对ReadFile堵塞进行异步处理
解决方案:业主单位智慧工地监管云平台
Flink Doris Connector设计方案
[scientific literature measurement] analysis and visualization of readability indicators of Chinese and English literature titles and abstracts
J9 Digital Platform Theory: the possibilities and limitations of defi in the metauniverse