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. for else

  2. i++为什么没有自增探析——JVM中i++的实现(转)

    很多朋友在使用Java时候会发现一个很奇怪的现象. 那就是使用下列的短句时会发现i没有自增,这是很让人迷惑的,因为大家印象中,虽然i++优先级较低,但是总是会自增的,这里为什么i++没有自增? i=i ...

  3. linux性能监控基础命令

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

  4. java 代码分析工具——JDepend

    最近学习Mybatis的官方文档,看到了[项目文档]一节有很多内容没有见过,做个笔记,理解一下. 百科上的介绍,我竟然都看懂了,那就不找其他地方的资料了. JDepend 一个开放源代码的可以用来评价 ...

  5. The Hidden Pitfalls of AsyncTask

    http://logc.at/2011/11/08/the-hidden-pitfalls-of-asynctask/

  6. Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.

    svn更新代码后,打开xcode工程文件,会出现  xxx..xcodeproj  cannot be opened because the project file cannot be parsed ...

  7. Radiobutton编辑

    package com.example.yuekao3; import java.util.ArrayList;import java.util.List; import com.baidu.farm ...

  8. hdu3308 线段树——区间合并

    更新一个点: 求某个区间的最长连续上升序列: 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 #include <cstdio> #in ...

  9. JavaWeb学习记录(十九)——jsp标签库

    1.out标签 <%        //局部变量        String name="zsf>&<zz";        pageContext.se ...

  10. HDU-1542 Atlantis(离散化+扫描线)

    题目大意:给n个矩形,可能重叠,求面积. 题目分析:线段树维护扫描线. 代码如下: # include<bits/stdc++.h> using namespace std; # defi ...