8.13 美团笔试第四题
过了91%,听同学说改成long long就AC了,自以为逻辑应该没啥问题。抛砖引玉之。
题目:i<j<k满足于a[i] - a[j] = 2a[j] - a[k],求有几对。
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { //freopen("test.txt", "r", stdin); ios::sync_with_stdio(false); int n, ans = 0; cin >> n; vector<LL> v(n); unordered_map<LL, vector<int>> m; for (int i = 0; i < n; ++i) { cin >> v[i]; m[v[i]].push_back(i); } for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { LL sum = v[i] + v[j]; if (sum % 3 != 0) { continue; } else { LL the_num = sum / 3; if (m.find(the_num) == m.end()) { continue; } else { ans += m[the_num].end() - upper_bound(m[the_num].begin(), m[the_num].end(), j); } } } } cout << ans; return 0; }#美团笔试#