牛客小白月赛4 J 强迫症 思维

链接:https://www.nowcoder.com/acm/contest/134/J
来源:牛客网

题目描述

铁子最近犯上了强迫症,他总是想要把一个序列里的元素变得两两不同,而他每次可以执行一个这样的操作,他可以选择序列里的任意两个元素相加,不妨记作ai和aj,然后把ai+aj放进序列里,再删掉ai和aj其中的随便一个,问最少操作多少次可以完成铁子的愿望?

输入描述:

第一行一个整数n表示序列的长度(1≤n≤10
5
)
第二行n个整数a
i
表示序列的每个整数(1≤a
i
≤10
9
)

输出描述:

输出一行表示答案
示例1

输入

复制
3
1 2 2

输出

复制
1

说明

将序列的第1个整数和序列的第2个整数相加,再删掉第2个整数。

分析:因为每次操作是在有相同数的情况下合并两个数,直到最后没有相同的数,所以我们每次合并的肯定是相同的数,所以直接加上每个数重复的个数-1就好了
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e6+10;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
int main() {
    ios::sync_with_stdio(0);
    ll n, ans = 0;
    cin >> n;
    map<ll,ll> mp;
    for( ll i = 0, x; i < n; i ++ ) {
        cin >> x;
        mp[x] ++;
    }
    for( auto i : mp ) {
        ans += i.second - 1;
    }
    cout << ans << endl;
    return 0;
}

  

原文地址:https://www.cnblogs.com/l609929321/p/9531934.html