zoukankan      html  css  js  c++  java
  • Codeforces1176B(B题)Merge it!

    B. Merge it!

    You are given an array aanna1,a2,,ana1,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][3,4][3,4][1,6][1,6][2,5][2,5]

    Your task is to find the maximum possible number of elements divisible by 33

    You have to answer tt

    Input

    The first line contains one integer tt1t10001≤t≤1000

    The first line of each query contains one integer nn1n1001≤n≤100

    The second line of each query contains nna1,a2,,ana1,a2,…,an1ai1091≤ai≤109

    Output

    For each query print one integer in a single line — the maximum possible number of elements divisible by 3

    代码:

     1 #include<iostream>
     2 #include<algorithm>
     3 using namespace std;
     4 int main() {
     5     int n,m;
     6     cin>>n;
     7     for(int i=0; i<n; i++) {
     8         int cnt=0,cnt1=0,a1=0,b1=0;
     9         int a[105];
    10         int b[105];
    11         cin>>m;
    12         for(int j=0; j<m; j++) {
    13             cin>>a[j];
    14             if(a[j]%3==0) {
    15                 cnt++;
    16             } else {
    17                 a[j]%=3;
    18                 b[cnt1++]=a[j];
    19             }
    20         }
    21         for(int i=0; i<cnt1; i++) {
    22             if(b[i]==1) {
    23                 a1++;
    24             }
    25             if(b[i]==2) {
    26                 b1++;
    27             }
    28         }
    29         if(a1>b1) {
    30             int h=a1-b1;
    31             if(h>=3) {
    32                 cnt+=h/3;
    33             }
    34             cnt+=b1;
    35         } else {
    36             int h=b1-a1;
    37             if(h>=3) {
    38                 cnt+=h/3;
    39             }
    40             cnt+=a1;
    41         }
    42         cout<<cnt<<endl;
    43     }
    44 }

    思路分析:若本身可整除3就加1,将不可整除3的数模3,都转化成1和2,比较1和2的数量,1比2多那就配对2数量个3,然后看1比2多了几个,如果超过3,看有多少个3,再记数。

    链接:https://codeforces.com/contest/1176/problem/B

  • 相关阅读:
    MATLAB accumarray
    函数rand,randn,randi
    bsxfun
    sub2ind函数
    MAX
    & 和 &&
    matlab函数int2str, num2str, str2num
    ASCII对照表
    STM32的ADC配置
    单节锂电池基本知识
  • 原文地址:https://www.cnblogs.com/yuanhang110/p/11267914.html
Copyright © 2011-2022 走看看