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

本文解析了CodeForces竞赛中编号为1151的D题,详细介绍了使用C++进行排序算法实现的步骤。通过对数组元素a与b的特定排序,优化计算过程,达到高效求解问题的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接: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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值