Quantcast
Channel: プログラミング
Viewing all articles
Browse latest Browse all 7930

【AtCoder】ABC 378 A - Pairing | 茶コーダーが解くAtCoder - Yuulis.log

$
0
0

atcoder.jp

実行時間制限: 2 sec / メモリ制限: 1024 MB / Difficulty: 22

問題概要

4個のボールがあり、  i個目のボールの色は  A_iです。同じ色のボールを2つ選び両方捨てるという操作を最大何回行えるか求めてください。

制約

 A_1, A_2, A_3, A_4はそれぞれ整数で、  1以上  4以下。

考察

 cのボールの個数を  x_cとすると、  \displaystyle \sum_c \left\lfloor \frac{x_c}{2} \right\rfloorが答えとなる。

 x_cの管理には、keyを色  cvalue x_cとしたmapを使った。もちろんvectorで処理してもよい。

コード

#include <bits/stdc++.h>usingnamespacestd;

// ======================================== //intmain()
{
    int A1, A2, A3, A4;
    cin>> A1 >> A2 >> A3 >> A4;

    map<int, int> mp;
    mp[A1]++;
    mp[A2]++;
    mp[A3]++;
    mp[A4]++;

    int ans = 0;
    for (auto&&m : mp)
    {
        if (m.second > 1)
            ans += m.second / 2;
    }

    cout<< ans << endl;
}

atcoder.jp

実装時間: 5分


Viewing all articles
Browse latest Browse all 7930

Trending Articles