Codeforces Round #565 (Div. 3) B. Merge it!
链接:
https://codeforces.com/contest/1176/problem/B
题意:
You are given an array a consisting of n integers a1,a2,…,an.
In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not matter where you insert the new element). For example, from the array [2,1,4] you can obtain the following arrays: [3,4], [1,6] and [2,5].
Your task is to find the maximum possible number of elements divisible by 3 that are in the array after performing this operation an arbitrary (possibly, zero) number of times.
You have to answer t independent queries.
思路:
找除三的余数,优先配对1和2.
再3个1或3个2可以配成一个。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e2 + 10;
const int MOD = 1e9 + 7;
int n, m, k, t;
int a[3];
int main()
{
scanf("%d", &t);
while (t--)
{
memset(a, 0, sizeof(a));
scanf("%d", &n);
int v;
for (int i = 1;i <= n;i++)
{
scanf("%d", &v);
a[v%3]++;
}
if (a[1] > a[2])
swap(a[1], a[2]);
int res = a[0]+a[1]+(a[2]-a[1])/3;
cout << res << endl;
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 《HelloGitHub》第 106 期
· 数据库服务器 SQL Server 版本升级公告
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)