Codeforces_962D_Merge Equals(STL map)

传送门

题意:给你n个数,然后如果同一个数出现过两次或者多次的话,从左边开始往右边,把第一个删掉,第二个变成2倍的值。
思路:强大的STL!map和set搞一搞就行。

#include<bits/stdc++.h>
#define debug(a) cout << #a << " " << a << endl
#define LL long long
#define ull unsigned long long
#define PI acos(-1.0)
#define eps 1e-6
const int N=2e5+7;
using namespace std;
LL a[N],ans[N];
map<LL,set<int>>mp;
int main ()
{
    //yyy_3y
    //freopen("1.in","r",stdin);
    int n;scanf("%d",&n);
    for(LL i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        mp[a[i]].insert(i);
    }
    LL ret=0;
    for(auto &it:mp){
        while(it.second.size()>=2){
            it.second.erase(it.second.begin());
            mp[it.first*2].insert(*it.second.begin());
            it.second.erase(it.second.begin());
        }
        if(it.second.size()==1){
            ans[*it.second.begin()]=it.first;
            ret++;
        }
    }
    printf("%lld\n",ret);
    for(auto &i:ans){
        if(i) printf("%lld ",i);
    }
    return 0;
}


#include<bits/stdc++.h>
#define debug(a) cout << #a << " " << a << endl
#define LL long long
#define ull unsigned long long
#define PI acos(-1.0)
#define eps 1e-6
const int N=2e5+7;
using namespace std;
map<LL,int>mp;
LL a[N];
int main ()
{
    //yyy_3y
    //freopen("1.in","r",stdin);
    int n;scanf("%d",&n);
    int ret=0;
    for(LL i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        while(mp[a[i]]){
            a[mp[a[i]]]=0;
            mp[a[i]]=0;
            a[i]*=2;
            ret++;
        }
        mp[a[i]]=i;
    }
    printf("%d\n",n-ret);
    for(auto &i:a) if(i) printf("%lld ",i);
    return 0;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值