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

标签: 贪心 01背包 dp 思维
63人阅读 评论(0) 收藏 举报
分类:

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

查看评论

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

01背包问题(DP解决)

问题主题:著名的01背包问题 问题描述: 有n个重量和价值分别为wi、vi的物品,现在要从这些物品中选出总重量不超过W的物品,求所有挑选方案中的价值最大值。 限制条件: 1...
  • qq_31828929
  • qq_31828929
  • 2016-09-03 21:07:39
  • 632

01背包问题讲解(dp)

给大家附上一个题目吧,便于理解 ctest有n个苹果,要将它放入容量为v的背包。给出第i个苹果的大小和价钱,求出能放入背包的苹果的总价钱最大值。 输入:每组测试数据第一行为2个正整数,分别代表苹...
  • Yumerina
  • Yumerina
  • 2017-02-12 13:53:45
  • 427

2012-2013 Northwestern European Regional Contest (NWERC 2012)【solved:6 / 11】

有一个大家都会的D。 emmm。我不会 但是!队友会呀2333先放放 -update:2017年10月16日20:49:39...
  • qq_29556211
  • qq_29556211
  • 2017-09-01 14:44:39
  • 166

【贪心】 NWERC 2013 Absurdistan Roads1

先做最小生成树,然后找距离不相同权值最小的边。。。。 #include #include #include #include #include #include #includ...
  • blankcqk
  • blankcqk
  • 2015-06-30 15:53:30
  • 273

【NWERC2015】【BZOJ4425】AssigningWorkstations分配工作站

DescriptionPenelope is part of the admin team of the newly built supercomputer. Her job is to assign...
  • CreationAugust
  • CreationAugust
  • 2016-03-24 15:39:27
  • 754

I - Identifying Map Tiles 2015-2016 Northwestern European Regional Contest (NWERC 2015)

题意: 给出一个map,询问对应点的坐标 思路: 找规律可得。x*2 + z   y*2+z 对于每个0 1  2 3  分情况讨论 #include #include #incl...
  • qq_33951440
  • qq_33951440
  • 2017-10-09 12:21:12
  • 140

2013-2014 Northwestern European Regional Contest (NWERC 2013)

http://codeforces.com/gym/100405/ 补题: A 已知一个图的点数为n,且改图为连通图。告诉了每两个点之间的最短路的距离。让你构造这个图,且这个图的边数为n。 I...
  • Codeblocksm
  • Codeblocksm
  • 2016-09-07 21:31:08
  • 361

DP背包之01背包、完全背包、多重背包笔记

这是个经典话题,值得好好研究一番,本文作为学习笔记将会不断更新。 主要参考了以下资料: 背包问题九讲:http://love-oriented.com/pack/Index.html 背包之...
  • hellobinfeng
  • hellobinfeng
  • 2013-12-23 13:32:36
  • 3153
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 7万+
    积分: 3285
    排名: 1万+
    博客专栏
    最新评论