NWERC 2017 :Installing Apps (01背包DP + 思维+ 贪心)

Installing Apps

时间限制: 2 Sec  内存限制: 128 MB
提交: 170  解决: 24
[提交][状态][讨论版][命题人: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

提示

来源


[题意]

    手机容量大小为C, n个app   每个app 安装包大小为d  安装后大小为s; d安装可以直接转换成s

    现在问你, 如何安装顺序,能够安装更多的app , 问最多能装几个app

[思路]

    安装一个app  空间要满足  max(d,s)   最多安装几个, 贪心的思想, 根据 (d-s) 的从大到小 贪心;

    根据差值排序获得 更大的可能,若只安装其中一个考虑, 另一个则会成为牵制, 若按照(d+s) 考虑, d会转换成s

    

    贪心后 dp 个数,  dp[i]  代表 容量 i  安装了几个app  类似于01 背包,  s 是重量;  但还要满足 剩下的空间 大于 d-s


[Code]

    

/*
* Date:4/16/2018
* Tile: NWERC 2017 :Installing Apps 
* Category: 01DP 贪心-greedy
* Attention: 注意贪心方式 d-s 
* DP[i] 代表容量 i 个app 个数
*/
#include <iostream>
#include <bits/stdc++.h>

typedef long long ll;
const int MAXN=1e4+7;

using namespace std;

struct node{
    int d,s;
}a[MAXN];
int dp[504*MAXN];
int cmp(node a,node b)
{
 //   if( (a.d-a.s) == (b.d-b.s))
  //      return a.s<b.s;
    return (a.d-a.s)>(b.d-b.s);
}
int main()
{
    int n,c;
    cin>>n>>c;
    for(int i=1;i<=n;i++)
    {
        int x,y;
        cin>>x>>y;
        a[i].d=x;a[i].s=y;
    }
    memset(dp,0,sizeof(dp));
    dp[0]=0;
    sort(a+1,a+n+1,cmp);
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=c;j>=1;j--)
        {
            if(j>=a[i].s && c-j >=( a[i].d-a[i].s))
            {
                dp[j]= max(dp[j],dp[j-a[i].s]+1);
                ans=max(ans,dp[j]);
            }

        }
    }
    cout<<ans<<endl;
    return 0;
}

123

posted @ 2018-04-16 18:37  Sizaif  阅读(472)  评论(0编辑  收藏  举报