题解:B4350 [信息与未来 2025] 美味水果

· · 题解

洛谷 B4350

题目大意:

有 n 个水果,每个水果的美味值为 a_i,每天,每个水果的美味值变成 \sqrt{a_i}。求最多能获取多少美味值。

解题思路:

简单贪心。很明显,大的数开根后,缩减的美味值一定大于小的数缩减的美味值。所以我们直接对原数组倒序排列,先吃美味值较大的水果。然后模拟对后面所有水果的美味值开根。注意:1 开根后还是 1,可以特判减少时间。

AC 代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5+20;
int n, ans;
int a[N];
bool cmp(int x, int y) {
    return x > y;
}

signed main() {
    cin >> n;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    sort(a + 1, a + 1 + n, cmp);

    for (int i = 1; i <= n; i++) {
        if (a[i] == 1) {
            ans++;
            continue;
        }

        ans += a[i];

        for (int j = i + 1; j <= n; j++) {
            a[j] = sqrt(a[j]);
        }
    }

    cout << ans;
    return 0;
}