Stas and the Queue at the Buffet

Problem - D - Codeforcesicon-default.png?t=M1L8https://codeforces.com/contest/1151/problem/D题目的意思 : 给你一个数组 , 让你找出一个最小的答案

当前位置 的答案 ai *(j-1) + bi * (n-j)

这个公式 可以转换成 ai * j + ai + bi * n - bi * j ---------> (ai - bi ) * j + bi * n - ai

如果 让答案变小   只能改变  (ai - bi ) * j   , bi*n-ai 改变不了   所以只要让 ai-bi  从大到小排序就行了 

#include"bits/stdc++.h"
#define ll long long
#define pi pair<int,int>
#define inf 0x3f3f3f3f
#define  _for(i,a,b) for(int i=a;i<=b;i++)
#define  for_(i,a,b) for(int i=a;i<b;i++)
#define _fr(i,a,b) for(int i=a;i>=b;i--)
#define fr_(i,a,b) for(int i=a;i>b;i--)
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e5+5;
const ll mod = 1000000007;
const double lo=1.000000011;
map<int,int>mp;
struct node{
    ll ai,bi,w;
}arr[N];
bool cmp(node s1, node s2){
    return s1.w > s2.w;
}
void solve(){
    int n,a,b;
    cin >> n;
    ll ans = 0;
    _for(i,1,n){
        cin >> arr[i].ai >> arr[i].bi ;
        arr[i].w = arr[i].ai-arr[i].bi;
        ans+=(arr[i].bi*n-arr[i].ai);
    }
    sort(arr+1,arr+1+n,cmp);
//    a*(j-1)+b*(n-j) --> aj-a+bn-bj --> (a-b)*j+(b*n-a)
    _for(i,1,n){
        ans+= (arr[i].ai-arr[i].bi)*(i);
    }
    cout << ans << endl;
}
int main()
{
    IOS;
    solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值