当前位置:网站首页>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
注意:要好好读题,这题最后一个状态尤其细节。。也至关重要
边栏推荐
- CheckBox美化按钮选中样式
- 【云原生】什么是微服务?怎么搭建?手把手教你搭建第一个微服务(框架)
- C语言数组的简单认识
- The solution of invalid @data annotation in idea2018
- cv. Houghcircles: Circular Hough transform opencv
- Difference between idea open and import project
- R分析可视化实用数据(航班_教育_餐厅_租户_变迁_寿命_安全)
- OPPO K9试水“捆绑销售”,消费者“赚了”还是“亏了”?
- HQChart实战教程55-欧易网K线面积图
- Hqchart actual combat tutorial 55 area map of K line of ouyi.com
猜你喜欢
随机推荐
TimeHelper
【安全科普】挖矿技术,从一个理工男的爱情故事讲起
科技PRO实力测评:高端按摩椅市场综合PK,究竟谁才配得上机皇?
单片机通信数据延迟问题排查
ROS Basics - use the launch file (I) - start multiple ROS nodes in batch
正则表达式
In June, 2022, China Database ranking: tidb made a comeback to win the crown, and Dameng was dormant and won the flowers in May
B_QuRT_User_Guide(17)
R analysis visual practical data (flight \u education \u restaurant \u tenant \u change \u life \u safety)
C language pointer
B / Qurt Utilisateur Guide (19)
{dataSource-1} closing ... {dataSource-1} closed
VMware virtual machine IP, gateway settings. The virtual machine cannot be pinged to the Internet
postgresql源码学习(十八)—— MVCC③-创建(获取)快照
ASLR
B_ QuRT_ User_ Guide(20)
MySQL学习笔记:JSON嵌套数组查询
postgresql 语句
Gd32 can sends no mailbox fault
Dépannage du problème de retard des données de communication du micro - ordinateur à puce unique









