Sheryl's ACM

一条默默划水的咸鱼

导航

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;
}

posted on 2018-06-08 05:48  EDGsheryl  阅读(227)  评论(0编辑  收藏  举报