zoukankan      html  css  js  c++  java
  • Codeforces Round #565 (Div. 3) B. Merge it!

    Codeforces Round #565 (Div. 3) 

    B. Merge it!

    You are given an array aa consisting of nn 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][2,1,4] you can obtain the following arrays: [3,4][3,4], [1,6][1,6] and [2,5][2,5].

    Your task is to find the maximum possible number of elements divisible by 33 that are in the array after performing this operation an arbitrary (possibly, zero) number of times.

    You have to answer t independent queries.

    Input

    The first line contains one integer tt (1≤t≤1000) — the number of queries.

    The first line of each query contains one integer n (1≤n≤100).

    The second line of each query contains n integers a1,a2,…,ana1,a2,…,an (1≤ai≤109).

    Output

    For each query print one integer in a single line — the maximum possible number of elements divisible by 3 that are in the array after performing described operation an arbitrary (possibly, zero) number of times.

    Example

    input

    2

    5

    3 1 2 3 1

    7

    1 1 1 1 1 2 2

    output

    3

    3

    Note

    In the first query of the example you can apply the following sequence of operations to obtain 33 elements divisible by 33: [3,1,2,3,1]→[3,3,3,1][3,1,2,3,1]→[3,3,3,1].

    In the second query you can obtain 33 elements divisible by 33 with the following sequence of operations: [1,1,1,1,1,2,2]→[1,1,1,1,2,3]→[1,1,1,3,3]→[2,1,3,3]→[3,3,3][1,1,1,1,1,2,2]→[1,1,1,1,2,3]→[1,1,1,3,3]→[2,1,3,3]→[3,3,3].

     

    题意:这题意思就是给你一个数组,你可以无数次操作(把两个数合并成他们的和),问你这个数组最多能有多少个被3整除的数。

    思路:这显然是一道数学题,可以用三个变量记录数组中被3整除、余1、余2的个数,

    然后考虑到最大,则最好每个余1的都跟余2的组合,剩下的余1的或余2的如果满足3个则又能组成一组......

     

     

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<map>
     7 #include<set>
     8 #include<vector>
     9 using namespace std;
    10 #define ll long long
    11 //const int maxn=;
    12 int main()
    13 {
    14     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    15     int T;
    16     cin>>T;
    17     int n,num;
    18     while(T--)
    19     {
    20         cin>>n;
    21         ll int a=0,b=0,c=0;
    22         for(int i=0;i<n;i++)
    23         {
    24             cin>>num;
    25             if(num%3==0)
    26                 a++;
    27             else if(num%3==1)
    28                 b++;
    29             else if(num%3==2)
    30                 c++;
    31         }
    32         ll int ans=0;
    33         ans+=a;//第一部分 
    34         ans+=min(b,c);//第二部分 
    35         ans+=(b-min(b,c))/3+(c-min(b,c))/3;//第三部分 
    36         cout<<ans<<endl;
    37     }
    38     return 0;
    39 }
    大佬见笑,,
  • 相关阅读:
    轻松学习Linux之AWK使用初步
    轻松学习Linux之理解Shell的硬链接与软连接
    轻松学习Linux之自动执行任务
    轻松学习Linux系统安装篇之fdisk命令行工具的使用
    Leetcode-1030 Next Greater Node In Linked List(链表中的下一个更大节点)
    Leetcode-1028 Convert to Base -2(负二进制转换)
    Leetcode-1029 Binary Prefix Divisible By 5(可被 5 整除的二进制前缀)
    ACM模板——2的次方表
    ACM模板——快速幂
    ACM模板——素数相关
  • 原文地址:https://www.cnblogs.com/xwl3109377858/p/11000509.html
Copyright © 2011-2022 走看看