1722 D. Line

1772 D. Line

题目

传送门:Problem - D - Codeforces

There are nn 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][0,3,2,3,4], and the value is 0+3+2+3+4=120+3+2+3+4=12.

You are given the initial arrangement of people in the line. For each kk from 11 to nn, determine the maximum value of the line if you can change the direction of at most kk people.

Input

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the line.

The following line contains a string consisting of nn 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 nn over all test cases does not exceed 2⋅1052⋅105.

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++).

Output

For each test case, output nn space-separated non-negative integers — the maximum value of the line if you can change the direction of at most kk people for each kk from 11 to nn, inclusive.

Example

input

Copy

6
3
LLR
5
LRRLL
1
L
12
LRRRLLLRLLRL
10
LLLLLRRRRR
9
LRLRLRLRL

output

Copy

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 

Note

In the first test case:

  • k=1k=1: change the direction of 11 person to make the line RLR. The total value is 2+1+0=32+1+0=3.
  • k=2k=2: change the direction of 22 people to make the line RLL. The total value is 2+1+2=52+1+2=5.
  • k=3k=3: change the direction of 22 people to make the line RLL. The total value is 2+1+2=52+1+2=5. Note that you have to change the direction of at most kk people.

In the second test case, it is optimal to only change the direction of the first person for all kk from 11 to 55 (that is, make the line RRRLL).

题解

贪心;

算换边的收益,有收益就换,没收益就算了;

#include<bits/stdc++.h> 

using namespace std;

const int N  = 2E6+10;

int n , val[N]; 

long long ans = 0;

string s;

void solve()
{
	cin>>n>>s;
	ans = 0;
	for(int i = 0 ; i < n ; i++)
	{
		if(s[i] == 'L')
		{
			ans+= i;
			val[i] = n - 2 * i - 1;//往右边看的收益; 
		}
		else 
		{
			ans += n - i - 1;
			val[i] = 2 * i + 1 - n ;//往左边看的收益; 
			
		}
		
	}
	sort(val, val + n);
		for(int i = n - 1 ; i >= 0 ; i --)
			{
				if(val[i] > 0)ans+=val[i];
				cout<<ans<<" ";
				
			}
			cout<<endl;
} 

int main()
{
	int t ;
	cin>>t;
	while(t -- )
	{
		solve();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值