zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    bzoj 1208: [HNOI2004]宠物收养所 (Treap)
    Bzoj 2431: [HAOI2009]逆序对数列 (DP)
    Bzoj 1055: [HAOI2008]玩具取名 (区间DP)
    线段树入门详解
    Bzoj 1087: [SCOI2005]互不侵犯King
    Bzoj 2748: [HAOI2012]音量调节 (DP)
    Bzoj 2752 高速公路 (期望,线段树)
    惨淡的模拟赛
    GSS4
    Bzoj 近期题目一句话题解
  • 原文地址:https://www.cnblogs.com/EDGsheryl/p/9153681.html
Copyright © 2011-2022 走看看