【CodeForces - 988C 】Equal Sums (思维,STLmap,STLset,tricks)_小a有 n 个整数数列 a1,a2,…an ,每个数列的长度为li。 请你找出两个编号不同的数列,并-程序员宅基地

技术标签: STL  思维  Codeforce~  

题干:

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

2
5
2 3 1 3 2
6
1 1 2 2 2 1

Output

YES
2 6
1 2

Input

3
1
5
5
1 1 1 1 1
2
2 3

Output

NO

Input

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

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.

 

题目大意:

给你 n 个整数数列 a1,a2,…an ,每个数列的长度为Li。

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

解题报告:

  首先看到读入这么多数据,所以肯定不能用数组保存下来在离线处理,肯定是边读入边判断的,这种题通常是读入的同时,用数据结构记录一些信息,然后用后面读入的数去判断的。这样就很容易想到,需要记录的信息就是有哪些数字是可用的,题意都不用转化,模拟题意就行了:删去一个数求和。所以先记录下sum,然后再减去每一个数,用map记录下是第几个序列的第几个位置,方便后来查询的时候输出答案。

  然后为了处理不在同一个序列中,这个问题,刚开始想把序列离散化了,后来想了想不行,,因为还需要记录当前数的位置,所以干脆直接判断原来那个数是否也是第i个序列。或者还有一种处理方法

AC代码:(156ms,改成uset则124ms)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
typedef pair<int,int> PII;//pair<第i个序列 , 第j个字符>
const int MAX = 2e5 + 5;
int a[MAX];
map<int,PII> mp;
set<int> ss;
int main()
{
	int n,len,flag = 0;
	int ans1,ans2,ans3,ans4;
	cin>>n;
	for(int i = 1; i<=n; i++) {
		scanf("%d",&len);
		int sum = 0;
		for(int j = 1; j<=len; j++) scanf("%d",a+j),sum += a[j];
		if(flag) continue;
		for(int j = 1; j<=len; j++) {
			int tmp = sum - a[j];
			if(ss.count(tmp) && mp[tmp].fi != i) {
				flag = 1;
				ans1 = mp[tmp].fi; ans2 = mp[tmp].se;
				ans3 = i; ans4 = j;
			}
			else {
				ss.insert(tmp);
				mp[tmp] = pm(i,j);
			}
		}
		
	} 
	if(flag) {
		puts("YES");
		printf("%d %d\n%d %d\n",ans1,ans2,ans3,ans4);
	}
	else {
		puts("NO");
	}
	return 0 ;
 }

另一种处理办法:(78ms)

	for(int i = 1; i<=n; i++) {
		scanf("%d",&len);
		int sum = 0;
		for(int j = 1; j<=len; j++) scanf("%d",a+j),sum += a[j];
		if(flag) continue;
		for(int j = 1; j<=len; j++) {
			int tmp = sum - a[j];
			if(mp.count(sum - a[j])) {
				flag = 1;
				ans1 = mp[tmp].fi; ans2 = mp[tmp].se;
				ans3 = i; ans4 = j;
			}
		}
		for(int j = 1; j<=len; j++) {
			int tmp = sum - a[j];
			mp[tmp] = {i,j};
		}
	} 

错误代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
typedef pair<int,int> PII;//pair<第i个序列 , 第j个字符>
const int MAX = 2e5 + 5;
int a[MAX];
map<int,PII> mp;
set<int> ss;
int main()
{
	int n,len,flag = 0;
	int ans1,ans2,ans3,ans4;
	cin>>n;
	for(int i = 1; i<=n; i++) {
		scanf("%d",&len);
		int sum = 0;
		for(int j = 1; j<=len; j++) scanf("%d",a+j),sum += a[j];
		sort(a+1,a+len+1);
		int x = unique(a+1,a+len+1) - a - 1;
	//	printf("x = %d\n",x);
		if(flag) continue;
		for(int j = 1; j<=x; j++) {
			int tmp = sum - a[j];
			if(ss.count(tmp)) {
				flag = 1;
				ans1 = mp[tmp].fi;
				ans2 = mp[tmp].se;
				ans3 = i;
				ans4 = j;
			}
			else {
				ss.insert(tmp);
				mp[tmp] = pm(i,j);
			}
		}
		
	} 
	if(flag) {
		puts("YES");
		printf("%d %d\n%d %d\n",ans1,ans2,ans3,ans4);
	}
	else {
		puts("NO");
	}
	return 0 ;
 }

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_41289920/article/details/86773211

智能推荐

linux下编译GDAL外加扩展格式支持(五)--完-程序员宅基地

文章浏览阅读229次。接1、2、3、4篇。10、安装mysql支持安装fedora15或者16系统时若选择安装mysql数据库,则必须自行安装mysql开发包。因为自带默认数据库不会安装这个包。否则会遇到mysql错误:ogr_mysql.h:34:23: fatal error: my_global.h: No such file or directory#问题原因:找不到mysql头文件..._linux gdal netcdf5

Linux tc qdisc 模拟网络丢包延时-程序员宅基地

文章浏览阅读1.2k次。Linux tc qdisc 模拟网络丢包延时_tc qdisc

