Numbers

题目链接

题意

有一个数列$a$,从中任意挑出两个相加,可以形成一个新的数列$b$。
现在把两个数列混在一起,问哪些是属于数列$a$的。

思路

贪心

因为所有数字都是正数,所以最小的一个数字一定是数列$a$的。
我们可以把所有备用的已知的和记录一下,然后获得的数字如果是我们已经记录下来的要出现的,那么就把那个数字除掉。
否则就是$a$数列的,其实每一次就是看新的数字是不是在我们已知的$b$数列范围里。

Code

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
39
40
41
42
43
44
45
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#ifndef ONLINE_JUDGE
#define dbg(x...) do{cout << "\033[33;1m" << #x << "->" ; err(x);} while (0)
void err(){cout << "\033[39;0m" << endl;}
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x){for (auto v: a) cout << v << ' '; err(x...);}
template<typename T, typename... A>
void err(T a, A... x){cout << a << ' '; err(x...);}
#else
#define dbg(...)
#endif
#define inf 1ll << 50

const int N = 505;
int a[N], to[125500];
priority_queue<int, vector<int>, greater<int> > pq;
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
while (!pq.empty())
pq.pop();
int cur = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &to[i]);
if (!pq.empty() && to[i] == pq.top())
{
pq.pop();
continue;
}
for (int j = 1; j <= cur; j++)
pq.push(to[i] + a[j]);
a[++cur] = to[i];
//dbg(cur, a[cur]);
}
printf("%d\n", cur);
for (int i = 1; i <= cur; i++)
printf("%d%c", a[i], i == cur? '\n':' ');
}
return 0;
}
0%