Problem IInstalling AppsTime limit: 2 seconds

Mobile radio telephone, public domainSandra recently bought her first smart phone. One ofher friends suggested a long list of applications (morecommonly known as “apps”) that she should install onthe phone. Sandra immediately started installing theapps from the list, but after installing a few, the phonedid not have enough disk space to install any more apps.Sometimes, the app installation failed because there wasnot even enough space to download the installation pack-age. Other apps could be downloaded just fine, but had insufficient space to store the installed app.Each app that Sandra installs has a download sized  and a storage size s To download the app, Sandra’s phone must have at least d megabytes of free disk space. After the app has been installed, it then uses s  megabytes of  disk space on the phone. The download size may be smaller than the storage size (e.g., if the  app data is heavily compressed) or larger than the storage size (e.g., if the download contains  material that might not get used such as translations to different languages). The installer is very  efficient and can transform the downloaded package to an installed app without using any extra   disk space. Thus, to install an app, the phone must have at least

max(d, s)megabytes of freedisk space.Sandra quickly realised that she may have run out of space just because she installed apps in thewrong order. Thus, she decided to give the installation another try. She uninstalled all apps, andwill now choose an installation order that lets her install the largest number of apps from the list.

Sandra may not install any app more than once.Help her determine what apps on the list she should install, and in what order.

InputThe input consists of:•One line with two integersn,c(1≤n≤500,1≤c≤10 000), the number of availableapps and the availabledisk space of the phone in megabytes.

n lines, each with two integersd, s(1≤d, s≤10 000), the download size and storagesize of an app, in megabytes.

Output

Output one line with the maximum number of apps that can be installed. Then output one line

listing the numbers of those apps, in the order that Sandra should install them. In the case that

no apps can be installed, this line can be omitted.

The apps are numbered from1to n , in the order they are given in the input. If there are multiple

optimal solutions, output any one of them.

NWERC 2017 Problem I: Installing Apps

1 dp[j] j表示的是我们用包里面j的容量会产生最大的价值,这个时候,包内剩余的容量是c-j;

如果是我们想要拿第i个物品来更新j的状态的话我们会有,这个状态的最大dp[j-[i].v],并且对于这个

状态包内剩余的空间是c-(j-[i].v);

一般01背包的最大效益与物品排放的顺序是没有关系的,但是这里放东西的前提是我们必须先有足够大

的空间,那么我们放的顺序如果是不同的话就会出现不同的价值;

按照d-s值从大到小排序,我们可以这么想d-s很大的话,d一定很大,s很小如果我们前面不加入的话,后面它的可以用的体积减小了,就更不可能加上了;所以放的话一定是在前面放;

对于某一个体积下产生的最大值要放的物品数目是一定的,我们要记录状态的转移,这里我们可以用vector来记录一下;

#include <bits/stdc++.h>
using namespace std;
const int Max = 1e5+10;
typedef long long ll;
struct node {ll s,d,id;
}p[Max];
bool cmp(node a, node b){return a.d-a.s>b.d-b.s;
}
ll dp[Max];
vector <int> pre[Max];
int main(){int n,c;scanf("%d %d",&n,&c);for(int i=1;i<=n;i++){scanf("%lld %lld",&p[i].d,&p[i].s);p[i].id=i;}sort(p+1,p+1+n,cmp);for(int i=1;i<=n;i++){for(int j=c;j>=p[i].s;j--){if(c>=j-p[i].s+p[i].d){if(dp[j]<dp[j-p[i].s]+1){dp[j]=dp[j-p[i].s]+1;pre[j]=pre[j-p[i].s];pre[j].push_back(p[i].id);}}}}int ans=0,id=0;for(int i=1;i<=c;i++){if(dp[i]>ans){ans=dp[i];id=i;}}printf("%d\n",ans);int len=pre[id].size();for(int i=0;i<len;i++){int to=pre[id][i];printf("%d%c",to,(i==len-1)?'\n':' ');}return 0;
}

