随手练——小米OJ 高弗雷勋爵

 高弗雷勋爵

题目链接:https://code.mi.com/problem/list/view?id=113

这个解法比较暴力,主要需要注意的是一颗子弹 弹死两个及以上的情况。

#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <vector>

using namespace std;
int a[10001];

void infect(int i, int n) {
    bool flag = false;
    for (int j = i; j < n; j++) {
        //一枪弹死两个的情况
        if (a[j] <= 0)continue;
        a[j] -= 2;
        if (a[j] <= 0)flag = true;
    }
    if (flag)
        infect(i + 1, n);
}

int main() {
    int n = 0, res = 0, t;
    while (~scanf("%d", &t)) a[n++] = t;
    sort(a, a + n);
    for (int i = 0; i < n; i++) {
        if (a[i] > 0) {
            if (a[i] % 2) {
                a[i]++;
            }
            res += a[i] / 2;
            for (int j = i + 1; j < n; j++) {
                a[j] -= a[i];
            }
        }
        infect(i + 1, n);
        while (i < n&&a[i + 1] <= 0)
            i++;
    }
    cout << res << endl;
    return 0;
}
View Code

 

这个比较抽象,不进行减操作,中间用base递增记录下来。(写完才想起来,base开成long long好一点,不过也没爆)

#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <vector>

using namespace std;
int a[10001];
int base = 0;


int main() {
    int n = 0, res = 0, t;
    while (~scanf("%d", &t)) {
        if (t == 0)break;
        a[n++] = t;
    } 
    sort(a, a + n);
    for (int i = 0; i < n; i++) {
        //一枪蹦死两个以上的情况
        if (a[i] - (base - 2) <= 0) continue;
        //只在弹射 弹死的第一个人时,base+=2
        if (a[i] - base <= 0) { base += 2; continue; }
        if (a[i] % 2) {
            a[i]++;
        }
        res += (a[i] - base) / 2;
        base += (a[i] - base) + 2;
    }
    cout << res << endl;
    return 0;
}
View Code

 提交详情

编程语言C++11 - G++ 6.4.0结果 通过
最大执行时间2.81 ms最大内存开销4136 KiB
判题信息

运行时间打败了 94.3% 的 C++ 玩家!

转载于:https://www.cnblogs.com/czc1999/p/10493293.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值