Installing Apps Kattis - installingapps (贪心 + 背包)

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;

using namespace std;
#define ms(a,b) memset(a,b,sizeof(a))
#define lson rt*2,l,(l+r)/2
#define rson rt*2+1,(l+r)/2+1,r
typedef unsigned long long ull;
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;
}

原文地址:https://www.cnblogs.com/smallhester/p/10324888.html

时间: 2024-11-09 01:49:30

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

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

题目描述 给出 $n$ 个括号序列,从中选出任意个并将它们按照任意顺序连接起来,求以这种方式得到匹配括号序列的最大长度. 输入 第一行包含一个正整数n(1<=n<=300),表示括号序列的个数. 接下来n行,每行一个长度在[1,300]之间的括号序列,仅由小括号构成. 输出 输出一行一个整数,即最大长度,注意你可以一个序列也不选,此时长度为0. 样例输入 3 ()) ((() )() 样例输出 10 题解 贪心+背包dp 首先对于一个括号序列,有用的只有:长度.消耗'('的数目.以及'('减去

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 Problem Description There are n apple trees planted along a cyclic road, which is L metres long. Your storehouse is built at position 0 on that cyclic road. The ith tree is planted at position xi,

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

/* 大连热身A题 不要低头,不要放弃,不要气馁,不要慌张 题意: 给一棵树,每条边上有权值.给一个起点,放置n个机器人,要求使得任意一个节点至少被一个机器人经过. 每个机器人经过某条边时的代价为这条边的权值.反复经过需要反复累积. 问最小的代价是什么. 思路: 1.转化为背包问题.考虑给某个子树i个机器人的最小代价,即有i个机器人跑到某棵子树不回来.其中0个代表给某子树一个机器人,该机器人 遍历完该子树所有节点以后又返回该节点的代价.然后相当于每棵子树有几个物品,至少从中选择一个.进行分组背包

POJ2392Space Elevator(贪心+背包)

Space Elevator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9970   Accepted: 4738 Description The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <

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

本文出自:http://blog.csdn.net/svitter/ 题意:三种苹果,每种都有对应的Size,Value,给你一个背包空间,求最大的价值. 本题目的关键就在于非常大的背包空间 依据indicates the size (1 <= S<= 100) 我们可以考虑在1000000(100^3)之外的空间放性价比最高的苹果.为什么时100^3? 要知道背包如果正好填满,而填满空间相应价值的苹果大于不填满的价值的苹果,那么就选择能填满空间而使价值最大的苹果,而非性价比最高的苹果--性价

Northwestern European Regional Contest 2017-I题- Installing Apps题解

一.题意 有一个手机,容量为$C$,网上有$N$个app,每个app有个安装包大小$d_i$,有个安装后的占用空间大小$s_i$,安装app是瞬间完成的,即app的占用空间可以瞬间由$d_i$变成$s_i$,而不需要其他多余的空间.问这个手机最多可以安装多少个app,输出最多可以安装的app数量和安装顺序. 二.思路 很显然的$dp$.按照$max(d,s)-s$从大到小排序.$dp[i][j]$表示在前$i$个app中,占用空间不超过$j$的条件下最多可以安装的app的数量.那么,有如下递推式

【贪心+背包】BZOJ1334 [Baltic2008]Elect

Description 从N个数中选出任意个数且和尽量大,但要满足去掉任意一个和就小于总和的一半.n<=300, ai<=1e5. Solution 这个条件其实就是 去掉选出的最小的一个和就小于一半 所以从大到小背包 Code 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 const int maxn=1e5+5; 6 7 int

2017 NWERC

2017 NWERC Problem A. Ascending Photo 题目描述:给出一个序列,将其分成\(m\)份(不需要均等),使得将这\(m\)份重新排列后构成的是不下降序列,输出最小的\(m-1\). solution 待解决. Problem B. Boss Battle 题目描述:有环形的\(n\)根柱子,只有一根柱子后面有boss,每次向一根柱子投一个炸弹,炸弹波及范围为那根柱子和相邻的柱子,若boss在这三根柱子后面,则boss被炸死,若boss没有被炸死,则boss会选择原

Can Microsoft’s exFAT file system bridge the gap between OSes?

转自:http://arstechnica.com/information-technology/2013/06/review-is-microsofts-new-data-sharing-system-a-cross-platform-savior/ With Apple's licensing of Microsoft's exFAT file system, it seems like we finally have a good option for OS X and Windows d