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. 设置viewport进行缩放

    <meta name="viewport" content="width=320,maximum-scale=1.3,user-scalable=no"& ...

  2. linux性能监控基础命令

    压力测试监控下系统性能方法之一 #top 该命令监控的是进程的信息 看图逐行意义 top:执行命令的之间 up:已经执行了277天 2users:目前有两个使用者,使用#who可以查看具体的使用者详情 ...

  3. Qt之QFileSystemWatcher

    简述 QFileSystemWatcher类用于提供监视文件和目录修改的接口. QFileSystemWatcher通过监控指定路径的列表,监视文件系统中文件和目录的变更. 调用addPath()函数 ...

  4. eclipse workspace出错而导致启动不了

    遇到这种问题也是第一次让我觉得eclipse没有vs强大. 说正题,关于怎么解决的问题,也是上网搜了一大堆.主要有两种吧: 1.就是删除这个workspace的整个metadata,这样就可以打开这个 ...

  5. 简单插入排序(C++版)

    #include <iostream> using namespace std; /** \ Insert Sort * * Key: * * reserve: tm = a[i] * * ...

  6. [转]国内良心DNS汇集

    http://www.changbizi.net/archives/664.html 长鼻子实验室 湖北电信的DNS服务器真是烂到掉渣,曾经有一年我给他们的售后打电话到人家都记住我的手机号码,但是DN ...

  7. PCL可视化显示 直接加载显示pcb文件

    简单可视化类,是指直接在程序中使用,而且不支持多线程. #include<iostream> #include<pcl\point_cloud.h> #include<p ...

  8. 【转】 WebService到底是什么?

    WebService到底是什么? http://blog.csdn.net/wooshn/article/details/8069087/

  9. hdu1003 dp(最大子段和)

    题意:给出一列数,求其中的最大子段和以及该子段的开头和结尾位置. 因为刚学过DP没几天,所以还会这题,我开了一个 dp[100002][2],其中 dp[i][0] 记录以 i 为结尾的最大子段的和, ...

  10. java基础之:匿名内部类应用例子一

    一:接口 package com.yeepay.sxf.testclassforinner; /** * 回调接口. * @author shangxiaofei * */ public interf ...