HDU 6168 - Numbers 水题 2017 Multi-University Training Contest - Team 9

题目链接: HDU 6168 - Numbers

题目大意

数组a大小为n, 对于所有 i,j,1i<jn , 构成一个新数字a[i]+a[j], 放到b数组中 
最后将两个数组混在一起打乱, 给你打乱后的数, 求原来的a数组

思路

混合数组中最小两个数字一定是a中的数字, 因为没有比它们更小的数字来构成它们 
所以用一个multset存储所有数字, 这样每次取出来都是最小的数字并且方便删除 
先取出第一个数字, 放到数组a中, 并删除第一个数字 
然后从multset中一直取最小的数字(*S.begin()), 放到a数组中, 并从multset中删除它 
然后循环遍历a数组所有元素, 和刚取出来的那个数字相加, 得到的数字就是b数组中的一个数字, 从multset中erese掉(S.erase(S.lower_bound(ans[cnt-1]+ans[i])); 不能erase(ans[cnt-1]+ans[i]), 这样会把所有相同的值都删掉, 我们只需要删一个)

代码

858MS 7680K

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 125250 + 100;
int n, m, ans[maxn], cnt;

int main()
{
    while(scanf("%d", &m) == 1)
    {
        multiset<int> S;
        for(int i=0; i<m; ++i)
        {
            int t;
            scanf("%d", &t);
            S.insert(t);
        }

        cnt = 0;
        ans[cnt++] = *S.begin();
        S.erase(S.begin());
        while(!S.empty())
        {
            ans[cnt++] = *S.begin();
            S.erase(S.begin());
            for(int i=0; i<cnt-1; ++i)
            {
                S.erase(S.lower_bound(ans[cnt-1]+ans[i]));
            }
        }

        printf("%d\n", cnt);
        for(int i=0; i<cnt; ++i) 
            printf("%d%c", ans[i], i==cnt-1 ? '\n' :' ');
    }

    return 0;
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值