【Java8最优解】P1478 陶陶摘苹果(升级版)
陶陶摘苹果(升级版)
题目描述
又是一年秋季时,陶陶家的苹果树结了 n n n 个果子。陶陶又跑去摘苹果,这次他有一个 a a a 公分的椅子。当他手够不着时,他会站到椅子上再试试。
这次与 NOIp2005 普及组第一题不同的是:陶陶之前搬凳子,力气只剩下 s s s 了。当然,每次摘苹果时都要用一定的力气。陶陶想知道在 s < 0 s<0 s<0 之前最多能摘到多少个苹果。
现在已知 n n n 个苹果到达地上的高度 x i x_i xi,椅子的高度 a a a,陶陶手伸直的最大长度 b b b,陶陶所剩的力气 s s s,陶陶摘一个苹果需要的力气 y i y_i yi,求陶陶最多能摘到多少个苹果。
输入格式
第 1 1 1 行:两个数 苹果数 n n n,力气 s s s。
第 2 2 2 行:两个数 椅子的高度 a a a,陶陶手伸直的最大长度 b b b。
第 3 3 3 行~第 3 + n − 1 3+n-1 3+n−1 行:每行两个数 苹果高度 x i x_i xi,摘这个苹果需要的力气 y i y_i yi。
输出格式
只有一个整数,表示陶陶最多能摘到的苹果数。
样例 #1
样例输入 #1
8 15 20 130 120 3 150 2 110 7 180 1 50 8 200 0 140 3 120 2
样例输出 #1
4
提示
对于 100 % 100\% 100% 的数据, n ≤ 5000 nleq 5000 n≤5000, a ≤ 50 aleq 50 a≤50, b ≤ 200 bleq 200 b≤200, s ≤ 1000 sleq 1000 s≤1000, x i ≤ 280 x_ileq 280 xi≤280, y i ≤ 100 y_ileq 100 yi≤100。
代码
import java.io.*; import java.util.Arrays; public class Main { static final BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static final PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static final StreamTokenizer st = new StreamTokenizer(in); public static void main(String[] args) throws IOException { st.nextToken(); int n = (int) st.nval; st.nextToken(); int s = (int) st.nval; st.nextToken(); int a = (int) st.nval; st.nextToken(); int b = (int) st.nval; int c = a + b; int res = 0; if (n <= 0) { out.println(0); out.flush(); return; } int[] ints = new int[n]; for (int i = 0; i < n; i++) { st.nextToken(); int x = (int) st.nval; st.nextToken(); int y = (int) st.nval; if (x > c) { ints[i] = -1; continue; } ints[i] = y; } Arrays.sort(ints); for (int i = 0; i < ints.length; i++) { if (ints[i] == -1) continue; s -= ints[i]; res++; if (s < 0){ out.println(res-1); break; } } out.flush(); } }
下一篇:
三数之和(双指针法)