STL K - Equal Sums

题目
小A有 n 个整数数列 a1,a2,…an ,每个数列的长度为li。

请你找出两个编号不同的数列,并从这两个数列中各恰好删除一个数,使得这两个数列的和相等。

【输入格式】
输入格式如下:
n
l1
a1-1 a1-2 … a1-l1
l2
a2-1 a2-2 … a2-l2

ln
an-1 an-2 … an-ln

【输出格式】
如果不存在解,请在第一行输出"NO"(不含引号)。
否则,请按如下格式输出:
第一行输出"YES"(不含引号),
第二行输出两个整数 i,x ,表示选择的第一个数列编号为 i ,并删除其中的第 x 个数,
第三行输出两个整数 j,y ,表示选择的第二个数列编号为 j ,并删除其中的第 y 个数。

若存在多解,你可以输出任何一个。

【样例输入/输出】
编号 输入 输出
1
2
5
2 3 1 3 2
6
1 1 2 2 2 1
YES
2 6
1 2
2
3
1
5
5
1 1 1 1 1
2
2 3
NO
3
4
6
2 2 2 2 2 2
5
2 2 2 2 2
3
2 2 2
5
2 2 2 2 2
YES
2 2
4 1
【样例解释】
请自行计算。

【数据范围】
对于所有测试数据:
2 ≤ n ≤ 2×105
1 ≤ li < 2×105
∑li ≤ 2×105
-104 ≤ ai-j ≤ 104
用一个集合存之前出现过去掉一个数后的序列和,遍历长度为n的序列求和分别减去其中的数字,判断集合里面有没有相同的和,如果有记录答案。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<cstdio>
#include<vector>
#include<set>
#include<cstring>
#include<cstdlib>
#include <time.h>
#include<stack>
const int maxn=2e5+5;
using namespace std;
struct dd{
    int v,id;
};
int main()
{
    int k;cin>>k;
    map<int,pair<int,int>>mp;
    int a[maxn];
    pair<int,int>p1,p2;
    p1.first=-1;
    for(int i=0;i<k;i++){
        int n;cin>>n;
        int temp=0;
        for(int j=0;j<n;j++){
            cin>>a[j];
            temp+=a[j];
        }
        for(int j=0;j<n;j++){
            int sum=temp-a[j];
            auto it=mp.find(sum);
            if(it==mp.end())continue;
            p1=it->second;
            p2={i,j};
        }
        if(p1.first!=-1)break;
        for(int j=0;j<=n;j++){
            int sum=temp-a[j];
            mp.insert({sum,{i,j}});
        }
    }
    if(p1.first!=-1){
        cout<<"YES"<<endl;
        cout<<p1.first<<""<<p1.second<<endl;
        cout<<p2.first<<""<<p2.second<<endl;
    }
    else cout<<"NO"<<endl;
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值