码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #486 (Div. 3) C. Equal Sums

时间:2018-06-08 10:32:29      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:题意   时间   +=   rem   round   sequence   -o   保存   lse   

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

标签:题意   时间   +=   rem   round   sequence   -o   保存   lse   

原文地址:https://www.cnblogs.com/EDGsheryl/p/9153681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!