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;
    }
    
  • 相关阅读:
    目前最大的IPV6安全风险有哪些?
    PHP中exec、system等函数调用linux命令问题
    ubuntu下LAMP环境安装
    Ubuntu配置Apache虚拟主机
    XML和YAML的区别与使用方法
    使用CURL访问站点的时候出现403的解决办法
    IPv6安装及使用手册
    SuperSlide2.1-滚动
    HTML常用手册
    关于Ajax参数的思考
  • 原文地址:https://www.cnblogs.com/EDGsheryl/p/9153681.html
Copyright © 2011-2022 走看看