传送门
- 题意:题意:给 n 个数组,找出两个数组,这两个数组各删除一个数字后,使得两个数组的和相同.
- 题解:可以用一个
map<int, pair<int, int> > 的关键字存储第i个数组删除第j个值后总和,关键字对应的值为pair类型,pair分别储存对应的i,j值。 - 收获:学到如何判断上述类型的map是否被使用过。我是又开了一个
map<int,int> 数组,关键字与上述map相同。 mp.count(sum - a[j]) != 0
my code:
const int N = 2e5, M = 1e6;
map<int, pii>id;
vector<int>v[N];
map<int, int>num;
int main()
{
IOS;
int k;
cin >> k;
int f = 0;
for (int i = 0; i < k; i++)
{
int n; cin >> n;
int x, sum = 0;
for (int j = 0; j < n; j++)
{
cin >> x;
sum += x;
v[i].push_back(x);
}
for (int j = 0; j < n; j++)
{
if (num[sum - v[i][j]] == 0)
{
num[sum - v[i][j]]++;
id[sum - v[i][j]].ft = i;
id[sum - v[i][j]].sd = j;
}
else if(id[sum - v[i][j]].ft != i && !f)
{
f = 1;
cout << "YES\n";
cout << id[sum - v[i][j]].ft + 1 << " " << id[sum - v[i][j]].sd + 1 << endl;
cout << i + 1 << " " << j + 1 << endl;
}
}
}
if(!f) cout << "NO\n";
return 0;
}
others code:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
LL k,n,sum,flag,a[N];
map <LL,pair<LL,LL>> mp;
int main(){
ios :: sync_with_stdio(false);
cin.tie(0);
cin >> k;
flag = false;
for(int i = 1;i <= k;i++){
cin >> n;
sum = 0;
for(int j = 1;j <= n;j++){
cin >> a[j];
sum += a[j];
}
if(flag) continue;
for(int j = 1;j <= n;j++){
if(mp.count(sum - a[j]) != 0){
flag = true;
cout << "YES" << endl << i << " " << j << endl << mp[sum - a[j]].first << " " << mp[sum -
a[j]].second << endl;
break;
}
}
for(int j = 1;j <= n;j++) mp[sum - a[j]] = {i,j};
}
if(!flag) cout << "NO" << endl;
return 0;
}
|