HackerRank "The Indian Job"
A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/tree/master/hackerrank/algorithms/the-indian-job
Lesson learnt: The Italian\Indian job is two-way 01 Knapsack. And some complext problem can be converted to a simpler one.
Code is amazingly simple:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; int knapsack(vector<int> &A, int G)
{
int n = A.size(); vector<int> dp(G + ); for(int i =; i <= n; i ++)
for(int v =G; v >= A[i - ]; v --)
dp[v] = max(dp[v], dp[v- A[i - ]]+ A[i- ]);
return dp[G]; }
int main()
{
int T; cin >> T;
while(T--)
{
// Get input
int N, G;
cin >> N >> G;
vector<int> A(N);
int total = ;
for(int i = ; i < N; i++) {
cin >> A[i];
total += A[i];
} //
if(total > * G)
cout << "NO" << endl;
else
cout << ((total - knapsack(A, G) <= G) ? "YES" : "NO") << endl;
} return ;
}
HackerRank "The Indian Job"的更多相关文章
- 日常小测:颜色 && Hackerrank Unique_colors
题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...
- Number To Indian Rupee Words in Oracle Forms / Reports
Convert numbers to Indian Rupees format in Oracle Forms / Reports.Create the below mention function ...
- HackerRank "Square Subsequences" !!!
Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...
- HackerRank "Minimum Penalty Path"
It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial ...
- HackerRank "TBS Problem" ~ NPC
It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...
- HackerRank Extra long factorials
传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...
- HackerRank "Lucky Numbers"
Great learning for me:https://www.hackerrank.com/rest/contests/master/challenges/lucky-numbers/hacke ...
- HackerRank "Playing with numbers"
This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah ...
- HackerRank "Array and simple queries" !
The most interesting, flexible and juicy binary tree problem I have ever seen. I learnt it from here ...
随机推荐
- 【avalon】data
if (root.dataset) { avalon.fn.data = function (name, val) { name = name && camelize(name) va ...
- 【转】eclipse集成开发工具的插件安装
转发一:打开Eclipse下载地址(http://www.eclipse.org/downloads/),可以看到有好多版本的Eclipse可供下载,初学者往往是一头雾水,不知道下载哪一个版本. 各个 ...
- Linux发行分支时间轴
官网:http://futurist.se/gldt/ 全图png:http://futurist.se/gldt/wp-content/uploads/12.10/gldt1210.png 全图sv ...
- Apache搭建多个站点方法详解
www.111cn.net 编辑:Bolshevik 来源:转载 Apache的虚拟主机是一种允许在同一台机器上配置多个不同站点的web服务器环境的,就是iis一样可以创建多站点了,但是apache需 ...
- EasyUI修改DateBox和DateTimeBox的默认日期格式
最近整理Easyui控件的时候,对Easyui的DateBox控件和DateTimeBox控件进行了梳理,而我之所以将EasyUI的DateBox控件和DateTimeBox控件放在一起,归为一类 ...
- Centos6下DRBD的安装配置
导读 Distributed Replicated Block Device(DRBD)是一个用软件实现的.无共享的.服务器之间镜像块设备内容的存储复制解决方案.数据镜像:实时.透明.同步(所有服务器 ...
- 移动互联网实战--Apple的APNS桩推送服务的实现(2)
前记: 相信大家在搞IOS推送服务的开发时, 会直接使用javapns api来简单实现, 调试也直连Apple的APNS服务(产品/测试版)来实现. 很少有人会写个APNS的桩服务, 事实也是如此. ...
- CSS控制a标签链接的四种状态
/*CSS链接的四种状态 *a:link 普通的.未被访问的链接样式 *a:visited 用户已访问的链接样式 *a:hover 鼠标指针位于链接上方样式 *a:active 链接被点击的时刻样式 ...
- Qt 串口编程学习1
1.测试设备:USB 转串口 将RX和TX短接 2.开发环境:windows 1安装Qt for windows 2测试,新建项目编译 出现问题 Cannot find file: C:\Docume ...
- leetcode 126. Word Ladder II ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...