社区讨论

求助

题目总版参与者 2已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@lo2oluoa
此快照首次捕获于
2023/10/23 17:14
2 年前
此快照最后确认于
2023/10/23 17:14
2 年前
查看原帖

陶陶摘苹果III

题目描述

又是一年秋季时,陶陶家的苹果树结了 nn 个果子。陶陶又跑去摘苹果,这次他有一个 aa 公分的椅子。当他手够不着时,他会站到椅子上再试试。
陶陶之前搬凳子,力气只剩下 ss 了。当然,每次摘苹果时都要用一定的力气。陶陶想知道在 s<0s<0 之前最多能摘到多少个苹果。
现在已知有 nn 个苹果,陶陶所剩的力气 ss,椅子的高度 aa,陶陶手伸直的最大长度 bb,每个苹果距离地面的高度xix_i,陶陶摘一个苹果需要的力气 yiy_i,求陶陶最多能摘到多少个苹果。

输入格式

11 行:两个数 苹果数 nn,力气 ss
22 行:两个数 椅子的高度 aa,陶陶手伸直的最大长度 bb
33 行~第 3+n13+n-1 行:每行两个数 苹果高度 xix_i,摘这个苹果需要的力气 yiy_i

输出格式

只有一个整数,表示陶陶最多能摘到的苹果数。

样例 #1

样例输入 #1

CPP
8 15
20 130
120 3
150 2
110 7
180 1
50 8
200 0
140 3
120 2

样例输出 #1

CPP
4

提示

对于 100%100\% 的数据,n5000n\leq 5000, a50a\leq 50, b200b\leq 200, s1000s\leq 1000, xi280x_i\leq 280, yi100y_i\leq 100
我的答案:
CPP
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <map>
#include <stack>
#include <algorithm>
#include <string>
#include <queue>
#include <deque>
#include <vector>
using namespace std;
struct tree{
	int x,y;
	bool operator < (const tree &o) const{
		return y<o.y;
	}
};
int A[1145140],B[1145140];
int main() {
	tree tre;
	int n,s,a,b,cnt=0;
	cin>>n>>s>>b>>a;
	for(int i=0;i<n;i++){
		cin>>tre.x>>tre.y;
		A[i]=tre.x,B[i]=tre.y;
		
	}
	sort(A,A+n);sort(B,B+n);
	for(int i=0;i<1145141919810;i++){
		if(A[i]<=a+b && B[i]<=s){
			s-=B[i];
			cnt++;
		}
		else {
			cout<<cnt;
			return 0;
			
		}
	}
	
}

回复

4 条回复,欢迎继续交流。

正在加载回复...