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;
}

Installing Apps Kattis - installingapps (贪心 + 背包)的更多相关文章

  1. 【bzoj4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+背包dp

    题目描述 给出 $n$ 个括号序列,从中选出任意个并将它们按照任意顺序连接起来,求以这种方式得到匹配括号序列的最大长度. 输入 第一行包含一个正整数n(1<=n<=300),表示括号序列的 ...

  2. HDU3466Proud Merchants(贪心&背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=3466 题目大意是说n个物品每个物品的花费是p,但是如果你现在的钱少于q就买不了这个物品,每个物品的价值是v,求有 ...

  3. HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 Problem Description There are n apple trees plan ...

  4. bzoj4922 [Lydsy1706月赛]Karp-de-Chant Number 贪心+背包

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4922 题解 记录每一个串的没有匹配的右括号 \()\) 的数量为 \(a_i\),为匹配的左括 ...

  5. AT4120-[ARC096D]Sweet Alchemy【贪心,背包】

    正题 题目链接:https://www.luogu.com.cn/problem/AT4120 题目大意 给出\(n\)个物品和一个容量\(m\),第\(i\)个物品体积为\(c_i\).除了第一个物 ...

  6. HDU 4003 [树][贪心][背包]

    /* 大连热身A题 不要低头,不要放弃,不要气馁,不要慌张 题意: 给一棵树,每条边上有权值.给一个起点,放置n个机器人,要求使得任意一个节点至少被一个机器人经过. 每个机器人经过某条边时的代价为这条 ...

  7. POJ2392Space Elevator(贪心+背包)

    Space Elevator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9970   Accepted: 4738 De ...

  8. 【贪心+背包】【HDU2546】【饭卡】

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  9. sdut2408 pick apples (贪心+背包)山东省第三届ACM省赛

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/svitter/article/details/24642587 本文出自:http://blog.c ...

随机推荐

  1. 原型设计工具 Axure RP 7.0下载地址及安装说明

    Axure RP是产品经理必备的原型制作工具,因为很多同学是新手,在这里整理一下axure7.0的下载.安装和汉化流程,希望能够帮到大家. Axure RP是美国Axure Software Solu ...

  2. ZOJ 5638——Prime Query——————【线段树区间更新,区间查询,单点更新】

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  3. Android ImageView的几种对图片的缩放处理 解决imageview放大图片后失真问题解决办法

    我的解决办法: 1 首先设置android:layout_width=”wrap_content”和android:layout_height=”wrap_content”,否则你按比例缩放后的图片放 ...

  4. Android RxJava2+Retrofit2单文件下载监听进度封装

    RxJava2和Retrofit2用的越来越多,最近也在封装一个通用的网络请求库,其中就包括了单文件下载的方法,所以这里进行记录.文末附带Demo 由于网上很多的方法都是使用拦截器进行进度的监听,个人 ...

  5. Eucalyptus管理页面密码设置

    桉树环境什么的都已经是配置好了的,但是过了一段时间不用,也不知道密码是什么了,看着下面的页面也不知道如何进去,这里我们通过命令行的方式重置用户名和密码信息. 登陆clc所在机器,输入下命令: euar ...

  6. Spring+Hibernateh使用小结

    由此我们可以看出,报出错误的地方主要是slf4j的jar包,而故障码中“Failed to load class ’org.slf4j.impl.StaticLoggerBinder‘”的意思则是“加 ...

  7. iOS核心动画高级技巧之CALayer(一)

    iOS核心动画高级技巧之CALayer(一) iOS核心动画高级技巧之图层变换和专用图层(二)iOS核心动画高级技巧之核心动画(三)iOS核心动画高级技巧之性能(四)iOS核心动画高级技巧之动画总结( ...

  8. SqlServer Alwayson主副本图标显示问号的原因

    搭建完alwayson后,登录辅助副本服务器,查看alwayson可用性副本列表,看到主副本前面显示了一个问号,这里借用网上一张图片做展示: 在显示问号的主副本上右键属性查看,“角色”一栏中,显示的是 ...

  9. 美国L1签证面谈的时候一般VO会问到什么问题?

    L签证:L签证签发给被其中国公司调派到美国分公司或合资公司工作的人员.申请人必须将在美国担任经理级职务或具有专业知识,且在申请签证前的三年中至少为同一雇主或公司连续工作至少一年.签证签发费将因签证的入 ...

  10. 2017.12.22 Java序列化中你不知道的事(一)

    Java 序列化简介 Java 对象序列化是 JDK 1.1 中引入的一组开创性特性之一,用于作为一种将 Java 对象的状态转换为字节数组,以便存储或传输的机制,以后,仍可以将字节数组转换回 Jav ...