Codeforces Raif Round 1 (Div. 1 + Div. 2) E. Carrots for Rabbits(贪心)

传送门


题目大意

给出 n n n个萝卜,现在需要分成长度为正整数的若干个萝卜,定义每个长度为 x x x的胡萝卜贡献为 x 2 x^2 x2,问最小的贡献是多少。

解题思路

手玩一下样例,还是不难得出对于一个胡萝卜若需要切成 l l l份,那么肯定是均摊才能是贡献最小。但是对于多个胡萝卜,并不容易得出每次切哪一根才能得到最优解。实际上无需考虑这一点,因为萝卜不能拼接,也就是如果最后的答案是将某些胡萝卜切成若干份,每份仍然是均摊的。这就启发我们,对于长度为 x i x_i xi假设已经切了 p i p_i pi刀的胡萝卜,那么考虑所有胡萝卜被切前后的 f ( p i ) − f ( p i + 1 ) f(p_i)-f(p_{i}+1) f(pi)f(pi+1),差值越大我们越需要,那么使用优先队列贪心切 k − n k-n kn次即可。

代码
//
// Created by Happig on 2020/10/26
//
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define Vector Point
#define ENDL "\n"
#define lowbit(x) (x&(-x))
#define mkp(x, y) make_pair(x,y)
#define mem(a, x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double dinf = 1e300;
const ll INF = 1e18;
const int Mod = 1e9 + 7;
const int maxn = 2e5 + 10;


struct node {
    ll val, a, b, delta;

    bool operator<(const node &p) const {
        return delta < p.delta;
    }
};

int a[maxn];

priority_queue<node> pq;

inline ll cal(ll x, ll y) {
    ll t = x / y, res = x % y;
    return (y - res) * t * t + res * (t + 1) * (t + 1);
}

int main() {
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n, k;
    cin >> n >> k;
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        ans += 1LL * a[i] * a[i];
        pq.push({a[i], 1, 2, cal(a[i], 1) - cal(a[i], 2)});
    }
    int cnt = k - n;
    while (cnt--) {
        node cur = pq.top();
        pq.pop();
        ans -= cur.delta;
        ll x = cur.val, num = cur.b;
        pq.push({x, num, num + 1, cal(x, num) - cal(x, num + 1)});
    }
    cout << ans << ENDL;
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值