为啥是66分

Teacher_zhao 躺平 2024-12-04 18:08:15 2024-12-04 18:40:46 7
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100010;
int w[N], s[N];
int n, m;

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &w[i]);

    scanf("%d", &m);
    while (m--) {
        int l, r;
        scanf("%d%d", &l, &r);
        s[l]++, s[r + 1]--;
    }

    for (int i = 1; i <= n; i++) s[i] += s[i - 1];

    LL res1 = 0;
    for (int i = 1; i <= n; i++) res1 += (LL)w[i] * s[i];

    sort(w + 1, w + n + 1);
    sort(s + 1, s + n + 1);
    LL res2 = 0;
    for (int i = 1; i <= n; i++) res2 += (LL)w[i] * s[i];

    printf("%lld\n", res2 - res1);

    return 0;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    static int N = 100010;
    static int[] a = new int[N];
    static long[] s = new long[N];
    static long[] g = new long[N];
    static int n;
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(br.readLine());
        String[] v = br.readLine().split(" ");
        for (int i = 1; i <= n; i++) {
            a[i] = Integer.parseInt(v[i - 1]);
            s[i] = s[i - 1] + a[i];
        }
        int m = Integer.parseInt(br.readLine());
        long res = 0;
        for (int i = 0; i < m; i++) {
            v = br.readLine().split(" ");
            int l = Integer.parseInt(v[0]);
            int r = Integer.parseInt(v[1]);
            g[l]++;
            g[r + 1]--;
            res += s[r] - s[l - 1];
        }
        for (int i = 1; i <= n; i++) {
            g[i] += g[i - 1];
        }
        long ans = 0;
        Arrays.sort(a);
        Arrays.sort(g);
        int pre = g.length - 1;
        while (g[pre] != 0) {
            ans += g[pre] * a[pre];
            pre--;
        }
        System.out.println(ans - res);
    }
}

在其他OJ都是AC的啊?
错了嘛?
@Wind_Rises
@root
题解都不对
(低调中)

{{ vote && vote.total.up }}

共 3 条回复

root 站长

要在评论区艾特才有用哈,

Teacher_zhao 躺平

@Wind_Rises 好的

Wind_Rises 砂糖老师

数据错误 一直没时间改 过段时间