个人简单题解

B. Bsueh- and Gold Medals

题意:给一定数量的奖牌,然后给出他们的大小,需要选择m个奖牌,问差值选择的奖牌差距最小是多少。

使用二分查找。

    #include <bits/stdc++.h>

using namespace std;

const int N = 1010;

int n, m;

int a[N];

//int sum[N];

bool check(int mid)

{

    int cnt = 0;

    int qwq = a[1];

    for(int i = 2; i <= n - 1;  )

    {

        if(a[i] - qwq >= mid)

        {

            qwq = a[i];

            cnt ++;

            i ++;

        }else{

            i ++;

        }

    }

    if(a[n] - qwq >= mid) cnt++;

 

    if(cnt >= m - 1) return true;

    return false;

}

void solve()

{

    scanf("%d %d",&n, &m);

    //for(int i = 1; i <= n; i ++ ) sum[i] = 0;

    for(int i = 1; i <= n; i ++ )   scanf("%d", &a[i]);

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

   // for(int i = 1; i <= n; i ++ ) sum[i] = sum[i - 1] + a[i];

    int l = 0, r = a[n] + 1;

    while(l < r)

    {

        int mid = l + r + 1 >> 1;

        if(check(mid)) l = mid;

        else r = mid - 1;

    }

    printf("%d\n", l);

}

int main()

{

    int t;

    scanf("%d",&t);

    while(t -- )

    {

        solve();

    }

    return 0;

}

E. Ewo Slices of Bread with Cheese

题意:每次吃掉一个新鲜度为偶数的奶酪,其余奶酪新鲜度减一,吃完所有需要多少天?

寻找规律即可

    #include <bits/stdc++.h>

    using namespace std;

    int n;

    int a[100010];

    int cnt1, cnt2;

    int main()

    {

        scanf("%d",&n);

        for(int i = 1; i <= n; i ++ )

        {

            scanf("%d", &a[i]);

            if(a[i] % 2) cnt1 ++;

            else cnt2 ++;

        }

        int ans = 0;

        if(cnt2 == n){

            ans = 2 * n - 1;

        }else if(cnt1 == n){

            ans = 2 * n;

        }

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

        }

        printf("%d\n", ans);

        return 0;

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值