Codeforces Beta Round #42 (Div. 2)-C. Lucky Tickets

本文介绍了一个算法问题:如何从撕裂成两半并部分丢失的幸运票中,通过组合剩余的部分来最大化恢复幸运票的数量。文章提供了一种有效的算法解决方案,通过统计各个模数类别下的票数来确定最大可恢复的幸运票数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接

C. Lucky Tickets
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky.

When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest.

Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123and 99 can be glued in two ways: 12399 and 99123.

What maximum number of tickets could Vasya get after that?

Input

The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai(1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide.

Output

Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together.

Examples
input
3
123 123 99
output
1
input
6
1 1 1 23 10 3
output
1


把所有的数字对3取模,分别记录0, 1, 2的个数为h0, h1, h2, 那么说有h0个数已经是三的倍数那么他们两两组合形成h0/2张卡片,取模结果为1的有h1张,结果为2的有h2张,它们能形成min(h1, h2)张卡片

#include <bits/stdc++.h>
#define maxn 200005
using namespace std;
typedef long long ll;

int main(){
	
	int n, a;
	int k0 = 0, k1 = 0, k2 = 0;
	
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d", &a);
	    if(a % 3 == 0)
	     k0++;
	    else if(a % 3 == 1)
	     k1++;
	    else
	     k2++;
	}
	printf("%d\n", k0 / 2 + min(k1, k2));
	
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值