CF988 C. Equal Sums

题目链接:http://codeforces.com/problemset/problem/988/C
题意:给n个数列,若存在两个不一样的数列,两者各去掉一项后值一样那么输出YES,并且输出这两个数列的编号和对应项的序号
题解:使用map存储,键:每一个数列的和除去每一项,值:对应的编号和序号队(使用pair)

#include<iostream>
#include<map>
#include<vector>
using namespace std;
map<int,pair<int,int> >m;
vector<int>arr;
int main()
{
    int k;
    cin>>k;
    for(int i = 0; i < k; i ++){
        int n,tmp,sum = 0;
        cin>>n;
        for(int j = 0; j < n; j ++){
            cin>>tmp;
            arr.push_back(tmp);
            sum += tmp;
        }
        for(int j= 0; j < n; j ++){
            if(m.find(sum - arr[j]) == m.end()){
                m[sum-arr[j]] = make_pair(i + 1, j + 1);
            }
            else{
                if((i + 1) != m[sum-arr[j]].first){
                    cout<<"YES"<<endl;
                    cout<<m[sum-arr[j]].first<<" "<<m[sum-arr[j]].second<<endl;
                    cout<<i + 1<<" "<<j + 1;
                    return 0;
                }
            }
        }
        arr.clear();

    }
    cout<<"NO"<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/goxy/p/9152021.html