codeforces(D. Stas and the Queue at the Buffet)贪心

在这里插入图片描述
在这里插入图片描述

纯贪心题目
遇到这种给个公式,然后肯定是要根据公式来判断怎么贪心,化简一下公式很很显然了

#include <bits/stdc++.h>
#define ll long long
using namespace std;

//ll a[100005],b[100005];
struct node
{
    
    
    ll a,b;
    ll w;
}q[100005];
bool cmp(node A,node B)
{
    
    
    if(A.w>B.w)
    {
    
    return 1;}
    if(A.w==B.w)
    {
    
    
        if(A.b<B.b)
        {
    
    return 1;}
        return 0;
    }
    return 0;
}

int main()
{
    
    
    std::ios::sync_with_stdio(false);
    cin.tie(0);

    unsigned long long n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
    
    
        cin>>q[i].a>>q[i].b;
        q[i].w=q[i].a-q[i].b;
    }
    sort(q,q+n,cmp);
    unsigned long long ans;
    for(ll i=0;i<n;i++)
    {
    
    
        ans+=q[i].a*i+q[i].b*(n-i-1);
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43781431/article/details/105751386
今日推荐