linux64bit 安装 jdk 1.7-程序员宅基地

文章浏览阅读336次。linux64bit 安装 jdk 1.7下载地址 : https://edelivery.oracle.com/otn-pub/java/jdk/7u21-b11/jdk-7u21-linux-x64.rpm0. 卸载rpm版的jdk: #rpm -qa|grep jdk 显示:jdk-1.6.0_10-fcs 卸载:#rpm -e --nodep..._liunx64位得jdk1.7

【Linux笔记】-----Nginx/LVS/HAProxy负载均衡的优缺点_中间件应用场景nginx lvs proxy-程序员宅基地

文章浏览阅读552次。开始听到负载均衡的时候,我第一反应想到的是链路负载均衡,在此之前主要是在学习网络方面知识,像在NA、NP阶段实验做链路负载均衡时常会遇到,后来还接触到SLB负载分担技术,这都是在链路基础上实现的。 其实负载均衡可以分为硬件实现负载均衡和软件实现负载均衡。 硬件实现负载均衡:常见F5和Array负载均衡器,配套专业维护服务,但是成本昂贵。 软件实现负载均衡:常见开源免费的负载均衡软件有Ngin..._中间件应用场景nginx lvs proxy

多维时序 | MATLAB实现CNN-LSTM多变量时序预测_cnn可以进行多步预测-程序员宅基地

文章浏览阅读4.7k次。多维时序 | MATLAB实现CNN-LSTM多变量时序预测目录多维时序 | MATLAB实现CNN-LSTM多变量多步预测基本介绍模型特点程序设计学习总结参考资料基本介绍本次运行测试环境MATLAB2020b,MATLAB实现CNN-LSTM多变量多步预测。模型特点深度学习使用分布式的分层特征表示方法自动提取数据中的从最低层到最高层固有的抽象特征和隐藏不变结构. 为了充分利用单个模型的优点并提高预测性能, 现已提出了许多组合模型。CNN 是多层前馈神经网络, 已被证明在提取隐藏_cnn可以进行多步预测

随便推点

【9.3】用户和组的管理、密码_polkitd:input 用户和组-程序员宅基地

文章浏览阅读219次。3.1 用户配置文件和密码配置文件3.2 用户组管理3.3 用户管理3.4 usermod命令3.5 用户密码管理3.6 mkpasswd命令_polkitd:input 用户和组

pca算法python代码_三种方法实现PCA算法(Python)-程序员宅基地

文章浏览阅读670次。主成分分析,即Principal Component Analysis(PCA),是多元统计中的重要内容,也广泛应用于机器学习和其它领域。它的主要作用是对高维数据进行降维。PCA把原先的n个特征用数目更少的k个特征取代,新特征是旧特征的线性组合,这些线性组合最大化样本方差,尽量使新的k个特征互不相关。关于PCA的更多介绍,请参考:https://en.wikipedia.org/wiki/Prin..._inprementation python code of pca

内存地址Linux下内存分配与映射之一-程序员宅基地

文章浏览阅读35次。发一下牢骚和主题无关:地址类型:32位的cpu,共4G间空,其中0-3G属于用户间空地址,3G-4G是内核间空地址。用户虚拟地址:用户间空程序的地址物理地址:cpu与内存之间的用使地址总线地址:外围总线和内存之间的用使地址内核逻辑地址:内存的分部或全体射映,大多数情况下,它与物理地址仅差一个偏移量。如Kmalloc分..._linux 内存条与内存地址

自动化测试介绍_自动化测试中baw库指的什么-程序员宅基地

文章浏览阅读1.3k次,点赞2次,收藏16次。什么是自动化测试?   做测试好几年了,真正学习和实践自动化测试一年,自我感觉这一个年中收获许多。一直想动笔写一篇文章分享自动化测试实践中的一些经验。终于决定花点时间来做这件事儿。  首先理清自动化测试的概念,广义上来讲,自动化包括一切通过工具(程序)的方式来代替或辅助手工测试的行为都可以看做自动化,包括性能测试工具(loadrunner、jmeter),或自己所写的一段程序,用于_自动化测试中baw库指的什么

a0图框标题栏尺寸_a0图纸尺寸(a0图纸标题栏尺寸标准国标)-程序员宅基地

文章浏览阅读1.6w次。A0纸指的是一平方米大小的白银比例长方形纸(长为1189mm宽为841mm)。A0=1189mm*841mm A1=841mm*594mm 相当于1/2张A0纸 A2=594mm*420mm 相当于1/4.A1图纸大小尺寸:841mm*594mm 即长为841mm,宽为594mm 过去是以多少"开"(例如8开或16开等)来表示纸张的大小,我国采用国际标准,规定以 A0、A1、A2、.GB/T 14..._a0图纸尺寸

TreeTable的简单实现_treetable canvas-程序员宅基地

文章浏览阅读966次。最终效果图:UI说明:针对table本身进行增强的tree table组件。 tree的数据来源是单元格内a元素的自定义属性:level和type。具体代码如下:Java代码 DepartmentEmployeeIDposi_treetable canvas