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

Equal Sums

这里写图片描述

题意:给你几个数列,问是否存在这样两个数列使得他们删去各自的某一个成员后二者的和相等。

因为题目限制了 n i <=2e5,所以这题的解法很多。

感觉自己最初的解法有点复杂了,在这里我讲一个STL应用的解法。

我们定义 s u m i 为第 i 行的和, i d i == i p o s 表示第 i 行的第 p o s 个.

m a p < s u m i , p a i r < i d i , p o s >> 这样便简单的存下了我们需要的所有信息。

对于每一个新的行,我们找前面是否出现过 s u m i 等于我现在的某一个 s u m j
只需 m p . l o w e r _ b o u n d ( s u m j ) 返回迭代器即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<limits.h>
#include<string.h>
#include<map>
#include<list>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
#define eps double(1e-6)
#define pi acos(-1.0)
#define lson  root << 1
#define rson  root << 1 | 1



map< int,pair<int ,int> >mmp;

map< int ,pair<int,int> >::iterator it;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int zu;
    mmp.clear();
    cin>>zu;
    for(int t=1;t<=zu;t++)
    {
        int n;
        cin>>n;
        vector<int> v;
        int sum=0;
        int x;
        for(int i=0;i<n;i++)
            cin>>x,v.push_back(x),sum+=x;
        for(int i=0;i<n;i++)
        {
            int ans=sum-v[i];
            if(t!=1)
            {
                it=mmp.lower_bound(ans);
                if(it!=mmp.end()&&it->first==ans&&it->second.first!=t)
                {
                    cout<<"YES"<<endl;
                    cout<<it->second.first<<" "<<it->second.second<<endl;
                    cout<<t<<" "<<i+1<<endl;
                    return 0;
                }

            }
            mmp[ans]=make_pair(t,i+1);
        }
    }
    cout<<"NO"<<endl;
}

猜你喜欢

转载自blog.csdn.net/murphyc/article/details/80571866