n2还能过
查看原帖
n2还能过
1092647
dddvag_21楼主2024/5/11 18:04

如题(再来个快读还能更快)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define F1(a,b,c) for(int (a)=(b);(a)<=(c);(a)++)
#define F2(a,b,c) for(int (a)=(b);(a)>=(c);(a)--)
const int N = 5e4 + 1;
struct dd {
    ll a, b;
}d[N];
int n;
bool cmp(dd a, dd b) { return a.a < b.a; }
int main() {
    IOS;
    cin >> n;
    F1(i, 1, n)cin >> d[i].a >> d[i].b;
    ll ans = 0, bc = 0, f;
    sort(d + 1, d + 1 + n, cmp);
    F1(i, 1, n){
        bc = 0;
        f = d[i].b;
        F1(j, 1, i - 1)bc += abs(f - d[j].b);
        ans += bc * d[i].a;
    }
    cout << ans;

}

900ms左右

2024/5/11 18:04
加载中...