【蓝桥杯专题】 DP(C++ | 洛谷 | acwing | 蓝桥)
迪丽瓦拉
2025-05-30 09:21:03
0次

菜狗现在才开始备战蓝桥杯QAQ

文章目录

  • 【蓝桥杯专题】 DP(C++ | 洛谷 | acwing | 蓝桥)
  • AcWing 1205. 买不到的数目
  • Acwing 1216. 饮料换购【模拟】
  • 01背包
  • 271. 杨老师的照相排列
  • 最长公共上升子序列
  • P
  • P
  • P
  • P
  • P
  • P
  • P
  • P
  • 总结

在这里插入图片描述

【蓝桥杯专题】 DP(C++ | 洛谷 | acwing | 蓝桥)

  • 看最后总结!!

AcWing 1205. 买不到的数目

链接 链接

  • 思路 暴力打表 找规律
#include 
using namespace std;int main () {int p, q;cin >> p >> q;cout << ((p - 1) * (q - 1) - 1) << endl;return 0;
} 

Acwing 1216. 饮料换购【模拟】

链接 链接

#include 
#include 
#include using namespace std;int main()
{int n, ans ;cin >> n; ans = n;while((n / 3) >= 1) {// cout <

01背包

链接 链接
在这里插入图片描述

#include 
// #include 
using namespace std;
typedef long long ll;
typedef double db;
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define per(i, a, n) for(int i = n; i <= a; i --)
#define pb push_back;
#define fs first;
#define sz second;
#include  // atoi
#define debug cout<<"debug"<<"\n"
#define endl "\n";
const int INF = 0x3f3f3f3f;
const int mod=1e9+7;
const int N = 1010;int w[N], v[N];
int f[N][N];void solve () {int N, V;ll ans = 0;cin >> N >> V;rep(i, 1, N) {cin >> v[i] >> w[i];}
// 当前背包容量不够(j < v[i]),没得选,因此前 i 个物品最优解即为前 i−1个物品最优解:f[i][j] = f[i - 1][j]。
// 如果可以选 :f[i][j] = f[i - 1][j - v[i]] + w[i]。rep(i, 1, N) {    rep(j, 1, V) {if(j < v[i]) f[i][j] = f[i - 1][j];else f[i][j] = max(f[i - 1][j], f[i - 1][j - v[i]] + w[i]);}}cout << f[N][V] << endl;
}int main(void){freopen("in.txt","r",stdin);ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T = 1;// cin >> T;while(T --) solve();return 0;
}

271. 杨老师的照相排列

链接 链接
在这里插入图片描述


#include 
// #include 
using namespace std;
typedef long long ll;
typedef double db;
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define per(i, a, n) for(int i = n; i <= a; i --)
#define pb push_back;
#define fs first;
#define sz second;
#include  // atoi
#define debug cout<<"debug"<<"\n"
#define endl "\n";
const int INF = 0x3f3f3f3f;
const int mod=1e9+7;
const int N = 31;ll f[N][N][N][N][N];void solve () {int n;while(cin >> n , n) {int s[5] = {0};rep(i, 0, n - 1) cin >> s[i];memset(f, 0 , sizeof f);f[0][0][0][0][0] = 1;   rep(a, 0, s[0]) {rep(b, 0, min(s[1], a)) {rep(c, 0, min(s[2], b)) {rep(d, 0, min(s[3], c)) {rep(e, 0, min(s[4], d)) {// f[a][b][c][d][e]代表从后往前每排人数分别为a, b, c, d, e的所有方案的集合, 其中a >= b >= c >= d >= e; 逆序的
// f[a][b][c][d][e]的值是该集合中元素的数量;ll &x = f[a][b][c][d][e];if (a && a - 1 >= b) x += f[a - 1][b][c][d][e];if (b && b - 1 >= c) x += f[a][b - 1][c][d][e];if (c && c - 1 >= d) x += f[a][b][c - 1][d][e];if (d && d - 1 >= e) x += f[a][b][c][d - 1][e];if (e) x += f[a][b][c][d][e - 1];// 当a > 0 && a - 1 >= b时,最后一个同学可能被安排在第1排,方案数是f[a - 1][b][c][d][e];
// 当b > 0 && b - 1 >= c时,最后一个同学可能被安排在第2排,方案数是f[a][b - 1][c][d][e];
// 当c > 0 && c - 1 >= d时,最后一个同学可能被安排在第3排,方案数是f[a][b][c - 1][d][e];
// 当d > 0 && d - 1 >= e时,最后一个同学可能被安排在第4排,方案数是f[a][b][c][d - 1][e];
// 当e > 0时,最后一个同学可能被安排在第5排,方案数是f[a][b][c][d][e - 1];} }}}}cout << f[s[0]][s[1]][s[2]][s[3]][s[4]] << endl;}}int main(void){freopen("in.txt","r",stdin);ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T = 1;// cin >> T;while(T --) solve();return 0;
}

最长公共上升子序列

链接 链接

#include 
// #include 
using namespace std;
typedef long long ll;
typedef double db;
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define per(i, a, n) for(int i = n; i <= a; i --)
#define pb push_back;
#define fs first;
#define sz second;
#include  // atoi
#define debug cout<<"debug"<<"\n"
#define endl "\n";
const int INF = 0x3f3f3f3f;
const int mod=1e9+7;
const int N = 3111;int a[N], b[N];
int f[N][N];void solve () {int n ;cin >> n;rep(i, 1, n) cin>> a[i];rep(i, 1, n) cin>> b[i];//版本1// rep(i, 1, n) {//     rep(j, 1 , n) {//         f[i][j] = f[i - 1][j];//         if(a[i] == b[j]) {//             // int maxv = 1;  // O(n^ 3)//             // for(int k =1; k < j; k ++) {//             //     if(b[j] > b[k]) {//             //         maxv = max(maxv, f[i - 1][k] + 1);//             //     }//             //     f[i][j] = max(maxv, f[i][j]);//             // }//         }//     }// }//版本2: rep(i, 1, n) {int maxv = 1;rep(j, 1, n) {f[i][j] = f[i - 1][j];if(a[i] == b[j] ) f[i][j] = max(maxv, f[i][j]);if(a[i] > b[j]) maxv = max(maxv, f[i - 1][j] + 1);}}int res = 0;rep(i, 1, n) {res = max(res, f[n][i]);}cout << res << endl;}int main(void){freopen("in.txt","r",stdin);ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T = 1;// cin >> T;while(T --) solve();return 0;
}
# P
[链接 链接]( )

P

链接 链接

P

链接 链接

P

链接 链接

P

链接 链接

P

链接 链接

P

链接 链接

P

链接 链接

P

链接 链接

总结

  • 数论别浪费太多时间, 做法暴力打表找规律 , 能做出来就做
  • exit(0) 调试bug 针对没有输出的时候好用
  • DP 多刷 (大部分题型)

在这里插入图片描述

相关内容

热门资讯

AI做攻略、“双向奔赴”……国... 本文转自:人民网人民网记者 许维娜今年国庆假期,AI(人工智能)成了不少年轻人的“旅行搭子”。行前做...
合肥的这个“梗”, 翻篇了 社交平台的评论区,曾长期流传一句专属于合肥的黑色幽默。有人问合肥哪里好玩,本地网友半开玩笑地回复:合...
7万+旅游中高层都在的群,今天... 做旅游这行,资源卡位、渠道互通、项目落地——往往就差一个“对的机会”。我们的行业社群已聚集70,00...
一场帐篷音乐节,如何让井研从“... 帐篷音乐节现场封面新闻记者 周洪攀10月的研溪湿地,晚风里裹着柑橘的清甜与民谣的回响。夜幕降临,10...
白鹤翩翩落星愿,上海迪士尼度假... 10月9日,世界候鸟日前夕,上海迪士尼度假区星愿公园的湖风里多了一分白鹤的灵动。三座白鹤雕塑正式揭幕...
成都锦江边赏初秋枫叶飘飘 目前,成都锦江边国华街枫叶树红了。近年来,国华街附近的几条街都栽种了红枫树,是城区较集中看枫叶的点位...
GBTA:商务旅行正在复苏,但... 亚太正在成为全球商务旅行增长的重要引擎。在经历疫情后的快速反弹之后,全球商务旅行正在进入一个新的增长...
从景区焕新到节赛引流 邹城双节... 齐鲁网·闪电新闻10月9日讯 中秋、国庆“双节”假期,邹城市文化和旅游局提前谋划、周密部署,立足自身...
江山如此多娇 且看石楼今朝 金秋十月,黄河岸畔秋意盎然,丰收盛会喜迎国庆。本次石楼国庆文旅活动以“江山如此多娇,且看石楼今朝”为...
海南国庆假期接待游客475.4... 2026年国庆假期,海南接待游客475.40万人次,较2025年增长13.1%,较2024年增长15...