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
c4ea01b4fbbfe7d80426236e6675c0ce
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.lang.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(br.readLine()); while (t-->0) { int N = Integer.parseInt(br.readLine()); String str = br.readLine(); ArrayList<Long> res = new ArrayList<>(); long totSum = 0; int[] change = new int[N]; int[] orginal = new int[N]; for(int i = 0; i < N; i++) { if (str.charAt((int) i) == 'L') { change[i] = (N - i - 1 )- i; totSum += i; } else { change[i] = i-(N - i - 1); totSum += (N - i - 1); } } int flag = 0; Arrays.sort(change); for(int i = N-1; i>=0;i--) { if(change[i]>0) { totSum = totSum+change[i]; } if(flag == 0) { bw.write(totSum + ""); flag = 1; } else{ bw.write(" "+totSum); } } 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 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
1dceea928a1acd28c740a0d54115711c
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.lang.reflect.Array; import java.util.*; import java.io.*; public class D { public static void main(String[] args) throws IOException { Reader sc = new Reader(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); char[] arr = sc.next().toCharArray(); long sum = 0; for(int i = 0;i < n;i++) { if(arr[i] == 'R') sum += (n - i - 1); else sum += i; } List<Integer> s = new ArrayList<>(); List<Integer> e = new ArrayList<>(); List<Long> ans = new ArrayList<>(); int update = 1; int start = 0; int end = n - 1; int mid = n / 2; for(int i = start;i < mid;i++) { if(arr[i] != 'R') s.add(i); } for(int i = end;i >= mid;i--) { if(arr[i] != 'L') e.add(i); } int s_idx = 0; int e_idx = 0; for(int k = 1;k <= n;k++) { if(s_idx < s.size()) start = s.get(s_idx); if(e_idx < e.size()) end = e.get(e_idx); if(n % 2 == 0) { if(start < mid && arr[start] != 'R' && (start <= (n - 1 - end) || e_idx >= e.size())) { arr[start] = 'R'; sum -= start; sum += (n - start - 1); update++; s_idx++; } if(mid <= end && update <= k && arr[end] != 'L' && (start >= (n - 1 - end) || s_idx >= s.size())) { arr[end] = 'L'; sum += end; sum -= (n - end - 1); update++; e_idx++; } } else { if(start < mid && update <= k && arr[start] != 'R' && (start <= (n - 1 - end) || e_idx >= e.size())) { arr[start] = 'R'; sum -= start; sum += (n - start - 1); update++; s_idx++; } if(mid < end && update <= k && arr[end] != 'L' && (start >= (n - 1 - end) || s_idx >= s.size())) { arr[end] = 'L'; sum += end; sum -= (n - end - 1); update++; e_idx++; } } ans.add(sum); } for(Long i : ans) out.print(i + " "); out.println(); } out.flush(); } static int[] sort(int[] arr,int n) { List<Integer> list = new ArrayList<>(); for(int i = 0;i < n;i++) list.add(arr[i]); Collections.sort(list); for(int i = 0;i < n;i++) arr[i] = list.get(i); return arr; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static int isPrime(int n) { if(n < 2) return 0; if(n < 4) return 1; if((n % 2) == 0 || (n % 3) == 0) return 0; for(int i = 5; (i * i) <= n; i += 6) if((n % i) == 0 || (n % (i + 2)) == 0) return 0; return 1; } static class Reader { BufferedReader br; StringTokenizer st; public Reader() { 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; } } } // ********************* Custom Pair Class ********************* //class Pair implements Comparable<Pair> { // int a,b; // public Pair(int a,int b) { // this.a = a; // this.b = b; // } // @Override // public int compareTo(Pair other) { //// if(this.b == other.b) //// return Integer.compare(this.a,other.a); // return Integer.compare(other.b,this.b); // } //} // ****************** Segment Tree ****************** //public class SegmentTreeNode { // public SegmentTreeNode left; // public SegmentTreeNode right; // public int Start; // public int End; // public int Sum; // public SegmentTreeNode(int start, int end) { // Start = start; // End = end; // Sum = 0; // } //} //public SegmentTreeNode buildTree(int start, int end) { // if(start > end) // return null; // SegmentTreeNode node = new SegmentTreeNode(start, end); // if(start == end) // return node; // int mid = start + (end - start) / 2; // node.left = buildTree(start, mid); // node.right = buildTree(mid + 1, end); // return node; //} //public void update(SegmentTreeNode node, int index) { // if(node == null) // return; // if(node.Start == index && node.End == index) { // node.Sum += 1; // return; // } // int mid = node.Start + (node.End - node.Start) / 2; // if(index <= mid) // update(node.left, index); // else // update(node.right, index); // node.Sum = node.left.Sum + node.right.Sum; //} //public int SumRange(SegmentTreeNode root, int start, int end) { // if(root == null || start > end) // return 0; // if(root.Start == start && root.End == end) // return root.Sum; // int mid = root.Start + (root.End - root.Start) / 2; // if(end <= mid) // return SumRange(root.left, start, end); // else if(start > mid) // return SumRange(root.right, start, end); // return SumRange(root.left, start, mid) + SumRange(root.right, mid + 1, 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 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
e5bb751b3f7c8ea8ece877d3d7b8303c
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 codechef; // don't place package name! */ import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public 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(); } } public static long gcd(long a,long b) { long temp = 0; while (b != 0) { temp = b; b = a % b; a = temp; } return a; } public static void main (String[] args) throws java.lang.Exception { // your code goes here //Reader s=new Reader(); //Reader s=new Reader(); Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); String iiii=s.nextLine(); String st=s.nextLine(); int x=0; int j=n-1; int c=0; int m=0; long val=0; long max=0; for(int i=0;i<n;i++) { if(st.charAt(i)=='L') val+=i; else val+=n-1-i; if(i<n/2) max+=n-1-i; else max+=i; } //System.out.println(val); for(int i=0;i<n;i++) { if(n%2==1) { if(x>n/2&&j<n/2) break; } else { if(x>=n/2&&j<n/2) break; } int tt=0; while(x<=j&&tt==0) { if(x<=n-1-j&&st.charAt(x)=='L'&&x<n/2) { val-=x; val+=(n-1-x); x+=1; tt+=1; //System.out.println("L"+x); } else if(j>=n-1-x&&st.charAt(j)=='R'&&j>=n/2) { val=val-(n-1-j); val+=j; j-=1; tt+=1; //System.out.println("R"+x); } else { if(st.charAt(x)=='R'&&x<n/2) x+=1; if(st.charAt(j)=='L'&&j>=n/2) j-=1; } } if(c<n-1) System.out.print(val+" "); else System.out.print(max+" "); c+=1; } while(c<n) { System.out.print(max+" "); c+=1; } 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
ce4980a1d521998780473a4fd58459f0
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 D { static int n; static char[] arr; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { n = sc.nextInt(); arr = sc.next().toCharArray(); get(); } pw.close(); } static void get() { if(n==1) System.out.println(0); else { long res = 0; for (int i = 0; i < n; i++) res += arr[i] == 'L' ? i : n - i - 1; int done = 0; long max = 0; for (int i = 0, j = n - 1; i < n / 2; i++, j--) { if (arr[i] == 'L') { res -= i; res += n - i - 1; System.out.print(res + " "); if (res > max) max = res; done++; } if (arr[j] == 'R') { res -= (n - j - 1); res += j; if (res > max) max = res; System.out.print(res + " "); done++; } } if(max==0) max = res; while (done < n ) { System.out.print(max + " "); done++; } System.out.println(); } } static int gcd ( int a, int b){ return a == 0 ? b : gcd(b % a, a); } static int lcm ( int a, int b){ return a * b / gcd(a, b); } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair p) { return x == p.x ? y - p.y : x - p.x; } public String toString(){return x + " " + y;} } static class Tuple implements Comparable<Tuple> { int x; int y; char c; Tuple(char c,int x,int y){ this.c=c; this.x=x; this.y=y; } public int compareTo(Tuple t) { return x == t.x ? y - t.y : x - t.x; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public long[] nextlongArr(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public Long[] nextLongArr(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public int[] nextIntArr(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public Integer[] nextIntegerArr(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } 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 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
c72dac6e6919cf755a31da14cbcdf148
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 D { static int n; static char[] arr; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { n = sc.nextInt(); arr = sc.next().toCharArray(); get(); } pw.close(); } static void get() { if(n==1) System.out.println(0); else { long res = 0; for (int i = 0; i < n; i++) res += arr[i] == 'L' ? i : n - i - 1; int done = 0; long max = 0; for (int i = 0, j = n - 1; i < n / 2; i++, j--) { if (arr[i] == 'L') { res = Math.max(res,res+n-1-2*i); System.out.print(res + " "); if (res > max) max = res; done++; } if (arr[j] == 'R') { res=Math.max(res,res+1+2*j-n); if (res > max) max = res; System.out.print(res + " "); done++; } } if(max==0) max = res; while (done < n ) { System.out.print(max + " "); done++; } System.out.println(); } } static int gcd ( int a, int b){ return a == 0 ? b : gcd(b % a, a); } static int lcm ( int a, int b){ return a * b / gcd(a, b); } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair p) { return x == p.x ? y - p.y : x - p.x; } public String toString(){return x + " " + y;} } static class Tuple implements Comparable<Tuple> { int x; int y; char c; Tuple(char c,int x,int y){ this.c=c; this.x=x; this.y=y; } public int compareTo(Tuple t) { return x == t.x ? y - t.y : x - t.x; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public long[] nextlongArr(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public Long[] nextLongArr(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public int[] nextIntArr(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public Integer[] nextIntegerArr(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } 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 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
87a3185b206ef7f675c9b1503cba2858
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 Line { public static void main(String[] args) { try { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int testcases = in.nextInt(); while (testcases-- > 0) { int n = in.nextInt(); String s = in.next(); long total = 0; long[] extra = new long[n]; for (int i = 0; i < n; i++) { total = s.charAt(i) == 'L' ? total + i : total + n - i - 1; if (s.charAt(i) == 'L') { extra[i] = (n - i - 1 - i); } if (s.charAt(i) == 'R') { extra[i] = (i + i - n + 1); } } reverseSortLongArr(extra); long ans=total; for (int i = 0; i < n; i++) { if(extra[i]>=0) ans+=extra[i]; out.print(ans+" "); } out.println(); } out.close(); } catch (Exception e) { // TODO: handle exception return; } } 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()); } 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; } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static boolean LOCAL = System.getProperty("ONLINE_JUDGE") == null; static void dbg(Object... o) { if (LOCAL) System.err.println(Arrays.deepToString(o)); } static final Random random = new Random(); static final int mod = 1_000_000_007; static void sortIntArray(int[] a) { int n = a.length;// shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static void sortLongArray(long[] arr) { int n = arr.length; for (int i = 0; i < n; ++i) { long tmp = arr[i]; int randomPos = i + random.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } Arrays.sort(arr); } static void reverseSortLongArr(long[] arr) { int n = arr.length; for (int i = 0; i < n; ++i) { long tmp = arr[i]; int randomPos = i + random.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } Arrays.sort(arr); reverseLongArr(arr); } static void reverseSortIntArr(int[] a) { int n = a.length;// shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); reverseIntArr(a); } public static void reverseLongArr(long[] arr) { int n = arr.length; for (int i = 0; i < n / 2; i++) { long temp = arr[i]; arr[i] = arr[n - i - 1]; arr[n - i - 1] = temp; } } public static void reverseIntArr(int[] array) { int n = array.length; for (int i = 0; i < n / 2; i++) { int temp = array[i]; array[i] = array[n - i - 1]; array[n - i - 1] = temp; } } static long add(long a, long b) { return (a + b) % mod; } static long sub(long a, long b) { return ((a - b) % mod + mod) % mod; } static long mul(long a, long b) { return (a * b) % mod; } static long exp(long base, long exp) { if (exp == 0) return 1; long half = exp(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials = new long[2_000_001]; static long[] invFactorials = new long[2_000_001]; static void precompFacts() { factorials[0] = invFactorials[0] = 1; for (int i = 1; i < factorials.length; i++) factorials[i] = mul(factorials[i - 1], i); invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2); for (int i = invFactorials.length - 2; i >= 0; i--) invFactorials[i] = mul(invFactorials[i + 1], i + 1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k])); } 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 printArray(int arr[]) { for (int i : arr) { System.out.print(i + " "); } System.out.println(); } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }
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
b1aa07d48ec37b2341e515d24b2fea36
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 Line { public static void main(String[] args) { try { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int testcases = in.nextInt(); while (testcases-- > 0) { int n = in.nextInt(); String s = in.next(); long total = 0; for (int i = 0; i < n; i++) { total = s.charAt(i) == 'L' ? total + i : total + n - i - 1; } // dbg(total); long[] ans = new long[n + 1]; int count = 0; ans[0] = total; long[] extra=new long[n]; for (int i = 0; i < n; i++) { if (i < n / 2 && s.charAt(i) == 'L') { extra[count++] = (n - i - 1 - i); } if (i >= n / 2 && s.charAt(i) == 'R') { if(n%2!=0&&i==n/2) continue; extra[count++] = (i + i - n + 1); } } reverseSortLongArr(extra); //dbg(extra); for(int i=1;i<=n;i++) { ans[i]=ans[i-1]+extra[i-1]; } for (int i = 1; i <= n; i++) { out.print(ans[i] + " "); } out.println(); } out.close(); } catch (Exception e) { // TODO: handle exception return; } } 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()); } 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; } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static boolean LOCAL = System.getProperty("ONLINE_JUDGE") == null; static void dbg(Object... o) { if (LOCAL) System.err.println(Arrays.deepToString(o)); } static final Random random = new Random(); static final int mod = 1_000_000_007; static void sortIntArray(int[] a) { int n = a.length;// shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static void sortLongArray(long[] arr) { int n = arr.length; for (int i = 0; i < n; ++i) { long tmp = arr[i]; int randomPos = i + random.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } Arrays.sort(arr); } static void reverseSortLongArr(long[] arr) { int n = arr.length; for (int i = 0; i < n; ++i) { long tmp = arr[i]; int randomPos = i + random.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } Arrays.sort(arr); reverseLongArr(arr); } static void reverseSortIntArr(int[] a) { int n = a.length;// shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); reverseIntArr(a); } public static void reverseLongArr(long[] arr) { int n = arr.length; for (int i = 0; i < n / 2; i++) { long temp = arr[i]; arr[i] = arr[n - i - 1]; arr[n - i - 1] = temp; } } public static void reverseIntArr(int[] array) { int n = array.length; for (int i = 0; i < n / 2; i++) { int temp = array[i]; array[i] = array[n - i - 1]; array[n - i - 1] = temp; } } static long add(long a, long b) { return (a + b) % mod; } static long sub(long a, long b) { return ((a - b) % mod + mod) % mod; } static long mul(long a, long b) { return (a * b) % mod; } static long exp(long base, long exp) { if (exp == 0) return 1; long half = exp(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials = new long[2_000_001]; static long[] invFactorials = new long[2_000_001]; static void precompFacts() { factorials[0] = invFactorials[0] = 1; for (int i = 1; i < factorials.length; i++) factorials[i] = mul(factorials[i - 1], i); invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2); for (int i = invFactorials.length - 2; i >= 0; i--) invFactorials[i] = mul(invFactorials[i + 1], i + 1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k])); } 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 printArray(int arr[]) { for (int i : arr) { System.out.print(i + " "); } System.out.println(); } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }
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
5a3507e70abe29944424970e1fe6df71
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.StringTokenizer; public class Line { 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; } } public static void main(String[] args) { try { FastReader in = new FastReader(); int testcases = in.nextInt(); while (testcases-- > 0) { int n = in.nextInt(); String s = in.nextLine(); modifyAndPrint( s, n); System.out.println(); } } catch (Exception e) { // TODO: handle exception return; } } private static void modifyAndPrint( String s, int n) { long stringvalue = 0; long[] temp = new long[n]; for (int i = 0; i < n; i++) { if (s.charAt(i) == 'L') { stringvalue += i; temp[i] = Math.max(i,n-1-i)-i; } else { stringvalue += n - 1 - i; temp[i] = Math.max(i, n-1-i)-(n-1-i); } } Arrays.sort(temp); int k = 0; for (int i = 1; i <= n; i++) { stringvalue+=temp[n - 1 - k++]; System.out.print(stringvalue + " "); } } }
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
c2349228aedb848f5d92d003a91327b4
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.StringTokenizer; public class Line { 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; } } public static void main(String[] args) { try { FastReader in = new FastReader(); int testcases = in.nextInt(); while (testcases-- > 0) { int n = in.nextInt(); String s = in.nextLine(); long[] idealarr = new long[n+1]; modifyAndPrint(idealarr, s, n); System.out.println(); } } catch (Exception e) { // TODO: handle exception return; } } private static void modifyAndPrint(long[] idealarr, String s, int n) { long stringvalue = 0; long[] temp = new long[n]; if (n % 2 == 0) { for (int i = 0; i < n / 2; i++) { if (s.charAt(i) == 'L') { stringvalue += i; temp[i] = n - 2 * i - 1; } else { stringvalue += n - 1 - i; temp[i] = 0; } } // second half for (int i = n / 2; i < n; i++) { if (s.charAt(i) == 'L') { stringvalue += i; temp[i] = 0; } else { stringvalue += n - 1 - i; temp[i] = 2 * i - n + 1; } } } else { // first half stringvalue=n/2; for (int i = 0; i < n / 2; i++) { if (s.charAt(i) == 'L') { stringvalue += i; temp[i] = n - 2 * i - 1; } else { stringvalue += n - 1 - i; temp[i] = 0; } } temp[n / 2] = 0; // second half for (int i = n / 2 + 1; i < n; i++) { if (s.charAt(i) == 'L') { stringvalue += i; temp[i] = 0; } else { stringvalue += n - 1 - i; temp[i] = 2 * i - n + 1; } } } idealarr[0]=stringvalue; Arrays.sort(temp); int k=0; for(int i=1;i<=n;i++) { idealarr[i]=idealarr[i-1]+temp[n-1-k++]; System.out.print(idealarr[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
d160d07842f0b6aa9606053c484f3c54
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.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; public class D1722_Line { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); byte t = scanner.nextByte(); while (t-- > 0) { int n = scanner.nextInt(); String line = scanner.next(); if (n == 1) { System.out.println(0); continue; } // initializations long value = 0; Stack<Integer> stackR = new Stack<>(); Queue<Integer> queueL = new LinkedList<>(); for (int i = 0; i < n; i++) { if (line.charAt(i) == 'L') { queueL.add(i); value += i; } else { stackR.push(i); value += n - 1 - i; } } // calculate each k int mid = n / 2 - 1; StringBuilder result = new StringBuilder(); for (int i = 0; i < n; i++) { result.append(" "); //* if (stackR.isEmpty() && queueL.isEmpty()) result.append(value); else if(stackR.isEmpty()) { if (queueL.peek() <= mid) value = poll(queueL, value, n); result.append(value); } else if (queueL.isEmpty()) { if (stackR.peek() > mid) value = pop(stackR, value, n); result.append(value); } else { if (stackR.peek() > mid && queueL.peek() <= mid) { value = value - n + 1 + 2 * stackR.peek() > value + n - 1 - 2 * queueL.peek() ? pop(stackR, value, n) : poll(queueL, value, n); } else if (stackR.peek() > mid) value = pop(stackR, value, n); else if (queueL.peek() <= mid) value = poll(queueL, value, n); result.append(value); } } System.out.println(result.deleteCharAt(0)); //* } } public static long pop(Stack<Integer> stack, long value, int n) { int pos = stack.pop(); return value + 1 - n + 2 * pos; } public static long poll(Queue<Integer> q, long value, int n) { int pos = q.poll(); return value + n - 1 - 2 * pos; } }
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
9247df2ed5c67bcc6df1501634747370
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.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; public class D1722_Line { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); byte t = scanner.nextByte(); while (t-- > 0) { int n = scanner.nextInt(); String line = scanner.next(); if (n == 1) { System.out.println(0); continue; } // initializations long value = 0; Stack<Integer> stackR = new Stack<>(); Queue<Integer> queueL = new LinkedList<>(); for (int i = 0; i < n; i++) { if (line.charAt(i) == 'L') { queueL.add(i); value += i; } else { stackR.push(i); value += n - 1 - i; } } // calculate each k int mid = n / 2 - 1; StringBuilder result = new StringBuilder(); for (int i = 0; i < n; i++) { result.append(" "); //* if (stackR.isEmpty() && queueL.isEmpty()) result.append(value); else if(stackR.isEmpty()) { if (queueL.peek() <= mid) value = poll(queueL, value, n); result.append(value); } else if (queueL.isEmpty()) { if (stackR.peek() > mid) value = pop(stackR, value, n); result.append(value); } else { if (stackR.peek() > mid && queueL.peek() <= mid) { if ( value - n + 1 + 2 * stackR.peek() > value + n - 1 - 2 * queueL.peek() ) value = pop(stackR, value, n); else value = poll(queueL, value, n); } else if (stackR.peek() > mid) value = pop(stackR, value, n); else if (queueL.peek() <= mid) value = poll(queueL, value, n); result.append(value); } } System.out.println(result.deleteCharAt(0)); //* } } public static long pop(Stack<Integer> stack, long value, int n) { int pos = stack.pop(); return value + 1 - n + 2 * pos; } public static long poll(Queue<Integer> q, long value, int n) { int pos = q.poll(); return value + n - 1 - 2 * pos; } }
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
39ec733c408880702564810dfd3ceebd
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.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; public class D1722_Line { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); byte t = scanner.nextByte(); while (t-- > 0) { int n = scanner.nextInt(); String line = scanner.next(); if (n == 1) { System.out.println(0); continue; } // initial line value long value = 0; Stack<Integer> stackR = new Stack<>(); Queue<Integer> queueL = new LinkedList<>(); for (int i = 0; i < n; i++) { if (line.charAt(i) == 'L') { queueL.add(i); value += i; } else { stackR.push(i); value += n - 1 - i; } } // calculate each k int mid = n / 2 - 1; StringBuilder result = new StringBuilder(); for (int i = 0; i < n; i++) { result.append(" "); //* if (stackR.isEmpty() && queueL.isEmpty()) result.append(value); else if(stackR.isEmpty()) { if (queueL.peek() <= mid) { int pos = queueL.poll(); value += n - 1 - 2 * pos; } result.append(value); } else if (queueL.isEmpty()) { if (stackR.peek() > mid) { int pos = stackR.pop(); value += 1 - n + 2 * pos; } result.append(value); } else { if (stackR.peek() > mid && queueL.peek() <= mid) { if( value - n + 1 + 2 * stackR.peek() > value + n - 1 - 2 * queueL.peek() ) { int pos = stackR.pop(); value += 1 - n + 2 * pos; } else { int pos = queueL.poll(); value += n - 1 - 2 * pos; } } else if (stackR.peek() > mid) { int pos = stackR.pop(); value += 1 - n + 2 * pos; } else if (queueL.peek() <= mid) { int pos = queueL.poll(); value += n - 1 - 2 * pos; } result.append(value); } } System.out.println(result.deleteCharAt(0)); //* } } }
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
2e53e4868654fd933470ae27f9527226
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 _817D_Line { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int totalTestCase = scanner.nextInt(); while (totalTestCase-- > 0) { int n = scanner.nextInt(); String input = scanner.next(); solve(n, input); } } private static void solve(int n, String input) { // count init looking long[] initArray = new long[n]; long initTotal = 0; for (int i = 0; i < input.length(); i++) { if (input.charAt(i) == 'L') { initArray[i] = i; initTotal += initArray[i]; } else { initArray[i] = n - 1 - i; initTotal += initArray[i]; } } long maxValue = 0; if (n % 2 == 0) { for (long i = n / 2; i < n; i++) { maxValue += i*2; } } else { for (long i = n / 2 + 1; i < n; i++) { maxValue += i*2; } maxValue += n / 2; } // count result array; long[] resultArray = new long[n]; Arrays.fill(resultArray, maxValue); int leftpolong = 0, rightPolong = n - 1; int midPolong = n / 2, i = 0, m = n; while (m > 0) { if (leftpolong < midPolong) { if (input.charAt(leftpolong) != 'R') { resultArray[i] = initTotal + ((n - 1 - leftpolong) - initArray[leftpolong]); initTotal = resultArray[i]; m--; i++; } leftpolong++; } if (m <= 0 || (leftpolong >= midPolong && rightPolong < midPolong)) { break; } if (rightPolong >= midPolong) { if (input.charAt(rightPolong) != 'L') { resultArray[i] = initTotal + (rightPolong - initArray[rightPolong]); initTotal = resultArray[i]; m--; i++; } rightPolong--; } } for (int j = 0; j < resultArray.length; j++) { System.out.print(resultArray[j] + " "); } 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
a3c653877c6dda35ea356d6700fccd06
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.List; import java.util.Scanner; public class Solution { static class Pair implements Comparable { int index; Integer value; public Pair(int j, int i) { this.index = j; this.value = i; } @Override public int compareTo(Object o) { Pair o1 = (Pair) o; return o1.value.compareTo(this.value); } @Override public String toString() { return "Pair{" + "index=" + index + ", value=" + value + '}'; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); scanner.nextLine(); for (int i = 0; i < t; i++) { int n = scanner.nextInt(); scanner.nextLine(); String s = scanner.nextLine(); List<Pair> list = new ArrayList<>(); long[] sum = new long[s.length()]; for (int j = 0; j < s.length(); j++) { char c = s.charAt(j); if (c == 'L') { if ((s.length() - j - 1) > j) { list.add(new Pair(j, s.length() - j - 1)); } sum[j] = j; } else { if (j > (s.length() - j - 1)) { list.add(new Pair(j, j)); } sum[j] = s.length() - j - 1; } } Collections.sort(list); long su = Arrays.stream(sum).sum(); for (int j = 0; j < n; j++) { if (j < list.size()) { Pair pair = list.get(j); int index = pair.index; su -= sum[index]; su += pair.value; } System.out.print(su + " "); } 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
1caa31aba7378f21b6b5d4be5da9f15f
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 kflip { public static void main(String args[]) throws IOException { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); String s=sc.next(); long arr[]=new long[n]; long sum=0; for(int i=0;i<s.length();i++) { if(s.charAt(i)=='L') { if(i==0) arr[i]=0; else arr[i]=i; } else if(s.charAt(i)=='R') { if(i==s.length()-1) arr[i]=0; else arr[i]=n-i-1; } sum+=arr[i]; } //for(int i=0;i<arr.length;i++) // System.out.print(arr[i]); // System.out.println(); ArrayList<Long> modified=new ArrayList<>(); for(int i=0;i<n;i++) { char ch=s.charAt(i); if(ch=='R') { int temp=i; if(temp>arr[i]) modified.add(temp*1L); } else if(ch=='L') { int temp=n-i-1; if(temp>arr[i]) modified.add(temp*1L); } } //System.out.println(modified); Collections.sort(modified,Collections.reverseOrder()); Arrays.sort(arr); long res[]=new long[n]; // for(int i=0;i<n;i++) // { // int q=sum; // for(int j=0;j<Math.min(i+1,modified.size());j++) // { // if(modified.get(j)>arr[i]) // { // q+=(modified.get(j)-arr[i]); // } // } // res[i]=q; // } // int i,hold=0; // for(i=0;i<n;i++) // { // int c=0; // hold=sum; // int pos=0; // boolean flag=true; // while(c<=Math.min(i,modified.size()-1)) // { // if(modified.get(c)>arr[pos++]) // { // hold+=(modified.get(c)-arr[c]); // } // else // { // flag=false; // break; // } // c++; // } // res[i]=hold; // if(!flag) // break; // } // if(i!=n) // { // for(int w=i+1;w<n;w++) // { // res[w]=hold; // } // } long cpy[]=new long[arr.length]; System.arraycopy(arr, 0, cpy, 0, arr.length); for(int i=0;i<modified.size();i++) { if(modified.get(i)>arr[i]) cpy[i]=modified.get(i); else break; } for(int i=0;i<res.length;i++) { sum+=(cpy[i]-arr[i]); res[i]=sum; } for(int r=0;r<n;r++) { System.out.print(res[r]+" "); } 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 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
01c2e267f8f4ba3e18bd856cc93542f0
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.TreeSet; public class D { public static void main(String args[]){ Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while(t-->0){ int n = scanner.nextInt(); StringBuilder stb = new StringBuilder(scanner.next()); long lcnt = 0, rcnt = 0, cnt; TreeSet<Integer> ltre = new TreeSet<>(); TreeSet<Integer> rtre = new TreeSet<>(); for(int j=0;j<n;j++){ if(stb.charAt(j) == 'L'){ lcnt+=j; ltre.add(j); } else{ rcnt += n - j - 1; rtre.add(j); } } cnt = lcnt + rcnt; long max = cnt; n-=1; for(int i=0;i<n+1;i++){ //int lio = stb.lastIndexOf("R"); //int io = Math.abs(stb.indexOf("L")); int lio = 0, io = 0; if(!rtre.isEmpty()) lio = rtre.last(); if(!ltre.isEmpty()) io = ltre.first(); if(lio >= n-io){ //stb.replace(lio, lio+1, "L"); ltre.add(lio); rtre.remove(lio); cnt += lio - (n-lio); if(cnt>max) max = cnt; System.out.printf("%d ", max); } else{ //stb.replace(io, io+1, "R"); rtre.add(io); ltre.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 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
c02b27b4b987a80e1bcf2f9ea505049f
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.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.*; import static java.lang.System.out; public class app { public static long m; static int mod = 998244353; static int inf = (int) 1e9; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); //int t=1; while (t-- > 0) { int s=sc.nextInt(); String d=sc.next(); long f[]=new long[s]; long df=0; ArrayList<Integer>cx=new ArrayList<>(); for(int i=0;i<s;i++){ if(i>s-i-1&&d.charAt(i)=='R'){ cx.add(Math.max(i,s-i-1)-Math.min(i,s-i-1)); }else if(i<s-i-1&&d.charAt(i)=='L'){ cx.add(Math.max(i,s-i-1)-Math.min(i,s-i-1)); } df+=Math.max(i,s-i-1); } Collections.sort(cx); if(cx.size()>0) cx.remove(cx.get(cx.size()-1)); for(int i=s-1;i>=cx.size();i--){ f[i]=df; } for(int i=cx.size()-1;i>=0;i--){ df-=cx.get(cx.size()-1-i); f[i]=df; } out.println(Arrays.toString(f).replace(",","").replace("]","").replace("[","")); } } //out.println(Arrays.toString(d).replace(",","").replace("]","").replace("[","")); 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 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 class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { 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 String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextArrInt(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextArrLong(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextArrIntSorted(int n) throws IOException { int[] a = new int[n]; Integer[] a1 = new Integer[n]; for (int i = 0; i < n; i++) a1[i] = nextInt(); Arrays.sort(a1); for (int i = 0; i < n; i++) a[i] = a1[i].intValue(); return a; } public long[] nextArrLongSorted(int n) throws IOException { long[] a = new long[n]; Long[] a1 = new Long[n]; for (int i = 0; i < n; i++) a1[i] = nextLong(); Arrays.sort(a1); for (int i = 0; i < n; i++) a[i] = a1[i].longValue(); 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 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
1ffa46edcef47411fe053adaee2e4559
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 Test { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { int n = Integer.parseInt(br.readLine()); String str = br.readLine(); long total = 0L; int[] arr = new int[n]; int mid = (0 + (n - 1)) / 2; for (int i = 0; i < n; i++) { char currentChar = str.charAt(i); if (currentChar == 'L') { total += i - 0; int existingVal = i - 0; int newVal = (n - 1) - i; arr[i] = newVal - existingVal; } else { total += (str.length() - 1) - i; int existingVal = (n - 1) - i; int newVal = i - 0; arr[i] = newVal - existingVal; } } Arrays.sort(arr); for (int i = n - 1; i >= 0; i--) { int profitVal = arr[i]; total += (profitVal + total > total) ? profitVal : 0; System.out.print(total + " "); } System.out.println(); } } public static int[] sumPrefixScores(String[] words) { Map<String, Integer> map = new LinkedHashMap<>(); int[] arr = new int[words.length]; for (int i = 0; i < words.length; i++) { String str = words[i]; String prefix = ""; for (int j = 0; j < str.length(); j++) { prefix += str.charAt(j) + ""; Integer count = null; if (map.containsKey(prefix)) count = map.get(prefix); else count = 0; count += 1; map.put(prefix, count); } } for (int i = 0; i < words.length; i++) { String str = words[i]; String prefix = ""; int count = 0; for (int j = 0; j < str.length(); j++) { prefix += str.charAt(j) + ""; count += map.get(prefix); } arr[i] = count; } return arr; } /** * method to print int value in console output **/ private static void debug(int value) { if (System.getProperty("ONLINE_JUDGE") == null) { System.out.println("int value = " + value); } } /** * method to print int value in console output with a text message **/ private static void debug(int value, String message) { if (System.getProperty("ONLINE_JUDGE") == null) { if (message.charAt(message.length() - 1) != ' ') message += " "; System.out.println(message + "" + value); } } /** * method to print long value in console output **/ private static void debug(long value) { if (System.getProperty("ONLINE_JUDGE") == null) { System.out.println("long value = " + value); } } /** * method to print long value in console output with a text message **/ private static void debug(long value, String message) { if (System.getProperty("ONLINE_JUDGE") == null) { if (message.charAt(message.length() - 1) != ' ') message += " "; System.out.println(message + "" + value); } } /** * method to print String value in console output **/ private static void debug(String value) { if (System.getProperty("ONLINE_JUDGE") == null) { System.out.println("String value = " + value); } } /** * method to print String value in console output with a text message **/ private static void debug(String value, String message) { if (System.getProperty("ONLINE_JUDGE") == null) { if (message.charAt(message.length() - 1) != ' ') message += " "; System.out.println(message + "" + value); } } /** * method to print character value in console output **/ private static void debug(char value) { if (System.getProperty("ONLINE_JUDGE") == null) { System.out.println("Character value = " + value); } } /** * method to print character value in console output with a text message **/ private static void debug(char value, String message) { if (System.getProperty("ONLINE_JUDGE") == null) { if (message.charAt(message.length() - 1) != ' ') message += " "; System.out.println(message + "" + value); } } /** * method to print double value in console output **/ private static void debug(double value) { if (System.getProperty("ONLINE_JUDGE") == null) { System.out.println("Double value = " + value); } } /** * method to print double value in console output with a text message **/ private static void debug(double value, String message) { if (System.getProperty("ONLINE_JUDGE") == null) { if (message.charAt(message.length() - 1) != ' ') message += " "; System.out.println(message + "" + value); } } /** * method to print integer type array value in console output **/ private static void debug(int[] arr) { if (System.getProperty("ONLINE_JUDGE") == null) { int n = arr.length; System.out.print("["); for (int i = 0; i < n; i++) { if (i < n - 1) System.out.print(arr[i] + ", "); else System.out.print(arr[i]); } System.out.println("]"); } } /** * method to print long type array value in console output **/ private static void debug(long[] arr) { if (System.getProperty("ONLINE_JUDGE") == null) { int n = arr.length; System.out.print("["); for (int i = 0; i < n; i++) { if (i < n - 1) System.out.print(arr[i] + ", "); else System.out.print(arr[i]); } System.out.println("]"); } } /** * method to print long type array value in console output **/ private static void debug(String[] arr) { if (System.getProperty("ONLINE_JUDGE") == null) { int n = arr.length; System.out.print("["); for (int i = 0; i < n; i++) { if (i < n - 1) System.out.print(arr[i] + ", "); else System.out.print(arr[i]); } System.out.println("]"); } } /** * method to print char type array value in console output **/ private static void debug(char[] arr) { if (System.getProperty("ONLINE_JUDGE") == null) { int n = arr.length; System.out.print("["); for (int i = 0; i < n; i++) { if (i < n - 1) System.out.print(arr[i] + ", "); else System.out.print(arr[i]); } System.out.println("]"); } } /** * method to print double type array value in console output **/ private static void debug(double[] arr) { if (System.getProperty("ONLINE_JUDGE") == null) { int n = arr.length; System.out.print("["); for (int i = 0; i < n; i++) { if (i < n - 1) System.out.print(arr[i] + ", "); else System.out.print(arr[i]); } System.out.println("]"); } } /** * please ignore the below code as it's just used for * taking faster input in java */ 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(); } } }
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
1f58cac1d20dde75851b9b969155ee03
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 Codechef { public static void main (String[] args) throws java.lang.Exception { try{ FastReader read=new FastReader(); StringBuilder sb = new StringBuilder(); int t=read.nextInt(); for(int k=1;k<=t;k++) { //sb.append("Case #"+k+": "); int n = read.nextInt(); String s = read.next(); char ch[] = s.toCharArray(); long count = 0; for(int i=0;i<n;i++) { if(ch[i]=='L') count+=i; else count+=n-i-1; } int i=0,j=n-1,c1 = 0,c2=0; for(int o=1;o<=n;o++) { if(i>j) { sb.append(count+" "); } else { while(i<=j) { if(i<=(n-j-1)) { if(ch[i]=='L') { //System.out.print(i+" "+count+" "); count-= i; count+= n-i-1; //System.out.println(count); c1++; } i++; } else { if(ch[j]=='R') { //System.out.print(j+" "+count+" "); count-= n-j-1; count+= j; //System.out.println(count); c1++; } j--; } if(c1>=o) break; } sb.append(count+" "); } } sb.append('\n'); } System.out.println(sb); } catch(Exception e) {return; } } 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 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 void sort(ArrayList<Long> A,char ch) { int n = A.size(); Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A.get(i); int randomPos = i + rnd.nextInt(n - i); A.set(i,A.get(randomPos)); A.set(randomPos,tmp); } Collections.sort(A); } static void sort(ArrayList<Integer> A) { int n = A.size(); Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A.get(i); int randomPos = i + rnd.nextInt(n - i); A.set(i,A.get(randomPos)); A.set(randomPos,tmp); } Collections.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(); } } class pair implements Comparable<pair> { int X,Y,C; pair(int s,int e,int c) { X=s; Y=e; C=c; } public int compareTo(pair p ) { return this.C-p.C; } }
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
c84e452d57608c69763839f272097ef2
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.HashMap; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int i = 0; i < t; ++i) { int len = in.nextInt(); String ki = in.next(); StringBuilder subS = new StringBuilder(ki); long score = 0; for (int j = 0; j < len; ++j) { if (subS.charAt(j) == 'R') score += len - j - 1; else score += j; } /* идти с двух концов к середине и смотреть на чар, если уже стоит так, как надо, то збс, а если не стоит, то меняем: если до половины строки, то L->R, иначе R->L */ int k = 0; for (int j = 0; j < len; ++j) { boolean changed = false; while (!changed && k != len) { if (k % 2 == 0) { if (subS.charAt(k/2) == 'R') { ++k; continue; } else { changed = true; score -= k / 2; score += len - k/2 - 1; subS.setCharAt(k/2, 'R'); } } else { if (subS.charAt(len-k/2-1) == 'L') { ++k; continue; } else { changed = true; score -= k / 2; score += len - k / 2 - 1; subS.setCharAt(len-k/2 - 1, 'L'); } } ++k; } System.out.print(score + " "); } 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
79c84e3e14d146e43d8df29f46706eaa
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 HelloWorld { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-->0){ int n = scan.nextInt(); String str = scan.next(); ArrayList<Long> list = new ArrayList<>(); long total=0; for(int i=0; i<n; i++){ if(str.charAt(i) == 'L'){ list.add((n-i-1) - (long)i); total += i; }else{ list.add((long)i - (n-i-1)); total += n-i-1; } } Collections.sort(list, Collections.reverseOrder()); for(int i=0; i<list.size(); i++){ if(list.get(i) > 0){ total += list.get(i); } System.out.print(total + " "); } 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
635c4b304d9a2a6483ecd2c005af70fd
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.Math; public class CrossMarket { public static void main(String args[]){ Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while(test > 0){ int len = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); char ch[] = s.toCharArray(); long sum = 0; len = s.length(); for(int i=0;i<len;i++){ if(s.charAt(i) == 'L') sum += i; else sum += (len - i - 1); } long ans[] = new long[len]; int index = 0; int left = 0; int right = len - 1; while(left <= right){ if(index == len) break; if(ch[left] == 'L'){ long newSum = (long)(sum - left + (len-left-1)); ans[index] = newSum; index++; sum = newSum; ch[left] = 'R'; } else if(ch[right] == 'R'){ long newSum = (long)(sum - (len-right-1) + right); ans[index] = newSum; index++; sum = newSum; ch[right] = 'L'; } else{ ans[index] = sum; left++; right--; } } for(int i=index+1;i<len;i++) ans[i] = sum; for(int i=0;i<len;i++) System.out.print(ans[i]+" "); System.out.println(); test--; } } }
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
c025db0a08f5d9e60b34c8400eba310e
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.*; public class tes { public static void main (String[] args)throws java.lang.Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(),i,c=0; String s=sc.next(); long f=0; for(i=0;i<n;i++) { if(s.charAt(i)=='R') f=f+(n-i-1); else f=f+i; } for( i=0;i<n-i-1;i++) { if(s.charAt(i)=='L') { f=f+n-i-1-i; System.out.print(f+" "); c++; } if(s.charAt(n-i-1)=='R') { f=f+n-i-1-i; System.out.print(f+" "); c++; } } while(c<n) { System.out.print(f+" ");c++; } 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
fb711d2b5c7b2c6af5654d15b0538e68
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 comp { public static void main(String[] args) { Scanner s=new Scanner(System.in); int n=s.nextInt(); for(int r=0;r<n;r++){ int t=s.nextInt(); String str=s.next(); int count=0; long sum=0; int [] arr=new int[t]; for(int i=0;i<t;i++){ if(str.charAt(i)=='L'){ sum+=i; int change=t-1-i-i; arr[i]=change; }else{ sum+=t-1-i; int change=i-(t-1-i); arr[i]=change; } } Arrays.sort(arr); for(int i=t-1;i>=0;i--){ if(sum+arr[i]>sum){ sum+=arr[i]; System.out.print(sum+" "); }else{ System.out.print(sum+" "); } } System.out.println(); // char [] arr=str.toCharArray(); // //System.out.println(sum+" sum " ); // for(int i=0;i<t/2;i++){ // // System.out.print(i+" = i "); // if(arr[i]=='L'){ // count++; // sum=sum-i+t-1-i; // arr[i]='R'; // i--; // System.out.print(sum+" "); // }else if(arr[t-1-i]=='R'){ // count++; // int m=t-1-i; // sum=sum-(t-1-m)+m; // System.out.print(sum+" "); // } // } // for(int i=count;i<t;i++){ // 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 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
cd504642569879bedc6f4ed1fb1d11ea
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 comp { public static void main(String[] args) { Scanner s=new Scanner(System.in); int n=s.nextInt(); for(int r=0;r<n;r++){ int t=s.nextInt(); String str=s.next(); int count=0; long sum=0; for(int i=0;i<t;i++){ if(str.charAt(i)=='L'){ sum+=i; }else{ sum+=t-1-i; } } char [] arr=str.toCharArray(); //System.out.println(sum+" sum " ); for(int i=0;i<t/2;i++){ // System.out.print(i+" = i "); if(arr[i]=='L'){ count++; sum=sum-i+t-1-i; arr[i]='R'; i--; System.out.print(sum+" "); }else if(arr[t-1-i]=='R'){ count++; int m=t-1-i; sum=sum-(t-1-m)+m; System.out.print(sum+" "); } } for(int i=count;i<t;i++){ 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 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
f868a84412d49f9fb92923551a45bea8
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 FileNotFoundException { InputStream inputStream = System.in; OutputStream outputStream = System.out; boolean oj = System.getProperty("ONLINE_JUDGE") != null; if (!oj) { inputStream = new FileInputStream("input2.txt"); //outputStream = new FileOutputStream(new File("output.txt")); } InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int t = in.nextInt(); for (int i = 0; i < t; i++) { solve(in, out); } out.close(); } static void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); char[] arr = in.next().toCharArray(); int[] dis = new int[n]; long sum = 0; for (int i = 0; i < n; i++) { if (arr[i] == 'L'){ dis[i] = i; }else{ dis[i] = n - 1 - i; } sum += dis[i]; } int l = 0; int r = n - 1; int k = n; while ((l < n / 2 || r > n / 2) && k != 0){ if (l < n / 2 && arr[l] == 'L'){ arr[l] = 'R'; k--; sum += n - 1 - l - dis[l]; System.out.print(sum + " "); } if (r >= n / 2 && arr[r] == 'R'){ arr[r] = 'L'; k--; sum += r - dis[r]; System.out.print(sum + " "); } r--; l++; } for (int i = 0; i < k; i++) { System.out.print(sum + " "); } out.println(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); 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(); } int nextInt() { return Integer.parseInt(next()); } 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 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
aa5a9975a1cfc0238055100a2c1edf3b
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 FileNotFoundException { InputStream inputStream = System.in; OutputStream outputStream = System.out; boolean oj = System.getProperty("ONLINE_JUDGE") != null; if (!oj) { inputStream = new FileInputStream("input2.txt"); //outputStream = new FileOutputStream(new File("output.txt")); } InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int t = in.nextInt(); for (int i = 0; i < t; i++) { solve(in, out); } out.close(); } static void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); char[] arr = in.next().toCharArray(); int[] dis = new int[n]; long sum = 0; for (int i = 0; i < n; i++) { if (arr[i] == 'L'){ dis[i] = i; }else{ dis[i] = n - 1 - i; } sum += dis[i]; } int l = 0; int r = n - 1; int k = n; StringBuilder res = new StringBuilder(); while ((l < n / 2 || r > n / 2) && k != 0){ if (l < n / 2 && arr[l] == 'L'){ arr[l] = 'R'; k--; sum += n - 1 - l - dis[l]; res.append(sum); res.append(" "); } if (r >= n / 2 && arr[r] == 'R'){ arr[r] = 'L'; k--; sum += r - dis[r]; res.append(sum); res.append(" "); } r--; l++; } for (int i = 0; i < k; i++) { res.append(sum); res.append(" "); } out.println(res); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); 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(); } int nextInt() { return Integer.parseInt(next()); } 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 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
d3e574596f87f0da86f4e8e2b8d92423
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 FileNotFoundException { InputStream inputStream = System.in; OutputStream outputStream = System.out; boolean oj = System.getProperty("ONLINE_JUDGE") != null; if (!oj) { inputStream = new FileInputStream("input2.txt"); //outputStream = new FileOutputStream(new File("output.txt")); } InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int t = in.nextInt(); for (int i = 0; i < t; i++) { solve(in, out); } out.close(); } static void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); char[] arr = in.next().toCharArray(); int[] dis = new int[n]; long sum = 0; for (int i = 0; i < n; i++) { if (arr[i] == 'L'){ dis[i] = i; }else{ dis[i] = n - 1 - i; } sum += dis[i]; } int l = 0; int r = n - 1; int k = n; StringBuilder res = new StringBuilder(); while ((l < n / 2 || r > n / 2) && k != 0){ if (l < n / 2 && arr[l] == 'L'){ arr[l] = 'R'; k--; sum += n - 1 - l - dis[l]; res.append(sum); res.append(" "); l++; }else{ if (l < n / 2){ l++; } } if (r >= n / 2 && arr[r] == 'R'){ arr[r] = 'L'; k--; sum += r - dis[r]; res.append(sum); res.append(" "); r--; }else{ if (r > n / 2){ r--; } } } for (int i = 0; i < k; i++) { res.append(sum); res.append(" "); } out.println(res); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); 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(); } int nextInt() { return Integer.parseInt(next()); } 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 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
5283d993d7ef14622496cec4fe42e7d1
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.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Solve_1 { public static void main(String[] args) throws FileNotFoundException { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); while (n > 0){ n--; long ans = 0; int k = scanner.nextInt(); char[] line = scanner.next().toCharArray(); long[] dp = new long[k+1]; for (int i = 0; i < k; i++) { if (line[i] == 'L'){ ans += i; } else{ ans += k-1-i; } } dp[0] = ans; int m = 1; for (int i = 0; i < k/2; i++){ if (line[i] == 'L'){ dp[m] = dp[m-1] - i + k - i - 1; m++; } if(line[k-1-i] == 'R'){ dp[m] = dp[m-1] - i + k - i - 1; m++; } } while (m <= k) { dp[m] = dp[m-1]; m++; } for (int i = 1; i < dp.length; i++) { System.out.print(dp[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 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
7acf1253532d4462fd239f7ce3889e31
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 Exception{ PrintWriter out = new PrintWriter(System.out); Input in = new Input(); int test = in.getInt(); while(test-->0){ int n = in.getInt(); String s = in.getString(); ArrayList<Integer> a = new ArrayList<>(); long sum = 0L; for(int i=0;i<n;i++){ if(s.charAt(i)=='L'){ a.add(i); sum+=i; } else{ a.add(n-i-1); sum+=(n-i-1); } } Collections.sort(a); boolean flag = false; for(int b: a){ if(b>=(n-1-b)){ flag = true; } if(flag){ out.print(sum+" "); } else{ sum-=b; sum+=(n-1-b); out.print(sum+" "); } } out.println(); } out.close(); in.close(); } } class Input{ BufferedReader in; StringTokenizer st; public Input() throws Exception{ in = new BufferedReader(new InputStreamReader(System.in)); st = null; } public String getString() throws Exception{ while(st==null||st.hasMoreTokens()==false){ st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public boolean getBoolean() throws Exception{ return Boolean.parseBoolean(getString()); } public byte getByte() throws Exception{ return Byte.parseByte(getString()); } public short getShort() throws Exception{ return Short.parseShort(getString()); } public int getInt() throws Exception{ return Integer.parseInt(getString()); } public long getLong() throws Exception{ return Long.parseLong(getString()); } public float getFloat() throws Exception{ return Float.parseFloat(getString()); } public double getDouble() throws Exception{ return Double.parseDouble(getString()); } public String getLine() throws Exception{ return in.readLine(); } public void close() throws Exception{ if(in!=null) in.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
8df8278a07ae24814720d17fe73f62f7
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 { static class Pair implements Comparable<Pair> { int val,idx; Pair(int val,int idx) { this.val=val; this.idx=idx; } public int compareTo(Pair o) { return this.val-o.val; } } public static void main(String args[]) { int i,j,k,t,n; Scanner sc=new Scanner(System.in); t=sc.nextInt(); while(t-->0) { n=sc.nextInt(); String s=sc.next(); long tsum=0; PriorityQueue<Pair>pq=new PriorityQueue<>(); for(i=0;i<n;i++) { if(s.charAt(i)=='R') { int v=n-1-i; tsum+=v; pq.add(new Pair(v,i)); } else { tsum+=i; pq.add(new Pair(i,i)); } } while(pq.size()>0) { Pair p=pq.remove(); //System.out.println(p.val+" "+p.idx); int max=Math.max(p.idx, n-1-p.idx); tsum=tsum-p.val; tsum=tsum+max; System.out.print(tsum+" "); } 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
4fa365072381f3e6f1dbd309d5e765ce
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 class20 { 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 class pair implements Comparable<pair>{ int h,b; pair(int h,int b) { this.h=h; this.b=b; } public int compareTo(pair o) { return o.h-this.h; } } public static void main(String arg[]) { FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); String s=sc.next(); char ch[]=s.toCharArray(); int l[]=new int[n]; int r[]=new int[n]; long sum=0; int i,x=0,y=0; for(i=0;i<n;i++) { if(i<n/2) { if(ch[i]=='L') { l[x++]=i; } } else { if(ch[i]=='R') { r[y++]=i; } } if(ch[i]=='L') { sum+=(long)i-0; } else { sum+=(long)n-i-1; } } int j=0,k=y-1; for(i=1;i<=n;i++) { if(j==x && k==-1) System.out.print(sum+" "); else if(j==x) { sum-=(long)n-r[k]-1; sum+=(long)r[k]-0; k--; System.out.print(sum+" "); } else if(k==-1) { sum-=(long)l[j]-0; sum+=(long)n-l[j]-1; j++; System.out.print(sum+" "); } else { int diff1=l[j]-0; int diff2=n-r[k]-1; if(diff1<diff2) { sum-=(long)l[j]-0; sum+=(long)n-l[j]-1; j++; } else { sum-=(long)n-r[k]-1; sum+=(long)r[k]-0; 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 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
957a9b52d1ad408c435f6cd136dc72f6
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 { public static int findL(String str,int l) { for(int i=l+1;i<str.length();i++) { if(str.charAt(i)=='L') return i; } return str.length(); } public static int findR(String str,int r) { for(int i=r-1;i>=0;i--) { if(str.charAt(i)=='R') return i; } return -1; } public static void main (String[] args) throws Exception { //code BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t-->0) { ArrayList<Long> arr=new ArrayList<Long>(); long val=0; int n=Integer.parseInt(br.readLine()); String str=br.readLine(); for(int i=0;i<n;i++) { if(str.charAt(i)=='L') { val=val+i; } else{ val=val+n-i-1; } } int l=-1; int r=n; for(int i=0;i<n;i++) { int templ=findL(str,l); int tempr=findR(str,r); if((templ<n/2)&&(tempr>=n/2)) { if(templ<(n-tempr-1)) { l=templ; val=val-l+n-l-1; } else { r=tempr; val=val-(n-tempr-1)+tempr; } } else if(templ<n/2) { l=templ; val=val-l+n-l-1; } else if(tempr>=n/2) { r=tempr; val=val-(n-tempr-1)+tempr; } arr.add(val); } StringBuffer sb=new StringBuffer(); for(int i=0;i<n;i++) { sb.append(arr.get(i)+" "); } 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 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
6bba1bad31e4d77aab1dd02174e7c373
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 { public static long findL(String str,long l) { for(long i=l+1;i<str.length();i++) { if(str.charAt((int)i)=='L') return i; } return str.length(); } public static long findR(String str,long r) { for(long i=r-1;i>=0;i--) { if(str.charAt((int)i)=='R') return i; } return -1; } public static void main (String[] args) throws Exception { //code BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t-->0) { ArrayList<Long> arr=new ArrayList<Long>(); long val=0; long n=Integer.parseInt(br.readLine()); String str=br.readLine(); for(int i=0;i<n;i++) { if(str.charAt(i)=='L') { val=val+i; } else{ val=val+n-i-1; } } // System.out.println(val); long l=-1; long r=n; for(int i=0;i<n;i++) { long templ=findL(str,l); long tempr=findR(str,r); // System.out.println(templ+" "+tempr); if((templ<n/2)&&(tempr>=n/2)) { if(templ<(n-tempr-1)) { l=templ; val=val-l+n-l-1; // System.out.println(val+"hi1"); } else { r=tempr; val=val-(n-tempr-1)+tempr; // System.out.println(val+"hi2"); // System.out.println("hi"); } } else if(templ<n/2) { l=templ; val=val-l+n-l-1; // System.out.println(val+"hi3"); } else if(tempr>=n/2) { r=tempr; val=val-(n-tempr-1)+tempr; // System.out.println(val+"hi4"); } arr.add(val); } StringBuffer sb=new StringBuffer(); for(int i=0;i<n;i++) { sb.append(arr.get(i)+" "); } 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 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
b8234e8baea71fd73256ab55208ea652
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.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class H { public static void main (String[] args) throws java.lang.Exception { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t-- != 0) { int n = sc.nextInt(); String s = sc.next(); long c = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'L') { c += i; } else { c += (n - i - 1); } } int i = 0, j = n - 1; int cn = 0; ArrayList<D> l = new ArrayList<>(); for (; i < s.length() / 2; i++) { if (s.charAt(i) == 'L') { l.add(new D(i, (n - i -1) - i )); } } for (; j >= s.length() / 2 + (s.length() % 2); j--) { if (s.charAt(j) == 'R') { l.add(new D(j, j - (n - j - 1))); } } Collections.sort(l, new Comparator<D>(){ @Override public int compare(D d1, D d2) { return Integer.compare(d2.d, d1.d); } }); for (; cn < l.size(); cn++) { c += l.get(cn).d; out.print(c + " "); } for (int q = cn; q < n; q++) { out.print(c + " "); } out.println(""); } out.close(); } private static class D { public int a; public int d; public D(int a, int d) { this.a = a; this.d = d; } } public static PrintWriter out; 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
6d73fabe348c59aa08633f0341f48827
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 C{ public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0){ int n=s.nextInt(); char c[]=s.next().toCharArray(); long ans[]=new long[n]; long max[]=new long[n]; long curr[]=new long[n]; long k=0; for(int i=0;i<n;i++){ max[i]=Math.max(n-1-i,i); if(c[i]=='L') curr[i]=i; else curr[i]=n-i-1; k+=curr[i]; } int i=0,j=n-1; int itr=0; while(i<n && j>=0 &&i<=j&&itr<n){ while(i<=j && max[i]==curr[i])i++; while(j>=i && max[j]==curr[j])j--; if(i==n) break; if(j==-1) break; long incL=max[i]-curr[i]; long incR=max[j]-curr[j]; k+=Math.max(incR,incL); ans[itr++]=k; if(incL>incR) i++; else j--; } for(i=itr;i<n;i++) ans[i]=k; for(long a:ans)System.out.print(a+" "); 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
1c37fa8a8383ef70003cb758c5732a92
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.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import javax.swing.tree.TreeNode; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); List<String>list=new ArrayList<String>(); for (int i = 0; i < t; i++) { int n=sc.nextInt(); char[]arr=sc.next().toCharArray(); int l=arr.length; long sum=0; List<Long>queue=new ArrayList<Long>(); for (int j = 0; j < arr.length; j++) { if (arr[j]=='L') { sum+=(long)j; long y=l-1-j; if (y>j) { queue.add(y-(long)j); } }else { long y=l-1-j; sum+=y; if (j>y) { queue.add(0l-y+(long)j); } } } queue.sort(Comparator.reverseOrder()); String aString=""; String[]s=new String[l]; for (int j = 0; j < arr.length; j++) { if (j<queue.size()) { sum+=queue.get(j); } s[j]=String.valueOf(sum); } list.add(String.join(" ", s)); } for (String string : list) { System.out.println(string); } } }
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
6f61c9df6c507dda40a4d5649b88d396
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 static java.lang.Double.parseDouble; import static java.lang.Integer.compare; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.in; import static java.lang.System.out; import static java.util.Arrays.asList; import static java.util.Collections.max; import static java.util.Collections.min; public class D { private static final int MOD = (int) (1E9 + 7); private static final int INF = (int) (1E9); static FastScanner scanner = new FastScanner(in); public static void main(String[] args) throws IOException { // Write your solution here StringBuilder answer = new StringBuilder(); int t = 1; t = parseInt(scanner.nextLine()); while (t-- > 0) { answer.append(solve()).append("\n"); } //out.println(answer); } private static Object solve() throws IOException { int n = scanner.nextInt(); String s = scanner.nextLine(); long sum = 0; for (int i = 0; i < n; i++) { if(s.charAt(i)=='L') sum+=i; else sum+=n-i-1; } int i=0,j=s.length()-1,it=n; while(i<j){ if(s.charAt(i)=='L'){ sum=sum-i+(n-i-1); out.print(sum+" "); it--; } if(s.charAt(j)=='R'){ sum=sum+j-(n-j-1); out.print(sum+" "); it--; } i++; j--; } while (it-->0){ out.print(sum+" "); } out.println(); return "NO"; } private static class Pair implements Comparable<Pair> { int index, value; public Pair(int index, int value) { this.index = index; this.value = value; } public int compareTo(Pair o) { if (value != o.value) return compare(value, o.value); return compare(index, o.index); } } private static int maxx(Integer... a) { return max(asList(a)); } private static int minn(Integer... a) { return min(asList(a)); } private static long maxx(Long... a) { return max(asList(a)); } private static long minn(Long... a) { return min(asList(a)); } private static int add(int a, int b) { long res = ((long) (a + MOD) % MOD + (b + MOD) % MOD) % MOD; return (int) res; } private static int mul(int a, int b) { long res = ((long) a % MOD * b % MOD) % MOD; return (int) res; } private static int pow(int a, int b) { a %= MOD; int res = 1; while (b > 0) { if ((b & 1) != 0) res = mul(res, a); a = mul(a, a); b >>= 1; } return res; } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return parseInt(next()); } long nextLong() { return parseLong(next()); } double nextDouble() { return 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 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
26ed81b97ec11ae4de6cb27aa8ab8764
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 contest.CodeforcesRound817; import java.io.*; import java.util.*; public class D1722 { InputStream is; FastWriter out; String INPUT = ""; //提交时注意需要注释掉首行package //尽量用对象Arrays.sort(new Long[]{}) //int 最大值2**31-1,2147483647; //尽量使用long类型,避免int计算的数据溢出 //String尽量不要用+号来,可能会出现TLE,推荐用StringBuffer //注意对象类型Long,Integer相等使用equals代替== !!! //乘法可能溢出 void solve() { int t = ni(); for (; t > 0; t--) go(); } void go() { int n = ni(); char[] s = ns().toCharArray(); List<Integer> list = new ArrayList<>(); long count = 0; for (int i = 0; i < n; i++) { if (s[i] == 'L') { count += i; if (n - i - 1 > i) { list.add(n - i - 1 - i); } } else { count += (n - i - 1); if (i > n - i - 1) { list.add(i - (n - i - 1)); } } } Collections.sort(list); // out.println(count); long[] ans = new long[n]; for (int i = list.size() - 1; i >= 0; i--) { count += list.get(i); ans[list.size() - 1 - i] = count; } for (int i = list.size(); i < n; i++) { ans[i] = count; } out.println(ans); } void run() throws Exception { is = System.in; out = new FastWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); //debug log //tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new D1722().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private 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++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private Integer[] na(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private Long[] nal(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private Integer[][] nmi(int n, int m) { Integer[][] map = new Integer[n][]; for (int i = 0; i < n; i++) map[i] = na(m); return map; } 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(); } } public static class FastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter() { out = null; } public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastWriter write(char c) { return write((byte) c); } public FastWriter write(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if (x == Integer.MIN_VALUE) { return write((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if (x < 0) { write('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } write((long) x).write("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; write((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastWriter writeln(char c) { return write(c).writeln(); } public FastWriter writeln(int x) { return write(x).writeln(); } public FastWriter writeln(long x) { return write(x).writeln(); } public FastWriter writeln(double x, int precision) { return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for (int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for (long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(Long... xs) { boolean first = true; for (Long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte) '\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(Long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for (char[] line : map) write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c) { return writeln(c); } public FastWriter println(int x) { return writeln(x); } public FastWriter println(long x) { return writeln(x); } public FastWriter println(double x, int precision) { return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(Long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } public void trnz(int... o) { for (int i = 0; i < o.length; i++) if (o[i] != 0) System.out.print(i + ":" + o[i] + " "); System.out.println(); } // print ids which are 1 public void trt(long... o) { Queue<Integer> stands = new ArrayDeque<>(); for (int i = 0; i < o.length; i++) { for (long x = o[i]; x != 0; x &= x - 1) stands.add(i << 6 | Long.numberOfTrailingZeros(x)); } System.out.println(stands); } public void tf(boolean... r) { for (boolean x : r) System.out.print(x ? '#' : '.'); System.out.println(); } public void tf(boolean[]... b) { for (boolean[] r : b) { for (boolean x : r) System.out.print(x ? '#' : '.'); System.out.println(); } System.out.println(); } public void tf(long[]... b) { if (INPUT.length() != 0) { for (long[] r : b) { for (long x : r) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } System.out.println(); } } public void tf(long... b) { if (INPUT.length() != 0) { for (long x : b) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } } private void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
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
4f122ffacd7495314aa1b3e733d0b809
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 codeforces { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t=s.nextInt(); for(int z=0;z<t;z++) { int n=s.nextInt(); String str = s.next(); long score =0l; for(int i=0;i<n;i++) { if(str.charAt(i)=='L') { score += i; } else score += n-i-1; } ArrayList<Integer> a = new ArrayList<>(); for(int i=0;i<n;i++) { if( str.charAt(i) != 'R' && i<n/2) a.add(n-i-1 - i); if( str.charAt(i) != 'L' && i>n/2) a.add(i - n +i +1); } if(n%2==0) { if(str.charAt(n/2)=='R') a.add(1); } Collections.sort(a); for(int i=a.size()-1;i>=0;i--) { score+=a.get(i); System.out.print(score+ " "); } for(int i=0;i<n-a.size();i++) System.out.print(score + " "); 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
bcef4f4b08a1959a5a280413855b7863
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.Collections; import java.util.PriorityQueue; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int casos = sc.nextInt(); while (casos-->0){ int tam = sc.nextInt(); char [] letras =sc.next().toCharArray(); pw.println(obtenerMaximoValorLetras(letras,tam)); } pw.close(); } private static String obtenerMaximoValorLetras(char [] letras , int tam){ StringBuilder res = new StringBuilder(); long suma = 0; PriorityQueue<Integer> cola = new PriorityQueue<>(Collections.reverseOrder()); for (int pos=0;pos<tam;pos++){ if (letras[pos]=='L'){ suma+=pos; if (pos<tam-pos-1) cola.add((tam-pos-1-pos)); }else if (letras[pos] == 'R'){ suma+=tam-pos-1; if (pos >tam-pos-1) cola.add((pos-(tam-pos-1))); } } for (int i = 0 ;i<tam;i++){ if (!cola.isEmpty()) suma+=cola.poll(); res.append(suma).append(" "); } return res.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 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
082765e470cc68013776eeb2c22fd40b
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[] arg){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0){ int n = sc.nextInt(); sc.nextLine(); String linea = sc.nextLine(); int[] contL = new int[n]; long ans = 0; for(int i = 0; i < n;i++){ if(linea.charAt(i) == 'L'){ contL[i] = n-2*i-1; ans += i; }else{ contL[i] = 2*i+1-n; ans += n-i-1; } } Arrays.sort(contL); for(int i = n-1; i >= 0; i--){ if(contL[i] > 0) ans += contL[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 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
2ab0918d4db3c3859f64b15c4326921a
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 t = sc.nextInt(); while (t-- > 0){ int n = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); long count = 0; for (int i=0; i<n; i++){ char c = s.charAt(i); if (c == 'L'){ count += i; } else{ count += n-i-1; } } int[] arr = new int[n]; for (int i=0; i<n/2; i++){ if (s.charAt(i) == 'R'){ arr[i] = i - (n-i-1); } else { arr[i] = (n-i-1)-i; } if (s.charAt(n-1-i) == 'R'){ arr[n-1-i] = (n-i-1) - i; } else { arr[n-1-i] = i - (n-i-1); } } Arrays.sort(arr); for (int i=n-1; i>=0; i--){ if (arr[i] > 0){ count += arr[i]; } System.out.print(count+" "); } 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
d3821faafeae0e337a65aaa3c2dc0f4f
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
/****************************************************************************** Practice>>>>>> *******************************************************************************/ import java.util.*; import java.io.*; import java.util.Collections; 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.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 testCases=in.nextInt(); while(testCases-- > 0){ int n=in.nextInt(); String s=in.next(); int init[]=new int[n];int swap[]=new int[n]; for(int i=0;i<n;i++){ if(s.charAt(i)=='L'){ init[i]=i; swap[i]=n-i-1; } else{ init[i]=n-i-1; swap[i]=i; } } Integer change[]=new Integer[n]; for(int i=0;i<n;i++){ change[i]=swap[i]-init[i]; } Arrays.sort(change,Collections.reverseOrder()); long sum=0; for(int i=0;i<n;i++) sum+=init[i]; for(int i=0;i<n;i++){ if(change[i]>0) sum+=change[i]; System.out.print(sum+" "); } System.out.println(); } out.close(); } catch (Exception 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 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
e72958bd60e3d9bd1242cf2cc1780f45
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 codeforces { public static void main(String[] args) { Scanner ob=new Scanner(System.in); int t=ob.nextInt(); for(int o=0;o<t;o++) { int n=ob.nextInt(); ob.nextLine(); String s=ob.nextLine(); char c[]=s.toCharArray(); long ct=0; for(int i=0;i<n;i++) { if(c[i]=='L') ct+=(long)i; else ct+=(long)n-i-1; } int l=0; int r=0; for(int i=0;i<n/2;i++) { if(c[i]=='L') l++; } for(int i=n/2;i<n;i++) { if(c[i]=='R') r++; } int l1[]=new int[l]; int r1[]=new int[r]; int l2=0; int r2=0; int n1=0; for(int i=0;i<n/2;i++) { if(c[i]=='L') { l1[l2]=n-i-i-1; l2++; } } for(int i=n/2;i<n;i++) { if(c[i]=='R') { r1[r2]=i-n+1+i; r2++; } } int[] b = new int[r2]; int j = r2; for (int i = 0; i < r2; i++) { b[j - 1] = r1[i]; j = j - 1; } r1=b; int i1 = 0; int i2 = 0; while(i1 < l2 && i2 < r2) { if(l1[i1] < r1[i2]) { ct+=(long)r1[i2]; i2++; } else { ct+=(long)l1[i1]; i1++; } System.out.print(ct+" "); n1++; } while(i1 < l2) { ct+=(long)l1[i1]; i1++; System.out.print(ct+" "); n1++; } while(i2 < r2) { ct+=(long)r1[i2]; i2++; System.out.print(ct+" "); n1++; } for(int i=n1;i<n;i++) { System.out.print(ct+" "); } 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
7d50461a1851f358998a8c8a4df514cb
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.util.Map.Entry; import javax.swing.text.StyledEditorKit.BoldAction; import java.io.*; import java.lang.*; public class main1{ FastScanner in; PrintWriter out; public static void main(String[] arg) { new main1().run(); } /////////////////////////////////////////////////////////////////// ///////////////// /MAIN PROGRAM STARTS HERE/////////////////////// ////////////////////////////////////////////////////////////////// public void solve() throws IOException { int tc=in.nextInt(); while(tc-->0) { int n=in.nextInt(); String s=in.next(); long sum=0; int[] arr=new int[n]; for(int i=0;i<n;i++) { if(s.charAt(i)=='L') { sum+=(i+1)-1; arr[i]=(i+1)-1; } else { sum+=(n-(i+1)); arr[i]=(n-(i+1)); } } Queue<Integer> q=new LinkedList<>(); int l=0,r=s.length()-1; while(l<r) { if(s.charAt(l)=='L') { q.offer(l); } if(s.charAt(r)=='R') { q.offer(r); } l++; r--; } // for(int i=0;i<n;i++) // { // out.print(arr[i]+" "); // } int remain=n-q.size(); int mid=n/2; while(!q.isEmpty()) { int idx=q.poll(); sum=sum-arr[idx]; long ok1=sum; long ok2=sum; if(idx>mid) { sum+=idx; } else { sum+=(n-(idx+1)); } ok1+=idx; ok2+=(n-(idx+1)); sum=Math.max(ok1,ok2); out.print(sum+" "); } while(remain-- >0) { out.print(sum+ " "); } out.println(); } } /////////////////////////////////////////////////////////////// //////////////////////MAIN PROGRAM ENDS HERE/////////////////// /////////////////////////////////////////////////////////////// public void run() { try { in = new FastScanner(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader f) { br = new BufferedReader(f); } 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; } } }
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
9d58078eab9e03a1220c5e4f4c91a09b
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.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; public class Cinema { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); byte t = scanner.nextByte(); while (t-- > 0) { int n = scanner.nextInt(); String line = scanner.next(); if (n == 1) { System.out.println(0); continue; } long value = 0; Stack<Integer> stackR = new Stack<>(); Queue<Integer> queueL = new LinkedList<>(); for (int i = 0; i < n; i++) { if (line.charAt(i) == 'L') { queueL.add(i); value += i; } else { stackR.push(i); value += n - 1 - i; } } int mid = n / 2 - 1; StringBuilder result = new StringBuilder(); for (int i = 0; i < n; i++) { result.append(" "); if (stackR.isEmpty() && queueL.isEmpty()) result.append(value); else if(stackR.isEmpty()) { if (queueL.peek() <= mid) value = poll(queueL, value, n); result.append(value); } else if (queueL.isEmpty()) { if (stackR.peek() > mid) value = pop(stackR, value, n); result.append(value); } else { if (stackR.peek() > mid && queueL.peek() <= mid) { value = value - n + 1 + 2 * stackR.peek() > value + n - 1 - 2 * queueL.peek() ? pop(stackR, value, n) : poll(queueL, value, n); } else if (stackR.peek() > mid) value = pop(stackR, value, n); else if (queueL.peek() <= mid) value = poll(queueL, value, n); result.append(value); } } System.out.println(result.deleteCharAt(0)); } } public static long pop(Stack<Integer> stack, long value, int n) { int pos = stack.pop(); return value + 1 - n + 2 * pos; } public static long poll(Queue<Integer> q, long value, int n) { int pos = q.poll(); return value + n - 1 - 2 * pos; } }
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
2dbc823a183a56e7382d573dc1a4dc94
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.Collections; import java.util.List; public class R817D { public static void main(String[] args) throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(in.readLine()); while(n-- > 0){ int l=Integer.parseInt(in.readLine()); String s=in.readLine(); long total=0; List<Integer> v=new ArrayList<>(); for(int i=0;i<l;i++){ if(s.charAt(i)=='L'){ v.add((l-1-i)-i); total+=i; }else{ v.add(i-(l-1-i)); total+=(l-1-i); } } Collections.sort(v); for(int i=v.size()-1;i>=0;i--){ if(v.get(i)>0)total+=v.get(i); System.out.print(total+" "); } 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
f5463420bdc7f48ed6475c7cd25b9237
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 Main { private static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); String s = sc.next(); long total = 0; int[] change = new int[n]; for (int i = 0; i < n; i++) { char c = s.charAt(i); int l = i; int r = n - i - 1; if (c == 'L') { total += l; if (r > l) { change[i] = r - l; } } else { total += r; if (l > r) { change[i] = l - r; } } } Arrays.sort(change); StringBuilder sb = new StringBuilder(); for (int i = change.length - 1; i >= 0; i--) { sb.append(total += change[i]); if (i > 0) sb.append(" "); } System.out.println(sb.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 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
11679a804a1620d834ead215dae28a00
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.Comparator; import java.util.Scanner; public class Main { private static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); String s = sc.next(); long total = 0; int[] change = new int[n]; for (int i = 0; i < n; i++) { char c = s.charAt(i); int l = i; int r = n - i - 1; if (c == 'L') { total += l; if (r > l) { change[i] = r - l; } } else { total += r; if (l > r) { change[i] = l - r; } } } Arrays.sort(change); for (int i = change.length - 1; i >= 0; i--) { System.out.print(total += change[i]); if (i > 0) 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 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
1a75bcef85e1774929fb2315960d50e7
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
// Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.*; public class HelloWorld { static long smallestDivisor(long n) { if (n % 2 == 0) return 2; for (long i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } ArrayList <Integer> ans = new ArrayList<>(); public static int solve(int n, int x,int y){ if(x!=0 && y!=0){ return -1; } int p = Math.max(x,y); if(p==0){ return -1; } if((n-1)%p != 0){ return -1; } int condi= 0; int pi = p; int o = 1; for(int j = 0;j<n-1;j+=p){ for(int i = 0;i<p;i++){ System.out.print(o + " "); } if(condi == 0){ condi=1; o+=pi+1; } else{ o+=pi; } } System.out.println(); return 1; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); long t = sc.nextLong(); while(t>0){ t--; int n = sc.nextInt(); int a1[] = new int[n]; String s = sc.next(); long power= 0; ArrayList<Long> a = new ArrayList<Long>(); for(long i = 0;i<s.length() ; i++){ if(s.charAt((int)i) == 'L'){ power+=i; a.add((n - 1 - i) - i); } else{ power+= n-1-i; a.add(i-(n - 1 - i)); } } int condi=0; Collections.sort(a); for(int i=n-1;i>=0;i--){ if(a.get(i)>0){ power+=a.get(i); } System.out.print(power + " "); } /*for(int i = n/2;i<n;i++){ if(n%2==1){ if(i == n/2){ System.out.print(power+" "); continue; } } if(s.charAt(i) == 'R'){ power+=i; }else{ ; } System.out.print(power+" "); }*/ 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
65b7c4fa5e96748a5594d28d72f7f53a
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 a1558 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t,n,i,j,d; String s; long sum; t=sc.nextInt(); for(j=1;j<=t;j++){ n=sc.nextInt(); s=sc.next(); PriorityQueue<Integer> pq=new PriorityQueue<>(Collections.reverseOrder()); for(i=0;i<n;i++){ if(i<n/2&&s.charAt(i)=='L') pq.add(n-i-1); if(i>n/2&&s.charAt(i)=='R') pq.add(i); } if(n%2==0&&s.charAt(n/2)=='R') pq.add(n/2); sum=0; for(i=0;i<n;i++){ if(s.charAt(i)=='L') sum+=i; if(s.charAt(i)=='R') sum+=n-i-1; } d=0; for(i=0;i<n;i++){ //System.out.print(i+" "+pq.size()+" "); if(pq.size()>0){ d=pq.poll(); sum-=(n-d-1); sum+=d; } 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 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
fac7fb37365b7a160d30d9c663513fb5
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 scan = new Scanner (System.in); int t= scan.nextInt (); while(t>0){ Map <String, Integer> map = new HashMap<> (); int n= scan.nextInt (); char c[]= new char[n]; int i,j; long ans=0; long store[] = new long[n]; String s= scan.next (); for(i=0;i<n;i++){ c[i]=s.charAt (i); if(c[i]=='L') store[i]=i; else store[i]=(n-i-1); ans+=store[i]; } long benefit[]= new long[n]; for(i=0;i<n;i++){ benefit[i]=(store[i]-(long)(Math.max (i,(n-i-1)))); } Arrays.sort (benefit); for(i=0;i<n;i++){ System.out.print(ans-benefit[i]+" "); ans-=benefit[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 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
98583b4261192e1a2079ffd10d67083f
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
//KENAA //5-9-22 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.HashMap; import java.util.HashSet; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { D_Line(); } public static void D_Line() { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out); int tt = fr.nextInt(); while (tt-- > 0) { int n = fr.nextInt(); long sum = 0; String s = fr.next(); int[] arr = new int[s.length()]; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'L') { sum += i; arr[i] = (n - 1 - i) - i; } else { sum += n - 1 - i; arr[i] = i - (n - 1 - i); } } Arrays.sort(arr); for (int i = arr.length - 1; i >= 0; i--) { if (arr[i] > 0) { sum += arr[i]; } out.print(sum + " "); } out.println(); } out.close(); } static class FastReader { 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) { } return st.nextToken(); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static long lcm(long a, long b) { return a * b / gcd(a, b); } public static long gcd(long a, long b) { while (b != 0) { long m = a % b; a = b; b = m; } return a; } public static long factorial(int n) { if (n == 0) return 1; long res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } public static long nCr(int n, int r) { return factorial(n) / (factorial(r) * factorial(n - r)); } public static ArrayList<Integer> factors(long n) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 2; i < n / i; i++) { while (n % i == 0) { if (!list.contains(i)) { list.add(i); n /= i; } } } if (n > 2) { if (!list.contains((int) n)) { list.add((int) n); } } return list; } public static int numOfPrimes(int n) { if (n < 2) { return 0; } boolean[] bool = new boolean[n + 1]; outer: for (int i = 2; i < bool.length / i; i++) { if (bool[i]) { continue outer; } for (int j = 2 * i; j < bool.length; j += i) { bool[j] = true; } } int counter = 0; for (int i = 0; i < bool.length; i++) { if (!bool[i]) { counter++; } } return counter; } public static void sort2DGivenArray(Object[][] arr, int colNum) { Arrays.sort(arr, (val1, val2) -> { if (val1[colNum] == val2[colNum]) { return 0; } else if (((Integer) (val1[colNum])).compareTo(((Integer) (val2[colNum]))) < 0) { return 1; } else { return -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 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
9f607073496129bfac74302457a6c555
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.Collection; import java.util.Collections; import java.util.Scanner; public class line { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tests = sc.nextInt(); while(tests-->0) { int n = sc.nextInt(); char[] line = sc.next().toCharArray(); ArrayList<Long> changes = new ArrayList<>(); Long value = 0L; Long ontheleft, ontheright; for(int i = 0 ; i < n ; i++) { //calculating the current value ontheleft = (long) i; ontheright = (long) (n-1-i); if(line[i] == 'L') { value += ontheleft; if(ontheright > ontheleft) //it will make a positive effect if that person turns around changes.add(ontheright - ontheleft); //the amount of change that will happen } else { value += ontheright; if(ontheleft > ontheright) changes.add(ontheleft - ontheright); } } Collections.sort(changes, Collections.reverseOrder()); for(int k = 1 ; k <= n ; k++) { //possible number of changes if any if(k > changes.size()) //no further better effects System.out.print(value + " "); else { value += changes.get(k-1); System.out.print(value + " "); } } 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 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
9ae93d57f08d16e6d47eca13dd543cad
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.math.*; import java.util.* ; import java.io.* ; @SuppressWarnings("unused") //Scanner s = new Scanner(new File("input.txt")); //s.close(); //PrintWriter writer = new PrintWriter("output.txt"); //writer.close(); public class cf { static long gcd(long a, long b){if (b == 0) {return a;}return gcd(b, a % b);} public static void main(String[] args)throws Exception { FastReader in = new FastReader() ; // FastIO in = new FastIO(System.in) ; StringBuilder op = new StringBuilder() ; int T = in.nextInt() ; // int T=1 ; for(int tt=0 ; tt<T ; tt++){ //CODE : int n = in.nextInt() ; char[] ch = in.nextLine().toCharArray(); List<Long> list = new ArrayList<>() ; long cval=0 ; for(int i=0 ; i<n ; i++) { if(ch[i]=='R') { long left = i ; long right = n-1-left ; cval+=right; if(left>right)list.add(left-right) ; } else { long left = i ; long right = n-1-left ; cval+=left ; if(right>left)list.add(right-left) ; } } Collections.sort(list , Collections.reverseOrder()); int index=0 , ls = list.size() ; for(int i=0 ; i<n ; i++) { if(i<ls) { cval += list.get(i) ; op.append(cval+" "); } else { op.append(cval+" "); } } op.append("\n") ; } System.out.println(op.toString()) ; } static class pair implements Comparable<pair> { int first, second; pair(int first, int second) { this.first = first; this.second = second; } @Override public int compareTo(pair other) { return this.first - other.first; } @Override public boolean equals(Object obj) { pair other = (pair)obj ; return this.first == other.first && this.second == other.second ; } } 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 class FastIO { InputStream dis; byte[] buffer = new byte[1 << 17]; int pointer = 0; public FastIO(String fileName) throws Exception { dis = new FileInputStream(fileName); } public FastIO(InputStream is) throws Exception { dis = is; } int nextInt() throws Exception { int ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } long nextLong() throws Exception { long ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } byte nextByte() throws Exception { if (pointer == buffer.length) { dis.read(buffer, 0, buffer.length); pointer = 0; } return buffer[pointer++]; } String next() throws Exception { StringBuffer ret = new StringBuffer(); byte b; do { b = nextByte(); } while (b <= ' '); while (b > ' ') { ret.appendCodePoint(b); b = nextByte(); } return ret.toString(); } int[] readArray(int n) throws Exception { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return 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; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); 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 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
b2b7743666cf4906b520e6f7a5686fae
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 sol { public static void main(String args[]) { // Your Code Here Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); char arr[]=sc.next().toCharArray(); PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); long s=0; for(int i=0;i<n;i++){ if(arr[i]=='L'){ s+=i; if(i < n - i - 1) pq.add(n - i - 1 - i); } else{ s+=n-i-1; if(i>n-i-1) pq.add(i - (n - i - 1)); } } for(int i=0;i<n;i++){ if(!pq.isEmpty()) s+=pq.poll(); System.out.print(s+" "); } 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
b405020d9cbf941e8ba1a42f2d97c917
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 Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); long t = in.nextLong(); while(t-- != 0) { solve(in); } in.close(); } public static void solve(Scanner in) { int n = in.nextInt(); String input = in.next(); long sum = 0; int[] arr = new int[n]; for(int i =0; i < n; i++) { if(input.charAt(i) == 'L') { arr[i] = i; } else { arr[i] = n-1-i; } sum += arr[i]; } int LP = 0, RP = n-1; int counter = 0; while(LP <= RP) { if(arr[LP] < n-1-arr[LP]) { sum += n-1-2*arr[LP]; counter++; System.out.print(sum+" "); } if(arr[RP] < n-1-arr[RP]) { sum += n-1-2*arr[RP]; counter++; System.out.print(sum+" "); } LP++; RP--; } for(int i = counter; i < n; i++) { System.out.print(sum+" "); } System.out.println(); } // public static int countRow(boolean[][] matrix, int col, int k) { // int count = 0; // for(int i = col; i < k+col; i++) { // if(matrix[i][col]) // count++; // } // return count; // } }
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
75b4ed0a641f119aac4a307e7171707c
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.File; import java.io.FileInputStream; import java.io.PrintWriter; public class T1722D { private static final T1722DFastScanner in = new T1722DFastScanner(); private static final PrintWriter out = new PrintWriter(System.out); public static void main(String args[]) { int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); sourceArray = in.nextLine().toCharArray(); long currentMax = 0; for (int j = 0; j < n; j++) { if (sourceArray[j] == 'L') { currentMax += j; } else { currentMax += n - j - 1; } } borderLeftPart = n / 2 - 1; int currentLInLeft = nextIndex(-1, false); borderRightPart = (int) Math.ceil(n / 2.0); int currentRInRight = nextIndex(n, true); for (int j = 1; j <= n; j++) { if (currentLInLeft == -1 && currentRInRight == -1) { //all ok } else if (currentLInLeft == -1) { currentMax += - n + currentRInRight + currentRInRight + 1; currentRInRight = nextIndex(currentRInRight, true); } else if (currentRInRight == -1) { currentMax += n - currentLInLeft - currentLInLeft - 1; currentLInLeft = nextIndex(currentLInLeft, false); } else { int potentialRight = - n + currentRInRight + currentRInRight + 1; int potentialLeft = n - currentLInLeft - currentLInLeft - 1; if (potentialLeft > potentialRight) { currentMax += potentialLeft; currentLInLeft = nextIndex(currentLInLeft, false); } else { currentMax += potentialRight; currentRInRight = nextIndex(currentRInRight, true); } } out.print(currentMax + " "); } out.println(); } out.flush(); } private static char sourceArray[]; private static int borderLeftPart; private static int borderRightPart; private static int nextIndex(int oldIndex, boolean inRight) { int index = oldIndex; char localArray[] = sourceArray; if (inRight) { index--; while (index >= borderRightPart) { if (localArray[index] == 'R') { return index; } index--; } return -1; } else { index++; while (index <= borderLeftPart) { if (localArray[index] == 'L') { return index; } index++; } return -1; } } } class T1722DFastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public T1722DFastScanner() { in = new BufferedInputStream(System.in, BS); } public T1722DFastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n' && c != '\r') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) 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 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
2e2f2c99f0a94ff129e2eb35234d8de7
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.File; import java.io.FileInputStream; import java.io.PrintWriter; public class T1722D { private static final T1722DFastScanner in = new T1722DFastScanner(); private static final PrintWriter out = new PrintWriter(System.out); public static void main(String args[]) { int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); line = in.nextLine(); long currentMax = 0; for (int j = 0; j < line.length(); j++) { if (line.charAt(j) == 'L') { currentMax += j; } else { currentMax += n - j - 1; } } borderLeftPart = n / 2 - 1; int currentLInLeft = nextIndex(-1, false); borderRightPart = (int) Math.ceil(n / 2.0); int currentRInRight = nextIndex(n, true); for (int j = 1; j <= n; j++) { if (currentLInLeft == -1 && currentRInRight == -1) { //all ok } else if (currentLInLeft == -1) { currentMax += - n + currentRInRight + currentRInRight + 1; currentRInRight = nextIndex(currentRInRight, true); } else if (currentRInRight == -1) { currentMax += n - currentLInLeft - currentLInLeft - 1; currentLInLeft = nextIndex(currentLInLeft, false); } else { int potentialRight = - n + currentRInRight + currentRInRight + 1; int potentialLeft = n - currentLInLeft - currentLInLeft - 1; if (potentialLeft > potentialRight) { currentMax += potentialLeft; currentLInLeft = nextIndex(currentLInLeft, false); } else { currentMax += potentialRight; currentRInRight = nextIndex(currentRInRight, true); } } out.print(currentMax + " "); } out.println(); } out.flush(); } private static String line; //toCharArray? private static int borderLeftPart; private static int borderRightPart; private static int nextIndex(int oldIndex, boolean inRight) { int index = oldIndex; //local array? if (inRight) { index--; while (index >= borderRightPart) { if (line.charAt(index) == 'R') { return index; } index--; } return -1; } else { index++; while (index <= borderLeftPart) { if (line.charAt(index) == 'L') { return index; } index++; } return -1; } } } class T1722DFastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public T1722DFastScanner() { in = new BufferedInputStream(System.in, BS); } public T1722DFastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n' && c != '\r') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) 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 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
56d23f481c405f9caff6f36ab9604f22
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 input = new Scanner(System.in); int tc = input.nextInt(); work: while (tc-- > 0) { StringBuilder ans = new StringBuilder(); int n = input.nextInt(); String s = input.next(); ArrayList<Long> a = new ArrayList<>(); long now = 0; for (int i = 0; i <n; i++) { if(i<(n/2)) { if(s.charAt(i)=='L') { a.add(n-2*i-1L); } } else { if(s.charAt(i)=='R') { a.add(i-(n-i-1L)); } } if(s.charAt(i)=='L') { now+=(i); } else { now+=(n-i-1L); } } int j = 0; Collections.sort(a,Collections.reverseOrder()); for (int i = 0; i <n; i++) { if(j<a.size()) { ans.append(now+a.get(j)+" "); now+=a.get(j); j++; } else { ans.append(now+" "); } } 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 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
4f1de2f711153b083c982417fc44b1bb
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 PrintWriter out = new PrintWriter(System.out); static Scanner in = new Scanner(System.in); static BufferedReader re = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out)); //String[] strs = re.readLine().split(" "); int a = Integer.parseInt(strs[0]); public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); //String[] strs = re.readLine().split(" "); //int T=Integer.parseInt(strs[0]); int T=in.nextInt(); while(T>0){ //String[] strs1 = re.readLine().split(" "); //int n=Integer.parseInt(strs1[0]); //String s=re.readLine(); //char arr[]=s.toCharArray(); Map<String,Integer>map=new HashMap<>(); int n=in.nextInt(); String s=in.next(); char arr[]=s.toCharArray(); int l=0;int r=arr.length-1; long sum=0; for(int i=0;i<n;i++){ if(arr[i]=='L')sum+=i; else sum+=n-1-i; } int len=n; while(l<=r&&len>=1){ int a=0;int b=0;int c=0;int d=0; int t=0;int v=0; if(arr[l]=='L'){ a=l;//改前 b=n-1-l;//改后 if(b-a<0){l++;t++;} } else{ a=n-1-l; b=l; if(b-a<0){l++;t++;} } if(arr[r]=='L'){ c=r; d=n-1-r; if(d-c<0){r--;v++;} } else{ c=n-1-r; d=r; if(d-c<0){r--;v++;} } if(t>0&&v>0){continue;} if(b-a>=d-c){ l++;sum=sum-a+b;//System.out.print(sum+" L ");len--; } else{ r--;sum=sum-c+d;//System.out.print(sum+" R ");len--; } //System.out.println(l+" "+r); System.out.print(sum+" ");len--; } for(int i=len;i>=1;i--)System.out.print(sum+" "); System.out.println(); T--; } 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
ba9eee31c0f6fb9bd1ae152b46948c06
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; import java.util.*; public class d { public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); while (t-- > 0) { int n = fs.nextInt(); char[] s = fs.next().toCharArray(); long sum = 0; long[] a = new long[n]; for (int i = 0; i < s.length; ++i) { sum += s[i] == 'L' ? i : (n-i-1); a[i] = s[i] == 'L' ? n-i-1-i : i-(n-i-1); } Arrays.sort(a); for (int i = s.length-1; i >=0; --i) { if (a[i] > 0) { sum += a[i]; } out.print(sum + " "); } out.println(); } out.close(); } 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 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
28cc55ec85c5d4562af8c87ac1974e88
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 test { 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(); int c[]=new int[n]; int r=0; long res=0; for(int i=0;i<n;i++){ if(s.charAt(i)=='L'){ r=i; }else{ r=(n-i-1); } res+=r; } for(int i=0;i<n;i++){ r=0; if(s.charAt(i)=='L'){ r=n-1-i-i; }else{ r=i-(n-i-1); } c[i]=r; } Arrays.sort(c); for(int k=1;k<=n;k++){ if(c[n-1-(k-1)]>0){ res=res+c[n-1-(k-1)]; } System.out.print(res+" "); } System.out.println(""); } /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ } } /* if(s.charAt(idx)=='L'){ s=s.substring(0,idx)+'R'+s.substring(idx,n); }else{ s=s.substring(0,idx)+'L'+s.substring(idx,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 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
606d6873529ec7dd405de0424cefa1bb
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.lang.*; import java.util.*; public class GFG { static Scanner sc = new Scanner(System.in); public static void main (String[] args) { int t = sc.nextInt(); for(int i=0; i<t; i++){ solve(); } } public static void solve(){ int n = sc.nextInt(); String str = sc.next(); long val=0; for(int i=0; i<n; i++){ if(str.charAt(i)=='L') val+=i; else val+=n-i-1; } Integer[] change = new Integer[n]; for(int i=0; i<n; i++){ if(str.charAt(i)=='L') change[i] =n-i-1-i; else change[i] = i - (n-i-1); } Arrays.sort(change, Collections.reverseOrder()); long[] ans = new long[n]; for(int i=0; i<n; i++){ if(change[i]>0) val=val+change[i]; ans[i] = val; } for (int i=0; i<n; i++) { System.out.printf("%d " ,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 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
a58ddd60f000dddca69860575d44f290
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 codeforces1722D { public static void main(String[] args) throws Exception { FastScanner in = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int numCases = in.nextInt(); while (numCases-->0) { int n = in.nextInt(); char [] arr = in.nextString().toCharArray(); long currTotal = 0; ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i<n;i++) { if (arr[i]=='L') { currTotal+=i; a.add(n-i-1 - i); } else { currTotal+=(n-i-1); a.add(i-(n-i-1)); } } Collections.sort(a,Collections.reverseOrder()); StringBuilder s = new StringBuilder(); for (int i = 0; i<n;i++) { if (a.get(i)>0) currTotal+=a.get(i); s.append(currTotal+" "); } pw.println(s); } pw.close(); } private static class FastScanner { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; private FastScanner() throws IOException { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private short nextShort() throws IOException { short ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = (short) (ret * 10 + c - '0'); while ((c = read()) >= '0' && c <= '9'); if (neg) return (short) -ret; return ret; } private 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; } private char nextChar() throws IOException { byte c = read(); while (c <= ' ') c = read(); return (char) c; } private String nextString() throws IOException { StringBuilder ret = new StringBuilder(); byte c = read(); while (c <= ' ') c = read(); do { ret.append((char) c); } while ((c = read()) > ' '); return ret.toString(); } 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++]; } } }
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
581e0dc7270b8840f73fa9d0c7da9841
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.util.ArrayList; import java.util.Arrays; public class Calculation { public static void main(String args[]) throws IOException { try { BufferedReader rd=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(rd.readLine()); while(t-->0) { int n=Integer.parseInt(rd.readLine()); String str=rd.readLine(); int arr[]=new int[n]; long sum=0; for(int i=0;i<n;i++) { if(str.charAt(i)=='R') { arr[i]=(n-(i+1)); sum=sum+arr[i]; } else { arr[i]=i; sum=sum+arr[i]; } //System.out.print(arr[i]+" "); } //System.out.println(); //System.out.println(sum); PriorityQueue<Integer> left=new PriorityQueue<>(); PriorityQueue<Integer> right=new PriorityQueue<>(Collections.reverseOrder()); for(int i=0;i<n/2;i++) { if(str.charAt(i)=='L') { left.add(i); //System.out.println("left"+" "+left.peek()); } } for(int i=n/2;i<n;i++) { if(str.charAt(i)=='R') { right.add(i); //System.out.println("right"+" "+right.peek()); } } //System.out.println(); int k=1; while(left.size()>0||right.size()>0) { if(left.size()==0||right.size()==0) { if(left.size()==0) { sum=sum-arr[right.peek()]; sum=sum+right.peek(); right.poll(); } else if(right.size()==0) { sum=sum-arr[left.peek()]; sum=sum+n-(left.peek()+1); left.poll(); } } else { if(left.peek()<((n-1)-right.peek())) { sum=sum-arr[left.peek()]; sum=sum+n-(left.peek()+1); left.poll(); } else { sum=sum-arr[right.peek()]; sum=sum+right.peek(); right.poll(); } } System.out.print(sum+" "); k++; } for(int i=k;i<=n;i++) { if(i==n) { System.out.print(sum); } else{ System.out.print(sum+" "); } } System.out.println(); } } catch(Exception 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 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
848b3e68165459293e80f8ff67bc401f
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.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.StringTokenizer; public class D { static final long mod = (long) 1e9 + 7l; private static void solve(int t){ int n = fs.nextInt(); List<Integer> ll = new ArrayList<>(); List<Integer> rr = new ArrayList<>(); char [] ss = fs.next().toCharArray(); long[] og = new long[n]; long total = 0; boolean odd = n%2==0?false:true; for (int i = 0; i < n; i++) { if(ss[i]=='L'){ if(odd && i<=n/2)ll.add(i); if(!odd && i<n/2)ll.add(i); og[i] = i; } else { if(i>=n/2)rr.add(n-i-1); og[i] = n-i-1; } total+=og[i]; } Collections.reverse(rr); long[]ans = new long[n]; long curr = total; int l = 0; int r = 0; for (int i = 0; i < n; i++) { // int k=0; // while (k<=i){ if(l<ll.size() && r<rr.size() && ll.get(l)<=rr.get(r)){ int pos = ll.get(l); curr-=og[pos]; curr+=n-pos-1; l++; }else if(l<ll.size() && r<rr.size() && ll.get(l)>rr.get(r)){ int pos = rr.get(r); curr-=og[n-pos-1]; curr+=n-pos-1; r++; }else if( l<ll.size()){ int pos = ll.get(l); curr-=og[pos]; curr+=n-pos-1; l++; }else if(r<rr.size()){ int pos = rr.get(r); curr-=og[n-pos-1]; curr+=n-pos-1; r++; } // k++; // } // ans[i]= curr; out.print(curr+" "); } // for (int i = 0; i < n; i++) { // out.print(ans[i]+" "); // } out.println(); } private static int[] sortByCollections(int[] arr, boolean reverse) { ArrayList<Integer> ls = new ArrayList<>(arr.length); for (int i = 0; i < arr.length; i++) { ls.add(arr[i]); } if(reverse)Collections.sort(ls, Comparator.reverseOrder()); else Collections.sort(ls); for (int i = 0; i < arr.length; i++) { arr[i] = ls.get(i); } return arr; } public static void main(String[] args) { fs = new FastScanner(); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); int t = fs.nextInt(); for (int i = 1; i <= t; i++) solve(t); out.close(); // System.err.println( System.currentTimeMillis() - s + "ms" ); } static boolean DEBUG = true; static PrintWriter out; static FastScanner fs; static void trace(Object... o) { if (!DEBUG) return; System.err.println(Arrays.deepToString(o)); } static void pl(Object o) { out.println(o); } static void p(Object o) { out.print(o); } static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } static void sieveOfEratosthenes(int n, int factors[]) { factors[1] = 1; for (int p = 2; p * p <= n; p++) { if (factors[p] == 0) { factors[p] = p; for (int i = p * p; i <= n; i += p) factors[i] = p; } } } static long mul(long a, long b) { return a * b % mod; } static long fact(int x) { long ans = 1; for (int i = 2; i <= x; i++) ans = mul(ans, i); return ans; } static long fastPow(long base, long exp) { if (exp == 0) return 1; long half = fastPow(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long modInv(long x) { return fastPow(x, mod - 2); } static long nCk(int n, int k) { return mul(fact(n), mul(modInv(fact(k)), modInv(fact(n - k)))); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) 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()); } } static class _Scanner { InputStream is; _Scanner(InputStream is) { this.is = is; } byte[] bb = new byte[1 << 15]; int k, l; byte getc() throws IOException { if (k >= l) { k = 0; l = is.read(bb); if (l < 0) return -1; } return bb[k++]; } byte skip() throws IOException { byte b; while ((b = getc()) <= 32) ; return b; } int nextInt() throws IOException { int n = 0; for (byte b = skip(); b > 32; b = getc()) n = n * 10 + b - '0'; return 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 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
ffdfb79501acc9275ce685fda8a010ad
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(); while (t-->0){ int n = in.nextInt(); in.nextLine(); char[] arr = in.nextLine().toCharArray(); long[] num = new long[n]; for (int i=0; i<n; i++){ if (arr[i] == 'L'){ num[i] = i; }else { num[i] = n - 1 - i; } } Arrays.sort(num); long max = 0; 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(); } } }
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
17209f058edfef7068d23b9394791f01
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.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Main { static PrintWriter out = new PrintWriter(System.out); 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) { FastReader sc = new FastReader(); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); String s = sc.next(); char ch[] = s.toCharArray(); int val[] = new int[n]; for(int i=0;i<n;i++) { char c = ch[i]; if(c=='R') { val[i] = n-i-1; }else { val[i] = i; } } long sum = 0l; for(int ele:val) sum+=ele; int i = 0; int j = n-1; int cnt = 0; ArrayList<Long> each = new ArrayList<>(); while(cnt<n && i<n/2 && j>=n/2) { if(ch[i]=='R' && ch[j]=='L') { i++; j--; }else { if(ch[i]=='L' && ch[j]=='R') { int v1 = n-i-1; int v2 = j; if(v1>v2) { sum-=val[i]; sum+=v1; i++; }else if(v2>=v1) { sum-=val[j]; sum+=v2; j--; } }else if(ch[i]=='L') { int v1 = n-i-1; sum-=val[i]; sum+=v1; i++; j--; }else if(ch[j]=='R') { int v1 = j; sum-=val[j]; sum+=v1; j--; i++; } cnt++; each.add(sum); } } while(i<n/2 && cnt<n) { if(ch[i]=='L') { int v1 = n-i-1; sum-=val[i]; sum+=v1; i++; } each.add(sum); cnt++; } while(j>=n/2 && cnt<n) { if(ch[j]=='R') { int v1 = j; sum-=val[j]; sum+=v1; j--; } each.add(sum); cnt++; } while(cnt<n) { each.add(sum); cnt++; } for(long ele:each) print(ele+" "); println(); } out.flush(); out.close(); } public static int dis(int x,int y) { return Math.abs(x-y); } public static int dir[][] = {{1,0},{0,1},{-1,0},{0,-1}}; public static class pair{ int val; int cnt; pair(int v1,int v2){ this.val = v1; this.cnt = v2; } } public static class graph{ int v; int maxbit; ArrayList<int[]> edgelist = new ArrayList<>(); ArrayList<int[]> adj[]; public static int[][] table; graph(int v){ this.v = v; adj = new ArrayList[v]; for(int i=0;i<v;i++) adj[i] = new ArrayList<>(); maxbit = 19; } public void addD(int f,int t){ int a[] = {f,t}; adj[f].add(a); edgelist.add(new int[] {f,t}); } public void addUD(int f,int t) { addD(f,t); addD(t,f); } public void addD(int f,int t,int w){ int a[] = {f,t,w}; adj[f].add(a); edgelist.add(new int[] {f,t,w}); } public void addUD(int f,int t,int w) { addD(f,t,w); addD(t,f,w); } public void printEdgelist() { for(int ele[]:edgelist) { println(ele); } } public void parentArray(int par[],int src,int parent) { for(int ele[]:adj[src]) { int t = ele[1]; if(t==parent) continue; par[t] = src; parentArray(par,t,src); } } public boolean IsBipartite(int i,boolean visited[]) { int color[] = new int[v]; Arrays.fill(color, -1); Queue<int[]> queue = new ArrayDeque<>(); queue.add(new int[] {i,1,-1}); while(queue.size()>0) { int top[] = queue.poll(); int src = top[0]; int pc = top[1]; int parent = top[2]; visited[src] = true; if(color[src]!=-1) { if(color[src]==pc) return false; }else color[src] = 1-pc; for(int ele[]:adj[src]) { int t = ele[1]; if(t==parent) continue; else { if(!visited[t]) queue.add(new int[] {t,color[src],src}); } } } return true; } public void printGraph() { for(int i=0;i<v;i++) { out.print(i+" - "); for(int ele[]:adj[i]) { out.print(ele[1]+" "); } out.println(); } } public void fillLevel(int src,int d,int level[],int parent) { level[src] = d; for(int ele[]:this.adj[src]) { int t = ele[1]; if(t==parent) continue; fillLevel(t,d+1,level,src); } } public void LcaTable(int parent[]) { table = new int[maxbit+1][v]; table[0] = parent; for(int i=1;i<=maxbit;i++) { for(int j=0;j<v;j++) { table[i][j] = table[i-1][table[i-1][j]]; } } } public int getLCA(int u,int v,int level[],int parent[]) { if(level[u]>level[v]) { int temp = u; u = v; v = temp; } int k = level[v]-level[u]; for(int i=0;i<maxbit;i++) { int bit = 1<<i; if((bit&k)>0) { v = table[i][v]; } } if(v==u) return u; for(int i=maxbit;i>=0;i--) { int p1 = table[i][u]; int p2 = table[i][v]; if(p1==p2) continue; else { u = p1; v = p2; } } return table[0][u]; } } public static int[] mergeSort(int arr[],int l,int r) { if(l>r) return new int[] {}; if(l==r) return new int[] {arr[l]}; int mid = l+(r-l)/2; int larr[] = mergeSort(arr,l,mid); int rarr[] = mergeSort(arr,mid+1,r); int newarr[] = merge(larr,rarr); return newarr; } public static int[] merge(int a[],int b[]) { int m[] = new int[a.length+b.length]; int i=0; int j=0; int ind = 0; while(i<a.length && j<b.length) { if(a[i]<b[j]) { m[ind] = a[i]; i++;ind++; }else { m[ind++] = b[j++]; } } while(i<a.length) m[ind++] = a[i++]; while(j<b.length) m[ind++] = b[j++]; return m; } public static void print(String str) { out.print(str); } public static void println() { out.println(); } public static void print(int a) { out.print(a); } public static void print(char c) { out.print(c); } public static void print(char c[]) { out.print(Arrays.toString(c)); } public static void print(int a[]) { out.println(Arrays.toString(a)); } public static void print(String a[]) { out.print(Arrays.toString(a)); } public static void print(float a) { out.print(a); } public static void print(double a) { out.print(a); } public static void print(long a[]) { out.print(Arrays.toString(a)); } public static void println(String str) { out.println(str); } public static void println(int a) { out.println(a); } public static void println(char c) { out.println(c); } public static void println(char c[]) { out.println(Arrays.toString(c)); } public static void println(int a[]) { out.println(Arrays.toString(a)); } public static void println(String a[]) { out.println(Arrays.toString(a)); } public static void println(float a) { out.println(a); } public static void println(double a) { out.println(a); } public static void println(long a[]) { out.println(Arrays.toString(a)); } public static void print(int a[][]) { for(int ele[]:a) { out.println(Arrays.toString(ele)); } out.println(); } public static void print(char a[][]) { for(char ele[]:a) { out.println(Arrays.toString(ele)); } } public static void print(long a[][]) { for(long ele[]:a) { out.println(Arrays.toString(ele)); } } public static void print(String a[][]) { for(String ele[]:a) { out.println(Arrays.toString(ele)); } } public static void println(long a) { out.println(a); } public static void println(Long a) { out.println(a); } public static void println(Integer a) { out.println(a); } public static void println(Long a[]) { out.println(Arrays.toString(a)); } public static void println(Integer a[]) { out.println(Arrays.toString(a)); } public static void println(boolean a[]) { out.println(Arrays.toString(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 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
098c438a729a8e5cab242f937447be64
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 codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-->0){ int n = scan.nextInt(); String str = scan.next(); long total = 0; for(int i=0; i<n; i++){ if(str.charAt(i)=='L') total+=i; else if(str.charAt(i)=='R') total+=n-1-i; } int temp = n/2; int count = 0; for(int i=0; i<temp; i++){ int index1 = i; int index2 = n-1-i; if(str.charAt(index1)=='L'){ total=total-index1; total=total+n-1-i; System.out.print(total+" "); count++; } if(str.charAt(index2)=='R'){ total=total-(n-index2-1); total=total+index2; System.out.print(total+" "); count++; } } while(count < n){ System.out.print(total+" "); count++; } System.out.println(); }//while } }
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
1610cf3c84a95395fc721b93971a9ded
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 Balabizo { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tt = sc.nextInt(); while(tt-->0) { int n =sc.nextInt(); char[] c = sc.next().toCharArray(); Long[] pre = new Long[n]; long sum = 0; for(int i=0; i<n ;i++){ if(c[i] == 'L') pre[i] = (long)i; else pre[i] = (long) n-i-1; sum += pre[i]; } Arrays.sort(pre); for(int i=0; i<n ;i++){ if(pre[i] < n/2){ sum -= pre[i]; sum += n-pre[i]-1; } pw.print(sum+" "); } pw.println(); } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws IOException { br = new BufferedReader(new FileReader(file)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } 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 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(); } } }
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
27f64ee1ea90217b0364d6b67cf97e84
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.PriorityQueue; import java.util.Scanner; public class Solution { 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(); sc.nextLine(); String str = sc.nextLine(); maxLineSums(str, n); } } static void maxLineSums(String str, int n) { PriorityQueue<Integer> pq = new PriorityQueue<>(); long sum = 0; for (int i = 0; i < n; i++) { char c = str.charAt(i); int power = 0; if (c == 'L') { power = i - 0; } else { power = (n - 1) - i; } sum += power; if (power < n / 2) { pq.add(power); } } for (int k = 1; k <= n; k++) { if (!pq.isEmpty()) { int x = pq.poll(); sum += n - 1 - x; sum -= x; } 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 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
4f3206a5cd31c1b443aeda0eec079362
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
//created by Toufique on 30/08/2022 import java.io.*; import java.util.*; public class Div4_117D { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = in.nextInt(); for (int tt = 0; tt < t; tt++) { int n = in.nextInt(); char[] s = ("1" + in.next()).toCharArray(); long[] a = solve(n, s); for (long i: a) pw.print(i + " "); pw.println(); } pw.close(); } static long[] solve(int n, char[] s) { ArrayList<Long> ls = new ArrayList<>(); ArrayList<Long> diff = new ArrayList<>(); long tot = 0; for (int i = 1; i <= n; i++) { if (s[i] == 'R') { long v = n - i; tot += v; if (v < (i - 1)) { diff.add((long)(i - 1) - v); }else diff.add(0L); } else { tot += ((long)i - 1); if ((n - i) > (i - 1)) { diff.add((long)(n - i) - (long)(i - 1)); } else diff.add(0L); } } Collections.sort(diff); Collections.reverse(diff); long[] a = new long[diff.size()]; a[0] = diff.get(0); for (int i = 1; i < n; i++) { a[i] = a[i - 1] + diff.get(i); } // debug(tot); long[] ans = new long[n]; ans[0] = tot + a[0]; for (int i = 1; i < n; i++) ans[i] = tot + a[i]; return ans; } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
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
e88fe5f2e86e965594688347a15307ef
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; public class D2908 { public static void main(String[] args) { try { final BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(in.readLine()); long [][] vals = new long[t][]; for (int i = 0; i < t; i++) { int n = Integer.parseInt(in.readLine()); char [] row1 = in.readLine().toCharArray(); vals[i] = new long[n]; int rowLInd = n / 2; ArrayList <Integer> LW = new ArrayList<>(); ArrayList <Integer> RW = new ArrayList<>(); long sum = 0; for (int j = 0; j < n; j++) { if (j < rowLInd) { if (row1[j] == 'L') { LW.add(j); } } else { if ((n % 2 == 1) && (j == rowLInd)){ } else if (row1[j] == 'R') { RW.add(j); } } if (row1[j] == 'L') { sum += j; } else { sum += n - j - 1; } } int indL = 0; int indR = RW.size() - 1; for (int j = 0; j < n; j++) { int LV = -1; int RV = -1; int incr = 0; if (indR >= 0) { RV = RW.get(indR); } if (indL < LW.size()) { LV = LW.get(indL); } if (LV != -1 || RV != - 1) { if (LV != -1) { if (RV != -1) { if ((n - LV - 1) < RV) { incr = RV - (n - RV - 1); indR--; } else { incr = n - LV - 1 - (LV); indL++; } } else { incr = n - LV - 1 - (LV); indL++; } } else { incr = RV - (n - RV - 1); indR--; } } sum += incr; vals[i][j] = sum; } // rec("", 0, ar, n - 1); //vals[i] = (String[]) ar.toArray(); } for (int i = 0; i < t; i++) { StringBuffer strBuffer = new StringBuffer(); for (int b = 0; b < vals[i].length; b++) { strBuffer.append(vals[i][b]).append(" "); } System.out.println(strBuffer.toString()); } } catch (final IOException e) { e.printStackTrace(); } } }
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
a4679fb8fe8e108fd4e4baba4f2ee5e1
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 tp { public static void main(String[] args) throws IOException{ BufferedReader bs = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(bs.readLine()); while (t-- != 0){ int size = Integer.parseInt(bs.readLine()); String ip1 = bs.readLine(); PriorityQueue<Integer> q1 = new PriorityQueue<>(); if((size&1)==1){ int half = size/2; for(int i = 0;i<half;i++){ if(ip1.charAt(i)=='L'){ q1.add(i); } } for(int i = half+1;i<ip1.length();i++){ if(ip1.charAt(i)=='R'){ q1.add(ip1.length()-1-i); } } } else{ int half = size/2; for(int i = 0;i<half;i++){ if(ip1.charAt(i)=='L'){ q1.add(i); } } for(int i = half;i<ip1.length();i++){ if(ip1.charAt(i)=='R'){ q1.add(ip1.length()-1-i); } } } long count = 0; for(int i = 0;i<ip1.length();i++) { if (ip1.charAt(i) == 'L') { count += i; } else { count += ip1.length() - 1 - i; } } int l = ip1.length()- q1.size(); StringBuffer sb = new StringBuffer(); while(!q1.isEmpty()) { int p = q1.poll(); count += ip1.length() - p-1; count -= p; sb.append(count + " "); } while(l-- != 0){ sb.append(count + " "); } 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 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
a18f8840464b480eee5a0013e5172035
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 tp { static class Pair implements Comparable<Pair>{ Character ip; int dist; Pair(Character ip,int dist){ this.ip = ip; this.dist = dist; } @Override public int compareTo(Pair pair) { return this.dist-pair.dist; } @Override public String toString() { return "Pair{" + "ip='" + ip + '\'' + ", dist=" + dist + '}'; } } public static void main(String[] args) throws IOException{ BufferedReader bs = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(bs.readLine()); while (t-- != 0){ int size = Integer.parseInt(bs.readLine()); String ip1 = bs.readLine(); PriorityQueue<Pair> q1 = new PriorityQueue<>(); if((size&1)==1){ int half = size/2; for(int i = 0;i<half;i++){ if(ip1.charAt(i)=='L'){ q1.add(new Pair(ip1.charAt(i),i)); } } for(int i = half+1;i<ip1.length();i++){ if(ip1.charAt(i)=='R'){ q1.add(new Pair(ip1.charAt(i),ip1.length()-1-i)); } } } else{ int half = size/2; for(int i = 0;i<half;i++){ if(ip1.charAt(i)=='L'){ q1.add(new Pair(ip1.charAt(i),i)); } } for(int i = half;i<ip1.length();i++){ if(ip1.charAt(i)=='R'){ q1.add(new Pair(ip1.charAt(i),ip1.length()-1-i)); } } } long count = 0; for(int i = 0;i<ip1.length();i++) { if (ip1.charAt(i) == 'L') { count += i; } else { count += ip1.length() - 1 - i; } } int l = ip1.length()- q1.size(); StringBuffer sb = new StringBuffer(); while(!q1.isEmpty()) { Pair p = q1.poll(); count += ip1.length() - p.dist-1; count -= p.dist; sb.append(count + " "); } while(l-- != 0){ sb.append(count + " "); } 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 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
5eec39c8b611d59a7c861417023869e4
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.*; public class D_1722 { public static void main(String[] args) { FastReader sc = new FastReader(); PrintWriter out=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); String s=sc.nextLine(); 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]; out.print(val+" "); } out.println(); } out.flush(); } 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
d041898cd0f08fbd4c2e49cae84412e8
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 CF1722D { public static void main (String[] arg) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); for(int test = 0; test < n; test++) { int l = Integer.parseInt(br.readLine()); String s = br.readLine(); Vertex[] A = new Vertex[l]; long sum = 0; long[] ans = new long[l]; for(int i = 0; i < l; i++) { if (s.charAt(i) == 'L') { A[i] = new Vertex(i - 0, l - i - 1, true); sum += A[i].l; } else if (s.charAt(i) == 'R') { A[i] = new Vertex(i - 0, l - i - 1, false); sum += A[i].r; } A[i].gap = (A[i].left) ? A[i].r - A[i].l : A[i].l - A[i].r; } Arrays.sort(A); Arrays.fill(ans, sum); long[] prefixS = new long[l + 1]; for(int i = 1; i <= l; i++) { prefixS[i] = (A[i - 1].gap <= 0) ? prefixS[i-1]: prefixS[i-1] + A[i - 1].gap; } for(int i = 0; i < l; i++) { ans[i] = sum + prefixS[i+1]; } for(int i = 0; i < l - 1; i++) { System.out.print(ans[i] + " "); } System.out.println(ans[l - 1]); } } static class Vertex implements Comparable <Vertex>{ int l, r; boolean left; int gap; // flipped - original public Vertex (int l_, int r_, boolean left_) { l = l_; r = r_; left = left_; } @Override public int compareTo (Vertex other) { return other.gap - gap; } } }
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
ff6cc62d985f4c102fb82cc6ed38d116
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 javax.naming.ContextNotEmptyException; import java.io.*; import java.lang.Math; public class cp{ 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 sort(int[] a,boolean flag){ ArrayList<Integer> temp=new ArrayList<>(); for(int n:a) temp.add(n); Collections.sort(temp); if(flag){ for(int i=0;i<a.length;i++){ a[i]=temp.get(i); } } else{ for(int i=a.length-1;i>=0;i--){ a[i]=temp.get(i); } } } public static int gcd(int a,int b){ if (b == 0) return a; else return gcd(b, a % b); } 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(); char c[]=s.toCharArray(); long total=0; List<Integer> list=new ArrayList<>(); for(int i=0;i<n;i++){ if(c[i]=='L') total+=i; else total+=n-i-1; if(c[i]=='L' && i<n-i-1){ list.add(n-i-1-i); } else if(c[i]=='R' && i>n-i-1){ list.add(i-(n-i-1)); } } Collections.sort(list); Collections.reverse(list); long sum=0; for(int i=0;i<list.size();i++){ sum+=list.get(i); System.out.print((total+sum)+" "); } n-=list.size(); for(int i=0;i<n;i++){ System.out.print((total+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 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
970cb7d3a3e9cc65ba6ce8d192b782e4
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 div_4_817_a { public static void main(String args[]){ FScanner in = new FScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while(t-->0) { int n=in.nextInt(); char ch[]=in.next().toCharArray(); // int a[]=new int[n]; //TreeMap<Integer,Character> map=new TreeMap<>(); PriorityQueue<Pair> pq=new PriorityQueue<>(); long sum=0; for(int i=0;i<n;i++){ if(ch[i]=='L'){ sum+=i; pq.add(new Pair(i,i)); } else{ sum+=((n-1)-i); pq.add(new Pair(((n-1)-i),i)); } } //out.println(sum); //ArrayList<Integer> al=new ArrayList<>(); while(!pq.isEmpty()){ Pair p=pq.poll(); if(p.y<n/2){ sum+=(((n-1)-p.y)-p.x); } else if(p.y>=n/2){ sum+=(p.y-p.x); } out.print(sum+" "); } out.println(); } out.close(); } static class Pair implements Comparable<Pair> { int x; int y; Pair(int a,int b) { x=a; y=b; } @Override public int compareTo(Pair obj) { if(obj.x-this.x>0) return -1; else if(obj.x-this.x<0) return 1; else return 0; } } static class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } 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 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
015393bb71d95730f422140c597c8034
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.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = Integer.parseInt(sc.nextLine()); for (int i = 0; i < t; i++) { int n = Integer.parseInt(sc.nextLine()); String s = sc.nextLine(); long sum = 0; for (int j = 0; j < n; j++) { if (s.charAt(j) == 'L') { sum += j; } else sum += n-j-1; } int mid = n/2; int l = 0, r = n-1; for (int k = 1; k <= n; k++) { if (l >= mid && r < mid) { System.out.printf("%d ",sum); if (k==n) System.out.printf("\n"); continue; } while (l<mid && s.charAt(l)=='R') l++; while (r>=mid && s.charAt(r)=='L') r--; if (l >= mid && r < mid) { System.out.printf("%d ",sum); if (k==n) System.out.printf("\n"); continue; } int benefitl = n - l - 1 - l; int benefitr = r - n + r + 1; if (benefitl > benefitr) { l++; sum+=benefitl; System.out.printf("%d ",sum); }else { r--; sum+=benefitr; System.out.printf("%d ",sum); } if (k==n) System.out.printf("\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 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
4592b3ea874d7e7e636bf0dac780f7aa
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[]){ FScanner in = new FScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while(t-->0) { int n=in.nextInt(); String s=in.next(); char ch[]=s.toCharArray(); int mid=n/2; if(n%2==0) mid--; int arr[]=new int[n]; int pf[]=new int[n]; for(int i=0;i<=mid;i++) pf[i]=(n-(i+1)); for(int j=mid+1;j<n;j++) pf[j]=j; long sum=0; for(int i=0;i<n;i++) { if(ch[i]=='R') { arr[i]=(n-(i+1)); } else arr[i]=i; sum+=arr[i]; } int lst=n-1,c=0,pr=0; //out.println(sum); for(int i=0;i<n;i++) { if(c==n) break; if(i>lst) { out.print(sum+" "); c++; continue; } if(pf[i]-arr[i]==0 && pf[lst]-arr[lst]==0) { lst--; pr++; continue; } if(pf[i]-arr[i]>(pf[lst]-arr[lst])) { sum-=arr[i]; sum+=pf[i]; if(pf[lst]-arr[lst]==0) lst--; } else if(pf[lst]-arr[lst]>=(pf[i]-arr[i])) { sum-=arr[lst]; sum+=pf[lst]; lst--; if(pf[i]-arr[i]!=0) i--; } out.print(sum+" "); c++; if(lst<0) break; } for(int i=c;i<n;i++) out.print(sum+" "); out.println(); } out.close(); } 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 boolean checkprime(int n1) { if(n1%2==0||n1%3==0) return false; else { for(int i=5;i*i<=n1;i+=6) { if(n1%i==0||n1%(i+2)==0) return false; } return true; } } static class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } 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 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
4a5268f72e4bc7bbad3e34dd2bb121e0
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 in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(in.readLine()); while(t-- > 0) { int n = Integer.parseInt(in.readLine()); String[] s = in.readLine().split(""); long sum = 0L; //System.out.println(Arrays.toString(s) +" "+n); for (int i = 0; i < s.length; i++) { if (s[i].equals("L")) { sum += i; //System.out.println("L"); } else { sum += (n - (i + 1)); //System.out.println("R"); } //System.out.println(i); } //System.out.println(sum); int k = n; int mid = (n / 2) - 1; int left = 0; int right = s.length - 1; while (left <= right) { if (left <= mid) { if (s[left].equals("L")) { sum -= left; sum += (n - (left + 1)); k--; out.write(sum+" "); } } if (right > mid && right > left) { if (s[right].equals("R")) { sum -= (n - (right + 1)); sum += right; k--; out.write(sum +" "); } } left++; right--; } while (k > 0) { out.write(sum+" "); k--; } out.write("\n"); 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
82f8cdbaeb4714a85afa6717dfccf76b
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.HashMap; import java.util.Scanner; public class cfContest1722 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int t = scan.nextInt(); k: while (t-- > 0) { int n = scan.nextInt(); String s = scan.next(); int[] f = new int[n]; long sum = 0; char[] k = new char[n]; for (int i = 0; i < n; i++) { k[i] = s.charAt(i); if (s.charAt(i) == 'L') { sum += i; f[i] = i; } else { sum += (n - 1 - i); f[i] = (n - i - 1); } } int l = 0; int r = n - 1; for (int i = 0; i < n; i++) { while (l < n && k[l] != 'L') { ++l; } while (r>=0 && k[r] != 'R') { --r; } // System.out.println(l+" "+r); int left = 0; int right = 0; if (l < n && k[l] == 'L') { left = (n - l - 1) - f[l]; } if (r >= 0 && k[r] == 'R') { right = r - f[r]; } if (left > right && left >= 1) { sum -= f[l]; sum += (n - 1 - l); k[l] = 'R'; f[l] = 100000000; } else if (right >= 1) { sum -= f[r]; sum += r; k[r] = 'L'; f[r] = 100000000; } // System.out.println(left + " " + right+" "+l+" "+r+" "+k[l]); sb.append(sum + " "); } sb.append("\n"); } System.out.println(sb); } } /* 6 3 LLR 5 LRRLL 1 L 12 LRRRLLLRLLRL 10 LLLLLRRRRR 9 LRLRLRLR 3 5 5 16 16 16 16 16 0 86 95 98 101 102 102 102 102 102 102 102 102 29 38 45 52 57 62 65 68 69 70 44 50 54 56 56 56 56 56 56 */
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
6122ad182d588d11e38b330b1c513967
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 sc=new Scanner(System.in); int T=sc.nextInt(); for(int i=0;i<T;i++) { int n=sc.nextInt(); String s=sc.next(); int k=0; int start=0,end=n-1; long total=0; while(start<end) { char ch=s.charAt(start); if(ch=='L') total+=start; else total+=(n-start-1); ch=s.charAt(end); if(ch=='L') total+=end; else total+=(n-end-1); start++; end--; } if(n%2!=0) { if(s.charAt(start)=='L') total+=start; else total+=(n-start-1); } start=0;end=n-1; int last=0; while(start<end) { char ch=s.charAt(start); if(ch=='L') { total-=start; total+=(n-start-1); k++; } if(k!=last) { System.out.print(total+" "); last=k; } if(k==n) break; ch=s.charAt(end); if(ch=='R') { total-=(n-end-1); total+=end; k++; } if(k!=last) { System.out.print(total+" "); last=k; } if(k==n) break; start++; end--; } while(k<n) { System.out.print(total+" "); 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
8a8fdd9df1897c495a56295dc823a719
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.*; public class Codechef { 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 double nextDouble() throws IOException { return Double.parseDouble(next()); } } static Reader in = new Reader(); static PrintWriter out = new PrintWriter(System.out); static long mod = 998244353; //-----------------------------INPUT-------------------------------------------------// public static int ni() throws IOException { return in.nextInt(); } public static long nl() throws IOException { return in.nextLong(); } public static String rl() throws IOException { return in.next(); } public static char nc() throws IOException { return in.next().charAt(0); } //----------------------------ARRAYS INPUT--------------------------------------------// public static int[] ai(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } return arr; } public static long[] al(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextLong(); } return arr; } public static char[] ac() throws IOException { String s = in.next(); return s.toCharArray(); } //----------------------------Print---------------------------------------------------// public static void pt(int a) { out.println(a + " "); } public static void pt(long a) { out.println(a + " "); } public static void pt(double a) { out.println(a + " "); } public static void pt(char a) { out.println(a + " "); } public static void pt(int[] arr) { for (int j : arr) out.print(j + " "); out.println(); } public static void pt(long[] arr) { for (long l : arr) out.print(l + " "); out.println(); } public static void pt(char[] arr) { for (char l : arr) out.print(l + " "); out.println(); } public static void pt(String s) { out.println(s + " "); } //--------------------------------------------Decleration Functions-----------------------------------------------// public static HashSet<Character> set(char c) { HashSet<Character> h = new HashSet<>(); return h; } public static HashSet<Integer> set() { HashSet<Integer> h = new HashSet<>(); return h; } public static HashSet<Long> set(long l) { HashSet<Long> h = new HashSet<>(); return h; } //--------------------------------------------Other Functions----------------------------------------------------// public static long gcd(long a, long b) { BigInteger x = BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)); return Long.parseLong(String.valueOf(x)); } public static long expo(long a, long b) { long res = 1; while (b > 0) { if ((b & 1) != 0) res = res * a; a = a * a; b >>= 1; } return res; } public static long modexp(long a, long b) { long res = 1; while (b > 0) { if ((b & 1) != 0) res = (res * a % mod) % mod; a = (a % mod * a % mod) % mod; b >>= 1; } return res % mod; } public static int[] permute(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = i; } return arr; } public static long min(long[] arr) { long min = Long.MAX_VALUE; for (long l : arr) { if (l < min) min = l; } return min; } public static long max(long[] arr) { long max = Long.MIN_VALUE; for (long l : arr) { if (l > max) max = l; } return max; } 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); } } public static long countfactors(long n) { long ans = 0; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ans += 2; if (n / i == i) ans--; } } return ans; } public static boolean isprime(long n) { for (long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } //--------------------------------------------------------------------------------------------------------------------------// //--------------------------------------MAIN--------------------------------------------// public static void main(String[] args) throws Exception { int t=ni(); while(t--!=0) { int n = ni(); char[] arr = ac(); long s = 0; for (int i = 0; i < n; i++) { if (arr[i] == 'L') s += i; else s += (n - i - 1); } ArrayList<Long> ar=new ArrayList<>(); for(int i=0;i<n/2;i++) { if(arr[i]=='L') { long k=(n-i-1); k-=i; ar.add(k) ; } } for(int i=n/2;i<n;i++) { if(arr[i]=='R') { long k=i; k-=(n-i-1); ar.add(k) ; } } // System.out.println(ar); Collections.sort(ar,Collections.reverseOrder()); // System.out.println(s+"---"); for(int i=0;i<n;i++) { if(i<ar.size()) { s+=ar.get(i); } System.out.print(s+" "); } System.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 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
453caa628c17c87e4d436eb7f5b1a3c3
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); Solve solver = new Solve(); int T = 1; T = in.nextInt(); for (int i = 1; i <= T; i++) { solver.solve(i, in, out); } out.close(); } static class Solve { public int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static final long inf = 0x3f3f3f3f; static final int N = (int) 2e5 + 7; static final int mod = (int) 1e9 + 7; static final int[] monthDay = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; public void solve(int testNumber, InputReader in, OutputWriter out) { //ow.println("Case #"+testNumber+": "); //write code here //long startTime = System.currentTimeMillis(); int n = in.nextInt(); char[] ch = in.next().toCharArray(); long ans = 0; for (int i = 0; i < ch.length; i++) { if(ch[i]=='L') ans += i; else ans += n-i-1; } //System.out.println(ans); long preAns = ans; List<Integer> list = new ArrayList<>(); if((n&1)==0){ for (int i = 0; i < n / 2; i++) { if(ch[i]=='L') list.add(n-2*i-1); else list.add(0); } for (int i = n/2; i < n; i++) { if(ch[i]=='R') list.add(i-(n-i-1)); else list.add(0); } }else{ for (int i = 0; i <= n / 2; i++) { if(ch[i]=='L') list.add(n-2*i-1); else list.add(0); } for (int i = n/2+1; i < n; i++) { if(ch[i]=='R') list.add(i-(n-i-1)); else list.add(0); } } Collections.sort(list); // for (Integer it : list) { // out.print(it + " "); // } // out.println(); for(int i = list.size() - 1 ; i >= 0 ; i--){ ans += Math.max(0,list.get(i)); out.print(ans +" "); } out.println(); //long endTime = System.currentTimeMillis();int time=(int) (endTime - startTime);System.out.println("运行时间:" + time + "ms"); //here is end! } static void test(int[] nums, OutputWriter out) { for (int num : nums) out.print(num + " "); out.println(); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } writer.println(); } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } long sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } }
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
a52ac8a069a23bc8a714f5661794fc1f
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
//SHIVAM GARG import java.util.*; import java.io.*; import java.lang.*; public class Codechef { public static FastReader reader = new FastReader(); public static int[] primes; public static void main(String[] args) throws java.lang.Exception { int n = reader.nextInt(); for(int i=0; i<n; i++){ solve(); } } public static void solve(){ int n = reader.nextInt(); char[] arr = reader.nextLine().toCharArray(); long current = 0L; long[] serve = new long[n]; for(int i=0; i<n; i++){ int view = (arr[i] == 'L') ? i : (n-i-1); current = current+view; serve[i] = Math.max(i, n-i-1) - view; } Arrays.sort(serve); StringBuilder builder = new StringBuilder(""); for(int i=n-1; i>=0; i--){ current = current + serve[i]; builder.append(current + " "); } System.out.println(builder.toString()); } public static int gcd_one(int n ){ if(n == 1) return 2; if(n == 2) return 3; if(n == 3) return 4; int sqrt = ((int)Math.sqrt(n))+1; for(int i= sqrt; i< primes.length; i++){ if(primes[i] == 1){ return i*i; } } return -1; } public static int[] sieve(int n){ //-1-> not prime , 1-> prime primes = new int[n+1]; primes[1] = -1; for(int i =2 ; i<=n; i = i+2){ primes[i]= -1; } primes[2] = 1; for(int i =3 ; i<=n; i = i+2){ if(primes[i] == 0 ){ for(int j = i; j<=n; j = j+i){ primes[j]= -1; } primes[i] =1; } } return primes; } static int gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } public static HashMap<Integer, Integer> factors(long n) { HashMap < Integer, Integer > tracker = new HashMap < Integer, Integer > (); if (n % 2 == 0) { tracker.put(2, 0); } while (n % 2 == 0) { tracker.put(2, tracker.get(2) + 1); n = n / 2; } int tt = (int) Math.sqrt(n) + 3; for (int i = 3; i < tt; i = i + 2) { if (i > n) { break; } if (n % i == 0) { tracker.put(i, 0); } while (n % i == 0) { tracker.put(i, tracker.get(i) + 1); n = n / i; } } return tracker; } /* Iterative Function to calculate (x^y)%p in O(log y) */ public static long prime = 1000000007; public static long[] fact; static long power(long x, long y) { // Initialize result long res = 1; // Update x if it is more than or // equal to p x = x % prime; while (y > 0) { // If y is odd, multiply x with result if (y % 2 == 1) res = (res * x) % prime; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % prime; } return res; } // Returns n^(-1) mod p static long modInverse(long n) { return power(n, prime - 2); } // Returns nCr % p using Fermat's little theorem. static long nCrModPFermat(int n, int r) { // Base case if (r == 0) return 1; long a1 = fact[n]; long a2 = modInverse(fact[r]) % prime; long a3 = modInverse(fact[n - r]) % prime; return ((fact[n] * a2) % prime * a3) % prime; } 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()); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int ii = 0; ii < n; ii++) { arr[ii] = this.nextInt(); } return arr; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char nextChar(){ return next().charAt(0); } 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
0e39c3defb074146333ed9a2b6f63997
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 Main { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static Scanner sc = new Scanner(System.in); public static void main(String[] args) throws IOException { int n = Integer.parseInt(readLine()); while(n-- > 0){ int m = Integer.parseInt(readLine()); String s = readLine(); long curr = 0; for(int i = 0 ; i < s.length();i++){ if(s.charAt(i) =='L'){ curr+=i; }else{ curr+=(m-i-1); } } int left = 0,right = m-1; List<Long> lists = new ArrayList<>(); long ans = curr; while(left < right){ boolean f1 = false; if(s.charAt(left) == 'L'){ ans+=(m-1-left*2); f1 = true; } if(f1) { lists.add(ans); } boolean f2 = false; if(s.charAt(right) == 'R'){ ans+=(m-1-(m-1-right)*2); f2 = true; } if(f2) { lists.add(ans); } left++; right--; } for(int i = 0 ; i <m;i++) { if(lists.size()>0) { if (i < lists.size()) { System.out.print(lists.get(i) + " "); } else { System.out.print(lists.get(lists.size() - 1) + " "); } }else{ System.out.print(curr+" "); } } System.out.println(); } } public static String readLine() throws IOException { return reader.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 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
9c239826559e05acdd9d3ff67baac8c6
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.Collections; import java.util.Iterator; import java.util.StringTokenizer; public class Main { static int N; static long sum; static Integer gap[]; static char[] str; public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = null; st = new StringTokenizer(br.readLine()); int T = Integer.parseInt(st.nextToken()); for (int test_case = 0; test_case < T; test_case++) { N = Integer.parseInt(br.readLine()); sum = 0; gap = new Integer[N]; str = br.readLine().toCharArray(); for (int i = 0; i < N; i++) { if(str[i]=='L') { sum += i; gap[i] = N - 1 - i - i; } else { sum += N - 1 - i; gap[i] = i - (N-1-i); } } Arrays.sort(gap, Collections.reverseOrder()); long tmpSum = sum; long max = sum; for (int i = 0; i < N; i++) { tmpSum += gap[i]; if(max < tmpSum) max = tmpSum; System.out.print(max); if(i!=N-1) 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 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
b0affb79b437ee26bbecba716882fa26
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.math.BigInteger; import java.util.*; import java.io.*; public class test1 { static int m, d, n; static char[] a; static long[][][][] dp; static long sum; static int sx; static int sy; static long[][] memo; static int c=0; public static long dp(int i,int j) { //System.out.println(i+" "+j); c++; if(i>n||j>m)return (long)1e12; if(i==n&&j==m) { if(Math.abs(sx-i)+Math.abs(sy-j)>d)return 0; return (long)1e12; } if(Math.abs(sx-i)+Math.abs(sy-j)<=d) { // System.out.println(i+" "+j); return (long)1e12; } if(memo[i-1][j-1]!=-1)return memo[i-1][j-1]; long down=1+dp(i+1,j); long right=1+dp(i,j+1); // long cross=1+dp(i+1,j+1); return memo[i-1][j-1]= Math.min(right,down); } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter sp = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); String s=sc.next(); long[] perf=new long[n]; long[] x=new long[n]; int st=n-1; long max=0; for(int i=0;i<n;i++) { perf[i]=st; max+=st; if(i<n/2) { if(n%2==0&&i==(n/2)-1)continue; st--; } else st++; } //for(int i:perf)sp.print(i+" "); //sp.println(); long sum=0; int l=0; for(int i=0;i<n;i++) { if(s.charAt(i)=='L') { x[i]=i; sum+=x[i]; } else { x[i]=n-i-1; sum+=x[i]; } if(x[i]!=perf[i])l++; } //for(int i:x)sp.print(i+" "); //sp.println(); //sp.println(sum+" "+max); ArrayList<Long>ans=new ArrayList<Long>(); for(int i=0;i<n;i++) { if(x[i]!=perf[i]) { ans.add(perf[i]-x[i]); } else ans.add(0l); } Collections.sort(ans,Collections.reverseOrder()); for(int i=0;i<n;i++) { sp.print(sum+ans.get(i)+" "); sum+=ans.get(i); } sp.println(); //int max= } sp.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws IOException { br = new BufferedReader(new FileReader(file)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } 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 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(); } } }
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
780c4bc5d90b53b676a656fe1473c82a
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
/*############################################################################################################ ########################################## >>>> Diaa12360 <<<< ############################################### ########################################### Just Nothing ################################################# #################################### If You Need it, Fight For IT; ######################################### ###############################################.-. 1 5 9 2 .-.################################################ ############################################################################################################*/ import java.io.*; import java.math.BigInteger; import java.util.*; public class Solution { static final int maxN = 1000000000; static int dx[] = {1, 0, 0, -1}; static int dy[] = {0, -1, 1, 0}; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // in = new BufferedReader(new FileReader("in.text")); StringBuilder out = new StringBuilder(); StringTokenizer tk; int t = anInt(in.readLine()); while (t-- > 0) { int n = anInt(in.readLine()); char[] lf = in.readLine().toCharArray(); int[] arr = new int[n]; int[] rev = new int[n]; long sum = 0; for (int i = 0; i < n; i++) { if(lf[i] == 'L'){ arr[i] = i; rev[i] = n - i - 1; } else{ arr[i] = n - i - 1; rev[i] = i; } sum += arr[i]; } ArrayList<Long> ans = new ArrayList<>(); // System.out.println(sum); for (int i = 0; i < n; i++) { if(arr[i] < rev[i]){ ans.add((long)rev[i] - arr[i]); } } Collections.sort(ans); for (int i = ans.size() - 1; i >= 0; i--) { out.append(sum += ans.get(i)).append(' '); } for (int i = 0; i < n - ans.size(); i++) { out.append(sum).append(' '); } out.append('\n'); } System.out.println(out); } static boolean v[][]; static int solve(int cuX, int cuY, int sx, int sy, int n, int m, int d, int count) { if (cuX == n && cuY == m) { return count; } v[cuX][cuY] = true; int c = 0; for (int i = 0; i < 4; i++) { int nextX = cuX + dx[i]; int nextY = cuY + dy[i]; if (nextX > n || nextY > m || nextX < 1 || nextY < 1 || v[nextX][nextY]) continue; if (Math.abs(nextY - sy) + Math.abs(nextX - sx) <= d) continue; c++; return solve(nextX, nextY, sx, sy, n, m, d, count + 1); } if (c == 0) { return -1; } return -1; } /* 3 2 3 1 3 0 2 3 1 3 1 5 5 3 4 2 */ static int lcm(int a, int b) { return a / gcd(a, b) * b; } static int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } //all primes static ArrayList<Integer> primes; static boolean[] primesB; //sieve algorithm static void sieve(int n) { primes = new ArrayList<>(); primesB = new boolean[n + 1]; for (int i = 2; i <= n; i++) { primesB[i] = true; } for (int i = 2; i * i <= n; i++) { if (primesB[i]) { for (int j = i * i; j <= n; j += i) { primesB[j] = false; } } } for (int i = 0; i <= n; i++) { if (primesB[i]) { primes.add(i); } } } // Function to find gcd of array of // numbers static int findGCD(int[] arr, int n) { int result = arr[0]; for (int element : arr) { result = gcd(result, element); if (result == 1) { return 1; } } return result; } private static int anInt(String s) { return Integer.parseInt(s); } } class Pair<K, V> implements Comparable<Pair<K, V>> { K first; V second; Pair(K f, V s) { first = f; second = s; } @Override public int compareTo(Pair<K, V> o) { return 0; } } class PairInt implements Comparable<PairInt> { int first; int second; PairInt(int f, int s) { first = f; second = s; } @Override public int compareTo(PairInt o) { if (this.first > o.first) { return 1; } else if (this.first < o.first) return -1; else { if (this.second < o.second) return 1; else if (this.second == o.second) return -1; return 0; } } @Override public String toString() { return "<" + first + ", " + second + ">"; } } /* 5 1 5 4 3 2 1 1 4 5 3 1 2 3 5 4 1 3 4 5 3 5 5 3 1 2 2 2 1 1 4 2 5 1 5 5 2 1 1 1 2 1 2 3 4 5 10 1 3 1 5 2 3 5 7 4 3 5 1 1 1 1 2 1 4 2 4 1 2 4 1 3 1 3 3 3 3 2 5 4 1 5 3 2 1 1 5 1 2 3 3 5 3 3 6 8 4 */
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
4d466e11d498c9621ed489b6cbbb1282
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 t = sc.nextInt(); while (t > 0) { t -= 1; int n = sc.nextInt(); String s = sc.next(); long sum = 0; for (int i = 0; i < n; i += 1) { if (s.charAt(i) == 'L') { sum += i; } else { sum += n - i - 1; } } int l = 0; int r = n - 1; long[] ans = new long[n]; int count = 0; if (n % 2 == 0) { int lm = n / 2 - 1; int rm = n / 2; while (l <= lm && r >= rm) { while (l <= lm && s.charAt(l) == 'R') { l += 1; } while (r >= rm && s.charAt(r) == 'L') { r -= 1; } if (l > lm || r < rm) { break; } int a = n - l - 1 - l; int b = r - (n - r - 1); if (a >= b) { while (l <= lm && s.charAt(l) == 'R') { l += 1; } ans[count] = sum + a; sum += a; l += 1; } else { while (r >= rm && s.charAt(r) == 'L') { r -= 1; } r -= 1; ans[count] = sum + b; sum += b; } count += 1; } while (r >= rm) { while (r >= rm && s.charAt(r) == 'L') { r -= 1; } if (r < rm) { break; } int b = r - (n - r - 1); while (r >= rm && s.charAt(r) == 'L') { r -= 1; } r -= 1; ans[count] = sum + b; sum += b; count += 1; } while (l <= lm) { while (l <= lm && s.charAt(l) == 'R') { l += 1; } if (l > lm) { break; } int a = n - l - 1 - l; while (l <= lm && s.charAt(l) == 'R') { l += 1; } l += 1; ans[count] = sum + a; sum += a; count += 1; } for (int i = count; i < n; i += 1) { ans[i] = sum; } } else { int lm = (n - 1) / 2 - 1; int rm = lm + 2; while (l <= lm && r >= rm) { while (l <= lm && s.charAt(l) == 'R') { l += 1; } while (r >= rm && s.charAt(r) == 'L') { r -= 1; } if (l > lm || r < rm) { break; } int a = n - l - 1 - l; int b = r - (n - r - 1); if (a >= b) { while (l <= lm && s.charAt(l) == 'R') { l += 1; } ans[count] = sum + a; sum += a; l += 1; } else { while (r >= rm && s.charAt(r) == 'L') { r -= 1; } r -= 1; ans[count] = sum + b; sum += b; } count += 1; } while (r >= rm) { while (r >= rm && s.charAt(r) == 'L') { r -= 1; } if (r < rm) { break; } int b = r - (n - r - 1); while (r >= rm && s.charAt(r) == 'L') { r -= 1; } r -= 1; ans[count] = sum + b; sum += b; count += 1; } while (l <= lm) { while (l <= lm && s.charAt(l) == 'R') { l += 1; } if (l > lm) { break; } int a = n - l - 1 - l; while (l <= lm && s.charAt(l) == 'R') { l += 1; } l += 1; ans[count] = sum + a; sum += a; count += 1; } for (int i = count; i < n; i += 1) { ans[i] = sum; } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i += 1) { if (i < n - 1) { sb.append(ans[i]); sb.append(' '); } else { sb.append(ans[i]); } } 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 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
3146845d79dc2c26b0870df7963cb2d1
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 final 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 str=sc.next(); char ch[]=str.toCharArray(); int arr[]=new int[n]; int arr2[]=new int[n]; long s=0; for(int i=0;i<n;i++) { if(ch[i]=='L') { arr[i]=i; arr2[i]=n-1-i; } else { arr[i]=n-1-i; arr2[i]=i; } s+=(long)arr[i]; } int i=0;int j=n-1; ArrayList<Long>l=new ArrayList<>();int k=n; int flag=0; while(i<=j) { if(flag==0) { int val=arr[i]; if(val<arr2[i]) { k--; s+=(long)(arr2[i]-val); l.add(s); } i++; flag=1; } else { int val=arr[j]; if(val<arr2[j]) { k--; s+=(long)(arr2[j]-val); l.add(s); } j--; flag=0; } } while(k>0) { l.add(s); k--; } for(long each: l) System.out.print(each+" "); 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
2488a5db0aec1a042c07c92770ca5007
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 c321 { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); int t=Integer.parseInt(br.readLine()); while(t-->0) { long n=Long.parseLong(br.readLine()); String s = br.readLine(); ArrayList<Long> al = new ArrayList<>(); long ans = 0; for(int i=0;i<n;i++) { char ch = s.charAt(i); ans += (ch == 'L' ? i : n-i-1); if(i < n/2 && ch == 'L') al.add(n-i-1-i); else if(i >= n/2 && ch == 'R') al.add((long)i-(n-i-1)); } // System.out.println(al+" hello "+ans); Collections.sort(al, Collections.reverseOrder()); int pos = 0; for(long i:al) { pos++; ans += i; sb.append(ans).append(" "); } for(int i=pos;i<n;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 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
cb8f8da4e94d54230625530d52d99c04
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 Line { public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 1; i <= t; i++) { int n = in.nextInt(); String s = in.next(); char[] sArr = s.toCharArray(); long currentScore = 0; for (int j = 0; j < n; j++) { if (sArr[j] == 'L') { currentScore += j; } else if (sArr[j] == 'R') { currentScore += n - 1 - j; } } int k = 0; for (int j = 0; j < n / 2; j++) { if (sArr[j] == 'L') { currentScore += n - 1 - 2 * j; k++; out.print(currentScore); out.print(" "); } if (sArr[n - 1 - j] == 'R') { currentScore += n - 1 - 2 * j; k++; out.print(currentScore); out.print(" "); } } for (int j = k + 1; j <= n; j++) { out.print(currentScore); out.print(" "); } out.print("\n"); } out.close(); } 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) { // noop } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } 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 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
2f296288729beed6106b4a5fc2433141
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 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws Exception { int t = Integer.parseInt(br.readLine()); while(t-->0){ int n = Integer.parseInt(br.readLine()); char c[] = br.readLine().toCharArray(); PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); long sum = 0; for(int i = 0; i<n; i++){ if(c[i] == 'L'){ sum += i; if(i < n - i - 1) pq.add(n - i - 1 - i); }else if(c[i] == 'R'){ // R sum += n - i - 1; if(i > n - i - 1) pq.add(i - (n - i - 1)); } } // 판단 for(int i = 0; i<n; i++){ if(!pq.isEmpty()) sum += pq.poll(); 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 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
716bc390770464beaa30d615e54d2b19
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 { static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); static int dx[]={0,0,-1,1},dy[]={-1,1,0,0}; static final double pi=3.1415926536; static long mod=1000000007; // static long mod=998244353; static int MAX=Integer.MAX_VALUE; static int MIN=Integer.MIN_VALUE; static long MAXL=Long.MAX_VALUE; static long MINL=Long.MIN_VALUE; static ArrayList<pair> graph[]; static long fact[]; static long seg[]; static int dp[][]; // static long dp[][]; public static void main (String[] args) throws java.lang.Exception { // code goes here int t=I(); outer:while(t-->0) { int n=I(); char c[]=S().toCharArray(); long a[]=new long[n]; for(int i=0;i<n;i++){ if(c[i]=='L'){ a[i]=i; }else{ a[i]=n-i-1; } } pair ch[]=new pair[n]; int mid=n/2; for(int i=0;i<n;i++){ if(i<mid){ ch[i]=new pair(n-i-1,a[i]); }else{ ch[i]=new pair(i,a[i]); } } Arrays.sort(ch,new myComp()); pair pre[]=new pair[n]; pre[0]=new pair(ch[0].a,ch[0].b); for(int i=1;i<n;i++){ pre[i]=new pair(0,0); pre[i].a=pre[i-1].a+ch[i].a; pre[i].b=pre[i-1].b+ch[i].b; } long ans[]=new long[n]; for(int i=0;i<n;i++){ ans[i]=pre[n-1].b-pre[i].b+pre[i].a; } printArray(ans); } out.close(); } public static class pair { long a; long b; public pair(long aa,long bb) { a=aa; b=bb; } } public static class myComp implements Comparator<pair> { public int compare(pair p1,pair p2) { if(p1.a-p1.b==p2.a-p2.b)return 0; if(p1.a-p1.b>p2.a-p2.b)return -1; return 1; } //sort in ascending order. // public int compare(pair p1,pair p2) // { // if(p1.a==p2.a) // return 0; // else if(p1.a<p2.a) // return -1; // else // return 1; // } // sort in descending order. // public int compare(pair p1,pair p2) // { // if(p1.b==p2.b) // return 0; // else if(p1.b<p2.b) // return 1; // else // return -1; // } } // public static void setGraph(int n,int m)throws IOException // { // graph=new ArrayList[n+1]; // for(int i=0;i<=n;i++){ // graph[i]=new ArrayList<>(); // } // for(int i=0;i<m;i++){ // int u=I(),v=I(); // graph[u].add(v); // graph[v].add(u); // } // } //LOWER_BOUND and UPPER_BOUND functions //It returns answer according to zero based indexing. public static int lower_bound(pair[] arr,int X,int start, int end) //start=0,end=n-1 { if(start>end)return -1; // if(arr.get(end)<X)return end; if(arr[end].a<X)return end; // if(arr.get(start)>X)return -1; if(arr[start].a>X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; if(arr[mid].a==X){ // if(arr.get(mid)==X){ //Returns last index of lower bound value. if(mid<end && arr[mid+1].a==X){ // if(mid<end && arr.get(mid+1)==X){ left=mid+1; }else{ return mid; } } // if(arr.get(mid)==X){ //Returns first index of lower bound value. // if(arr[mid]==X){ // // if(mid>start && arr.get(mid-1)==X){ // if(mid>start && arr[mid-1]==X){ // right=mid-1; // }else{ // return mid; // } // } else if(arr[mid].a>X){ // else if(arr.get(mid)>X){ if(mid>start && arr[mid-1].a<X){ // if(mid>start && arr.get(mid-1)<X){ return mid-1; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1].a>X){ // if(mid<end && arr.get(mid+1)>X){ return mid; }else{ left=mid+1; } } } return left; } //It returns answer according to zero based indexing. public static int upper_bound(long arr[],long X,int start,int end) //start=0,end=n-1 { if(arr[0]>=X)return start; if(arr[arr.length-1]<X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; if(arr[mid]==X){ //returns first index of upper bound value. if(mid>start && arr[mid-1]==X){ right=mid-1; }else{ return mid; } } // if(arr[mid]==X){ //returns last index of upper bound value. // if(mid<end && arr[mid+1]==X){ // left=mid+1; // }else{ // return mid; // } // } else if(arr[mid]>X){ if(mid>start && arr[mid-1]<X){ return mid; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1]>X){ return mid+1; }else{ left=mid+1; } } } return left; } //END //Segment Tree Code public static void buildTree(long a[],int si,int ss,int se) { if(ss==se){ seg[si]=a[ss]; return; } int mid=(ss+se)/2; buildTree(a,2*si+1,ss,mid); buildTree(a,2*si+2,mid+1,se); seg[si]=max(seg[2*si+1],seg[2*si+2]); } // public static void update(int si,int ss,int se,int pos,int val) // { // if(ss==se){ // // seg[si]=val; // return; // } // int mid=(ss+se)/2; // if(pos<=mid){ // update(2*si+1,ss,mid,pos,val); // }else{ // update(2*si+2,mid+1,se,pos,val); // } // // seg[si]=min(seg[2*si+1],seg[2*si+2]); // if(seg[2*si+1].a < seg[2*si+2].a){ // seg[si].a=seg[2*si+1].a; // seg[si].b=seg[2*si+1].b; // }else{ // seg[si].a=seg[2*si+2].a; // seg[si].b=seg[2*si+2].b; // } // } public static long query1(int si,int ss,int se,int qs,int qe) { if(qs>se || qe<ss)return 0; if(ss>=qs && se<=qe)return seg[si]; int mid=(ss+se)/2; long p1=query1(2*si+1,ss,mid,qs,qe); long p2=query1(2*si+2,mid+1,se,qs,qe); return max(p1,p2); } public static void merge(ArrayList<Integer> f,ArrayList<Integer> a,ArrayList<Integer> b) { int i=0,j=0; while(i<a.size() && j<b.size()){ if(a.get(i)<=b.get(j)){ f.add(a.get(i)); i++; }else{ f.add(b.get(j)); j++; } } while(i<a.size()){ f.add(a.get(i)); i++; } while(j<b.size()){ f.add(b.get(j)); j++; } } //Segment Tree Code end //Prefix Function of KMP Algorithm public static int[] KMP(char c[],int n) { int pi[]=new int[n]; for(int i=1;i<n;i++){ int j=pi[i-1]; while(j>0 && c[i]!=c[j]){ j=pi[j-1]; } if(c[i]==c[j])j++; pi[i]=j; } return pi; } public static long kadane(long a[],int n) //largest sum subarray { long max_sum=Long.MIN_VALUE,max_end=0; for(int i=0;i<n;i++){ max_end+=a[i]; if(max_sum<max_end){max_sum=max_end;} if(max_end<0){max_end=0;} } return max_sum; } public static ArrayList<Long> primeFact(long x) { ArrayList<Long> arr=new ArrayList<>(); if(x%2==0){ arr.add(2L); while(x%2==0){ x/=2; } } for(long i=3;i*i<=x;i+=2){ if(x%i==0){ arr.add(i); while(x%i==0){ x/=i; } } } if(x>0){ arr.add(x); } return arr; } public static long nPr(int n,int r) { long ans=divide(fact(n),fact(n-r),mod); return ans; } public static long nCr(int n,int r) { long ans=divide(fact[n],mul(fact[n-r],fact[r]),mod); return ans; } public static boolean isSorted(int a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static boolean isSorted(long a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static int computeXOR(int n) //compute XOR of all numbers between 1 to n. { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } public static int np2(int x) { x--; x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x++; return x; } public static int hp2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static long hp2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static ArrayList<Integer> primeSieve(int n) { ArrayList<Integer> arr=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; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } // Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n); public static class FenwickTree { int farr[]; int n; public FenwickTree(int c) { n=c+1; farr=new int[n]; } // public void update_range(int l,int r,long p) // { // update(l,p); // update(r+1,(-1)*p); // } public void update(int x,int p) { for(;x<n;x+=x&(-x)) { farr[x]+=p; } } public int get(int x) { int ans=0; for(;x>0;x-=x&(-x)) { ans=ans+farr[x]; } return ans; } } //Disjoint Set Union //NOTE: call find function for all the index in the par array at last, //in order to set parent of every index properly. public static class DSU { int par[],rank[]; public DSU(int c) { par=new int[c+1]; rank=new int[c+1]; for(int i=0;i<=c;i++) { par[i]=i; rank[i]=0; } } public int find(int a) { if(a==par[a]) return a; return par[a]=find(par[a]); } public void union(int a,int b) { int a_rep=find(a),b_rep=find(b); if(a_rep==b_rep) return; if(rank[a_rep]<rank[b_rep]) par[a_rep]=b_rep; else if(rank[a_rep]>rank[b_rep]) par[b_rep]=a_rep; else { par[b_rep]=a_rep; rank[a_rep]++; } } } public static boolean isVowel(char c) { if(c=='a' || c=='e' || c=='i' || c=='u' || c=='o')return true; return false; } public static boolean isInteger(double N) { int X = (int)N; double temp2 = N - X; if (temp2 > 0) { return false; } return true; } public static boolean isPalindrome(String s) { int n=s.length(); for(int i=0;i<=n/2;i++){ if(s.charAt(i)!=s.charAt(n-i-1)){ return false; } } return true; } public static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static long fact(long n) { long fact=1; for(long i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static long fact(int n) { long fact=1; for(int i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } 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; 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; } public static boolean isPrime(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; } public static void printArray(long a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(int a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(char a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]); } out.println(); } public static void printArray(String a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(boolean a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(pair a[]) { for(pair p:a){ out.println(p.a+"->"+p.b); } } public static void printArray(int a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(String a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(boolean a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(long a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(char a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(ArrayList<?> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printMapI(HashMap<?,?> hm){ for(Map.Entry<?,?> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } public static void printMap(HashMap<Long,ArrayList<Integer>> hm){ for(Map.Entry<Long,ArrayList<Integer>> e:hm.entrySet()){ out.print(e.getKey()+"->"); ArrayList<Integer> arr=e.getValue(); for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); }out.println(); } } public static void printGraph(ArrayList<Integer> graph[]) { int n=graph.length; for(int i=0;i<n;i++){ out.print(i+"->"); for(int j:graph[i]){ out.print(j+" "); }out.println(); } } //Modular Arithmetic public static long add(long a,long b) { a+=b; if(a>=mod)a-=mod; return a; } public static long sub(long a,long b) { a-=b; if(a<0)a+=mod; return a; } public static long mul(long a,long b) { return ((a%mod)*(b%mod))%mod; } public static long divide(long a,long b,long m) { a=mul(a,modInverse(b,m)); return a; } public static long modInverse(long a,long m) { int x=0,y=0; own p=new own(x,y); long g=gcdExt(a,m,p); if(g!=1){ out.println("inverse does not exists"); return -1; }else{ long res=((p.a%m)+m)%m; return res; } } public static long gcdExt(long a,long b,own p) { if(b==0){ p.a=1; p.b=0; return a; } int x1=0,y1=0; own p1=new own(x1,y1); long gcd=gcdExt(b,a%b,p1); p.b=p1.a - (a/b) * p1.b; p.a=p1.b; return gcd; } public static long pwr(long m,long n) { long res=1; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m); } n=n>>1; m=(m*m); } return res; } public static long modpwr(long m,long n) { long res=1; m=m%mod; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m)%mod; } n=n>>1; m=(m*m)%mod; } return res; } public static class own { long a; long b; public own(long val,long index) { a=val; b=index; } } //Modular Airthmetic public 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); } public static void sort(char[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { char tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public 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); } //max & min public static int max(int a,int b){return Math.max(a,b);} public static int min(int a,int b){return Math.min(a,b);} public static int max(int a,int b,int c){return Math.max(a,Math.max(b,c));} public static int min(int a,int b,int c){return Math.min(a,Math.min(b,c));} public static long max(long a,long b){return Math.max(a,b);} public static long min(long a,long b){return Math.min(a,b);} public static long max(long a,long b,long c){return Math.max(a,Math.max(b,c));} public static long min(long a,long b,long c){return Math.min(a,Math.min(b,c));} public static int maxinA(int a[]){int n=a.length;int mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static long maxinA(long a[]){int n=a.length;long mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static int mininA(int a[]){int n=a.length;int mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long mininA(long a[]){int n=a.length;long mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long suminA(int a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} public static long suminA(long a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} //end public static int[] I(int n)throws IOException{int a[]=new int[n];for(int i=0;i<n;i++){a[i]=I();}return a;} public static long[] IL(int n)throws IOException{long a[]=new long[n];for(int i=0;i<n;i++){a[i]=L();}return a;} public static long[] prefix(int a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;} public static long[] prefix(long a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;} public static int I()throws IOException{return sc.I();} public static long L()throws IOException{return sc.L();} public static String S()throws IOException{return sc.S();} public static double D()throws IOException{return sc.D();} } 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 I(){ return Integer.parseInt(next());} long L(){ return Long.parseLong(next());} double D(){return Double.parseDouble(next());} String S(){ 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
ea21dec70b5fe888f048646746b09be0
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.List; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); for (int tNum = 1; tNum <= t; tNum++) { int N = Integer.parseInt(br.readLine()); int halfIdx = N / 2; long initTotal = 0; String str = br.readLine(); List<Integer> idx = new ArrayList<>(); for (int i = 0; i < N; i++) { if (str.charAt(i) == 'L') { initTotal += i; if (i < halfIdx) { idx.add(N - i - 1 - i); } } else { initTotal += N - i - 1; if (i >= halfIdx) { idx.add(i - N + i + 1); } } } idx.sort((a, b) -> b - a); for(int i = 0; i < N; i++) { if (i < idx.size()) { initTotal += idx.get(i); } sb.append(initTotal).append(" "); } sb.append("\n"); } System.out.print(sb.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 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