洛谷 题解 CF1151D 【Stas and the Queue at the Buffet】

本蒟蒻又双叒叕被爆踩辣!!!

[题目链接](https://www.luogu.org/problem/CF1151D)

~~这道题我个人觉得没有紫题的水平。~~

### 步入正题

先看题:

共有n个人,每个人2个属性,a,b;

窝们要求的是总的不满意度最小,最满意度的公式是什么?

$ai * (j - 1) + bi * (n - j)$

那么我将它化简就是这样的:

$ai * j - ai + bi * n - bi * j$

在化简:

$(ai - bi) * j + bi * n - ai$

窝们在把它拆开,分为:

$(ai - bi) * j$ 和 $bi * b - ai$

后者是不是一个定值?

前者的最小值是不是很好维护:

窝们将其排序,用小的序号对于大的权值,可以求解最小值。

那不就出来了?

```cpp
#include<bits/stdc++.h>

using namespace std;

const int dx[5] = {0, 1, -1, 0, 0};
const int dy[5] = {0, 0, 0, 1, -1};

//#define XRZ
#define int long long
#define maxn 100010
#define maxm
#define ll long long
#define mian main
#define inf 0x3f3f3f3f
#define ls (x << 1)
#define rs (x << 1 | 1)
#define mid (l + r >> 1)
#define debug(x) printf("now here is %d\n", x);
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout);
#define Rep(x, a, b) for(int x = a; x <= b; ++ x)
#define Dep(x, a, b) for(int x = a; x >= b; -- x)
#define Next(x, u) for(int i = head[u]; i ; i = e[i].nxt)

int n, a[maxn], b[maxn], c[maxn], ans;

bool cmp(int x, int y) {return x > y;}//因为要用小的序号对于大的权值,所以从大到小排序。

signed mian(){
// file(a);
scanf("%lld", &n);//读入
Rep(i, 1, n){
scanf("%lld%lld", &a[i], &b[i]);//输入每个角色的属性,好像a[],b[]并没有什么鸟用
c[i] = a[i] - b[i];//这里是窝们求的(a[i] - b[i]),窝们只要维护它即可。
ans += (n * b[i]) - a[i];//先加上定值。
}
sort(c + 1, c + n + 1, cmp);//排序。。。
Rep(i, 1, n) ans += c[i] * i;//小的序号对于大的权值
printf("%lld", ans);//输出
return 0;
}


```

猜你喜欢

转载自www.cnblogs.com/Flash-plus/p/11769404.html