<NOIP> 26 . P1478 陶陶摘苹果(升级版)
2017-07-04 23:24
399 查看
题解:这是洛谷的第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;
}
注意
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;
}
相关文章推荐
- |洛谷|贪心|P1478 陶陶摘苹果(升级版)
- P1478 陶陶摘苹果(升级版)
- 洛谷P1478 陶陶摘苹果升级版
- luogu【P1478】陶陶摘苹果(升级版)
- 【模拟】洛谷 P1478 陶陶摘苹果(升级版)
- P1478 陶陶摘苹果(升级版)
- 洛谷P1478 陶陶摘苹果(升级版)
- <NOIP> 19 . P1553 数字反转(升级版)
- <noip>陶陶摘苹果
- 陶陶摘苹果(0)<P2005_1>
- 洛谷 P1478 陶陶摘苹果(升级版)
- <NOIP> 13 . P1046 陶陶摘苹果
- <NOIP> 28 . P1579 哥德巴赫猜想(升级版)
- <NOIP> 29 . P2089 烤鸡
- (noip 模拟 Matrix)<逃避矩阵乘法的好方法#滑稽>
- <NOIP> 6 . P1089 津津的储蓄计划
- [洛谷1478]陶陶摘苹果(升级版)
- (noip 2014 寻找道路)<有向图最短路>
- <转自原博客> NOIP2008 传纸条
- (洛谷 1632 点的移动 noip 模拟 tower)<脑洞题>