Stas and the Queue at the Buffet

Stas and the Queue at the Buffet

题解:贪心+思维

我们看到公式\(ai⋅(j−1)+bi⋅(n−j)\),直接进行化简得到

\[(a[i]-b[i])*j+n*b[i]-a[i] \]

我们就知道\(n*b[i]-a[i]\)是个定值,所以我们把差值最大的放在前面,把最小的放在后面,贪心放即可

#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define endl '\n'
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 1e5 + 10;
struct node
{
    ll a, b, d;
    bool operator<(const node &t) const
    {
        return d > t.d;
    }
} a[N];
int main(void)
{
    Zeoy;
    int t = 1;
    // cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n; ++i)
        {
            cin >> a[i].a >> a[i].b;
            a[i].d = a[i].a - a[i].b;
        }
        sort(a + 1, a + 1 + n);
        ll sum = 0;
        for (int i = 1; i <= n; ++i)
        {
            sum += a[i].d * i + n * a[i].b - a[i].a;
        }
        cout << sum << endl;
    }
    return 0;
}
posted @ 2023-01-07 00:31  Zeoy_kkk  阅读(18)  评论(0编辑  收藏  举报