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;
}



#美团笔试#
全部评论
大佬能不能解释下思路
点赞 回复 分享
发布于 2022-10-02 17:57 湖北
都是手撕代码吗
点赞 回复 分享
发布于 2022-08-15 18:23

相关推荐

2025-12-15 14:16
门头沟学院 Java
回家当保安:发offer的时候会背调学信网,最好不要这样。 “27届 ”和“28届以下 ”公司招聘的预期是不一样的。
实习简历求拷打
点赞 评论 收藏
分享
程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
评论
1
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务