1 solutions

  • 0
    @ 2023-10-29 14:15:44

    如题目所说,就是贪心的思想,就最大的数放在求和次数最多的位置,每个位置的求和次数就用差分和前缀和来求

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    int n,m,a[300001],tip[300001],l,r,ans;
    signed main(){
    	cin>>n>>m;
    	for (int i=1;i<=n;i++) cin>>a[i];
    	sort(a+1,a+n+1);
    	for (int i=1;i<=m;i++) cin>>l>>r,tip[l]++,tip[r+1]--;
    	for (int i=1;i<=n;i++) tip[i]+=tip[i-1];
    	sort(tip+1,tip+n+1);
    	for (int i=1;i<=n;i++) ans+=tip[i]*a[i];
    	cout<<ans;
    	return 0;
    }

    Information

    ID
    6940
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    9
    Tags
    # Submissions
    112
    Accepted
    10
    Uploaded By