CF思维联系– Codeforces-988C Equal Sums (哈希)
生活随笔
收集整理的這篇文章主要介紹了
CF思维联系– Codeforces-988C Equal Sums (哈希)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ACM思維題訓練集合
You are given k sequences of integers. The length of the i-th sequence equals to ni.You have to choose exactly two sequences i and j (i≠j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to ni?1) equals to the sum of the changed sequence j (its length will be equal to nj?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 0) sequence is 0.Input The first line contains an integer k (2≤k≤2?105) — the number of sequences.Then k pairs of lines follow, each pair containing a sequence.The first line in the i-th pair contains one integer ni (1≤ni<2?105) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of ni integers ai,1,ai,2,…,ai,ni.The elements of sequences are integer numbers from ?104 to 104.The sum of lengths of all given sequences don't exceed 2?105, i.e. n1+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 i, x (1≤i≤k,1≤x≤ni), in the third line — two integers j, y (1≤j≤k,1≤y≤nj). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y.Two chosen sequences must be distinct, i.e. i≠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] and [1,1,2,2,2,1]. You can remove the second element from the first sequence to get [2,1,3,2] and you can remove the sixth element from the second sequence to get [1,1,2,2,2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal.這個題,無論怎么循環都是超時的,所以必須想到一種不需要循環的方法,這里我用的是hash,因為是每個序列刪掉一個之后相等,那我們就保存刪掉一個數之后的值,用hash存起來,如果再遇到而且不是同一個序列的話,那就是可以構造出提要求的兩個序列。
#include <bits/stdc++.h> using namespace std; template <typename t> void read(t &x) {char ch = getchar();x = 0;int f = 1;while (ch < '0' || ch > '9')f = (ch == '-' ? -1 : f), ch = getchar();while (ch >= '0' && ch <= '9')x = x * 10 + ch - '0', ch = getchar();x *f; } #define wi(n) printf("%d ", n) #define wl(n) printf("%lld ", n) #define rep(m, n, i) for (int i = m; i < n; ++i) #define P puts(" ") typedef long long ll; #define MOD 1000000007 #define mp(a, b) make_pair(a, b) //---------------https://lunatic.blog.csdn.net/-------------------//const int N = 2e5 + 5; vector<int> v[N]; int a[N]; int ln[N]; int ans[4]; unordered_map<int, pair<int, int>> m; int main() {int k, flag = 0;read(k);rep(0, k, i){read(ln[i]);int sum = 0;rep(0, ln[i], j){cin >> a[j];sum += a[j];}if (flag == 1)continue;rep(0, ln[i], j){int temp = sum - a[j];if (m.count(temp) != 0 && m[temp].first != i){ans[0] = m[temp].first + 1;ans[1] = m[temp].second + 1;ans[2] = i + 1;ans[3] = j + 1;flag = 1;break;}else{m[temp] = make_pair(i, j);}}}if (flag == 0){puts("NO");return 0;}puts("YES");cout << ans[0] << " " << ans[1] << endl<< ans[2] << " " << ans[3] << endl; }總結
以上是生活随笔為你收集整理的CF思维联系– Codeforces-988C Equal Sums (哈希)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 奔驰车熄火十分钟后自燃 车主:未进行任何
- 下一篇: 【Java】格林尼治时间转化为本地时间