codeforces988C(map简单运用)

题目链接: http://codeforces.com/problemset/problem/988/C
题意:给你k个长度为n[i]的序列,让你每个序列去掉一个数判断这n个序列中是否有两个和相等的序列,如果有,输出其中两个序列的编号以及他们去掉数字在相应序列的位置。
思路:对于每个序列直接累加和,然后用一个map记录序列的id,一个map记录序列和,标记那些和出现过,如果已经出现过了并且不再同一个序列中则可以输出相应答案,另一个map记录所删除序列数字的位置。
那么为什么不用数组代替map呢?观察所给数据范围,数字为-10^4~10^4,开数组不用多说,直接RE(这里是指内存访问违规)
代码:https://paste.ubuntu.com/p/2XNWnTsgvk/

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
typedef long long LL;
LL a[maxn];
map<LL,int>mp1;
map<LL,int>mp2;
map<LL,int>mp3;
int main()
{
    int k;
    while(~scanf("%d",&k))
    {
        bool flag=false;
        int num1,num2;
        int num3,num4;
        for(int i=1; i<=k; i++)
        {
            int n;
            scanf("%d",&n);
            LL sum=0;
            for(int j=1; j<=n; j++)
            {
                scanf("%lld",&a[j]);
                sum+=a[j];
            }
            for(int j=1; j<=n; j++)
            {
                LL num=sum-a[j];
                if(mp1[num])
                {
                    if(mp2[num]!=i)
                    {
                        num1=i,num2=j;
                        num3=mp2[num];
                        num4=mp3[num];
                        flag=true;
                    }
                }
                else
                {
                    mp1[num]=1;
                    mp2[num]=i;
                    mp3[num]=j;
                }
            }
        }
        if(!flag) puts("NO");
        else
        {
            puts("YES");
            printf("%d %d\n",num1,num2);
            printf("%d %d\n",num3,num4);
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/dl962454/article/details/80726267