[CF1077C]Good Array

题目大意:一个序列是好的当且仅当有一个数是其它所有数的和,问一个序列可以删掉哪个数变成好的序列。输出所有方案。

题解:发现等于其他数的和的那个数一定是其中最大的,只要排序一下(其实只要找到最大的两个数),就可以$O(1)$判断是否合法

卡点:

C++ Code:

#include <cstdio>
#include <vector>
#include <algorithm>
#define maxn 200010
std::vector<int> ans;
int n, s[maxn], rnk[maxn];
long long sum;
inline bool cmp(int a, int b) {return s[a] > s[b];}
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", s + i), rnk[i] = i, sum += s[i];
	std::sort(rnk + 1, rnk + n + 1, cmp);
	for (int i = 1; i <= n; i++) {
		int MAX = i == 1 ? s[rnk[2]] : s[rnk[1]];
		if (sum - s[rnk[i]] - MAX == static_cast<long long> (MAX)) {
			ans.push_back(rnk[i]);
		}
	}
	std::sort(ans.begin(), ans.end());
	printf("%d
", ans.size());
	for (std::vector<int>::iterator it = ans.begin(); it != ans.end(); it++) printf("%d ", *it);
	puts("");
	return 0;
}

  

原文地址:https://www.cnblogs.com/Memory-of-winter/p/9984888.html