Hackerrank "The Indian Job"

Source: Internet
Author: User

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 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 namespacestd;intKnapsack (vector<int> &a,intG) {    intn =a.size (); Vector<int> dp (G +1);  for(inti =1; I <= N; i + +)     for(intV =g; V >= a[i-1]; V--) Dp[v]= Max (Dp[v], dp[v-a[i-1]]+ a[i-1]); returnDp[g]; }intMain () {intT CIN >>T;  while(t--)     {        //Get Input        intN, G; CIN>> N >>G; Vector<int>A (N); intTotal =0;  for(inti =0; i < N; i++) {cin>>A[i]; Total+=A[i]; }                //        if(Total >2*G) cout<<"NO"<<Endl; Elsecout<< ((Total-knapsack (A, G) <= g)?"YES":"NO") <<Endl; }    return 0;}

Hackerrank "The Indian Job"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.