当前位置:网站首页>C. Jump and treat (DP + monotonic queue optimization)
C. Jump and treat (DP + monotonic queue optimization)
2022-06-11 03:29:00 【to cling】
Problem
Give an example from 0 Start , The length is n+1 A sequence , from 1 To n Each point has a certain value of gold coins a[i]( Positive but negative ). from 0 Start at node , You can jump no more than each time p Nodes , Jump to the node i Go up ( Assuming that the i Node No. jumps to j Node No , i < j , j − i ≤ p i < j, j - i\leq p i<j,j−i≤p ). Yes q A asked , One at a time x( 1 ≤ x ≤ n 1 \leq x \leq n 1≤x≤n), You can only jump to x On the node of multiples ( If the node number is less than n Words , Can only jump to x On the integer multiple node of . When jumping to less than or equal to n The last x On the integer multiple node of , Just take the last step out n, And this step does not exceed p that will do ), meanwhile , Each jump cannot exceed p, And must eventually jump to greater than n On the node of : Ask how much gold coins you can collect . If you can't jump to greater than n On the node of , The output "Noob".
Solution
- State means :dp[i] It means to jump to the node i The most valuable gold coin that can be collected on the .
- State shift : 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)
Can be maintained with monotonic queues that can be transferred to state i Several states of . - Be careful : Finally, you must jump to greater than n Node , The last state should be
n + 1, Not the last x On the integer multiple node of (n / x * x + x). And the two states are not equivalent .
for example : n = 10,p = 5, x = 2.
When the last state isn + 1when (11), Can be dp[6],dp[8],dp[10] The biggest one in the transfer .
and , The last state isn / x * x + xwhen (12), Can be dp[8],dp[10] The biggest one in the transfer .
obviously , The two cases are not equivalent , Pay special attention to .
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);// Be careful : In this question n You can only jump within x Multiple ,n It can not be x Multiple , So it can't be written 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
Be careful : Read the questions carefully , The last state of this question is especially detailed .. It's also crucial
边栏推荐
- Resolved: JDBC connection to MySQL failed with an error:'The last packet sent successfully to the server was 0 milliseconds ago. '
- Unity's data persistence -- Jason
- Mazhiqiang: research progress and application of speech recognition technology -- RTC dev Meetup
- postgresql 捕获函数中的异常
- Mavros controls UAV to conduct binocular slam in gazebo environment
- 一文搞懂单片机驱动8080LCD
- Azure Kubernates Service 更新|提升开发体验和效率
- 音乐正版率关键数据缺失,网易云音乐IPO胜算几何?
- {dataSource-1} closing ... {dataSource-1} closed
- Canvas rotation drawing H5 animation JS effect
猜你喜欢
![[cloud native] what is micro service? How to build it? Teach you how to build the first micro service (framework)](/img/2c/50c692e090d64ab67f7501beb1d989.png)
[cloud native] what is micro service? How to build it? Teach you how to build the first micro service (framework)

UML series articles (28) architecture modeling - collaboration

/10个值得推荐的学习编程的网站 世界已经进入了互联网的时代。据最近发布的一篇《2016年互联网趋势》报告显示,中国已成为互联网市场的领导者,中国互联网用户的数量达到了6.68亿。可以预见,有

【安全科普】挖矿技术,从一个理工男的爱情故事讲起

TweenMax五彩小球弹跳动画

【ELT.ZIP】OpenHarmony啃论文俱乐部——电子设备软件更新压缩

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

How should Xiaobai start the Amazon self support evaluation?

js顶部图标菜单点击切换背景色js特效

WinDbg virtual machine dual machine debugging driver file debugging
随机推荐
If the source code of the home page module is not separated ----- > nanny level source code analysis (1)
ASLR
023 MySQL索引优化口诀-索引失效的常见情况
postgresql 函数的参数为自定义类型时传参格式
What has TCL done right to break through the technological strength of Chinese brand innovation?
GD32 can发送报no mailbox 故障
Lecturer paging query_ Instructor condition query with page
OpenGL第八章 材质material
Free flying animation of paper plane based on SVG
canvas绘图——如何把图形放置画布中心
【ELT.ZIP】OpenHarmony啃论文俱乐部——数据高通量无损压缩方案
Discussion on the concurrency security of a common set on players
canvas旋转绘图h5动画js特效
PostgreSQL source code learning (21) -- fault recovery ② - transaction log initialization
摘桃子(双指针)
UML系列文章(28)体系结构建模---协作
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
Canvas+svg line particle animation web page background
Iqoo 8 measured hands-on experience: return of the king, never high profile
OpenGL第十一章 多光源