Problem Description
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup" competition,you must have seem this title.If you haven't seen it before,it doesn't matter,I will give you a link:

Here is the link:http://acm.hdu.edu.cn/showproblem.php?pid=2602

Today we are not desiring the maximum value of bones,but the K-th maximum value of the bones.NOTICE that,we considerate two ways that get the same value of bones are the same.That means,it will be a strictly decreasing sequence from the 1st maximum , 2nd maximum .. to the K-th maximum.

If the total number of different values is less than K,just ouput 0.

 
Input
The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, K(N <= 100 , V <= 1000 , K <= 30)representing the number of bones and the volume of his bag and the K we need. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
 
Output
One integer per line representing the K-th maximum of the total value (this number will be less than 231).
 
Sample Input
3
5 10 2
1 2 3 4 5
5 4 3 2 1
5 10 12
1 2 3 4 5
5 4 3 2 1
5 10 16
1 2 3 4 5
5 4 3 2 1
 
Sample Output
12
2
0
 
思路:
对于求最优解的情况,我们对每一种状态只保存了该状态下的最优解,忽略了其他解,进而实现状态之间的转移,而对于求第K优解的情况呢?其实只需要保存每一种状态下的前K优解,从这K个状态进行状态间的转移,同时去重,保存当前状态的K优解即可。(感觉时间复杂度还是挺高的)
 
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
using namespace std; const int N = ;
const int INF = 0x3fffffff;
const long long MOD = ;
typedef long long LL;
#define met(a,b) (memset(a,b,sizeof(a))) int dp[N][];
int a[N], b[N], c[N];
///dp[j][k] 代表容量为 j 的背包的第 k+1 优解 int cmp(int a, int b)
{
return a > b;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int i, j, k, n, v; scanf("%d%d%d", &n, &v, &k); met(a, );
met(b, );
met(dp, ); for(i=; i<=n; i++)
scanf("%d", &a[i]);
for(i=; i<=n; i++)
scanf("%d", &b[i]); for(i=; i<=n; i++)
{
for(j=v; j>=b[i]; j--)
{
int w = ;
for(int z=; z<k; z++) ///每次只需考虑前 k 优解的状态转换即可
{
c[w++] = dp[j][z];
c[w++] = dp[j-b[i]][z]+a[i];
} sort(c, c+w, cmp);
w = unique(c, c+w) - c;
for(int t=; t<k && t<w; t++) ///t的范围, 既不能大于 k,也不能大于 w
dp[j][t] = c[t];
}
} printf("%d\n", dp[v][k-]); }
return ;
}

最新文章

  1. Web 开发中 20 个很有用的 CSS 库
  2. Openstack的计算节点的nova-network异常中止及实例无法删除排错过程
  3. 蓝牙SIG
  4. fedora下python3 安装tkinter和pygame
  5. windows server 许可port或执行技巧
  6. python高级编程 编写一个包1
  7. linux kickstart 自动安装
  8. 姿势体系结构的详细解释 -- C
  9. AndroidStudio意外崩溃,电脑重启,导致重启打开Androidstudio后所有的import都出错
  10. Vijos1982 NOIP2015Day2T2 子串 substring 动态规划
  11. Ubuntu上查内存情况
  12. vbs调用bat 隐藏bat运行时的黑框
  13. 人才需求之Java程序员与AI程序员
  14. div中的img垂直居中的方法,最简单! 偷学来的,,,不要说我抄袭啊(*^__^*)
  15. 这个C#程序真了不起
  16. vim的翻页、跳转到某一行功能
  17. leetcode950
  18. python环境搭建,开发环境
  19. 7个去伪存真的JavaScript面试题
  20. thinkphp5保存远程图片到本地

热门文章

  1. qrcodenet二维码图片下扩展区域添加号段的操作
  2. Oracle_高级功能(2) 索引
  3. C# Contains 包含空字符串的问题
  4. 安装SQL Server 2008,一直要求重启电脑的解决办法
  5. Tools.Eclipse.HowToImportAnAndroidLibraryProjectIntoWorkspace
  6. [转载]linux awk命令详解
  7. 检查mysql是否运行
  8. Ubuntu 双网卡设置
  9. ios微信打开网页键盘弹起后页面上滑,导致弹框里的按钮响应区域错位
  10. 【Web】移动端下拉刷新、上拉加载更多插件