A15307 | Line
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There are $n$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.
For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $[0, 3, 2, 3, 4]$ , and the value is $0+3+2+3+4=12$ .
You are given the initial arrangement of people in the line. For each $k$ from $1$ to $n$ , determine the maximum value of the line if you can change the direction of at most $k$ people.
For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $[0, 3, 2, 3, 4]$ , and the value is $0+3+2+3+4=12$ .
You are given the initial arrangement of people in the line. For each $k$ from $1$ to $n$ , determine the maximum value of the line if you can change the direction of at most $k$ people.
输入格式
The input consists of multiple test cases. The first line contains an integer $t$ ( $1 \leq t \leq 100$ ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer $n$ ( $1 \leq n \leq 2\cdot10^5$ ) — the length of the line.
The following line contains a string consisting of $n$ characters, each of which is either L or R, representing a person facing left or right, respectively — the description of the line.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$ .
Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
The first line of each test case contains an integer $n$ ( $1 \leq n \leq 2\cdot10^5$ ) — the length of the line.
The following line contains a string consisting of $n$ characters, each of which is either L or R, representing a person facing left or right, respectively — the description of the line.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$ .
Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
输出格式
For each test case, output $n$ space-separated non-negative integers — the maximum value of the line if you can change the direction of at most $k$ people for each $k$ from $1$ to $n$ , inclusive.
输入输出样例
输入 #1
6 3 LLR 5 LRRLL 1 L 12 LRRRLLLRLLRL 10 LLLLLRRRRR 9 LRLRLRLRL
输出 #1
3 5 5 16 16 16 16 16 0 86 95 98 101 102 102 102 102 102 102 102 102 29 38 45 52 57 62 65 68 69 70 44 50 54 56 56 56 56 56 56
In the first test case:
- $k=1$ : change the direction of $1$ person to make the line RLR. The total value is $2+1+0=3$ .
- $k=2$ : change the direction of $2$ people to make the line RLL. The total value is $2+1+2=5$ .
- $k=3$ : change the direction of $2$ people to make the line RLL. The total value is $2+1+2=5$ . Note that you have to change the direction of at most $k$ people.
In the second test case, it is optimal to only change the direction of the first person for all $k$ from $1$ to $5$ (that is, make the line RRRLL).
- $k=1$ : change the direction of $1$ person to make the line RLR. The total value is $2+1+0=3$ .
- $k=2$ : change the direction of $2$ people to make the line RLL. The total value is $2+1+2=5$ .
- $k=3$ : change the direction of $2$ people to make the line RLL. The total value is $2+1+2=5$ . Note that you have to change the direction of at most $k$ people.
In the second test case, it is optimal to only change the direction of the first person for all $k$ from $1$ to $5$ (that is, make the line RRRLL).
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted