HackerRank "Dorsey Thief"
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"的更多相关文章
- 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 ...
- Codeforces632E Thief in a Shop(NTT + 快速幂)
题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...
- 日常小测:颜色 && Hackerrank Unique_colors
题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...
- HackerRank "Square Subsequences" !!!
Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...
- 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 ...
- HackerRank "TBS Problem" ~ NPC
It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...
- HackerRank Extra long factorials
传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...
- HackerRank "Lucky Numbers"
Great learning for me:https://www.hackerrank.com/rest/contests/master/challenges/lucky-numbers/hacke ...
- HackerRank "Playing with numbers"
This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah ...
随机推荐
- 一个js对象的代码结构
初步理解的js里一个对象的写法: GameLayer GameLayer.js var GameLayer = cc.Layer.extend({ //私有属性(带下划线"_&q ...
- iOS学习笔记---oc语言第六天
Block .数组高级 block本质上就是匿名函数(没有名称的函数) block语法和函数指针很相似 回顾函数 函数:C语⾔中,实现某一类功能的代码段. 完整的函数包含两部分:函数声明.函数定义 函 ...
- ZOJ 1216 Deck
原题链接 题目大意:1/2+1/4+1/6+…1/n 解法:直接累加即可. 参考代码: #include<stdio.h> int main(){ printf("# Cards ...
- OC小结
#import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * a ...
- scala言语基础学习四
伴生对象 object方法构造函数只会执行一次.伴生对象和对象之间private对象可以互相访问 让object继承抽象类 apply方法 可以不new就构造对象 类似于var s = ArrayBu ...
- 关于ServiceLocator ,AdpaterAwareInterface 注入
今天学习zf2 的过程,视频中讲到要把数据库的中的表继承TableGateway中,然后注册在ServiceManager中,但是里面没有$adapter,需要在serviceManger,中配置in ...
- 黑马程序员——JAVA基础之构造函数,构造代码块
------- android培训.java培训.期待与您交流! ---------- 构造函数特点: 1. 函数名与类名相同 2. 不用定义返回值类型 3. 不可以写return语句 构造函数 ...
- JSON的转换(将JSON转换为字符串,将字符串转化为JSON)
eval()和JSON.stringify() 1.eval用于从一个字符串中解析出json 对象,创建包含 JSON 语法的 JavaScript 字符串.例如 var str = '{ &qu ...
- js类型判断
console.log('---------------------'); var a="string"; console.log(a); //string var a=1; co ...
- android屏蔽软键盘并且显示光标
if (android.os.Build.VERSION.SDK_INT <= 10) {//4.0以下 danielinbiti editText.setInputType(InputType ...