This is the easy version of this problem. The only difference is the constraint on k — the number of gifts in the offer. In this version: k=2.

Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer “k of goods for the price of one” is held in store. Remember, that in this problem k=2.

Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.

More formally, for each good, its price is determined by ai — the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:

Vasya can buy one good with the index i if he currently has enough coins (i.e p≥ai). After buying this good, the number of Vasya’s coins will decrease by ai, (i.e it becomes p:=p−ai).
Vasya can buy a good with the index i, and also choose exactly k−1 goods, the price of which does not exceed ai, if he currently has enough coins (i.e p≥ai). Thus, he buys all these k goods, and his number of coins decreases by ai (i.e it becomes p:=p−ai).
Please note that each good can be bought no more than once.

For example, if the store now has n=5 goods worth a1=2,a2=4,a3=3,a4=5,a5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins.

Help Vasya to find out the maximum number of goods he can buy.

Input
The first line contains one integer t (1≤t≤104) — the number of test cases in the test.

The next lines contain a description of t test cases.

The first line of each test case contains three integers n,p,k (2≤n≤2⋅105, 1≤p≤2⋅109, k=2) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.

The second line of each test case contains n integers ai (1≤ai≤104) — the prices of goods.

It is guaranteed that the sum of n for all test cases does not exceed 2⋅105. It is guaranteed that in this version of the problem k=2 for all test cases.

Output
For each test case in a separate line print one integer m — the maximum number of goods that Vasya can buy.

Example
Input
6
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
2 10000 2
10000 10000
2 9999 2
10000 10000
5 13 2
8 2 8 2 5
3 18 2
1 2 3
Output
3
4
2
0
4
3
emmmm这道题设计的这个k不知道有啥用,可能有简单的做法吧。对于不同的k我们都可以dp来做。
对于第i件物品,我们可以有两种做法来买,一个是直接买,另一种就是买从i-k+1~i件,这两种方法取最优的。我们用dp[i]代表的是买前i件物品所需要的最小值。最后遍历一遍就可以了。
代码如下:

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;const int maxx=2e5+100;
int a[maxx];
ll dp[maxx];//dp[i]代表的是前i件物品花费最小值
int n,p,k;int main()
{int t;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&p,&k);for(int i=1;i<=n;i++) scanf("%d",&a[i]);sort(a+1,a+1+n);//2 3 4 5 7->11memset(dp,inf,sizeof(dp));dp[0]=0;for(int i=1;i<=n;i++){if(i>=k) dp[i]=min(dp[i-1],dp[i-k])+(ll)a[i];else dp[i]=dp[i-1]+(ll)a[i];}int ans=0;for(int i=1;i<=n;i++){if(dp[i]<=p) ans=i;}cout<<ans<<endl;}return 0;
}

努力加油a啊,(o)/~

K for the Price of One(EASY HARD)相关推荐

  1. Android 高仿唱吧 咔拉ok 商业项目开源代码 K歌合成 伴奏录音合成MP3(音频五)

    Android MediaRecorder录音录像 暂停 继续录音 播放 ARM格式(音频一) https://blog.csdn.net/WHB20081815/article/details/88 ...

  2. 股票走势及K线绘制练习代码笔记(无注释)

    股票走势及K线绘制练习代码笔记(无注释) ###所需要安装的新库:tushare;mpl_finance #22.04.09 # In[走势图] import tushare as ts df=ts. ...

  3. 左神算法:将单链表的每K个节点之间逆序(Java版)

    本题来自左神<程序员代码面试指南>"将单链表的每K个节点之间逆序"题目. 题目 给定一个单链表的头节点head,实现一个调整单链表的函数,使得每K 个节点之间逆序,如果 ...

  4. LeetCode 1679. K 和数对的最大数目(哈希)

    文章目录 1. 题目 2. 解题 1. 题目 给你一个整数数组 nums 和一个整数 k . 每一步操作中,你需要从数组中选出和为 k 的两个整数,并将它们移出数组. 返回你可以对数组执行的最大操作数 ...

  5. LeetCode 1100. 长度为 K 的无重复字符子串(滑动窗口)

    文章目录 1. 题目 2. 解题 1. 题目 给你一个字符串 S,找出所有长度为 K 且不含重复字符的子串,请你返回全部满足要求的子串的 数目. 示例 1: 输入:S = "havefuno ...

  6. LeetCode 658. 找到 K 个最接近的元素(二分查找)

    1. 题目 给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之差最小)的 k 个数.返回的结果必须要是按升序排好的.如果有两个数与 x 的差值一样,优先选择数值较小的那个数. ...

  7. Only老K说-爬取妹子图片(简单入门)

    简单爬虫 安装第三方库 请求页面 解析网页 保存图片 今天深圳的晚上很冷,半夜就睡醒了,出去买点吃的难受的一批 说好的24小时呢? 开搞开搞 安装第三方库 安装第三方请求库(requests) pip ...

  8. k线图形态这样记(口诀篇)

    在学习了一定的K线基础知识之后,投资者就会进入组合形态的学习阶段,这时面对千百种形态的变化,实在难以一一记忆.其实,投资者可以重点记忆一些经典的.具有特殊看涨和.看跌意味的组合,如果过程中能够结合以下 ...

  9. 两个有序链表排序C语言,K个有序链表的归并排序(C语言)

    归并算法时间复杂度:O(NlogN) 注意:断链和合并的思想 两个链表的归并: #include #include typedef struct listNode{ int val; struct l ...

最新文章

  1. SQL DBHelp.cs 操作数据库的底层类
  2. 安装网关报mysql服务ini_linux 操作系统下ORACLE数据库使用透明网关连接MYSQL
  3. OpenCV + python 实现人脸检测(基于照片和视频进行检测)
  4. 保持寄存器和输入寄存器的区别_串行移位寄存器74HC595
  5. Mybatis的CRUD之XML方式以及动态SQL
  6. 硬件基础:理解串口通信以及232,485,422常见问题
  7. 设计模式:不可变的嵌入式构建器
  8. 一文搞懂JVM架构:跳槽面试大厂被拒
  9. 小白上手Mysql数据库指南~~
  10. MySQL group_concat函数使用详解
  11. 性能测试(一)性能测试是什么?有哪些分类?
  12. win10屏蔽自动更新方法
  13. java jvm内存模型_Java(JVM)内存模型– Java中的内存管理
  14. matlab图形标注名称_matlab入门(三)图像可视化
  15. 1月4日 每次安装都有新问题
  16. 学会如何使用移动用户反馈系统,让你玩转APP
  17. 掌握这三种方法!Word、PPT、Excel、JPG、PDF之间任你转换
  18. excel打开很慢_从海量Excel文件中快速高效地提取数据
  19. 【微信小程序】video视频组件问题
  20. UVaOJ 12304 2D Geometry 110 in 1!

热门文章

  1. 剑指offer(12)旋转数组的最小数字
  2. (个人总结)Linux命令——任意目录查看穿越
  3. 则执行C语言语句unsigned,部分C语言题目
  4. tomcat jar包编译后变成文件夹_tomcat学习|tomcat中的类加载器
  5. python自学可以吗_可以自学python吗?
  6. 数据分析结果解读_聚类分析的实际运用及案例解读(二)
  7. java netty聊天室_netty实现消息中心(二)基于netty搭建一个聊天室
  8. python中面向对象的ui_python中面向对象
  9. GCD dispatch_semaphore
  10. 启动欢迎页面时,Android Studio设置全屏Activity