B类——Stas and the Queue at the Buffet

http://codeforces.com/contest/1151/problem/D

题意:

n个学生,每个学生都有自己的位置,最后要使

最小,求最小的总和

题解:

开始各种排序(a,b同时排,a-b和a,b同时排),都不对,其实只要给ai-bi排序就行。。

代码:

#include<iostream>
#include<algorithm>
using namespace std;
struct node{
    long long a,b;
}stu[100010];
int cmp(const node x,const node y){
    return (x.a-x.b)>(y.a-y.b);
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>stu[i].a>>stu[i].b;
       // a[i]=stu[i].a-stu[i].b;
    }
    sort(stu,stu+n,cmp);
    long long sum=0;
    for(int i=0;i<n;i++){
        sum+=stu[i].a*(i+1-1)+stu[i].b*(n-i-1);
        //cout<<sum<<endl;
    }
    cout<<sum<<endl;
    return 0;
}

 

posted @ 2019-04-21 22:18  zhyyyy  阅读(189)  评论(0编辑  收藏  举报