当前位置:网站首页>C. Jump and Treasure(dp + 单调队列优化)
C. Jump and Treasure(dp + 单调队列优化)
2022-06-11 03:21:00 【to cling】
Problem
给出一个从0开始,长度为n+1一段序列,从1到n每个点上都有一定价值的金币a[i](可正可负)。从0号结点开始,每次可以跳跃不超过p个结点,跳到结点i上就(假设从i号结点跳到j号结点, i < j , j − i ≤ p i < j, j - i\leq p i<j,j−i≤p )。有q个询问,每次给出一个x( 1 ≤ x ≤ n 1 \leq x \leq n 1≤x≤n),问每次只能跳到x倍数的结点上(如果该结点编号小于n的话,只能跳到x的整数倍结点上。当跳到小于等于n的最后一个x的整数倍节点上时,只要最后一步跳出n,且这一步不超过p即可),同时,每次跳跃不能超过p,且最终必须跳到大于n的结点上:问多可以收集多少价值的金币。如果不能跳到大于n的结点上,则输出"Noob"。
Solution
- 状态表示:dp[i]表示跳到结点i上能够收集的最大价值的金币。
- 状态转移: d p [ i ] = m a x ( d p [ j ] ) + a [ i ] ( i % x = 0 , j % x = 0 , i − j ≤ p ) dp[i] = max(dp[j]) + a[i]~~~~~~(i \% x = 0, j \% x = 0,i - j \leq p) dp[i]=max(dp[j])+a[i] (i%x=0,j%x=0,i−j≤p)
可以用单调队列维护可以转移到状态i的几种状态。 - 注意:最终必须跳到大于n的节点上,最后一个状态应该是
n + 1,而不是最后一个x的整数倍节点上(n / x * x + x)。而且这两个状态不等价。
例如: n = 10,p = 5, x = 2.
当最后一种状态是n + 1时(11),可以由 dp[6],dp[8],dp[10]中最大的一个转移过来。
而,最后一种状态是n / x * x + x时(12),可以由dp[8],dp[10]中最大的一个转移过来。
显然,两种情况不等价,尤其需要注意。
Code
const int N = 2e6 + 6;
ll dp[N];
ll a[N];
unordered_map<ll, ll>mp;
int main()
{
IOS;
int n, q, p; cin >> n >> q >> p;
for (int i = 1; i <= n; i++)
cin >> a[i];
while (q--)
{
deque<int> q;
int x; cin >> x;
if (mp.count(x))
{
cout << mp[x] << endl;
continue;
}
if (x > p) cout << "Noob\n";
else
{
dp[0] = 0;
q.push_back(0);
vector<int>b;
b.push_back(0);
for (int i = x; i <= n; i += x)
b.push_back(i);
b.push_back(n + 1);//注意:本题中n以内只能跳x的倍数,n以外可以不是x的倍数,所以不能写成n / x * x + x
for (int i = 1; i < sz(b); i++)
{
while (q.empty() == 0 && b[i] - b[q.front()] > p) q.pop_front();
dp[i] = dp[q.front()] + a[b[i]];
while (q.empty() == 0 && dp[q.back()] <= dp[i]) q.pop_back();
q.push_back(i);
}
mp[x] = dp[sz(b) - 1];
cout << mp[x] << endl;
}
}
return 0;
}
Tips
注意:要好好读题,这题最后一个状态尤其细节。。也至关重要
边栏推荐
- cv. Matchtemplate image model matching opencv
- Resolved: JDBC connection to MySQL failed with an error:'The last packet sent successfully to the server was 0 milliseconds ago. '
- Disk quota exceeded
- ArTalk | 如何用最小投入,构建国产超融合进化底座?
- 蓄力618 ,苏宁如何打下这场硬仗?
- three.js炫酷科技感背景h5动画
- Deep parsing of question mark expressions
- 2022年6月中国数据库排行榜:TiDB卷土重来摘桂冠,达梦蛰伏五月夺探花
- 【ELT.ZIP】OpenHarmony啃论文俱乐部——快速随机访问字符串压缩
- The solution of invalid @data annotation in idea2018
猜你喜欢

WinDbg-虚拟机-双机调试-驱动文件的调试

B_ QuRT_ User_ Guide(18)

音乐正版率关键数据缺失,网易云音乐IPO胜算几何?

Artalk | how to build a domestic hyperfusion evolutionary base with minimum investment?

three.js炫酷科技感背景h5动画

PostgreSQL source code learning (22) - fault recovery ③ - transaction log registration

If the source code of the home page module is not separated ----- > nanny level source code analysis (1)

Checkbox beautify button selected style

【云原生】什么是微服务?怎么搭建?手把手教你搭建第一个微服务(框架)

一文搞懂单片机驱动8080LCD
随机推荐
Pyqt5: basic usage of label control
postgresql源码学习(21)—— 故障恢复②-事务日志初始化
Integrated MP code generator
一文搞懂单片机驱动8080LCD
Basic use of sonarqube platform
PostgreSQL source code learning (XX) -- fault recovery ① - transaction log format
TweenMax五彩小球弹跳动画
JS top icon menu click to switch background color JS special effect
Disk quota exceeded
File file = new File(“test.txt“)文件路径
C language array and pointer exercises
配置用命令行编译的环境-MSVC
B_QuRT_User_Guide(19)
js点击太阳月亮切换白天黑夜js特效
Windows10 installing keras
Promise使用
【ELT.ZIP】OpenHarmony啃论文俱乐部——电子设备软件更新压缩
Logical deletion_ Swagger2 framework integration
sonarqube平台基础使用
Difference between idea open and import project