Installing Apps(贪心+01背包)

4人阅读 评论(0) 收藏 举报
分类:

5008: Installing Apps

时间限制: 2 Sec  内存限制: 128 MB
提交: 163  解决: 22
[提交][状态][讨论版][命题人:admin]

题目描述

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 d 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 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.

输入

The input consists of:
• One line with two integers n, c (1 ≤ n ≤ 500, 1 ≤ c ≤ 10 000), the number of available apps and the available disk space of the phone in megabytes.
• n lines, each with two integers d, s (1 ≤ d, s ≤ 10 000), the download size and storage size of an app, in megabytes.

输出

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

样例输入

2 100
99 1

1 99

样例输出

2

提示

来源

题意:有n种手机软件,每一种软件的安装包大小是d,安装后的软件大小是s(删除了安装包),那么若有c的手机内存,最多能安装多少个手机软件。

题解:按照贪心原则,可按安装包和软件大小的差值进行排序,同时在装背包的时候要考虑是否能装进d,即c-j>=d-s

#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;

inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
struct node
{
    int d,s,m;
}e[11000];
int n,c;
int cmp(node a,node b)
{
    return a.d-a.s>b.d-b.s;
}

int dp[30000305];
int main()
{
    int i,j;

    scanf("%d%d",&n,&c);
    for(i=1;i<=n;i++)
        scanf("%d%d",&e[i].d,&e[i].s);

    sort(e+1,e+1+n,cmp);

    int ans=0;
    for(i=1;i<=n;i++)
        for(j=c;j>=e[i].s;j--)
            if(c-j>=e[i].d-e[i].s)
            dp[j]=max(dp[j],dp[j-e[i].s]+1),ans=max(ans,dp[j]);

    printf("%d\n",ans);
    return 0;
}

查看评论

贪心法解决01背包(贪心算法)

  • 2009年12月20日 23:13
  • 901B
  • 下载

从01背包学习贪心算法和动态规划

从01背包学习贪心算法和动态规划: 算法的思路其实很大程度上都是相通的,比如在提升算法运行时间的不断探索中,我们用分治的思想来将一个大问题分解为很多小问题进行求解,并且这些子问题与原问题的结构是一样...
  • u014449866
  • u014449866
  • 2015-05-01 21:31:36
  • 2344

【算法笔记】贪心算法——01背包问题

插入代码块#include using namespace std;int main(){ int n; //物品数 float c; //背包容量 float res = ...
  • MASILEJFOAISEGJIAE
  • MASILEJFOAISEGJIAE
  • 2016-05-29 12:06:46
  • 2579

HDU3466 - Proud Merchants(贪心+01背包)

题目链接 HDU3466 【题意】有M( 【分析】一看就是01背包,但是他加了个限制条件,购买者需要有qi元钱,这样就让每个物品购买顺序的不同而影响到01背包的结果,则需要贪心的排一下序才可以,以...
  • u012760629
  • u012760629
  • 2014-11-27 09:09:48
  • 413

贪心算法_01背包问题_Java实现

什么是贪心算法?是指在对问题进行求解时,总是做出当前看来是最好的选择。也就是说,不从整体最优上加以考虑,所得出的结果仅仅是某种意义上的局部最优解。因此贪心算法不会对所有问题都能得到整体最优解,但对于很...
  • ljmingcom304
  • ljmingcom304
  • 2015-12-15 09:41:31
  • 4645

NWERC 2017(English Restaurant-期望)

Random groups of sizes between 1 and g people arrive at the restaurant. Each group occupies the s...
  • nike0good
  • nike0good
  • 2017-12-13 17:30:00
  • 197

Codeforce 730J(贪心+01背包)

链接:点击打开链接 题意:给n个瓶子,给出每个瓶子剩余水的容量和每个瓶子的体积,问最少需要使用的瓶子数量能装完剩下全部的水,每次改变1单位的水花费1单位时间,并输出最少时间 代码:#include...
  • stay_accept
  • stay_accept
  • 2016-12-05 13:21:57
  • 246

关于 IOS 的299$ Enterprize IDP (In House)及Installing Apps Wirelessly 详解

1。首先要明白什么是 IOS 299$ 美元企业模式企业类型的是不让上传到appstore的,所以不存在审核期。 它是要你自己配置个服务器,让别人通过URL的形式来安装的。 2.官文的文档地址...
  • fhbystudy
  • fhbystudy
  • 2013-10-23 10:11:36
  • 1777

Installing Ionic

Installing Ionic Improve this doc Ionic apps are created and developed primarily through the Ionic c...
  • zhupengfei
  • zhupengfei
  • 2017-11-28 10:14:02
  • 48

证明01分数背包问题具有贪心选择性质

写公式比较麻烦,所以干脆就在纸上写好之后,把照片贴上来。
  • u010339647
  • u010339647
  • 2016-01-26 16:56:48
  • 1783
    个人资料
    持之以恒
    等级:
    访问量: 752
    积分: 548
    排名: 9万+
    文章存档
    最新评论