HDU 6168 Numbers 模拟

本文介绍了一种从混合序列中恢复原始序列的算法。通过不断选取最小元素并移除其与其他元素组合形成的所有可能值,逐步还原出初始序列。示例展示了输入为混合序列时,如何迭代找出原始序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

zk有一个序列,a1,a2,a3......an,对于每一个数对(i,j),满足1<=i<j<=n,
构成了一个新的数字(ai+aj),这些新的数字又构成了一个新的序列b_1,b_2,b_3.....b_n(n-1)/2
lsf给他找麻烦,把这两个序列混合成一个序列,让你找出来原来的序列a
例如:
input:
6
2 2 2 4 4 4
output:
2 2 2

分析

最小的数一定不是数组b中的,因为它是最小的,不可能由其他的相加产生,所以也就有了挑选的规则,每次挑选出最小的
数字,然后把其他的数字都跟它相加得到c,如果数组b中出现c就将b中的c删掉,
循环一遍以后,最小的数字所能构成的数字就都被删除,然后再次选择最小的元素,依次进行。
例如:
input:
21
1 2 3 3 4 4 5 5 5 6 6 6 7 7 7 8 8 9 9 10 11
output:
6
1 2 3 4 5 6
第一个最小的数字是1,将1拿出来,然后把1和剩下的数字分别相加,1+2 = 3,然后删除一个3;还有一个3,
1+3=4,删除一个4;还剩下一个4,1+4=5,删除一个5;.....1+9=10,删除一个10;1+11=12,没有12,不进行任何操作。
一遍之后的序列为:2 3 4 5 5 6 6 7 7 8 9 11;重复上面的操作。。

代码

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#define maxn 125255
using namespace std;
int num[maxn];
multiset<int> ms;
int main(){
    std::ios::sync_with_stdio(false);
    int m;
    while(cin>>m){
        memset(num,0,sizeof(num));
        ms.clear();
        int temp;
        for(int i = 0;i < m;i++){
            cin>>temp;
            ms.insert(temp);
        }
        int cnt = 0;
        while(ms.size()){
            int t = *ms.begin();
            ms.erase(ms.lower_bound(t));
            for(int i = 0; i < cnt;i++){
                ms.erase(ms.lower_bound(t+num[i]));
            }
            num[cnt++] = t;
        }
        cout<<cnt<<endl;
        for(int i = 0; i < cnt;i++){
            if(i>0){
                cout<<" ";
            }
            cout<<num[i];
        }
        cout<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值