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

  • 相关阅读:
    3524: [Poi2014]Couriers -- 主席树
    bzoj 2190: [SDOI2008]仪仗队 -- 欧拉函数
    模板 -- 树链剖分
    bzoj 1823: [JSOI2010]满汉全席 -- 2-sat
    bzoj 1704: [Usaco2007 Mar]Face The Right Way 自动转身机 -- 贪心
    bzoj 1231: [Usaco2008 Nov]mixup2 混乱的奶牛 -- 状压DP
    redis 主从复制
    redis 事务
    redis持久化——AOF
    redis 持久化 ——RDB
  • 原文地址:https://www.cnblogs.com/yuanhang110/p/11267914.html
Copyright © 2011-2022 走看看