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

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

根据题目对a与b排序

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#include<stack>
using namespace std;
#define int long long
struct node
{
	int a,b;
}p[1000001];
bool cmp(node a,node b)
{
	return a.a-a.b>b.a-b.b;
}

int ans=0;

int main()
{
	int n;
	scanf("%I64d",&n);
	for(int i=1;i<=n;i++)
		scanf("%I64d%I64d",&p[i].a,&p[i].b);
	sort(p+1,p+n+1,cmp);
	for(int i=1;i<=n;i++)
		ans+=p[i].a*(i-1);
	for(int i=n;i>=1;i--)
		ans+=p[i].b*(n-i);
	printf("%I64d",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/dukig/article/details/89432570
今日推荐