题解:T594862 T2 上坟

· · 题解

思路

贪心,先去 a_i \times t_i 小的,再去 a_i \times t_i 大的。时间复杂度 O(n \log n)。题目较水,时间充裕。

好吧如果你过不了就只能是人傻常数大了。

Code:

#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1000000 + 5;

int a[N], t[N], tmp[N]; 

int n;

signed main() {
    cin >> n; 
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> t[i];
    }
    for (int i = 1; i <= n; i++) {
        tmp[i] = a[i] * t[i];
    }
    sort(tmp + 1, tmp + n + 1);
    int ans = 0ll;
    for (int i = 1; i <= n; i++) {
        ans += (long long)i * tmp[i];
    }
    cout << ans;
    return 0;   
}