【noip模拟】太空电梯 贪心

题目大意

人数n,电梯载重k,电梯限制人数2,给出每个人的体重v,求按照怎样的顺序排队电梯运送的次数越多。

题解

排序后,每次都先选择最小的,然后看最大的上来是否超出载重,

  • 若超出,则这两个对答案贡献2,

  • 若不超出,则再添加一个次小的(不浪费最大的).

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const int N = 1e5 + 5;
int n, a[N], ans, k;

int main(){
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    sort(a + 1, a + n + 1);
    int head = 1, tail = n;
    while(head <= tail){
        if(head == tail){
            ans++;
            break;
        }
        else if(a[head] + a[tail] <= k){
            head += 2;
            ans++;
        }
        else{
            ans += 2;
            head++, tail--;
        }
        // cout<<ans<<endl;
    }
    printf("%d", ans);
    return 0;
}

转载于:https://www.cnblogs.com/CzYoL/p/7450019.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值