Installing Apps Kattis - installingapps

Sandra recently bought her first smart phone. One of her friends suggested a long list of applications (more commonly known as “apps”) that she should install on the phone. Sandra immediately started installing the apps from the list, but after installing a few, the phone did not have enough disk space to install any more apps. Sometimes, the app installation failed because there was not even enough space to download the installation package. Other apps could be downloaded just fine, but had insufficient space to store the installed app.

Each app that Sandra installs has a download size dd and a storage size ss. To download the app, Sandra’s phone must have at least dd megabytes of free disk space. After the app has been installed, it then uses ss 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)max(d,s) megabytes of free disk space.

Sandra quickly realised that she may have run out of space just because she installed apps in the wrong order. Thus, she decided to give the installation another try. She uninstalled all apps, and will 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.

Input

The input consists of:

  • One line with two integers nn, cc (1≤n≤500,1≤c≤100001≤n≤500,1≤c≤10000), the number of available apps and the available disk space of the phone in megabytes.

  • nn lines, each with two integers d,sd,s (1≤d,s≤100001≤d,s≤10000), the download size and storage size 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 from 11 to nn, in the order they are given in the input. If there are multiple optimal solutions, output any one of them.

Sample Input 1 Sample Output 1
2 100
99 1
1 99
2
1 2
Sample Input 2 Sample Output 2
2 100
500 1
1 500
0

题意:一个内存为V 的手机可以安装一些app,每一个app有两个不同的数值,表示已开始下载的大小和安装完以后的大小(必须下载的和安装后的大小都比手机的剩余容量要小才可以放进去),问最多可以放进去几个app,和放进去背包的一些顺序

题解:这是一个背包问题,但是普通的背包拿去是没有顺序关系的,这道题拿去的顺序关系是有关系的,前面的一个的拿去是有可能会影响后面一个app的,所以要先贪心一下。具体的贪心操作是按照d-s的大小来排序,就是下载的和安装后的相减的值的从大到小排序。为什么要这样来贪心我的理解是容量的改变多的话如果放在后面,他由于改变的多了会更加容易影响。同时这道题在uva上好像不能ac,可能是uva的数据源出问题了

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std; typedef long long ll;
const int MAXN=1e4+5;
const double EPS=1e-8;
const int INF=0x3f3f3f3f;
const int MOD = 1e9+7;
struct Node{
int d,s,id;
}a[MAXN];
int f[505][MAXN],n,V;
bool cmp(const Node a, const Node b){
return (a.d - a.s) > (b.d - b.s);
//return a.s < b.s;
}
int main(){ ios::sync_with_stdio(false);
while(cin >> n >> V)
{
for(int i=1;i<=n;i++)
{
cin >> a[i].d >> a[i].s;
a[i].id = i;
}
sort(a+1,a+n+1,cmp);
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++)
{
for(int j=V;j>=0;j--)
{
f[i][j] = f[i-1][j];
if(V-(j-a[i].s) < a[i].d)
continue;
if(j < a[i].s)
continue;
f[i][j] = max(f[i-1][j], f[i-1][j-a[i].s]+1);
}
}
int Max = 0, v = 0;
for(int i=0;i<=V;i++)
{
if(f[n][i] > Max)
{
Max = f[n][i];
v = i;
}
}
cout << f[n][v] << "\n";
if(f[n][v] == 0) continue;
int q[MAXN],cnt = 0;
for(int i=n;i>=1&&v>0;i--)
{
if(f[i][v] == f[i-1][v-a[i].s]+1)
{
q[++cnt] = a[i].id;
v -= a[i].s;
}
}
for(int i=cnt;i>=1;i--)
{
cout << q[i] << " \n"[i==1] ;
}
}
return 0;
}

最新文章

  1. 【一次面试】再谈javascript中的继承
  2. php 字符串和数字比较一些问题
  3. android MediaPlayer API大全已经方法详解(转载)
  4. [Android] 关于getinstalledpackages参数的分析
  5. [CareerCup] 4.3 Create Minimal Binary Search Tree 创建最小二叉搜索树
  6. Android Keycode详解
  7. JqGrid在IE8中表头不能分组的解决办法
  8. [记录] web icon 字体
  9. vs2013 上传碰到的问题:“输入的不是有效的 Base-64 字符串 ”
  10. json.net json转换神器
  11. cache的工作原理
  12. 自定义控件之圆形颜色渐变进度条--SweepGradient
  13. Office远程代码执行漏洞CVE-2017-0199复现
  14. 201521123040《Java程序设计》第3周学习总结
  15. Yii2自带的验证码背景颜色怎么调?
  16. Mac终端开启代理
  17. Mac 比较实用的软件
  18. linux上的图片查看器FEH_image_view
  19. 读取excel 文件到datatable
  20. STS中web.xml配置文件

热门文章

  1. jsp内置对象和el表达式内置对象误区
  2. P1736 创意吃鱼法80
  3. JAVA基础系列(一) 概述与相关概念
  4. 13.padding和margin,几种参数
  5. mysql通过event和存储过程实时更新简单Demo
  6. SublimeText相关配置
  7. 表格&lt;table&gt;
  8. FusionCharts使用教程:为JavaScript图表提供数据
  9. FMCW 雷达原理(转)
  10. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:0.概述