Codeforces Round #486 (Div. 3) C. Equal Sums
Posted edgsheryl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #486 (Div. 3) C. Equal Sums相关的知识,希望对你有一定的参考价值。
Codeforces Round #486 (Div. 3) C. Equal Sums
题目连接:
http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/C
Description
You are given k sequences of integers. The length of the i-th sequence equals to ni.
You have to choose exactly two sequences i and j (i≠j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to ni?1) equals to the sum of the changed sequence j (its length will be equal to nj?1).
Note that it‘s required to remove exactly one element in each of the two chosen sequences.
Assume that the sum of the empty (of the length equals 0) sequence is 0.
Sample Input
2
5
2 3 1 3 2
6
1 1 2 2 2 1
Sample Output
YES
2 6
1 2
题意
给定几个序列,问是否存在两个序列各删除一个元素后的和相等。
题解:
操作一个序列,将所有去掉一个元素之后的值保存下来,然后用 map 来检索是否存在答案。时间复杂度O(nlgn)
代码
#include <bits/stdc++.h>
using namespace std;
int T;
vector<int> v;
int n;
map<int, pair<int, int> > m;
int main() {
cin >> T;
for (int ii = 1; ii <= T; ii++) {
cin >> n;
v.clear();
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
int sum = 0;
for (auto i:v) sum += i;
for (int i = 0; i < v.size(); i++) {
if (!m.count(sum - v[i]) || m[sum - v[i]].first == ii) {
m[sum - v[i]] = make_pair(ii, i+1);
} else {
cout << "YES" << endl;
cout << ii << " " << i+1 << endl;
cout << m[sum - v[i]].first << " " << m[sum - v[i]].second;
return 0;
}
}
}
cout << "NO" << endl;
}
以上是关于Codeforces Round #486 (Div. 3) C. Equal Sums的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #486 (Div. 3)完结
Codeforces Round #486 (Div. 3) A. Diverse Team
Codeforces Round #486 (Div. 3) C. Equal Sums
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas