Input The first line of the input contains an integer T(1≤T≤10000) , denoting the number of test cases.In each test case, there is one integer n(1≤n≤100000) in the first line, denoting the number of nodes.In the second line, there are n integers h1,h2,...,hn(1≤hi≤109,h⌊i2⌋≤hi) , denoting the key of each node.It is guaranteed that ∑n≤106 .
Output For each test case, print a single line containing two integers S and E , denoting the final score of Sunset and Elephant.
Sample Input 1 3 1 2 3
Sample Output 4 2
Source 2019 Multi-University Training Contest 6 思路:一开始没看对题,忽略了子节点的值一定不比父节点的小,后来才看懂,明白他是个小顶堆... #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <cstdlib> #include <queue> #include <stack> #include <vector> using namespace std; #define MAXN 100010 #define ll long long int t, n; ll h[100000 + 8], sums, sume; int main() { scanf("%d", &t); while(t--) { sume = 0; sums = 0; scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%lld", &h[i]); sort(h, h+n, greater<ll>()); for(int i = 0; i < n; i += 2) sums += h[i]; for(int i = 1; i < n; i += 2) sume += h[i]; printf("%lld %lld\n", sums, sume); } return 0; }
转载于:https://www.cnblogs.com/RootVount/p/11357376.html
相关资源:JAVA上百实例源码以及开源项目