The 2021 Hangzhou Normal U Summer Trials 12-7

Problem B - Bsueh- and Gold Medals

题意:
多组输入,每组样例先输入两个值n, m:金牌的数量和容器最多可以装多少金牌。我们需要选择一些金牌来填满这个容器。金牌的摆放是叠成塔状的,这要求每个位于下层的金牌面积要比上层的金牌至少大 p, 求最大的p。

知识点:
贪心,二分

题解:
本题可以运用贪心的思想来对结果直接二分求答案,先从较小值的那一端进行查找,若最后找到的值的数量小于容器的容量,chk函数就返回false。

代码:

#include <bits/stdc++.h>
using namespace std;

#define _rep(i,a,b) for( int i = (a); i <= (b); i ++ )
const int N = 1010;

int n, m, ans;
int q[N];
 
bool chk(int mid) {
    ans = 0;
    int temp = q[1];
    _rep (i, 2, n) {
        if(q[i] - temp >= mid) {
            temp = q[i];
            ans ++;
        }
    }
    if(ans >= m - 1) return true;
    else return false;
}
 
void solve() {
    scanf ("%d %d", &n, &m);
    _rep (i, 1, n)   scanf("%d", &q[i]);

    sort(q + 1, q + n + 1);

    int l = 0, r = q[n];
    while(l < r) {
        int mid = l + r + 1 >> 1;
        if(chk(mid)) l = mid;
        else r = mid - 1;
    }
    printf("%d\n", l);
}
 
int main() {
    int tt;
    cin >> tt;
    while(tt -- ) solve();
    system ("pause");
    return 0;
}

Problem E - Ewo Slices of Bread with Cheese

题意:
输入第一行表示奶酪数量n,第二行输入n个数表示每个奶酪的新鲜度,问你他至少需要多少天吃完这些奶酪,注意他每天最多吃一个奶酪,并且只吃新鲜度为偶数的奶酪,每过一天,所有奶酪的新鲜度减一。

知识点:
模拟

题解:
对奶酪数量分为3种情况,奇数等于、大于或小于偶数的数量,分别模拟每天的消耗量得到公式。

代码:

#include <bits/stdc++.h>
using namespace std;
#define _rep(i,a,b) for( int i = (a); i <= (b); i ++ )
int n;
int q[100010];
int cnt1, cnt2, ans;

int main() {
    cin >> n;
    _rep (i , 1, n) cin >> q[i];
    if(cnt1 == cnt2) ans = n;
    else if(cnt2 > cnt1) ans = 2 * cnt1 + 2 * (cnt2 - cnt1) - 1;
    else if(cnt1 > cnt2) ans = 2 * cnt2 + (cnt1 - cnt2) * 2;
    cout << ans << endl;
    // system ("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pale_B

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值