题解:Codeforces Round 967 (Div. 2) A [暴力/贪心]
A. Make All Equal
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
You are given a cyclic array
You can perform the following operation on
- Let
be the current size of , you can choose any two adjacent elements where the previous one is no greater than the latter one (In particular, and are adjacent and is the previous one), and delete exactly one of them. In other words, choose an integer ( ) where holds, and delete exactly one of or from .
Your goal is to find the minimum number of operations needed to make all elements in
给你一个循环数组
你最多可以对
- 假设
是 的当前大小,你可以选择任意两个相邻的元素,其中前一个元素不大于后一个元素(特别是 和 是相邻的,而 是前一个元素),并恰好删除其中一个元素。换句话说,选择一个 成立的整数 ( ),并从 中恰好删除 或 中的一个。
你的目标是找出使
Input
Each test contains multiple test cases. The first line contains the number of test cases
The first line of each test case contains a single integer
The second line of each test case contains
输入
每个测试包含多个测试用例。第一行包含测试用例的数量
每个测试用例的第一行包含一个整数
每个测试用例的第二行包含
Output
For each test case, output a single line containing an integer: the minimum number of operations needed to make all elements in
输出
对于每个测试用例,输出一行包含一个整数:使
Example
Input
7 1 1 3 1 2 3 3 1 2 2 5 5 4 3 2 1 6 1 1 2 2 3 3 8 8 7 6 3 8 7 6 3 6 1 1 4 5 1 4
Output
0 2 1 4 4 6 3
Note
In the first test case, there is only one element in
In the second test case, we can perform the following operations to make all elements in
- choose
, delete , then would become . - choose
, delete , then would become .
It can be proven that we can't make all elements in
注
在第一个测试用例中,
在第二个测试用例中,我们可以执行以下操作,使
- 选择
,删除 ,那么 将变为 。 - 选择
,删除 ,则 将变为 。
可以证明,使用少于
题意
有一个循环数组
你每次可以操作:
对数组中相邻两个不同的数字删去任意一个(首尾两个也算相邻)
你需要不断操作,直到数组中只有一种元素时停止
问:你至少需要操作多少次
题解
只需要用一个map存储某种数字出现的个数
然后最后的数组就是全部那种数字了
所以只需要输出
即可
代码
#include <bits/stdc++.h> #define int long long #define INF 0x3f3f3f3f #define all(x) x.begin(),x.end() using i64 = long long; const int N = 2e5 + 10; int t = 1; int a[N]; void solve() { std::map<int,int> mp; int n; std::cin >> n; int max = 0; for(int i = 0 ; i < n ; i ++) { int num; std::cin >> num; mp[num]++; max = std::max(max,mp[num]); } std::cout << n - max << "\n"; } signed main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cin >> t; while(t--){ solve(); } return 0; }
有什么建议或者意见的欢迎在评论区留言!
posted on 2024-08-21 15:10 Jiejiejiang 阅读(73) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」