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. css中常用的hack

    <!DOCTYPE html> <html> <head> <title>Css Hack</title> <style> #t ...

  2. Think Python - Chapter 15 - Classes and objects

    15.1 User-defined typesWe have used many of Python’s built-in types; now we are going to define a ne ...

  3. mapreduce入门之wordcount注释详解

    mapreduce版本:0.2.0之前 说明: 该注释为之前学习时找到的一篇,现在只是在入门以后对该注释做了一些修正以及添加. 由于版本问题,该代码并没有在集群环境中运行,只将其做为理解mapredu ...

  4. JS初学之-选项卡(图片切换类)

    初学选项卡,主要问题卡在了索引值上面,花了较长的时间学习. 索引值其实很好理解,就是为每一个元素用JS的方法添加一个属性,即自定义属性. 在for循环里的函数里用i,会直接弹出这个数组的length, ...

  5. makefile--模式规则(七)

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 上一节讲到目录创建成功,目标文件没有生产到对应目录下,这里我们先给目标文件加上对应目录,这样的话 ...

  6. POJ1459 Power Network(网络最大流)

                                         Power Network Time Limit: 2000MS   Memory Limit: 32768K Total S ...

  7. 【P1835】小红花

    很简单的题,然而我没想到,在NOIP上怎么办嘛QAQ 话说这题不知道怎么分类啊……先扔到玄学里边把…… 原题: Fj在圣诞节来临之际,决定给他的奶牛发一些小红花.现在Fj一共有N头奶牛,这N头牛按照编 ...

  8. Python爬虫学习笔记——豆瓣登陆(一)

    #-*- coding:utf-8 -*- import requests from bs4 import BeautifulSoup import html5lib import re import ...

  9. PS CS5

    1.图层 背景图层:双击解锁 右下按键:新建图层.删除图层.新建图层组 眼睛-图层的显示与隐藏 缩略图大小选择:右上三角-面板设置 打开那个图片然后选择工具里面的移动工具然后按住鼠标拖动,拖到那个图片 ...

  10. 下载word文档

    来源:http://www.cnblogs.com/damonlan/archive/2012/04/28/2473525.html 作者:浪迹天涯 protected void GridView1_ ...