日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

牛客网暑期ACM多校训练营(第三场)A - PAXM Team(01背包)

發布時間:2024/4/18 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 牛客网暑期ACM多校训练营(第三场)A - PAXM Team(01背包) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

思路

多重限制條件的01背包,輸出選取那幾小組,可以開個bool數組記錄選取物品時的狀態,然后從最后一個物品往上找標記。

后記

這道題比賽的時候數據太水,簡單排序選一件物品就行,看別人代碼還有直接輸出零過了。。。比賽結束1小時左右,加強數據了,還好提前過了 ^ _ ^
物品標記當時想錯了,賽后看看學長代碼真的強

#include <iostream> #include <algorithm> #include <stdio.h> #include <vector> #include <map> #include <bitset> #include <set> #include <string.h> #include <cmath> #include <queue> #include <algorithm> #define N 40 #define ll long long #define lowbit(a) a&(-a) #define pb(a) push_back(a) #define mk(a, b) make_pair(a, b) #define mem(a, b) memset(a, b, sizeof(a)) #define read(a) scanf("%d", &a) #define print(a) printf("%d\n", a) using namespace std; struct group{int p, a, c, m, g; }a[N]; // 也可以開4個數組存每個小組信息,因為沒有問最大的分值 int dp[N][N][N][N]; bool vis[N][N][N][N][N]; int main(){#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifint n, P, A, M, C, G;scanf("%d", &n);for (int i = 0; i < n; ++i) {scanf("%d%d%d%d%d", &a[i].p, &a[i].a, &a[i].c, &a[i].m, &a[i].g);}scanf("%d%d%d%d", &P, &A, &C, &M);for (int i = 0; i < n; ++i) {for (int j = P; j >= a[i].p; --j) {for (int k = A; k >= a[i].a; --k) {for (int l = C; l >= a[i].c; --l) {for (int r = M; r >= a[i].m; --r) {if (dp[j - a[i].p][k - a[i].a][l - a[i].c][r - a[i].m] + a[i].g > dp[j][k][l][r]) {dp[j][k][l][r] = dp[j - a[i].p][k - a[i].a][l - a[i].c][r - a[i].m] + a[i].g;// 將當前狀態標記vis[i][j][k][l][r] = true;}}}}}}vector<int> wu;// 從最后一個物品向前找標記for (int i = n - 1, j = P, k = A, l = C, r = M; i >= 0; --i) {if (vis[i][j][k][l][r]) {wu.push_back(i);j -= a[i].p;k -= a[i].a;l -= a[i].c;r -= a[i].m;}if (j < 0 || k < 0 || l < 0 || r < 0)break;}if (wu.empty()) {printf("%d\n", 0);}else {printf("%d\n", wu.size());sort(wu.begin(), wu.end());for (auto it : wu) {printf("%d ", it);}printf("\n");}return 0; }

總結

以上是生活随笔為你收集整理的牛客网暑期ACM多校训练营(第三场)A - PAXM Team(01背包)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。