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 }
    大佬见笑,,
  • 相关阅读:
    搜狗输入法体验评价
    找水王
    onActivityResult方法中返回码resultCode始终为RESULT_CANCEL(0)的问题排查
    win10平台Android平台签名证书(.keystore)生成出现乱码问题
    Toolbar融入状态栏实现沉浸式遇到的问题
    使用MuMu模拟器调试AndroidStudio项目
    解决Android Studio在同步sync时各种can't resolve *** 问题
    Android Studio解决新建项目时support:appcompat问题
    使用ActivityLifecycleCallbacks对活动监听和管理
    butterknife的使用
  • 原文地址:https://www.cnblogs.com/xwl3109377858/p/11000509.html
Copyright © 2011-2022 走看看