题目链接
C. Equal Sums
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given kk sequences of integers. The length of the ii-th sequence equals to nini.
You have to choose exactly two sequences ii and jj (i≠ji≠j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence ii (its length will be equal to ni?1ni?1) equals to the sum of the changed sequence jj (its length will be equal to nj?1nj?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 00) sequence is 00.
Input
The first line contains an integer kk (2≤k≤2?1052≤k≤2?105) — the number of sequences.
Then kk pairs of lines follow, each pair containing a sequence.
The first line in the ii-th pair contains one integer nini (1≤ni<2?1051≤ni<2?105) — the length of the ii-th sequence. The second line of the ii-th pair contains a sequence of nini integers ai,1,ai,2,…,ai,niai,1,ai,2,…,ai,ni.
The elements of sequences are integer numbers from ?104?104 to 104104.
The sum of lengths of all given sequences don't exceed 2?1052?105, i.e. n1+n2+?+nk≤2?105n1+n2+?+nk≤2?105.
Output
If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers ii, xx (1≤i≤k,1≤x≤ni1≤i≤k,1≤x≤ni), in the third line — two integers jj, yy (1≤j≤k,1≤y≤nj1≤j≤k,1≤y≤nj). It means that the sum of the elements of the ii-th sequence without the element with index xx equals to the sum of the elements of the jj-th sequence without the element with index yy.
Two chosen sequences must be distinct, i.e. i≠ji≠j. You can print them in any order.
If there are multiple possible answers, print any of them.
Examples
input
Copy
2
5
2 3 1 3 2
6
1 1 2 2 2 1
output
Copy
YES
2 6
1 2
input
Copy
3
1
5
5
1 1 1 1 1
2
2 3
output
Copy
NO
input
Copy
4
6
2 2 2 2 2 2
5
2 2 2 2 2
3
2 2 2
5
2 2 2 2 2
output
Copy
YES
2 2
4 1
Note
In the first example there are two sequences [2,3,1,3,2][2,3,1,3,2] and [1,1,2,2,2,1][1,1,2,2,2,1]. You can remove the second element from the first sequence to get [2,1,3,2][2,1,3,2] and you can remove the sixth element from the second sequence to get [1,1,2,2,2][1,1,2,2,2]. The sums of the both resulting sequences equal to 88, i.e. the sums are equal.
算法分析:
题意:
给你多对数组,让你任选两个数组,删掉一个元素,保证和相同,找到输出YES,并且输出第几个数组和删除元素在第几个位置,找不到输出NO。
分析:
一个神器map<int,pair<int,int> > m // 表示 key:总和 value:第几个数组,数组的第几位,注意:这里的总和是,每一个数组任意
删除一个元素的剩下的和。
map可以实现快速查找,这是是map的一大好处。
改变题意:
1.无非想到删除两个或三个元素怎么办。
代码实现:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
using namespace std;
const double eps = 1e-8;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN=40010;
const int MAXM=100010;
const int M=500;
vector<int> v;
map<int,pair<int,int> >m;
int main()
{int n,t;cin>>t;for(int i=1;i<=t;i++){int n;int sum=0;scanf("%d",&n);for(int j=1;j<=n;j++){int temp;scanf("%d",&temp);sum+=temp;v.push_back(temp);}for(int j=0;j<n;j++){if(m.find(sum-v[j])!=m.end()){if(i!=m[sum-v[j]].first){cout<<"YES"<<endl;cout<<m[sum-v[j]].first<<" "<<m[sum-v[j]].second+1<<endl;cout<<i<<" "<<j+1<<endl;return 0;}}else {m[sum-v[j]]=make_pair(i,j);}}v.clear();}cout<<"NO"<<endl;return 0;
}