当前位置: 首页 > news >正文

5.7线性动态规划1

P2285 [HNOI2004] 打鼹鼠

#include<bits/stdc++.h>
using namespace std;
struct node{int x, y, t;
}a[100010];
int dp[100010];
void solve(){int n, m; cin >> n >> m;for(int i = 1; i <= m; i++){cin >> a[i].t >> a[i].x >> a[i].y;}int maxx = 0;for(int i = 1; i <= m; i++){dp[i] = 1;for(int j = 1; j < i; j++){if((abs(a[i].x - a[j].x) + abs(a[i].y - a[j].y)) <= (a[i].t - a[j].t)){dp[i] = max(dp[i], dp[j] + 1);}}maxx = max(maxx, dp[i]);}cout << maxx << endl;
}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve();return 0;
} 

P1020 [NOIP 1999 提高组] 导弹拦截

#include<bits/stdc++.h>
using namespace std;
const int N = 50010;
int a[N], b[N];
void solve(){int cnta = 0, cntb = 0, x;while(cin >> x){//最长不上升子序列的长度==最少上升子序列的个数 int s = 0;int l = -1, r = cnta;while(l + 1 != r){int mid = l + r >> 1;if(a[mid] < x)r = mid;else l = mid;}if(r != cnta){a[r] = x;s = 1;}/*for(int i = 0; i < cnta; i++){if(a[i] < x){a[i] = x;s = 1;break;}}*/if(!s)a[cnta++] = x;//最少不上升子序列的个数==最长上升子序列的长度 s = 0, l = -1, r = cntb;while(l + 1 != r){int mid = l + r >> 1;if(b[mid] >= x)r = mid;else l = mid;}if(r != cntb){b[r] = x;s = 1;}/*for(int i = 0; i < cntb; i++){if(b[i] >= x){b[i] = x;s = 1;break;}}*/if(!s)b[cntb++] = x;}cout << cnta << endl << cntb << endl;
}
signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve();return 0;
} 

P1725 琪露诺

#include<bits/stdc++.h>
using namespace std;
const int N = 400010;
int dp[N];
void solve(){int n, l, r; cin >> n >> l >> r;for(int i = 0; i <= n; i++)cin >> dp[i];for(int i = n - l; i >= 0; i--){int s = -1e9;for(int j = l; j <= r; j++){s = max(s, dp[i + j]);}dp[i] += s;}cout << dp[0] << endl;
}
signed main()
{ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve();return 0;
}

做法2:单调队列

#include<bits/stdc++.h>
using namespace std;
const int N = 400010;
int dp[N];
void solve(){deque<int> q; int n, l, r; cin >> n >> l >> r;for(int i = 0; i <= n; i++)cin >> dp[i];q.push_back(n + 1);//把n + 1索引的0加进去,防止负数影响 for(int i = n - l; i >= 0; i--){if(q.front() > i + r)q.pop_front();//不在滑动窗口if(!q.empty()){while(dp[i + l] > dp[q.back()]){//把小的丢掉 q.pop_back();if(q.empty())break;}} q.push_back(i + l);//放入大的索引 dp[i] += dp[q.front()];//窗口最大值 }cout << dp[0] << endl;
}
signed main()
{ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);solve();return 0;
}


文章转载自:
http://bisexed.hfytgp.cn
http://acetal.hfytgp.cn
http://barricade.hfytgp.cn
http://anastrophy.hfytgp.cn
http://archimage.hfytgp.cn
http://cancerroot.hfytgp.cn
http://benighted.hfytgp.cn
http://afterdamp.hfytgp.cn
http://adversarial.hfytgp.cn
http://assibilation.hfytgp.cn
http://arrenotokous.hfytgp.cn
http://bigot.hfytgp.cn
http://anglesite.hfytgp.cn
http://backhanded.hfytgp.cn
http://accessibly.hfytgp.cn
http://antipathy.hfytgp.cn
http://capelin.hfytgp.cn
http://carpometacarpus.hfytgp.cn
http://candock.hfytgp.cn
http://booter.hfytgp.cn
http://backformation.hfytgp.cn
http://castries.hfytgp.cn
http://beta.hfytgp.cn
http://alderman.hfytgp.cn
http://bipinnate.hfytgp.cn
http://bopomofo.hfytgp.cn
http://animalization.hfytgp.cn
http://aphorize.hfytgp.cn
http://callosity.hfytgp.cn
http://campagus.hfytgp.cn
http://www.dtcms.com/a/177526.html

相关文章:

  • Ubuntu 安装 Keepalived、LVS
  • ROS1和ROS2使用桥接工具通信
  • leeCode算法之独一无二出现次数
  • 自由浮动时间和总浮动时间对比
  • 贷中业务提额、降额策略
  • (eNSP)策略路由实验配置
  • Java中有哪些锁?
  • OpenShift AI - 模型注册管理
  • 绕线机的制作与研究
  • ‌人工智能在农作物病虫害识别中的应用前景分析
  • 【LUT技术专题】基于扩展卷积的极快速LUT算法
  • 如何快速获取旺店通奇门原始数据至本地
  • 嵌入式软件学习指南:从入门到进阶
  • STM32基础教程——软件SPI
  • Cadence 高速系统设计流程及工具使用二
  • 前端面经-VUE3篇(四)--pinia篇-基本使用、store、state、getter、action、插件
  • MDP相关内容
  • 贵州省棒球运动发展中长期规划(2024-2035)·棒球1号位
  • 第二天 网络与通信协议
  • 【c++】 我的世界
  • 汽车加气站操作工考试知识点总结
  • 手机端调试工具 eruda 使用方法
  • C++ 中的 `it->second` 和 `it.second`:迭代器与对象访问的微妙区别
  • 图片转文字-Tesseract-OCR,完成文字转换。
  • vtkSmartPointer<vtkPolyData> 常用的函数方法
  • 二项式反演及其代数证明
  • 生物化学笔记:神经生物学概论12 大脑全景图 知觉、行为和语言 注意力
  • LeetCode105_从先序与中序遍历序列构造二叉树
  • Napkin 简易教程
  • async/await的另一种食用方法