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"的更多相关文章

  1. 日常小测:颜色 && Hackerrank Unique_colors

    题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...

  2. Number To Indian Rupee Words in Oracle Forms / Reports

    Convert numbers to Indian Rupees format in Oracle Forms / Reports.Create the below mention function ...

  3. HackerRank "Square Subsequences" !!!

    Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...

  4. 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 ...

  5. HackerRank "TBS Problem" ~ NPC

    It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...

  6. HackerRank Extra long factorials

    传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...

  7. HackerRank "Lucky Numbers"

    Great learning for me:https://www.hackerrank.com/rest/contests/master/challenges/lucky-numbers/hacke ...

  8. HackerRank "Playing with numbers"

    This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah ...

  9. HackerRank "Array and simple queries" !

    The most interesting, flexible and juicy binary tree problem I have ever seen. I learnt it from here ...

随机推荐

  1. 转载blog_Linux下Tomcat日志定期清理 及 logrotate 配置

    服务器上的tomcat的catalina.out文件越来越大,查看起来很不方便,以前每次都是想起来的时候手工清理一下(cat /dev/null > catalina.out),后来发现了log ...

  2. php面向对象之抽像类、接口、final、类常量

    一.抽像类(abstract)        在我们实际开发过程中,有些类并不需要被实例化,如前面学习到的一些父类,主要是让子类来继承,这样可以提高代码复用性语法结构:  代码如下 复制代码   ab ...

  3. HPCC 登录总结

    最近开始做NGS的分析,数据明显更大,在自己的机子上面做有些不现实了,需要登录高性能计算机. 1. 目录结构: home directory: /auto/rcf-40/USERNAME -- onl ...

  4. Codeforces Round #301 (Div. 2) B. School Marks

    其实是很水的一道bfs题,昨晚比赛的时候没看清题意,漏了一个条件. #include<cstdio> #include<cstring> #include<iostrea ...

  5. C#使用指针的2个主要原因

    一下内容来自于书籍:<C#高级编程(第六版)> C#使用指针的2个主要原因:

  6. eBay_Relist(退换刊登费)

    如果物品首次刊登结束时没有人中标,或是买家并没有付款完成交易,那么卖家以通过重新刊登的方法来再次销售.如果该物品在第二次刊登时成功售出,且满足eBay的重新刊登退费条件,那么eBay便会退还重新刊登的 ...

  7. Apache使用mod_deflate模块压缩页面优化传输速度

    可以写为一行,也可以写多行,默认为gzip SetOutputFilter DEFLATE # Restrict compression to these MIME types AddOutputFi ...

  8. wordpress(一)wordpress环境的搭建

    搭建wordpress环境因为自动安装的脚本不提供创建数据库的功能,所以先要创建数据库. 1.使用如下命令创建数据库(都是在已经登陆的mysql界面中的命令) ①CREATE DATABASE 数据库 ...

  9. VPS搭建VPN(pptpd)

    环境:Ubuntu Server 12.04 下载FQ程序 wget http://cdxf.yun.ftn.qq.com/ftn_handler/40ad8a2875adf1f7b5193f54a5 ...

  10. HTTP 状态消息

    1xx: 信息 消息: 描述: 100 Continue 服务器仅接收到部分请求,但是一旦服务器并没有拒绝该请求,客户端应该继续发送其余的请求. 101 Switching Protocols 服务器 ...