一、题意

有一个手机,容量为$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的数量。那么,有如下递推式:

枚举$1 \le i \le N,0 \le j \le C$,如果$j<s_i$,$dp[i][j]=dp[i-1][j]$;

如果$j \ge s_i且C-(j-s_i) \ge d_i$,$dp[i][j]=max(dp[i-1][j],dp[i-1][j-s_i]+1)$;

初始状态,全部的$dp$都是$0$。

然后,在状态转移的时候,需要记录选择的路径。

三、注意点

1、“如果$j \ge s_i且C-(j-s_i) \ge d_i$”,意思是,如果$j \ge s_i$且选择当前这个app之前剩余空间大于当前这个app的安装包大小,那么就可以安装这个app。

2、最后的答案不一定是$dp[N][C]$,而是$max\{dp[N][j]|0 \le j \le C\}$。

3、这题其实就是01背包模型,记录路径的方案和01背包一样。

4、切记:记录路径时,用额外数组的方式最靠谱。

四、代码

#include<bits/stdc++.h>
using namespace std;
struct app {
    int d, s, id;
} p[];
bool cmp(app a1, app a2) {
    return max(a1.d, a1.s) - a1.s > max(a2.d, a2.s) - a2.s;
}
][], ans[], acnt;
][];
int main() {
    scanf("%d%d", &N, &C);
    ; i <= N; ++i)scanf("%d%d", &p[i].d, &p[i].s), p[i].id = i;
    ; i <= N; ++i) {
        ; j <= C; ++j)dp[i][j] = ;
    }
    sort(p + , p + N + , cmp);
    ; i <= N; ++i) {
        ; j <= C; ++j) {
            dp[i][j] = dp[i - ][j];
            if(j >= p[i].s) {
                int last = j - p[i].s;
                if(C - last >= p[i].d) {
                    ][last] + ) {
                        dp[i][j] = dp[i - ][last] + ;
                        path[i][j] = ;
                    }
                }
            }
        }
    }
    ;
    ; --j) {
        if(aa < dp[N][j]) {
            aa = dp[N][j], V = j;
        }
    }
    ; --i) {
        if(path[i][j]) {
            ans[++acnt] = p[i].id;
            j -= p[i].s;
        }
    }
    reverse(ans + , ans + acnt + );
    printf("%d\n", acnt);
    ; i <= acnt; ++i)printf("%d ", ans[i]);
    ;
}
/*
3 4
2 1
3 2
3 3
*/

Northwestern European Regional Contest 2017-I题- Installing Apps题解的更多相关文章

  1. 2017-2018 Northwestern European Regional Contest (NWERC 2017)

    A. Ascending Photo 贪心增广. #include<bits/stdc++.h> using namespace std; const int MAXN = 1000000 ...

  2. Northwestern European Regional Contest 2016 NWERC ,F题Free Weights(优先队列+Map标记+模拟)

    传送门: Vjudge:https://vjudge.net/problem/Gym-101170F CF: http://codeforces.com/gym/101170 The city of ...

  3. 2015-2016 Northwestern European Regional Contest (NWERC 2015)

    训练时间:2019-04-05 一场读错三个题,队友恨不得手刃了我这个坑B. A I J 简单,不写了. C - Cleaning Pipes (Gym - 101485C) 对于有公共点的管道建边, ...

  4. Northwestern European Regional Contest 2014 Gym - 101482

    Gym 101482C Cent Savings 简单的dp #include<bits/stdc++.h> #define inf 0x3f3f3f3f #define inf64 0x ...

  5. codeforces Gym - 101485 D Debugging (2015-2016 Northwestern European Regional Contest (NWERC 2015))

    题目描述: 点击打开链接 这题题意其实很不好理解,你有一个n行的程序,现在程序运行了r时间之后停止了运行,证明此处有一个bug,现在你需要在程序中加printf来调试找到bug所在的位置,你每次加一个 ...

  6. 2012-2013 Northwestern European Regional Contest (NWERC 2012)

    B - Beer Pressure \(dp(t, p_1, p_2, p_3, p_4)\)表示总人数为\(t\),\(p_i\)对应酒吧投票人数的概率. 使用滚动数组优化掉一维空间. 总的时间复杂 ...

  7. 2006 ACM Northwestern European Programming Contest C题(二分求最大)

    My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a numberN o ...

  8. ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbilisi, November 24, 2010

    ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbil ...

  9. 2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17)

    2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17) A 题意:有 n 个时刻 ...

随机推荐

  1. Express文件上传之Multer

    Express文件上传之Multer Multer是一个nodejs中间件,用来处理http提交multipart/form-data,也就是文件上传.它是在busboy的基础上开发的. 在我看来,M ...

  2. 第33课 C++中的字符串类

    在C语言中学习字符串时,我们使用的是字符数组的概念. C语言中没有真正意义的字符串.为了表达字符串的概念,我们使用了字符数组来模拟字符串. 在应用程序开发中,我们需要大量的处理字符串,如果还用C语言中 ...

  3. UNIMRCP 代码走读

    基于UNIMRCP1.5.0的代码走读 与 填坑记录 1. server启动配置加载 入口:unimrcp_server.c static apt_bool_t unimrcp_server_load ...

  4. [LeetCode&Python] Problem 908. Smallest Range I

    Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...

  5. 注解JAVA对象(基于Hibernate4.3)

    package com.chauvet.po; import java.util.Date; import javax.persistence.Column; import javax.persist ...

  6. (4)socket的基础使用(基于TCP协议的并发编程)

    需要实现并发需要依靠socketserver 模块 socketserver模块下有几个功能 def __init__(self, request, client_address, server): ...

  7. C语言运算符优先级和ASCII表

    1. C语言运算符优先级及结合性 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右 -- () 圆括号 (表达式)/函数名(形参表) -- . 成 ...

  8. 【转】每天一个linux命令(27):linux chmod命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/11/29/2794010.html chmod命令用于改变linux系统文件或目录的访问权限.用它控制文 ...

  9. Oracle 表空间与数据文件

    -============================== --Oracle 表空间与数据文件 --============================== /* 一.概念 表空间:是一个或多 ...

  10. centos7 MFS drbd keepalived

    环境: centos7.3 + moosefs 3.0.97 + drbd84-utils-8.9.8-1 + keepalived-1.2.13-9 工作原理: 架构图: 节点信息: 节点名     ...