西西艾弗岛的购物中心里店铺林立,商品琳琅满目。为了帮助游客根据自己的预算快速选择心仪的商品,IT 部门决定研发一套商品检索系统,支持对任意给定的预算 x,查询在该预算范围内(≤x)价格最高的商品。如果没有商品符合该预算要求,便向游客推荐可以免费领取的西西艾弗岛定制纪念品。
假设购物中心里有 n 件商品,价格从低到高依次为 A1,A2⋯An,则根据预算 x 检索商品的过程可以抽象为如下序列查询问题。
题解:
import java.util.Scanner; /** * @author lanjingjing * @date 2021/12/15 */ public class test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int N = sc.nextInt(); int sum = 0; int[] nums = new int[n+2]; nums[0] = 0; int[] subs = new int[n+1];