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. 《JS高程》引用类型学习笔记

    2月圆满的结束了,结束之前是如凤凰般的涅槃.一边上班,一边搞科研的忙碌有点让人透不过气,心会不由得浮躁起来.但是,无论什么事情,只要充满耐心.专心去做,总会朝好的方向发展,心态真的很重要.Anyway ...

  2. 学习opengl十大网站(转载)

    [转载] 1.http://nehe.gamedev.net/这个是我觉得全世界最知名的OpenGL教程,而且有网友将其中48个教程翻译成了中文http://www.owlei.com/Dancing ...

  3. android获取inflater

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: ? LayoutInflater inflater=(La ...

  4. 1130-host ... is not allowed to connect to this MySql server 开放mysql远程连接 不使用localhost

    报错:1130-host ... is not allowed to connect to this MySql server 解决方法: 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在loc ...

  5. 转载:为什么要对URI进行编码

            为什么需要Url编码,通常如果一样东西需要编码,说明这样东西并不适合传输.原因多种多样,如Size过大,包含隐私数据,对于Url来说,之所以要进行编码,是因为Url中有些字符会引起歧义 ...

  6. 7. Reverse Integer java

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 代码如下: pub ...

  7. 【渗透测试学习平台】 web for pentester -3.XSS

    Example 1 http://192.168.91.139/xss/example1.php?name=hacker<script>alert('xss')</script> ...

  8. jce

    jdk8:jce 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html jdk ...

  9. Codeforces Round #104 (Div. 1)

    A.Lucky Conversion 题意 给定两个长度为 \(N(N \le 10^5)\) 且由4和7构成的 \(a, b\)串 对 \(a\) 可以有两种操作: 交换两个位置的字符; 改变一个位 ...

  10. mysql 新建用户

    //===============================================================================  //代理商子账户订单服务器为了安全 ...