Codeforces Round #486 (Div. 3)-C. Equal Sums
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.
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.
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.
2
5
2 3 1 3 2
6
1 1 2 2 2 1
YES
2 6
1 2
3
1
5
5
1 1 1 1 1
2
2 3
NO
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
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.
思路:好吧,这道题还是比较水的(原谅我的菜),本题的题意大概就是给N个序列,问里面是否有两个序列,在这两个序列中去掉其中一个数后他们的和相同。当时自己以想。。。WC序列这么多,怎么办???要是按照每个序列存下每一种可能map绝对炸啊。后来经过大佬指点,其实这个题并不难,原因就是我只需要找到一组即可,并且我考虑的是:和去掉一个元素的数值,我可以在每个序列输入完以后得到和,再循环一次得到减去一个元素的集合,然后保存每一种可能,并把它所在的序列号,和位置都存下来即可。然后后面的循环中判断是否出现过这个数字。出现过就不循环进行以上操作,直接把剩余数输入即可,然后输出这种情况,要注意有可能在自己序列中出现和自己相同的情况,需要舍去。
实现:由于值有可能为负数,因此判断这个值是否存在,可以用一个map映射即可,对于存这个数值所在的行和列,由于数值的行和列,可以结构体数组或者二维map即可
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<map> 5 using namespace std; 6 const int maxx=200005; 7 map<int,map<int,int> >p;//存这个数值对应所在的序列和序列中的位置 8 map<int,int>vis;//是否前面有和这个数值一样的数 9 int main() 10 { 11 int k,n,sum; 12 int a[maxx]; 13 while(~scanf("%d",&k)) 14 { 15 sum=0; 16 int flag=0; 17 vis.clear(); 18 int ans11,ans12,ans21,ans22; 19 for(int i=1;i<=k;i++) 20 { 21 scanf("%d",&n); 22 sum=0; 23 for (int j=1; j<=n; j++) 24 { 25 scanf("%d",&a[j]); 26 sum+=a[j]; 27 } 28 if (flag)continue;//如果已经找到这组数值就不进行下次操作 29 for(int j=1;j<=n;j++) 30 { 31 int tmp=sum-a[j]; 32 if (vis[tmp]==0 || p[tmp][1]==i)//如果这个值没有存在过 或者 这个值相同的出现在同一个序列中 33 { 34 vis[tmp]=1; 35 p[tmp][1]=i; 36 p[tmp][2]=j; 37 } 38 else//存在所在序列和位置 39 { 40 flag=1; 41 ans11=p[tmp][1]; 42 ans12=p[tmp][2]; 43 ans21=i; 44 ans22=j; 45 } 46 } 47 } 48 if (flag==0)printf("NO\n"); 49 else 50 { 51 printf("YES\n"); 52 printf("%d %d\n",ans11,ans12); 53 printf("%d %d\n",ans21,ans22); 54 } 55 } 56 return 0; 57 }