HDU 6168 Numbers 模拟

题意:

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;
}

猜你喜欢

转载自blog.csdn.net/shensiback/article/details/80088877