A1009 | No Time to Paint--Silver
来源USACO
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
Bessie has recently received a painting set, and she wants to paint the long
fence at one end of her pasture. The fence consists of $N$ consecutive 1-meter
segments ($1\le N\le 10^5$). Bessie has 26 different colors available, which
she labels with the letters 'A' through 'Z' in increasing order of darkness
('A' is a very light color, and 'Z' is very dark). She can therefore describe
the desired color she wants to paint each fence segment as a length-$N$ string
where each character is a letter.
Initially, all fence segments are uncolored. Bessie can color any contiguous
range of segments with a single color in a single brush stroke as long as she
never paints a lighter color over a darker color (she can only paint darker
colors over lighter colors).
For example, an initially uncolored segment of length four can be colored as
follows:
.... -> BBB. -> BBLL -> BQQL
Running short on time, Bessie thinks she may need to leave some consecutive
range of fence segments unpainted! Currently, she is considering $Q$ candidate
ranges ($1\le Q\le 10^5$), each described by by two integers $(a,b)$ with $1
\leq a \leq b \leq N$ giving the indices of endpoints of the range $a \ldots
b$ of segments to be left unpainted.
For each candidate range, what is the minimum number of strokes needed to
paint every fence segment outside those in the range with its desired color
while leaving all fence segments inside the range uncolored? Note that Bessie
does not actually do any painting during this process, so the answers for each
candidate range are independent.
fence at one end of her pasture. The fence consists of $N$ consecutive 1-meter
segments ($1\le N\le 10^5$). Bessie has 26 different colors available, which
she labels with the letters 'A' through 'Z' in increasing order of darkness
('A' is a very light color, and 'Z' is very dark). She can therefore describe
the desired color she wants to paint each fence segment as a length-$N$ string
where each character is a letter.
Initially, all fence segments are uncolored. Bessie can color any contiguous
range of segments with a single color in a single brush stroke as long as she
never paints a lighter color over a darker color (she can only paint darker
colors over lighter colors).
For example, an initially uncolored segment of length four can be colored as
follows:
.... -> BBB. -> BBLL -> BQQL
Running short on time, Bessie thinks she may need to leave some consecutive
range of fence segments unpainted! Currently, she is considering $Q$ candidate
ranges ($1\le Q\le 10^5$), each described by by two integers $(a,b)$ with $1
\leq a \leq b \leq N$ giving the indices of endpoints of the range $a \ldots
b$ of segments to be left unpainted.
For each candidate range, what is the minimum number of strokes needed to
paint every fence segment outside those in the range with its desired color
while leaving all fence segments inside the range uncolored? Note that Bessie
does not actually do any painting during this process, so the answers for each
candidate range are independent.
输入格式
The first line contains $N$ and $Q$.
The next line contains a string of length $N$ characters representing the
desired color for each fence segment.
The next $Q$ lines each contain two space-separated integers $a$ and $b$
representing a candidate range to possibly leave unpainted.
The next line contains a string of length $N$ characters representing the
desired color for each fence segment.
The next $Q$ lines each contain two space-separated integers $a$ and $b$
representing a candidate range to possibly leave unpainted.
输出格式
For each of the $Q$ candidates, output the answer on a new line.
输入输出样例
输入 #1
8 2 ABBAABCB 3 6 1 4
输出 #1
4 3
In this example, excluding the sub-range corresponding to the desired pattern
$\texttt{BAAB}$ requires four strokes to paint while excluding $\texttt{ABBA}$
requires only three.
.... -> AA.. -> ABBB -> ABCB
$\texttt{BAAB}$ requires four strokes to paint while excluding $\texttt{ABBA}$
requires only three.
.... -> AA.. -> ABBB -> ABCB
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted