<NOIP> 26 . P1478 陶陶摘苹果(升级版)

题解:这是洛谷的第26道题目,其实题干就是“最大可以摘到多少苹果”以及“凳子的高度+手臂的长度>=苹果的高度”,还有体力最小为0。

注意

1 . “最大可以摘到多少苹果”提示:需要将每一行的数据按照第二列排列(按照体力的顺序来排列);

2 . 排列完之后,按照上述的规则判断;

源代码:

#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>

using namespace std;

struct MyStruct
{
    int a;
    int b;
};

bool compare(MyStruct A, MyStruct B)
{
    return A.b < B.b;
}

int main()
{
    int sum = 0;
    int apple, strength = 0;
    int height, length;
    struct MyStruct number;
    vector< MyStruct > temp;

    cin >> apple >> strength;
    cin >> height >> length;
    for (long long i = 0; i < apple; i++)
    {
        long long a, b;
        cin >> a >> b;
        number.a = a;
        number.b = b;
        temp.push_back(number);
    }

    std::sort(temp.begin(),temp.end(), compare);

    for (size_t i = 0; i < apple; i++)
        if (strength >= 0 && strength >= temp.at(i).b)
        {
            if (temp[i].a <= length + height)
            {
                sum++;
                strength -= temp[i].b;
            }
        }

    cout << sum << endl;
    system("pause");
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值