题目描述
BSD is a lazy boy. He doesn‘t want to wash his socks, but he will have a data structure called ‘socks heap‘.By maintaining this structure, he can quickly pick out two of the most smelless socks from millions of socks everyday. As one of his good friends, you know the data structure of BSD, and want to predict ‘the air pollution level‘ of a certain day. Of course, it‘s BSD‘s socks that pollute the air.
We will enter numbers that indices for ‘socks smell level‘ and expect you to output the total amount of pollution that he will wear on the day of BSD design.
输入描述:
First line contain an integer T 1<T<=10 ,means the sum of test cases.
for every test cases,we will firstly give N(1<N<1000000) in a single line,means the count of socks.Next line Contains N numbers (you can use int to save all of them) ,means ‘smell level‘ for every socks.
输出描述:
please putout answer in a single line for every test cases.
输入
3 5 1 1 2 3 5 3 1 4 7 3 200 4 10000
输出
2 5 204
题解
找到最小值、次小值输出即可。
#include<cstdio> using namespace std; const double eps=1e-8; #define zero(x)(((x)>0?(x):(-x))<eps) const int maxn = 1000000 + 10; long long a[maxn]; int n; long long INF = 0x7FFFFFFF; int main() { int T; scanf("%d", &T); while(T --) { scanf("%d", &n); for(int i = 1; i <= n; i ++) { scanf("%lld", &a[i]); } long long ans = 0; long long num; int pos1 = -1, pos2 = -1; for(int i = 1; i <= n; i ++) { if(pos1 == -1) pos1 = i; if(a[i] < a[pos1]) pos1 = i; } for(int i = 1; i <= n; i ++) { if(pos1 == i) continue; if(pos2 == -1) pos2 = i; if(a[i] < a[pos2]) pos2 = i; } ans = a[pos1] + a[pos2]; printf("%lld\n", ans); } return 0; }