Stas and the Queue at the Buffet

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;
}

原文链接: https://www.cnblogs.com/Zeoy-kkk/p/17032010.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Stas and the Queue at the Buffet

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/310248

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 上午11:27
下一篇 2023年2月16日 上午11:27

相关推荐