41 01背包 记录方案相关推荐

  1. [01背包] 背包问题求具体方案(01背包+求方案数+思维)

    文章目录 0. 前言 1. 01背包+求方案数+思维 0. 前言 相关: [背包] 背包问题算法模板(模板) 1. 01背包+求方案数+思维 12. 背包问题求具体方案 求方案数也是背包问题.dp 的 ...

  2. [01背包] 数字组合(01背包+求方案数)

    文章目录 0. 前言 1. 01背包求方案数 0. 前言 相关: [背包] 背包问题算法模板(模板) 1. 01背包求方案数 278. 数字组合 将 M 看作背包容量,每个数看成一个物品,Ai 看成是 ...

  3. POJ 1293 - Duty Free Shop 01背包记录所选物品

    裸的01背包.dp[x]只要是bool型记录当前空间是否可用.. 而为了找到用了哪些物品..dp[x]设置为int型..进行记录.. Program: #include<iostream> ...

  4. ☆【UVA - 624 】CD(dp + 0-1背包 + 记录路径)

    题干: You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music ...

  5. 【01背包记录路径】东东开车了

    题面 东东开车出去泡妞(在梦中),车内提供了 n 张CD唱片,已知东东开车的时间是 n 分钟,他该如何去选择唱片去消磨这无聊的时间呢 假设: CD数量不超过20张 没有一张CD唱片超过 N 分钟 每张 ...

  6. 物资调度(01背包+求方案数)

    物资调度 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 某地区发生了地震,灾区已经非常困难,灾民急需一些帐篷.衣物.食品和血浆等物资.可通往灾区的道路到处都是塌方,70 ...

  7. Bone Collector(骨头收集器) ---01背包 记录

    问题描述 多年前,在泰迪的家乡,有一个人叫"骨头收藏家".这个人喜欢收集各种各样的骨头,狗的,牛的,他也去坟墓了-- 收集骨头的人有一个V体积的大袋子,在他收集的过程中,骨头有很多 ...

  8. 【01背包求方案数模板】洛谷 P1164 小A点菜

    洛谷 P1164 小A点菜 https://www.luogu.org/problemnew/show/P1164 题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家--餐馆, ...

  9. hdu 2955 01背包

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 如果认为:1-P是背包的容量,n是物品的个数,sum是所有物品的总价值,条件就是装入背包的物品的体积和不能 ...

最新文章

  1. 【生活】 戴尔XPS 插上耳机之后,说话没声音,麦克风失效 window10 系统
  2. 【Android】可以下拉刷新的webview,使你的webview效果更加好看,封装自己的WebView...
  3. 在centOS7.2里安装virtualenv和flask
  4. 训练日志 2019.1.13
  5. Timer定时器控件 1130
  6. 头条号【编编成程】开通
  7. 我的MVVM框架 v3教程——todos例子
  8. STM32 IWDG时间计算方法
  9. Linux的chkconfig命令详解
  10. SAP打印脱机请求和输出请求管理
  11. 【EI会议推荐】机电一体化、自动化与智能控制领域
  12. 内蒙古自治区包头市谷歌高清卫星地图下载
  13. python 完全平方数
  14. c语言九宫格的递归算法,九宫格 数独 求解 算法 栈实现
  15. unity3d开发 打飞机小游戏(五)(飞机移动/敌机死亡动画)
  16. 巅峰极客pwn wp
  17. 第七章、Spring Boot MyBatis升级篇
  18. 关于Twitter Card的一个巨坑
  19. 图像自动去暗角算法(vegnetting
  20. Jmeter 安装历史版本

热门文章

  1. K - Oulipo
  2. PAT--1094 谷歌的招聘(C语言)
  3. 第九天javaSE基础(学生管理系统)
  4. 某公司高管疾呼:底层程序员离职率太高,有人入职不到半年就走!建议把恶意离职加入征信,让年轻人对公司有起码的尊重!...
  5. 读刘未鹏老大《你应当怎样学习C++(以及编程)》
  6. 变更3个概念:变更请求,批准的变更请求,确认的变更的理解
  7. 变更请求、批准的变更、确认的变更
  8. 小觅相机录制rosbag数据集
  9. 价值链-商业模式名词解释11
  10. java面向对象知识点整理--用前人智慧,为后人铺路