题解:这是洛谷的第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;
}