A variation to 0-1 Knapsack. (No Python code got fully AC. Python is too slow for this problem)

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; int main()
{
// Get input
int N, X; cin >> N >> X;
vector<int> weight, cost;
for (int i = ; i < N; i++)
{
int w, c; cin >> w >> c;
weight.push_back(w);
cost.push_back(c);
} // T
typedef pair<long long, int> Rec; // max-profit, cost
vector<Rec> f(X + );
for (int i = ; i < N; i++)
for (int v = X; v >= cost[i]; v--)
{
int newCost = f[v - cost[i]].second + cost[i];
long long newProf = f[v - cost[i]].first + weight[i];
if (newCost == v)
{
f[v].first = max(f[v].first, f[v - cost[i]].first + weight[i]);
f[v].second = v;
}
}
if (f[X].second < X)
cout << "Got caught!" << endl;
else
cout << f[X].first << endl;
return ;
}

HackerRank "Dorsey Thief"的更多相关文章

  1. codeforces 632+ E. Thief in a Shop

    E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input standard ...

  2. Codeforces632E Thief in a Shop(NTT + 快速幂)

    题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...

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

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

  4. HackerRank "Square Subsequences" !!!

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

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

  6. HackerRank "TBS Problem" ~ NPC

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

  7. HackerRank Extra long factorials

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

  8. HackerRank "Lucky Numbers"

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

  9. HackerRank "Playing with numbers"

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

随机推荐

  1. Qt之动画框架

    简述 Qt动画框架旨在为创建动画和平滑的GUI提供了一种简单的方法.通过Qt动画属性,该框架为部件和其它QObject对象的动画操作提供了非常大的自由性,框架也可以被用于图形视图框架中,动画框架中许多 ...

  2. 2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

    odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  3. 学习使用:before和:after伪元素

    http://www.w3cplus.com/css3/learning-to-use-the-before-and-after-pseudo-elements-in-css.html

  4. hdu2546 饭卡    01背包

    link:http://acm.hdu.edu.cn/showproblem.php?pid=2546 也算一个贪心的想法吧. 先把总钱数减去5,再把价值最大的挑出来.然后用01背包.最终买下挑出来的 ...

  5. JS初学之-自定义属性(索引值)

    重点:1.添加索引值的作用:建立匹配.对应的关系. 比如:使每一个按钮对应数组里的每一张图,arrImg[this.index]. 2.不要在for循环的函数里面使用i. 3.添加索引值的方法aBtn ...

  6. spark yarn-cluster 和 yarn-client提交的配置

    1. spark conf 目录下需要配置进去hadoop home 2.需要spark 提交的配置文件 加上‘-- master yarn-cluster/yarn-client’设置提交的模式

  7. poj3660 最短路/拓扑序

    题意:有n头牛,为了给牛排顺序,给出了牛之间的胜负关系,具有传递性,问给出的胜负关系是否可以给这些牛排出唯一的顺序. 其实是个拓扑排序问题,牛的胜负关系就是有向边,然后判断是否有唯一的拓扑序就行.当然 ...

  8. Java设计模式之适配器设计模式

    1.适配器模式( Adapter)定义将一个类的接口转换成客户希望的另外一个接口.Adapter 模式使得原来由于接口不兼容而不能一起工作的 那些类可以一起工作. 现实案例如下:  墙上电源类(22 ...

  9. 使用Cloudera部署,管理Hadoop集群

    Hadoop系列之(三):使用Cloudera部署,管理Hadoop集群 http://www.cnblogs.com/ee900222/p/hadoop_3.html Hadoop系列之(一):Ha ...

  10. /usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h:No such file or directory的解决办法

    在编译32位HDecode时出现如题所示的错误,原因时没有安装32位glibc库导致的: ubuntu: sudo apt-get install libc6-dev-i386 CentOS:yum ...