第一周题单
H - Boxes Packing (CodeForces - 903C)
Mishka has got n empty boxes. For every i ( 1 ≤ i ≤ n ) i (1 ≤ i ≤ n) i(1 ≤ i ≤ n), i i i-th box is a cube with side length a i a_i ai.
Mishka can put a box i i i into another box j j j if the following conditions are met:
- i i i-th box is not put into another box;
- j j j-th box doesn’t contain any other boxes;
- box i i i is smaller than box j ( a i < a j ) j (a_i < a_j) j(ai < aj).
Mishka can put boxes into each other an arbitrary number of times. He wants to minimize the number of visible boxes. A box is called visible iff it is not put into some another box.
Help Mishka to determine the minimum possible number of visible boxes!
Input
The first line contains one integer n ( 1 ≤ n ≤ 5000 ) n (1 ≤ n ≤ 5000) n(1 ≤ n ≤ 5000) — the number of boxes Mishka has got.
The second line contains n n n integers a 1 , a 2 , . . . , a n ( 1 ≤ a i ≤ 1 0 9 ) a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) a1,a2,...,an(1 ≤ ai ≤ 109), where ai is the side length of i i i-th box.
Output
Print the minimum possible number of visible boxes.
Simple1
Input
3
1 2 3
Output
1
Simple2
Input
4
4 2 4 3
Output
2
Note
In the first example it is possible to put box 1 into box 2, and 2 into 3.
In the second example Mishka can put box 2 into box 3, and box 4 into box 1.
题目大意
有一列箱子,数量为 n n n,把箱子 i i i放进 j j j需要满足一下条件:
- 第 i 个盒子没有放入另一个盒子中;
- 第 j 个盒子不包含任何其他盒子;
- a i < a j a_i<a_j ai<aj;
放进其他箱子的箱子为不可见,问最后有几个箱子可见
题解过程
用容器vector承接所有箱子的边长,然后对所有的箱子按边长升序排列,最后模拟整个放入过程,每次将箱子放入其他箱子后擦除本箱子,最后擦除那个可见的箱子并计数
代码部分(cpp)#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> boxes;
bool cmp(int a, int b)
{
return a < b;
}
void solution()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int temp;
cin >> temp;
boxes.push_back(temp);
}
sort(boxes.begin(), boxes.end(), cmp);
int num = 0;
while (boxes.size() != 0)
{
// 遍历
auto temp = boxes.begin();
for (auto i = next(boxes.begin()); i != boxes.end(); i++)
{
if (*temp < *i)
{
boxes.erase(temp);
i--;
temp = i;
}
}
// 擦除最大数 即可见的那一个箱子
boxes.erase(temp);
num++;
}
cout << num;
}
int main(int argc, const char **argv)
{
solution();
return 0;
}
刚开始学习,欢迎指正