C++_cnt

牛牛看云

https://ac.nowcoder.com/acm/contest/23106/H

运用 cnt[ ] 计数 来缩减时间 达到 不超时


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<=n;i++)
typedef long long ll;
int n;
int a[1000010];
ll cnt[1010];
int main() {
    cin >> n;
    rep(i, 1, n) {
        cin >> a[i];

        cnt[a[i]]++;
    }
    ll ans = 0;
    rep(i, 0, 1000) {
        rep(j, i, 1000) {
            ll add;
            if (i == j)    add = (cnt[i] + cnt[i] * (cnt[i] - 1) / 2);
            else    add = cnt[i] * cnt[j];
            ans = ans + add * (ll)abs(i + j - 1000);
        }
    }
    printf("%lld\n", ans);
    return 0;
}


凉心哥哥的小宝藏 文章被收录于专栏

c++

全部评论
大佬这题数组顺序对答案有影响吗
点赞 回复 分享
发布于 2022-01-25 18:16
巧妙运用 cnt [i]* cnt[j];
点赞 回复 分享
发布于 2022-01-25 17:54

相关推荐

在debug的柠檬精很迷人:好消息:现在HR挑三拣四 15年后 HR跪着求要简历 坏消息:被挑的是这代人,到时候求人的也是这代人。真好。
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

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