首页 > 其他分享 >前缀后缀01背包(牛客2023多校D清楚姐姐学01背包)

前缀后缀01背包(牛客2023多校D清楚姐姐学01背包)

时间:2023-01-30 23:12:42浏览次数:58  
标签:pre suf 01 int max 多校 long 背包

0x1f 题目:


0x2f 题意:

  • 定义初始背包的最优解\(V_{max}\)
  • 定义n个物品去掉任意一个后,最优解为\(V_{max}'\)
  • 每一个物品\(w[i],v[i]\),在\(v[i]\)上加上一个最小值,使得\(V_{max}\) > \(V_{max}'\)
  • 背包的容量是m

0x3f 思路:

  • 前缀背包:$pre[ i ][ j ] $ 表示前 \(i\) 个物品使用体积为 \(j\) 的最大价值
  • 后缀背包:\(suf[ i ][ j ]\) 表示 \(i\) 之后物品使用体积为 \(j\) 的最大价值(转移的方法和01背包一样)
  • 接下来贪心考率在什么情况下\(V_{max}\) > \(V_{max}'\)
    • 计算\(V_{max}'\):\(\max _{0 \to m}^j (pre[i-1][j] + suf[i+1][m-j])\)
    • 要让\(V_{max}\) > \(V_{max}'\)成立,可以在背包里提前预留\(w[i]\),计算出最大值,要让物品\(i\)成为必需品,至少需要加上 \(V_{max}\) + 1 - \((V_{max}' + v[i])\)
    • 为什么?因为贪心结果是必须把物品 \(i\) 放入背包的,只有这一个物品,容易贪心留\(w[i]\)的空间是最优的
    • 计算预留\(w[i]\)空间:\(\max _{0 \to m - w[i]}^j (pre[i-1][j] + suf[i+1][m-j-w[i]])\)

0x4f 代码:

#include<bits/stdc++.h>
#define debug1(a) cout<<#a<<'='<< a << endl;
#define debug2(a,b) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<endl;
#define debug3(a,b,c) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<endl;
#define debug4(a,b,c,d) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<"  "<<#d<<" = "<<d<<endl;
#define debug5(a,b,c,d,e) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<"  "<<#d<<" = "<<d<<"  "<<#e<<" = "<<e<<endl;
#define endl "\n"
#define fi first
#define se second
#define caseT int T;cin >> T;while(T--)

#define int long long
//#define int __int128
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;

//#pragma GCC optimize(3,"Ofast","inline")
//#pragma GCC optimize(2)

inline int rd()
{
	int f=1,x=0;char c=getchar();
	while(c<'0'||'9'<c){if(c=='-')f=-1;c=getchar();}
	while('0'<=c&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return f*x;
}

//常数定义
const double eps = 1e-6;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int N = 5010;

void solve(){
    int n, m; cin >> n >> m;
    vector<int>w(n), v(n);
    for(int i = 0; i < n; ++i) {
        cin >> w[i] >> v[i];
    }
    vector<vector<LL>> pre(n, vector<LL>(m + 1));
    vector<vector<LL>> suf(n, vector<LL>(m + 1));
    for(int  i = 0; i < n; ++i) {
        if(i) pre[i] = pre[i - 1];
        for(int j = m; j >= w[i]; --j) {
            pre[i][j] = max(pre[i][j], pre[i][j - w[i]] + v[i]);
        }
    }
    for(int i = n - 1; i >= 0; --i) {
        if(i < n - 1) suf[i] = suf[i + 1];
        for(int j = m; j >= w[i]; --j) {
            suf[i][j] = max(suf[i][j], suf[i][j - w[i]] + v[i]);
        }
    }
    for(int i = 0; i < n; ++i) {
        LL max1 = 0, max2 = 0;
        for(int j = 0; j <= m; ++j) {
            max1 = max(max1, (i ? pre[i - 1][j] : 0) + (i + 1 < n ? suf[i + 1][m - j] : 0));
        }
        for(int j = 0; j <= m - w[i]; ++j) {
            max2 = max(max2, (i ? pre[i - 1][j] : 0) + (i + 1 < n ? suf[i + 1][m - j - w[i]] : 0));
        }
        cout << max(0LL, max1 + 1 - v[i] - max2) << "\n";
    }
}

signed main()
{
    
    /*
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    */

    //caseT
        solve();

    return 0;

}
/*

*/

标签:pre,suf,01,int,max,多校,long,背包
From: https://www.cnblogs.com/cfddfc/p/17077485.html

相关文章

  • [CTSC2015]日程管理
    linkSolution鉴于其于我的教育性意义,所以决定还是不放在寒假橄榄计划......
  • day14-JdbcTemplate-01
    JdbcTemplate-01看一个实际需求:如果希望使用spring框架做项目,Spring框架如何处理对数据库的操作呢?方案一:使用之前的JdbcUtils类方案二:spring提供了一个操作数据库(......
  • Windows11 WSL 打开Ubuntu 报错 WslRegisterDistribution failed with error: 0x80070
    Windows11WSL打开Ubuntu报错WslRegisterDistributionfailedwitherror:0x800701bc 1、背景说明Windows在不安装虚拟化软件的情况下,如果想安装Linux子系统,可以......
  • drf项目 day01 入门
    一、web应用模式#前后端混合开发(前后端不分离):返回的是html的内容,需要写模板,就像是我们写bbs项目时创建的.html文件,在里面写Python代码#前后端分离:后端工程师只专注于写......
  • C/C++学生成绩信息管理[2023-01-30]
    C/C++学生成绩信息管理[2023-01-30]2.学生成绩信息管理【问题描述】利用哈希表完成学生成绩信息的管理。每个学生记录包含学生学号(Number)、真实姓名(Name)、网名(Scre......
  • 0145-Go-环境变量
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/environment-variables目标使用Go语言的环境变量。示例packagemainimport("fmt"......
  • 0146-Go-HTTP 客户端
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/http-clients目标使用Go语言的HTTP客户端。示例packagemainimport("bufio"......
  • 0147-Go-HTTP 服务端
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/http-servers目标使用Go语言HTTP服务端。示例packagemainimport("fmt""ne......
  • 0148-Go-上下文
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/context目标使用Go语言的上下文。示例packagemainimport("fmt""net/http""time"......
  • drf 01
    web应用模式#1.djangoweb框架,专门用来写web项目#2.前后端混合开发-作为后端人员也需要写模板语法-作为前后端都混合时期的全栈工程师,则需要都写,内容比......