Sugoroku(单调队列优化DP)

题目描述Takahashi is playing a board game called Sugoroku.On the board, there are N+1 squares numbered 0 to N. Takahashi starts at Square 0, and he has to stop exactly at Square N to win the game.The g...
摘要由CSDN通过智能技术生成

题目描述
Takahashi is playing a board game called Sugoroku.
On the board, there are N+1 squares numbered 0 to N. Takahashi starts at Square 0, and he has to stop exactly at Square N to win the game.
The game uses a roulette with the M numbers from 1 to M. In each turn, Takahashi spins the roulette. If the number x comes up when he is at Square s, he moves to Square s+x. If this makes him go beyond Square N, he loses the game.
Additionally, some of the squares are Game Over Squares. He also loses the game if he stops at one of those squares. You are given a string S of length N+1, representing which squares are Game Over Squares. For each i (0≤i≤N), Square i is a Game Over Square if S[i]=1 and not if S[i]=0.
Find the sequence of numbers coming up in the roulette in which Takahashi can win the game in the fewest number of turns possible. If there are multiple such sequences, find the lexicographically smallest such sequence. If Takahashi cannot win the game, print −1.

Constraints
·1≤N≤105
·1≤M≤105
·|S|=N+1
·S consists of 0 and 1.
·S[0]= 0
·S[N]= 0

输入
Input is given from Standard Input in the following format:

N M
S

输出
If Takahashi can win the game, print the lexicographically smallest sequence among the shortest sequences of numbers coming up in the roulette in which Takahashi can win the game, with spaces in between.
If Takahashi cannot win the game, print −1.

样例输入
【样例1】
9 3
0001000100
【样例2】
5 4
011110
【样例3】
6 6
0101010

样例输出
【样例1】
1 3 2 3
【样例2】
-1
【样例3】
6

提示
样例1解释
If the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9 via Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and this is the lexicographically smallest sequence in which he reaches Square 9 in four turns.

思路
由题意可直接想到dp的解法,由于N<105所以直接DP时间复杂度为O(n2),因此使用单调队列优化DP

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long <
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值