exec_outcome
stringclasses 1
value | code_uid
stringlengths 32
32
| file_name
stringclasses 111
values | prob_desc_created_at
stringlengths 10
10
| prob_desc_description
stringlengths 63
3.8k
| prob_desc_memory_limit
stringclasses 18
values | source_code
stringlengths 117
65.5k
| lang_cluster
stringclasses 1
value | prob_desc_sample_inputs
stringlengths 2
802
| prob_desc_time_limit
stringclasses 27
values | prob_desc_sample_outputs
stringlengths 2
796
| prob_desc_notes
stringlengths 4
3k
⌀ | lang
stringclasses 5
values | prob_desc_input_from
stringclasses 3
values | tags
listlengths 0
11
| src_uid
stringlengths 32
32
| prob_desc_input_spec
stringlengths 28
2.37k
⌀ | difficulty
int64 -1
3.5k
⌀ | prob_desc_output_spec
stringlengths 17
1.47k
⌀ | prob_desc_output_to
stringclasses 3
values | hidden_unit_tests
stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED
|
d96cb5b6df166531bfd909d8c9ab13e4
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.Map.Entry;
public class Main {
// static int [] arr;
// static boolean prime[] = new boolean[1000];
// static int l;
// static String s; static StringBuilder sb;
// static HashSet<L> hs;
// static HashSet<Long> hs = new HashSet<Long>();
// static int ans;
// static boolean checked [];
//static final int mod = 1000000007;
// static int[][] dp;
// static int[] w ,v;
static int n,k;
// static int arr[][];
// static long ans;
static Scanner sc;
static PrintWriter out;
//static int n, m, w, t;
// static char [] a , b;
//static StringBuilder sb;
// static int ans;
static int arr[];
static long[] sum;
static long dp[][];
static long ans;
//static char[] []arr;
static ArrayList<Integer> v;
public static void main(String[] args) throws IOException, InterruptedException {
sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
char [] s = sc.next().toCharArray();
int i = 0 ; int j = n-1;
int k = 0;
long ans = 0;
for(int ii = 0 ; ii < n ; ii++) {
if(s[ii]== 'L')
ans+=ii;
else
ans+=n-1-ii;
}
//out.println(ans);
while(i<j) {
if(k==n)
break;
if(i<=n/2 && s[i] == 'L' && (n-1-i>i)) {
s[i] = 'R';
ans += n-1-i - i;
k++;
out.print(ans + " ");
}
if(k==n)
break;
if(j>=n/2 && s[j] == 'R' && j>(n-1-j)) {
s[j] = 'L';
int bef = n-1-j;
int af = j;
ans += j-(n-1-j);
k++;
out.print(ans + " ");
}
i++;
j--;
}
while (k<n) {
out.print(ans+" ");k++;
}
out.println();
}
out.close();
}
public static void solve() {
}
public static int bs(int target) {
int l = 0;
int r = v.size()-1;
while(l<=r) {
int m = (l+r)/2;
if(v.get(m) < target)
l = m+1;
else
r = m-1;
}
return l;
}
public static void swap(int i , int j ,int [] arr) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// public static int trace(int idx,int cost){
// if(cost> t || idx == m)
// return 0;
// int take = v[idx] + solve(idx+1,cost + 3*w*d[idx]);
// //int leave = solve(idx+1,t);
// if(dp[idx][cost] == take){
// sb.append(d[idx]+" "+v[idx]+"\n");
// return 1 + trace(idx+1,cost+3*w*d[idx]);
// }
//
// return trace(idx+1,cost);
// }
private static void reverse(long[] arr) {
// TODO Auto-generated method stub
}
// recursive implementation
static long arrayLCM(int[] arr, int idx) {
// lcm(a,b) = (a*b/gcd(a,b))
if (idx == arr.length - 1) {
return arr[idx];
}
long a = arr[idx];
long b = arrayLCM(arr, idx + 1);
return (a * b / gcd(a, b)); //
}
static int longestSubarrWthSumDivByK(int arr[], int n, int k) {
// unordered map 'um' implemented as
// hash table
HashMap<Integer, Integer> um = new HashMap<Integer, Integer>();
// 'mod_arr[i]' stores (sum[0..i] % k)
int mod_arr[] = new int[n];
int max_len = 0;
long curr_sum = 0;
// traverse arr[] and build up the
// array 'mod_arr[]'
for (int i = 0; i < n; i++) {
curr_sum += arr[i];
// as the sum can be negative,
// taking modulo twice
mod_arr[i] = (int) ((curr_sum % k) + k) % k;
// if true then sum(0..i) is
// divisible by k
if (mod_arr[i] == 0)
// update 'max'
max_len = i + 1;
// if value 'mod_arr[i]' not present in 'um'
// then store it in 'um' with index of its
// first occurrence
else if (um.containsKey(mod_arr[i]) == false)
um.put(mod_arr[i], i);
else
// if true, then update 'max'
if (max_len < (i - um.get(mod_arr[i])))
max_len = i - um.get(mod_arr[i]);
}
// return the required length of longest subarray
// with sum divisible by 'k'
return max_len;
}
static int longestSubArrayOfSumK(int[] arr, int n, int k) {
// HashMap to store (sum, index) tuples
HashMap<Integer, Integer> map = new HashMap<>();
int sum = 0, maxLen = 0;
// traverse the given array
for (int i = 0; i < n; i++) {
// accumulate sum
sum += arr[i];
// when subarray starts from index '0'
if (sum == k)
maxLen = i + 1;
// make an entry for 'sum' if it is
// not present in 'map'
if (!map.containsKey(sum)) {
map.put(sum, i);
}
// check if 'sum-k' is present in 'map'
// or not
if (map.containsKey(sum - k)) {
// update maxLength
if (maxLen < (i - map.get(sum - k)))
maxLen = i - map.get(sum - k);
}
}
return maxLen;
}
static boolean isPrime(long n) {
if (n == 2 || n == 3)
return true;
if (n <= 1 || n % 2 == 0 || n % 3 == 0)
return false;
// To check through all numbers of the form 6k ± 1
for (long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}
static long smallestDivisor(long n) {
// if divisible by 2
if (n % 2 == 0)
return 2;
// iterate from 3 to sqrt(n)
for (long i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return i;
}
return n;
}
static long nCr(int n, int r) {
return fact(n) / (fact(r) * fact(n - r));
}
static long fact(int n) {
long res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
public static boolean isSorted(int[] arr) {
for (int i = 0; i < arr.length - 1; i++)
if (arr[i] > arr[i + 1])
return false;
return true;
}
// static void findsubsequences(String s, String ans){
// if (s.length() == 0) {
// if(ans!="")
// if(ans.length()!=l)
// al.add(Long.parseLong(ans));
// return;
// }
//
// // We add adding 1st character in string
// findsubsequences(s.substring(1), ans + s.charAt(0));
//
// // Not adding first character of the string
// // because the concept of subsequence either
// // character will present or not
// findsubsequences(s.substring(1), ans);
//}
static void sieve(int n, boolean[] prime, List<Integer> al) {
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p]) {
al.add(p);
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
}
// Print all prime numbers
// for(int i = 2; i <= n; i++)
// {
// if(prime[i] == true)
// System.out.print(i + " ");
// }
public static void reverse(Object[] arr) {
int i = 0;
int j = arr.length - 1;
while (i < j) {
Object temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
}
class Pair implements Comparable<Pair> {
int a;
int b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Pair o) {
// TODO Auto-generated method stub
return this.a - o.a;
}
public String toString() {
return "( " + this.a + " , " + this.b + " )\n";
}
}
class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public int[][] nextInt2DArr(int l, int w) throws IOException {
int[][] arr = new int[l][w];
for (int i = 0; i < l; i++)
for (int j = 0; j < w; j++)
arr[i][j] = Integer.parseInt(next());
return arr;
}
public Scanner(String file) throws FileNotFoundException {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArr(int length) throws IOException {
int[] arr = new int[length];
for (int i = 0; i < length; i++)
arr[i] = Integer.parseInt(next());
return arr;
}
public long[] nextLongArr(int length) throws IOException {
long[] arr = new long[length];
for (int i = 0; i < length; i++)
arr[i] = Long.parseLong(next());
return arr;
}
public boolean ready() throws IOException {
return br.ready();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 8
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
76c1c83541cf9931dbe43f7ad3611f06
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class d {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int numC = sc.nextInt();
for (int c = 0; c < numC; c++) {
int n = sc.nextInt();
char[] arr = sc.next().toCharArray();
long curSum = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 'L')
curSum += i;
else
curSum += n - i - 1;
}
// flip people one by one if its beneficial to do so and print out the result.
ArrayList<Long> prints = new ArrayList<Long>();
for (int i = 0; i < n / 2; i++) {
// front
if (arr[i] == 'L') {
curSum -= i;
curSum += n - i - 1;
prints.add(curSum);
}
if (arr[n - i - 1] == 'R') {
curSum -= i;
curSum += n - i - 1;
prints.add(curSum);
}
}
for (int i = prints.size(); i < n; i++) {
prints.add(curSum);
}
for (int i = 0; i < prints.size(); i++) {
pw.print(prints.get(i) + (i < prints.size() - 1 ? " " : "\n"));
}
}
pw.close();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 8
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
5d4f61f5013c6527c1c6c0ac527d55c4
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class CodeForce {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int caseNum = scanner.nextInt();
while(caseNum > 0){
int length = scanner.nextInt();
scanner.nextLine();
// 对每个位置来说,有一个修改之后的变化,1-k就是说全变一遍,有可能溢出的!
String input = scanner.nextLine();
long[] origin = new long[length];
long[] change = new long[length];
long base = 0;
for(int i=0;i<length;i++){
char c = input.charAt(i);
if(c == 'L'){
origin[i] = i;
change[i] = length - i - 1 - i;
}else{
origin[i] = length - i - 1;
change[i] = i - (length - i - 1);
}
base += origin[i];
}
Arrays.sort(change);
StringBuilder sb = new StringBuilder();
long m = Math.max(change[length - 1], 0);
long value1 = base + m;
sb.append(value1);
for(int i=1; i<length;i++){
m = change[length-i-1] < 0 ? m : (m + change[length-i-1]);
long value = base + m;
sb.append(" ");
sb.append(value);
}
System.out.println(sb);
caseNum --;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 8
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
4a3ae07defea32262b7556e071a727aa
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main(String[] args){
MyScanner scanner = new MyScanner();
int numOfTests = scanner.nextInt();
for(int t = 1; t <= numOfTests; t++){
int n = scanner.nextInt();
String str = scanner.next();
char[] arr = str.toCharArray();
solve(n, arr);
}
out.close();
}
public static void solve(int n, char[] arr){
long sum = 0L;
PriorityQueue<long[]> pq = new PriorityQueue<>(new Comparator<long[]>() {
@Override
public int compare(long[] o1, long[] o2) {
return Long.compare(o2[2] - o2[1], o1[2] - o1[1]);
}
});
int curr;
for(int i = 0; i < n; i++){
if(arr[i] == 'L'){
curr = i;
}
else{
curr = n - i - 1;
}
sum += curr;
pq.offer(new long[]{i, curr, n - curr - 1});
}
long[] res = new long[n+1];
res[0] = sum;
for(int i = 1; i <= n && !pq.isEmpty(); i++){
long[] x = pq.poll();
long diff = x[2] - x[1];
if(diff > 0){
res[i] = res[i-1] + diff;
}
else{
res[i] = res[i-1];
}
}
for(int i = 1; i <= n; i++){
out.print(res[i]);
if(i < n){
out.print(" ");
}
}
out.print("\n");
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 8
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
49a0d7ea1d8b76ba64ff9a6288012980
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class D {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter output = new BufferedWriter(
new OutputStreamWriter(System.out));
int t = Integer.valueOf(in.readLine());
for(int c = 0 ; c < t ; ++c){
int n = Integer.parseInt(in.readLine());
PriorityQueue<Integer> values = new PriorityQueue<>((a,b ) -> b - a);
char[] people = in.readLine().toCharArray();
long total = 0l;
for(int i = 0 ; i < n ; ++i){
if(people[i] == 'L'){
total+= left(i);
values.add(right(i, n) - left(i));
}else{
total+= right(i, n);
values.add( left(i) - right(i, n));
}
}
for(int i = 0 ; i < n ; ++i){
if(values.peek() > 0){
total+= values.poll();
}
output.write(total+" ");
}
output.write("\n");
}
output.flush();
}
public static int right(int i, int n){
return n - i - 1;
}
public static int left(int i){
return i;
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 8
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
3488f1cefd7b1ba6bd055334e6ff296e
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Reader in = new Reader();
PrintWriter out = new PrintWriter(System.out);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int N = in.nextInt();
String s = in.next();
Queue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
long current = 0;
for (int i = 0; i < N; i++) {
if (s.charAt(i) == 'L') {
current += i;
pq.offer(N - 1 - i - i);
} else {
current += N - 1 - i;
pq.offer(i - (N - 1 - i));
}
}
for (int i = 0; i < N; i++) {
if (pq.peek() > 0) {
current += pq.poll();
}
out.print(current + (i == N - 1 ? "\n" : " "));
}
}
out.close();
}
static class Reader {
BufferedReader in;
StringTokenizer st;
public Reader() {
in = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
public String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
}
public static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i : arr) {
list.add(i);
}
Collections.sort(list);
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 8
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
ae6467cab28f4053db355248b1668e09
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.Scanner;
public class Task_1722D {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
solve(in);
}
}
private static void solve(Scanner in) {
int n = in.nextInt();
char[] s = in.next().toCharArray();
long sum = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == 'L') {
sum += i;
} else {
sum += n - i - 1;
}
}
int l = 0, r = n - 1;
int k = 1;
while (k <= n) {
while (l < n / 2 - 1 && s[l] == 'R') l++;
while (r > n / 2 && s[r] == 'L') r--;
long s1 = s[l] == 'L' ? sum - l + (n - l - 1): sum;
long s2 = s[r] == 'R' ? sum - (n - r - 1) + r: sum;
if (s1 > sum || s2 > sum) {
if (s1 >= s2) {
sum = s1;
s[l] = 'R';
l++;
} else {
sum = s2;
s[r] = 'L';
r--;
}
}
System.out.print(sum + " ");
k++;
}
System.out.println();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 8
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
fd734b27a87d661086e6fb7a586fdbd6
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t>0) {
int n=sc.nextInt();
String s=sc.next();
long val=0;
for(int i=0;i<n;i++) {
if(s.charAt(i)=='R') val+=n-i-1;
else val+=i;
}
int c=0;
long[] arr=new long[n];
for(int i=0;i<n;i++) {
int val1=0;
if(n%2==1 && i==(n-1)/2) continue;
if(i<n/2 && s.charAt(i)=='L') {
val1+=n-i-1-i;
}
else if(i>=n/2 && s.charAt(i)=='R') {
val1+=i-(n-i-1);
}
else continue;
arr[c]=val1;
c++;
}
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
for(int i=n-1;i>=0;i--) {
val+=arr[i];
System.out.print(val+" ");
}
System.out.println();
t--;
}
System.out.flush();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 8
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
9189e2be9b8a1afb13c458e734908b03
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.*;
/**
* @author Naitik
*
*/
public class Main
{
static FastReader sc=new FastReader();
static int dp[][];
// static boolean v[][][];
// static int mod=998244353;;
static int mod=1000000007;
static long max;
static int bit[];
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args)
{
// StringBuffer sb=new StringBuffer("");
int ttt=1;
ttt =i();
outer :while (ttt-- > 0)
{
int n=i();
char A[]=s().toCharArray();
long ans=0;
for(int i=0;i<n;i++) {
if(A[i]=='L') {
ans+=i;
}
else {
ans+=n-i-1;
}
}
int i=0;
int j=n-1;
int cnt=1;
long R[]=new long[n+1];
boolean f=false;
while(i<j) {
if(A[i]=='L') {
ans-=i;
ans+=n-i-1;
R[cnt++]=ans;
f=true;
}
if(A[j]=='R') {
ans-=(n-j-1);
ans+=j;
R[cnt++]=ans;
f=true;
}
i++;
j--;
}
if(!f) {
for(i=0;i<n;i++) {
out.print(ans+" ");
}
}
else {
for( i=1;i<=n;i++) {
out.print(max(R[i],R[i-1])+" ");
R[i]=max(R[i],R[i-1]);
}
}
out.println();
}
//System.out.println(sb.toString());
out.close();
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1
}
// public static void main(String args[]) {
// Scanner sc=new Scanner(System.in);
//
// }
static class Pair implements Comparable<Pair>
{
int x;
int y;
int z;
Pair(int x,int y){
this.x=x;
this.y=y;
// this.z=z;
}
@Override
public int compareTo(Pair o) {
if(this.x>o.x)
return 1;
else if(this.x<o.x)
return -1;
else {
if(this.y>o.y)
return 1;
else if(this.y<o.y)
return -1;
else
return 0;
}
}
public int hashCode()
{
final int temp = 14;
long ans = 1;
ans =x*31+y*13;
return (int)ans;
}
// @Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (this.getClass() != o.getClass()) {
return false;
}
Pair other = (Pair)o;
if (this.x != other.x || this.y!=other.y) {
return false;
}
return true;
}
//
/* FOR TREE MAP PAIR USE */
// public int compareTo(Pair o) {
// if (x > o.x) {
// return 1;
// }
// if (x < o.x) {
// return -1;
// }
// if (y > o.y) {
// return 1;
// }
// if (y < o.y) {
// return -1;
// }
// return 0;
// }
}
//static int find(int A[],int a) {
// if(A[a]==a)
// return a;
// return A[a]=find(A, A[a]);
//}
//FENWICK TREE
static void update(int i, int x){
for(; i < bit.length; i += (i&-i))
bit[i] += x;
}
static int sum(int i){
int ans = 0;
for(; i > 0; i -= (i&-i))
ans += bit[i];
return ans;
}
//END
static void add(HashMap<Long,Integer> map,long v) {
if(!map.containsKey(v)) {
map.put(v, 1);
}
else {
map.put(v, map.get(v)+1);
}
}
static void remove(HashMap<Long,Integer> map,long v) {
if(map.containsKey(v)) {
map.put(v, map.get(v)-1);
if(map.get(v)==0)
map.remove(v);
}
}
static void add(HashMap<Integer,Integer> map,int v) {
if(!map.containsKey(v)) {
map.put(v, 1);
}
else {
map.put(v, map.get(v)+1);
}
}
static void remove(HashMap<Integer,Integer> map,int v) {
if(map.containsKey(v)) {
map.put(v, map.get(v)-1);
if(map.get(v)==0)
map.remove(v);
}
}
static void add(TreeMap<Integer,Integer> map,int v) {
if(!map.containsKey(v)) {
map.put(v, 1);
}
else {
map.put(v, map.get(v)+1);
}
}
static void add(TreeMap<Long,Integer> map,long v) {
if(!map.containsKey(v)) {
map.put(v, 1);
}
else {
map.put(v, map.get(v)+1);
}
}
static void remove(TreeMap<Integer,Integer> map,int v) {
if(map.containsKey(v)) {
map.put(v, map.get(v)-1);
if(map.get(v)==0)
map.remove(v);
}
}
static void remove(TreeMap<Long,Integer> map,long v) {
if(map.containsKey(v)) {
map.put(v, map.get(v)-1);
if(map.get(v)==0)
map.remove(v);
}
}
static long modInverse(long n, int p)
{
return power(n, p - 2, p);
}
static int[] copy(int A[]) {
int B[]=new int[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static long[] copy(long A[]) {
long B[]=new long[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static void reverse(long A[]) {
int n=A.length;
long B[]=new long[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void reverse(int A[]) {
int n=A.length;
int B[]=new int[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void input(int A[],int B[]) {
for(int i=0;i<A.length;i++) {
A[i]=sc.nextInt();
B[i]=sc.nextInt();
}
}
static int[][] input(int n,int m){
int A[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
A[i][j]=i();
}
}
return A;
}
static char[][] charinput(int n,int m){
char A[][]=new char[n][m];
for(int i=0;i<n;i++) {
String s=s();
for(int j=0;j<m;j++) {
A[i][j]=s.charAt(j);
}
}
return A;
}
static int nextPowerOf2(int n)
{
if(n==0)
return 1;
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
static int highestPowerof2(int x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static long highestPowerof2(long x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static int max(int A[]) {
int max=Integer.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static int min(int A[]) {
int min=Integer.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long max(long A[]) {
long max=Long.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static long min(long A[]) {
long min=Long.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long [] prefix(long A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] prefix(int A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] suffix(long A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static long [] suffix(int A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static void fill(int dp[]) {
Arrays.fill(dp, -1);
}
static void fill(int dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(int dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(int dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static void fill(long dp[]) {
Arrays.fill(dp, -1);
}
static void fill(long dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(long dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(long dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long power(long x, long y, long p)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void print(int A[]) {
for(int i : A) {
out.print(i+" ");
}
out.println();
}
static void print(long A[]) {
for(long i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static void sort(int[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static String sort(String s) {
Character ch[]=new Character[s.length()];
for(int i=0;i<s.length();i++) {
ch[i]=s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st=new StringBuffer("");
for(int i=0;i<s.length();i++) {
st.append(ch[i]);
}
return st.toString();
}
static HashMap<Integer,Integer> hash(int A[]){
HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static HashMap<Long,Integer> hash(long A[]){
HashMap<Long,Integer> map=new HashMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Integer,Integer> tree(int A[]){
TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Long,Integer> tree(long A[]){
TreeMap<Long,Integer> map=new TreeMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 8
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
c6a6a512da47c0ed00131df1022abc73
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main{
static StreamTokenizer st ;
static BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
st = new StreamTokenizer(re);
int t = I();
while(t-->0) {
int n = I();
re.readLine();
char[] s = re.readLine().toCharArray();
long ans = 0;
long a[]= new long [n];
for (int i = 0 ; i < n ; i++) {
if(s[i] == 'L') a[i] = i;
else a[i] = n-1-i;
ans += a[i];
}
Arrays.sort(a);
for (int i = 0 ; i < n ; i++) {
if(n-1-2*a[i] > 0) ans += n-1-2*a[i];
pw.print(ans+" ");
}
pw.println();
pw.flush();
}
}
static int I() throws IOException {
st.nextToken();
return (int)st.nval;
}
static long L() throws IOException {
st.nextToken();
return (long)st.nval;
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
a1d0e9646e2c32cc9f61ec26273e64dc
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
//import static java.lang.Math.*;
import static java.lang.System.*;
import java.io.*;
import java.util.*;
public class Q4{
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int t = Integer.parseInt(br.readLine());
for(int z=0;z<t;z++){
int n = Integer.parseInt(br.readLine());
String pos = br.readLine();
int left = 0;
int right = n-1;
long sum = 0;
for(int i =0;i<pos.length();i++){
if(pos.charAt(i) == 'L'){
sum += i;
}else{
sum += n-i-1;
}
}
Queue<Long> q = new LinkedList<>();
while(left<right){
long l = -1;
long r = -1;
if(!(pos.charAt(left) == 'R'))
{
l = - left;
l = l + n-left-1;
}
if(!(pos.charAt(right) =='L'))
{
r = - (n-right-1);
r = r + right;
}
if(r != -1){
sum += r;
q.add(sum);
}
if(l != -1){
sum += l;
q.add(sum);
}
left++;
right--;
}
int k=0;
for(long it : q){
k++;
out.print(it + ((k==n)?"":" "));
}
for(int i=k;i<n;i++){
out.print(sum + ((k==n)?"":" "));
}
out.println();
}
}
}
// import java.util.*;
// import java.io.*;
// public class Main {
// static Scanner sc;
// static PrintWriter pw;
// public static void main(String[] args) throws IOException {
// sc = new Scanner(System.in);
// pw = new PrintWriter(System.out);
// int t = sc.nextInt();
// while (t-- > 0) {
// int n = sc.nextInt();
// String s = sc.next();
// PriorityQueue<Integer> pq = new PriorityQueue<>();
// for (int i = 0; i < n; i++) {
// if (s.charAt(i) == 'L')
// pq.add(i);
// else
// pq.add(n - i - 1);
// }
// long sum = 0;
// for (int x : pq)
// sum += x;
// while (!pq.isEmpty()) {
// int a = pq.poll();
// sum -= a;
// if (a < n / 2)
// a = n - 1 - a;
// sum += a;
// pw.print(sum +" ");
// }
// pw.println();
// }
// pw.close();
// }
// static class Scanner {
// StringTokenizer st;
// BufferedReader br;
// public Scanner(InputStream s) {
// br = new BufferedReader(new InputStreamReader(s));
// }
// public Scanner(FileReader r) {
// br = new BufferedReader(r);
// }
// public String readAllLines(BufferedReader reader) throws IOException {
// StringBuilder content = new StringBuilder();
// String line;
// while ((line = reader.readLine()) != null) {
// content.append(line);
// content.append(System.lineSeparator());
// }
// return content.toString();
// }
// public String next() throws IOException {
// while (st == null || !st.hasMoreTokens())
// st = new StringTokenizer(br.readLine());
// return st.nextToken();
// }
// public int nextInt() throws IOException {
// return Integer.parseInt(next());
// }
// public long nextLong() throws IOException {
// return Long.parseLong(next());
// }
// public String nextLine() throws IOException {
// return br.readLine();
// }
// public double nextDouble() throws IOException {
// String x = next();
// StringBuilder sb = new StringBuilder("0");
// double res = 0, f = 1;
// boolean dec = false, neg = false;
// int start = 0;
// if (x.charAt(0) == '-') {
// neg = true;
// start++;
// }
// for (int i = start; i < x.length(); i++)
// if (x.charAt(i) == '.') {
// res = Long.parseLong(sb.toString());
// sb = new StringBuilder("0");
// dec = true;
// } else {
// sb.append(x.charAt(i));
// if (dec)
// f *= 10;
// }
// res += Long.parseLong(sb.toString()) / f;
// return res * (neg ? -1 : 1);
// }
// public long[] nextlongArray(int n) throws IOException {
// long[] a = new long[n];
// for (int i = 0; i < n; i++)
// a[i] = nextLong();
// return a;
// }
// public Long[] nextLongArray(int n) throws IOException {
// Long[] a = new Long[n];
// for (int i = 0; i < n; i++)
// a[i] = nextLong();
// return a;
// }
// public int[] nextIntArray(int n) throws IOException {
// int[] a = new int[n];
// for (int i = 0; i < n; i++)
// a[i] = nextInt();
// return a;
// }
// public Integer[] nextIntegerArray(int n) throws IOException {
// Integer[] a = new Integer[n];
// for (int i = 0; i < n; i++)
// a[i] = nextInt();
// return a;
// }
// public boolean ready() throws IOException {
// return br.ready();
// }
// }
// }
// import java.util.*;
// public class Q4{
// public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
// int t = sc.nextInt();
// while (t-- > 0) {
// int n = sc.nextInt();
// sc.nextLine();
// String s = sc.nextLine();
// char[] arr = s.toCharArray();
// Long[] num = new Long[n];
// for (int i = 0; i < n; i++) {
// if (arr[i] == 'L')
// num[i] = Long.valueOf(i);
// else
// num[i] = Long.valueOf(n - 1 - i);
// }
// Arrays.sort(num);
// Long max = 0L;
// for (int i = 0; i < n; i++)
// max += num[i];
// int i;
// for (i = 0; i < n; i++) {
// if (num[i] < n / 2)
// max += (n - 1 - 2 * num[i]);
// System.out.print(max + " ");
// }
// System.out.println();
// }
// sc.close();
// }
// }
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
79a9cbe0def2072b93bac91da6852c93
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.util.Objects;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
static class Pair implements Comparable<Pair> {
final BigInteger value;
final int index;
public Pair(BigInteger value, int index) {
this.value = value;
this.index = index;
}
@Override
public int compareTo(Pair o) {
return o.value.compareTo(value);
}
@Override
public boolean equals(Object obj) {
return obj instanceof Pair && compareTo((Pair) obj) == 0;
}
@Override
public int hashCode() {
return Objects.hash(value, index);
}
}
static class Task {
public void solve(InputReader in, PrintWriter out) {
int T = in.nextInt();
for (int tt = 0; tt < T; tt++) {
var n = in.nextInt();
var s = in.next();
long ans = 0;
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L') {
ans += i;
} else {
ans += n - i - 1;
}
}
int l = 0, r = n - 1;
int cnt = 0;
while (l <= r) {
int i = (l <= n - r - 1) ? l++ : r--;
if (s.charAt(i) == 'L' && i < n / 2) {
ans += (n - i - 1) - i;
cnt++;
out.print(ans + " ");
}
if (s.charAt(i) == 'R' && i >= n / 2) {
ans += i - (n - i - 1);
cnt++;
out.print(ans + " ");
}
}
for (int i = cnt; i < n; i++) {
out.print(ans + " ");
}
out.println();
}
}
}
static class InputReader {
public static final int BUFFER_SIZE = 32768;
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), BUFFER_SIZE);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
3ac73dfc4c2ef7ff29e4206b8bcb0756
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.util.Objects;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(in, out);
out.close();
}
static class Pair implements Comparable<Pair> {
final BigInteger value;
final int index;
public Pair(BigInteger value, int index) {
this.value = value;
this.index = index;
}
@Override
public int compareTo(Pair o) {
return o.value.compareTo(value);
}
@Override
public boolean equals(Object obj) {
return obj instanceof Pair && compareTo((Pair) obj) == 0;
}
@Override
public int hashCode() {
return Objects.hash(value, index);
}
}
static class TaskA {
public void solve(InputReader in, PrintWriter out) {
int t = in.nextInt();
for (int tt = 0; tt < t; tt++) {
int n = in.nextInt();
String s = in.next();
// StringBuffer ss = new StringBuffer();
// for (int i = 0; i < 100000; i++) {
// ss.append('L');
// }
// for (int i = 0; i < 100000; i++) {
// ss.append('L');
// }
// String s = ss.toString();
long ans = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'R') {
ans += s.length() - i - 1;
} else {
ans += i;
}
}
//out.print(ans + " | ");
int l = 0, r = s.length() - 1;
int cnt = 0;
while (true) {
while (l < s.length() / 2 && s.charAt(l) == 'R') {
l++;
}
while (r >= s.length() / 2 && s.charAt(r) == 'L') {
r--;
}
if (l < s.length() / 2 && s.charAt(l) == 'L' && l <= s.length() - r - 1) {
ans -= l;
ans += s.length() - l - 1;
out.print(ans + " ");
cnt++;
l++;
}
if (r >= s.length() / 2 && s.charAt(r) == 'R' && l >= s.length() - r - 1) {
ans -= s.length() - r - 1;
ans += r;
out.print(ans + " ");
cnt++;
r--;
}
if (l >= s.length() / 2 && r < s.length() / 2) {
break;
}
}
for (int i = cnt; i < n; i++) {
out.print(ans + " ");
}
out.println();
}
}
}
static class InputReader {
public static final int BUFFER_SIZE = 32768;
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), BUFFER_SIZE);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
29882a64fefab1bef9db2d6c3ea6de73
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
public class Main {
static long ans;
public static void main(String arg[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
while(T --> 0) {
int N = Integer.parseInt(br.readLine());
char[] strarr = br.readLine().toCharArray();
ans = 0;
long[] v = new long[N];
for(int i = 0; i < N; i++) {
if(strarr[i] == 'L') {
v[i] = (N-1-i) - i;
ans += i;
}
else {
v[i] = i-(N-1-i);
ans += N-i-1;
}
}
Arrays.sort(v);
for(int i = N-1; i >= 0; i--) {
if(v[i] > 0) {
ans += v[i];
}
sb.append(ans).append(" ");
}
sb.append('\n');
}
System.out.println(sb);
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
79a28f8c43a782a66e3ab60d6a20bf31
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
public class Main {
static long ans;
public static void main(String arg[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
while(T --> 0) {
int N = Integer.parseInt(br.readLine());
char[] strarr = br.readLine().toCharArray();
ans = 0;
for(int i = 0; i < N; i++) {
if(strarr[i] == 'L') {
ans += i;
}
else {
ans += N-i-1;
}
}
int l = 0;
int r = N-1;
int mid = (l + r) >>> 1;
for(int i = 0; i < N; i++) {
if(l > mid || r <= mid) {
while(l <= mid && strarr[l] == 'R') {
l++;
}
while(r > mid && strarr[r] == 'L') {
r--;
}
if(l <= mid && strarr[l] == 'L') {
ans -= l;
ans += N-l-1;
l++;
}
else if(r > mid && strarr[r] == 'R') {
ans -= N-r-1;
ans += r;
r--;
}
}
else {
while(l <= mid && strarr[l] == 'R' ) {
l++;
}
while(r > mid && strarr[r] == 'L') {
r--;
}
if(l <= mid && l <= N-r-1) {
if(strarr[l] == 'L') {
ans -= l;
ans += N-l-1;
l++;
}
else if(r > mid && strarr[r] == 'R') {
ans -= N-r-1;
ans += r;
r--;
}
}
else if(r > mid) {
if(strarr[r] == 'R') {
ans -= N-r-1;
ans += r;
r--;
}
else if(l <= mid) {
ans -= l;
ans += N-l-1;
l++;
}
}
}
sb.append(ans+" ");
}
sb.append('\n');
}
System.out.println(sb);
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
fc4a253cf9946ee9522fa629359085b9
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
// 17-05 //
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class E {
public static void main(String[] args) {
Scanner sc = new Scanner();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
char s[] = sc.nextLine().toCharArray();
if (n == 1) {
System.out.println(0);
continue;
}
long ans = 0L;
for (int i = 0; i < n; i++) {
if (s[i] == 'L') {
ans += i;
} else {
ans += n - i - 1;
}
}
int l = 0;
int r = n - 1;
long res[] = new long[n];
int j = 0;
while (l != r + 1 && l < n && r >= 0 && j < n) {
if (s[l] != 'R' && s[r] != 'L') {
if (l > n - r - 1) {
ans += (long) (r - (n - r - 1));
res[j++] = ans;
r--;
} else {
ans += (long) (n - l - 1 - l);
res[j++] = ans;
l++;
}
} else if (s[l] != 'R') {
ans += (long) (n - l - 1 - l);
res[j++] = ans;
l++;
r--;
} else if (s[r] != 'L') {
ans += (long) (r - (n - r - 1));
res[j++] = ans;
r--;
l++;
} else {
l++;
r--;
}
}
if (res[0] == 0) {
res[0] = ans;
}
for (int i = 1; i < n; i++) {
if (res[i] < res[i - 1]) {
res[i] = res[i - 1];
}
}
for (int i = 0; i < n; i++) {
System.out.print(res[i] + " ");
}
System.out.println();
}
}
static class pp implements Comparable<pp> {
int a;
int b;
pp(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(pp o) {
return this.a - o.a;
}
}
static class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String str = "";
String nextLine() {
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static void sort(int[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n), temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static final int M = 1_000_000_007;
static final int inf = Integer.MAX_VALUE;
static final int ninf = inf + 1;
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
469bbf2b9453022329a17d439a9c9388
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.*;
import javax.lang.model.util.ElementScanner6;
import javax.swing.plaf.multi.MultiButtonUI;
public class codeforces {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
String joess = "";
Map<String, Integer> joes= new HashMap<>();
String[] values = line.split(" ");
int j = Integer.parseInt(values[0]);
while(j>0)
{
line = br.readLine();
values = line.split(" ");
int jg = Integer.parseInt(values[0]);
line = br.readLine();
long count = 0;
String pattern = line;
String backwords = "";
for(int i =0; i<pattern.length(); i++)
{
if(pattern.charAt(i)=='R')
{
count+=pattern.length()-i-1;
}
if(pattern.charAt(i)=='L')
{
count+=i;
}
}
ArrayList<Long> joss = new ArrayList<>();
for(int i = 0; i<pattern.length()/2; i++)
{
if(pattern.charAt(i)=='L')
{
count-=i;
count+=pattern.length()-i-1;
joss.add(count);
}
if(pattern.charAt(pattern.length()-i-1)=='R')
{
count-=i;
count+=pattern.length()-i-1;
joss.add(count);
}
}
for(int m = joss.size(); m<pattern.length();m++)
{
joss.add(count);
}
for(int k = 0; k<joss.size(); k++)
{
System.out.print(joss.get(k) + " ");
}
System.out.println();
j--;
}
}
}
/*int[] bruh = new int[j*2];
int i = 0;
while(j>0)
{
line = br.readLine();
values = line.split(" ");
joes.put(Integer.parseInt(values[0]),Integer.parseInt(values[2]));
joes.put(Integer.parseInt(values[1]),-Integer.parseInt(values[2]));
bruh[i] = Integer.parseInt(values[0]);
i++;
bruh[i] = Integer.parseInt(values[1]);
i++;
j--;
}
Arrays.sort(bruh);
int sum = 0;
int max = 0;
for(int k = 0; k<bruh.length; k++)
{
sum+=joes.get(bruh[k]);
max = Math.max(sum, max);
}
System.out.println(max); */
/*BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
Map<Integer, Integer> joes = new HashMap<>();
String[] values = line.split(" ");
int j = Integer.parseInt(values[0]);
int m = 3;
int[] bruh = new int[j];
line = br.readLine();
values= line.split(" ");
int[] finale = new int[j];
for(int i = 0; i<j; i++)
{
bruh[i] = Integer.parseInt(values[i]);
}
line = br.readLine();
values= line.split(" ");
for(int k = 0; k<j; k++)
{
finale[k] = Integer.parseInt(values[k]);
}
while(m>0)
{
int[] man = new int[j];
for(int z = 0; z<bruh.length; z++)
{
man[z] = finale[bruh[z]-1];
}
finale = man;
m--;
}
for(int y = 0; y<finale.length; y++)
{
System.out.println(finale[y]);
} */
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
1191fa75ce6c8c4d0f60b2abbadf15da
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.StringTokenizer;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.util.*;
public class CodeForces_817_D {
public static void main(String[] args)throws Exception{
FastScanner in = new FastScanner();OutputStream out = new BufferedOutputStream ( System.out );
int t=in.nextInt();
while (t-->0) {
int n=in.nextInt();char[] s=in.nextLine().toCharArray();
long ans=0;
//BigInteger ans=BigInteger.ZERO;
ArrayList<Integer>a=new ArrayList<>();
for(int i=0;i<n;i++){
int org,chg;
if(s[i]=='L'){
org=i;chg=n-i-1;
if(org<chg)
a.add(chg-org);
//ans=ans.add(BigInteger.valueOf(org));
ans+=org;
}
else{
org=n-i-1;chg=i;
if(org<chg)
a.add(chg-org);
//ans=ans.add(BigInteger.valueOf(org));
ans+=org;
}
}
a.sort(Collections.reverseOrder());int ind=0;
for(int i=0;i<n;i++){
if(ind<a.size()){
//ans=ans.add(BigInteger.valueOf(a.get(0)));
ans+=a.get(ind);ind++;
//a.remove(0);
}
out.write((ans+" ").getBytes());
}
out.write("\n".getBytes());
}
out.flush();
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
5fe9a6b55682dac3f6c2e98c922f4863
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.StringTokenizer;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.util.*;
public class CodeForces_817 {
public static void main(String[] args)throws Exception{
FastScanner in = new FastScanner();OutputStream out = new BufferedOutputStream ( System.out );
int t=in.nextInt();
while (t-->0) {
int n=in.nextInt();char[] s=in.nextLine().toCharArray();
long ans=0;
//BigInteger ans=BigInteger.ZERO;
ArrayList<Integer>a=new ArrayList<>();
for(int i=0;i<n;i++){
int org,chg;
if(s[i]=='L'){
org=i;chg=n-i-1;
if(org<chg)
a.add(chg-org);
//ans=ans.add(BigInteger.valueOf(org));
ans+=org;
}
else{
org=n-i-1;chg=i;
if(org<chg)
a.add(chg-org);
//ans=ans.add(BigInteger.valueOf(org));
ans+=org;
}
}
a.sort(Collections.reverseOrder());int ind=0;
for(int i=0;i<n;i++){
if(ind<a.size()){
//ans=ans.add(BigInteger.valueOf(a.get(0)));
ans+=a.get(ind);ind++;
//a.remove(0);
}
out.write((ans+" ").getBytes());
}
out.write("\n".getBytes());
}
out.flush();
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
622d09e511adf98ab74543d6d3439a51
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testCase = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
while(testCase-->0){
int length = Integer.parseInt(br.readLine());
int[] leftArray = new int[length];
int[] rightArray = new int[length];
String s = br.readLine();
long score=0;
for(int i=0; i<length; i++){
if(s.charAt(i)=='L') {
leftArray[i]=1;
score += i;
}
else {
rightArray[i]=1;
score += length-i-1;
}
}
int startI=0;
for(int k=0; k<length; k++){
boolean tf = false;
for(int i=startI; i<length/2; i++){
if(leftArray[i]==1){
leftArray[i]=0;
tf=true;
score += length-2*i-1;
sb.append(score).append(' ');
startI=i;
break;
}
if(rightArray[length-1-i]==1){
rightArray[length-1-i]='L';
tf=true;
score += length-2*i-1;
sb.append(score).append(' ');
startI=i;
break;
}
}
if(!tf){
for(int i=k; i<length; i++){
sb.append(score).append(' ');
}
break;
}
}
sb.append('\n');
}
System.out.println(sb);
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
e7d3197bb9ce1500387fb0e3c7c7826a
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class CodeForces31_8 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
String s = in.next();
int start = 0;
int end = s.length() - 1;
long count = 0;
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == 'L') count += j;
else count += s.length() - j - 1;
}
int mid = start + (end - start) / 2;
int k = s.length();
while (k > 0 && (start <= mid || end > mid)) {
if (start < s.length() - end - 1){
if (start <=mid && s.charAt(start)=='L') {
count = count - start + s.length() - start - 1;
k--;
System.out.print(count + " ");
}
start++;
}
else {
if (end > mid && s.charAt(end) == 'R') {
count = count - s.length() + end + 1 + end;
k--;
System.out.print(count+" ");
}
end--;
}
}
while (k > 0) {
System.out.print(count+" ");
k--;
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
e6142615ca10610d8013eb868f635e7a
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
public class Main {
static Reader r = new Reader();
static BufferedWriter o = new BufferedWriter(new OutputStreamWriter(System.out));
public static int firstL(char[] arr) {
for(int i = 0; i<arr.length; i++) {
if(arr[i]=='L') {
return i;
}
}
return -1;
}
public static int lastR(char[] arr) {
for(int i = arr.length-1; i>=0; i--) {
if(arr[i]=='R') {
return i;
}
}
return -1;
}
public static void main(String[] args) throws IOException {
int t = r.readI();
outer: for(int z = 1; z<=t; z++){
int n = r.readI();
char[] arr = r.readS().toCharArray();
long val = 0;
for(int i = 0; i<n; i++) {
if(arr[i] == 'L') {
val+=i;
}
else {
val+=n-i-1;
}
}
int L = firstL(arr);
int R = lastR(arr);
int k = 1;
if(n%2==0) {
for(k = 1; k<=n; k++) {
// int L = firstL(arr);
// int R = lastR(arr);
boolean flag = false;
if(L==-1) {
arr[R] = 'L';
val = val - (n-R-1) + R;
L = R;
R--;
o.write(val + " ");
}
else if(R==-1) {
arr[L] = 'R';
R = L;
val = val - L + n-L-1;
L++;
o.write(val + " ");
}
else if(L>=n/2 && R<n/2) {
o.write(val + " ");
flag = true;
break;
}
else {
if(L>n-R-1) {
arr[R] = 'L';
val = val - (n-R-1) + R;
L = Math.min(R, L);
boolean c = false;
for(int j = R-1; j>=0; j--) {
if(arr[j]=='R') {
R = j;
c = true;
break;
}
}
if(!c) {
R = -1;
}
}
else {
arr[L] = 'R';
val = val - L + n-L-1;
R = Math.max(L,R);
boolean c = false;
for(int j = L+1; j<n; j++) {
if(arr[j]=='L') {
L = j;
c = true;
break;
}
}
if(!c) {
L = -1;
}
}
o.write(val + " ");
}
}
}
else {
for(k = 1; k<=n; k++) {
// int L = firstL(arr);
// int R = lastR(arr);
boolean flag = false;
if(L==-1) {
arr[R] = 'L';
L = R;
val = val - (n-R-1) + R;
R--;
o.write(val + " ");
}
else if(R==-1) {
arr[L] = 'R';
R = L;
val = val - L + n-L-1;
L++;
o.write(val + " ");
}
else if(L>=n/2 && R<=n/2) {
o.write(val + " ");
flag = true;
break;
}
else {
if(L>n-R-1) {
arr[R] = 'L';
val = val - (n-R-1) + R;
L = Math.min(R, L);
boolean c = false;
for(int j = R-1; j>=0; j--) {
if(arr[j]=='R') {
R = j;
c = true;
break;
}
}
if(!c) {
R=-1;
}
}
else {
arr[L] = 'R';
val = val - L + n-L-1;
R = Math.max(L,R);
boolean c = false;
for(int j = L+1; j<n; j++) {
if(arr[j]=='L') {
L = j;
c = true;
break;
}
}
if(!c) {
L=-1;
}
}
o.write(val + " ");
}
}
}
for(int i = k+1; i<=n; i++) {
o.write(val + " ");
}
o.write("\n");
o.flush();
}
}
}
class Reader {
BufferedReader r;
Reader(){
r = new BufferedReader(new InputStreamReader(System.in));
}
public int[] readArrI() throws IOException {
String[] s = r.readLine().split(" ");
int n = s.length;
int[] a = new int[n];
for(int j=0; j<n;j++) {
a[j] = Integer.parseInt(s[j]);
}
return a;
}
public long[] readArrL() throws IOException {
String[] s = r.readLine().split(" ");
int n = s.length;
long[] a = new long[n];
for(int j=0; j<n;j++) {
a[j] = Long.parseLong(s[j]);
}
return a;
}
public String[] readArrS() throws IOException {
String[] s = r.readLine().split(" ");
return s;
}
public int readI() throws IOException {
String[] s = r.readLine().split(" ");
int n = Integer.parseInt(s[0]);
return n;
}
public long readL() throws IOException {
String[] s = r.readLine().split(" ");
long n = Long.parseLong(s[0]);
return n;
}
public String readS() throws IOException {
String s = r.readLine();
return s;
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
60b8fd0151cf6ba991cc4350e9f6e418
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class New {
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int testCases = Integer.parseInt(br.readLine());
while(testCases-->0){
int n = Integer.parseInt(br.readLine());
String s = br.readLine();
Long total = 0L;
for(int i=0;i<n;i++) {
if(s.charAt(i)=='L') total+=(i);
else total+=(n-1-i);
}
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for(int i=0;i<n;i++) {
int opt1=i;
int opt2=n-1-i;
if(s.charAt(i)=='L') {
if(opt2>opt1) {
pq.add(opt2-opt1);
}
} else {
if(opt1>opt2) {
pq.add(opt1-opt2);
}
}
}
for(int i=1;i<=n;i++) {
if(!pq.isEmpty()) total+=pq.poll();
out.print(total+" ");
}
out.println();
}
out.close();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
3e4270872d46d9ea916998fa9dd28ec7
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long mod = (int)1e9+7;
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
int t=sc.nextInt();
// int t=1;
while(t-->0)
{
int n = sc.nextInt();
char arr[] = sc.next().toCharArray();
int diff[] = new int[n];
int maxi[] = new int[n];
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
diff[i] = (arr[i] == 'L') ? i : n - i - 1;
maxi[i] = Math.max(i , n - i - 1);
list.add(maxi[i] - diff[i]);
}
Collections.sort(list , Collections.reverseOrder());
long sum = 0 ;
for(int x:diff)
{
sum += x;
}
for(Integer x:list)
{
sum += x;
out.print(sum+" ");
}
out.println();
}
out.flush();
}
static long cal(char[] arr , int n)
{
long sum = 0;
for(int i=0;i<n;i++)
{
if(arr[i] == 'L')
{
sum += i;
}
else
{
sum += (n - i - 1);
}
}
return sum;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
static class FenwickTree
{
//Binary Indexed Tree
//1 indexed
public int[] tree;
public int size;
public FenwickTree(int size)
{
this.size = size;
tree = new int[size+5];
}
public void add(int i, int v)
{
while(i <= size)
{
tree[i] += v;
i += i&-i;
}
}
public int find(int i)
{
int res = 0;
while(i >= 1)
{
res += tree[i];
i -= i&-i;
}
return res;
}
public int find(int l, int r)
{
return find(r)-find(l-1);
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
private static long mergeAndCount(int[] arr, int l,
int m, int r)
{
// Left subarray
int[] left = Arrays.copyOfRange(arr, l, m + 1);
// Right subarray
int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l;long swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swaps += (m + 1) - (l + i);
}
}
while (i < left.length)
arr[k++] = left[i++];
while (j < right.length)
arr[k++] = right[j++];
return swaps;
}
// Merge sort function
private static long mergeSortAndCount(int[] arr, int l,
int r)
{
// Keeps track of the inversion count at a
// particular node of the recursion tree
long count = 0;
if (l < r) {
int m = (l + r) / 2;
// Total inversion count = left subarray count
// + right subarray count + merge count
// Left subarray count
count += mergeSortAndCount(arr, l, m);
// Right subarray count
count += mergeSortAndCount(arr, m + 1, r);
// Merge count
count += mergeAndCount(arr, l, m, r);
}
return count;
}
static void my_sort(long[] arr)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list);
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static void reverse_sorted(int[] arr)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list , Collections.reverseOrder());
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static int LowerBound(int a[], int x) { // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value
int l=-1,r=list.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(list.get(m)<=x) l=m;
else r=m;
}
return l+1;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static class Queue_Pair implements Comparable<Queue_Pair> {
int first , second;
public Queue_Pair(int first, int second) {
this.first=first;
this.second=second;
}
public int compareTo(Queue_Pair o) {
return Integer.compare(o.first, first);
}
}
static void leftRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
static void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n-1] = temp;
}
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters to compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
static boolean palindrome_array(char arr[], int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
return false;
else
return true;
}
static boolean allElementsEqual(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
static boolean allElementsDistinct(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
public static void reverse_Long(long[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
long temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
static boolean isSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static boolean isReverseSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] < a[i + 1]) {
return false;
}
}
return true;
}
static int[] rearrangeEvenAndOdd(int arr[], int n)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
int[] array = list.stream().mapToInt(i->i).toArray();
return array;
}
static long[] rearrangeEvenAndOddLong(long arr[], int n)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
long[] array = list.stream().mapToLong(i->i).toArray();
return array;
}
static boolean isPrime(long n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (long i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static long getSum(long n)
{
long sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcdLong(long a, long b)
{
if (b == 0)
return a;
return gcdLong(b, a % b);
}
static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static int countDigit(int n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
}
class Pair
{
int first;
char second;
Pair(int first , char second)
{
this.first = first;
this.second = second;
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
57cbe542f19949b9c34a0262c82b88f4
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import jdk.jfr.Unsigned;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;
public class D1722 {
public static BigInteger findSum(String d){
BigInteger sum = BigInteger.valueOf(0);
for(int i=0;i<d.length();i++){
if(d.charAt(i)=='L'){
sum = sum.add(BigInteger.valueOf(i));
}else{
sum = sum.add(BigInteger.valueOf(d.length()-i-1));
}
}
return sum;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0){
int n = s.nextInt();
String d = s.next();
ArrayList<BigInteger> sum = new ArrayList<>();
int k =0;
sum.add(findSum(d));
for(int i=0,j=d.length()-1;i<j&&k<=n;){
if (d.charAt(i)=='L'){
sum.add(sum.get(k).add(BigInteger.valueOf(d.length()-i-1-i)));
k++;i++;
}else{
i++;
}
if(d.charAt(j)=='R'){
sum.add(sum.get(k).add(BigInteger.valueOf(j-d.length()+j+1)));
k++;j--;
}else{
j--;
}
}
while(k<n){
sum.add(sum.get(k));
k++;
}
for(int i =1;i<sum.size();i++){
System.out.print(sum.get(i)+" ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
5c7f50b766b4dd3e0122bf31b6e8ebc7
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class M {
private M(){
}
public static void main(String[] args) {
FastScanner x = new FastScanner();
int a = x.nextInt();
while (a-- > 0){
Long n = x.nextLong();
String s = x.nextLine();
Long[] arr = new Long[Math.toIntExact(n)];
Long sum = 0L;
if(n % 2 == 0){
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L'){
if (i < n / 2){
arr[i] = n - i - 1 - i;
}
sum += i;
}else{
if (i >= n / 2){
arr[i] = i - (n - i - 1);
}
sum += n - i - 1;
}
// if (n % 2 == 1 && n != 1){
// arr[(int) (n / 2)] = n / 2;
// }
if (arr[i] == null){
arr[i] = 0L;
}}
}else{
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L'){
if (i < n / 2){
arr[i] = n - i - 1 - i;
}
sum += i;
}else{
if (i > n / 2){
arr[i] = i - (n - i - 1);
}
sum += n - i - 1;
}
// if (n % 2 == 1 && n != 1){
// arr[(int) (n / 2)] = n / 2;
// }
if (arr[i] == null){
arr[i] = 0L;
}}
}
Arrays.sort(arr);
for (Long i = n - 1; i >= 0; i--) {
sum += arr[Math.toIntExact(i)];
System.out.print(sum + " ");
}
System.out.println();
}
}
public static boolean ch(Integer[] arr,int a){
Integer br[] = new Integer[arr.length];
int ind = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != a){
br[ind++] = arr[i];
}
}
return checkpal(br);
}public static boolean checkpal(Integer[] br){
int l = 0;
int r = br.length - 1;
while (br[r] == 0){
r--;
}
while (l < r){
if (br[l] != br[r]){
return false;
}l++;
r--;
}return true;
}
public static int intfromstring(String s){
String[] nums = {"zero","one","two","three","four","five","six","seven","eight","nine","ten"};
for (int i = 0; i < nums.length; i++) {
if (s.equals(nums[i])){
return i;
}
}return -1;
}
public static long NOK(long a,long b){
return lcm(a,b);
}static long gcd(long a,long b){
return b == 0 ? a : gcd(b,a % b);
}static long lcm(long a,long b){
return a / gcd(a,b) * b;
}
public static long sum(long a){
return a * (a - 1) / 2;
}
static boolean odd(int e){
return e % 2 == 1;
}static boolean even(int e){
return e % 2 == 0;
}
static boolean isPrime(long n)
{
if (n <= 1){
return false;}
if (n <= 3){
return true;}
if (n % 2 == 0 || n % 3 == 0){
return false;}
for (int i = 5; (long) i * i <= n; i = i + 6){
if (n % i == 0 || n % (i + 2) == 0){
return false;}}
return true;
}static String arrtostring(long res[]){
return Arrays.toString(res).replace(",","").replace("[","").replace("]","");
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextLine() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextLine());
}
long nextLong() {
return Long.parseLong(nextLine());
}
double nextDouble() {
return Double.parseDouble(nextLine());
}
Integer[] readArray(int n) {
Integer[] a=new Integer[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
Long[] readArrayLong(int n) {
Long[] a=new Long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
1486c9320dd72b4cae3d7616dd724efa
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static final PrintWriter out =new PrintWriter(System.out);
static final FastReader sc = new FastReader();
//I invented a new word!Plagiarism!
//Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them
//What do Alexander the Great and Winnie the Pooh have in common? Same middle name.
//I finally decided to sell my vacuum cleaner. All it was doing was gathering dust!
//ArrayList<Integer> a=new ArrayList <Integer>();
//PriorityQueue<Integer> pq=new PriorityQueue<>();
//char[] a = s.toCharArray();
// char s[]=sc.next().toCharArray();
public static boolean sorted(int a[])
{
int n=a.length,i;
int b[]=new int[n];
for(i=0;i<n;i++)
b[i]=a[i];
Arrays.sort(b);
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
return false;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
int tes=sc.nextInt();
label:while(tes-->0)
{
int n=sc.nextInt();
char s[]=sc.next().toCharArray();
ArrayList<Long> arr=new ArrayList<>();
ArrayList<Long> ans=new ArrayList<>();
long Ans=0;
long score=0; int i;
for(i=0;i<n;i++)
{
if(s[i]=='L'){
score+=i;
arr.add((long)i);
}
else{
score+=(n-i-1);
arr.add((long)n-i-1);
}
}
Collections.sort(arr);
Ans=score;
for(i=0;i<n;i++)
{
long a=arr.get(i);
if(a<=n-a-1)
Ans+=Math.max(a,n-a-1)-Math.min(a,n-a-1);
ans.add(Ans);
}
for(long it:ans)
System.out.print(it+" ");
System.out.println();
}
}
public static int first(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x)
return mid;
else if (x > arr.get(mid))
return first(arr, (mid + 1), high, x, n);
else
return first(arr, low, (mid - 1), x, n);
}
return -1;
}
public static int last(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x)
return mid;
else if (x < arr.get(mid))
return last(arr, low, (mid - 1), x, n);
else
return last(arr, (mid + 1), high, x, n);
}
return -1;
}
public static int lis(int[] arr) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>=x) {
al.add(arr[i]);
}else {
int v = upper_bound(al, 0, al.size(), arr[i]);
al.set(v, arr[i]);
}
}
return al.size();
}
public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get((int)mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long mod=1000000007;
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
25f42934fbf8d530d40f4808d252a2b6
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static final PrintWriter out =new PrintWriter(System.out);
static final FastReader sc = new FastReader();
//I invented a new word!Plagiarism!
//Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them
//What do Alexander the Great and Winnie the Pooh have in common? Same middle name.
//I finally decided to sell my vacuum cleaner. All it was doing was gathering dust!
//ArrayList<Integer> a=new ArrayList <Integer>();
//PriorityQueue<Integer> pq=new PriorityQueue<>();
//char[] a = s.toCharArray();
// char s[]=sc.next().toCharArray();
public static boolean sorted(int a[])
{
int n=a.length,i;
int b[]=new int[n];
for(i=0;i<n;i++)
b[i]=a[i];
Arrays.sort(b);
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
return false;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
int tes=sc.nextInt();
label:while(tes-->0)
{
int n=sc.nextInt();
char s[]=sc.next().toCharArray();
ArrayList<Long> arr=new ArrayList<>();
ArrayList<Long> ans=new ArrayList<>();
long Ans=0;
long score=0; int i;
for(i=0;i<n;i++)
{
if(s[i]=='L'){
score+=i;
arr.add((long)i);
}
else{
score+=(n-i-1);
arr.add((long)n-i-1);
}
}
Collections.sort(arr);
Ans=score;
for(i=0;i<n;i++)
{
long a=arr.get(i);
if(a<=n-a-1)
Ans+=Math.max(a,n-a-1)-Math.min(a,n-a-1);
ans.add(Ans);
}
for(long it:ans)
System.out.print(it+" ");
System.out.println();
}
}
public static int first(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x)
return mid;
else if (x > arr.get(mid))
return first(arr, (mid + 1), high, x, n);
else
return first(arr, low, (mid - 1), x, n);
}
return -1;
}
public static int last(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x)
return mid;
else if (x < arr.get(mid))
return last(arr, low, (mid - 1), x, n);
else
return last(arr, (mid + 1), high, x, n);
}
return -1;
}
public static int lis(int[] arr) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>=x) {
al.add(arr[i]);
}else {
int v = upper_bound(al, 0, al.size(), arr[i]);
al.set(v, arr[i]);
}
}
return al.size();
}
public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get((int)mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long mod=1000000007;
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
b30a368eed1814a166a0aa687eeb3014
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static final PrintWriter out =new PrintWriter(System.out);
static final FastReader sc = new FastReader();
//I invented a new word!Plagiarism!
//Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them
//What do Alexander the Great and Winnie the Pooh have in common? Same middle name.
//I finally decided to sell my vacuum cleaner. All it was doing was gathering dust!
//ArrayList<Integer> a=new ArrayList <Integer>();
//PriorityQueue<Integer> pq=new PriorityQueue<>();
//char[] a = s.toCharArray();
// char s[]=sc.next().toCharArray();
public static boolean sorted(int a[])
{
int n=a.length,i;
int b[]=new int[n];
for(i=0;i<n;i++)
b[i]=a[i];
Arrays.sort(b);
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
return false;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
int tes=sc.nextInt();
label:while(tes-->0)
{
int n=sc.nextInt();
char s[]=sc.next().toCharArray();
ArrayList<Long> arr=new ArrayList<>();
int i; long score=0;
for(i=0;i<n;i++)
{
if(s[i]=='L')
score+=i;
else
score+=(n-1-i);
}
int l=0,r=n-1,res=0;
while(l<=r)
{
if(s[l]=='L'){
score+=n-l-1;
score-=l;
arr.add(score);
res++;
}
if(s[r]=='R'){
score+=r;
score-=n-r-1;
arr.add(score);
res++;
}
l++;
r--;
}
for(i=res;i<n;i++)
arr.add(score);
for(long it:arr)
System.out.print(it+" ");
System.out.println();
}
}
public static int first(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x)
return mid;
else if (x > arr.get(mid))
return first(arr, (mid + 1), high, x, n);
else
return first(arr, low, (mid - 1), x, n);
}
return -1;
}
public static int last(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x)
return mid;
else if (x < arr.get(mid))
return last(arr, low, (mid - 1), x, n);
else
return last(arr, (mid + 1), high, x, n);
}
return -1;
}
public static int lis(int[] arr) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>=x) {
al.add(arr[i]);
}else {
int v = upper_bound(al, 0, al.size(), arr[i]);
al.set(v, arr[i]);
}
}
return al.size();
}
public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get((int)mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long mod=1000000007;
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
15f4325040c5ecf62fa779ead53af5f9
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
final static int mod = 998244353;
// final static int mod = (int)1e9+7;
static boolean[] prime = new boolean[1];
static int[][] dir1 = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
static int[][] dir2 = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
final static int inf = 0x3f3f3f3f;
static void yes() throws Exception {
print("YES");
}
static void no() throws Exception {
print("NO");
}
static {
for (int i = 2; i < prime.length; i++)
prime[i] = true;
for (int i = 2; i < prime.length; i++) {
if (prime[i]) {
for (int k = 2; i * k < prime.length; k++) {
prime[i * k] = false;
}
}
}
}
static class DSU {
int[] fa;
DSU(int n) {
fa = new int[n];
for (int i = 0; i < n; i++)
fa[i] = i;
}
int find(int t) {
if (t != fa[t])
fa[t] = find(fa[t]);
return fa[t];
}
void join(int x, int y) {
x = find(x);
y = find(y);
fa[x] = y;
}
}
static List<Integer>[] lists;
static void init(int n) {
lists = new List[n];
for(int i = 0; i< n;i++){
lists[i] = new ArrayList<>();
}
}
static class LCA{
int[] dep;
int[][] fa;
int[] log;
boolean[] v;
public LCA(int n){
dep = new int[n+5];
log = new int[n+5];
fa = new int[n+5][31];
v = new boolean[n+5];
for (int i = 2; i <= n; ++i) {
log[i] = log[i/2] + 1;
}
dfs(1,0);
}
private void dfs(int cur, int pre){
if(v[cur]) return;
v[cur] = true;
dep[cur] = dep[pre]+1;
fa[cur][0] = pre;
for (int i = 1; i <= log[dep[cur]]; ++i) {
fa[cur][i] = fa[fa[cur][i - 1]][i - 1];
}
for(int i : lists[cur]){
dfs(i,cur);
}
}
private int lca(int a, int b){
if(dep[a] > dep[b]){
int t = a;
a = b;
b = t;
}
while (dep[a] != dep[b]){
b = fa[b][log[dep[b] - dep[a]]];
}
if(a == b) return a;
for (int k = log[dep[a]]; k >= 0; k--) {
if (fa[a][k] != fa[b][k]) {
a = fa[a][k]; b = fa[b][k];
}
}
return fa[a][0];
}
}
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static int get() throws Exception {
String ss = bf.readLine();
if (ss.contains(" "))
ss = ss.substring(0, ss.indexOf(" "));
return Integer.parseInt(ss);
}
static long getx() throws Exception {
String ss = bf.readLine();
if (ss.contains(" "))
ss = ss.substring(0, ss.indexOf(" "));
return Long.parseLong(ss);
}
static int[] getint() throws Exception {
String[] s = bf.readLine().split(" ");
int[] a = new int[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(s[i]);
}
return a;
}
static long[] getlong() throws Exception {
String[] s = bf.readLine().split(" ");
long[] a = new long[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Long.parseLong(s[i]);
}
return a;
}
static String getstr() throws Exception {
return bf.readLine();
}
static void println() throws Exception {
bw.write("\n");
}
static void print(int a) throws Exception {
bw.write(a + "\n");
}
static void print(long a) throws Exception {
bw.write(a + "\n");
}
static void print(String a) throws Exception {
bw.write(a + "\n");
}
static void print(int[] a) throws Exception {
for (int i : a) {
bw.write(i + " ");
}
println();
}
static void print(long[] a) throws Exception {
for (long i : a) {
bw.write(i + " ");
}
println();
}
static void print(int[][] a) throws Exception {
for (int i[] : a)
print(i);
}
static void print(long[][] a) throws Exception {
for (long[] i : a)
print(i);
}
static void print(char[] a) throws Exception {
for (char i : a) {
bw.write(i + "");
}
println();
}
static long pow(long a, long b) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans *= a;
}
a *= a;
b >>= 1;
}
return ans;
}
static int powmod(long a, long b, int mod) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return (int) ans;
}
static void sort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static void sort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static void resort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[n - 1 - i];
}
static void resort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[n - 1 - i];
}
static int max(int a, int b) {
return Math.max(a, b);
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long max(long a, long b) {
return Math.max(a, b);
}
static long min(long a, long b) {
return Math.min(a, b);
}
static int max(int[] a) {
int max = a[0];
for (int i : a)
max = max(max, i);
return max;
}
static int min(int[] a) {
int min = a[0];
for (int i : a)
min = min(min, i);
return min;
}
static long max(long[] a) {
long max = a[0];
for (long i : a)
max = max(max, i);
return max;
}
static long min(long[] a) {
long min = a[0];
for (long i : a)
min = min(min, i);
return min;
}
static int abs(int a) {
return Math.abs(a);
}
static long abs(long a) {
return Math.abs(a);
}
static int[] getarr(List<Integer> list) {
int n = list.size();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = list.get(i);
return a;
}
public static void main(String[] args) throws Exception {
int T = 1;
T = get();
while (T-- > 0) {
int n = get();
String s = getstr();
long ans[] = new long[n];
if(n == 1){
ans[0] = 0;
print(ans);
continue;
}
long x = 0;
for(int i = 0;i < n;i++){
if(s.charAt(i) == 'L'){
x += i;
}else{
x += n-1-i;
}
}
int l = 0, r = n-1;
int k = 0;
boolean ok = true;
ans[k] = x;
while(l <= r){
if(ok){
if(s.charAt(l) == 'L') {
ans[k++] += (n-1-l-l);
}
l++;
ok = !ok;
}else{
if(s.charAt(r) == 'R') {
ans[k++] += (r-(n-1-r));
}
r--;
ok = !ok;
}
if(k < n && k > 0) ans[k] = ans[k-1];
}
for(int i = k;i < n;i++) {
if(i == 0) continue;
ans[i] = ans[i-1];
}
print(ans);
}
bw.flush();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
6e9a1594f44224d9a51093aa45e7db04
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class question {
public static void main(String[] args) throws java.lang.Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0)
{
int n = Integer.parseInt(br.readLine());
String st = br.readLine();
long sum=0;
for(int i=0;i<n;i++)
{
if(st.charAt(i)=='L')
sum = sum+i;
else
sum = sum+n-i-1;
}
//System.out.println(sum);
ArrayList<Integer> left = new ArrayList<>();
ArrayList<Integer> right = new ArrayList<>();
for(int i=0;i<n/2;i++)
{
if(st.charAt(i)=='L')
{
left.add(i);
}
}
if(n%2==0)
{
for(int i=n-1;i>=n/2;i--)
{
if(st.charAt(i)=='R')
{
right.add(i);
}
}
}
else
{
for(int i=n-1;i>n/2;i--)
{
if(st.charAt(i)=='R')
{
right.add(i);
}
}
}
// System.out.println(left);
// System.out.println(right);
int i=0;
int j=0;
int count=0;
while(i<left.size()&&j<right.size())
{
int l = left.get(i);
int r = right.get(j);
if(n-l-1>r)
{
sum = sum+n-l-1-l;
i++;
}
else
{
sum = sum+r-(n-r-1);
j++;
}
count++;
System.out.print(sum+" ");
}
while(i<left.size())
{
int l = left.get(i);
sum = sum+n-l-1-l;
i++;
count++;
System.out.print(sum+" ");
}
while(j<right.size())
{
int r = right.get(j);
sum = sum+r-(n-r-1);
j++;
count++;
System.out.print(sum+" ");
}
for(int k=0;k<n-count;k++)
System.out.print(sum+" ");
System.out.println();
}
}}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
a9c0327a225e2012b20631a03a8fdf08
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
public class Solution
{
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
int ll = 1;
// int t=1;
while(t-->0){
int n = sc.nextInt();
String s = sc.next();
long l = 0;
long r = n-1;
long[] res = new long[n];
long sum = 0;
for(int i=0;i<n;i++) sum+= (long)(s.charAt(i)=='L' ? i : n-1-i);
int k=0;
while(l<r){
if(s.charAt((int)l)=='L'){
sum-=l;
sum+=(n-1-l);
res[k++]=sum;
}
if(s.charAt((int)r)=='R'){
sum-=(n-1-r);
sum+=(r);
res[k++]=sum;
}
l++;
r--;
}
if(res[0]==0) res[0]=sum;
for(int i=1;i<n;i++) if(res[i]==0) res[i]=res[i-1];
for(int i=0;i<n;i++) System.out.print(res[i]+" ");
System.out.println();
}
// out.close();
}
}
class Pair{
String name;
int val;
Pair(String name, int val){
this.name=name;
this.val=val;
}
}
// class Pair{
// int a;
// int b;
// Pair(int a, int b){
// this.a=a;
// this.b=b;
// }
// int getA(){return a;}
// int getB(){return b;}
// }
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {br = new BufferedReader(new InputStreamReader(System.in));}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();}
int[] nextIntArray(int n)
{
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=Integer.parseInt(next());
}
return arr;
}
long[] nextLongArray(int n)
{
long arr[]=new long[n];
for(int i=0;i<n;i++)
{
arr[i]=Long.parseLong(next());
}
return arr;
}
Integer[] nextIntegerArray(int n)
{
Integer arr[]=new Integer[n];
for(Integer i=0;i<n;i++)
{
arr[i]=Integer.parseInt(next());
}
return arr;
}
int nextInt() { return Integer.parseInt(next());}
long nextLong() { return Long.parseLong(next());}
double nextDouble() { return Double.parseDouble(next());}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
03ab2aa4ce92227a212dcb6a1b6979fb
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public final class CF_1722D_v2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
for(int testcase = 0; testcase < t; testcase++){
int n = in.nextInt();
char[] line = in.next().toCharArray();
int[] count = new int[n];
long sum = 0;
for(int i = 0; i<n; i++){
count[line[i] == 'L' ? i : n-i-1]++;
sum += line[i] == 'L' ? i : n-i-1;
}
// System.out.println(Arrays.toString(count));
int c = 0;
for(int k = 1; k<=n; k++){
while(count[c] == 0) c++;
if(n-c-1 > c){
count[c]--;
sum += n-c-1;
sum -= c;
}
out.print(sum);
out.print(" ");
}
out.println();
}
in.close();
out.close();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
c7a1ea085bc5674b6a06c6d5e3ca6f12
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class look {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int zx = Integer.parseInt(br.readLine());
for(int alpha = 0; alpha < zx; alpha++){
int n = Integer.parseInt(br.readLine());
String in = br.readLine();
char[] input = in.toCharArray();
int len = input.length-1;
int counter = 0;
Long total = 0L;
for(int i = 0; i <= len; i++){
if(input[i] == 'L'){
total += i;
}else{
total += len-i;
}
}
int changed = 0;
StringBuilder out = new StringBuilder();
while(changed <= n && counter < (len+1)/2){
if(input[counter] == 'L'){
input[counter] = 'R';
total -=counter;
total += len-counter;
out.append(total);
out.append(" ");
changed++;
}
if(changed < n){
if(input[len-counter] == 'R'){
input[len-counter] = 'L';
total -= counter;
total += len-counter;
out.append(total);
out.append(" ");
changed++;
}
}
counter++;
}
if(changed < n){
int t = n-changed;
for(int m = 0; m < t; m++){
out.append(total);
out.append(" ");
}
out.delete(out.length()-1, out.length()-1);
}
System.out.println(out.toString());
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
dc39d325931770074a47a3bfe20f6d79
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println("Error");
}
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t>0){
int n = sc.nextInt();
String s = sc.nextLine();
long[] arr = new long[n+1];
ArrayList<Integer> left = new ArrayList<>();
ArrayList<Integer> right = new ArrayList<>();
int xx = (n+1)/2;
if(n%2==1){
xx--;
}
for(int i=0;i<xx;i++){
if(s.charAt(i)=='L'){
left.add(i);
}
}
for(int i=0;i<xx;i++){
if(s.charAt(n-i-1)=='R'){
right.add(i);
}
}
long sum = 0;
for(int i=0;i<n;i++){
if(s.charAt(i)=='R'){
sum+=(n-i-1);
}
else{
sum+=i;
}
}
int x1=0,x2=0;
for(int i=1;i<=n;i++){
if(x1<left.size() && x2<right.size()){
if(left.get(x1)<right.get(x2)){
sum-=left.get(x1);
sum+=(n-left.get(x1)-1);
x1++;
}
else{
sum-=right.get(x2);
sum+=(n-right.get(x2)-1);
x2++;
}
}
else if(x1<left.size()){
sum-=left.get(x1);
sum+=(n-left.get(x1)-1);
x1++;
}
else if(x2<right.size()){
sum-=right.get(x2);
sum+=(n-right.get(x2)-1);
x2++;
}
arr[i] = sum;
}
for(int i=1;i<=n;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
t--;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
9657e3301b982c6efe690b86cfb5b78e
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class James
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-- > 0)
{
long n = scanner.nextLong();
StringBuilder stb = new StringBuilder(scanner.next());
long lcnt = 0, rcnt = 0, cnt;
TreeSet<Long> lset = new TreeSet<Long>();
TreeSet<Long> rset = new TreeSet<Long>();
for (int i = 0; i < n; i++)
{
if(stb.charAt(i) == 'L')
{
lcnt += i;
lset.add((long)i);
}
else
{
rcnt += n - i - 1;
rset.add((long)i);
}
}
cnt = lcnt + rcnt;
long max = cnt;
n--;
for (long i = 0; i < n + 1; i++)
{
//int lio = stb.lastIndexOf("R");
//int io = Math.abs(stb.indexOf("L"));
long lio = -1;
long io = 1;
if (!rset.isEmpty())
lio = rset.last();
if (!lset.isEmpty())
io = lset.first();
if(lio >= n - io)
{
//stb.replace(lio, lio + 1, "L");
lset.add(lio);
rset.remove(lio);
cnt += lio - (n - lio);
if (cnt > max)
max = cnt;
System.out.printf("%d ", max);
}
else
{
//stb.replace(io, io + 1, "R");
rset.add(io);
lset.remove(io);
cnt += (n - io) - io;
if (cnt > max)
max = cnt;
System.out.printf("%d ", cnt);
}
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
ed2a7210b8608131f0e10aa8c5603334
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Scanner;
public class Line {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for (int i = 0; i < t; i++) {
int n=in.nextInt();
String s=in.next();
long s1=0;
boolean f1=true,f2=true;
char a[]=s.toCharArray();
ArrayList<Integer> a1= new ArrayList<>();
ArrayList<Integer> a2= new ArrayList<>();
for (int j = 0; j < n; j++) {
if(a[j]=='L'){
s1+=j;
}
if(a[j]=='R')s1+=n-j-1;
}
for (int j = 0; j < n/2; j++) {
if(a[j]=='L')a1.add(j);
}
for (int j = n-1; j >=n/2 ; j--) {
if(a[j]=='R'){
a2.add(j);
}
}
int c1=0,c2=0;
for (int j = 0; j < n; j++) {
long ca1=0,ca2=0;
if(c1<a1.size())
ca1=a1.get(c1);
else f1=false;
if(c2<a2.size())ca2=a2.get(c2);
else f2=false;
if(f1&&f2){
if(ca1<n-ca2-1){
s1+=n-ca1-1-ca1;
c1++;
}
else{
s1+=ca2-n+ca2+1;
c2++;
}
}
else if(f1){
s1+=n-ca1-1-ca1;
c1++;
}
else if(f2){
s1+=ca2-n+ca2+1;
c2++;
}
f1=true;
f2=true;
System.out.print(s1+" ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
20767b64a1c519561073e6bf2ed72532
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class codeforces {
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
public static int cnt;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static int reverseBits(int n)
{
int rev = 0;
// traversing bits of 'n'
// from the right
while (n > 0)
{
// bitwise left shift
// 'rev' by 1
rev <<= 1;
// if current bit is '1'
if ((int)(n & 1) == 1)
rev ^= 1;
// bitwise right shift
//'n' by 1
n >>= 1;
}
// required number
return rev;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
StringBuilder sb = new StringBuilder();
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
String s=sc.next();
long s1=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='L')
{
s1=s1+i;
}
else
{
s1=s1+n-1-i;
}
}
ArrayList<Long> x1=new ArrayList<Long>();
for(int i=0;i<n/2;i++)
{
if(s.charAt(i)=='L')
{
x1.add((long) (n-1-i-i));
}
}
for(int i=n/2;i<n;i++)
{
if(s.charAt(i)=='R')
{
x1.add((long) (i-(n-1-i)));
}
}
ArrayList<Long> x=new ArrayList<Long>();
Collections.sort(x1);
Collections.sort(x1,Collections.reverseOrder());
for(int i=0;i<x1.size();i++)
{
s1=s1+x1.get(i);
x.add(s1);
}
while(x.size()<n)
{
x.add(s1);
}
if(x.isEmpty())
{
out.print(s1);
}
else
{
for(int i=0;i<x.size();i++)
{
out.print(x.get(i)+" ");
}
}
out.println();
}
out.close();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
aa46076fb4f2a3ab5a5739eea5bf7295
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.util.stream.Collectors;
public class Main {
InputStream is;
PrintWriter out;
String INPUT = "";
void run() throws Exception {
is = System.in; out = new PrintWriter(System.out);
solve(); out.flush(); out.close();
}
public static void main(String[] args) throws Exception {
new Main().run();
}
public byte[] inbuf = new byte[1 << 16];
public int lenbuf = 0, ptrbuf = 0;
public int readByte() {
if (lenbuf == -1) {
throw new InputMismatchException();
}
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
public boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
public double nd() {
return Double.parseDouble(ns());
}
public char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private int ni() {
return (int) nl();
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
class Pair {
int first;
int second;
Pair(int a, int b) {
first = a;
second = b;
}
}
long[] nal(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
void solve() {
int test_case = 1;
test_case = ni();
while (test_case-- > 0) {
go();
}
}
// WRITE CODE FROM HERE :-
void go() {
long n = ni(); String s = "";
do{s=ns();}while(s.length()!=n); long tot = 0;
ArrayList<Long> v = new ArrayList<>();
for (long i = 0; i < n; i++) {
if (s.charAt((int) i) == 'L') {
v.add((n - 1 - i) - i);
tot += i;
}
else {
v.add(i - (n - 1 - i));
tot += (n - 1 - i);
}
}
Collections.sort(v, Collections.reverseOrder());
for (long i = 0; i < n; i++) {
if (v.get((int) i) > 0) {
tot += v.get((int) i);
}
out.print(tot + " ");
}
out.println();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
f4c7a18fffb0a4433ba5bc055e2e9986
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.util.stream.Collectors;
public class Main {
InputStream is;
PrintWriter out;
String INPUT = "";
void run() throws Exception {
is = System.in; out = new PrintWriter(System.out);
solve(); out.flush(); out.close();
}
public static void main(String[] args) throws Exception {
new Main().run();
}
public byte[] inbuf = new byte[1 << 16];
public int lenbuf = 0, ptrbuf = 0;
public int readByte() {
if (lenbuf == -1) {
throw new InputMismatchException();
}
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
public boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
public double nd() {
return Double.parseDouble(ns());
}
public char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private int ni() {
return (int) nl();
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
class Pair {
int first;
int second;
Pair(int a, int b) {
first = a;
second = b;
}
}
long[] nal(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
void solve() {
int test_case = 1;
test_case = ni();
while (test_case-- > 0) {
go();
}
}
// WRITE CODE FROM HERE :-
void go() {
long n = ni(); String s = "";
do{s=ns();}while(s.length()!=n); long tot = 0;
ArrayList<Long> v = new ArrayList<>();
for (long i = 0; i < n; i++) {
if (s.charAt((int) i) == 'L') {
v.add((n - 1 - i) - i);
tot += i;
}
else {
v.add(i - (n - 1 - i));
tot += n - 1 - i;
}
}
Collections.sort(v, Collections.reverseOrder());
for (long i = 0; i < n; i++) {
if (v.get((int) i) > 0) {
tot += v.get((int) i);
}
out.print(tot + " ");
}
out.println();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
16f9e4f0093db94eb9fd698770e79755
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.Scanner;
import java.util.Scanner;
import java.util.*;
public class MyPractise1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
ArrayList<Long> list=new ArrayList<>();
int n=sc.nextInt();
String str=sc.next();
long sum=0;
for(int i=0;i<n;i++){
if(str.charAt(i)=='L')
sum=sum+i;
else
sum=sum+n-i-1;
}
for(int i=0;i<n;i++){
long curr=0;
if(str.charAt(i)=='L')
curr=i;
else
curr=n-i-1;
long max=Math.max(i,n-i-1);
if(curr!=max){
list.add(max - curr);
}
}
Collections.sort(list,Collections.reverseOrder());
for(int i=1;i<list.size();i++)
list.set(i,list.get(i-1)+list.get(i));
for(int i=0;i<n;i++){
long data=sum;
if(list.size()>0&&i<list.size())
data=data+list.get(i);
else if(list.size()>0)
data=data+list.get(list.size()-1);
if(i==n-1)
System.out.print(data);
else
System.out.print(data+" ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
56bad486a2a2657fea35e0c02a8f1604
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class S {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
sc.nextLine();
String s = sc.nextLine();
char[] arr = s.toCharArray();
Pair[] num = new Pair[n];
long max = 0l;
for (int i = 0; i < n; i++) {
if (arr[i] == 'L')
num[i] = new Pair(Long.valueOf(i),i);
else
num[i] = new Pair(Long.valueOf(n - 1 - i),i);
max += num[i].sum;
}
Arrays.sort(num,(A,B)->(int)(A.sum-B.sum));
for (int i = 0; i < n; i++) {
if (num[i].sum < n / 2){
max += (Math.max(n-1-num[i].index ,num[i].index) - num[i].sum);
}
System.out.print(max + " ");
}
System.out.println();
}
}
}
class Pair{
long sum;
int index;
Pair(long s,int in){
sum = s;
index = in;
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
1ff2994b8c384973ebf6af49c2d3cbe9
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
public class weird_algrithm {
static BufferedWriter output = new BufferedWriter(
new OutputStreamWriter(System.out));
static BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
static int mod = 1000000007;
static String toReturn = "";
static int steps = Integer.MAX_VALUE;
static int maxlen = 1000005;
/*MATHEMATICS FUNCTIONS START HERE
MATHS
MATHS
MATHS
MATHS*/
static long gcd(long a, long b) {
if(b == 0) return a;
else return gcd(b, a % b);
}
static long powerMod(long x, long y, int mod) {
if(y == 0) return 1;
long temp = powerMod(x, y / 2, mod);
temp = ((temp % mod) * (temp % mod)) % mod;
if(y % 2 == 0) return temp;
else return ((x % mod) * (temp % mod)) % mod;
}
static long modInverse(long n, int p) {
return powerMod(n, p - 2, p);
}
static long nCr(int n, int r, int mod, long [] fact, long [] ifact) {
return ((fact[n] % mod) * ((ifact[r] * ifact[n - r]) % mod)) % mod;
}
static boolean isPrime(long a) {
if(a == 1) return false;
else if(a == 2 || a == 3 || a== 5) return true;
else if(a % 2 == 0 || a % 3 == 0) return false;
for(int i = 5; i * i <= a; i = i + 6) {
if(a % i == 0 || a % (i + 2) == 0) return false;
}
return true;
}
static int [] seive(int a) {
int [] toReturn = new int [a + 1];
for(int i = 0; i < a; i++) toReturn[i] = 1;
toReturn[0] = 0;
toReturn[1] = 0;
toReturn[2] = 1;
for(int i = 2; i * i <= a; i++) {
if(toReturn[i] == 0) continue;
for(int j = 2 * i; j <= a; j += i) toReturn[j] = 0;
}
return toReturn;
}
static long [] fact(int a) {
long [] arr = new long[a + 1];
arr[0] = 1;
for(int i = 1; i < a + 1; i++) {
arr[i] = (arr[i - 1] * i) % mod;
}
return arr;
}
static ArrayList<Long> divisors(long n) {
ArrayList<Long> arr = new ArrayList<Long>();
for(long i = 2; i * i <= n; i++) {
if(n % i == 0) {
arr.add(i);
if(i != n / i) arr.add(n / i);
}
}
if(n > 1) arr.add(n);
return arr;
}
static int euler(int n) {
int ans = n;
for(int i = 2; i * i <= n; i++) {
if(n % i == 0) {
while(n % i == 0) {
n /= i;
}
ans -= ans / i;
}
}
if(n > 1) ans -= ans / n;
return ans;
}
static long extendedEuclid(long a, long b, long [] arr) {
if(b == 0) {
arr[0] = 1;
arr[1] = 0;
return a;
}
long [] arr1 = new long[2];
long d = extendedEuclid(b, a % b, arr1);
arr[0] = arr1[1];
arr[1] = arr1[0] - arr1[1] * (a / b);
return d;
}
/*MATHS
MATHS
MATHS
MATHS
MATHEMATICS FUNCTIONS END HERE */
/*SWAP FUNCTION START HERE
SWAP
SWAP
SWAP
SWAP
*/
static void swap(int i, int j, long[] arr) {
long temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static void swap(int i, int j, int[] arr) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static void swap(int i, int j, String [] arr) {
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static void swap(int i, int j, char [] arr) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/*SWAP
SWAP
SWAP
SWAP
SWAP FUNCTION END HERE*/
/*BINARY SEARCH METHODS START HERE
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
*/
static boolean BinaryCheck(long test, long [] arr, long health) {
for(int i = 0; i <= arr.length - 1; i++) {
if(i == arr.length - 1) health -= test;
else if(arr[i + 1] - arr[i] > test) {
health = health - test;
}else {
health = health - (arr[i + 1] - arr[i]);
}
if(health <= 0) return true;
}
return false;
}
static long binarySearchModified(long start1, long n, ArrayList<Integer> arr, int val) {
long start = start1, end = n, ans = -1;
while(start <= end) {
long mid = (start + end) / 2;
if(arr.get((int)mid) >= val) {
if(arr.get((int)mid) == val && mid + 1 < arr.size() && arr.get((int)mid + 1) == val) {
start = mid + 1;
}else if(arr.get((int)mid) == val) {
return mid;
}else end = mid - 1;
}else {
start = mid + 1;
}
}
//System.out.println();
return start;
}
static int upper(int start, int end, ArrayList<Integer> pairs, long val) {
while(start < end) {
int mid = (start + end) / 2;
if(pairs.get(mid) <= val) start = mid + 1;
else end = mid;
}
return start;
}
static int lower(int start, int end, ArrayList<Long> arr, long val) {
while(start < end) {
int mid = (start + end) / 2;
if(arr.get(mid) >= val) end = mid;
else start = mid + 1;
}
return start;
}
/*BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
BINARY SEARCH METHODS END HERE*/
/*RECURSIVE FUNCTION START HERE
* RECURSIVE
* RECURSIVE
* RECURSIVE
* RECURSIVE
*/
static int recurse(int x, int y, int n, int steps1, Integer [][] dp) {
if(x > n || y > n) return 0;
if(dp[x][y] != null) {
return dp[x][y];
}
else if(x == n || y == n) {
return steps1;
}
return dp[x][y] = Math.max(recurse(x + y, y, n, steps1 + 1, dp), recurse(x, x + y, n, steps1 + 1, dp));
}
/*RECURSIVE
* RECURSIVE
* RECURSIVE
* RECURSIVE
* RECURSIVE
RECURSIVE FUNCTION END HERE*/
/*GRAPH FUNCTIONS START HERE
* GRAPH
* GRAPH
* GRAPH
* GRAPH
* */
static class edge{
int from, to;
long weight;
public edge(int x, int y, long weight2) {
this.from = x;
this.to = y;
this.weight = weight2;
}
}
static class sort implements Comparator<TreeNode>{
@Override
public int compare(TreeNode a, TreeNode b) {
// TODO Auto-generated method stub
return 0;
}
}
static void addEdge(ArrayList<ArrayList<edge>> graph, int from, int to, long weight) {
edge temp = new edge(from, to, weight);
edge temp1 = new edge(to, from, weight);
graph.get(from).add(temp);
//graph.get(to).add(temp1);
}
static int ans = 0;
static void topoSort(ArrayList<ArrayList<Integer>> graph, int vertex, boolean [] visited, ArrayList<Integer> toReturn) {
if(visited[vertex]) return;
visited[vertex] = true;
for(int i = 0; i < graph.get(vertex).size(); i++) {
if(!visited[graph.get(vertex).get(i)]) topoSort(graph, graph.get(vertex).get(i), visited, toReturn);
}
toReturn.add(vertex);
}
static boolean isCyclicDirected(ArrayList<ArrayList<Integer>> graph, int vertex, boolean [] visited, boolean [] reStack) {
if(reStack[vertex]) return true;
if(visited[vertex]) return false;
reStack[vertex] = true;
visited[vertex] = true;
for(int i = 0; i < graph.get(vertex).size(); i++) {
if(isCyclicDirected(graph, graph.get(vertex).get(i), visited, reStack)) return true;
}
reStack[vertex] = false;
return false;
}
static int e = 0;
static long mst(PriorityQueue<edge> pq, int nodes) {
long weight = 0;
int [] size = new int[nodes + 1];
Arrays.fill(size, 1);
while(!pq.isEmpty()) {
edge temp = pq.poll();
int x = parent(parent, temp.to);
int y = parent(parent, temp.from);
if(x != y) {
//System.out.println(temp.weight);
union(x, y, rank, parent, size);
weight += temp.weight;
e++;
}
}
return weight;
}
static void floyd(long [][] dist) { // to find min distance between two nodes
for(int k = 0; k < dist.length; k++) {
for(int i = 0; i < dist.length; i++) {
for(int j = 0; j < dist.length; j++) {
if(dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
}
static void dijkstra(ArrayList<ArrayList<edge>> graph, long [] dist, int src) {
for(int i = 0; i < dist.length; i++) dist[i] = Long.MAX_VALUE / 2;
dist[src] = 0;
boolean visited[] = new boolean[dist.length];
PriorityQueue<pair> pq = new PriorityQueue<>();
pq.add(new pair(src, 0));
while(!pq.isEmpty()) {
pair temp = pq.poll();
int index = (int)temp.a;
for(int i = 0; i < graph.get(index).size(); i++) {
if(dist[graph.get(index).get(i).to] > dist[index] + graph.get(index).get(i).weight) {
dist[graph.get(index).get(i).to] = dist[index] + graph.get(index).get(i).weight;
pq.add(new pair(graph.get(index).get(i).to, graph.get(index).get(i).weight));
}
}
}
}
static int parent1 = -1;
static boolean ford(ArrayList<ArrayList<edge>> graph1, ArrayList<edge> graph, long [] dist, int src, int [] parent) {
for(int i = 0; i < dist.length; i++) dist[i] = Long.MIN_VALUE / 2;
dist[src] = 0;
boolean hasNeg = false;
for(int i = 0; i < dist.length - 1; i++) {
for(int j = 0; j < graph.size(); j++) {
int from = graph.get(j).from;
int to = graph.get(j).to;
long weight = graph.get(j).weight;
if(dist[to] < dist[from] + weight) {
dist[to] = dist[from] + weight;
parent[to] = from;
}
}
}
for(int i = 0; i < graph.size(); i++) {
int from = graph.get(i).from;
int to = graph.get(i).to;
long weight = graph.get(i).weight;
if(dist[to] < dist[from] + weight) {
parent1 = from;
hasNeg = true;
/*
* dfs(graph1, parent1, new boolean[dist.length], dist.length - 1);
* //System.out.println(ans); dfs(graph1, 0, new boolean[dist.length], parent1);
*/
//System.out.println(ans);
if(ans == 2) break;
else ans = 0;
}
}
return hasNeg;
}
/*GRAPH FUNCTIONS END HERE
* GRAPH
* GRAPH
* GRAPH
* GRAPH
*/
/*disjoint Set START HERE
* disjoint Set
* disjoint Set
* disjoint Set
* disjoint Set
*/
static int [] rank;
static int [] parent;
static int parent(int [] parent, int x) {
if(parent[x] == x) return x;
else return parent[x] = parent(parent, parent[x]);
}
static boolean union(int x, int y, int [] rank, int [] parent, int [] setSize) {
if(parent(parent, x) == parent(parent, y)) {
return true;
}
if (rank[x] > rank[y]) {
parent[y] = x;
setSize[x] += setSize[y];
} else {
parent[x] = y;
setSize[y] += setSize[x];
if (rank[x] == rank[y]) rank[y]++;
}
return false;
}
/*disjoint Set END HERE
* disjoint Set
* disjoint Set
* disjoint Set
* disjoint Set
*/
/*INPUT START HERE
* INPUT
* INPUT
* INPUT
* INPUT
* INPUT
*/
static int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(sc.readLine());
}
static long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(sc.readLine());
}
static long [] inputLongArr() throws NumberFormatException, IOException{
String [] s = sc.readLine().split(" ");
long [] toReturn = new long[s.length];
for(int i = 0; i < s.length; i++) {
toReturn[i] = Long.parseLong(s[i]);
}
return toReturn;
}
static int max = 0;
static int [] inputIntArr() throws NumberFormatException, IOException{
String [] s = sc.readLine().split(" ");
//System.out.println(s.length);
int [] toReturn = new int[s.length];
for(int i = 0; i < s.length; i++) {
toReturn[i] = Integer.parseInt(s[i]);
}
return toReturn;
}
/*INPUT
* INPUT
* INPUT
* INPUT
* INPUT
* INPUT END HERE
*/
static long [] preCompute(int level) {
long [] toReturn = new long[level];
toReturn[0] = 1;
toReturn[1] = 16;
for(int i = 2; i < level; i++) {
toReturn[i] = ((toReturn[i - 1] % mod) * (toReturn[i - 1] % mod)) % mod;
}
return toReturn;
}
static class pair{
long a;
long b;
long d;
public pair(long in, long y) {
this.a = in;
this.b = y;
this.d = 0;
}
}
static int [] nextGreaterBack(char [] s) {
Stack<Integer> stack = new Stack<>();
int [] toReturn = new int[s.length];
for(int i = 0; i < s.length; i++) {
if(!stack.isEmpty() && s[stack.peek()] >= s[i]) {
stack.pop();
}
if(stack.isEmpty()) {
stack.push(i);
toReturn[i] = -1;
}else {
toReturn[i] = stack.peek();
stack.push(i);
}
}
return toReturn;
}
static int [] nextGreaterFront(char [] s) {
Stack<Integer> stack = new Stack<>();
int [] toReturn = new int[s.length];
for(int i = s.length - 1; i >= 0; i--) {
if(!stack.isEmpty() && s[stack.peek()] >= s[i]) {
stack.pop();
}
if(stack.isEmpty()) {
stack.push(i);
toReturn[i] = -1;
}else {
toReturn[i] = stack.peek();
stack.push(i);
}
}
return toReturn;
}
static int [] lps(String s) {
int [] lps = new int[s.length()];
lps[0] = 0;
int j = 0;
for(int i = 1; i < lps.length; i++) {
j = lps[i - 1];
while(j > 0 && s.charAt(i) != s.charAt(j)) j = lps[j - 1];
if(s.charAt(i) == s.charAt(j)) {
lps[i] = j + 1;
}
}
return lps;
}
static int [][] vectors = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
static String dir = "DRUL";
static boolean check(int i, int j, boolean [][] visited) {
if(i >= visited.length || j >= visited[0].length) return false;
if(i < 0 || j < 0) return false;
return true;
}
static void selectionSort(long arr[], long [] arr1, ArrayList<ArrayList<Integer>> ans)
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
else if(arr[j] == arr[min_idx]) {
if(arr1[j] < arr1[min_idx]) min_idx = j;
}
if(i == min_idx) {
continue;
}
ArrayList<Integer> p = new ArrayList<Integer>();
p.add(min_idx + 1);
p.add(i + 1);
ans.add(new ArrayList<Integer>(p));
swap(i, min_idx, arr);
swap(i, min_idx, arr1);
}
}
static int saved = Integer.MAX_VALUE;
static String ans1 = "";
public static boolean isValid(int x, int y, String [] mat) {
if(x >= mat.length || x < 0) return false;
if(y >= mat[0].length() || y < 0) return false;
return true;
}
public static void recurse3(ArrayList<Character> arr, int index, String s, int max, ArrayList<String> toReturn) {
if(s.length() == max) {
toReturn.add(s);
return;
}
if(index == arr.size()) return;
recurse3(arr, index + 1, s + arr.get(index), max, toReturn);
recurse3(arr, index + 1, s, max, toReturn);
}
/*
if(arr[i] > q) return Math.max(f(i + 1, q - 1) + 1, f(i + 1, q);
else return f(i + 1, q) + 1
*/
static void dfsDP(ArrayList<ArrayList<Integer>> graph, int src, int [] dp1, int [] dp2, int parent) {
int sum1 = 0; int sum2 = 0;
for(int x : graph.get(src)) {
if(x == parent) continue;
dfsDP(graph, x, dp1, dp2, src);
sum1 += Math.min(dp1[x], dp2[x]);
sum2 += dp1[x];
}
dp1[src] = 1 + sum1;
dp2[src] = sum2;
System.out.println(src + " " + dp1[src] + " " + dp2[src]);
}
static int balanced = 0;
static void dfs(ArrayList<ArrayList<ArrayList<Long>>> graph, long src, int [] dist, long sum1, long sum2, long parent, ArrayList<Long> arr, int index) {
index = 0;//binarySearch(index, arr.size() - 1, arr, sum1);
if(index < arr.size() && arr.get(index) <= sum1) {
dist[(int)src] = index + 1;
}
else dist[(int)src] = index;
for(ArrayList<Long> x : graph.get((int)src)) {
if(x.get(0) == parent) continue;
if(arr.size() != 0) arr.add(arr.get(arr.size() - 1) + x.get(2));
else arr.add(x.get(2));
dfs(graph, x.get(0), dist, sum1 + x.get(1), sum2, src, arr, index);
arr.remove(arr.size() - 1);
}
}
static int compare(String s1, String s2) {
Queue<Character> q1 = new LinkedList<>();
Queue<Character> q2 = new LinkedList<Character>();
for(int i = 0; i < s1.length(); i++) {
q1.add(s1.charAt(i));
q2.add(s2.charAt(i));
}
int k = 0;
while(k < s1.length()) {
if(q1.equals(q2)) {
break;
}
q2.add(q2.poll());
k++;
}
return k;
}
static long pro = 0;
public static int len(ArrayList<ArrayList<Integer>> graph, int src, boolean [] visited
) {
visited[src] = true;
int max = 0;
for(int x : graph.get(src)) {
if(!visited[x]) {
visited[x] = true;
int len = len(graph, x, visited) + 1;
//System.out.println(len);
pro = Math.max(max * (len - 1), pro);
max = Math.max(len, max);
}
}
return max;
}
public static void recurse(int l, int [] ans) {
if(l < 0) return;
int r = (int)Math.sqrt(l * 2);
int s = r * r;
r = s - l;
recurse(r - 1, ans);
while(r <= l) {
ans[r] = l;
ans[l] = r;
r++;
l--;
}
}
static boolean isSmaller(String str1, String str2)
{
// Calculate lengths of both string
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2)
return true;
if (n2 < n1)
return false;
for (int i = 0; i < n1; i++)
if (str1.charAt(i) < str2.charAt(i))
return true;
else if (str1.charAt(i) > str2.charAt(i))
return false;
return false;
}
// Function for find difference of larger numbers
static String findDiff(String str1, String str2)
{
// Before proceeding further, make sure str1
// is not smaller
if (isSmaller(str1, str2)) {
String t = str1;
str1 = str2;
str2 = t;
}
// Take an empty string for storing result
String str = "";
// Calculate length of both string
int n1 = str1.length(), n2 = str2.length();
// Reverse both of strings
str1 = new StringBuilder(str1).reverse().toString();
str2 = new StringBuilder(str2).reverse().toString();
int carry = 0;
// Run loop till small string length
// and subtract digit of str1 to str2
for (int i = 0; i < n2; i++) {
// Do school mathematics, compute difference of
// current digits
int sub
= ((int)(str1.charAt(i) - '0')
- (int)(str2.charAt(i) - '0') - carry);
// If subtraction is less than zero
// we add then we add 10 into sub and
// take carry as 1 for calculating next step
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str += (char)(sub + '0');
}
// subtract remaining digits of larger number
for (int i = n2; i < n1; i++) {
int sub = ((int)(str1.charAt(i) - '0') - carry);
// if the sub value is -ve, then make it
// positive
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str += (char)(sub + '0');
}
// reverse resultant string
return new StringBuilder(str).reverse().toString();
}
static void solve() throws IOException {
/*
while(true) {
long [] n = inputLongArr();
if(n[0] == 0 && n[1] == 0 && n[2] == 0 && n[3] == 0) break;
long d = (n[3] + n[2] - n[0]);
System.out.println(d);
long [] ans = new long [2];
long gcd;
if(d < 0) {
n[1] = -1 * n[1];
}else n[2] = -1 * n[2];
gcd = extendedEuclid(Math.abs(n[1]), Math.abs(n[2]), ans);
if(d % gcd == 0) {
System.out.println(ans[0] + " " + ans[1]);
ans[0] *= Math.abs(d) / gcd;
ans[1] *= Math.abs(d) / gcd;
//System.out.println(ans[0] + " " + ans[1]);
if(n[1] < 0) {
ans[0] *= -1;
}
if(n[2] < 0) {
ans[1] *= -1;
}
if(ans[1] < 0) {
long k = -1 * (long)Math.ceil(ans[1] / (double)n[1]);
System.out.println(k);
ans[0] = ans[0] - n[2] * k;
ans[1] = ans[1] + n[1] * k;
}
if(ans[0] < 0) {
long k = (long)Math.ceil(ans[0] / (double)n[2]);
ans[0] = ans[0] - n[2] * k;
ans[1] = ans[1] + n[1] * k;
System.out.println(ans[0] + " " + ans[1] + " 1");
}
output.write(n[0] + ans[0] * n[1] + "\n");
System.out.println(ans[0] + " " + ans[1]);
}else {
output.write("Impossible\n");
}
}
int n = nextInt();
char [] s = sc.readLine().toCharArray();
int [] hash = new int [256];
char [] given = "Timur".toCharArray();
Arrays.sort(s);
Arrays.sort(given);
if(s.length != given.length) {
output.write("NO\n");
return;
}
for(int i = 0; i < s.length; i++) {
if(s[i] != given[i]) {
output.write("NO\n");
return;
}
}
output.write("YES\n");
*/
int n = nextInt();
String s = sc.readLine();
long [] dp = new long [n];
if(n == 1) {
output.write(0 + "\n");
return;
}
long sum = 0;
for(int i = 0; i < n; i++) {
if(s.charAt(i) == 'L') {
sum += i;
}else sum += s.length() - 1 - i;
}
int k = 0;
long max = sum;
//System.out.println(sum);
int i = 0; int j = s.length() - 1;
TreeMap<Integer, Long> map = new TreeMap<Integer, Long>();
while(i <= j) {
while(s.charAt(i) != 'L' && i < n / 2) i++;
while(s.charAt(j) != 'R' && j >= n / 2) j--;
long l1 = s.length() - 1 - i;
long r1 = j;
if(s.charAt(i) != 'L' || i > n / 2) {
l1 = s.length() - 1 - j;
}
if(s.charAt(j) != 'R' || j < n / 2) {
r1 = i;
}
if(l1 > r1) {
sum -= i;
sum += l1;
max = Math.max(sum, max);
//arr.add(sum);
i++;
}else {
sum -= s.length() - 1 - j;
sum += r1;
max = Math.max(sum, max);
//arr.add(sum);
j--;
}
//System.out.println(i + " " + j + " " + sum + " " + l1 + " " + r1);
k++;
map.put(k, max);
}
for(i = 0; i < s.length(); i++) {
output.write(map.get(map.floorKey(i + 1)) + " ");
}
output.write("\n");
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int t = Integer.parseInt(sc.readLine()); for(int i = 0; i < t; i++)
solve();
output.flush();
}
}
class TreeNode {
int val; int index;
public TreeNode(int val, int index) {
this.val = val;
this.index = index;
}
public String toString() {
return val + " " + index;
}
}
/*
1
10
6 10 7 9 11 99 45 20 88 31
*/
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
ceb64aed6357751bbefca9f182d9299e
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class bhasad {
static class Pair implements Comparable<Pair>{
int f;
int s;
Pair(int f, int s){
this.f = f;
this.s = s;
}
public int compareTo(Pair o){
return Math.abs(o.f - o.s)-Math.abs(this.f - this.s);
}
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-- > 0){
int n = scn.nextInt();
String s = scn.next();
long cnt = 0;
for(int i = 0;i<s.length();i++){
char ch =s.charAt(i);
if(ch == 'L'){
cnt += i;
}
else{
cnt += (n-(i+1));
}
}
ArrayList<Long> arr = new ArrayList<>();
Pair[] a = new Pair[n];
for(int i = 0;i<n/2;i++){
char ch =s.charAt(i);
int f = 0;
if(ch == 'L'){
f = i;
}
else{
f = (n-(i+1));
}
int m = (n-(i+1));
Pair p = new Pair(f, m);
a[i] = p;
}
for(int i = n/2;i<n;i++){
char ch =s.charAt(i);
int f = 0;
if(ch == 'R'){
f = (n-(i+1));
}
else{
f = i;
}
int m = i;
Pair p = new Pair(f, m);
a[i] = p;
}
Arrays.sort(a);
for(int i = 0;i<a.length;i++){
if(a[i].f-a[i].s != 0){
cnt = cnt - a[i].f;
cnt = cnt + a[i].s;
System.out.print(cnt + " ");
}
else{
System.out.print(cnt + " ");
}
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
d1dc71e5dcf6a95940598527c46c7c6f
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class D_Line {
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0)
{
long n=sc.nextLong();
String s=sc.next();
// int l=s.length();
long c=0;
ArrayList<Long> a=new ArrayList<>();
ArrayList<Long> aa=new ArrayList<>();
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='L')
{
c+=i;
}
else{
c+=(n-1-i);
}
}
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='L' && (i<(n-1-i)))
{// a[i]=n-1-2*i;
aa.add(n-1-2*i);
}
else if(s.charAt(i)=='R' && (i>(n-i-1))){
// a[i]=2*i+1-n;
aa.add(2*i+1-n);
}
// else{
// a[i]=c;
// }
}
Collections.sort(aa,Comparator.reverseOrder());
long p=0;
for(int i=0;i<aa.size();i++)
{
p=p+aa.get(i);
a.add(p+c);
// System.out.print((p+c)+" ");
}
for(int i=aa.size();i<n;i++)
{a.add(p+c);
// p=p+aa.get(i);
// System.out.print((p+c)+" ");
}
for(int i=0;i<n;i++)
{System.out.print(a.get(i)+" ");
// p=p+aa.get(i);
// System.out.print((p+c)+" ");
}
System.out.println();
}
sc.close();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
e39cfe752aa84c47e16c94deb718921a
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class TrialCodes {
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
String s = sc.next();
long sum = 0;
for(int i = 0; i < n ; i++) {
if(s.charAt(i)=='L') {
sum += i;
}
if(s.charAt(i) =='R') {
sum += n-(i+1);
}
}
ArrayList<Long> al = new ArrayList<>();
int mid = n/2;
for(int i = 0 , j = n-1 ;i < mid && j >= mid;i++, j--){
if(s.charAt(i) == 'L') {
sum -= i;
sum += n-(i+1);
al.add(sum);
}
if(s.charAt(j) == 'R') {
sum -= n-(j+1);
sum += j;
al.add(sum);
}
}
if(al.size() < n) {
if(al.size() == 0) {
for(int i = 0 ;i < n ; i++) {
System.out.print(sum+" ");
}
System.out.println();
continue;
}
for(int i = 0 ; i < al.size();i++) {
System.out.print(al.get(i)+" ");
}
for(int i = al.size() ;i < n ;i++) {
System.out.print(al.get(al.size()-1)+" ");
}
}else {
for(int i = 0 ; i < al.size();i++) {
System.out.print(al.get(i)+" ");
}
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
079bff8123b825f832cf64e28324a469
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class line {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i =0;i<t;i++){
int n = sc.nextInt();
String s = sc.next();
char [] line1 = s.toCharArray();
long sum =0;
ArrayList<Integer> arr = new ArrayList<>();
for(int j=0;j<n;j++){
if(line1[j]=='L'){
sum = sum + j;
}
else{
sum = sum+ (n-j-1);
}
if(j<n/2){
if(line1[j]!='R')
arr.add(n-j-1-j);
}
else if(line1[j]!='L'){
arr.add(j-n+j+1);
}
}
Collections.sort(arr);
long add=0;
for(int j=0;j<n;j++){
if(!arr.isEmpty()) {
add = add + arr.get(arr.size()-1);
arr.remove(arr.size()-1);
}
long sol = add +sum;
System.out.print(sol + " ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
f03364c57b39333b9834b4411e8ca1de
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
// LARGEST SQUARE
// package com.company
/*
* @author :: Yuvraj Singh
* CS UNDERGRAD AT IT GGV BILASPUR
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import static java.lang.Math.*;
import static java.lang.System.out;
public class Main {
// Solution here
void solve() {
int n = in.nextInt();
char[] s = in.next().toCharArray();
ArrayList<Long> arr= new ArrayList<>();
ArrayList<Long> ans= new ArrayList<>();
long tot = 0;
for(int i = 0; i<n; i++){
if(s[i]=='L'){
tot += i;
}else{
tot += (n-1-i);
}
if(s[i]=='L' && (i<(n-1-i))){
arr.add((long) (n-1-2*i));
}
else if(s[i]=='R' && i>(n-1-i)){
arr.add((long) (2*i+1-n));
}
}
Collections.sort(arr);
Collections.reverse(arr);
long sum = 0;
for(int i = 0; i<arr.size(); i++){
sum+= arr.get(i);
ans.add(tot + sum);
}
int sz = arr.size();
for(int i = sz; i<n; i++){
ans.add(tot + sum);
}
for(long x:ans){
sb.append(x).append(" ");
}
sb.append("\n");
}
void start(){
sb= new StringBuffer();
int t= in.nextInt();
for(int i=1;i<=t;i++) {
solve();
}
out.print(sb);
}
// Starter Code
FastReader in;
StringBuffer sb;
public static void main(String[] args) {
new Main().run();
}
void run(){
in= new FastReader();
start();
}
int lower_bound(ArrayList<Integer> a, int x) { // x is the target value or key
int l=-1,r=a.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(a.get(m)>=x) r=m;
else l=m;
}
return r;
}
void bubbleSort(int[] arr){
int n= arr.length;
for(int i=n-1; i>0; i--){
for(int j=0; j<i; j++){
if(arr[i] < arr[j]){
swap(arr, i, j);
}
}
}
}
void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
long numberOfWays(long n, long k) {
// Base Cases
if (n == 0)
return 1;
if (k == 0)
return 1;
if (n >= (int)Math.pow(2, k)) {
int curr_val = (int)Math.pow(2, k);
return numberOfWays(n - curr_val, k)
+ numberOfWays(n, k - 1);
}
else
return numberOfWays(n, k - 1);
}
boolean palindrome(String s){
int i=0, j= s.length()-1;
while(i<j){
if(s.charAt(i)!=s.charAt(j)) return false;
i++; j--;
}
return true;
}
int call(int[] A, int N, int K) {
int i = 0, j = 0, sum = 0;
int maxLen = Integer.MIN_VALUE;
while (j < N) {
sum += A[j];
if (sum < K) {
j++;
}
else if (sum == K) {
maxLen = Math.max(maxLen, j-i+1);
j++;
}
else if (sum > K) {
while (sum > K) {
sum -= A[i];
i++;
}
if(sum == K){
maxLen = Math.max(maxLen, j-i+1);
}
j++;
}
}
return maxLen;
}
int largestNum(int n)
{
int num = 0;
// Iterate through all possible values
for (int i = 0; i <= 32; i++)
{
// Multiply the number by 2 i times
int x = (1 << i);
if ((x - 1) >= n)
num = (1 << i) - 1;
else
break;
}
// Return the final result
return num;
}
static boolean isPrime(int n)
{
// Corner case
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i <= sqrt(n); i++)
if (n % i == 0)
return false;
return true;
}
// Useful Functions
// void swap(int[] arr, int i , int j) {
// int tmp = arr[i];
// i = j;
// j = tmp;
// }
int call(int i,int j, int[][] mat, int[][] dp){
int m= mat.length;
int n= mat[0].length;
if(i>=m || j>=n){
return Integer.MIN_VALUE;
}
if(i==m-1 && j==n-1){
return mat[i][j];
}
if(dp[i][j] != -1){
return dp[i][j];
}
return dp[i][j] = max(call(i+1, j, mat, dp), call(i, j+1, mat, dp)) + mat[i][j];
}
int util(int i,int j, int[][] mat, int[][] dp){
int m= mat.length;
int n= mat[0].length;
if(i>=m || j>=n){
return Integer.MAX_VALUE;
}
if(i==m-1 && j==n-1){
return mat[i][j];
}
if(dp[i][j] != -1){
return dp[i][j];
}
return dp[i][j] = min(util(i+1, j, mat, dp), util(i, j+1, mat, dp)) + mat[i][j];
}
long power(long x, long y, long p) {
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int lower_bound(long[] a, long x) { // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
int upper_bound(ArrayList<Integer> arr, int key) {
int i=0, j=arr.size()-1;
if(j==-1){
return 0;
}
if (arr.get(j)<=key) return j+1;
if(arr.get(i)>key) return i;
while (i<j){
int mid= (i+j)/2;
if(arr.get(mid)<=key){
i= mid+1;
}else{
j=mid;
}
}
return i;
}
void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
int[] intArr(int n){
int[] res= new int[n];
for(int i=0;i<n;i++){
res[i]= in.nextInt();
}
return res;
}
long[] longArr(int n){
long[] res= new long[n];
for(int i=0;i<n;i++){
res[i]= in.nextLong();
}
return res;
}
// // sieve of eratosthenes code for precomputing whether numbers are prime or not up to MAX_VALUE
long MAX= 100000000;
int[] precomp;
void sieve(){
long n= MAX;
precomp = new int[(int) (n+1)];
boolean[] prime = new boolean[(int) (n+1)];
for(int i=0;i<=n;i++)
prime[i] = true;
for(long p = 2; p*p <=n; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[(int) p])
{
// Update all multiples of p
for(long i = p*p; i <= n; i += p)
prime[(int) i] = false;
}
}
// Print all prime numbers
for(long i = 2; i <= n; i++)
{
if(prime[(int) i])
precomp[(int) i]= 1;
}
}
long REVERSE(long N) {
// code here
long rev=0;
long org= N;
while (N!=0){
long d= N%10;
rev = rev*10 +d;
N /= 10;
}
return rev;
}
long sumOfDigits(String n){
long sum= 0;
for (char c: n.toCharArray()){
sum += Integer.parseInt(String.valueOf(c));
}
return sum;
}
long[] revArray(long[] arr) {
int n= arr.length;
int i=0, j=n-1;
while (i<j){
long temp= arr[i];
arr[i]= arr[j];
arr[j]= temp;
i++;
j--;
}
return arr;
}
long gcd(long a, long b){
if (b==0)
return a;
return gcd(b, a%b);
}
long lcm(long a,long b){
return (a*b)/gcd(a,b);
}
static class Pair implements Comparable<Pair>{
long first;
long second;
Pair(long x, long y){
this.first=x;
this.second=y;
}
@Override
public int compareTo(Pair o) {
return 0;
}
}
// static class Compare {
// static void compare(ArrayList<Pair> arr, int n) {
// arr.sort(new Comparator<Pair>() {
// @Override
// public int compare(Pair p1, Pair p2) {
// return (int) (p2.first - p1.first);
// }
// });
// }
// }
public static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st==null || !st.hasMoreElements()){
try{
st=new StringTokenizer(br.readLine());
}catch (Exception e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
float nextFloat(){
return Float.parseFloat(next());
}
String nextLine(){
String str="";
try{
str=br.readLine();
}catch (Exception e){
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
41ff6a7f2590dea73fdef8de398b0455
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
//codeforces
//package someAlgorithms;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.io.File;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
String[] strNums = br.readLine().split(" ");
int t=Integer.parseInt(strNums[0]);
// int ot=t;
// FileWriter fw = new FileWriter("C:\\Users\\Lenovo\\Desktop\\output.txt");
// fw.write("");
// fw.close();
while(t-->0) {
//todo
String[] strNums1 = br.readLine().split(" ");
long n=Long.parseLong(strNums1[0]);
// long m=Long.parseLong(strNums1[1]);
// long l1=Long.parseLong(strNums1[2]);
// long l2=Long.parseLong(strNums1[3]);
// long d=Long.parseLong(strNums1[4]);
String str = br.readLine();
// String str2 = br.readLine();
// Long[] arr = new Long[(int)n];
// StringTokenizer tk = new StringTokenizer(br.readLine().trim());
// for(int i=0;i<n;i++) {
// arr[i]=Long.parseLong(tk.nextToken());
// }
int l=0;
int r=(int)(long)n-1;
long ans=0;
for(int i=0;i<n;i++) {
if(str.charAt(i)=='L') {
ans+=i;
}
else {
ans+=(n-i-1);
}
}
long[] out = new long[(int)n];
int ind=0;
long ans1=ans;
while(l<n/2 && r>=n/2){
// if(l<=(n-r-1)) {
if(str.charAt(l)=='L') {
ans1-=l;
ans1+=(n-l-1);
out[ind]=ans1;
ind++;
// l++;
}
if(str.charAt(r)=='R' ) {
ans1-=(n-r-1);
ans1+=(r);
out[ind]=ans1;
ind++;
// r--;
}
// else {
l++;
r--;
// }
// }
// else {
//
// if(str.charAt(r)=='R' && (n-r-1)<r) {
// ans1-=(n-r-1);
// ans1+=(r);
// out[ind]=ans1;
// ind++;
// r--;
// }
// else if(str.charAt(l)=='L' && l<(n-l-1)) {
// ans1-=l;
// ans1+=(n-l-1);
// out[ind]=ans1;
// ind++;
// l++;
// }else {
// l++;
// r--;
// }
// }
}
while(l<n/2) {
if(str.charAt(l)=='L' ) {
ans1-=l;
ans1+=(n-l-1);
out[ind]=ans1;
ind++;
}
l++;
}
while(r>=n/2) {
if(str.charAt(r)=='R') {
ans1-=(n-r-1);
ans1+=(r);
out[ind]=ans1;
ind++;
}
r--;
}
if(ind==0) {
for(int i=0;i<n;i++) {
out[i]=ans;
}
}
else {
long temp = out[ind-1];
for(int i=ind;i<n;i++) {
out[i]=temp;
}
}
for(int i=0;i<n;i++) {
System.out.print(out[i]+" ");
}
System.out.println();
}
}
}
/*
bw.write(n+"");
bw.newLine();
bw.flush();
System.out.println("Case #"+(ot-t)+":"+" YES");
FileWriter myWriter = new FileWriter("C:\\Users\\Lenovo\\Desktop\\output.txt",true);
myWriter.write("Case #"+(ot-t)+": "+"YES"+"\n");
myWriter.close();
*/
//class Pair{
// public long a=0;
// public long b=0;
// public Pair(long val,long id){
// this.a=val;
// this.b=id;
// }
//
//}
//class Pair{
// public long val;
// public long id;
// public Pair(long val,long id){
// this.val=val;
// this.id=id;
// }
//
//}
//class Node{
// public int val;
//// public Node left=null;
//// public Node right=null;
// ArrayList<Node> children = new ArrayList<>();
//
//}
//class Comp implements Comparator<Pair>{
// public int compare(Pair p1,Pair p2) {
// if(p1.val<p2.val) {
// return -1;
// }
// if(p1.val>p2.val){
// return 1;
// }
// else return 0; //MUST WRITE THIS RETURN 0 FOR EQUAL CASE SINCE GIVE RUNTIME ERROR IN SOME COMPLIERS
// }
//}
//if take gcd of whole array the take default gcd=0 and keep doing gcd of 2 elements of array!
//public static int gcd(int a, int b){
// if(b==0){
// return a;
// }
// return gcd(b,a%b);
//}
//
//ArrayList<Long>[] adjlist = new ArrayList[(int)n+1]; //array of arraylist
//for(int i=0;i<n;i++) {
// String[] strNums2 = br.readLine().split(" ");
// long a=Long.parseLong(strNums2[0]);
// long b=Long.parseLong(strNums2[1]);
//
// adjlist[(int)a].add(b);
// adjlist[(int)b].add(a);
//}
//int[][] vis = new int[(int)n+1][(int)n+1];
//OR can make list of list :-
//List<List<Integer>> adjlist = new ArrayList<>();
//for(int i=0;i<n;i++){
// adjlist.add(new ArrayList<>());
//}
//OR 1-D vis array
//int[] vis = new int[(int)n+1];
/*
Long[] arr = new Long[(int)n];
StringTokenizer tk = new StringTokenizer(br.readLine().trim());
for(int i=0;i<n;i++) {
arr[i]=Long.parseLong(tk.nextToken());
}
Long[][] arr = new Long[(int)n][(int)m];
for(int i=0;i<n;i++) {
String[] strNums2 = br.readLine().split(" ");
for(int j=0;j<m;j++) {
arr[i][j]=Long.parseLong(strNums2[j]);
}
}
4
4 4 3 2
4 4 4 3
Main m = new Main(); //no need since pair class main class ne niche banao
Pair p = m.new Pair(i,i+1);
li.add(p);
*/
//double num = 3.9634;
//double onum=num;
//num = (double)((int)(num*100))/100;
//double down = (num);
//float up =(float) down; //if take up as double then will get large value when add (3.96+0.01)!!
//if(down<onum) {
// up=(float)down+(float)0.01;
//// System.out.println(((float)3.96+(float)0.01));
//// System.out.println(3.96+0.01);//here both double, so output double , so here get large output other than 3.97!!
//}
////in c++ 3.96+0.01 is by default 3.97 but in java need to type cast to float to get this output!!
//System.out.println(down +" "+up);
/*
#include <iostream>
#include <string>
#include<vector>
#include<queue>
#include<utility>
#include<limits.h>
#include <unordered_set>
#include<algorithm>
using namespace std;
*/
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
8c04515c1df4c0a1f8e3d5beb9358cb1
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
///WizardAP - 当你休息的时候,很多人比你付出更多的努力!不放弃 !
/// Time : 2022-10-12, Wed, 14:37
import java.io.*;
import java.util.*;
import static java.lang.Double.parseDouble;
import static java.lang.System.in;
import static java.lang.System.out;
public class Main {
static final int MOD = (int) 1e9 + 7;
static class Pair implements Comparable<Pair>
{
public int ft,sd;
public Pair(int ft,int sd)
{
this.ft = ft;
this.sd=sd;
}
public int get(){
return sd-ft;
}
@Override
public int compareTo(Pair o) {
return o.get()-get();
}
}
public static void main(String[] args) throws Exception {
FastIO fs = new FastIO();
int t = fs.nextInt();
while(t -- >0)
{
int n = fs.nextInt();
String s = fs.next();
Pair[] a =new Pair[n];
long sum =0 ;
for (int i = 0;i<n;i++)
{
if (s.charAt(i) == 'L') {
a[i] = new Pair(i, n - i - 1);
sum+=i;
}
else {
a[i] = new Pair(n-i-1,i);
sum+=n-i-1;
}
}
Arrays.sort(a,(x,y)->x.compareTo(y));
long[] p= new long[n];
p[0] = Math.max(0,a[0].get());
for (int i = 1;i<n;i++)
p[i] =p[i-1] + Math.max(0,a[i].get());
for (int i = 0;i<n;i++)
System.out.print(p[i]+sum+ " ");
System.out.println();
}
fs.close();
}
//BeginCodeSnip{FastIO}
static class FastIO extends PrintWriter {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar;
private int numChars;
// standard input
public FastIO() {
this(in, System.out);
}
public FastIO(InputStream i, OutputStream o) {
super(o);
stream = i;
}
// file input
public FastIO(String i, String o) throws IOException {
super(new FileWriter(o));
stream = new FileInputStream(i);
}
// throws InputMismatchException() if previously detected end of file
int nextByte() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars == -1) {
return -1; // end of file
}
}
return buf[curChar++];
}
// to read in entire lines, replace c <= ' '
// with a function that checks whether c is a line break
public String next() {
int c;
do {
c = nextByte();
} while (c <= ' ');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > ' ');
return res.toString();
}
public int nextInt() { // nextLong() would be implemented similarly
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public double nextDouble() {
return parseDouble(next());
}
}
//EndCodeSnip
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
7d6788e0e5d4f0cfdfa18ffa1a8b0085
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Raw4 {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(reader.readLine());
for (int i = 0; i < t; i++) {
int n = Integer.parseInt(reader.readLine());
String s = reader.readLine();
char[] initRaw = s.toCharArray();
int medL, medR;
if (n == 1) System.out.println(0);
else {
if (n % 2 == 0) {
medR = n / 2;
medL = medR - 1;
} else {
medL = n / 2 - 1;
medR = medL + 2;
}
int idxL = 0;
int idxR = n - 1;
boolean isLeft = true;
char[] raw = initRaw;
long rawValue = 0;
for(int j = 0; j < n; j++){
if (raw[j] == 'L'){
rawValue += j;
}
else{
rawValue += (n - 1 - j);
}
}
int count = 0;
for (int k = 1; k <= n; k++) {
while (count < k){
if (isLeft && idxL <= medL){
if (raw[idxL] == 'L'){
raw[idxL] = 'R';
rawValue = rawValue - idxL + (n - 1 - idxL);
count++;
}
isLeft = false;
idxL++;
}
if (!isLeft && idxR >= medR){
if (count < k) {
if (raw[idxR] == 'R') {
raw[idxR] = 'L';
rawValue = rawValue - (n - 1 - idxR) + idxR;
count++;
}
isLeft = true;
idxR--;
}
}
else{
break;
}
}
System.out.print(rawValue + " ");
}
System.out.println();
}
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
eeeda5a8e6f95eed9f60cc8d7ec095cf
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class lINE
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int tc=sc.nextInt();
while(tc-->0)
{
int n=sc.nextInt();
String str=sc.next();
char ch[]=str.toCharArray();
long ans=0;
long[] changes=new long[n];
for(int i=0;i<n;i++)
{
if(ch[i]=='L')
ans+=i;
else
{
ans+=n-i-1;
}
if(ch[i]=='L')
{
changes[i]=n-i-1-i;
}
else
changes[i]=i-(n-i-1);
}
Arrays.sort(changes);
for(int i=n-1;i>=0;i--)
{
if(changes[i]>(long)0)
ans+=changes[i];
System.out.print(ans+" ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
118944a0fffe4f8f3dda9d7cab4a8253
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class D {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
//number of test cases
int n = sc.nextInt();
//LRRLL
//k j
//second input
for (int i = 0; i < n; i++) {
int changes = sc.nextInt();
String str = sc.nextToken();
long result = countSum(str);
char[] array = str.toCharArray();
int k = 0;
int j = str.length()-1;
int mid = str.length() / 2;
while(k <= j && changes >= 1) {
if(mid-k > j - mid) {
if(array[k] == 'R') {
k++;
}
else {
array[k] = 'R';
result -= k;
result += str.length()-k-1;
//result = countSum(new String(array));
changes--;
k++;
System.out.print(result +" ");
}
}
else {
if(array[j]== 'L') {
j--;
}
else{
array[j] = 'L';
//result = countSum(new String(array));
//take away prev value
result -= (str.length()-j -1);
result += j;
//result += j - (str.length()-j);
changes--;
j--;
System.out.print(result+" ");
}
}
}
if(changes > 0) {
while(changes > 0) {
System.out.print(result + " ");
changes--;
}
}
System.out.println();
}
}
public static long countSum(String str) {
long result = 0;
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) == 'L')
result += i;
else
result += str.length() - i-1;
}
return result;
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
34e749b77d14e352375c597a14a36414
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
class Pair{
int original,opesite,index;
public Pair(int original, int opesite,int index) {
this.original = original;
this.opesite = opesite;
this.index = index;
}
@Override
public String toString() {
return "Pair{" +
"original=" + original +
", opesite=" + opesite +
", index=" + index +
'}';
}
}
outer:
while (t-->0){
int n = in.nextInt();
char [] a =in.next().toCharArray();
ArrayList<Pair>persons = new ArrayList<>();
long ans=0;
for (int i = 0; i < n; i++) {
int original = 0,opesite=0;
if (a[i]=='R'){
original =(n-i-1);
opesite= i;
}else{
original = i;
opesite=(n-i-1);
}
ans+=original;
persons.add(new Pair(original,opesite,i));
}
// System.out.println(ans);
persons.sort((pair, t1)-> (t1.opesite-t1.original) -(pair.opesite-pair.original));
// System.out.println(persons);
for (int i = 0; i <n ; i++) {
int original = 0;
Pair person = persons.get(i);
if (person.opesite>person.original) {
ans += (person.opesite-person.original);
}
System.out.print(ans+" ");
}
System.out.println();
}
}
static boolean solution(String inputString) {
String [] parts = inputString.split("\\.");
if (parts.length!=4)return false;
else{
for(String s : parts){
try{
Integer num = Integer.parseInt(s);System.out.println(num);
if(num<0 ||num>255)return false;
}catch(Exception ex){
System.out.println("catch");
return false;
}
}
}
return true;
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
6faf63c40e15820420f3e54a2316e34d
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
class Pair{
int original,opesite,index;
public Pair(int original, int opesite,int index) {
this.original = original;
this.opesite = opesite;
this.index = index;
}
@Override
public String toString() {
return "Pair{" +
"original=" + original +
", opesite=" + opesite +
", index=" + index +
'}';
}
}
outer:
while (t-->0){
int n = in.nextInt();
char [] a =in.next().toCharArray();
ArrayList<Pair>persons = new ArrayList<>();
long ans=0;
for (int i = 0; i < n; i++) {
int original = 0,opesite=0;
if (a[i]=='R'){
original =(n-i-1);
opesite= i;
}else{
original = i;
opesite=(n-i-1);
}
ans+=original;
persons.add(new Pair(original,opesite,i));
}
// System.out.println(ans);
persons.sort(new Comparator<Pair>() {
@Override
public int compare(Pair pair, Pair t1) {
return (t1.opesite-t1.original) -(pair.opesite-pair.original);
}
});
// System.out.println(persons);
for (int i = 0; i <n ; i++) {
int original = 0;
Pair person = persons.get(i);
if (person.opesite>person.original) {
ans += (person.opesite-person.original);
}
System.out.print(ans+" ");
}
System.out.println();
}
}
static boolean solution(String inputString) {
String [] parts = inputString.split("\\.");
if (parts.length!=4)return false;
else{
for(String s : parts){
try{
Integer num = Integer.parseInt(s);System.out.println(num);
if(num<0 ||num>255)return false;
}catch(Exception ex){
System.out.println("catch");
return false;
}
}
}
return true;
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
a022d34289a14d1a2f78531d461ae8ee
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
/*
_oo0oo_
o8888888o
88" . "88
(| -_- |)
0\ = /0
___/`---'\___
.' \\| |// '.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' |_/ |
\ .-\__ '-' ___/-. /
___'. .' /--.--\ `. .'___
."" '< `.___\_<|>_/___.' >' "".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `_. \_ __\ /__ _/ .-` / /
=====`-.____`.___ \_____/___.-`___.-'=====
`=---='
*/
import java.util.*;
import java.math.*;
import java.io.*;
import java.lang.Math.*;
public class KickStart2020 {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
public static class Pair implements Comparable<Pair> {
public int index;
public long value;
public Pair(int index, long value) {
this.index = index;
this.value = value;
}
@Override
public int compareTo(Pair other) {
// multiplied to -1 as the author need descending sort order
if(other.index < this.index) return 1;
if(other.index > this.index) return -1;
return 0;
}
@Override
public String toString() {
return this.index + " " + this.value;
}
}
static int isPrime(long d) {
if (d == 1)
return -1;
for (int i = 2; i <= (long) Math.sqrt(d); i++) {
if (d % i == 0)
return i;
}
return -1;
}
static boolean isPali(String n) {
String s = n;
int l = 0;
int r = s.length() - 1;
while(l < r) if(s.charAt(l++) != s.charAt(r--)) return false;
return true;
}
static void decimalTob(long n, int k , int arr[], int i) {
arr[i] += (n % k);
n /= k;
if(n > 0) {
decimalTob(n, k, arr, i + 1);
}
}
static long powermod(long x, long y, long mod) {
if(y == 0) return 1;
long value = powermod(x, y / 2, mod);
if(y % 2 == 0) return (value * value) % mod;
return (value * (value * x) % mod) % mod;
}
static long power(long x, long y) {
if(y == 0) return 1;
long value = power(x, y / 2);
if(y % 2 == 0) return (value * value);
return value * value * x;
}
static int bS(int l, int r, int find, Integer arr[]) {
int ans = -1;
while(l <= r) {
int mid = (l + r) / 2;
if(arr[mid] >= find) {
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
return ans;
}
static void build(int index, int l, int r, int seqtree[],int arr[]) {
if(l == r) {
seqtree[index] = arr[l];
return;
}
int mid = (l + r) / 2;
build(2 * index + 1, l, mid, seqtree, arr);
build(2 * index + 2, mid + 1, r, seqtree, arr);
seqtree[index] = Math.max(seqtree[2 * index + 1], seqtree[2 * index + 2]);
}
static int query(int index, int low, int high, int l, int r, int seqtree[]) {
if(high < l || low > r) return Integer.MIN_VALUE;
if(l <= low && r >= high) {
return seqtree[index];
}
int mid = (low + high) / 2;
int left = query(2 * index + 1, low , mid, l, r, seqtree);
int right = query(2 * index + 2, mid + 1, high, l, r, seqtree);
return Math.max(left, right);
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
outerloop:
while(t-- > 0) {
int n = sc.nextInt();
String s = sc.next();
long sum = 0;
long arr[] = new long[n];
int l = 0;
int r = n - 1;
int k = 0;
for(int i = 0; i < n; i++) {
if(s.charAt(i) == 'L') {
sum += i;
}
else sum += n - i - 1;
}
arr[k] = sum;
k = 0;
while(l <= r) {
if(s.charAt(l) == 'L') {
sum += (n - l - 1);
sum -= l;
k++;
arr[k - 1] = sum;
}
if(k == n) break;
if(s.charAt(r) == 'R') {
sum += r;
sum -= (n - r - 1);
k++;
arr[k - 1] = sum;
}
l++;
r--;
}
for(int i = 0; i < n; i++) {
if(i != 0 && arr[i] == 0) {
out.print(arr[i - 1] + " ");
arr[i] = arr[i - 1];
}
else out.print(arr[i] + " ");
}
out.println();
}
out.close();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
cf03b71ef35d5999f2288bc13bd7775c
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
/***** ---> :) Vijender Srivastava (: <--- *****/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static FastReader sc =new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=(long)32768;
static StringBuilder sb = new StringBuilder();
/* start */
public static void main(String [] args)
{
int testcases = 1;
testcases = i();
// calc();
while(testcases-->0)
{
solve();
}
out.flush();
out.close();
}
static void solve()
{
int n = i();
char c[] = inputC();
ArrayList<Long> al = new ArrayList<>();
long cnt = 0;
for(int i=0;i<n;i++)
{
if(c[i]=='L') cnt+=i;
else cnt+=(long)(n-i-1);
}
// pl(cnt);
for(int i=0;i<n;i++)
{
if(c[i]=='L')
{
if(n-i-1>i) al.add((long)(n-i-1-i));
} else {
if(i>n-i-1) al.add((long)(i-(n-i-1)));
// pl(i-(n-i-1));
}
}
Collections.sort(al,Collections.reverseOrder());
for(int i=0;i<al.size();i++){
p((cnt+al.get(i))+" ");
cnt+=al.get(i);
}
for(int i=al.size();i<n;i++)
{
p(cnt+" ");
}
pl();
// pl(al);
}
/* end */
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
// print code start
static void p(Object o)
{
out.print(o);
}
static void pl(Object o)
{
out.println(o);
}
static void pl()
{
out.println("");
}
// print code end //
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static char[] inputC()
{
String s = sc.nextLine();
return s.toCharArray();
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static long[] putL(long a[]) {
long A[]=new long[a.length];
for(int i=0;i<a.length;i++) {
A[i]=a[i];
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static long power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) ;
y = y >> 1;
x = (x * x);
}
return res;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long[] sort(long a[]) {
ArrayList<Long> arr = new ArrayList<>();
for(long i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
static int[] sort(int a[])
{
ArrayList<Integer> arr = new ArrayList<>();
for(Integer i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
//pair class
private static class Pair implements Comparable<Pair> {
long first, second;
public Pair(long f, long s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair p) {
if (first < p.first)
return 1;
else if (first > p.first)
return -1;
else {
if (second > p.second)
return 1;
else if (second < p.second)
return -1;
else
return 0;
}
}
}
// segment t start
static long seg[] ;
static void build(long a[], int v, int tl, int tr) {
if (tl == tr) {
seg[v] = a[tl];
} else {
int tm = (tl + tr) / 2;
build(a, v*2, tl, tm);
build(a, v*2+1, tm+1, tr);
seg[v] = Math.min(seg[v*2] , seg[v*2+1]);
}
}
static long query(int v, int tl, int tr, int l, int r) {
if (l > r || tr < tl)
return Integer.MAX_VALUE;
if (l == tl && r == tr) {
return seg[v];
}
int tm = (tl + tr) / 2;
return (query(v*2, tl, tm, l, min(r, tm))+ query(v*2+1, tm+1, tr, max(l, tm+1), r));
}
static void update(int v, int tl, int tr, int pos, long new_val) {
if (tl == tr) {
seg[v] = new_val;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v*2, tl, tm, pos, new_val);
else
update(v*2+1, tm+1, tr, pos, new_val);
seg[v] = Math.min(seg[v*2] , seg[v*2+1]);
}
}
// segment t end //
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
dafebf0557dc28159e5fcc718f2ee960
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class ProblemB {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner();
StringBuilder sb = new StringBuilder();
int test = in.readInt();
while(test-->0){
int n = in.readInt();
String s = in.readLine();
long cur = 0;
for(int i = 0;i<n;i++){
if(s.charAt(i) == 'L')cur+=i;
else cur+=(n-1-i);
}
Deque<Integer>q = new LinkedList<>();
for(int i = 0;i<n;i++){
if(s.charAt(i) == 'L' && i<n/2)q.add(i);
if(s.charAt(i) == 'R' && i>=n/2)q.add(i);
}
long res[] = new long[n];
for(int i = 0;i<n;i++){
if(q.size() == 0){
res[i] = cur;
continue;
}
int l = q.peekFirst();
int r = q.peekLast();
long currl = s.charAt(l) == 'R'?n-1-l:l;
long currr = s.charAt(r) == 'R'?n-r-1:r;
if(n-1-l>r){
cur-=currl;
cur+= (n-1-l);
q.removeFirst();
}else{
cur-=currr;
cur+=r;
q.removeLast();
}
res[i] = cur;
}
for(long it:res)sb.append(it+" ");
sb.append("\n");
}
System.out.println(sb);
}
public static boolean isPalindrome(int val){
String s = String.valueOf(val);
for(int i = 0;i<s.length()/2;i++){
if(s.charAt(i) != s.charAt(s.length()-i-1))return false;
}
return true;
}
public static int sum(int n){
int sum = 0;
while(n>0){
sum+=(n%10);
n/=10;
}
return sum;
}
public static void swap(int a[],int i, int j){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
public static boolean subsequence(String cur,String t){
int pos = 0;
for(int i = 0;i<cur.length();i++){
if(pos<t.length() && cur.charAt(i) == t.charAt(pos)){
pos++;
}
}
return pos == t.length();
}
public static long gcd(long a, long b){
return b ==0 ?a:gcd(b,a%b);
}
static class Pair{
int x,y;
public Pair(int x,int y){
this.x = x;
this.y = y;
}
}
static class Scanner{
BufferedReader br;
StringTokenizer st;
public Scanner(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public String read()
{
while (st == null || !st.hasMoreElements()) {
try { st = new StringTokenizer(br.readLine()); }
catch (Exception e) { e.printStackTrace(); }
}
return st.nextToken();
}
public int readInt() { return Integer.parseInt(read()); }
public long readLong() { return Long.parseLong(read()); }
public double readDouble(){return Double.parseDouble(read());}
public String readLine() throws Exception { return br.readLine(); }
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
b1e56429c8ae53ca8346684f6d0a7d0b
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class ProblemB {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner();
StringBuilder sb = new StringBuilder();
int test = in.readInt();
while(test-->0){
int n = in.readInt();
String s = in.readLine();
long cur = 0;
for(int i = 0;i<n;i++){
if(s.charAt(i) == 'L')cur+=i;
else cur+=(n-1-i);
}
Deque<Integer>q = new LinkedList<>();
for(int i = 0;i<n;i++){
if(s.charAt(i) == 'L' && i<n/2)q.add(i);
if(s.charAt(i) == 'R' && i>=n/2)q.add(i);
}
long res[] = new long[n];
for(int i = 0;i<n;i++){
if(q.size() == 0){
res[i] = cur;
continue;
}
int l = q.peekFirst();
int r = q.peekLast();
if(l<n/2 && r>=n/2){
if(n-1-l>r){
cur-=l;
cur+=(n-1-l);
q.removeFirst();
}else{
cur-=(n-1-r);
cur+=r;
q.removeLast();
}
}else{
if(l<n/2){
cur-=l;
cur+=(n-l-1);
q.removeFirst();
}else{
cur-=(n-1-r);
cur+=r;
q.removeLast();
}
}
res[i] = cur;
}
for(long it:res)sb.append(it+" ");
sb.append("\n");
}
System.out.println(sb);
}
public static boolean isPalindrome(int val){
String s = String.valueOf(val);
for(int i = 0;i<s.length()/2;i++){
if(s.charAt(i) != s.charAt(s.length()-i-1))return false;
}
return true;
}
public static int sum(int n){
int sum = 0;
while(n>0){
sum+=(n%10);
n/=10;
}
return sum;
}
public static void swap(int a[],int i, int j){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
public static boolean subsequence(String cur,String t){
int pos = 0;
for(int i = 0;i<cur.length();i++){
if(pos<t.length() && cur.charAt(i) == t.charAt(pos)){
pos++;
}
}
return pos == t.length();
}
public static long gcd(long a, long b){
return b ==0 ?a:gcd(b,a%b);
}
static class Pair{
int x,y;
public Pair(int x,int y){
this.x = x;
this.y = y;
}
}
static class Scanner{
BufferedReader br;
StringTokenizer st;
public Scanner(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public String read()
{
while (st == null || !st.hasMoreElements()) {
try { st = new StringTokenizer(br.readLine()); }
catch (Exception e) { e.printStackTrace(); }
}
return st.nextToken();
}
public int readInt() { return Integer.parseInt(read()); }
public long readLong() { return Long.parseLong(read()); }
public double readDouble(){return Double.parseDouble(read());}
public String readLine() throws Exception { return br.readLine(); }
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
e59ce9cfc0a771398e9dde236bf4cc67
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class ProblemB {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner();
StringBuilder sb = new StringBuilder();
int test = in.readInt();
while(test-->0){
int n = in.readInt();
String s = in.readLine();
long cur = 0;
for(int i = 0;i<n;i++){
if(s.charAt(i) == 'L')cur+=i;
else cur+=(n-1-i);
}
Deque<Integer>q = new LinkedList<>();
for(int i = 0;i<n/2;i++){
if(s.charAt(i) == 'L')q.add(i);
}
for(int i = (n/2);i<n;i++){
if(s.charAt(i) == 'R')q.add(i);
}
long res[] = new long[n];
for(int i = 0;i<n;i++){
if(q.size() == 0){
res[i] = cur;
continue;
}
int l = q.peekFirst();
int r = q.peekLast();
if(l<n/2 && r>=n/2){
if(n-1-l>r){
cur-=l;
cur+=(n-1-l);
q.removeFirst();
}else{
cur-=(n-1-r);
cur+=r;
q.removeLast();
}
}else{
if(l<n/2){
cur-=l;
cur+=(n-l-1);
q.removeFirst();
}else{
cur-=(n-1-r);
cur+=r;
q.removeLast();
}
}
res[i] = cur;
}
for(long it:res)sb.append(it+" ");
sb.append("\n");
}
System.out.println(sb);
}
public static boolean isPalindrome(int val){
String s = String.valueOf(val);
for(int i = 0;i<s.length()/2;i++){
if(s.charAt(i) != s.charAt(s.length()-i-1))return false;
}
return true;
}
public static int sum(int n){
int sum = 0;
while(n>0){
sum+=(n%10);
n/=10;
}
return sum;
}
public static void swap(int a[],int i, int j){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
public static boolean subsequence(String cur,String t){
int pos = 0;
for(int i = 0;i<cur.length();i++){
if(pos<t.length() && cur.charAt(i) == t.charAt(pos)){
pos++;
}
}
return pos == t.length();
}
public static long gcd(long a, long b){
return b ==0 ?a:gcd(b,a%b);
}
static class Pair{
int x,y;
public Pair(int x,int y){
this.x = x;
this.y = y;
}
}
static class Scanner{
BufferedReader br;
StringTokenizer st;
public Scanner(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public String read()
{
while (st == null || !st.hasMoreElements()) {
try { st = new StringTokenizer(br.readLine()); }
catch (Exception e) { e.printStackTrace(); }
}
return st.nextToken();
}
public int readInt() { return Integer.parseInt(read()); }
public long readLong() { return Long.parseLong(read()); }
public double readDouble(){return Double.parseDouble(read());}
public String readLine() throws Exception { return br.readLine(); }
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
27b396f43fa71a9cc53043ee979dad5a
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class one {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
int t = obj.nextInt();
while (t != 0) {
int n = obj.nextInt();
String s = obj.next();
long arr[] = new long[n + 1];
solve(s, arr);
for (int i = 1; i <= n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
t--;
}
}
public static void solve(String s, long arr[]) {
int n = s.length();
long count = 0;
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L')
count += i;
else
count += (n - 1) - i;
}
arr[0] = count;
int k = 1;
int i = 0;
int j = n - 1;
while (i <= j) {
long countLeft = 0;
long countRight = 0;
if (s.charAt(i) == 'L') {
if (i < ((n - 1) - i))
countLeft = ((n - 1) - i) - i;
} else {
if (i > ((n - 1) - i))
countLeft = i - ((n - 1) - i);
}
if (s.charAt(j) == 'L') {
if (j < ((n - 1) - j))
countRight = ((n - 1) - j) - j;
} else {
if (j > ((n - 1) - j))
countRight = j - ((n - 1) - j);
}
boolean flag = true;
if (countLeft == 0 && countRight == 0) {
i++;
j--;
flag = false;
} else if (countLeft != 0 && countRight != 0) {
if (countLeft > countRight) {
count += countLeft;
i++;
} else {
count += countRight;
j--;
}
} else if (countLeft != 0) {
// i++;
// count += countLeft;
flag = false;
j--;
} else {
// j--;
// count += countRight;
i++;
flag = false;
}
if (flag) {
arr[k] = count;
k++;
}
}
while (k <= n) {
arr[k] = arr[k - 1];
k++;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
db9525de1762b7b86f9122fb5cbbeb53
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import jdk.swing.interop.SwingInterOpUtils;
import java.util.*;
import java.io.*;
public class Solution{
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
public static void main(String[] args) {
try {
FastReader in=new FastReader();
FastWriter out=new FastWriter();
int t=in.nextInt();
for(int i=1;i<t+1;i++) {
long n= in.nextLong();
TreeMap<Long,Long> tm=new TreeMap<>(Collections.reverseOrder());
String s= in.next();
long ans=0;
for(int k=0;k<n;k++){
if(s.charAt(k)=='L')
ans+=k;
else
ans+=n-1-k;
if(n%2==0){
if(k+1<=n/2 && s.charAt(k)=='L'){
long point=n-1-k;
if(tm.containsKey(point))
tm.put(point,tm.get(point)+1);
else
tm.put(point,(long)1);
}
else if(k+1>n/2 && s.charAt(k)=='R'){
long point=k;
if(tm.containsKey(point))
tm.put(point,tm.get(point)+1);
else
tm.put(point,(long)1);
}
}
else{
if(k+1<=n/2 && s.charAt(k)=='L'){
long point=n-1-k;
if(tm.containsKey(point))
tm.put(point,tm.get(point)+1);
else
tm.put(point,(long)1);
}
else if(k+1>n/2+1 && s.charAt(k)=='R'){
long point=k;
if(tm.containsKey(point))
tm.put(point,tm.get(point)+1);
else
tm.put(point,(long)1);
}
}
}
long arr[]=new long[(int)n];
int it=0;
// for(Map.Entry<Long,Long>e: tm.entrySet()){
// out.println(e.getKey()+" "+e.getValue());
// }
for(Map.Entry<Long,Long>e: tm.entrySet()){
long freq=e.getValue();
long value=e.getKey();
// System.out.println(value);
while(freq>0){
arr[it]=ans+value-(n-1-value);
ans+=value-(n-1-value);
// out.println(arr[it]);
it++;
freq--;
}
}
// out.println(it);
if(it!=0){
for(int k=it;k<n;k++){
arr[k]=arr[it-1];
}
}
else{
for(int k=it;k<n;k++){
arr[k]=ans;
}
}
for(int k=0;k<n;k++){
out.print(arr[k]+" ");
}
out.print("\n");
}
out.close();
} catch (Exception e) {
System.out.println("Something went wrong "+e);
return ;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
cbb5903aad21c1372c46df7b06e5d49e
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public final class Solution{
static long inf = 10000000000000l;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static long sum(int n){
long curr = (long) n;
return curr*(curr+1)/2;
}
static int inputInteger() throws IOException {
int n = Integer.parseInt(br.readLine().trim());
return n;
}
static long inputLong() throws IOException{
long n = Long.parseLong(br.readLine().trim());
return n;
}
static int[] inputLine(int n) throws IOException {
String[] arr = br.readLine().trim().split(" ");
int[] curr = new int[n];
for (int i = 0; i < n; i++) {
curr[i] = Integer.parseInt(arr[i]);
}return curr;
}
static int[] inputLine() throws IOException {
String[] arr = br.readLine().trim().split(" ");
int n = arr.length;
int[] curr = new int[n];
for (int i = 0; i < n; i++) {
curr[i] = Integer.parseInt(arr[i]);
}return curr;
}
static String inputString() throws IOException {
return br.readLine().trim();
}
static int binSearchJustGreaterOrEqual(int[] arr, int l, int r, int x){
int res = -1;
while(l<=r){
int mid = l + (r-l)/2;
if(arr[mid] >= x){
res = mid;
r = mid - 1;
}else{
l = mid + 1;
}
}
return res;
}
static int binSearchJustSmallerOrEqual(int[] arr, int l, int r, int x){
int res = -1;
while(l<=r){
int mid = l + (r-l)/2;
if(arr[mid] <= x){
res = mid;
l = mid + 1;
}else{
r = mid - 1;
}
}
return res;
}
static int solve(int n, int[] arr){
int res = 0;
Arrays.sort(arr);
res += arr[0];
int prev = arr[0];
for(int i = 1; i<n; i++){
if(arr[i] <= prev){
prev++;
res += prev;
}else{
res += arr[i];
prev = arr[i];
}
}
return res;
}
public static void main(String[] args) throws IOException {
int t = 1;
t = inputInteger();
while(t-- != 0){
int n = inputInteger();
char[] arr = inputString().toCharArray();
long res = 0;
for (int i = 0; i < n; i++) {
if(arr[i] == 'L'){
res += i;
}else{
res += n-(i+1);
}
}
int l = 0;
int r = n-1;
for (int i = 1; i <= n; i++) {
while(l<n/2){
if(arr[l] == 'L'){
break;
}
l++;
}
while(r>(n-1)/2){
if(arr[r] == 'R'){
break;
}
r--;
}
long lb = -1;
long rb = -1;
if(l<n/2 && r>(n-1)/2){
lb = res - l + (n-(l+1));
rb = res - (n-(r+1)) + r;
if(lb > rb){
l++;
}else{
r--;
}
}
else if(l<n/2){
lb = res - l + (n-(l+1));
l++;
}
else if(r>(n-1)/2){
rb = res - (n-(r+1)) + r;
r--;
}
res = Math.max(res, Math.max(lb, rb));
System.out.print(res+" ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
06893b32499e6cff98f9e0dabe970bde
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public final class Solution{
static long inf = 10000000000000l;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static long sum(int n){
long curr = (long) n;
return curr*(curr+1)/2;
}
static int inputInteger() throws IOException {
int n = Integer.parseInt(br.readLine().trim());
return n;
}
static long inputLong() throws IOException{
long n = Long.parseLong(br.readLine().trim());
return n;
}
static int[] inputLine(int n) throws IOException {
String[] arr = br.readLine().trim().split(" ");
int[] curr = new int[n];
for (int i = 0; i < n; i++) {
curr[i] = Integer.parseInt(arr[i]);
}return curr;
}
static int[] inputLine() throws IOException {
String[] arr = br.readLine().trim().split(" ");
int n = arr.length;
int[] curr = new int[n];
for (int i = 0; i < n; i++) {
curr[i] = Integer.parseInt(arr[i]);
}return curr;
}
static String inputString() throws IOException {
return br.readLine().trim();
}
static int binSearchJustGreaterOrEqual(int[] arr, int l, int r, int x){
int res = -1;
while(l<=r){
int mid = l + (r-l)/2;
if(arr[mid] >= x){
res = mid;
r = mid - 1;
}else{
l = mid + 1;
}
}
return res;
}
static int binSearchJustSmallerOrEqual(int[] arr, int l, int r, int x){
int res = -1;
while(l<=r){
int mid = l + (r-l)/2;
if(arr[mid] <= x){
res = mid;
l = mid + 1;
}else{
r = mid - 1;
}
}
return res;
}
static int solve(int n, int[] arr){
int res = 0;
Arrays.sort(arr);
res += arr[0];
int prev = arr[0];
for(int i = 1; i<n; i++){
if(arr[i] <= prev){
prev++;
res += prev;
}else{
res += arr[i];
prev = arr[i];
}
}
return res;
}
public static void main(String[] args) throws IOException {
int t = 1;
t = inputInteger();
while(t-- != 0){
int n = inputInteger();
char[] arr = inputString().toCharArray();
long res = 0;
for (int i = 0; i < n; i++) {
if(arr[i] == 'L'){
res += i;
}else{
res += n-(i+1);
}
}
int l = 0;
int r = n-1;
if(n%2 != 0){
for (int i = 1; i <= n; i++) {
while(l<n/2){
if(arr[l] == 'L'){
break;
}
l++;
}
while(r>n/2){
if(arr[r] == 'R'){
break;
}
r--;
}
long lb = -1;
long rb = -1;
if(l<n/2 && r>n/2){
lb = res - l + (n-(l+1));
rb = res - (n-(r+1)) + r;
if(lb > rb){
l++;
}else{
r--;
}
}
else if(l<n/2){
lb = res - l + (n-(l+1));
l++;
}
else if(r>n/2){
rb = res - (n-(r+1)) + r;
r--;
}
res = Math.max(res, Math.max(lb, rb));
System.out.print(res+" ");
}
}else{
for (int i = 1; i <= n; i++) {
while(l<n/2){
if(arr[l] == 'L'){
break;
}
l++;
}
while(r>=n/2){
if(arr[r] == 'R'){
break;
}
r--;
}
long lb = -1;
long rb = -1;
if(l<n/2 && r>=n/2){
lb = res - l + (n-(l+1));
rb = res - (n-(r+1)) + r;
if(lb > rb){
l++;
}else{
r--;
}
}
else if(l<n/2){
lb = res - l + (n-(l+1));
l++;
}
else if(r>=n/2){
rb = res - (n-(r+1)) + r;
r--;
}
res = Math.max(res, Math.max(lb, rb));
System.out.print(res+" ");
}
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
bfd40eaa1828a4af64d5563bc08854a1
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.stream.IntStream;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
// public static boolean areSame(ArrayList<Integer> list)
// {
// Integer first = list.get(0);
// for (int i=1; i<list.size(); i++)
// if (list.get(i) != first)
// return false;
// return true;
// }
// public static int maxOfArray( int[] array, int idx, int max ) {
// if ( idx == array.length ) {
// return max;
// }
// if ( array[idx] > max ) {
// max = array[idx];
// }
// return maxOfArray( array, idx + 1, max );
// }
// public static int minOfArray( int[] array, int idx, int min ) {
// if ( idx == array.length ) {
// return min;
// }
// if ( min > array[idx] ) {
// min = array[idx];
// }
// return minOfArray( array, idx + 1, min );
// }
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static int[] rev(int a[], int n)
{
int[] b = new int[n];
int j = n;
for (int i = 0; i < n; i++) {
b[j - 1] = a[i];
j = j - 1;
}
return b;
}
public static String sortString(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
public static void main(String[] args) throws IOException
{
// your code goes here
FastReader sc = new FastReader();
Scanner sca=new Scanner(System.in);
int t=sc.nextInt();
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
while(t--!=0){
int n=sc.nextInt();
char s[]=sc.next().toCharArray();
ArrayList<Long> arr=new ArrayList<>();
int i; long sum=0;
for(i=0;i<n;i++)
{
if(s[i]=='L')
sum+=i;
else
sum+=(n-1-i);
}
int l=0,r=n-1,res=0;
while(l<=r)
{
if(s[l]=='L'){
sum+=n-l-1;
sum-=l;
arr.add(sum);
res++;
}
if(s[r]=='R'){
sum+=r;
sum-=n-r-1;
arr.add(sum);
res++;
}
l++; r--;
}
for(i=res;i<n;i++)arr.add(sum);
for(long e:arr) System.out.print(e+" ");
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
e803023a8fa5920434e35ba7d6b0df34
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
public class D {
// global variables - must be static
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static char[] people;
static int n;
public static void main(String[] args) throws IOException {
// testcase
int t = Integer.parseInt(br.readLine());
for (int testcase = 0; testcase < t; testcase++) {
// input
String[] strings = br.readLine().split(" ");
n = Integer.parseInt(strings[0]);
////////////////////////////////
// fight!
people = br.readLine().toCharArray();
long value = 0;
for (int space = 0; space < Math.ceil((double) n / 2.0); space++) {
// left
if (people[0 + space] == 'L') {
value += space;
} else { // 'R'
value += n - space - 1;
}
// appropriate break
if (space * 2 == n - 1) break;
// right
if (people[n - 1 - space] == 'R') {
value += space;
} else { // 'L'
value += n - 1 - space;
}
}
changeAndPrintValue(0, value, 0, false);
bw.write("\n");
bw.flush();
/////////////////////////////////
}
}
// functions & classes - must be static
static void changeAndPrintValue(int alreadyChangedTimes, long value, int spaceStart, boolean noChangeAnymore) throws IOException {
if (alreadyChangedTimes == n) {
return;
}
// change once
for (int space = spaceStart; space < Math.ceil((double) n / 2.0); space++) {
if (noChangeAnymore)
break;
// left
if (people[space] == 'L') {
value -= space;
value += n - space - 1;
people[space] = 'R';
break;
}
// appropriate break
if (space * 2 == n - 1) {
noChangeAnymore = true;
break;
}
// right
if (people[n - 1 - space] == 'R') {
value -= space;
value += n - 1 - space;
people[n - 1 - space] = 'L';
break;
}
spaceStart = space + 1;
}
bw.write(value + " ");
changeAndPrintValue(alreadyChangedTimes + 1, value, spaceStart, noChangeAnymore);
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
f0ebd6e1e215f114ca0252289df255de
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class D {
static PrintWriter out;
static Kioken sc;
static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null;
public static void main(String[] args) throws FileNotFoundException {
if (checkOnlineJudge) {
out = new PrintWriter("E:/CF_V2/output.txt");
sc = new Kioken(new File("E:/CF_V2/input.txt"));
} else {
out = new PrintWriter((System.out));
sc = new Kioken();
}
int tt = 1;
tt = sc.nextInt();
while (tt-- > 0) {
solve();
}
out.flush();
out.close();
}
public static void solve() {
int n = sc.nextInt();
String s = sc.nextLine();
int[] arr = new int[n];
for(int i = 0; i < n; i++){
if(s.charAt(i) == 'L'){
arr[i] = i;
}else{
arr[i] = n - 1 - i;
}
}
long[] prefix = new long[n+1];
for(int i = 0; i < n; i++){
prefix[i+1] = prefix[i] + arr[i];
}
long totalSum = prefix[n];
List<Long> values = new ArrayList<>();
int i = 0, e = n - 1;
while(i <= e){
// out.println(" tt " + totalSum);
long sum1 = totalSum;
long sum2 = totalSum;
while(i <= e){
if(s.charAt(i) == 'L' && i > n - 1 - i){
i++;
}else if(s.charAt(i) == 'R' && n - 1 - i > i){
i++;
}else break;
}
while(i <= e){
if(s.charAt(e) == 'L' && e > n - 1 - e){
e--;
}else if(s.charAt(e) == 'R' && n - 1 - e > e){
e--;
}else break;
}
// out.println(e + " " + i + totalSum + " " );
if(i <= e){
sum1 = sum1 - arr[i] + (Math.max(i, n - 1 - i));
sum2 = sum2 - arr[e] + Math.max(e, n - 1 - e);
if(sum1 > sum2){
i++;
values.add(sum1);
totalSum = sum1;
}else{
e--;
values.add(sum2);
totalSum = sum2;
}
}
}
// out.println(values);
for(int k = 0; k < n; k++){
if(k < values.size()){
out.print(values.get(k) + " ");
}else{
if(values.size() > 0){
out.print(values.get(values.size() - 1) + " ");
}else{
out.print(prefix[n] + " ");
}
}
}
// out.println(values);
out.println();
}
public static long gcd(long a, long b) {
while (b != 0) {
long rem = a % b;
a = b;
b = rem;
}
return a;
}
static long MOD = 1000000007;
static void reverseSort(int[] arr){List<Integer> list = new ArrayList<>();for (int i=0; i<arr.length; i++){list.add(arr[i]);}Collections.sort(list, Collections.reverseOrder());for (int i = 0; i < arr.length; i++){arr[i] = list.get(i);}}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void sort(long[] a){
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class Kioken {
// FileInputStream br = new FileInputStream("input.txt");
BufferedReader br;
StringTokenizer st;
Kioken(File filename) {
try {
FileReader fr = new FileReader(filename);
br = new BufferedReader(fr);
st = new StringTokenizer("");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
Kioken() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean hasNext() {
String next = null;
try {
next = br.readLine();
} catch (Exception e) {
}
if (next == null || next.length() == 0) {
return false;
}
st = new StringTokenizer(next);
return true;
}
public int[] readArrayInt(int n){
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = nextInt();
}
return arr;
}
public long[] readArrayLong(int n){
long[] arr = new long[n];
for(int i = 0; i < n; i++){
arr[i] = nextLong();
}
return arr;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
0e674c70c89d1e1f8f09ba71ba0fd6d3
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int TC = parse(br.readLine());
while (TC-- > 0) {
int n = parse(br.readLine()); // 사람 수
String s = br.readLine();
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
long score = 0;
for (int i = 0; i < n; i++) {
if (n % 2 == 1 && i == n / 2) {
score += i;
} else if (s.charAt(i) == 'L') {
score += i;
if (i < n / 2) {
pq.offer(n - 2 * i - 1);
}
} else if (s.charAt(i) == 'R') {
score += n - i - 1;
if (i >= n / 2) {
pq.offer(2 * i - n + 1);
}
}
}
for (int i = 0; i < n; i++) {
if (!pq.isEmpty()) {
score += pq.poll();
}
System.out.print(score + " ");
}
System.out.println();
}
}
private static int parse(String s) {
return Integer.parseInt(s);
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
e9d5d149f548cc255f776f69d9b23c7d
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
while(N-- != 0) {
int k = in.nextInt();
String s = in.next();
long cnt = 0;
List<Integer> list = new ArrayList<>();
for(int i = 0;i < s.length();i++) {
char c = s.charAt(i);
if(c == 'L') {
cnt += i;
if(i < s.length() - i - 1)
list.add(s.length() - i - 1 - i);
}
else {
cnt += s.length() - 1 - i;
if(s.length() - 1 - i < i)
list.add(i - s.length() + i + 1 );
}
}
// System.out.println("cnt=" + cnt);
Collections.sort(list);
Collections.reverse(list);
long add = 0;
for(int i = 0;i < k;i++) {
if(i < list.size())
add += list.get(i);
System.out.print((cnt + add) + " ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
27db7af4232b3e59794d00ed997e3350
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class cf {
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static boolean isok(long x, long h, long k) {
long sum = 0;
if (h > k) {
long t1 = h - k;
long t = t1 * k;
sum += (k * (k + 1)) / 2;
sum += t - (t1 * (t1 + 1) / 2);
} else {
sum += (h * (h + 1)) / 2;
}
if (sum < x) {
return true;
}
return false;
}
public static boolean binary_search(long[] a, long k) {
long low = 0;
long high = a.length - 1;
long mid = 0;
while (low <= high) {
mid = low + (high - low) / 2;
if (a[(int) mid] == k) {
return true;
} else if (a[(int) mid] < k) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return false;
}
public static long lowerbound(long a[], long ddp) {
long low = 0;
long high = a.length;
long mid = 0;
while (low < high) {
mid = low + (high - low) / 2;
if (a[(int) mid] == ddp) {
return mid;
}
if (a[(int) mid] < ddp) {
low = mid + 1;
} else {
high = mid;
}
}
// if(low + 1 < a.length && a[(int)low + 1] <= ddp){
// low++;
// }
if (low == a.length && low != 0) {
low--;
return low;
}
if (a[(int) low] > ddp && low != 0) {
low--;
}
return low;
}
public static long upperbound(long a[], long ddp) {
long low = 0;
long high = a.length;
long mid = 0;
while (low < high) {
mid = low + (high - low) / 2;
if (a[(int) mid] <= ddp) {
low = mid + 1;
} else {
high = mid;
}
}
if (low == a.length) {
return a.length - 1;
}
return low;
}
// public static class pair implements Comparable<pair> {
// long w;
// long h;
// public pair(long w, long h) {
// this.w = w;
// this.h = h;
// }
// public int compareTo(pair b) {
// if (this.w != b.w)
// return (int) (this.w - b.w);
// else
// return (int) (this.h - b.h);
// }
// }
public static class pair {
long w;
long h;
public pair(long w, long h) {
this.w = w;
this.h = h;
}
}
public static class trinary {
long a;
long b;
long c;
public trinary(long a, long b, long c) {
this.a = a;
this.b = b;
this.c = c;
}
}
public static long lowerboundforpairs(pair a[], long pr) {
long low = 0;
long high = a.length;
long mid = 0;
while (low < high) {
mid = low + (high - low) / 2;
if (a[(int) mid].w <= pr) {
low = mid + 1;
} else {
high = mid;
}
}
// if(low + 1 < a.length && a[(int)low + 1] <= ddp){
// low++;
// }
// if(low == a.length && low != 0){
// low--;
// return low;
// }
// if(a[(int)low].w > pr && low != 0){
// low--;
// }
return low;
}
public static pair[] sortpair(pair[] a) {
Arrays.sort(a, new Comparator<pair>() {
public int compare(pair p1, pair p2) {
return (int) p1.w - (int) p2.w;
}
});
return a;
}
public static boolean ispalindrome(String s) {
long i = 0;
long j = s.length() - 1;
boolean is = false;
while (i < j) {
if (s.charAt((int) i) == s.charAt((int) j)) {
is = true;
i++;
j--;
} else {
is = false;
return is;
}
}
return is;
}
public static void sort(long[] arr) {
ArrayList<Long> a = new ArrayList<>();
for (long i : arr) {
a.add(i);
}
Collections.sort(a);
for (int i = 0; i < a.size(); i++) {
arr[i] = a.get(i);
}
}
public static void sortForObjecttypes(Long[] arr) {
ArrayList<Long> a = new ArrayList<>();
for (Long i : arr) {
a.add(i);
}
Collections.sort(a);
for (int i = 0; i < a.size(); i++) {
arr[i] = a.get(i);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
void readArr(int[] ar, int n) {
for (int i = 0; i < n; i++) {
ar[i] = nextInt();
}
}
}
public static void solve(FastReader sc, PrintWriter w) throws Exception {
int n = sc.nextInt();
String s = sc.nextLine();
StringBuilder sb = new StringBuilder();
Vector<Integer> r = new Vector<>();
Vector<Integer> l = new Vector<>();
long ans = 0;
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L') {
l.add(i);
ans += i;
} else {
r.add(i);
ans += (n - i - 1);
}
}
for (int i = 0; i < n; i++) {
long temp = ans;
if (temp < ans) {
System.out.println(ans);
continue;
}
if (r.size() == 0 && l.size() == 0) {
System.out.println(temp);
} else if (r.size() == 0) {
int tl = l.get(0);
temp -= tl;
temp += (n - 1 - tl);
l.remove(0);
} else if (l.size() == 0) {
int tr = r.get(r.size() - 1);
temp -= (n - 1 - tr);
temp += tr;
r.remove(r.size() - 1);
} else {
int tr = r.get(r.size() - 1);
int tl = l.get(0);
if (tr > n - 1 - tl) {
temp -= (n - 1 - tr);
temp += tr;
r.remove(r.size() - 1);
} else {
temp -= tl;
temp += (n - 1 - tl);
l.remove(0);
}
}
ans = Math.max(temp, ans);
sb.append(ans + " ");
}
System.out.println(sb.toString());
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter w = new PrintWriter(System.out);
long o = sc.nextLong();
while (o > 0) {
solve(sc, w);
o--;
}
w.close();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
2d131d4c9a6952a2253ba9bf59f29f6d
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class test {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
String s = br.readLine();
long sum = 0;
for(int i = 0; i < n; i++){
if(s.charAt(i) == 'L'){
sum += i;
}
else{
sum += (n - (i + 1));
}
}
int k = 0;
for(int i = 0, j = n - 1; i < n / 2; i++, j--){
if(s.charAt(i) == 'L'){
sum -= i;
sum += (n - (i + 1));
if(k > 0){
pw.print(" ");
}
k++;
pw.print(sum);
}
if(s.charAt(j) == 'R')
{
sum -= (n - (j + 1));
sum += j;
if(k > 0){
pw.print(" ");
}
k++;
pw.print(sum);
}
}
for (int i = k + 1; i <= n; i++) {
if(k > 0){
pw.print(" ");
}
k++;
pw.print(sum);
}
pw.println();
}
pw.flush();
pw.close();
br.close();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
1dac949c0463e93b7172803b9261112a
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class D817 {
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader fs = new FastReader();
int T = fs.nextInt();
while (T-- > 0) {
int n = fs.nextInt();
String s = fs.next();
char[] sr = s.toCharArray();
long ans=0;
int k=1;
int ind=0;
for(int m=1;m<=n;m++) {
for(int i=ind;i<n/2;i++) {
if(sr[i]=='L') {
if(k>0) {
k--;
if(m>1) {
ans-=i;
}
ans+=n-i-1;
ind=i;
sr[i]='R';
}
else {
if(m==1)
ans+=i;
else {
break;
}
}
}
else {
if(m==1)
ans+=n-i-1;
}
//println(ans);
if(sr[n-i-1]=='R') {
if(k>0) {
k--;
if(m>1) {
ans-=i;
}
ans+=n-i-1;
ind=i+1;
sr[n-i-1]='L';
}
else {
if(m==1)
ans+=i;
else break;
}
}
else {
if(m==1)
ans+=n-i-1;
}
//println(ans);
}
if(n%2==1 && m==1) {
ans+=n/2;
}
print(ans+" ");
k=1;
}
newln();
}
}
static void println(Object a) {
System.out.println(a);
}
static void newln() {
System.out.print("\n");
}
static void print(Object a) {
System.out.print(a);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if (st.hasMoreTokens()) {
str = st.nextToken("\n");
} else {
str = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] nextArr(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = this.nextInt();
}
return arr;
}
long[] nextLongArr(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = this.nextLong();
}
return arr;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
97c3641fad9c351eec1bc2cd10a271f2
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
/*
بسم الله الرحمن الرحيم
/$$$$$ /$$$$$$ /$$ /$$ /$$$$$$
|__ $$ /$$__ $$ |$$ |$$ /$$__ $$
| $$| $$ \ $$| $$|$$| $$ \ $$
| $$| $$$$$$$$| $$ / $$/| $$$$$$$$
/ $$ | $$| $$__ $$ \ $$ $$/ | $$__ $$
| $$ | $$| $$ | $$ \ $$$/ | $$ | $$
| $$$$$$/| $$ | $$ \ $/ | $$ | $$
\______/ |__/ |__/ \_/ |__/ |__/
/$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$
| $$__ $$| $$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$$ /$$$| $$_____/| $$__ $$
| $$ \ $$| $$ \ $$| $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$$$ /$$$$| $$ | $$ \ $$
| $$$$$$$/| $$$$$$$/| $$ | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$ | $$$$$$$/
| $$____/ | $$__ $$| $$ | $$| $$|_ $$| $$__ $$| $$__ $$| $$ $$$| $$| $$ $$$| $$| $$__/ | $$__ $$
| $$ | $$ \ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$| $$\ $ | $$| $$ | $$ \ $$
| $$ | $$ | $$| $$$$$$/| $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$| $$ \/ | $$| $$$$$$$$| $$ | $$
|__/ |__/ |__/ \______/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/ |__/|________/|__/ |__/
TREESET.HIGHER() METHOD GIVES THE LEAST STRICTLY GREATER VALUE THAN THE PARAMETER;
IF NO SUCH ELEMENT EXISTS IT RETURN NULL
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class D817 {
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
try {
// FAST SORT TO PASS---->arr = Arrays.stream(arr).boxed().sorted().mapToInt($->$).toArray();
// Scanner sc=new Scanner(System.in);
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n=sc.nextInt();
char[] arr=sc.next().toCharArray();
if(n==1){
System.out.println(0);
continue;
}
int l=0;
int r=n-1;
long[] ans=new long[n];
long sum=0;
for(int i=0;i<n;i++){
if(arr[i]=='L')sum+=i;
else sum+=n-1-i;
}
// System.out.println(sum);
int i=0;
while(l<n/2 && r>=n/2){
if(l<n/2 && arr[l]=='L'){
sum-=Long.valueOf(l);
sum+=Long.valueOf(n-1-l);
ans[i]=sum;
i++;
arr[l]='R';
}
l++;
if(r>=n/2 && arr[r]=='R'){
sum-=Long.valueOf(n-1-r);
sum+=Long.valueOf(r);
arr[r]='L';
ans[i]=sum;
i++;
}
r--;
}
while(l<n/2){
while(l<n/2 && arr[l]=='R')l++;
if(l<n/2 && arr[l]=='L'){
sum-=Long.valueOf(l);
sum+=Long.valueOf(n-1-l);
arr[l]='R';
l++;
ans[i]=sum;
i++;
}
}
while(r>=n/2){
while(r>=n/2 && arr[r]=='L')r--;
if(r>=n/2 && arr[r]=='R'){
sum-=Long.valueOf(n-1-r);
sum+=Long.valueOf(r);
arr[r]='L';
ans[i]=sum;
r--;
i++;
}
}
if(i==0){
ans[i]=sum;
for(int j=1;j<n;j++)ans[j]=ans[j-1];
}
else{
for(int j=i;j<n;j++)ans[j]=ans[j-1];
}
for(long ii:ans)System.out.print(ii+" ");
System.out.println();
/*int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}*/
/*long[] arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextLong();
}*/
//System.out.println(gcd_int(4, 6));
}
} catch (Exception e) {
return;
}
}
public static int lowerbound(int[] ar,int k)
{
int s=0;
int e=ar.length;
while (s !=e)
{
int mid = s+e>>1;
if (ar[mid] <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.length)
{
return -1;
}
return s;
}
public static class pair {
int ff;
int ss;
pair(int ff, int ss) {
this.ff = ff;
this.ss = ss;
}
}
static int BS(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] == element) {
return low;
} else if (arr[high] == element) {
return high;
}
return -1;
}
static int lower_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] >= element) {
return low;
} else if (arr[high] >= element) {
return high;
}
return -1;
}
static int upper_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] <= element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] > element) {
return low;
} else if (arr[high] > element) {
return high;
}
return -1;
}
public static int upperbound(long[] arr, int k) {
int s = 0;
int e = arr.length;
while (s != e) {
int mid = s + e >> 1;
if (arr[mid] <= k) {
s = mid + 1;
} else {
e = mid;
}
}
if (s == arr.length) {
return -1;
}
return s;
}
public static long pow(long x,long y,long mod){
if(x==0)return 0l;
if(y==0)return 1l;
//(x^y)%mod
if(y%2l==1l){
return ((x%mod)*(pow(x,y-1l,mod)%mod))%mod;
}
return pow(((x%mod)*(x%mod))%mod,y/2l,mod);
}
public static long gcd_long(long a, long b) {
// a/b,a-> dividant b-> divisor
if (b == 0)
return a;
return gcd_long(b, a % b);
}
public static int gcd_int(int a, int b) {
// a/b,a-> dividant b-> divisor
if (b == 0)
return a;
return gcd_int(b, a % b);
}
public static int lcm(int a, int b) {
int gcd = gcd_int(a, b);
return (a * b) / gcd;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble(){
return Double.valueOf(Integer.parseInt(next()));
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
Long nextLong() {
return Long.parseLong(next());
}
}
public static boolean contains(String main, String Substring) {
boolean flag=false;
if(main==null && main.trim().equals("")) {
return flag;
}
if(Substring==null) {
return flag;
}
char fullstring[]=main.toCharArray();
char sub[]=Substring.toCharArray();
int counter=0;
if(sub.length==0) {
flag=true;
return flag;
}
for(int i=0;i<fullstring.length;i++) {
if(fullstring[i]==sub[counter]) {
counter++;
} else {
counter=0;
}
if(counter==sub.length) {
flag=true;
return flag;
}
}
return flag;
}
}
/*
* public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){
* return true; } if(n<1 || m<1 || k<0){ return false; } boolean
* tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; }
* return false; }
*/
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
de78c2250165e98f17bb82bcf4fbe3c1
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class abc
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc =new Scanner(System.in);
int n1=sc.nextInt();
while(n1-->0){
int n=sc.nextInt();
String s=sc.next();
int[]arr=new int[n];
long count=0;
for(int i=0;i<n;i++){
if(s.charAt(i)=='L')
arr[i]=i;
else
arr[i]=n-i-1;
count+=arr[i];
}
int i=0;
int j=n-1;
PriorityQueue<Integer>pq=new PriorityQueue<>(Collections.reverseOrder());
for(i=0;i<n/2;i++)
if(s.charAt(i)=='L')
pq.add(n-i-i-1);
else
pq.add(0);
for(i=n/2;i<n;i++)
if(s.charAt(i)=='R')
pq.add(n-2*(n-i-1)-1);
else
pq.add(0);
if(pq.size()!=0)
while(!pq.isEmpty()){
count+=pq.poll();
System.out.print(count+" ");
}
else
System.out.println("0");
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
96a88ddab42fa590980e0b4c847af7ce
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) throws IOException{
FastReader s = new FastReader();
int t = s.nextInt();
while(t-->0){
int n = s.nextInt();
char[] ch = s.next().toCharArray();
int[] see = new int[n];
long ans = 0;
for(int i = 0; i < n; i++){
if(ch[i] == 'L'){
see[i] = i;
ans += see[i];
}
else{
see[i] = n - i - 1;
ans += see[i];
}
}
int changes = 0;
for(int lo = 0, hi = n - 1; lo < hi; lo++, hi--){
if(ch[lo] == 'L'){
ans -= see[lo];
see[lo] = n - lo - 1;
ans += see[lo];
System.out.print(ans);
System.out.print(" ");
changes++;
}
if(ch[hi] == 'R'){
ans -= see[hi];
see[hi] = hi;
ans += see[hi];
System.out.print(ans);
System.out.print(" ");
changes++;
}
}
for(int i = changes; i < n; i++){
System.out.print(ans);
System.out.print(" ");
}
System.out.println();
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() throws IOException{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() throws IOException{ return Integer.parseInt(next()); }
long nextLong() throws IOException{ return Long.parseLong(next()); }
double nextDouble() throws IOException{return Double.parseDouble(next());}
String nextLine() throws IOException{
String str = "";
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
return str;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
08fb74828b1f7fa9cee6c576226bc1c0
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
/*input
Prateek Singh
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.*;
import java.util.*;
public class codeforces {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]) {
if (System.getProperty("ONLINE_JUDGE") == null) {
// Input is a file
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println("Error");
}
} else {
// Input is System.in
}
FastReader sc = new FastReader();
// Scanner sc = new Scanner(System.in);
//System.out.println(java.time.LocalTime.now());
StringBuilder sb = new StringBuilder();
int t = sc.nextInt();
while (t > 0) {
int n = sc.nextInt();
int[] arr = new int[n];
int max = n - 1;
for (int i = 0; i < (n + 1) / 2; i++) {
arr[i] = max--;
}
max ++;
if (n % 2 == 1)max++;
for (int i = (n + 1) / 2; i < n; i++)arr[i] = max++;
//System.out.println(Arrays.toString(arr));
int[] a = new int[n];
long[] dif = new long[n];
String s = sc.next();
//Map<Integer, Integer> map = new HashMap<>();
long sum = 0;
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L')a[i] = i;
else a[i] = n - 1 - i;
//map.put(a[i], i);
sum += a[i];
dif[i] = arr[i] - a[i];
}
Arrays.sort(dif);
for (int i = n - 1; i >= 0; i--) {
sum += dif[i];
sb.append(sum + " ");
}
// int k = 0;
// int i = 0;
// int j = n - 1;
// while (k != n && i <= j) {
// if (a[i] == arr[i] && a[j] == arr[j]) {
// i++;
// j--;
// } else {
// if (a[i] != arr[i] && a[j] != arr[j]) {
// if (arr[j] - a[j] > arr[i] - a[i]) {
// sum += (arr[j] - a[j]);
// j--;
// k++;
// sb.append(sum + " ");
// } else {
// sum += (arr[i] - a[i]);
// i++;
// k++;
// sb.append(sum + " ");
// }
// } else if (a[j] != arr[j]) {
// sum += (arr[j] - a[j]);
// j--;
// k++;
// sb.append(sum + " ");
// } else if (a[i] != arr[j]) {
// sum += (arr[i] - a[i]);
// i++;
// k++;
// sb.append(sum + " ");
// }
// }
// }
// while (k < n) {
// sb.append(sum + " ");
// k++;
// }
sb.append("\n");
t--;
}
System.out.println(sb);
}
public static void shuffleArray(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
public static boolean isPowerOfTwo(long x) {
return x != 0 && ((x & (x - 1)) == 0);
}
static class Pair {
int a;
int b;
Pair (int a, int b) { //constructor
this.a = a;
this.b = b;
}
}
//////////nCr////////////////////////////////////////
///////// SUM OF EACH DIGIT OF A NUMBER ///////////////
public static long digSum(long a) {
long sum = 0;
while (a > 0) {
sum += a % 10;
a /= 10;
}
return sum;
}
///////// TO CHECK NUMBER IS PRIME OR NOT ///////////////
public static boolean isPrime(int n) {
if (n <= 1)return false;
if (n <= 3)return true;
if (n % 2 == 0 || n % 3 == 0)return false;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)return false;
}
return true;
}
///////// NEXT PRIME NUMBER BIGGER THAN GIVEN NUMBER ///////////////
public static int nextPrime(int n) {
while (true) {
n++;
if (isPrime(n)) break;
}
return n;
}
///////// GCD ///////////////
public static int gcd(int a, int b) {
if (b == 0)return a;
return gcd(b, a % b);
}
///////// LCM ///////////////
// public static int lcm(int a, int b) {
// return (a * b) / gcd(a, b);
// }
public static long factorial(int n) {
long ans = 1;
if (n == 0 || n == 1)return (ans);
for (int i = 2; i <= n; i++) {
ans *= i;
}
return ans;
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
28edeb0040db88886ccb58ee5e48d7f9
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
/*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class SolutionD {
public static void main(String[] args) throws Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 1; t <= test; t++) {
solve(t);
}
out.close();
}
private static void solve(int t) {
int n = sc.nextInt();
char[] arr = sc.next().toCharArray();
long score = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 'L') {
score += i;
}else {
score += n - i - 1;
}
}
List<Integer> extraScores = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (i < n / 2 && arr[i] == 'L') {
int diff = (n - i - 1) - i;
extraScores.add(diff);
}else if (i > n / 2 && arr[i] == 'R') {
int diff = i - (n - i - 1);
extraScores.add(diff);
}else if (i == n / 2 && n % 2 == 0 && arr[i] == 'R') {
extraScores.add(1);
}
}
Collections.sort(extraScores, Collections.reverseOrder());
int m = extraScores.size(), index = 0;
for (int i = 0; i < n; i++) {
if (index < m) {
score += extraScores.get(index);
index++;
}
out.print(score + " ");
}
out.println();
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
850c9018396f7a5be2f3e1a3260f0335
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
public class codeforces {
static Scanner sc=new Scanner(System.in);
static PrintWriter out=new PrintWriter(System.out);
static void solve(){
int n=sc.nextInt();
String str=sc.next();
long[] arr=new long[n];
long sum=0;
for(int i=0;i<n;i++){
arr[i]=str.charAt(i)=='L'?i:n-i-1;
sum+=arr[i];
// if(arr[i]!=(Math.max(i,n-i-1))){
arr[i]=((Math.max(i,n-i-1)-arr[i]));
// }
}
Arrays.sort(arr);
StringBuilder outstr=new StringBuilder("");
for(int i=n-1;i>=0;i--){
sum+=arr[i];
outstr.append(sum+" ");
}
out.println(outstr);
out.flush();
}
public static void main(String[] args) {
System.out.println();
int t=sc.nextInt();
while(t-->0){
solve();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
8a534d94b7bd3ab4c7ca21047416acfa
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class A {
static FastScanner sc = new FastScanner();
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static void solve(){
int n = sc.nextInt();
char[]chars = sc.next().toCharArray();
long sum = 0;
PriorityQueue<Long>queue = new PriorityQueue<>((o1, o2) -> (int) (o2 - o1));
for(int i = 0;i < n;i++){
int add;
if(chars[i] == 'L'){
add = i;
}else{
add = n - i - 1;
}
sum += add;
queue.add((long) (Math.max(i,n - i - 1) - add));
}
for(int i = 0;i < n;i++){
if(!queue.isEmpty()){
sum += queue.poll();
}
out.print(sum + " ");
}
out.println();
out.flush();
}
public static void main(String[] args) {
int n = sc.nextInt();
for(int i = 0;i < n;i++){
solve();
}
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
163dc847e77f26753f2af6a2dea47bd4
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Stack;
public class aaaaaa {
static Scanner sc;
public static void solve() {
int n = sc.nextInt();
String s = sc.next();
int [] change = new int[n];
long currScore = 0;
for(int i=0; i<n; i++){
char c = s.charAt(i);
if(c == 'L'){
currScore += i;
change[i] = (n-i-1) - i;
}
else {
currScore += n-i-1;
change[i] = i - (n-i-1);
}
}
Arrays.sort(change);
int ind = n-1;
for(int k=1; k<=n; k++){
if(change[ind] > 0){
currScore += change[ind--];
System.out.print(currScore + " ");
}
else{
System.out.print(currScore + " ");
}
}
System.out.println();
}
public static void main(String[] args) {
sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-->0){
solve();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
9fe4e8166d28dc07375a6ca6c7f15df8
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class line {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t-- > 0) {
int len = s.nextInt();
s.nextLine();
char[] line = s.nextLine().toCharArray();
// net value gained by turning around
ArrayList<Integer> values = new ArrayList<Integer>();
long res = 0;
// i = value facing left
// len - 1 - i = value facing right
for (int i = 0; i < len; i++) {
if (line[i] == 'L') {
values.add((len - 1 - i) - i);
res += i;
} else {
values.add(i - (len - 1 - i));
res += len - i - 1;
}
}
Collections.sort(values);
for (int i = 0; i < len; i++) {
res = Math.max(res, values.get(len - i - 1) + res);
System.out.print(res + " ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
5ae8c029dd05d6b2404fa96cbfa954b3
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
static class SortingString {
int oldIndex;
int ch;
public SortingString(int oldIndex, int ch) {
this.oldIndex = oldIndex;
this.ch = ch;
}
}
static class SortingComparator implements Comparator<SortingString> {
public int compare(SortingString a, SortingString b) {
return a.oldIndex - b.oldIndex;
}
}
public static void main(String[] args) throws IOException {
Reader s = new Reader();
Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
try {
int t = Integer.parseInt(br.readLine());
// BufferedReader bu = new BufferedReader(new InputStreamReader(System.in));
// StringBuilder sb = new StringBuilder();
// String[] str = bu.readLine().split(" ");
//
// int t = Integer.parseInt(str[0]);
while (t-- > 0) {
// str = bu.readLine().split(" ");
// solve(str[0],str[1]);
// }
int n = Integer.parseInt(br.readLine());
String ss = br.readLine();
solve(n, ss);
}
} catch (Exception e) {
return;
}
}
public static void solve(int n, String ss) {
StringBuilder ans = new StringBuilder();
long sum =0, counter=0;
char[] ch = ss.toCharArray();
for(int i=0;i<n;i++){
if(ch[i]=='L')sum+=i;
else sum+=n-i-1;
}
for(int i=0;i<(n/2);i++){
if(ch[i]=='L'){
sum+=n-i-1-i;
ans.append(sum).append(" ");
counter++;
}
if(ch[n-i-1]=='R'){
sum+=n-i-1-i;
ans.append(sum).append(" ");
counter++;
}
}
for(int i=1;i<=n-counter;i++){
ans.append(sum).append(" ");
}
System.out.println(ans);
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
70e5bef26715a0cf3d5984f807df8767
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
//package Div_4_817;
import java.util.*;
import java.io.*;
public class D {
public static void main(String[] args) {
FastScanner sc=new FastScanner();
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
String s=sc.next();
int[] ori=new int[n],gre=new int[n];
long sum=0;
PriorityQueue<Pair>pq=new PriorityQueue<>((a,b)->b.y-a.y);
for(int i=0;i<s.length();++i) {
char ch=s.charAt(i);
if(ch=='L') {
ori[i]=i;
gre[i]=Math.max(i,(s.length()-1)-i);
} else {
ori[i]=(s.length()-1)-i;
gre[i]=Math.max(i,(s.length()-1)-i);
}
sum+=ori[i];
if(gre[i]!=ori[i]) {pq.add(new Pair(i,gre[i]));}
}
//System.out.println(pq);
long curr=sum;
while(!pq.isEmpty()) {
n--;
Pair p=pq.remove();
curr-=ori[p.x];
curr+=p.y;
System.out.print(curr+" ");
}
while(n-->0) System.out.print(curr+" ");
System.out.println();
}
}
}
class Pair {
int x,y;
Pair(int a,int b) {
x=a;
y=b;
}
}
class FastScanner {
BufferedReader brd;
StringTokenizer stz;
public FastScanner() {
brd=new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(stz==null ||!stz.hasMoreElements()) {
try {
stz=new StringTokenizer(brd.readLine());
}
catch(IOException e) {
e.printStackTrace();
}
}
return stz.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str="";
try {
str=brd.readLine();
}
catch(IOException e) {
e.printStackTrace();
}
return str;
}
boolean hasNext() {
if (stz!=null && stz.hasMoreTokens()) {
return true;
}
String str="";
try {
brd.mark(1000);
str=brd.readLine();
if (str==null) {
return false;
}
brd.reset();
} catch (IOException e) {
return false;
}
return true;
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
d959e0ef1b7242c8a0bfc6a180e98d2a
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0){
int k = s.nextInt();
char[] a = s.next().toCharArray();
int n = a.length;
long ans=0;
List<Long> l = new ArrayList<>();
for(int i=0;i<n;i++){
if(a[i]=='L'){
l.add((long)(n-i-1)-i);
ans+=i;
}
else{
l.add((long)i-(n-i-1));
ans+=n-i-1;
}
}
Collections.sort(l,Collections.reverseOrder());
// System.out.println(l);
for(long v:l){
if(v>0){
ans+=v;
}
System.out.print(ans+" ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
65c4887a23b1bf5004c0f6238804d8b1
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class line {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
String[] line = sc.next().split("");
int[] difference = new int[n];
long currentValue = 0;
for (int j = 0 ; j < n ; j++) {
currentValue += line[j].equals("L")?j:n-1-j;
difference[j] = Math.max(j, n-1-j) - (line[j].equals("L")?j:n-1-j);
}
Arrays.sort(difference);
for (int j = n-1 ; j >= 0 ; j--) {
System.out.print(currentValue += difference[j]);
System.out.print(" ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
aee8b388e1070a0bc1c97a7f992c0c0e
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class line {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
String[] line = sc.next().split("");
int[] values = new int[n];
int[] potential = new int[n];
int[] difference = new int[n];
long currentValue = 0;
for (int j = 0 ; j < n ; j++) {
values[j] = line[j].equals("L")?j:n-1-j;
currentValue += values[j];
potential[j] = Math.max(j, n-1-j);
difference[j] = potential[j] - values[j];
}
Arrays.sort(difference);
for (int j = n-1 ; j >= 0 ; j--) {
System.out.print(currentValue += difference[j]);
System.out.print(" ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
33524bae994d58c09c00a1ba88cc3c9d
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class line {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int T = Integer.parseInt(st.nextToken());
for(int i=0;i<T;i++)
{
st = new StringTokenizer(br.readLine());
Long N=Long.parseLong(st.nextToken());
st = new StringTokenizer(br.readLine());
String a=st.nextToken();
char[] direction=a.toCharArray();
PriorityQueue<Long> pQueue = new PriorityQueue<Long>(Collections.reverseOrder());
long ret=0;
for(int j=0;j<N;j++)
{
if(direction[j]=='L')
{
if(j<=(N/2)-1)
{
if(N-j-1!=j)
{
pQueue.add(N-j-1-j);
ret+=j;
}
else
{
ret+=j;
}
}
else
{
ret+=j;
}
}
else
{
if(j<=(N/2)-1)
{
ret+=N-j-1;
}
else
{
if(N-j-1!=j)
{
pQueue.add(j-(N-j-1));
ret+=N-j-1;
}
else
{
ret+=N-j-1;
}
}
}
}
int c=0;
while(!pQueue.isEmpty())
{
ret+=pQueue.poll();
System.out.print(ret+" ");
c++;
}
for(int o=c;o<N;o++)
{
System.out.print(ret+" ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
f9921a9256f212f5c0c97e2a78063c0b
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
while(test-->0) {
int n = sc.nextInt();
String s = sc.next();
char[] a = s.toCharArray();
int [] cnt = new int[n];
long res = 0;
for(int i=0; i<n; i++) {
cnt[i] = (n-1-i)-i;
if(a[i]=='R') cnt[i] = i-(n-1-i);
if(a[i]=='L') res += i;
else res += n-1-i;
}
Arrays.sort(cnt);
for(int i=0; i<n; i++) {
res = Math.max(res, res+cnt[n-1-i]);
System.out.print(res);
System.out.print(" ");
}
System.out.print("\n");
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
263933a3bf92472d6589fce26d7877cf
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
//package Algorithm;
import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import javax.xml.stream.events.StartDocument;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int T = Integer.parseInt(st.nextToken());
for (int test = 1; test <= T; test++) {
st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
String str = st.nextToken();
long sum = 0;
int limit1;
int limit2;
if(N%2==0) {
limit1 = N/2 - 1;
limit2 = N/2;
}
else {
limit1 = limit2 = N/2;
}
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'L') {
sum += (long)i;
} else {
sum += (long)(str.length() - i - 1);
}
} // 우선 초기 value 를 구합니다.
int start = 0;
int end = str.length() - 1;
for (int i = 0; i < N; i++) {
while (start <= limit1) {
if (str.charAt(start) == 'L') {
break;
}
start++;
}
while (end >= limit2) {
if (str.charAt(end) == 'R') {
break;
}
end--;
}
if (start == limit1 + 1 && end == limit2 - 1) {
} else {
if (start < N - 1 - end) {
sum -= (long)start;
sum += (long)(N - 1 - start);
start++;
} else {
sum -= (long)(N - 1 - end);
sum += (long)end;
end--;
}
}
bw.write(sum + " ");
}
bw.write("\n");
}
bw.flush();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
b2c40f425b2d0d6a4d1999793d71bc35
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
// package codeforce;
import java.util.*;
import java.net.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.io.*;
public class A {
static class Node {
int id1;
int id2;
Node(int v1, int w1){
this.id1= v1;
this.id2=w1;
}
Node(){}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
}
static boolean[] seiveofEratoSthenes(int n) {
boolean[] isPrime= new boolean[n+1];
Arrays.fill(isPrime, true);
isPrime[0]=false;
isPrime[1]= false;
for(int i=2;i*i<=n;i++) {
for(int j=2*i; j<=n;j=j+i) {
isPrime[j]= false;
}
}
return isPrime;
}
static int in = 2;
// Function check whether a number
// is prime or not
public static boolean isPrime(int n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static class SortingComparator implements Comparator<Node>{
@Override
public int compare(Node p1, Node p2) {
// int n = p1.id1-p2.id1;
// if(n!=0)return n;
return p1.id2-p2.id2;
}
}
static int pp =1;
static long[] dp = new long[500001];
public static void main(String[] args) throws UnknownHostException {
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t--!=0) {
int n = sc.nextInt();
long score =0;
char[] arr = sc.next().toCharArray();
int[] org = new int[n];
int[] change = new int[n];
for(int i=0; i<n; i++) {
if(arr[i]=='L') {
org[i]= i;
change[i]= n-i-1-org[i];
}
else {
org[i]= n-i-1;
change[i]= i-org[i];
}
score+=org[i];
}
Arrays.sort(change);
for(int i=0; i<n/2; i++) {
int temp = change[i];
change[i]= change[n-i-1];
change[n-i-1]=temp;
}
// for(int i=0; i<n; i++) {
// System.out.print(change[i]+" ");
// }
// System.out.println();
for(int i=0; i<n; i++) {
// int ans =score;
if(change[i]>0)score+=change[i];
System.out.print(score+" ");
}
System.out.println();
}
}
static long nextPowerOf2(long N)
{
// if N is a power of two simply return it
if ((N & (N - 1)) == 0)
return N;
return 0x4000000000000000L
>> (Long.numberOfLeadingZeros(N) - 2);
}
public static int[] solve(int[] arr, int[] it) {
HashMap<Integer, Integer> hm = new HashMap<>();
for(int i=0; i<arr.length; i++) {
hm.put(arr[i], hm.getOrDefault(arr[i], 0)+1);
}
for(int i=0; i<arr.length; i++) {
it[i]= hm.get(arr[i]);
}
return it;
}
public static void rec(int l, int e, int[] arr) {
if(l>e || e>=arr.length || l<=0)return;
int mid = (e-l+1)%2==0?(e+l-1)/2:(e+l)/2;
if(arr[mid]==0 ) {
arr[mid]=pp;
pp++;
// if(pp>1)return;
}
if(e-mid-1<=mid-l-1) {rec(l, mid-1, arr);
rec(mid+1, e, arr);
}
else {
rec(mid+1, e, arr);
rec(l, mid-1, arr);
}
}
static double fact(double n)
{
int i=1;
double fact=1;
while(i<=n)
{
fact=fact*i;
i++;
}
return fact;
}
static double combination(int n,int r)
{
double com=fact(n)/(fact(n-r)*fact(r));
return com;
}
static boolean digit(long n) {
long ans = n;
while(n>0) {
long rem = n%10;
if(rem!=0 && ans%rem!=0)return false;
n=n/10;
}
return true;
}
static int nCr(int n, int r)
{
return fact(n) / (fact(r) *
fact(n - r));
}
// Returns factorial of n
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l, Collections.reverseOrder());
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static boolean isPalindrome(char[] arr, int i, int j) {
while(i<j) {
if(arr[i]!=arr[j])return false;
i++;
j--;
}
return true;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static int max =0;
static void dfs(int i, boolean[] vis , ArrayList<ArrayList<Integer>> adj) {
max = Math.max(max, i);
vis[i]= true;
for(int e: adj.get(i)) {
if(vis[e]==false) {
dfs(e, vis, adj);
}
}
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static double pow(int a, int b) {
long res= 1;
while(b>0) {
if((b&1)!=0) {
res= (res*a);
}
a= (a*a);
b= b>>1;
}
return res;
}
static void permute(String s , String answer, HashSet<String> hs)
{
if (s.length() == 0)
{
hs.add(answer);
return;
}
for(int i = 0 ;i < s.length(); i++)
{
char ch = s.charAt(i);
String left_substr = s.substring(0, i);
String right_substr = s.substring(i + 1);
String rest = left_substr + right_substr;
permute(rest, answer + ch, hs);
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
de3f141b68959b24c9da785e27541812
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
public class App {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader reader = new BufferedReader(new FileReader(new File("C:\\Development\\MyProjects\\EducationProject\\src\\main\\java\\ru\\ilbagmanov\\randomcode\\inp.txt")));
int t = Integer.parseInt(reader.readLine());
for (int i = 0; i < t; i++) {
int n = Integer.parseInt(reader.readLine());
int[] a = new int[n];
char[] b = new char[n];
long sum = 0;
for (int j = 0; j < n; j++) {
char cr = (char) reader.read();
b[j] = cr;
if (cr == 'L')
a[j] = j;
else
a[j] = n - 1 - j;
sum += a[j];
}
reader.readLine();
LinkedList<Long> result = new LinkedList<>();
for (int j = 0; j < n; j++) {
long x = getX(a, b, j);
if (x > a[j]) {
sum = sum - a[j] + x;
result.add(sum);
}
x = getX(a, b, n - 1 - j);
if (x > a[n - 1 - j]) {
sum = sum - a[n - 1 - j] + x;
result.add(sum);
}
if (result.size() >= n || (j + 1) > (n - 1 - (j + 1)))
break;
}
if (result.size() == 0)
result.add(sum);
if (result.size() > n) {
result.removeLast();
}
if (result.size() == n) {
result.forEach(x -> System.out.print(x + " "));
} else {
result.forEach(x -> System.out.print(x + " "));
for (int j = 0; j < n - result.size(); j++)
System.out.print(result.getLast() + " ");
}
System.out.println();
}
}
public static long getX(int[] a, char[] b, int j) {
int n = a.length;
long x;
if (b[j] == 'L') {
x = n - 1 - j;
} else {
x = j;
}
return x;
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
59f08eea1125c38f663c7a4e4956e8a2
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class claas{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
String s=sc.next();
List<Integer> ans=new ArrayList<>();
List<Integer> ans1=new ArrayList<>();
for(int i=0;i<n/2;i++) {
if(s.charAt(i)=='L') {
ans.add(n-i-1);
ans1.add(i);
}
if(s.charAt(n-i-1)=='R') {
ans.add(n-i-1);
ans1.add(n-i-1);
}
}
long sum=0;
for(int i=0;i<n;i++) {
if(s.charAt(i)=='L') {
sum+=(long)i;
}
else {
sum+=(long)n-i-1;
}
}
// System.out.println(ans1);
// System.out.println(ans);
int j=0;
for(int i=0;i<n;i++) {
if(j<ans.size()) {
sum-=(long)(n-1-ans.get(j));
sum+=(long)ans.get(j);
j++;
}
System.out.print(sum+" ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
88e50b216eb18d81ef6a99d874433fa8
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class _1722D {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i=1;i<=t;i++) {
int n = sc.nextInt();
String line = sc.next();
long ans=0;
ArrayList<Long> arr = new ArrayList<Long>();
for(int j=0;j<n;j++) {
if(line.charAt(j)=='L') {
arr.add((long)(n-j-1)-j);
ans+=j;
}else {
arr.add((long) (j-(n-j-1)));
ans+=n-j-1;
}
}
// for(int k=1;k<=n;k++) {
// StringBuilder li = new StringBuilder(line);
// int out=ans;
// int idx=0;
// for(int u=0;u<k;u++) {
// if(u%2==0) {
// idx =li.indexOf("L");
// if(idx==-1) {
// continue;
// }
// li.setCharAt(idx, 'R');
// //line=li.toString();
// out-=idx;
// out+=n-idx-1;
// }else {
// idx=li.lastIndexOf("R");
// if(idx==-1) {
// continue;
// }
// li.setCharAt(idx, 'L');
// //line=li.toString();
// out-=n-idx-1;
// out+=idx;
// }
// }
//
// System.out.print(out+" ");
// }
// System.out.println();
// StringBuilder li = new StringBuilder(line);
// int out=ans;
// for(int k=1;k<=n;k++) {
// int idx_l=li.indexOf("L");
// //int idx_l_2=li.lastIndexOf("L");
// //int idx_r_1 = li.indexOf("R");
// int idx_r=li.lastIndexOf("R");
//
// if(idx_l==-1 && idx_r==-1) {
// continue;
// }else if(idx_l==-1) {
// li.setCharAt(idx_r, 'L');
// out+=idx_r;
// }else if(idx_r==-1) {
// li.setCharAt(idx_l, 'R');
// out+=n-idx_l-1;
// }else if(idx_l > n-idx_r-1) {
// //replace R to L
// li.setCharAt(idx_r, 'L');
// out-=n-idx_r-1;
// out+=idx_r;
//
// }else {
// li.setCharAt(idx_l, 'R');
// out-=idx_l;
// out+=n-idx_l-1;
// }
//
// System.out.print(out+" ");
// }
// System.out.println();
arr.sort(Collections.reverseOrder());
for(Long h:arr) {
if(h>0) {
ans+=h;
}
System.out.print(ans+" ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
d0a83a57f259c3a5939d912e5f527888
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
while(t>0){
t--;
int n = sc.nextInt();
sc.nextLine();
String s =sc.nextLine();
solve(n,s);
System.out.println();
}
}
static void solve(int n , String s){
long [] left = new long [n];
long [] right = new long [n];
long [] t = new long [n+1];
for(int i=0;i<n;i++){
left[i] = i;
right[i] = n-i-1;
if(s.charAt(i)=='L') t[0] +=(i);
else t[0] += (n-i-1);
}
// System.out.println(left[n-1]-right[n-1]);
int i =0;
int j =n-1;
int k = 1;
while(i<n/2 && j>=n/2){
while(i<n && s.charAt(i)=='R') i++;
while( j>=0 && s.charAt(j)=='L') j--;
if(i>=n/2 || j<n/2) break;
if(right[i]>left[j]){
t[k] = t[k-1]+right[i]-left[i];
k++;
i++;
}
else{
t[k] = t[k-1]+left[j]-right[j];
k++;
j--;
}
}
while(j>=n/2){
while( j>=0 && s.charAt(j)=='L') j--;
if(j<n/2) break;
t[k] = t[k-1]+left[j]-right[j];
k++;
j--;
}
while(i<n/2){
while(i<n && s.charAt(i)=='R') i++;
if(i>=n/2) break;
t[k] = t[k-1]+right[i]-left[i];
k++;
i++;
}
while(k<=n){
t[k]=t[k-1];
k++;
}
for(int p=1;p<=n;p++){
System.out.print(t[p]+" ");
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
503b60589a6ca3adfc5af9d269b5e6cc
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class Prac {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int f=1; f<=T; f++)
{
int n = sc.nextInt();
String s = sc.next();
long count = 0;
for(int i=0; i<n; i++)
{
if(s.charAt(i)=='L')
{
count = count + i;
}
else
{
count = count + (n-i-1);
}
}
int start = 0;
int end = n-1;
long ans[] = new long[n];
int p = 0;
boolean S = true;
boolean E = true;
while(start<end)
{
if(s.charAt(start)=='L' && S)
{
count = count - start;
count = count + (n-start-1);
ans[p] = count;
p++;
S = false;
}
else if(s.charAt(end)=='R' && E)
{
count = count - (n-1-end);
count = count + end;
ans[p] = count;
p++;
E = false;
}
else
{
start++;
end--;
S = true;
E = true;
}
}
if(ans[0]==0)
{
ans[0] = count;
}
int j = 1;
while(j<n)
{
if(ans[j]==0)
{
break;
}
j++;
}
long prp = ans[j-1];
for(int i=j; i<n; i++)
{
ans[i] = prp;
}
for(int i=0; i<n; i++)
{
System.out.print(ans[i] + " ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
94dd65263b07da02abede20a5ac39cb6
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Codechef {
static FastReader f = new FastReader();
public static PrintWriter out = new PrintWriter(System.out);
static int primes[];
static int dp1[][];
static long ans = 0;
static List<List<Integer>> adj = new ArrayList<>();
static int up[][] = new int[200001][20];
public static void solve() {
int n = f.nextInt();
char c[] = f.next().toCharArray();
long[][] arr = new long[n][2];
long sum = 0;
for(int i =0 ; i < n ; i++) {
if(c[i] == 'L') {
arr[i][0] = i;
}
else {
arr[i][0] = n - i - 1;
}
arr[i][1] = i;
sum += arr[i][0];
}
Arrays.sort(arr, (a, b) -> Long.compare(a[0], b[0]));
long[] ans = new long[n];
for(int i = 0 ; i < n ; i++) {
long ind = arr[i][1];
long temp = Math.max(ind, n - ind - 1);
sum = sum - arr[i][0] + temp;
ans[i] = sum;
}
for(long it : ans) {
out.print(it + " ");
}
out.println();
out.flush();
}
static boolean isFlip(long a[], int i, int j) {
return a[i] != a[j];
}
static void binaryLifting(int src, int par) {
up[src][0] = par;
for(int i = 1; i < 20 ; i++) {
if(up[src][i - 1] != -1) {
up[src][i] = up[up[src][i - 1]][i - 1];
}
else {
up[src][i] = -1;
}
}
for(int child : adj.get(src)) {
if(child != par) {
binaryLifting(child, src);
}
}
}
static int helper(int node, int k) {
if(k == 0) return up[node][0];
while(k > 0 && node > 0) {
for(int i = 19 ; i >= 0 ; i--) {
if( (k&(1<<i)) > 0) {
k = k - (1<<i);
node = up[node][i];
}
}
}
return node==0?-1:node;
}
public static void main(String[] args) {
int t = f.nextInt();
// int t = 1;
while (t-- > 0) {
solve();
}
}
static boolean isPower2(long n) {
long temp = (long) (Math.log(n) / Math.log(2));
if (n == Math.pow(2, temp)) {
return true;
}
return false;
}
static int dp[][];
static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j))
return false;
i++;
j--;
}
return true;
}
public static List<Integer> PrimeFactors(int n) {
List<Integer> list = new ArrayList<Integer>();
int count = 0;
while (n % 2 == 0) {
count++;
n /= 2;
}
if (count > 0)
list.add(2);
for (int i = 3; i <= Math.sqrt(n); i += 2) {
count = 0;
while (n % i == 0) {
count++;
n /= i;
}
if (count > 0)
list.add(i);
}
if (n > 2)
list.add(n);
return list;
}
public static double setPrecisionK(double d) {
double ans = 0;
// If round off upto 5 digits
// ans = Math.round(d*1e5)/1e5;
return ans;
}
public static List<String[]> all_substrings(String s) {
List<String[]> substr = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
for (int j = i; j < s.length(); j++) {
substr.add(new String[] { s.substring(i, j + 1), s.substring(j + 1, s.length()) });
}
}
return substr;
}
public static int number_of_2_in_N(long n) {
int i = 0;
while (n % 2 == 0) {
i++;
n /= 2;
}
return i;
}
public static boolean is_power_of_2(long n) {
if (n == 0)
return false;
return ((n) & (n - 1)) == 0;
}
public static int highest_power_of_2(int n) {
int res = 0;
for (int i = n; i >= 1; i--) {
// If i is a power of 2
if ((i & (i - 1)) == 0) {
res = i;
break;
}
}
return res;
}
static List<Integer> fibonacci(int n) {
List<Integer> list = new ArrayList<>();
list.add(0);
list.add(1);
for (int i = 2; i < n; i++) {
list.add(list.get(i - 1) + list.get(i - 2));
}
return list;
}
static long nCr(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
// Returns factorial of n
static int factorial(int n) {
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
public static Map<Integer, Integer> prime_factors_with_freq(int n) {
// Print the number of 2s that divide n
Map<Integer, Integer> map = new HashMap<>();
int count = 0;
while (n % 2 == 0) {
// System.out.print(2 + " ");
count++;
n /= 2;
}
if (count > 0)
map.put(2, count);
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i += 2) {
// While i divides n, print i and divide n
count = 0;
while (n % i == 0) {
// System.out.print(i + " ");
count++;
n /= i;
}
if (count > 0)
map.put(i, count);
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
map.put(n, map.getOrDefault(n, 0) + 1);
return map;
}
static void sieve_of_Eratosthenes(int n) {
List<Integer> list = new ArrayList();
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
int idx = 0;
for (int i = 2; i <= n; i++) {
if (prime[i] == true) {
primes[idx++] = i;
}
}
}
static List<Integer> get_divisors(long n) {
List<Integer> ans = new ArrayList();
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
ans.add(i);
ans.add((int) (n / i));
}
}
return ans;
}
public static boolean isLucky(int n) {
boolean flag = true;
int nn = n;
while (n > 0) {
if (n % 10 != 4 && n % 10 != 7) {
flag = false;
break;
}
n /= 10;
}
return flag;
}
static boolean is_perfect_square(double number) {
// calculating the square root of the given number
double sqrt = Math.sqrt(number);
// finds the floor value of the square root and comparing it with zero
return ((sqrt - Math.floor(sqrt)) == 0);
}
public static long smallest_divisor(long n) {
// if divisible by 2
if (n % 2 == 0)
return 2;
// iterate from 3 to sqrt(n)
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return i;
}
return n;
}
public static boolean isPrime(long n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long factorize(long n) {
long ans = 0;
while (!(n % 2 > 0)) {
// equivalent to n=n/2;
n >>= 1;
ans++;
}
if (ans > 0)
ans = 1;
for (long i = 3; i <= (long) Math.sqrt(n); i += 2) {
int count = 0;
while (n % i == 0) {
count++;
n = n / i;
}
ans += count;
}
if (n > 2) {
ans++;
}
return ans;
}
static int count_set_bits(long n) {
int count = 0;
while (n > 0) {
n &= (n - 1);
count++;
}
return count;
}
public static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
24f6f4a911da21b03362b230ac8e82bf
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.*;
import java.sql.PreparedStatement;
import java.util.*;
public class Main {
static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void count(int[] arr,int start,int ind,HashSet<Integer> set){
for(int i=start;i<=ind;i++){
if(set.contains(arr[i])){
continue;
}
set.add(arr[i]);
}
}
public static void main(String[] args) throws IOException {
MyScanner s=new MyScanner();
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
String check=s.nextLine();
// if(check==null){
// return;
// }
// int[] arr=new int[26];
// for(char c:str1.toCharArray()){
// arr[c-'a']++;
// }
// for(char c:str2.toCharArray()){
// arr[c-'a']++;
// }
// String ans="";
// int start=0;
// boolean odd;
// char odd1=;
// for(int i=0;i<26;i++){
// if(arr[i]==0){
// continue;
// }else {
// if(arr[i]%2!=0){
// if(!odd){
// odd=true;
// odd1=(char) (arr[i]+'a');
// }
// arr[i]--;
// }
// while(arr[i]!=0){
// ans+=(char) (arr[i]+'a');
// arr[i]-=2;
// }
// }
// }
// string ans2=reverse(ans1);
// if(odd){
// ans+=odd1;
// }
// return ans+ans2;
// int[] diff=new int[n-1];
// sum=0;
// for(int i=0;i<n-1;i++){
// diff[i]=arr[i]-arr[i-1];
// sum+=diff[i];
// }
// for(int i=0;i<n;i++){
// sum-=ar
// }
int t=Integer.parseInt(check);
while(t-->0) {
int n=s.nextInt();
char[] arr=s.next().toCharArray();
if(n==1){
output.write(0+"\n");
output.flush();
continue;
}
long temp=0;
for(int i=0;i<n;i++){
if(arr[i]=='L'){
temp+=i;
}else{
temp+=n-1-i;
}
}
long[] ans=new long [n];
int ops=0;
for(int i=0;i<n/2;i++){
if(arr[i]!='R'){
temp-=i;
temp+=n-i-1;
ans[ops++]=temp;
}
if(arr[n-1-i]!='L'){
temp+=n-i-1;
temp-=i;
ans[ops++]=temp;
}
}
for(int i=ops;i<n;i++){
ans[i]=temp;
}
for(int i=0;i<n;i++){
output.write(ans[i]+" ");
}
output.write("\n");
output.flush();
// output.write();
// for(int i=0;i<n;i++){
// arr[i]=s.nextLong();
// }
// long[] resarr=new long [n];
// for(int i=0;i<n;i++){
// resarr[i]=s.nextLong();
// }
// int i=0,j=0;
// for(;i<arr.length;i++){
// while(arr[i]>resarr[j]){
// j++;
// }
// output.write(resarr[j]-arr[i]+" ");
// }
// output.write("\n");
// i=n-1;
// j=n-1;
// long[] result=new long [n];
// for(;i>=0;i--){
// if(j>0 && arr[i]<=resarr[j-1]){
// result[i]=resarr[j]-arr[i];
// }else{
// result[i]=resarr[j]-arr[i];
// j--;
// }
// }
// for(i=0;i<n;i++){
// output.write(result[i]+" ");
// }
// output.write("\n");
// output.flush();
}
}
// public static binary(int[] arr,int a){
// int lo=0,hi=arr.length-1;
// while(lo<hi){
// int mid=(arr[lo]+arr[hi])/2;
// if(mid>a){
// hi=mid-1;
// }else{
// l
// }
// }
// }
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
9b560af77b416b40ac76b2f035d53171
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.PriorityQueue;
public class D817 {
static AReader scan = new AReader();
static void solve() {
int n = scan.nextInt();
char[] cs = scan.next().toCharArray();
PriorityQueue<Integer> queue = new PriorityQueue<>(n,(o1,o2)->o2 - o1);
long sum = 0;
for(int i = 1;i<=n;i++){
char c = cs[i-1];
if(c == 'L'){
sum += i - 1;
queue.offer(Math.max(0,n - i - (i - 1)));
}else{
sum += n - i;
queue.offer(Math.max(0,i - 1 - (n - i)));
}
}
for(int i = 0;i<n;i++){
sum += queue.poll();
System.out.print(sum + " ");
}
System.out.println();
}
public static void main(String[] args) {
int T = scan.nextInt();
while (T-- > 0) {
solve();
}
}
}
class AReader {
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer tokenizer = new StringTokenizer("");
private String innerNextLine() {
try {
return reader.readLine();
} catch (IOException ex) {
return null;
}
}
public boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String nextLine = innerNextLine();
if (nextLine == null) {
return false;
}
tokenizer = new StringTokenizer(nextLine);
}
return true;
}
public String nextLine() {
tokenizer = new StringTokenizer("");
return innerNextLine();
}
public String next() {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
7e8035646bfda0f938a35aa0508ebb8c
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
import java.util.*;
/**
*
* @author vishrut
*/
public class JavaCodeTester {
/**
* @param args the command line arguments
*/
static long power(long n,long ans){
if(n==0)return ans;
return ans=power(n-1,ans)*2;
}
static boolean prime(int n){
for(int i=2;i*i<=n;i++){
if(n%i==0)return false;
}
return true;
}
static long power(long x, int y, int p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static int gcd(int a,int b)
{
if(b%a==0)
return a;
return gcd(b%a,a);
}
static int sum (int a){
if(a==0)return 0;
return a%10+sum(a/10);
}
static long power (int a,int b){
if(b==0)return 1;
return a*power(a, b-1);
}
static String binary (long decimal){
if(decimal==0) return "";
return binary(decimal/2)+ Long.toString((decimal%2));
}
static boolean isPalindrome ( String a , int n){
if(a.charAt(n)!=a.charAt(a.length()-n-1))return false;
if(n==0) return true;
return isPalindrome (a, n-1);
}
static int fibonacci (int n){
if(n==1)return 0;
if(n==2)return 1;
return fibonacci(n-1)+fibonacci(n-2);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
StringBuilder sb=new StringBuilder();
int t=sc.nextInt();
while(t-->0){
int n = sc.nextInt();
String s = sc.next();
long count = 0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='R') count+= (s.length()-i-1);
else count+=i;
}
int l = 0, r = s.length()-1;
ArrayList<Long> ans=new ArrayList();
while(r>=s.length()/2 && l<=s.length()/2){
if(s.charAt(r)=='R'){
count -= (s.length()-r-1);
count += r;
ans.add(count);
}
if(s.charAt(l)=='L'){
count -= l;
count += (s.length()-l-1);
ans.add(count);
}
r--;
l++;
}
for(int i=0;i<n;i++){
if(i<ans.size()) System.out.print(ans.get(i)+" ");
else System.out.print(count+" ");
}
System.out.println();
//sb.append(ans);
//if(t!=0)sb.append("\n");
}
System.out.println(sb);
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
| |
PASSED
|
fbe648dd6c345727bc5f184df6c2a938
|
train_109.jsonl
|
1661871000
|
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.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Mainn {
static long M = (long) (1e9 + 7);
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if (st.hasMoreTokens()) {
str = st.nextToken("\n");
} else {
str = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
public static void swap(int[] a, long i, long j) {
long temp = a[(int) i];
a[(int) i] = a[(int) j];
a[(int) j] = (int) temp;
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int lcm(int a, int b) {
return (int) (a * b / gcd(a, b));
}
public static String sortString(String inputString) {
// Converting input string to character array
char[] tempArray = inputString.toCharArray();
// Sorting temp array using
Arrays.sort(tempArray);
// Returning new sorted string
return new String(tempArray);
}
static boolean isSquare(int n) {
int v = (int) Math.sqrt(n);
return v * v == n;
}
static boolean PowerOfTwo(int n) {
if (n == 0) return false;
return (int) (Math.ceil((Math.log(n) / Math.log(2)))) ==
(int) (Math.floor(((Math.log(n) / Math.log(2)))));
}
static int power(long a, long b) {
long res = 1;
while (b > 0) {
if (b % 2 == 1) {
res = (res * a) % M;
}
a = ((a * a) % M);
b = b / 2;
}
return (int) res;
}
public static boolean isPrime(int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0) {
return false;
}
return true;
}
static int pown(long n) {
if (n == 0)
return 1;
if (n == 1)
return 1;
if ((n & (-n)) == n)
return 0;
return 1;
}
static long computeXOR(long n) {
// If n is a multiple of 4
if (n % 4 == 0)
return n;
// If n%4 gives remainder 1
if (n % 4 == 1)
return 1;
// If n%4 gives remainder 2
if (n % 4 == 2)
return n + 1;
// If n%4 gives remainder 3
return 0;
}
static long binaryToInteger(String binary) {
char[] numbers = binary.toCharArray();
long result = 0;
for (int i = numbers.length - 1; i >= 0; i--)
if (numbers[i] == '1')
result += Math.pow(2, (numbers.length - i - 1));
return result;
}
static String reverseString(String str) {
char ch[] = str.toCharArray();
String rev = "";
for (int i = ch.length - 1; i >= 0; i--) {
rev += ch[i];
}
return rev;
}
static int countFreq(int[] arr, int n) {
HashMap<Integer, Integer> map = new HashMap<>();
int x = 0;
for (int i = 0; i < n; i++) {
map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
}
for (int i = 0; i < n; i++) {
if (map.get(arr[i]) == 1)
x++;
}
return x;
}
static boolean symm(String str) {
if (str.length() == 1 || str.length() == 0)
return true;
if (str.substring(0, (str.length() / 2)).equals(str.substring(str.length() / 2)))
return symm(str.substring(0, (str.length() / 2)));
return false;
}
static void reverse(int[] arr, int l, int r) {
int d = (r - l + 1) / 2;
for (int i = 0; i < d; i++) {
int t = arr[l+i];
arr[l + i]= arr[r - i];
arr[r-i]= t;
}
// print array here
}
static int f(int x) {
x++;
while (x % 10 == 0) {
x /= 10;
}
return x;
}
public static boolean isFair(long x) {
long n = x;
while (n != 0) {
long r = (long) n % 10;
if (r != 0 && x % r != 0)
return false;
n = n / 10;
}
return true;
}
static void sort(String[] s, int n) {
for (int i = 1; i < n; i++) {
String temp = s[i];
// Insert s[j] at its correct position
int j = i - 1;
while (j >= 0 && temp.length() < s[j].length()) {
s[j + 1] = s[j];
j--;
}
s[j + 1] = temp;
}
}
static boolean sign(int x) {
if (x > 0)
return true;
else
return false;
}
static int sqr(int n) {
double x = Math.sqrt(n);
if ((int) x == x)
return (int) x;
else
return (int) (x + 1);
}
static int set_bits_count(int num) {
int count = 0;
while (num > 0) {
num &= (num - 1);
count++;
}
return count;
}
static int UpperBound(long[] a, int x) {// x is the key or target value
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] <= x) l = m;
else r = m;
}
return l + 1;
}
static long factorial(long n) {
if (n == 0)
return 1;
else
return (n * factorial(n - 1));
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- != 0) {
int n=sc.nextInt();
String s=sc.next();
int[] arr = new int[n];
long org=0L;
for(int i=0;i<n;i++){
char ch = s.charAt(i);
if(ch=='L'){
org+=i;
arr[i]=n-1-(2*i);
} else {
org+=n-i-1;
arr[i]= 2*i+1-n;
}
}
Arrays.sort(arr);
reverse(arr,0 , n-1);
long prev=org;
for(int i=0;i<n;i++){
if(arr[i] > 0){
prev+=arr[i];
}
System.out.print(prev+" ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn 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).
|
Java 11
|
standard input
|
[
"greedy",
"sortings"
] |
f0402399cbaf8997993ac2ee59a60696
|
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++).
| 1,100
|
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.
|
standard output
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.