Turtle Math: Fast Three Task

Turtle Math: Fast Three Task

题面翻译

t t t 组数据,每组数据给定 n n n 个数 a i a_i ai,现有两个操作:

  1. 选择一个数将其删除
  2. 选择一个数将其加 1 1 1

问最少需要几次操作使得剩余的所有数之和为 3 3 3 的倍数。

题目描述

You are given an array a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an .

In one move, you can perform either of the following two operations:

  • Choose an element from the array and remove it from the array. As a result, the length of the array decreases by 1 1 1 ;
  • Choose an element from the array and increase its value by 1 1 1 .

You can perform any number of moves. If the current array becomes empty, then no more moves can be made.

Your task is to find the minimum number of moves required to make the sum of the elements of the array a a a divisible by 3 3 3 . It is possible that you may need 0 0 0 moves.

Note that the sum of the elements of an empty array (an array of length 0 0 0 ) is equal to 0 0 0 .

输入格式

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104 ) — the number of test cases.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 1n105 ).

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 4 1 \le a_i \le 10^4 1ai104 ).

The sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105 .

输出格式

For each test case, output a single integer: the minimum number of moves.

样例 #1

样例输入 #1

8
4
2 2 5 4
3
1 3 2
4
3 7 6 8
1
1
4
2 2 4 2
2
5 5
7
2 4 8 1 9 3 4
2
4 10

样例输出 #1

1
0
0
1
1
2
1
1

提示说明

In the first test case, initially the array a = [ 2 , 2 , 5 , 4 ] a = [2, 2, 5, 4] a=[2,2,5,4] . One of the optimal ways to make moves is:

  • remove the current 4 4 4 th element and get a = [ 2 , 2 , 5 ] a = [2, 2, 5] a=[2,2,5] ;

As a result, the sum of the elements of the array a a a will be divisible by 3 3 3 (indeed, a 1 + a 2 + a 3 = 2 + 2 + 5 = 9 a_1 + a_2 + a_3 = 2 + 2 + 5 = 9 a1+a2+a3=2+2+5=9 ).In the second test case, initially, the sum of the array is 1 + 3 + 2 = 6 1+3+2 = 6 1+3+2=6 , which is divisible by 3 3 3 . Therefore, no moves are required. Hence, the answer is 0 0 0 .

In the fourth test case, initially, the sum of the array is 1 1 1 , which is not divisible by 3 3 3 . By removing its only element, you will get an empty array, so its sum is 0 0 0 . Hence, the answer is 1 1 1 .

代码内容

// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//队列
// #include <queue>//堆/优先队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll N=1e5+10;
ll a[N];

void solve()
{
    ll n;
    cin>>n;
    
    memset(a,0,sizeof a);
    
    if(n==1)
    {
        cin>>a[0];
        if(a[0]%3==0) cout<<0<<endl;
        else cout<<1<<endl;
        return;
    }

    ll sum=0,op=0;
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]%3==1) op=1;
        sum+=a[i];
    }
    if(sum%3==0) cout<<0<<endl;
    else if(sum%3==2) cout<<1<<endl;
    else
    {
        if(op) cout<<1<<endl;
        else cout<<2<<endl;
    }
}

int main()
{
    ll t;
    cin>>t;

    while(t--) solve();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pretty Boy Fox

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值