练习cf1478A. Nezzar and Colorful Balls
题目如下
A. Nezzar and Colorful Balls
time limit per test1 second
memory limit per test512 megabytes
Nezzar has 𝑛 balls, numbered with integers 1,2,…,𝑛. Numbers 𝑎1,𝑎2,…,𝑎𝑛 are written on them, respectively. Numbers on those balls form a non-decreasing sequence, which means that 𝑎𝑖≤𝑎𝑖+1 for all 1≤𝑖<𝑛.
Nezzar wants to color the balls using the minimum number of colors, such that the following holds.
For any color, numbers on balls will form a strictly increasing sequence if he keeps balls with this chosen color and discards all other balls.
Note that a sequence with the length at most 1 is considered as a strictly increasing sequence.
Please help Nezzar determine the minimum number of colors.
Input
The first line contains a single integer 𝑡 (1≤𝑡≤100) — the number of testcases.
The first line of each test case contains a single integer 𝑛 (1≤𝑛≤100).
The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤𝑛). It is guaranteed that 𝑎1≤𝑎2≤…≤𝑎𝑛.
Output
For each test case, output the minimum number of colors Nezzar can use.
题目大意
现在有一个数组,在这些数组中一共可以分为多少个严格递增的子数组;
题目分析
因为是分配,所以每个元素至多可以参与构成一个子数组,同时每个子数组的元素数大于等于1,所以允许一个数组只有一个元素,所以只要找出数组中出现次数最多的元素即可;
点击查看代码
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main(){
int t; cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
map<int, int> freq;
for(int i = 0; i < n; ++i){
cin >> a[i];
freq[a[i]]++;
}
int max = 0;
for (int i = 0; i < n; ++i) {
if (freq[a[i]] > max) {
max = freq[a[i]];
}
}
cout << max << endl;
}
return 0;
}

浙公网安备 33010602011771号