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
|
c2c10f2283ab8e340154ee5cd96418d6
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class C {
static IOHandler sc = new IOHandler();
static int base = 998244353;
static long [] arr;
static long [] factorial;
public static void main(String[] args) {
// TODO Auto-generated method stub
int testCases = sc.nextInt();
for (int i = 1; i <= testCases; ++i) {
solve(i);
}
}
private static void solve(int t) {
int n = sc.nextInt();
arr = new long [3];
arr[2] = 1;
factorial = new long [100];
factorial[0] = 1;
for (int i = 1; i < factorial.length; ++i) {
factorial[i] = i * factorial[i - 1];
factorial[i] %= base;
}
long [] arr = solve2(n);
arr[2] = 1;
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
}
private static long [] solve2(int n) {
if (n == 0) return new long [3];
int half = n / 2 - 1;
int nPrev = n - 1;
long [] arr = new long [3];
long val = factorial[nPrev];
val *= findInverse(factorial[half]);
val %= base;
val *= findInverse(factorial[nPrev - half]);
val %= base;
arr[0] += val;
arr[0] %= base;
if (n > 2 ) {
nPrev = n - 2;
half = n / 2 - 2;
val = factorial[nPrev];
val *= findInverse(factorial[half]);
val %= base;
val *= findInverse(factorial[nPrev - half]);
val %= base;
arr[1] += val;
arr[1] %= base;
}
long [] next = solve2(n - 2);
arr[1] += next[0];
arr[1] %= base;
arr[0] += next[1];
arr[0] %= base;
//System.out.println(Arrays.toString(arr));
return arr;
}
private static long findInverse(long num){
if (num == 1)
return 1;
return ( base + inverse(num, base%num, 1, -base/num, 0, 1) ) % base;
}
private static long inverse(long l, long r, long nL, long nR, long prvL, long prvR){
if (r == 1)
return nR;
long pL = nL;
long pR = nR;
long first = 1;
long second = -l/r;
nL *= second;
nL += prvL;
nL %= base;
nR *= second;
nR += prvR;
nR %= base;
return inverse (r, l % r, nL, nR, pL, pR);
}
private static class IOHandler {
BufferedReader br;
StringTokenizer st;
public IOHandler() {
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 [] readArray(int n) {
int [] res = new int [n];
for (int i = 0; i < n; ++i)
res[i] = nextInt();
return res;
}
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
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
72b932f7883fbf7b00c3a8e28239f8af
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static Scanner sc;
static PrintWriter out;
public static void main(String[] args) {
sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int t = 1;
init(100);
if (true) {
t = sc.nextInt();
}
for(int i=0; i<t; i++) {
new Main().solve();
}
out.flush();
}
public void solve() {
int n = sc.nextInt()/2;
long[][] dp = new long[n+1][3];
dp[1][0] = 1;
dp[1][1] = 0;
dp[1][2] = 1;
for(int i=2; i<=n; i++) {
dp[i][0] = conv(i*2-1, i-1) + dp[i-1][1];
dp[i][1] = conv(i*2-2, i) + dp[i-1][0];
dp[i][2] = dp[i-1][2];
for(int j=0; j<3; j++) {
dp[i][j] %= mod;
}
}
out.printf("%d %d %d\n", dp[n][0], dp[n][1], dp[n][2]);
}
private static long mod = 998244353 ;
static long[] fac;
static long[] finv;
static long[] inv;
static void init(int max) {
fac = new long[max];
finv = new long[max];
inv = new long[max];
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for(int i=2; i<max; i++) {
fac[i] = fac[i-1] * i % mod;
inv[i] = mod - inv[(int)mod%i] * (mod / i) % mod;
finv[i] = finv[i-1] * inv[i] % mod;
}
}
static long conv(int n, int k) {
if(n<k || n<0 || k<0) return 0;
return fac[n] * (finv[k]*finv[n-k]%mod) % mod;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
97d659c0012af90e4203cca7ffaace45
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author dauom
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CCardGame solver = new CCardGame();
solver.solve(1, in, out);
out.close();
}
static class CCardGame {
public final void solve(int testNumber, InputReader in, PrintWriter out) {
final ModArithmetic mod = new ModArithmetic(998244353, true);
int[][] fif = mod.getFactorialAndInverse(61);
int[] wins = new int[61];
int[] losses = new int[61];
for (int i = 2; i <= 60; i += 2) {
int total = mod.C(fif, i, i / 2);
int a = mod.add(mod.C(fif, i - 1, i / 2 - 1), losses[i - 2]);
int b = mod.sub(total, mod.add(a, 1));
wins[i] = a;
losses[i] = b;
}
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
out.println(wins[n] + " " + losses[n] + " 1");
}
}
}
static final class ModArithmetic {
public static final int DEFAULT_MOD = 1_000_000_007;
public final int m;
public final boolean isPrime;
public ModArithmetic() {
this(DEFAULT_MOD, true);
}
public ModArithmetic(int mod) {
this(mod, false);
}
public ModArithmetic(int mod, boolean isPrime) {
if (mod <= 0) {
throw new IllegalArgumentException("Modulo must be > 0");
}
this.m = mod;
this.isPrime = isPrime;
}
public final int add(int a, int b) {
a += b;
if (a >= m) {
a -= m;
}
return a;
}
public final int sub(int a, int b) {
a -= b;
if (a < 0) {
a += m;
}
return a;
}
public final int mul(int a, int b) {
long m = a * (long) b;
if (m >= this.m) {
m %= this.m;
}
return (int) m;
}
public final int div(int a, int b) {
if (isPrime) {
return mul(a, pow(b, m - 2));
}
throw new UnsupportedOperationException();
}
public final int pow(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1) == 1) {
res *= a;
if (res >= m) {
res %= m;
}
}
b >>= 1;
a *= a;
if (a >= m) {
a %= m;
}
}
return (int) res;
}
public final int[][] getFactorialAndInverse(int n) {
int[] f = new int[n + 1];
int[] fi = new int[n + 1];
f[0] = 1;
for (int i = 1; i <= n; i++) {
f[i] = mul(f[i - 1], i);
}
fi[n] = div(1, f[n]);
for (int i = n - 1; i >= 0; i--) {
fi[i] = mul(fi[i + 1], i + 1);
}
return new int[][]{f, fi};
}
public final int C(final int[][] fif, final int n, final int k) {
if (n < 0 || k < 0 || n < k) {
return 0;
}
return mul(fif[0][n], mul(fif[1][k], fif[1][n - k]));
}
public String toString() {
return "ModArithmetic{" + "m=" + m + ", isPrime=" + isPrime + '}';
}
}
static final class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1 << 18];
private int curChar;
private int numChars;
public InputReader() {
this.stream = System.in;
}
public InputReader(final InputStream stream) {
this.stream = stream;
}
private int read() {
if (this.numChars == -1) {
throw new UnknownError();
} else {
if (this.curChar >= this.numChars) {
this.curChar = 0;
try {
this.numChars = this.stream.read(this.buf);
} catch (IOException ex) {
throw new InputMismatchException();
}
if (this.numChars <= 0) {
return -1;
}
}
return this.buf[this.curChar++];
}
}
public final int nextInt() {
int c;
for (c = this.read(); isSpaceChar(c); c = this.read()) {
}
byte sgn = 1;
if (c == 45) { // 45 == '-'
sgn = -1;
c = this.read();
}
int res = 0;
while (c >= 48 && c <= 57) { // 48 == '0', 57 == '9'
res *= 10;
res += c - 48; // 48 == '0'
c = this.read();
if (isSpaceChar(c)) {
return res * sgn;
}
}
throw new InputMismatchException();
}
private static boolean isSpaceChar(final int c) {
return c == 32 || c == 10 || c == 13 || c == 9 || c == -1; // 32 == ' ', 10 == '\n', 13 == '\r', 9 == '\t'
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
1ceff8e4d03d0048dab0c94e93fea551
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
//Utilities
import java.io.*;
import java.util.*;
public class a {
static int t;
static int n;
static ComboMod c;
static final int MOD = 998244353;
public static void main(String[] args) throws IOException {
c = new ComboMod(100, MOD);
t = in.iscan();
outer : while (t-- > 0) {
n = in.iscan();
long total = c.ncr(n, n/2);
long win, lose, draw;
draw = 1;
win = 0;
int cur = n, cardsLeft = n/2;
boolean alexTurn = true;
while (cardsLeft-1 >= 0) {
if (alexTurn) {
win = (win + c.ncr(cur-1, cardsLeft-1)) % MOD;
}
else {
if (cardsLeft - 2 >= 0) {
win = (win + c.ncr(cur-2, cardsLeft-2)) % MOD;
}
}
cardsLeft--;
cur -= 2;
alexTurn = !alexTurn;
}
lose = ((total - win - draw) % MOD + MOD) % MOD;
out.println(win + " " + lose + " " + draw);
}
out.close();
}
static class ComboMod {
int n, MOD;
long[] fac, facinv, inv;
ComboMod(int n, int MOD) {
this.n = n; this.MOD = MOD;
fac = new long[n+1]; facinv = new long[n+1]; inv = new long[n+1];
fac[0] = facinv[0] = 1; inv[1] = 1;
for (int i = 2; i <= n; i++) {
inv[i] = (inv[MOD % i] * (-MOD / i) % MOD + MOD) % MOD;
}
for(int i = 1; i <= n; i++){
fac[i] = fac[i-1] * i % MOD;
facinv[i] = facinv[i-1] * inv[i] % MOD;
}
}
long fac(int n) { return fac[n]; }
long facinv(int n) { return facinv[n]; }
long inv(int n) { return inv[n]; }
long ncr(int n, int k) { return n < k || k < 0 ? 0 : fac[n] * facinv[k] % MOD * facinv[n-k] % MOD; }
}
static INPUT in = new INPUT(System.in);
static PrintWriter out = new PrintWriter(System.out);
private static class INPUT {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public INPUT (InputStream stream) {
this.stream = stream;
}
public INPUT (String file) throws IOException {
this.stream = new FileInputStream (file);
}
public int cscan () throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read (buf);
}
if (numChars == -1)
return numChars;
return buf[curChar++];
}
public int iscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
int res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public String sscan () throws IOException {
int c = cscan ();
while (space (c))
c = cscan ();
StringBuilder res = new StringBuilder ();
do {
res.appendCodePoint (c);
c = cscan ();
}
while (!space (c));
return res.toString ();
}
public double dscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
double res = 0;
while (!space (c) && c != '.') {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
res *= 10;
res += c - '0';
c = cscan ();
}
if (c == '.') {
c = cscan ();
double m = 1;
while (!space (c)) {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
m /= 10;
res += (c - '0') * m;
c = cscan ();
}
}
return res * sgn;
}
public long lscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
long res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public boolean space (int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
public static class UTILITIES {
static final double EPS = 10e-6;
public static void sort(int[] a, boolean increasing) {
ArrayList<Integer> arr = new ArrayList<Integer>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(long[] a, boolean increasing) {
ArrayList<Long> arr = new ArrayList<Long>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(double[] a, boolean increasing) {
ArrayList<Double> arr = new ArrayList<Double>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static int lower_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int upper_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static void updateMap(HashMap<Integer, Integer> map, int key, int v) {
if (!map.containsKey(key)) {
map.put(key, v);
}
else {
map.put(key, map.get(key) + v);
}
if (map.get(key) == 0) {
map.remove(key);
}
}
public static long gcd (long a, long b) {
return b == 0 ? a : gcd (b, a % b);
}
public static long lcm (long a, long b) {
return a * b / gcd (a, b);
}
public static long fast_pow_mod (long b, long x, int mod) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod;
return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod;
}
public static long fast_pow (long b, long x) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow (b * b, x / 2);
return b * fast_pow (b * b, x / 2);
}
public static long choose (long n, long k) {
k = Math.min (k, n - k);
long val = 1;
for (int i = 0; i < k; ++i)
val = val * (n - i) / (i + 1);
return val;
}
public static long permute (int n, int k) {
if (n < k) return 0;
long val = 1;
for (int i = 0; i < k; ++i)
val = (val * (n - i));
return val;
}
// start of permutation and lower/upper bound template
public static void nextPermutation(int[] nums) {
//find first decreasing digit
int mark = -1;
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break;
}
}
if (mark == -1) {
reverse(nums, 0, nums.length - 1);
return;
}
int idx = nums.length-1;
for (int i = nums.length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1, nums.length - 1);
}
public static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
public static void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
static int lower_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= cmp) high = mid;
else low = mid + 1;
}
return low;
}
static int upper_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > cmp) high = mid;
else low = mid + 1;
}
return low;
}
// end of permutation and lower/upper bound template
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
cc292d2e6c6fee77c406f0779e73dfa9
|
train_109.jsonl
|
1664462100
|
Consider a hallway, which can be represented as the matrix with $$$2$$$ rows and $$$n$$$ columns. Let's denote the cell on the intersection of the $$$i$$$-th row and the $$$j$$$-th column as $$$(i, j)$$$. The distance between the cells $$$(i_1, j_1)$$$ and $$$(i_2, j_2)$$$ is $$$|i_1 - i_2| + |j_1 - j_2|$$$.There is a cleaning robot in the cell $$$(1, 1)$$$. Some cells of the hallway are clean, other cells are dirty (the cell with the robot is clean). You want to clean the hallway, so you are going to launch the robot to do this.After the robot is launched, it works as follows. While at least one cell is dirty, the robot chooses the closest (to its current cell) cell among those which are dirty, moves there and cleans it (so the cell is no longer dirty). After cleaning a cell, the robot again finds the closest dirty cell to its current cell, and so on. This process repeats until the whole hallway is clean.However, there is a critical bug in the robot's program. If at some moment, there are multiple closest (to the robot's current position) dirty cells, the robot malfunctions.You want to clean the hallway in such a way that the robot doesn't malfunction. Before launching the robot, you can clean some (possibly zero) of the dirty cells yourself. However, you don't want to do too much dirty work yourself while you have this nice, smart (yet buggy) robot to do this. Note that you cannot make a clean cell dirty.Calculate the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
512 megabytes
|
import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class RoundEdu136E {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
// the code for the deep recursion (more than 100k or 3~400k or so)
// Thread t = new Thread(null, new RoundEdu130F(), "threadName", 1<<28);
// t.start();
// t.join();
RoundEdu136E sol = new RoundEdu136E();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
if(System.getProperty("ONLINE_JUDGE") != null)
hasMultipleTests = false;
initIO(isFileIO);
//test();
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
if(isDebug){
out.printf("Test %d\n", i);
}
getInput();
solve();
printOutput();
}
in.close();
out.close();
}
// use suitable one between matrix(2, n), matrix(n, 2), transposedMatrix(2, n) for graph edges, pairs, ...
int n;
int[][] mat;
final int CLEAN = 0;
final int DIRTY = 1;
void getInput() {
n = in.nextInt();
mat = new int[2][n+1];
String s1 = in.next();
String s2 = in.next();
for(int i=0; i<n; i++) {
mat[0][i] = s1.charAt(i)-'0';
mat[1][i] = s2.charAt(i)-'0';
}
}
void printOutput() {
out.printlnAns(ans);
}
int ans;
void solve(){
// notice that robot never goes to left
// dp[i][j][false]
// = how many dirty cells can remain after (i, j)
// if robot is at (i, j) and everything after (i, j) is untouched yet
// dp[i][j][true]
// no need to visit (i^1, j), and don't check the cell (i^1, j+1) (either cleaned manually or automatically clean)
// implementation done in 20 mins, but WA
// probably the issue is that we don't necessarily go right in this way
int[][] dp1 = new int[2][n+2];
int[][] dp2 = new int[2][n+2];
for(int j=n-1; j>=0; j--) {
for(int i=0; i<2; i++) {
dp2[i][j] = mat[i][j] + mat[i][j+1] + dp1[i][j+2];
}
for(int i=0; i<2; i++) {
dp1[i][j] = mat[i][j];
if(mat[i^1][j] == DIRTY && mat[i][j+1] == CLEAN)
dp1[i][j] += dp2[i^1][j];
else if(mat[i^1][j] == DIRTY && mat[i][j+1] == DIRTY)
dp1[i][j] += Math.max(dp2[i^1][j], dp1[i][j+1]);
else
dp1[i][j] += dp1[i][j+1];
dp1[i][j] = Math.max(dp1[i][j], dp2[i][j]);
}
}
ans = dp1[0][0];
}
// Optional<T> solve()
// return Optional.empty();
static class Pair implements Comparable<Pair>{
final static long FIXED_RANDOM = System.currentTimeMillis();
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int hashCode() {
// http://xorshift.di.unimi.it/splitmix64.c
long x = first;
x <<= 32;
x += second;
x += FIXED_RANDOM;
x += 0x9e3779b97f4a7c15l;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9l;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebl;
return (int)(x ^ (x >> 31));
}
@Override
public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
Pair other = (Pair) obj;
return first == other.first && second == other.second;
}
@Override
public String toString() {
return "[" + first + "," + second + "]";
}
@Override
public int compareTo(Pair o) {
int cmp = Integer.compare(first, o.first);
return cmp != 0? cmp: Integer.compare(second, o.second);
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[][] nextTransposedMatrix(int n, int m){
return nextTransposedMatrix(n, m, 0);
}
int[][] nextTransposedMatrix(int n, int m, int offset){
int[][] mat = new int[m][n];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[j][i] = nextInt()+offset;
}
}
return mat;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
String[] nextStringArray(int len) {
String[] s = new String[len];
for(int i=0; i<len; i++)
s[i] = next();
return s;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
// public <T> void printlnAns(Optional<T> ans) {
// if(ans.isEmpty())
// println(-1);
// else
// printlnAns(ans.get());
// }
public void printlnAns(OptionalInt ans) {
println(ans.orElse(-1));
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean[] ans) {
for(boolean b: ans)
printlnAns(b);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][--outDegree[u]] = v;
inNeighbors[v][--inDegree[v]] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][--degree[u]] = v;
neighbors[v][--degree[v]] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
|
Java
|
["2\n01\n11", "2\n01\n01", "4\n0101\n1011", "4\n0000\n0000", "5\n00011\n10101", "6\n011111\n111111", "10\n0101001010\n1010100110"]
|
3 seconds
|
["2", "2", "4", "0", "4", "8", "6"]
|
NoteIn the first example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 2)$$$.In the second example, you can leave the hallway as it is, so the path of the robot is $$$(1, 1) \rightarrow (1, 2) \rightarrow (2, 2)$$$.In the third example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (1, 4)$$$.In the fourth example, the hallway is already clean. Maybe you have launched the robot earlier?
|
Java 17
|
standard input
|
[
"bitmasks",
"dp"
] |
a04cfd22f90b2b87f7c5b48dfe3de873
|
The first line contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of columns in the hallway. Then two lines follow, denoting the $$$1$$$-st and the $$$2$$$-nd row of the hallway. These lines contain $$$n$$$ characters each, where 0 denotes a clean cell and 1 denotes a dirty cell. The starting cell of the robot $$$(1, 1)$$$ is clean.
| 2,400
|
Print one integer — the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
standard output
| |
PASSED
|
f2115866583fd3c57456eef23af70297
|
train_109.jsonl
|
1664462100
|
Consider a hallway, which can be represented as the matrix with $$$2$$$ rows and $$$n$$$ columns. Let's denote the cell on the intersection of the $$$i$$$-th row and the $$$j$$$-th column as $$$(i, j)$$$. The distance between the cells $$$(i_1, j_1)$$$ and $$$(i_2, j_2)$$$ is $$$|i_1 - i_2| + |j_1 - j_2|$$$.There is a cleaning robot in the cell $$$(1, 1)$$$. Some cells of the hallway are clean, other cells are dirty (the cell with the robot is clean). You want to clean the hallway, so you are going to launch the robot to do this.After the robot is launched, it works as follows. While at least one cell is dirty, the robot chooses the closest (to its current cell) cell among those which are dirty, moves there and cleans it (so the cell is no longer dirty). After cleaning a cell, the robot again finds the closest dirty cell to its current cell, and so on. This process repeats until the whole hallway is clean.However, there is a critical bug in the robot's program. If at some moment, there are multiple closest (to the robot's current position) dirty cells, the robot malfunctions.You want to clean the hallway in such a way that the robot doesn't malfunction. Before launching the robot, you can clean some (possibly zero) of the dirty cells yourself. However, you don't want to do too much dirty work yourself while you have this nice, smart (yet buggy) robot to do this. Note that you cannot make a clean cell dirty.Calculate the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class CF1739E extends PrintWriter {
CF1739E() { super(System.out, true); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1739E o = new CF1739E(); o.main(); o.flush();
}
static final int INF = 0x3f3f3f3f;
void main() {
int n = sc.nextInt();
byte[] aa = sc.next().getBytes();
byte[] bb = sc.next().getBytes();
int[][][][][][] dp = new int[n + 1][2][2][2][2][2]; // [i][a][b][c][d][e]
for (int a = 0; a <= 1; a++)
for (int d = 0; d <= 1; d++)
dp[n][a][1][1][d][0] = -INF;
for (int b = 0; b <= 1; b++)
for (int c = 0; c <= 1; c++)
dp[n][1][b][c][1][1] = -INF;
for (int i = n - 1; i >= 0; i--) {
for (int e = 0; e <= 1; e++)
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++) {
if (a == 0 && b == 0)
continue;
int x = dp[i + 1][0][0][a][b][e];
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][e]);
dp[i][0][0][a][b][e] = x;
}
if (aa[i] == '1') {
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++) {
int x = -INF;
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][0] + 1);
dp[i][1][0][a][b][0] = x;
}
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 0; b++) {
int x = -INF;
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][0] + 1);
dp[i][1][0][a][b][1] = x;
}
}
if (bb[i] == '1') {
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++) {
int x = -INF;
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][1] + 1);
dp[i][0][1][a][b][1] = x;
}
for (int a = 0; a <= 0; a++)
for (int b = 0; b <= 1; b++) {
int x = -INF;
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][1] + 1);
dp[i][0][1][a][b][0] = x;
}
}
if (aa[i] == '1' && bb[i] == '1') {
for (int a = 0; a <= 0; a++)
for (int b = 0; b <= 1; b++) {
int x = -INF;
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][1] + 2);
dp[i][1][1][a][b][0] = x;
}
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 0; b++) {
int x = -INF;
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][0] + 2);
dp[i][1][1][a][b][1] = x;
}
}
for (int a = 0; a <= 1; a++)
for (int d = 0; d <= 1; d++)
dp[i][a][1][1][d][0] = -INF;
for (int b = 0; b <= 1; b++)
for (int c = 0; c <= 1; c++)
dp[i][1][b][c][1][1] = -INF;
}
int ans = -INF;
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++)
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
ans = Math.max(ans, dp[0][a][b][c][d][0]);
println(ans);
}
}
|
Java
|
["2\n01\n11", "2\n01\n01", "4\n0101\n1011", "4\n0000\n0000", "5\n00011\n10101", "6\n011111\n111111", "10\n0101001010\n1010100110"]
|
3 seconds
|
["2", "2", "4", "0", "4", "8", "6"]
|
NoteIn the first example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 2)$$$.In the second example, you can leave the hallway as it is, so the path of the robot is $$$(1, 1) \rightarrow (1, 2) \rightarrow (2, 2)$$$.In the third example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (1, 4)$$$.In the fourth example, the hallway is already clean. Maybe you have launched the robot earlier?
|
Java 17
|
standard input
|
[
"bitmasks",
"dp"
] |
a04cfd22f90b2b87f7c5b48dfe3de873
|
The first line contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of columns in the hallway. Then two lines follow, denoting the $$$1$$$-st and the $$$2$$$-nd row of the hallway. These lines contain $$$n$$$ characters each, where 0 denotes a clean cell and 1 denotes a dirty cell. The starting cell of the robot $$$(1, 1)$$$ is clean.
| 2,400
|
Print one integer — the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
standard output
| |
PASSED
|
13f5a146e749305fccf477c18e12e145
|
train_109.jsonl
|
1664462100
|
Consider a hallway, which can be represented as the matrix with $$$2$$$ rows and $$$n$$$ columns. Let's denote the cell on the intersection of the $$$i$$$-th row and the $$$j$$$-th column as $$$(i, j)$$$. The distance between the cells $$$(i_1, j_1)$$$ and $$$(i_2, j_2)$$$ is $$$|i_1 - i_2| + |j_1 - j_2|$$$.There is a cleaning robot in the cell $$$(1, 1)$$$. Some cells of the hallway are clean, other cells are dirty (the cell with the robot is clean). You want to clean the hallway, so you are going to launch the robot to do this.After the robot is launched, it works as follows. While at least one cell is dirty, the robot chooses the closest (to its current cell) cell among those which are dirty, moves there and cleans it (so the cell is no longer dirty). After cleaning a cell, the robot again finds the closest dirty cell to its current cell, and so on. This process repeats until the whole hallway is clean.However, there is a critical bug in the robot's program. If at some moment, there are multiple closest (to the robot's current position) dirty cells, the robot malfunctions.You want to clean the hallway in such a way that the robot doesn't malfunction. Before launching the robot, you can clean some (possibly zero) of the dirty cells yourself. However, you don't want to do too much dirty work yourself while you have this nice, smart (yet buggy) robot to do this. Note that you cannot make a clean cell dirty.Calculate the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class CF1739E extends PrintWriter {
CF1739E() { super(System.out, true); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1739E o = new CF1739E(); o.main(); o.flush();
}
static final int INF = 0x3f3f3f3f;
void main() {
int n = sc.nextInt();
byte[] aa = sc.next().getBytes();
byte[] bb = sc.next().getBytes();
int[][][][][][] dp = new int[n + 1][2][2][2][2][2]; // [i][a][b][c][d][e]
for (int a = 0; a <= 1; a++)
for (int d = 0; d <= 1; d++)
dp[n][a][1][1][d][0] = -INF;
for (int b = 0; b <= 1; b++)
for (int c = 0; c <= 1; c++)
dp[n][1][b][c][1][1] = -INF;
for (int i = n - 1; i >= 0; i--) {
for (int e = 0; e <= 1; e++)
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++) {
if (a == 0 && b == 0)
continue;
int x = dp[i + 1][0][0][a][b][e];
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][e]);
dp[i][0][0][a][b][e] = x;
}
if (aa[i] == '1') {
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++) {
int x = -INF;
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][0] + 1);
dp[i][1][0][a][b][0] = x;
}
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 0; b++) {
int x = -INF;
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][0] + 1);
dp[i][1][0][a][b][1] = x;
}
}
if (bb[i] == '1') {
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++) {
int x = -INF;
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][1] + 1);
dp[i][0][1][a][b][1] = x;
}
for (int a = 0; a <= 0; a++)
for (int b = 0; b <= 1; b++) {
int x = -INF;
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][1] + 1);
dp[i][0][1][a][b][0] = x;
}
}
if (aa[i] == '1' && bb[i] == '1') {
for (int a = 0; a <= 0; a++)
for (int b = 0; b <= 1; b++) {
int x = -INF;
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][1] + 2);
dp[i][1][1][a][b][0] = x;
}
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 0; b++) {
int x = -INF;
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
x = Math.max(x, dp[i + 1][a][b][c][d][0] + 2);
dp[i][1][1][a][b][1] = x;
}
}
for (int a = 0; a <= 1; a++)
for (int d = 0; d <= 1; d++)
dp[i][a][1][1][d][0] = -INF;
for (int b = 0; b <= 1; b++)
for (int c = 0; c <= 1; c++)
dp[i][1][b][c][1][1] = -INF;
}
int ans = -INF;
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++)
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
ans = Math.max(ans, dp[0][a][b][c][d][0]);
println(ans);
}
}
|
Java
|
["2\n01\n11", "2\n01\n01", "4\n0101\n1011", "4\n0000\n0000", "5\n00011\n10101", "6\n011111\n111111", "10\n0101001010\n1010100110"]
|
3 seconds
|
["2", "2", "4", "0", "4", "8", "6"]
|
NoteIn the first example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 2)$$$.In the second example, you can leave the hallway as it is, so the path of the robot is $$$(1, 1) \rightarrow (1, 2) \rightarrow (2, 2)$$$.In the third example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (1, 4)$$$.In the fourth example, the hallway is already clean. Maybe you have launched the robot earlier?
|
Java 17
|
standard input
|
[
"bitmasks",
"dp"
] |
a04cfd22f90b2b87f7c5b48dfe3de873
|
The first line contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of columns in the hallway. Then two lines follow, denoting the $$$1$$$-st and the $$$2$$$-nd row of the hallway. These lines contain $$$n$$$ characters each, where 0 denotes a clean cell and 1 denotes a dirty cell. The starting cell of the robot $$$(1, 1)$$$ is clean.
| 2,400
|
Print one integer — the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
standard output
| |
PASSED
|
0faea6c40852e6b457dbbec00caac0d8
|
train_109.jsonl
|
1664462100
|
Consider a hallway, which can be represented as the matrix with $$$2$$$ rows and $$$n$$$ columns. Let's denote the cell on the intersection of the $$$i$$$-th row and the $$$j$$$-th column as $$$(i, j)$$$. The distance between the cells $$$(i_1, j_1)$$$ and $$$(i_2, j_2)$$$ is $$$|i_1 - i_2| + |j_1 - j_2|$$$.There is a cleaning robot in the cell $$$(1, 1)$$$. Some cells of the hallway are clean, other cells are dirty (the cell with the robot is clean). You want to clean the hallway, so you are going to launch the robot to do this.After the robot is launched, it works as follows. While at least one cell is dirty, the robot chooses the closest (to its current cell) cell among those which are dirty, moves there and cleans it (so the cell is no longer dirty). After cleaning a cell, the robot again finds the closest dirty cell to its current cell, and so on. This process repeats until the whole hallway is clean.However, there is a critical bug in the robot's program. If at some moment, there are multiple closest (to the robot's current position) dirty cells, the robot malfunctions.You want to clean the hallway in such a way that the robot doesn't malfunction. Before launching the robot, you can clean some (possibly zero) of the dirty cells yourself. However, you don't want to do too much dirty work yourself while you have this nice, smart (yet buggy) robot to do this. Note that you cannot make a clean cell dirty.Calculate the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
512 megabytes
|
// package c1739;
//
// Educational Codeforces Round 136 (Rated for Div. 2) 2022-09-29 07:35
// E. Cleaning Robot
// https://codeforces.com/contest/1739/problem/E
// time limit per test 3 seconds; memory limit per test 512 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// Consider a hallway, which can be represented as the matrix with 2 rows and n columns. Let's
// denote the cell on the intersection of the i-th row and the j-th column as (i, j). The distance
// between the cells (i_1, j_1) and (i_2, j_2) is |i_1 - i_2| + |j_1 - j_2|.
//
// There is a cleaning robot in the cell (1, 1). Some cells of the hallway are clean, other cells
// are dirty (the cell with the robot is clean). You want to clean the hallway, so you are going to
// launch the robot to do this.
//
// After the robot is launched, it works as follows. While at least one cell is dirty, the robot
// chooses among those which are dirty, moves there and cleans it (so the cell is no longer dirty).
// After cleaning a cell, the robot again finds the closest dirty cell , and so on. This process
// repeats until the whole hallway is clean.
//
// However, there is a critical bug in the robot's program. If at some moment, there are multiple
// closest (to the robot's current position) dirty cells, the robot malfunctions.
//
// You want to clean the hallway in such a way that the robot doesn't malfunction. , you can clean
// some (possibly zero) of the dirty cells yourself. However, you don't want to do too much dirty
// work yourself while you have this nice, smart (yet buggy) robot to do this. Note that you cannot
// make a clean cell dirty.
//
// Calculate the maximum possible number of cells you can leave dirty before launching the robot, so
// that it doesn't malfunction.
//
// Input
//
// The first line contains one integer n (2 <= n <= 2 * 10^5)-- the number of columns in the
// hallway.
//
// Then two lines follow, denoting the 1-st and the 2-nd row of the hallway. These lines contain n
// characters each, where 0 denotes a clean cell and 1 denotes a dirty cell. The starting cell of
// the robot (1, 1) is clean.
//
// Output
//
// Print one integer-- the maximum possible number of cells you can leave dirty before launching the
// robot, so that it doesn't malfunction.
//
// Example
/*
input:
2
01
11
output:
2
input:
2
01
01
output:
2
input:
4
0101
1011
output:
4
input:
4
0000
0000
output:
0
input:
5
00011
10101
output:
4
input:
6
011111
111111
output:
8
input:
10
0101001010
1010100110
output:
6
*/
// Note
//
// In the first example, you can clean the cell (1, 2), so the path of the robot is (1, 1) -> (2, 1)
// -> (2, 2).
//
// In the second example, you can leave the hallway as it is, so the path of the robot is (1, 1) ->
// (1, 2) -> (2, 2).
//
// In the third example, you can clean the cell (1, 2), so the path of the robot is (1, 1) -> (2, 1)
// -> (2, 3) -> (2, 4) -> (1, 4).
//
// In the fourth example, the hallway is already clean. Maybe you have launched the robot earlier?
//
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandles;
import java.util.Random;
import java.util.StringTokenizer;
public class C1739E {
static final int MOD = 998244353;
static final Random RAND = new Random();
static int solve(String s1, String s2) {
int n = s1.length();
int[][] a = new int[2][n];
int ones = 0;
for (int i = 0; i < n; i++) {
a[0][i] = s1.charAt(i) - '0';
a[1][i] = s2.charAt(i) - '0';
ones += a[0][i] + a[1][i];
}
int[][] next = new int[2][n+1];
for (int i = 0; i < 2; i++) {
next[i][n] = n;
for (int j = n - 1; j >= 0; j--) {
next[i][j] = a[i][j] == 1 ? j : next[i][j+1];
}
}
// dp[i][j] is the minimal number of cells to clean to handle
// suffix [j,n) while bot starts at (i,j-1)
int[][] dp = new int[2][n + 2];
for (int j = n - 1; j >= 0; j--) {
for (int i = 0; i < 2; i++) {
int t = next[i][j];
int o = next[1-i][j];
// System.out.format(" j:%d i:%d t:%d o:%d\n", j, i, t, o);
if (t >= n || o >= n) {
// no dirty cells on this row or the other row or both
dp[i][j] = 0;
} else if (t < o) {
// . . . *
// . . . . . *
// ^ ^
// t o
dp[i][j] = dp[i][t + 1];
} else if (t == o) {
if (t == n - 1 || a[i][t+1] == 0) {
// . . . * .
// . . . * .
// ^
// t/o
dp[i][j] = min(dp[1-i][t+1], 1 + dp[i][t+1]);
} else {
// i . . . * *
// 1-i . . . * ?
// ^
// t/o
dp[i][j] = min(1 + dp[i][t+1], 1 + dp[1-i][t+2]);
}
} else if (t == o + 1) {
// equal distance to dirty cells in this and the other row
// i . . . *
// 1-i . . * ?
// ^ ^
// o t
dp[i][j] = min(1 + dp[i][o+1], 1 + dp[1-i][t+1]);
} else {
// i . . . . *
// 1-i . . * ?
// ^ ^
// o t
dp[i][j] = min(dp[1-i][o+1], 1 + dp[i][o+1]);
}
}
}
return ones - dp[0][0];
}
static int min(int a, int b) {
return Math.min(a, b);
}
static int min(int a, int b, int c) {
return Math.min(a, Math.min(b, c));
}
static void test(int exp, String s1, String s2) {
int ans = solve(s1, s2);
System.out.format("%s %s => %d %s\n", s1, s2, ans, ans == exp ? "" : "Expected " + exp);
myAssert(ans == exp);
}
static boolean test = false;
static void doTest() {
if (!test) {
return;
}
long t0 = System.currentTimeMillis();
test(2, "01", "11");
test(2, "01", "01");
test(4, "0101", "1011");
test(0, "0000", "0000");
test(4, "00011", "10101");
test(8, "011111", "111111");
test(6, "0101001010", "1010100110");
System.out.format("%d msec\n", System.currentTimeMillis() - t0);
System.exit(0);
}
public static void main(String[] args) {
doTest();
MyScanner in = new MyScanner();
int n = in.nextInt();
String s1 = in.next();
String s2 = in.next();
int ans = solve(s1, s2);
System.out.println(ans);
}
static void output(int[] a) {
if (a == null) {
System.out.println("-1");
return;
}
StringBuilder sb = new StringBuilder();
for (int v : a) {
sb.append(v);
sb.append(' ');
if (sb.length() > 4000) {
System.out.print(sb.toString());
sb.setLength(0);
}
}
System.out.println(sb.toString());
}
static void myAssert(boolean cond) {
if (!cond) {
throw new RuntimeException("Unexpected");
}
}
static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", "");
cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname;
final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in");
br = new BufferedReader(new InputStreamReader(fin.exists()
? new FileInputStream(fin) : System.in));
} catch (Exception e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
public String next() {
try {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["2\n01\n11", "2\n01\n01", "4\n0101\n1011", "4\n0000\n0000", "5\n00011\n10101", "6\n011111\n111111", "10\n0101001010\n1010100110"]
|
3 seconds
|
["2", "2", "4", "0", "4", "8", "6"]
|
NoteIn the first example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 2)$$$.In the second example, you can leave the hallway as it is, so the path of the robot is $$$(1, 1) \rightarrow (1, 2) \rightarrow (2, 2)$$$.In the third example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (1, 4)$$$.In the fourth example, the hallway is already clean. Maybe you have launched the robot earlier?
|
Java 11
|
standard input
|
[
"bitmasks",
"dp"
] |
a04cfd22f90b2b87f7c5b48dfe3de873
|
The first line contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of columns in the hallway. Then two lines follow, denoting the $$$1$$$-st and the $$$2$$$-nd row of the hallway. These lines contain $$$n$$$ characters each, where 0 denotes a clean cell and 1 denotes a dirty cell. The starting cell of the robot $$$(1, 1)$$$ is clean.
| 2,400
|
Print one integer — the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
standard output
| |
PASSED
|
83b055bae1a6c3faf84e98e89afef94d
|
train_109.jsonl
|
1664462100
|
Consider a hallway, which can be represented as the matrix with $$$2$$$ rows and $$$n$$$ columns. Let's denote the cell on the intersection of the $$$i$$$-th row and the $$$j$$$-th column as $$$(i, j)$$$. The distance between the cells $$$(i_1, j_1)$$$ and $$$(i_2, j_2)$$$ is $$$|i_1 - i_2| + |j_1 - j_2|$$$.There is a cleaning robot in the cell $$$(1, 1)$$$. Some cells of the hallway are clean, other cells are dirty (the cell with the robot is clean). You want to clean the hallway, so you are going to launch the robot to do this.After the robot is launched, it works as follows. While at least one cell is dirty, the robot chooses the closest (to its current cell) cell among those which are dirty, moves there and cleans it (so the cell is no longer dirty). After cleaning a cell, the robot again finds the closest dirty cell to its current cell, and so on. This process repeats until the whole hallway is clean.However, there is a critical bug in the robot's program. If at some moment, there are multiple closest (to the robot's current position) dirty cells, the robot malfunctions.You want to clean the hallway in such a way that the robot doesn't malfunction. Before launching the robot, you can clean some (possibly zero) of the dirty cells yourself. However, you don't want to do too much dirty work yourself while you have this nice, smart (yet buggy) robot to do this. Note that you cannot make a clean cell dirty.Calculate the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
512 megabytes
|
// package c1739;
//
// Educational Codeforces Round 136 (Rated for Div. 2) 2022-09-29 07:35
// E. Cleaning Robot
// https://codeforces.com/contest/1739/problem/E
// time limit per test 3 seconds; memory limit per test 512 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// Consider a hallway, which can be represented as the matrix with 2 rows and n columns. Let's
// denote the cell on the intersection of the i-th row and the j-th column as (i, j). The distance
// between the cells (i_1, j_1) and (i_2, j_2) is |i_1 - i_2| + |j_1 - j_2|.
//
// There is a cleaning robot in the cell (1, 1). Some cells of the hallway are clean, other cells
// are dirty (the cell with the robot is clean). You want to clean the hallway, so you are going to
// launch the robot to do this.
//
// After the robot is launched, it works as follows. While at least one cell is dirty, the robot
// chooses among those which are dirty, moves there and cleans it (so the cell is no longer dirty).
// After cleaning a cell, the robot again finds the closest dirty cell , and so on. This process
// repeats until the whole hallway is clean.
//
// However, there is a critical bug in the robot's program. If at some moment, there are multiple
// closest (to the robot's current position) dirty cells, the robot malfunctions.
//
// You want to clean the hallway in such a way that the robot doesn't malfunction. , you can clean
// some (possibly zero) of the dirty cells yourself. However, you don't want to do too much dirty
// work yourself while you have this nice, smart (yet buggy) robot to do this. Note that you cannot
// make a clean cell dirty.
//
// Calculate the maximum possible number of cells you can leave dirty before launching the robot, so
// that it doesn't malfunction.
//
// Input
//
// The first line contains one integer n (2 <= n <= 2 * 10^5)-- the number of columns in the
// hallway.
//
// Then two lines follow, denoting the 1-st and the 2-nd row of the hallway. These lines contain n
// characters each, where 0 denotes a clean cell and 1 denotes a dirty cell. The starting cell of
// the robot (1, 1) is clean.
//
// Output
//
// Print one integer-- the maximum possible number of cells you can leave dirty before launching the
// robot, so that it doesn't malfunction.
//
// Example
/*
input:
2
01
11
output:
2
input:
2
01
01
output:
2
input:
4
0101
1011
output:
4
input:
4
0000
0000
output:
0
input:
5
00011
10101
output:
4
input:
6
011111
111111
output:
8
input:
10
0101001010
1010100110
output:
6
*/
// Note
//
// In the first example, you can clean the cell (1, 2), so the path of the robot is (1, 1) -> (2, 1)
// -> (2, 2).
//
// In the second example, you can leave the hallway as it is, so the path of the robot is (1, 1) ->
// (1, 2) -> (2, 2).
//
// In the third example, you can clean the cell (1, 2), so the path of the robot is (1, 1) -> (2, 1)
// -> (2, 3) -> (2, 4) -> (1, 4).
//
// In the fourth example, the hallway is already clean. Maybe you have launched the robot earlier?
//
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandles;
import java.util.Random;
import java.util.StringTokenizer;
public class C1739E {
static final int MOD = 998244353;
static final Random RAND = new Random();
static int solve(String s1, String s2) {
int n = s1.length();
int ans = 0;
int[][] a = new int[2][n];
int ones = 0;
for (int i = 0; i < n; i++) {
a[0][i] = s1.charAt(i) - '0';
a[1][i] = s2.charAt(i) - '0';
ones += a[0][i] + a[1][i];
}
int[][] next = new int[2][n+1];
for (int i = 0; i < 2; i++) {
next[i][n] = n;
for (int j = n - 1; j >= 0; j--) {
next[i][j] = a[i][j] == 1 ? j : next[i][j+1];
}
}
// dp[i][j] is the minimal number of cells to clean to handle
// suffix [j,n) while bot starts at (i,j-1)
int[][] dp = new int[2][n + 2];
for (int j = n - 1; j >= 0; j--) {
for (int i = 0; i < 2; i++) {
int t = next[i][j];
int o = next[1-i][j];
// System.out.format(" j:%d i:%d t:%d o:%d\n", j, i, t, o);
if (t >= n || o >= n) {
// no dirty cells on this row or the other row or both
dp[i][j] = 0;
} else if (t < o) {
// . . . *
// . . . . . *
// ^ ^
// t o
dp[i][j] = dp[i][t + 1];
} else if (t == o) {
if (t == n - 1 || a[i][t+1] == 0) {
// . . . * .
// . . . * .
// ^
// t/o
dp[i][j] = min(dp[1-i][t+1], 1 + dp[i][t+1]);
} else {
// i . . . * *
// 1-i . . . * ?
// ^
// t/o
dp[i][j] = min(1 + dp[i][t+1], 1 + dp[1-i][t+2]);
}
} else if (t == o + 1) {
// equal distance to dirty cells in this and the other row
// i . . . *
// 1-i . . * ?
// ^ ^
// o t
dp[i][j] = min(1 + dp[i][o+1], 1 + dp[1-i][t+1]);
} else {
// i . . . . *
// 1-i . . * ?
// ^ ^
// o t
dp[i][j] = min(dp[1-i][o+1], 1 + dp[i][o+1]);
}
}
}
int clean = 0;
if (a[1][0] == 0) {
clean = dp[0][1];
} else if (a[0][1] == 1) {
// . *
// * ?
clean = min(1 + dp[0][1], 1 + dp[1][2]);
} else {
// . .
// * ?
clean = min(dp[1][1], 1 + dp[0][1]);
}
// System.out.format(" dp0: %s\n", Arrays.toString(dp[0]));
// System.out.format(" dp1: %s\n", Arrays.toString(dp[1]));
myAssert(clean == dp[0][0]);
return ones - clean;
}
static int min(int a, int b) {
return Math.min(a, b);
}
static int min(int a, int b, int c) {
return Math.min(a, Math.min(b, c));
}
static void test(int exp, String s1, String s2) {
int ans = solve(s1, s2);
System.out.format("%s %s => %d %s\n", s1, s2, ans, ans == exp ? "" : "Expected " + exp);
myAssert(ans == exp);
}
static boolean test = false;
static void doTest() {
if (!test) {
return;
}
long t0 = System.currentTimeMillis();
test(2, "01", "11");
test(2, "01", "01");
test(4, "0101", "1011");
test(0, "0000", "0000");
test(4, "00011", "10101");
test(8, "011111", "111111");
test(6, "0101001010", "1010100110");
System.out.format("%d msec\n", System.currentTimeMillis() - t0);
System.exit(0);
}
public static void main(String[] args) {
doTest();
MyScanner in = new MyScanner();
int n = in.nextInt();
String s1 = in.next();
String s2 = in.next();
int ans = solve(s1, s2);
System.out.println(ans);
}
static void output(int[] a) {
if (a == null) {
System.out.println("-1");
return;
}
StringBuilder sb = new StringBuilder();
for (int v : a) {
sb.append(v);
sb.append(' ');
if (sb.length() > 4000) {
System.out.print(sb.toString());
sb.setLength(0);
}
}
System.out.println(sb.toString());
}
static void myAssert(boolean cond) {
if (!cond) {
throw new RuntimeException("Unexpected");
}
}
static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", "");
cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname;
final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in");
br = new BufferedReader(new InputStreamReader(fin.exists()
? new FileInputStream(fin) : System.in));
} catch (Exception e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
public String next() {
try {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["2\n01\n11", "2\n01\n01", "4\n0101\n1011", "4\n0000\n0000", "5\n00011\n10101", "6\n011111\n111111", "10\n0101001010\n1010100110"]
|
3 seconds
|
["2", "2", "4", "0", "4", "8", "6"]
|
NoteIn the first example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 2)$$$.In the second example, you can leave the hallway as it is, so the path of the robot is $$$(1, 1) \rightarrow (1, 2) \rightarrow (2, 2)$$$.In the third example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (1, 4)$$$.In the fourth example, the hallway is already clean. Maybe you have launched the robot earlier?
|
Java 11
|
standard input
|
[
"bitmasks",
"dp"
] |
a04cfd22f90b2b87f7c5b48dfe3de873
|
The first line contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of columns in the hallway. Then two lines follow, denoting the $$$1$$$-st and the $$$2$$$-nd row of the hallway. These lines contain $$$n$$$ characters each, where 0 denotes a clean cell and 1 denotes a dirty cell. The starting cell of the robot $$$(1, 1)$$$ is clean.
| 2,400
|
Print one integer — the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
standard output
| |
PASSED
|
59fc74631547e1a1699ba1eb135e0945
|
train_109.jsonl
|
1664462100
|
Consider a hallway, which can be represented as the matrix with $$$2$$$ rows and $$$n$$$ columns. Let's denote the cell on the intersection of the $$$i$$$-th row and the $$$j$$$-th column as $$$(i, j)$$$. The distance between the cells $$$(i_1, j_1)$$$ and $$$(i_2, j_2)$$$ is $$$|i_1 - i_2| + |j_1 - j_2|$$$.There is a cleaning robot in the cell $$$(1, 1)$$$. Some cells of the hallway are clean, other cells are dirty (the cell with the robot is clean). You want to clean the hallway, so you are going to launch the robot to do this.After the robot is launched, it works as follows. While at least one cell is dirty, the robot chooses the closest (to its current cell) cell among those which are dirty, moves there and cleans it (so the cell is no longer dirty). After cleaning a cell, the robot again finds the closest dirty cell to its current cell, and so on. This process repeats until the whole hallway is clean.However, there is a critical bug in the robot's program. If at some moment, there are multiple closest (to the robot's current position) dirty cells, the robot malfunctions.You want to clean the hallway in such a way that the robot doesn't malfunction. Before launching the robot, you can clean some (possibly zero) of the dirty cells yourself. However, you don't want to do too much dirty work yourself while you have this nice, smart (yet buggy) robot to do this. Note that you cannot make a clean cell dirty.Calculate the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class cleaningrobot {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
int[][] arr = new int[2][n];
for(int i=0; i<2; i++) {
String str = br.readLine();
for(int j=0; j<n; j++)arr[i][j] = str.charAt(j)-'0';
}
int[][][] dp = new int[n][2][2];
for(int i=0; i<n; i++) for(int j=0; j<2; j++)for(int k=0; k<2; k++)dp[i][j][k]=Integer.MIN_VALUE;
dp[0][0][0]=0;
if(arr[1][0]==1)dp[0][0][1]=1;
for(int i=0; i<n-1; i++) {
for(int j=0; j<2; j++) {
int next = 0;
int nextj = 0;
if(arr[j][i+1]==1)next=1;
if(arr[1-j][i+1]==1)nextj=1;
dp[i+1][1-j][0] = Math.max(dp[i+1][1-j][0], dp[i][j][1]+nextj);
dp[i+1][j][nextj] = Math.max(dp[i+1][j][nextj], dp[i][j][0]+nextj+next);
dp[i+1][j][0] =Math.max(dp[i+1][j][0], dp[i][j][0]+next);
}
}
out.write(Math.max(Math.max(dp[n-1][0][0],dp[n-1][1][0]), Math.max(dp[n-1][0][1], dp[n-1][1][1]))+"\n");
out.flush();
}
}
|
Java
|
["2\n01\n11", "2\n01\n01", "4\n0101\n1011", "4\n0000\n0000", "5\n00011\n10101", "6\n011111\n111111", "10\n0101001010\n1010100110"]
|
3 seconds
|
["2", "2", "4", "0", "4", "8", "6"]
|
NoteIn the first example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 2)$$$.In the second example, you can leave the hallway as it is, so the path of the robot is $$$(1, 1) \rightarrow (1, 2) \rightarrow (2, 2)$$$.In the third example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (1, 4)$$$.In the fourth example, the hallway is already clean. Maybe you have launched the robot earlier?
|
Java 11
|
standard input
|
[
"bitmasks",
"dp"
] |
a04cfd22f90b2b87f7c5b48dfe3de873
|
The first line contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of columns in the hallway. Then two lines follow, denoting the $$$1$$$-st and the $$$2$$$-nd row of the hallway. These lines contain $$$n$$$ characters each, where 0 denotes a clean cell and 1 denotes a dirty cell. The starting cell of the robot $$$(1, 1)$$$ is clean.
| 2,400
|
Print one integer — the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
standard output
| |
PASSED
|
7c23986306562734292f58531fc61dbe
|
train_109.jsonl
|
1664462100
|
Consider a hallway, which can be represented as the matrix with $$$2$$$ rows and $$$n$$$ columns. Let's denote the cell on the intersection of the $$$i$$$-th row and the $$$j$$$-th column as $$$(i, j)$$$. The distance between the cells $$$(i_1, j_1)$$$ and $$$(i_2, j_2)$$$ is $$$|i_1 - i_2| + |j_1 - j_2|$$$.There is a cleaning robot in the cell $$$(1, 1)$$$. Some cells of the hallway are clean, other cells are dirty (the cell with the robot is clean). You want to clean the hallway, so you are going to launch the robot to do this.After the robot is launched, it works as follows. While at least one cell is dirty, the robot chooses the closest (to its current cell) cell among those which are dirty, moves there and cleans it (so the cell is no longer dirty). After cleaning a cell, the robot again finds the closest dirty cell to its current cell, and so on. This process repeats until the whole hallway is clean.However, there is a critical bug in the robot's program. If at some moment, there are multiple closest (to the robot's current position) dirty cells, the robot malfunctions.You want to clean the hallway in such a way that the robot doesn't malfunction. Before launching the robot, you can clean some (possibly zero) of the dirty cells yourself. However, you don't want to do too much dirty work yourself while you have this nice, smart (yet buggy) robot to do this. Note that you cannot make a clean cell dirty.Calculate the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class A {
static Scanner sc;
static PrintWriter pw;
static char arr[][];
static int dp[][]=new int [2][200005] ;
static int t;
public static int find(int i,int j) {
if(j>t)
return 0;
if(dp[i][j]!=-1) return dp[i][j];
int next=(i+1)%2;
int ans=0;
if((j+1<t)&&arr[i][j+1]=='1'&&arr[next][j]=='1') {
ans=Math.min(find(next, j+2)+1,find(i, j+1)+1 );
}
else if((j+1<t)&&arr[next][j]=='1') {
ans=Math.min(find(next, j+1),find(i, j+1)+1 );
}
else {
ans=find(i, j+1);
}
return dp[i][j]=ans;
}
public static void main(String[] args) throws IOException {
sc = new Scanner(System.in);
pw = new PrintWriter(System.out);
t=sc.nextInt();
String s1=sc.next();
String s2=sc.next();
arr=new char[2][t];
arr[0]=s1.toCharArray();
arr[1]=s2.toCharArray();
int c=0;
for (int i = 0; i < t; i++) {
if(arr[0][i]=='1')++c;
if(arr[1][i]=='1')++c;
}
Arrays.fill(dp[0], -1);
Arrays.fill(dp[1], -1);
System.out.println(c-find(0, 0));
pw.flush();
}
}
class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String readAllLines(BufferedReader reader) throws IOException {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
return content.toString();
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
|
Java
|
["2\n01\n11", "2\n01\n01", "4\n0101\n1011", "4\n0000\n0000", "5\n00011\n10101", "6\n011111\n111111", "10\n0101001010\n1010100110"]
|
3 seconds
|
["2", "2", "4", "0", "4", "8", "6"]
|
NoteIn the first example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 2)$$$.In the second example, you can leave the hallway as it is, so the path of the robot is $$$(1, 1) \rightarrow (1, 2) \rightarrow (2, 2)$$$.In the third example, you can clean the cell $$$(1, 2)$$$, so the path of the robot is $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (1, 4)$$$.In the fourth example, the hallway is already clean. Maybe you have launched the robot earlier?
|
Java 8
|
standard input
|
[
"bitmasks",
"dp"
] |
a04cfd22f90b2b87f7c5b48dfe3de873
|
The first line contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of columns in the hallway. Then two lines follow, denoting the $$$1$$$-st and the $$$2$$$-nd row of the hallway. These lines contain $$$n$$$ characters each, where 0 denotes a clean cell and 1 denotes a dirty cell. The starting cell of the robot $$$(1, 1)$$$ is clean.
| 2,400
|
Print one integer — the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
|
standard output
| |
PASSED
|
c3753e4dd3e03c1aa07dedc3ab7a888e
|
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
{
BufferedReader br = new BufferedReader ( new InputStreamReader( System.in ));
int t = Integer.parseInt( br.readLine() );
List < ArrayList<Long> > answer = new ArrayList<>() ;
for( int i = 0 ; i != t ; ++i )
{
int n = Integer.parseInt( br.readLine() );
String str = br.readLine();
ArrayList< Long > max_sum_list = f( str );
answer.add( max_sum_list );
}
PrintWriter out = new PrintWriter(System.out);
for( int i = 0 ; i != t ; ++i )
{
for( int j = 0 ; j != answer.get(i).size() ; ++j )
out.print( answer.get(i).get(j) + " " );
out.flush();
out.println("");
}
return ;
}
public static ArrayList< Long > f ( String str )
{
ArrayList< Long > answer = new ArrayList<>() ;
long sum = 0 ;
int n = str.length() ;
for( int i = 0 ; i != n; ++i )
{
char ch = str.charAt(i) ;
if ( ch == 'R' )
sum += (long)( n - (1+i) ) ;
else
sum += (long)i;
}
int count = 0;
int start = 0 ;
int end = n-1 ;
for( int k = 1 ; k <= n ; ++k )
{
while ( count != k && start <= end )
{
int left_of_start = start ;
int right_of_start = n - ( 1+start ) ;
int left_of_end = end ;
int right_of_end = n - ( 1+end );
// Case 1 :- If both are in Optimal directions, then just go to next character and try if it is pssible to increase sum by changing it
if ( str.charAt( start ) == 'R' && right_of_start >= left_of_start || (str.charAt( start ) == 'L' && left_of_start >= right_of_start ) )
{
if ( ( str.charAt( end ) == 'R' && right_of_end >= left_of_end ) || (str.charAt( end ) == 'L' && left_of_end >= right_of_end) )
{
++start;
--end;
continue ;
}
}
boolean start_is_optimal = false;
boolean end_is_optimal = false;
if ( str.charAt( start ) == 'R' && right_of_start >= left_of_start || ( str.charAt( start ) == 'L' && left_of_start >= right_of_start ) )
start_is_optimal = true;
if ( str.charAt( end ) == 'R' && right_of_end >= left_of_end || (str.charAt( end ) == 'L' && left_of_end >= right_of_end ) )
end_is_optimal = true;
// Case 2 : start is fine but end is not
if ( start_is_optimal && !end_is_optimal )
{
sum += (long) Math.abs( left_of_end - right_of_end ) ;
++start;
--end;
++count ;
}
// Case 3 : start is not fine but end is fine
else if ( ! start_is_optimal && end_is_optimal )
{
sum += (long) Math.abs( left_of_start - right_of_start ) ;
++start ;
--end;
++count ;
}
// Case 4 : start is not fine and end is also not fine
else
{
long num1 = (long) ( sum + Math.abs( left_of_start - right_of_start ) );
long num2 = (long) (sum + Math.abs( left_of_end - right_of_end) ) ;
if ( num1 >= num2 )
{
sum = num1;
++count ;
++start ;
}
else
{
sum = num2;
++count ;
--end ;
}
}
}
answer.add(sum) ;
}
return answer;
}
}
|
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 17
|
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
|
919191cf792535177601149eabb1cb79
|
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
{
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
int t = Integer.parseInt( br.readLine() );
List < ArrayList<Long> > answer = new ArrayList<>() ;
for( int i = 0 ; i != t ; ++i )
{
int n = Integer.parseInt( br.readLine() );
String str = br.readLine();
ArrayList< Long > max_sum_list = f( str );
answer.add( max_sum_list );
}
for( int i = 0 ; i != t ; ++i )
{
for( int j = 0 ; j != answer.get(i).size() ; ++j )
System.out.print( answer.get(i).get(j) + " " );
System.out.println("");
}
return ;
}
public static ArrayList< Long > f ( String str )
{
ArrayList< Long > answer = new ArrayList<>() ;
long sum = 0 ;
int n = str.length() ;
for( int i = 0 ; i != n; ++i )
{
char ch = str.charAt(i) ;
if ( ch == 'R' )
sum += (long)( n - (1+i) ) ;
else
sum += (long)i;
}
int mid = -1 ; // just initialization
if ( (n&1) == 0 ) // size is even
mid = n/2 -1 ;
else // size is odd
mid = n/2 ;
List< Integer > list = new ArrayList<>() ;
for( int index = 0 ; index <= mid ; ++index )
if ( str.charAt ( index ) == 'L' )
list.add( Math.abs( n-( 1+index ) - index ) );
for( int index = n-1 ; index > mid ; --index )
if ( str.charAt( index ) == 'R' )
list.add ( Math.abs( n-( 1+index ) - index ) ) ;
Collections.sort( list );
int index = list.size()-1 ;
int count = 0 ;
for( int k = 1 ; k <= n ; ++k )
{
if ( index >= 0 )
sum += (long)list.get( index-- ) ;
answer.add( sum ) ;
}
return answer;
}
}
|
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 17
|
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
|
d66a1579d6314f2701b89c5dfdfcf1cb
|
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() ;
List < ArrayList<Long> > answer = new ArrayList<>() ;
for( int i = 0 ; i != t ; ++i )
{
int n = sc.nextInt();
String str = sc.next();
ArrayList< Long > max_sum_list = f( str );
answer.add( max_sum_list );
}
for( int i = 0 ; i != t ; ++i )
{
for( int j = 0 ; j != answer.get(i).size() ; ++j )
System.out.print( answer.get(i).get(j) + " " );
System.out.println("");
}
return ;
}
public static ArrayList< Long > f ( String str )
{
ArrayList< Long > answer = new ArrayList<>() ;
long sum = 0 ;
int n = str.length() ;
for( int i = 0 ; i != n; ++i )
{
char ch = str.charAt(i) ;
if ( ch == 'R' )
sum += (long)( n - (1+i) ) ;
else
sum += (long)i;
}
int mid = -1 ; // just initialization
if ( (n&1) == 0 ) // size is even
mid = n/2 -1 ;
else // size is odd
mid = n/2 ;
List< Integer > list = new ArrayList<>() ;
for( int index = 0 ; index <= mid ; ++index )
if ( str.charAt ( index ) == 'L' )
list.add( Math.abs( n-( 1+index ) - index ) );
for( int index = n-1 ; index > mid ; --index )
if ( str.charAt( index ) == 'R' )
list.add ( Math.abs( n-( 1+index ) - index ) ) ;
Collections.sort( list );
int index = list.size()-1 ;
int count = 0 ;
for( int k = 1 ; k <= n ; ++k )
{
if ( index >= 0 )
sum += (long)list.get( index-- ) ;
answer.add( sum ) ;
}
return answer;
}
}
|
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 17
|
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
|
e8b33604d4d69e5cb8808abe6766e023
|
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() ;
List < ArrayList<Long> > answer = new ArrayList<>() ;
for( int i = 0 ; i != t ; ++i )
{
int n = sc.nextInt();
String str = sc.next();
ArrayList< Long > max_sum_list = f( str );
answer.add( max_sum_list );
}
for( int i = 0 ; i != t ; ++i )
{
for( int j = 0 ; j != answer.get(i).size() ; ++j )
System.out.print( answer.get(i).get(j) + " " );
System.out.println("");
}
return ;
}
public static ArrayList< Long > f ( String str )
{
ArrayList< Long > answer = new ArrayList<>() ;
long sum = 0 ;
int n = str.length() ;
for( int i = 0 ; i != n; ++i )
{
char ch = str.charAt(i) ;
if ( ch == 'R' )
sum += (long)( n - (1+i) ) ;
else
sum += (long)i;
}
int count = 0;
int start = 0 ;
int end = n-1 ;
for( int k = 1 ; k <= n ; ++k )
{
while ( count != k && start <= end )
{
int left_of_start = start ;
int right_of_start = n - ( 1+start ) ;
int left_of_end = end ;
int right_of_end = n - ( 1+end );
// Case 1 :- If both are in Optimal directions, then just go to next character and try if it is pssible to increase sum by changing it
if ( str.charAt( start ) == 'R' && right_of_start >= left_of_start || (str.charAt( start ) == 'L' && left_of_start >= right_of_start ) )
{
if ( ( str.charAt( end ) == 'R' && right_of_end >= left_of_end ) || (str.charAt( end ) == 'L' && left_of_end >= right_of_end) )
{
++start;
--end;
continue ;
}
}
boolean start_is_optimal = false;
boolean end_is_optimal = false;
if ( str.charAt( start ) == 'R' && right_of_start >= left_of_start || ( str.charAt( start ) == 'L' && left_of_start >= right_of_start ) )
start_is_optimal = true;
if ( str.charAt( end ) == 'R' && right_of_end >= left_of_end || (str.charAt( end ) == 'L' && left_of_end >= right_of_end ) )
end_is_optimal = true;
// Case 2 : start is fine but end is not
if ( start_is_optimal && !end_is_optimal )
{
sum += (long) Math.abs( left_of_end - right_of_end ) ;
++start;
--end;
++count ;
}
// Case 3 : start is not fine but end is fine
else if ( ! start_is_optimal && end_is_optimal )
{
sum += (long) Math.abs( left_of_start - right_of_start ) ;
++start ;
--end;
++count ;
}
// Case 4 : start is not fine and end is also not fine
else
{
long num1 = (long) ( sum + Math.abs( left_of_start - right_of_start ) );
long num2 = (long) (sum + Math.abs( left_of_end - right_of_end) ) ;
if ( num1 >= num2 )
{
sum = num1;
++count ;
++start ;
}
else
{
sum = num2;
++count ;
--end ;
}
}
}
answer.add(sum) ;
}
return answer;
}
}
|
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 17
|
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
|
638cb02a5a6b823cd298662668cc40f5
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import java.util.Scanner;
public class linetest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n-- != 0) {
int noOfPeople = sc.nextInt();
String line = sc.next();
Long value = (long) 0;
Long maxValue = (long) 0;
for (int i = 0; i < noOfPeople; i++) {
if (i < noOfPeople / 2) {
maxValue += (noOfPeople - i - 1);
} else if (i >= noOfPeople / 2) {
maxValue += i;
}
if (line.charAt(i) == 'R') {
value += (noOfPeople - i - 1);
} else if (line.charAt(i) == 'L') {
value += i;
}
}
int count = 0;
int index = 0;
for (int i = 0; i < noOfPeople; i++) {
int flag = 0;
while (flag == 0) {
if (count > noOfPeople / 2) {
if (line.charAt(count) == 'R') {
index = count;
flag = 1;
}
count = noOfPeople - count;
} else if (count < noOfPeople / 2) {
if (line.charAt(count) == 'L') {
index = count;
flag = 2;
}
count = noOfPeople - count - 1;
} else {
flag = 3;
}
}
if (flag == 2) {
value = value - index + (noOfPeople - index - 1);
flag = 0;
System.out.print(value + " ");
} else if (flag == 1) {
value = value - (noOfPeople - index - 1) + index;
flag = 0;
System.out.print(value + " ");
} else if (flag == 3) {
System.out.print(maxValue + " ");
}
}
}
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 17
|
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
|
3984c03cb6cacdd2d290561f7e98603e
|
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 int tcs, n, mid, l, r;
static StringTokenizer st;
static String input;
static long originPoint = 0;
static long currentPoint = 0;
static char[] currentInput;
static long lastPoint;
static boolean isMax = false;
static void solved() throws IOException {
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
mid = n/2;
l = 1;
r = n;
input = " " + br.readLine();
currentInput = input.toCharArray();
currentPoint = getPoint();
isMax = false;
if (n == 1) {
System.out.println(0);
} else {
int count = 0;
while (++count <= n) {
if (!isMax) {
lastPoint = getMaxPoint();
if (lastPoint == getAllMax()) isMax = true;
}
System.out.print(lastPoint + " ");
}
System.out.println();
}
}
static long getAllMax() {
long res = 0;
if (n % 2 == 0) {
res = (long) n * n - (long) mid * (mid + 1);
} else {
res = (long) n * (n - 1) - (long) mid * mid;
}
return res;
}
static long getPoint() {
long res = 0;
for (int i = 1; i <= n; i++) {
if (input.charAt(i) == 'L') {
res += i - 1;
} else {
res += n - i;
}
}
return res;
}
static long getMaxPoint() {
while (l <= mid && r >= mid) {
if (currentInput[l] == 'L') {
currentPoint = currentPoint - (l - 1);
currentPoint = currentPoint + (n - l);
currentInput[l] = 'R';
break;
}
if (currentInput[r] == 'R') {
currentPoint = currentPoint + (r - 1);
currentPoint = currentPoint - (n - r);
currentInput[r] = 'L';
break;
}
l++;
r--;
}
return currentPoint;
}
public static void main(String[] args) throws IOException {
tcs = Integer.parseInt(br.readLine());
while (tcs-- > 0) {
solved();
}
}
}
|
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 17
|
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
|
04e8ef8b9657ecd93a1c004cae004616
|
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.*;
/**
* -----------------|___________|---------------------
* CCCCCCCCC OOOOOOOOOO DDDDDDDD EEEEEEEEE
* CCCC OOO OOO DD DDD EEEE
* CCCC OOO OOO DD DDD EEEEEEEEEE
* CCCC OOO OOO DD DDDD EEEE
* CCCCCCCCC OOOOOOOOOO DDDDDDDDDDDD EEEEEEEEEE
* -----------------|___________|---------------------
*/
public class Main {
static long M = 1000000007;
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter sout = new OutputWriter(outputStream);
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
int[] dp = new int[n];
long cnt = 0;
char[] arr = in.nextToken().toCharArray();
for (int i = 0; i < n; i++) {
if (arr[i] == 'L') {
dp[i] = i;
} else {
dp[i] = n - i - 1;
}
cnt += dp[i];
}
char[] idealArr = new char[n];
long maxSum = 0;
for (int i = 0; i < n; i++) {
if (i > (n - 1) / 2) {
idealArr[i] = 'L';
maxSum += i;
} else {
idealArr[i] = 'R';
maxSum += n - 1 - i;
}
}
// sout.println(cnt);
// sout.println(maxSum);
StringBuilder res = new StringBuilder();
int k = n;
int l = 0;
int r = n - 1;
while (l < r) {
if (arr[l]!='R') {
k--;
cnt -= dp[l];
cnt += n - l - 1;
res.append(cnt);
res.append(" ");
}
if (arr[r]!='L') {
k--;
cnt -= dp[r];
cnt += r;
res.append(cnt);
res.append(" ");
}
l++;
r--;
}
/*for (int i = 0; i < n; i++) {
if (n % 2 != 0 && i == n / 2) {
continue;
}
if (arr[i] != idealArr[i]) {
k++;
if (idealArr[i] == 'L') {
cnt += i;
} else {
cnt += n - 1 - i;
}
map.put(k, cnt);
}
}*/
for (int i = 0; i < k; i++) {
res.append(cnt);
res.append(" ");
}
sout.println(res);
}
sout.close();
}
public static Set<Long> factorize(long n) {
Set<Long> factorize = new HashSet<>();
for (long x = 2; x * x <= n; x++) {
while (n % x == 0) {
n /= x;
factorize.add(x);
}
}
if (n > 1 || factorize.size() == 0) {
factorize.add(n);
}
return factorize;
}
public static void bfs(Node node) {
Queue<Node> queue = new LinkedList<>();
node.visit = true;
queue.add(node);
node.color = 1;
while (queue.size() > 0) {
Node current = queue.poll();
int clr = 1;
if (current.prevNode == null) {
clr = 2;
} else {
while (current.prevNode.color == clr || current.color == clr) {
clr++;
}
}
for (Node other : current.neighbors) {
if (other.visit)
continue;
other.visit = true;
other.prevNode = current;
other.color = clr;
clr++;
if (current.prevNode != null) {
while (current.prevNode.color == clr || current.color == clr) {
clr++;
}
} else {
while (current.color == clr) {
clr++;
}
}
queue.add(other);
}
}
}
public static void dfs(Node node) {
if (node.visit) {
return;
}
node.visit = true;
if (node.prevNode == null) {
node.color = 1;
} else {
int clr = 1;
for (Node other : node.neighbors) {
if (other == node.prevNode) {
continue;
}
if (clr != node.prevNode.color && clr != node.color) {
other.color = clr++;
continue;
}
clr++;
other.color = clr;
}
}
for (Node other : node.neighbors) {
other.prevNode = node;
dfs(other);
}
}
public static long factorial(long N) {
long result = 1;
for (long x = 2; x <= N; x++) {
result = (result * x) % M;
}
return result;
}
public static long binomial(long n) {
long nom = factorial(n); // n!
long denom = ((long) 2 * factorial(n - 2)) % M; // k!(n-k)!
return (nom * fastPow(denom, M - 2)) % M;
}
public static long fastPow(long num, long pow) {
if (pow == 0)
return 1;
if (pow % 2 == 0) {
long result = fastPow(num, pow / 2);
result = (result * result) % M;
return result;
}
long result = fastPow(num, pow - 1);
result = (result * num) % M;
return result;
}
public static void sortLong(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
public static void sortInt(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);
}
public static class Node {
int value;
boolean visit = false;
Node prevNode = null;
int color = -1;
List<Node> neighbors = new ArrayList<>();
public Node(int value) {
this.value = value;
}
}
public static class InputReader {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String nextToken() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
public 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) {
print(objects);
writer.println();
}
public void close() {
writer.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 17
|
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
|
5b6ce7f0fd5d861d66088ecf0b8b9c74
|
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
|
/*
* Problem Link: https://codeforces.com/contest/1722/problem/D
* Problem Status:
*/
// package R817_D4;
import java.util.*;
public class PD_Line {
public static void main(String[] args) {
// ------------------------------ SEPARATOR ------------------------------
Scanner Reader = new Scanner(System.in);
int T = Reader.nextInt();
while(T > 0) {
int N = Reader.nextInt();
char[] A = new char[N];
A = Reader.next().toCharArray();
TheAmazingFunction(A);
T--;
}
Reader.close();
// ------------------------------ SEPARATOR ------------------------------
}
public static void TheAmazingFunction(char[] A) {
long Sum;
long[] currentSum = new long[A.length];
ArrayList<Long> myList = new ArrayList<Long>();
// ------------------------------ SEPARATOR ------------------------------
for(int i = 0 ; i < A.length ; i++) {
if(A[i] == 'L')
currentSum[i] = i;
else
currentSum[i] = A.length- (i+1);
}
// ------------------------------ SEPARATOR ------------------------------
Sum = getSum(currentSum);
// ------------------------------ SEPARATOR ------------------------------
for(int i = 0 ; i < A.length ; i++) {
if(A[i] == 'L') {
if(A.length- (i+1) > i) {
myList.add(Long.valueOf((A.length - (i+1)) - i )) ;
}
} else {
if(i > A.length - (i+1)) {
myList.add(Long.valueOf(i - (A.length - (i+1))));
}
}
}
// ------------------------------ SEPARATOR ------------------------------
Collections.sort(myList);
// ------------------------------ SEPARATOR ------------------------------
long[] H = new long[myList.size()];
int u = 0;
long ans = 0;
for(int i = myList.size()-1 ; i >= 0 ; i--) {
H[u] = ans + myList.get(i);
ans = H[u];
u++;
}
// ------------------------------ SEPARATOR ------------------------------
for(int i = 0 ; i < A.length ; i++)
System.out.print(Long.valueOf(Sum + getListSum(H, i)) + " ");
System.out.println();
// ------------------------------ SEPARATOR ------------------------------
}
public static void ShowArr(int[] A) {
for(int i = 0 ; i < A.length ; i++)
System.out.print(A[i] + " ");
System.out.println();
}
public static long getListSum(long[] S, int X) {
if(S.length == 0)
return 0;
if(X >= S.length)
return S[S.length-1];
else
return S[X];
}
public static long getSum(long[] A) {
long Answer = 0;
for(int i = 0 ; i < A.length ; i++) {
Answer += A[i];
}
return Answer;
}
}
|
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 17
|
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
|
eb9dee473de5bf64409095b33a14a7d4
|
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 Codeforces {
public static class fastReader {
BufferedReader br;
StringTokenizer st;
public fastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (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 boolean isPalindrome(String word) {
for (int i = 0; i < word.length() / 2; i++) {
if (word.charAt(i) != word.charAt(word.length() - 1 - i)) {
return false;
}
}
return true;
}
public static fastReader sc = new fastReader();
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
String word = sc.next();
long[] arr = new long[n];
PriorityQueue<Long> number = new PriorityQueue<>();
long current = 0;
List<Long> ans = new ArrayList<>();
for (int i = 0; i < n; i++) {
char c = word.charAt(i);
if (c == 'L')
arr[i] = i;
else if (c == 'R')
arr[i] = n - i - 1;
current += arr[i];
number.add(arr[i]);
}
int counter = 1;
int half = n / 2;
// StringBuilder ans = new StringBuilder();
while (!number.isEmpty()) {
counter++;
long top = number.poll();
if (!(top >= half)) {
long newVal = n - top - 1;
long toAdd = newVal - top;
current += toAdd;
}
ans.add(current);
}
while (counter <= n) {
counter++;
ans.add(current);
}
for (int i = 0; i < n; i++) {
System.out.print(ans.get(i) + " ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn the first test case: $$$k=1$$$: change the direction of $$$1$$$ person to make the line RLR. The total value is $$$2+1+0=3$$$. $$$k=2$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. $$$k=3$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. Note that you have to change the direction of at most $$$k$$$ people. In the second test case, it is optimal to only change the direction of the first person for all $$$k$$$ from $$$1$$$ to $$$5$$$ (that is, make the line RRRLL).
|
Java 17
|
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
|
77bd9069820f28c245238f987596ca32
|
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 static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.*;
import static java.lang.System.inheritedChannel;
import static java.lang.System.out ;
public class CP0040 {
public static void main(String args[])throws Exception{
PrintWriter pw = new PrintWriter(out);
FastReader sc = new FastReader();
Reader s = new Reader();
int test;
//code yaha se hai
try{
test = sc.nextInt();
}
catch(Exception E){
return;
}
while(test-- > 0){
int n = sc.nextInt() ;
String str = sc.next();
int start = 0 , end = n-1 ;
char arr[] = str.toCharArray() ;
long narr[] = new long[n];
long sum = 0 ;
for(int i = 0 ; i < n ; i++){
if(arr[i] == 'L' ){
narr[i] = i ;
}
else {
narr[i] = n -1-i ;
}
sum = sum + narr[i] ;
}
if(n == 1){
pw.println(0);
continue;
}
ArrayList<Long> ar = new ArrayList<>() ;
for (int i = 0; i < n/2 ; i++) {
if(str.charAt(i) == 'L' && str.charAt(n-1-i) == 'R'){
sum = sum - i + (n-i-1) ;
ar.add(sum) ;
sum = sum - i + (n-i-1) ;
ar.add(sum) ;
}
else if(str.charAt(i) == 'L' || str.charAt(n-1-i) == 'R'){
sum = sum - i + (n-i-1) ;
ar.add(sum) ;
}
}
for (int i = 0; i < ar.size(); i++) {
pw.print(ar.get(i) + " ");
}
if(ar.size() == 0){
for (int i = 0; i < n; i++) {
pw.print(sum+ " ");
}
pw.println();
continue;
}
int an = ar.size() ;
for (int i = an; i < n; i++) {
pw.print(ar.get(an-1) + " ");
}
pw.println();
}
pw.close();
}
public static int funcSort(int[] inputList)
{
int arr[] = new int[inputList.length +1 ] ;
arr[0] = 0 ;
for (int i = 0; i < inputList.length; i++) {
arr[i+1] = arr[i] + inputList[i] ;
}
for (int i = 0; i < arr.length; i++) {
out.println("ch " + arr[i]);
}
int minAns = Integer.MAX_VALUE ;
for (int i = 1; i < arr.length-1 ; i++) {
out.println( (arr[i] / i) + " " + ((arr[arr.length-1] -arr[i]) / (arr.length-i-1)) + " " + (arr[arr.length-1] -arr[i]) +" " + (arr.length-1-i) );
if(Math.abs( (arr[i] / i) - ((arr[arr.length-1] -arr[i]) / (arr.length-i-1)) ) < minAns){
minAns = Math.abs( (arr[i] / i) - ((arr[arr.length-1] -arr[i]) / (arr.length-i-1)) );
}
LinkedList<Integer> ls = new LinkedList<>();
}
return minAns ;
}
/*
Note :
Try using sorting , when approach is of O(N^2)
As sorting takes : O( N Log N )
*/
/*
len : Length of Array / inputs
num : key to map
//Putting values in map <Integer , ArrayList>
for (int i = 0; i < len; i++) {
num =sc.nextInt();
if(!mp.containsKey(num)){
mp.put(num ,new ArrayList<>());
}
}
//getting size of ArrayList
for(Integer key : mp.keySet()){
maxValue = max(maxValue , mp.get(key).size());
}
*/
/*
Sieve
boolean[] bool = new boolean[num];
for (int i = 0; i< bool.length; i++) {
bool[i] = true;
}
for (int i = 2; i< Math.sqrt(num); i++) {
if(bool[i] == true) {
for(int j = (i*i); j<num; j = j+i) {
bool[j] = false;
}
}
}
*/
static long exponentiation(long base, long exp)
{
long N = 1000000007 ;
if (exp == 0)
return 1;
if (exp == 1)
return base % N;
long t = exponentiation(base, exp / 2);
t = (t * t) % N;
// if exponent is even value
if (exp % 2 == 0)
return t;
// if exponent is odd value
else
return ((base % N) * t) % N;
}
public static int BinarySearch (long ar[] , int start , int end , long key , boolean lower){
int mid ;
boolean found = false;
int ans = 0;
if(lower){
while(start <= end){
mid = start + (end-start) / 2;
if(ar[mid] < key ){
if(!found){
ans = mid;
}
start = mid+1;
}
else{
if(ar[mid] == key){
found = true;
ans = mid ;
}
end = mid-1;
}
}
}
else{
while(start <= end){
mid = start + (end-start) / 2;
if(ar[mid] <= key ){
if(ar[mid] == key){
found = true;
ans = mid;
}
start = mid+1;
}
else{
if(!found){
ans = mid;
}
end = mid-1;
}
}
}
return ans;
}
public static int BinarySearchArrayList (ArrayList<Long> ar , int start , int end , long key , boolean lower){
int mid ;
boolean found = false;
int ans = -1;
if(lower){
while(start <= end){
mid = start + (end-start) / 2;
if(ar.get(mid) < key ){
if(!found){
ans = mid;
}
start = mid+1;
}
else{
if(ar.get(mid) == key){
found = true;
ans = mid ;
}
end = mid-1;
}
}
}
else{
while(start <= end){
mid = start + (end-start) / 2;
if(ar.get(mid) <= key ){
if(ar.get(mid) == key){
found = true;
ans = mid;
}
start = mid+1;
}
else{
if(!found){
ans = mid;
}
end = mid-1;
}
}
}
return ans;
}
public static String reverseString(String str){
StringBuilder input1 = new StringBuilder();
// append a string into StringBuilder input1
input1.append(str);
// reverse StringBuilder input1
input1.reverse();
return input1+"";
}
public static void sort(int[] arr){
//because Arrays.sort() uses quick sort , Worst Complexity : o(N^2)
ArrayList <Integer> ls = new ArrayList<>();
for(int x:arr){
ls.add(x);
}
Collections.sort(ls);
//Collections.sort(arrayList) : Uses Merge Sort : Complexity O(NlogN)
for(int i = 0 ; i < arr.length ; i++){
arr[i] = ls.get(i);
}
}
public static long gcd(long a , long b){
if(a>b){
a = (a+b) - (a = b);
}
if(a == 0L){
return b;
}
return gcd(b%a ,a);
}
public static boolean isPrime(long n){
if(n < 2){
return false;
}
if(n == 2 || n == 3){
return true;
}
if(n % 2 == 0 || n % 3 == 0){
return false;
}
long sq = (long)Math.sqrt(n) + 1;
for(long i = 5L; i <=sq ; i=i+6){
if(n % i == 0 || n % (i+2) == 0){
return false;
}
}
return true;
}
public static class Pair implements Comparable<Pair> {
long first ;
long second;
Pair(long fst , long scnd){
first = fst;
second = scnd;
}
public int compareTo(Pair obj){
if(this.first > obj.first){
return 1;
}
else if(this.first < obj.first){
return -1;
}
return 0;
}
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
static class 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 17
|
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
|
7879a7149dcb929000c5c86db20c9d0b
|
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
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
String s = sc.next();
long ssf = 0;
long[]a = new long[n];
for(int i=0;i<n;i++){
char ch = s.charAt(i);
if(ch == 'L'){
ssf += i;
a[i] = (n-1-i)-i;
}else{
ssf += n-1-i;
a[i] = i-(n-1-i);
}
}
Arrays.sort(a);
for(int k=n-1;k>=0;k--){
if(a[k] > 0){
ssf += a[k];
}
System.out.print(ssf+" ");
}
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 17
|
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
|
e36efff470346a31e153905308f7782b
|
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 Solution {
public static void main(String[] args) {
FastReader in = new FastReader();
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt(); // the length of the line
String s = in.nextLine();
int lo=0;
int hi=n-1;
Queue<Integer> q1=new ArrayDeque<>();
while(lo<hi)
{
if(s.charAt(lo)=='L')
q1.add(lo);
if(s.charAt(hi)=='R')
q1.add(hi);
lo++;
hi--;
}
long ans=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='L')
ans+=i;
else
ans+=n-i-1;
}
int p=q1.size();
while(!q1.isEmpty())
{
int ind=q1.poll();
if(s.charAt(ind)=='L') {
ans -= ind;
ans+=n-ind-1;
}
else {
ans-=n-ind-1;
ans+=ind;
}
System.out.print(ans+" ");
}
for(int i=p;i<n;i++)
System.out.print(ans+" ");
System.out.println();
} // end test case while
} // end main
static class FastReader {
private final BufferedReader br;
private 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;
}
} // end fast reader
} // end class
|
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 17
|
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
|
26f4e64833d39026b78bd51ed767a282
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
//package codeforce.div4;
import java.io.*;
import java.util.*;
public class Main {
static int t,n,arr[];
static long sum,record[],answer[];
public static void main(String[] args) throws IOException{
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
t = Integer.parseInt(br.readLine());
for(int i = 0; i<t; i++){
n = Integer.parseInt(br.readLine());
sum = 0;
arr = new int[n];
record = new long[n];
answer = new long[n+1];
String str = br.readLine();
for(int j = 0; j<n; j++){
int cur = str.charAt(j);
if(cur=='R'){
arr[j] = 1;
sum+=n-1-j;
}
else sum+=j;
}
record[0] = sum;
int cnt = 0;
for(int j= 0; j<n; j++){
if(arr[j]==0)record[j] = n-1-2*j;
else record[j] = 2*j+1-n;
}
Arrays.sort(record);
int dir = 0;
while(dir<n&&record[dir]<=0)dir++;
int fd = 1;
answer[0] = sum;
for(int j = n-1; j>=dir; j--){
answer[fd] = answer[fd-1]+record[j];
fd++;
}
if(fd!=0)Arrays.fill(answer,fd,n+1,answer[fd-1]);
for(int j = 1; j<=n; j++){
sb.append(answer[j]).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 17
|
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
|
ed31735331fab10862c423775a7bfa82
|
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) {
int t, n;
Scanner in = new Scanner(System.in);
String s;
t = in.nextInt();
in.nextLine();
while (t-- > 0){
n = in.nextInt();
in.nextLine();
s = in.nextLine();
long[] score = func(n, s);
System.out.println(print(score));
}
}
private static String print(long[] score) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < score.length; i++) {
sb.append(score[i]);
if (i < score.length - 1) {
sb.append(" ");
}
}
return sb.toString();
}
private static long[] func(int n, String s){
List<Long> scores = new ArrayList<>();
long total = 0;
for(int i = 0; i < n; i++) {
char c = s.charAt(i);
long val = 0;
long diff = 0;
if (c == 'R') {
val = n - 1 - i;
diff = i - val;
} else {
val = i;
diff = n - 1 - i - val;
}
if (diff > 0) {
scores.add(diff);
}
total += val;
}
// System.out.println("total : " + total + ". " + scores);
long[] maxScore = new long[n];
scores.sort(Comparator.reverseOrder());
for (int i = 0; i < n; i++) {
if (scores.isEmpty()) {
maxScore[0] = total;
}
if (i < scores.size()) {
maxScore[i] = i == 0 ? total + scores.get(i) : maxScore[i - 1] + scores.get(i);
} else if (i > 0) {
maxScore[i] = maxScore[i - 1];
}
}
return maxScore;
}
}
|
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 17
|
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
|
ab43e6dc616f8960f76659edea196723
|
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 cf;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.*;
import java.util.StringTokenizer;
public class cftt {
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) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void delete_from_map(Map<Integer,Integer>h,int cur) {
if(h.get(cur)>0)h.put(cur, h.get(cur)-1);
if(h.get(cur)==0)h.remove(cur);
}
public static void debug(int mat[][])
{
for(int i=0; i<mat.length; i++) {
System.out.print(mat[i][0]+" "+mat[i][1]);
System.out.println();
}
}
static long ans=0;
static int st=0,end=0;
public static void main(String[] args) {
FastScanner r = new FastScanner();
PrintWriter out=new PrintWriter(System.out);
//System.out.println("input");
int tc=r.nextInt();
while(tc-->0)
{
ans=0;
int n=r.nextInt();
st=0;end=n-1;
String s=r.next();
//long ans=0;
char ch[]=s.toCharArray();
long res[]=new long[n];
for(int i=0; i<n; i++) {
if(ch[i]=='R')ans+=(n-i-1);
else ans+=(i);
}
//System.out.println(ans);
for(int i=0; i<n; i++) {
res[i]=help(ch,1);
}
for(int i=0; i<n; i++)System.out.print(res[i]+" ");
System.out.println();
//out.close();
}
}
public static long help(char ch[],int k)
{
int n=ch.length;
for(int i=st,j=end; i<=j&&k>0;i++,j--)
{
if(k>0) {
if(ch[i]=='L') {
ch[i]='R';
k--;
ans-=i;
ans+=(n-i-1);
//i++;
}
}
if(k==0) {
st=i;
end=j;
break;
}
if(k>0) {
if(ch[j]=='R') {
ch[j]='L';
k--;
ans-=(n-j-1);
ans+=j;
//j--;
}
}
}
//System.out.println("ans3="+ans);
return ans;
}
}
class pair{
int x;
int y;
int cost;
public pair(int x,int y,int cost) {
this.x=x;
this.y=y;
this.cost=cost;
}
}
|
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 17
|
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
|
68afe86ba60fa147ad45a587502f97aa
|
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 CF_1722D_Line {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0) {
int n=sc.nextInt();
String s=sc.next();
long[] profit=new long[n];
long init=0;
for(int i=0;i<n;i++) {
if(s.charAt(i)=='L') {
profit[i]=n-2*i-1;
init+=i;
}else {
profit[i]=2*i-n+1;
init+=n-i-1;
}
}
Arrays.sort(profit);
for(int k=0;k<n;k++) {
if(profit[n-k-1]>0) {
init+=profit[n-k-1];
}
System.out.print(init+" ");
}
System.out.println();
t--;
}
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 17
|
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
|
fd230e3e9c4682d7c9a8e8e9c7a74890
|
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.HashMap;
import java.util.StringTokenizer;
public class Line {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException{
int T = Integer.parseInt(in.readLine());
for (int i=0;i<T;i++){
solve();
}
}
static void solve() throws IOException{
int N = Integer.parseInt(in.readLine());
String string = in.readLine();
int[] change = new int[N];
long current = 0;
for (int i=0;i<N;i++){
int forward=0;
int backward=0;
if (string.charAt(i)=='R'){
forward = N-1-i;
backward = i;
}else{
forward = i;
backward = N-1-i;
}
//System.out.println(forward+" "+backward);
current+=forward;
if (backward-forward>=0){
change[i] = backward-forward;
}
}
int count = 0;
for (int i=0;i<N/2;i++){
if (change[i]!=0){
current+=change[i];
System.out.print(current+" ");
count++;
}
if (change[N-1-i]!=0){
current+=change[N-1-i];
System.out.print(current+" ");
count++;
}
}
for (int i=count;i<N;i++){
System.out.print(current+" ");
}
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 17
|
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
|
6d012f763b5caee00d3b859281fde516
|
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.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class D_Line {
static class Pair{
long prev;
long newV;
Pair(long prev, long newV) {
this.prev = prev;
this.newV = newV;
}
}
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
InputReader sc = new InputReader(inputStream);
int t = sc.nextInt();
while (--t >= 0) {
long n = sc.nextLong();
char[] arr = sc.next().toCharArray();
long oldSum = 0;
PriorityQueue<Pair> pq = new PriorityQueue<Pair>((a, b) -> {
return (int)(b.newV - a.newV);
});
for(long i = 0 ;i<n ; i++){
long curVal = arr[(int)i] == 'L' ? i : n-i-1;
oldSum += curVal;
if(n-curVal-1 > curVal){
pq.add(new Pair(curVal, n-curVal-1));
}
}
long rem = n-pq.size();
while(pq.size() > 0){
Pair p = pq.poll();
oldSum = oldSum + p.newV - p.prev;
System.out.print(oldSum+" ");
}
while(rem > 0){
System.out.print(oldSum+" ");
rem--;
}
System.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();
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public char nextChar() {
return next().charAt(0);
}
public String nextLine() throws IOException {
return reader.readLine().trim();
}
}
}
|
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 17
|
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
|
cf13f40a5371180f37f35c50eb9f396f
|
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;
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 k = Integer.parseInt(in.readLine());
char[] chars = in.readLine().toCharArray();
Integer[] res = new Integer[k];
long cur = 0;
for (int i = 0; i < k; i++) {
if (chars[i] == 'L') {
cur += i;
res[i] = k - i - 1 - i;
} else {
cur += k - i - 1;
res[i] = i - (k - i - 1);
}
}
Arrays.sort(res, ((o1, o2) -> o2 - o1));
for (int i = 0; i < k; i++) {
cur += Math.max(res[i], 0);
out.write(String.valueOf(cur));
out.write(' ');
if (i == k - 1) out.newLine();
}
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 17
|
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
|
895925b26dd3038daf14d17a230c00d4
|
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 17
|
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
|
8ae57458e30d6cb0f3b8f78915766c75
|
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];
out.print(sum + " ");
}
if (r >= n / 2 && arr[r] == 'R'){
arr[r] = 'L';
k--;
sum += r - dis[r];
out.print(sum + " ");
}
r--;
l++;
}
for (int i = 0; i < k; i++) {
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 17
|
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
|
9f3fe9aefb1bb8f3630a6e8c0c122d16
|
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 17
|
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
|
156ffc2e7cf0748eb8214e3ac28173fc
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import static java.lang.System.out;
import static java.lang.Math.abs;
import static java.lang.Math.min;
import static java.lang.Math.max;
import static java.lang.Math.log;
import java.util.*;
import java.lang.*;
import java.io.*;
public class a_Codeforces {
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
FastWriter out = new FastWriter();
int t = sc.nextInt();
while (t-- > 0) {
long n = sc.nextLong();
String s = sc.next();
long c = 0;
ArrayList<Long> a = new ArrayList<Long>();
ArrayList<Long> b = new ArrayList<Long>();
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'R')
c += (n - i - 1);
else
c += i;
if (i < (n - 1 - i) && s.charAt(i) == 'L')
b.add((long) n - 1 - i - i);
else if (i > (n - 1 - i) && s.charAt(i) == 'R')
b.add((long) i + i + 1 - n);
}
Collections.sort(b, Collections.reverseOrder());
long ans = 0, m = b.size();
for (int i = 0; i < m; i++) {
ans += b.get(i);
a.add((long) ans + c);
}
for (long i = m; i < n; i++) {
a.add((long) ans + c);
}
for (int i = 0; i < a.size(); i++) {
out.print(a.get(i) + " ");
}
out.println("");
}
out.close();
}
/*
* int[] arr = new int[n];
* for (int i=0; i<n; i++){
* arr[i] = sc.nextInt();
* }
*/
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();
}
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();
}
}
}
|
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 17
|
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
|
e0b88cac1542eca5c1800d301943d02c
|
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 java.io.*;
import java.lang.*;
public class main1{
FastScanner in;
PrintWriter out;
public static void main(String[] arg) {
new main1().run();
}
//////////////SOLVE QUESTIONS HERE/////////////
public void solve() throws IOException {
int testcase=1;
testcase=in.nextInt();
while(testcase-- > 0)
{
int n=in.nextInt();
var 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();
}
}
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 17
|
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
|
0cfd9e047d6030a271d7017eca48f4f7
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.util.*;
import java.util.function.IntFunction;
import java.util.function.IntUnaryOperator;
import java.util.function.ToIntFunction;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Codeforces {
static void solve() {
int t = i32();
while (t-- > 0) {
int n = i32();
long total = 0;
String s = str();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == 'L') total += i;
else total += (n - 1 - i);
}
long[] a = new long[n + 1];
a[0] = total;
for (int i = 1, j = 0, k = n - 1; i <= n; i++) {
if (j > k) {
a[i] = a[i-1];
} else {
boolean found = false;
while (j <= k && !found) {
int jb = bonus(j, s, n);
int kb = bonus(k, s, n);
if (jb == 0 && kb == 0) {
j++; k--;
} else if (jb == 0) {
j++;
} else if (kb == 0) {
k--;
} else if (jb > kb) {
j++; a[i] = a[i-1] + jb;
found = true;
} else {
k--; a[i] = a[i-1] + kb;
found = true;
}
}
if (!found) {
a[i] = a[i-1];
}
}
}
long[] b = new long[n];
System.arraycopy(a, 1, b, 0, n);
print(b);
}
}
private static int bonus(int j, String s, int n) {
char c = s.charAt(j);
if (c == 'L') return Math.max(0, (n - 1 - j) - j);
else return Math.max(0, j - (n - 1 - j));
}
//####################################################################################################################
private static final Scanner _IN_ = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
private static final PrintStream _OUT_ = new PrintStream(new BufferedOutputStream(System.out));
public static void main(String[] args) { try(_IN_; _OUT_) { solve(); } }
// ARRAY #############################################################################################################
private static <T> T[] array(int n, Class<T> type, IntFunction<T> fun) {
@SuppressWarnings("unchecked") T[] array = (T[]) Array.newInstance(type, n);
for (int i = 0; i < n; i++) array[i] = fun.apply(i); return array;
}
private static int[] array(int n, int d) { int[] array = new int[n]; Arrays.fill(array, d); return array; }
private static int fst(int[] arr) { return arr[0]; }
private static int last(int[] arr) { return arr[arr.length - 1]; }
private static void swap(int[] arr, int i, int j) { int x = arr[i]; arr[i] = arr[j]; arr[j] = x; }
private static void shuffle(int[] arr) { for (int i = 0; i < arr.length; i++) swap(arr, (int) (Math.random() * arr.length), i); }
private static Stream<pair> pairs(int[] as) {
int n = as.length;
if (n < 2) return Stream.empty();
return Stream.iterate(new pair(0, 1), it -> it.fst < n - 1,
it -> it.snd + 1 == n ? new pair(it.fst + 1, it.fst + 2) : new pair(it.fst, it.snd + 1));
}
private static Stream<pair> cartesian(int m, int n) {
return Stream.iterate(new pair(0, 0), it -> it.fst < m,
it -> it.snd + 1 == n ? new pair(it.fst + 1, 0) : new pair(it.fst, it.snd + 1));
}
// DS ################################################################################################################
private static class pair {
final int fst, snd;
pair(int fst, int snd) { this.fst = fst; this.snd = snd; }
public String toString() { return fst + " " + snd; }
boolean neq(pair p) { return fst != p.fst || snd != p.snd; }
}
private static class triple {
final int fst, snd, thd;
triple(int fst, int snd, int thd) { this.fst = fst; this.snd = snd; this.thd = thd; }
public String toString() { return fst + " " + snd + " " + thd; }
}
// GEOMETRY ##########################################################################################################
private static double dist(int x1, int y1, int x2, int y2) { return Math.sqrt(Math.pow(x1-x2, 2) + Math.pow(y1-y2, 2)); }
// INPUT #############################################################################################################
private static int i32() { return _IN_.nextInt(); }
private static int[] i32(int n) { return IntStream.generate(Codeforces::i32).limit(n).toArray(); }
private static String str() { return _IN_.next(); }
private static String[] str(int n) { return Stream.generate(Codeforces::str).limit(n).toArray(String[]::new); }
// ITERATION #########################################################################################################
private static IntStream iter(int[] arr) { return Arrays.stream(arr); }
private static IntStream iter(int l, int r) { return IntStream.range(l, r); }
private static IntStream iter(int l, IntUnaryOperator next) { return IntStream.iterate(l, next); }
private static class Window<T> {
T[] arr; int i, sz;
Window(T[] arr, int sz) { this.arr = arr; this.sz = sz; }
T get(int j) { return arr[i + j]; }
T fst() { return arr[i]; }
T snd() { return arr[i + 1]; }
boolean hasNext() { return i + sz <= arr.length; }
public Window<T> next() { i++; return this; }
}
private static class IntWindow {
int[] arr; int i, sz;
IntWindow(int[] arr, int sz) { this.arr = arr; this.sz = sz; }
int get(int j) { return arr[i + j]; }
int fst() { return arr[i]; }
int snd() { return arr[i + 1]; }
boolean hasNext() { return i + sz <= arr.length; }
public IntWindow next() { i++; return this; }
}
private static <T> Stream<Window<T>> windows(T[] arr, int sz) {
if (sz > arr.length) return Stream.empty();
return Stream.iterate(new Window<>(arr, sz), Window::hasNext, Window::next);
}
private static Stream<IntWindow> windows(int[] arr, int sz) {
if (sz > arr.length) return Stream.empty();
return Stream.iterate(new IntWindow(arr, sz), IntWindow::hasNext, IntWindow::next);
}
// NUMBER ############################################################################################################
private static final int INF = Integer.MAX_VALUE;
private static int sum(int[] xs) { return iter(xs).sum(); }
private static int sum(int[] xs, int l) { return Arrays.stream(xs).skip(l).sum(); }
private static int min(int a, int b) { return Math.min(a, b); }
private static int abs(int x) { return Math.abs(x); }
// OUTPUT ############################################################################################################
private static void print(Object o) { _OUT_.println(o); }
private static void print(int[] xs) { for (int x : xs) _OUT_.print(x + " "); _OUT_.println(); }
private static void print(long[] xs) { for (long x : xs) _OUT_.print(x + " "); _OUT_.println(); }
// SORT ##############################################################################################################
private static <T> void sort(T[] arr, ToIntFunction<T> comp) { Arrays.sort(arr, Comparator.comparingInt(comp)); }
private static void sort(int[] arr) { shuffle(arr); Arrays.sort(arr); }
// STRING ############################################################################################################
private static class Str {
final String s; final int l, r;
Str(String s, int l, int r) { this.s = s; this.l = l; this.r = r; }
int len() { return r - l; }
boolean eq(Str that) {
if (that.len() != this.len()) return false;
for (int i = 0, len = len(); i < len; i++) if (s.charAt(l + i) != that.s.charAt(that.l + i)) return false;
return true;
}
}
private static String reverse(String s) { return new StringBuilder(s).reverse().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 17
|
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
|
7e8d59b95d995df0e8a2f516a3a8fc5d
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class D {
private static Scan sc = new Scan();
public static void main(String[] args) {
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
solve();
}
}
private static void solve() {
int n = sc.nextInt();
String s = sc.next();
double m = (double) (n - 1) / 2;
long p = 0;
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L') {
p += i;
if (i < m) {
pq.add(n - 1 - i - i);
}
} else {
p += n - i - 1;
if (i > m) {
pq.add(i - (n - 1 - i));
}
}
}
for (int i = 0; i < n; i++) {
if (!pq.isEmpty()) {
p += pq.poll();
}
System.out.print(p + " ");
}
System.out.println();
}
private static class Scan {
BufferedReader br;
StringTokenizer st;
public Scan() {
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());
}
String nextLine() {
String line = "";
try {
line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
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 17
|
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
|
2a808a2dc64b79562d37940eda309ef6
|
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 class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
private static final int modulo = 1000000007;
private static int evenCount = 0;
private static int oddCount = 0;
private static long arraySum = 0L;
private static long onesCountInBinary = 0L;
public static Scanner scan = new Scanner(System.in);
public static void main(String[] args){
int testCase = scan.nextInt();
for (int vedant = 0; vedant < testCase; vedant++) {
solve();
}
// out.println(5);
}
private static void solve() {
// ******************** S O L V E M E T H O D *****************
// ************* M A I N L O G I C G O E S H E R E **********
int size = scan.nextInt();
String str = scan.next();
long totalSum = 0L;
for (int i = 0; i < size; i++) {
if (str.charAt(i) == 'L') {
totalSum += i;
} else {
totalSum += size - i - 1;
}
}
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
if (str.charAt(i) == 'R') {
arr[i] += i - (size - i - 1);
} else {
arr[i] += (size - i - 1) - i;
}
}
Arrays.sort(arr);
for (int i = size - 1; i >= 0; i--) {
if (arr[i] > 0) {
totalSum += arr[i];
}
System.out.print(totalSum + " ");
}
System.out.println();
// **************************** S O L U T I O N E N D S H E R E ****************************
}
private static String readBinaryString() {
String binary = scan.next();
onesCountInBinary = getOnesCountInBinary(binary);
return binary;
}
private static long getOnesCountInBinary(String binary) {
onesCountInBinary = 0L;
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt(i) == '1') {
onesCountInBinary++;
}
}
return onesCountInBinary;
}
private static int lowerBound(int[] arr, int target) {
int starting = 0;
int ending = arr.length - 1;
int answer = -1;
while (starting <= ending) {
int mid = starting + ((ending - starting) / 2);
if (arr[mid] == target) {
ending = mid - 1;
answer = mid;
} else if (arr[mid] < target) {
starting = mid + 1;
} else {
ending = mid - 1;
}
}
return answer;
}
public int upperBound(int[] arr, int target) {
int starting = 0;
int ending = arr.length - 1;
int answer = -1;
while (starting <= ending) {
int mid = starting + ((ending - starting) / 2);
if (arr[mid] == target) {
answer = mid;
starting = mid + 1;
} else if (arr[mid] < target) {
starting = mid + 1;
} else {
ending = mid - 1;
}
}
return answer;
}
private static void printArray(long[] arr) {
StringBuilder output = new StringBuilder();
for (long j : arr) {
output.append(j).append(" ");
}
System.out.println(output);
}
private static ArrayList<Integer> readArrayList(int size) {
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < size; i++) {
arr.add(scan.nextInt());
if ((arr.get(i) & 1) == 0) {
evenCount++;
} else {
oddCount++;
}
arraySum += arr.get(i);
}
return arr;
}
private static void printArray(int[] arr) {
StringBuilder output = new StringBuilder();
for (int j : arr) {
output.append(j).append(" ");
}
System.out.println(output);
}
private static void printYES() {
System.out.println("YES");
}
private static void printNO() {
System.out.println("NO");
}
private static long getMaximumFromList(List<Long> arr) {
long max = Long.MIN_VALUE;
for (Long aLong : arr) {
max = Math.max(max, aLong);
}
return max;
}
public static int binarySearch(int[] arr, int target) {
int starting = 0;
int ending = arr.length - 1;
while (starting < ending) {
int mid = starting + (ending - starting) / 2;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] > target) {
ending = mid - 1;
} else {
starting = mid + 1;
}
}
return -1;
}
public static int binarySearch(long[] arr, long target) {
int starting = 0;
int ending = arr.length - 1;
while (starting < ending) {
int mid = starting + (ending - starting) / 2;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] > target) {
ending = mid - 1;
} else {
starting = mid + 1;
}
}
return -1;
}
public 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 long gcd(long a, long 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);
}
private static int[] readIntArray(int size) {
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = scan.nextInt();
if ((arr[i] & 1) == 1) {
oddCount++;
} else {
evenCount++;
}
arraySum += arr[i];
}
return arr;
}
private static long[] readLongArray(int size) {
long[] arr = new long[size];
for (int i = 0; i < size; i++) {
arr[i] = scan.nextLong();
if ((arr[i] & 1) == 1) {
oddCount++;
} else {
evenCount++;
}
arraySum += arr[i];
}
return arr;
}
private static boolean isVowel(char charAt) {
charAt = Character.toLowerCase(charAt);
return charAt == 'a' ||
charAt == 'e' ||
charAt == 'i' ||
charAt == 'o' ||
charAt == 'u';
}
private static long binaryToDecimal(String binary) {
int expo = 0;
int index = binary.length() - 1;
long answer = 0;
while (index >= 0) {
answer += Math.pow(2, expo) * Integer.parseInt(String.valueOf(binary.charAt(index)));
index--;
expo++;
}
return answer;
}
public static boolean isPrime(int n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
private static long getFrequency(List<Long> array, long target) {
long count = 0;
for (long element : array) {
if (element == target) {
count++;
}
}
return count;
}
private static int binarySearch(ArrayList<Integer> arr, int target) {
int starting = 0;
int ending = arr.size() - 1;
int mid;
while (starting <= ending) {
mid = starting + (ending - starting) / 2;
if (arr.get(mid) == target) {
return mid;
}
if (arr.get(mid) > target) {
ending = mid - 1;
} else {
starting = mid + 1;
}
}
return -1;
}
private static int binarySearch(ArrayList<Long> arr, long target) {
int starting = 0;
int ending = arr.size() - 1;
int mid;
while (starting <= ending) {
mid = starting + (ending - starting) / 2;
if (arr.get(mid) == target) {
return mid;
}
if (arr.get(mid) > target) {
ending = mid - 1;
} else {
starting = mid + 1;
}
}
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 17
|
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
|
deeacb78b1eaa1c330adc1f132676389
|
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 reverseArr(int[] arr){
int i = 0 ;
int j = arr.length -1 ;
while(i <= j){
int temp = arr[i] ;
arr[i] = arr[j] ;
arr[j] = temp ;
i++ ;
j-- ;
}
}
public static void Dline(String str , int n) {
long ans = 0 ;
for(int i=0 ; i< str.length() ; i++){
if(str.charAt(i) == 'L'){
ans = ans + i ;
}else{
ans = ans + (n-i-1) ;
}
}
int ch[] = new int[n] ;
for(int i=0 ; i< n ; i++){
if(str.charAt(i) == 'L'){
ch[i] = (n-i-1) -i ;
}else{
ch[i] = i - (n-i-1) ;
}
}
Arrays.sort(ch) ;
reverseArr(ch) ;
for(int k=1 ; k<= n ; k++){
if(ch[k-1] > 0){
ans += ch[k-1] ;
}
System.out.print(ans+" ");
}
}
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() ;
Dline(str , n);
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 17
|
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
|
287a89b474ca0b7da48ded246de35783
|
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 CF1722D {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
String s = sc.next();
ArrayList<Integer> al = new ArrayList<>();
long[] a = new long[n];
long sum = 0;
for (int i = 0; i < s.length(); i++) {
int inc = 0;
if (s.charAt(i) == 'L') {
sum = sum + i;
inc = Math.max(0, (n - i - 1) - i);
}
else {
sum = sum + n - i - 1;
inc = Math.max(0, -(n - i - 1) + i);
}
al.add(inc);
}
al.sort(new Comparator<Integer>(){
public int compare(Integer a, Integer b) {
return b - a;
}
});
for (int i = 0; i < n; i++) {
if (i == 0) {
a[i] = al.get(i);
continue;
}
a[i] = a[i-1] + (al.get(i)).longValue();
}
for (int i = 0; i < n; i++)
pr.print(sum + a[i] + " ");
pr.println();
pr.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 17
|
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
|
cd5340da5ffcf0ac0f7a66844cace631
|
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.math.*;
public class D1722 {
public static int getAns(char[] s, int index) {
if(s[index]=='R') {
return index;
}
else {
return s.length-index-1;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int l=0;l<t;l++) {
int n = sc.nextInt();
String ss = sc.next();
char[] s = ss.toCharArray();
long[] A = new long[n];
long ans = 0;
ArrayList<Integer> al_left = new ArrayList<Integer>();
ArrayList<Integer> al_right = new ArrayList<Integer>();
for(int i=0;i<n;i++) {
if(s[i]=='L') {
ans+=i;
al_left.add(i);
}
else {
ans+=n-i-1;
al_right.add(i);
}
}
int index_left = 0 , index_right = al_right.size()-1;
long left = ans , right = ans;
for(int i=0;i<n;i++) {
if(index_left<al_left.size()) {
left = left - al_left.get(index_left) + getAns(s,al_left.get(index_left));
}
if(index_right>=0) {
right = right - (n-al_right.get(index_right)-1) + getAns(s,al_right.get(index_right));
}
if(left>ans && left >right) {
A[i] = left;
index_left++;
ans = left;
}
else if(right > ans){
A[i] = right;
index_right--;
ans = right;
}
else {
A[i] = ans;
}
//System.out.println(index_left+" "+index_right+" "+left+" "+right);
left = ans ;
right = ans;
}
for(int i=0;i<n;i++) {
System.out.print(A[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 17
|
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
|
09db5d178cc3e3fac7c07468fb308abd
|
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 {
// For fast input output
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
try {
br = new BufferedReader(
new FileReader("input.txt"));
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
} catch (Exception e) {
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());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// end of fast i/o code
public static boolean isPalindrome(String str) {
int i = 0;
int j = str.length() - 1;
int flag = 0;
while (i <= j) {
if (str.charAt(i) != str.charAt(j)) {
flag = 1;
break;
}
i++;
j--;
}
return flag == 1 ? false : true;
}
public static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
class Pair {
int x1;
int x2;
public Pair(int x1, int x2) {
this.x1 = x1;
this.x2 = x2;
}
}
static void solve() {
}
public static void main(String[] args) {
FastReader fs = new FastReader();
PrintWriter out = new PrintWriter(System.out);
// solve();
int T =fs.nextInt();
while(T-->0){
int n =fs.nextInt();
String s=fs.next();
int nums[]=new int[n];
int temp[]=new int [n/2];
// put in temp
for(int i =0;i<n/2;i++)
temp[i]=n-i-1;
// for(int i =0;i<n/2;i++){
// System.out.print(temp[i]+" ");
// }
// System.out.println("-----------");
long sum=0;
for(int i =0;i<n;i++){
if(s.charAt(i)=='L')
nums[i]=i;
else
nums[i]=n-i-1;
sum+=nums[i];
}
int i =0,j=n-1;
for(int k =0;k<n;k++){
for(;i<j;i++,j--){
if(nums[i]<temp[i]){
sum+=(temp[i]-nums[i]);
nums[i]=temp[i];
break;
}
else if (nums[j]<temp[i]){
sum+=(temp[i]-nums[j]);
nums[j]=temp[i];
break;
}
}
out.print(sum+" ");
}
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 17
|
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
|
bcd5b3b6dee13e7d5286709ef4521b39
|
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.lang.*;
public class main{
static Scanner sc = new Scanner(System.in);
public static void Solve(){
int n = sc.nextInt();
String s = sc.next();
int[] res = new int[n];
long ans = 0;
for(int i = 0; i < n; ++ i) {
char x = s.charAt(i);
if(x == 'L') {
res[i] = n - 2 * i - 1;
ans += i;
}
else {
res[i] = i - (n - i - 1);
ans += n - i - 1;
}
}
Arrays.sort(res);
for(int i = n - 1; i >= 0; -- i){
if(res[i] > 0) ans += res[i];
System.out.print(ans + " ");
}
System.out.println();
}
public static void main(String[] argv){
int t = sc.nextInt();
while(t -- > 0){
Solve();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn the first test case: $$$k=1$$$: change the direction of $$$1$$$ person to make the line RLR. The total value is $$$2+1+0=3$$$. $$$k=2$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. $$$k=3$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. Note that you have to change the direction of at most $$$k$$$ people. In the second test case, it is optimal to only change the direction of the first person for all $$$k$$$ from $$$1$$$ to $$$5$$$ (that is, make the line RRRLL).
|
Java 17
|
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
|
bc950cb9fb87df31135f87014882feb6
|
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 Line
{
public static void process()throws IOException
{
int n = ni();
char[] ch = nnch();
int[] a = new int[n];
long total = 0L;
for(int i=0;i<n;i++){
if(ch[i] == 'L'){
a[i] = (n-i-1)-i;
total += i;
}else{
a[i] = i-(n-i-1);
total += n-i-1;
}
}
Arrays.sort(a);
for(int i=0;i<n;i++){
if(a[n-i-1] > 0 ) total+= a[n-i-1];
p(total+" ");
}
pn("");
}
static AnotherReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);}
else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");}
long s = System.currentTimeMillis();
int t=1;
t=sc.nextInt();
//int k = t;
while(t-->0) {/*out.print("Case #"+ (k-t) + ": ");*/process();}
out.flush();
System.err.println(System.currentTimeMillis()-s+"ms");
out.close();
}
static int ni()throws IOException{return sc.nextInt();}
static long nl()throws IOException{return sc.nextLong();}
static double nd()throws IOException{return sc.nextDouble();}
static String nln()throws IOException{return sc.nextLine();}
static String nn()throws IOException{return sc.next();}
static char[] nnch()throws IOException{return sc.next().toCharArray();}
static int[] nia(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;}
static long[] nla(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
static <T>void p(T a){out.print(a);}
static <T>void pn(T a){out.println(a);}
static int gcd(int a, int b) {if (b == 0) return a;return gcd(b, a%b);}
static int lcm(int a ,int b){return (a*b)/gcd(a,b);}
static void swap(char[] a,int i,int j){char c=a[i];a[i]=a[j];a[j]=c;}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static class AnotherReader{BufferedReader br; StringTokenizer st;
AnotherReader()throws FileNotFoundException{
br=new BufferedReader(new InputStreamReader(System.in));}
AnotherReader(int a)throws FileNotFoundException{
br = new BufferedReader(new FileReader("input.txt"));}
String next()throws IOException{
while (st == null || !st.hasMoreElements()) {try{
st = new StringTokenizer(br.readLine());}
catch (IOException e){ e.printStackTrace(); }}
return st.nextToken(); } int nextInt() throws IOException{
return Integer.parseInt(next());}
long nextLong() throws IOException
{return Long.parseLong(next());}
double nextDouble()throws IOException { return Double.parseDouble(next()); }
String nextLine() throws IOException{ String str = ""; 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 17
|
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
|
f7dbfeb2913e96524c7d0a6ae2df3eff
|
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.Arrays;
public class problemD {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
String s = sc.next();
char[] a = s.toCharArray();
int[] f = new int[n];
long sum = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 'L') {
f[i] = i - (n - 1 - i);
} else
f[i] = ((n - 1 - i) - i);
if (a[i] == 'L')
sum += i;
else
sum += (n - 1 - i);
}
Arrays.sort(f);
for (int i = 0; i < n; i++) {
if (f[i] < 0) {
sum += Math.abs(f[i]);
}
System.out.print(sum + " ");
}
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 17
|
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
|
4ae1f3cac39fe1b4f4b993987200b8ff
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int tt = 1;
tt = fs.nextInt();
while (tt-- > 0) {
int n = fs.nextInt();
String s = fs.next();
long total = 0;
for (int i = 0; i < n; ++i) {
char ch = s.charAt(i);
if (ch == 'L') total += i;
else total += n - i - 1;
}
long[] ans = new long[n];
int l = 0, r = n - 1, m = n / 2;
for (int i = 0; i < n; ++i) {
while (l < m && s.charAt(l) == 'R') ++l;
while (r >= m && s.charAt(r) == 'L') --r;
if (l == m && r == m - 1) {
ans[i] = total;
continue;
}
int leftNewLen = n - l - 1, rightNewLen = r;
if (leftNewLen > rightNewLen) {
total -= l++;
total += leftNewLen;
} else {
total -= n - r - 1;
total += rightNewLen;
--r;
}
ans[i] = total;
}
for (long val : ans) {
System.out.print(val + " ");
}
System.out.println();
}
}
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 17
|
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
|
80a36379c88029c987dbedd44d0f868c
|
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 CodeForces
{
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
int t=scn.nextInt();
StringBuilder sb=new StringBuilder();
while(t-->0)
{
int n=scn.nextInt();
String str=scn.next();
int[] arr=new int[n];
Long og=0L;
for(int i=0;i<n;i++)
{
if(str.charAt(i)=='L')
{
arr[i]=(n-1-i)-(i);
og+=i;
}
else
{
arr[i]=(i)-(n-i-1);
og+=n-i-1;
}
}
Arrays.sort(arr);
Long ans=0L;
int a=n+1;
for(int k=1;k<=n;k++)
{
if(arr[n-k]>0) {ans+=arr[n-k];
sb.append(ans+og+" ");}
else {a=k;break;}
}
for(int j=a;j<=n;j++) sb.append(ans+og+" ");
sb.append("\n");
}
System.out.print(sb);
}
}
/*
1
200000
RRRRLLRRRLRRRLLLRRRRRRLLRRLLRRLRLLLLLLLLRRRRRLLLLLRLLRLLLRRRLRRRRRRLLLLLLRRLRLLRLLLLRRLRRLLLRRRRLRRRLLRRRLRLRLLRRLRLRRRLRLRLLLRRRRRRRRRLLLLLRRLRRLLLRLRRRLRLRLLRRRRLLRRRRLLRLLRLLLRLRRRRLRRRRRLRRRLLRLRRLRRRLRRLRRRRRLLLLRRRRLLLRLLRRRRRLLRLRLLRLRRLLRRLRLRRRLLRRRRLLRLRLRLLRRLLLLRRRRRLLRLRLRLLLLRRLLRRLLLRLRRLLLRRLLRRLLRLLRRLRRLRRLLLRRLRLRRRLRLLRLLLRLLLRRLLLRLRRRLRLRLLLRRRLRLLRRLRRLRRLLRLLRLRLRRLLRRRLLRRRRRLLRRLRLRRLLLLRRLRRLRRLRRRRRLLLRRRLLLRLLRLRRLLRRLLLRLLRLLLRLRLRLLRRRLRLRRLRLLRRLRLRLLLRLLLLRLRLLLL...
*/
|
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 17
|
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
|
c79677ecfa5232c65fb0fca6516befa8
|
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[]) {
// System.out.println("Hello World!");
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-- >0){
int n;
n=sc.nextInt();
String arr=sc.next();
long max_sofar = 0;
for(int i=0;i<n;i++){
if(arr.charAt(i)=='L'){
max_sofar+=i;
}else{
max_sofar+=(n-i-1);
}
}
int i=0,j=n-1;
long[] counts = new long[n+1];
counts[0]=max_sofar;
int count=0;
long total=max_sofar;
while(i<=j){
if(arr.charAt(i)=='L'){
count++;
counts[count] = total-i+(n-i-1);
total = counts[count];
}if(arr.charAt(j)=='R'){
count++;
counts[count] = total-(n-j-1)+j;
total = counts[count];
}
i++;
j--;
}
/* for(int x=0;x<=n;x++){
System.out.print(counts[x]+" ");
}
System.out.println("");*/
int c=1;
while(c<=count){
System.out.print(counts[c]+" ");
c++;
}
while(c<=n){
System.out.print(counts[count]+" ");
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 17
|
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
|
435e4de9386ee1a5d324954cc6484561
|
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 Line{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int i = 0; i < t; i++){
int n= s.nextInt();
String r = s.next();
// System.out.println(r);
long current_score = 0;
ArrayList<Node> arr = new ArrayList<Node>();
for(int j = 0; j < r.length(); j++){
arr.add(new Node(j, r.charAt(j), n));
}
Iterator<Node> k = arr.listIterator();
while(k.hasNext()){
Node tmp = k.next();
current_score += tmp.curr_score;
}
Collections.sort(arr, new SortByChangeScore());
Iterator<Node> m = arr.listIterator();
while(m.hasNext()){
Node tmp = m.next();
current_score -= tmp.curr_score;
current_score += Math.max(tmp.curr_score, tmp.change_score);
System.out.printf("%d ", current_score);
// System.out.printf("(%d, %d) ", tmp.ind, tmp.change_score);
}
System.out.printf("\n");
// System.out.println(current_score);
}
s.close();
}
}
class SortByChangeScore implements Comparator<Node>{
public int compare(Node a, Node b){
return b.change_score - a.change_score;
}
}
class Node{
int ind;
char symbol;
int curr_score;
int change_score;
Node(int ind, char symbol, int n){
this.ind = ind;
this.symbol = symbol;
this.curr_score = score_of(ind, symbol, n);
if(symbol == 'L'){
if(ind <= (int)((n-1)/2)){
this.change_score = score_of(ind, 'R', n);
}
else{
this.change_score = 0;
}
}
else{
if(ind > (int)((n-1)/2)){
this.change_score = score_of(ind, 'L', n);
}
else{
this.change_score = 0;
}
}
}
private static int score_of(int ind, char ch, int n){
if(ch == 'L'){
return ind;
}
else{
return n - ind - 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 17
|
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
|
3ca119db44dbcd047bfde3e4d5d3fc6a
|
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();
String s = sc.next();
Integer [] a = new Integer[n];
long sum = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'L'){
sum += i;
a[i] = (n - i - 1) - i;
}
else {
sum += n - i - 1;
a[i] = i - (n - i - 1);
}
}
Arrays.sort(a, (o1, o2) -> o2 - o1);
for (int i = 0; i < n; i++) {
if (a[i] <= 0){
for (int j = i; j < n; j++) System.out.print(sum + " ");
break;
}
sum += a[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 17
|
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
|
a1a85c249be94cf19ce6fd511e3c7139
|
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.StringTokenizer;
public class D {
final private static FastReader reader = new FastReader();
final private static PrintWriter writer = new PrintWriter(System.out);
public static void main(String[] args) {
//solveTestCase();
runTestCases(false);
writer.flush();
}
public static void solveTestCase() {
int n = reader.nextInt();
String input = reader.nextLine();
long value = 0;
for (int i = 0; i < n; i++) {
value += (input.charAt(i) == 'L') ? i : (n - i - 1);
}
int halfN = n / 2;
int outputCount = n;
for (int i = 0; i < halfN; i++) {
if (input.charAt(i) == 'L') {
value += n - i - i - 1;
writer.print(value);
writer.print(' ');
outputCount--;
}
if (input.charAt(n-i-1) == 'R') {
value += n - i - i - 1;
writer.print(value);
writer.print(' ');
outputCount--;
}
}
while(outputCount-- > 0) {
writer.print(value);
writer.print(' ');
}
writer.println();
}
private static void runTestCases(boolean forGoogle) {
int T = reader.nextInt();
final String cases = "Case #", colon = ": ";
for (int _t = 0; _t < T; _t++){
if(forGoogle) {
writer.print(cases);
writer.print(_t);
writer.print(colon);
}
solveTestCase();
}
}
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;
}
}
}
|
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 17
|
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
|
8eec2889515f0ad86ea311d57f4c18bc
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import java.util.Scanner;
public class PLSPLS {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
String s = sc.next();
long mod = 100000000;
long add = 0;
long[] ans = new long[n];
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L') {
add += i;
} else {
add += n - i - 1;
}
}
int i = 0;
int j = n - 1;
int c = 0;
while (i < j) {
if (s.charAt(i) == 'L') {
add -= i;
add += n - i - 1;
ans[c] = add;
c++;
}
i++;
if (s.charAt(j) == 'R') {
add -= n - j - 1;
add += j;
ans[c] = add;
c++;
}
j--;
}
for (int m = c; m < n; m++) {
ans[m] = add;
}
for (int g = 0; g < n; g++) {
System.out.print(ans[g] + " ");
}
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 17
|
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
|
264b94ed063eb02302d163f5e77980b8
|
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 javax.swing.Painter;
public class sneh{
static PrintWriter out;
static Kioken sc;
public static void main(String[] args) throws FileNotFoundException {
boolean t = true;
boolean f = false;
if (f) {
out = new PrintWriter("output.txt");
sc = new Kioken("input.txt");
} else {
out = new PrintWriter((System.out));
sc = new Kioken();
}
int tt = 1;
tt = sc.nextInt();
while (tt-- > 0) {
solve();
}
out.flush();
out.close();
}
public static void solve() {
int n=sc.nextInt();
String s=sc.nextLine();
// System.out.println(n);
int st=0;
int en=n-1;
Long ans=0L;
for(int i=0;i<n;i++){
if(s.charAt(i)=='R'){
ans+=(n-i-1);
}
else{
ans+=i;
}
}
Vector<Long> v= new Vector<Long>();
while(st<=en){
if(st<n-1-en){
if(s.charAt(st)=='L'){
ans-=st;
ans+=(n-st-1);
v.add(ans);
}
st++;
}
else{
if(s.charAt(en)=='R'){
ans-=(n-en-1);
ans+=en;
v.add(ans);
}
en--;
}
}
if(v.size()>0){
ans=v.get(v.size()-1);
}
while(v.size()<n){
v.add(ans);
}
for(int i=0;i<v.size();i++){
System.out.print(v.get(i)+ " ");
}
System.out.println();
}
public static long gcd(long a, long b) {
while (b != 0) {
long rem = a % b;
a = b;
b = rem;
}
return a;
}
static long MOD = 1000000007;
static void reverseSort(int[] arr){List<Integer> list = new ArrayList<>();for (int i=0; i<arr.length; i++){list.add(arr[i]);}Collections.sort(list, Collections.reverseOrder());for (int i = 0; i < arr.length; i++){arr[i] = list.get(i);}}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void sort(long[] a){
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class Kioken {
// FileInputStream br = new FileInputStream("input.txt");
BufferedReader br;
StringTokenizer st;
Kioken(String filename) {
try {
FileReader fr = new FileReader(filename);
br = new BufferedReader(fr);
st = new StringTokenizer("");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
Kioken() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean hasNext() {
String next = null;
try {
next = br.readLine();
} catch (Exception e) {
}
if (next == null || next.length() == 0) {
return false;
}
st = new StringTokenizer(next);
return true;
}
}
}
|
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 17
|
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
|
1c4e94f190f3d6bb0cc320045df830a9
|
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 in=new Scanner(System.in);
int T=in.nextInt();
while(T-->0)
{
int n=in.nextInt();
String s=in.next();
int lo=0;
int hi=n-1;
Queue<Integer> q1=new ArrayDeque<>();
while(lo<hi)
{
if(s.charAt(lo)=='L')
q1.add(lo);
if(s.charAt(hi)=='R')
q1.add(hi);
lo++;
hi--;
}
long ans=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='L')
ans+=i;
else
ans+=n-i-1;
}
int p=q1.size();
while(!q1.isEmpty())
{
int ind=q1.poll();
if(s.charAt(ind)=='L') {
ans -= ind;
ans+=n-ind-1;
}
else {
ans-=n-ind-1;
ans+=ind;
}
System.out.print(ans+" ");
}
for(int i=p;i<n;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 17
|
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
|
b3888dda0c447c4f12b95dc9f5f39f52
|
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
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package Codeforces;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author Vinay Jain
*/
public class cf_817_d {
static class Solution {
public void solve(Scanner sc){
int n = sc.nextInt();
String str = sc.next();
long or[] = new long[n];
long ne[] = new long[n];
long sum = 0;
for(int i = 0 ; i < n ; i++){
if(str.charAt(i) == 'R'){
or[i] = n - i - 1;
ne[i] = i;
}
else{
or[i] = i;
ne[i] = n - i - 1;
}
sum += or[i];
}
long ar[] = new long[n];
int k = 0;
for(int i = 0 ; i < n ; i++){
if(ne[i] > or[i]){
ar[k] = ne[i] - or[i];
k++;
}
}
Arrays.sort(ar);
/*System.out.println("\n");
for(int i : or)
System.out.print(i+" ");
System.out.println("\n");
for(int i : ne)
System.out.print(i+" ");
System.out.println("\n");
for(int i : ar)
System.out.print(i+" ");
*/
for(int i = n-1 ; i >= 0 ; i--){
sum += ar[i];
System.out.print(sum+" ");
}
System.out.println();
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n-- > 0){
new Solution().solve(sc);
}
}
}
|
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 17
|
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
|
ec793e8e541f6937a89de26cdd9ac0fc
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import java.util.Scanner;
public class PLSPLS {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
String s = sc.next();
long mod = 100000000;
long add = 0;
long[] ans = new long[n];
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L') {
add += i;
} else {
add += n - i - 1;
}
}
int i = 0;
int j = n - 1;
int c = 0;
while (i < j) {
if (s.charAt(i) == 'L') {
add -= i;
add += n - i - 1;
ans[c] = add;
c++;
}
i++;
if (s.charAt(j) == 'R') {
add -= n - j - 1;
add += j;
ans[c] = add;
c++;
}
j--;
}
for (int m = c; m < n; m++) {
ans[m] = add;
}
for (int g = 0; g < n; g++) {
System.out.print(ans[g] + " ");
}
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 17
|
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
|
189a49fc516c36bcf8787c72b4d1bcb7
|
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 Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
String s = sc.next();
char[] arr = s.toCharArray();
int[] res = new int[n];
int[] left = new int[n];
int[] right = new int[n];
long sum = 0;
for(int i=0;i<n;i++) {
left[i] = i;
right[i] = n-1-i;
if(arr[i] == 'L')
res[i] = i;
else
res[i] = n-1-i;
sum += (long)res[i];
}
int leftLimit = n/2;
int rightLimit = n/2;
if(n%2 == 0) rightLimit--;
StringBuilder sb = new StringBuilder();
int i=0, j = n-1;
for(int k=1;k<=n;k++) {
while(i < leftLimit && arr[i] == 'R') i++;
while(j > rightLimit && arr[j] == 'L') j--;
// System.out.println(i+" "+j);
if(i == leftLimit && j == rightLimit) {
sb.append(sum).append(' ');
continue;
}
int dis1 = i, dis2 = n-1-j;
if(dis1 < dis2) {
sum = sum - (long)left[i] + (long)right[i];
sb.append(sum).append(' ');
i++;
}else {
sum = sum - (long)right[j] + (long)left[j];
sb.append(sum).append(' ');
j--;
}
}
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 17
|
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
|
ac2d27b5245154d223b30970b279f283
|
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 Solution {
public static void main(String[] args) throws IOException {
Reader scan = new Reader(System.in);
BufferedOutputStream b = new BufferedOutputStream(System.out);
StringBuffer sb = new StringBuffer();
int t = scan.nextInt();
// int t = 1;
while (t-->0)
{
int n = scan.nextInt();
String s = scan.next();
sb.append(solve(n, s.toCharArray())).append("\n");
}
b.write((sb.toString()).getBytes());
b.flush();
}
private static String solve(int n, char[] s) {
StringBuffer sb = new StringBuffer();
ArrayList<pair> ans = new ArrayList<>();
long total = 0;
for(int i=0;i<n;i++)
{
long val = 0;
if(s[i] == 'R')
{
val = n-i-1;
}
else {
val = i;
}
total += val;
ans.add(new pair(val, i));
}
ans.sort(new comp());
for(pair p : ans)
{
long t = 0;
if(p.idx < n/2)t = n-p.idx-1;
else { t = p.idx; }
total = total - p.val + t;
sb.append((total)).append(" ");
}
return sb.toString();
}
}
class pair {
int idx;
long val;
pair(long val, int idx)
{
this.val = val;
this.idx = idx;
}
}
class comp implements Comparator<pair>
{
public int compare(pair a, pair b)
{
return (int)(a.val-b.val);
}
}
class Reader{
private final BufferedReader reader;
private StringTokenizer tokenizer;
Reader(InputStream input)
{
this.reader = new BufferedReader(new InputStreamReader(input));
this.tokenizer = new StringTokenizer("");
}
public String next() throws IOException {
while(!tokenizer.hasMoreTokens())
{
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public int nextInt() throws IOException
{
return Integer.parseInt(next());
}
public double nextDouble() throws IOException
{
return Double.parseDouble(next());
}
public long nextLong() throws IOException
{
return Long.parseLong(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for(int i=0;i<n;i++)arr[i]=nextInt();
return arr;
}
public long[] nextLongArr(int n)throws IOException {
long[] arr = new long[n];
for(int i=0;i<n;i++)arr[i]=nextLong();
return arr;
}
public char[] nextCharArr() throws IOException {
return next().toCharArray();
}
}
|
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 17
|
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
|
abb75c17318622c8eb085a55d5fb455f
|
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 Solution {
public static void main(String[] args) throws IOException {
Reader scan = new Reader(System.in);
BufferedOutputStream b = new BufferedOutputStream(System.out);
StringBuffer sb = new StringBuffer();
int t = scan.nextInt();
// int t = 1;
while (t-->0)
{
int n = scan.nextInt();
String s = scan.next();
sb.append(solve(n, s.toCharArray())).append("\n");
}
b.write((sb.toString()).getBytes());
b.flush();
}
private static String solve(int n, char[] s) {
StringBuffer sb = new StringBuffer();
long[] score = new long[n];
char[] arr = new char[n];
for(int i=0;i<n/2;i++)
{
score[i] = n-(i+1);
arr[i] = 'R';
}
for(int i=(n/2);i<n;i++)
{
arr[i] = 'L';
score[i] = i;
}
ArrayList<pair> ans = new ArrayList<>();
long total = 0;
for(int i=0;i<n;i++)
{
long val = 0;
if(s[i] == 'R')
{
val = n-i-1;
}
else {
val = i;
}
total += val;
ans.add(new pair(val, i));
}
ans.sort(new comp());
for(pair p : ans)
{
total = total - p.val + score[p.idx];
sb.append((total)).append(" ");
}
return sb.toString();
}
}
class pair {
int idx;
long val;
pair(long val, int idx)
{
this.val = val;
this.idx = idx;
}
}
class comp implements Comparator<pair>
{
public int compare(pair a, pair b)
{
return (int)(a.val-b.val);
}
}
class Reader{
private final BufferedReader reader;
private StringTokenizer tokenizer;
Reader(InputStream input)
{
this.reader = new BufferedReader(new InputStreamReader(input));
this.tokenizer = new StringTokenizer("");
}
public String next() throws IOException {
while(!tokenizer.hasMoreTokens())
{
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public int nextInt() throws IOException
{
return Integer.parseInt(next());
}
public double nextDouble() throws IOException
{
return Double.parseDouble(next());
}
public long nextLong() throws IOException
{
return Long.parseLong(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for(int i=0;i<n;i++)arr[i]=nextInt();
return arr;
}
public long[] nextLongArr(int n)throws IOException {
long[] arr = new long[n];
for(int i=0;i<n;i++)arr[i]=nextLong();
return arr;
}
public char[] nextCharArr() throws IOException {
return next().toCharArray();
}
}
|
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 17
|
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
|
2c76726e8b87e8b912fcdc94084a94de
|
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.stream.Collectors;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int cases = scanner.nextInt();
Queue<Integer> q = new PriorityQueue<>((a, b)-> -Integer.compare(a, b));
while(cases-- > 0) {
long sum = 0;
int n = scanner.nextInt();
String s = scanner.next();
for(int i = 0; i < s.length(); i++) {
int l = i, r = s.length() - i - 1;
if(s.charAt(i) == 'L') {
sum += l;
q.add(r-l);
}
else {
sum += r;
q.add(l-r);
}
}
int i = 1;
for(; i <= n; i++) {
int add = q.remove();
if(add <= 0) break;
System.out.printf("%d ", sum+=add);
}
while(i++ <= n) System.out.printf("%d ", sum);
System.out.println();
}
}
}
/**
*
6
3
RLL
5
LRRLL
1
L
12
LRRRLLLRLLRL
10
LLLLLRRRRR
9
LRLRLRLRL
*
* **/
|
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 17
|
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
|
6be5039081da7709d0c0f703d58912f4
|
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.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
public class CF1722D {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
String s = scanner.next();
System.out.println(solve(n, s));
}
}
private static String solve(int n, String s) {
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Comparator.reverseOrder());
long sum = 0;
for (int i = 0; i < n; i++) {
int left = i;
int right = n - 1 - i;
if (s.charAt(i) == 'L') {
sum += left;
if (right > left) {
priorityQueue.add(right - left);
}
} else {
sum += right;
if (left > right) {
priorityQueue.add(left - right);
}
}
}
List<String> resList = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (!priorityQueue.isEmpty()) {
sum += priorityQueue.remove();
}
resList.add(String.valueOf(sum));
}
return String.join(" ", resList);
}
}
|
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 17
|
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
|
254b86d63d8f4b5fd61d1d5f4e7ce78b
|
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.util.*;
public class D {
public static long[] solve(String s) {
char[] buf = s.toCharArray();
int n = buf.length;
long[] res = new long[n];
PriorityQueue<Integer> pp = new PriorityQueue<>(Comparator.comparing(x -> -x));
long acc = 0;
for (int i = 0; i < n; i++) {
int left = i;
int right = (n - i - 1);
if (buf[i] == 'L') {
if (right > left) {
pp.offer(right - left);
}
acc += left;
} else {
if (left > right) {
pp.offer(left - right);
}
acc += right;
}
}
for (int i = 0; i < n; i++) {
if (!pp.isEmpty()) {
acc += pp.poll();
}
res[i] = acc;
}
return res;
}
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int kase = sc.nextInt();
while (kase-- > 0) {
int n = sc.nextInt();
String s = sc.next();
long[] res = solve(s);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < n; i++) {
if (i != 0) sb.append(" ");
sb.append(res[i]);
}
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 17
|
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
|
6f0655e09d941275c7b49d6859389171
|
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.*;
/*
L=0
R=1
* */
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
ArrayList<int []> list=new ArrayList<>();
String in=sc.next();
long sum=0;
for(int i=1;i<=n;i++)
{ char c=in.charAt(i-1);
int x=c=='L'?i-1:n-i;
list.add(new int[]{c=='L'?0:1,i,x});
sum+=x;
}
list.sort((Comparator.comparingInt(o -> o[2])));
for(int[] arr : list)
{
long currsum= sum+(arr[0]==0? n-arr[1]:arr[1]-1) -arr[2];
if(currsum>sum) sum=currsum;
System.out.print(sum+" ");
}
}
}
}
|
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 17
|
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
|
0b2129379d5864096dd2df5b53216bc8
|
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;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 0; tc < t; ++tc) {
sc.nextInt();
String line = sc.next();
System.out.println(solve1(line));
}
sc.close();
}
static String solve1(String line) {
long value = 0;
int[] deltas = new int[line.length()];
for (int i = 0; i < line.length(); ++i) {
int leftCount = i;
int rightCount = line.length() - 1 - i;
if (line.charAt(i) == 'L') {
value += leftCount;
deltas[i] = Math.max(0, rightCount - leftCount);
} else {
value += rightCount;
deltas[i] = Math.max(0, leftCount - rightCount);
}
}
int[] sortedDeltas =
Arrays.stream(deltas).boxed().sorted(Comparator.reverseOrder()).mapToInt(x -> x).toArray();
long[] result = new long[line.length()];
for (int i = 0; i < result.length; ++i) {
result[i] = ((i == 0) ? value : result[i - 1]) + sortedDeltas[i];
}
return Arrays.stream(result).mapToObj(String::valueOf).collect(Collectors.joining(" "));
}
}
|
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 17
|
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
|
e6e5473a1c2676a7fb5ed4c7bd4732dc
|
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 sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
long sum=0,n;
int l;
l=sc.nextInt();
int a[]=new int[l];
String s;
s=sc.next();
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(c=='L')
sum=sum+i;
if(c=='R')
sum=sum+s.length()-(i+1);
}
int y=l/2;
for(int i=0;i<l/2;i++)
{
if(s.charAt(i)=='L')
a[i]=s.length()-(i+1)-Math.abs(1-(i+1));
}
for(int i=l/2;i<l;i++)
{
if(s.charAt(i)=='R')
a[i]=Math.abs(1-(i+1))-(s.length()-(i+1));
}
Arrays.sort(a);
for(int i=l-1;i>=0;i--)
{
sum=sum+a[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 17
|
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
|
e3c2963a0bf97ec261c8d825e0295a2d
|
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 Solution {
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;
}
}
static int gcd(int a, int b) {
if (a == 0) return b;
if (b == 0) return a;
if (a == b) return a;
if (a > b) return gcd(a-b, b);
return gcd(a, b-a);
}
static void print(long []arr) {
for(long a : arr) System.out.print(a + " ");
System.out.println();
}
static void print(int []arr) {
for(int a : arr) System.out.print(a + " ");
System.out.println();
}
static void print(long a) {
System.out.print(a);
}
static void printl(long a) {
System.out.println(a);
}
static void printl(char ch) {
System.out.println(ch);
}
static void print(char ch) {
System.out.print(ch);
}
static void printl(String s) {
System.out.println(s);
}
static void print(String s) {
System.out.print(s);
}
static int[] intArray(int n) {
int arr[] = new int[n];
for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
return arr;
}
static long[] longArray(int n) {
long arr[] = new long[n];
for(int i = 0; i < n; i++) arr[i] = sc.nextLong();
return arr;
}
static double[] doubleArray(int n) {
double arr[] = new double[n];
for(int i = 0; i < n; i++) arr[i] = sc.nextDouble();
return arr;
}
static int[][] intMatrix(int n, int m) {
int mat[][] = new int[n][m];
for(int i = 0; i < mat.length; i++)
for(int j = 0; j < mat[0].length; j++) mat[i][j] = sc.nextInt();
return mat;
}
static long[][] longMatrix(int n, int m) {
long mat[][] = new long[n][m];
for(int i = 0; i < mat.length; i++)
for(int j = 0; j < mat[0].length; j++) mat[i][j] = sc.nextLong();
return mat;
}
static class Pair {
int v1, v2;
Pair(int v1, int v2) {
this.v1 = v1;
this.v2 = v2;
}
}
static FastReader sc = new FastReader();
public static void main(String[] args) {
int tc = sc.nextInt();
while(tc-- > 0) {
int n = sc.nextInt();
String s = sc.next();
int arr[] = new int[s.length()];
long sum = 0;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == 'L') {
sum += i;
arr[i] = i;
}
else {
sum += s.length() - i - 1;
arr[i] = s.length() - i - 1;
}
}
for(int i = 0; i < arr.length; i++) {
if(s.charAt(i) == 'L') {
int newCount = s.length() - i - 1;
arr[i] = newCount - arr[i];
} else {
int newCount = i;
arr[i] = newCount - arr[i];
}
}
Arrays.sort(arr);
for(int i = arr.length - 1; i >= 0; i--) {
if(arr[i] + sum > sum) {
sum += arr[i];
System.out.print(sum + " ");
} else 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 17
|
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
|
57bcb3a540afd1100a56a40724b2b94a
|
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 sc=new Scanner(System.in);
int test=sc.nextInt();
while(test-->0)
{
//System.out.println(solve(sc));
solve(sc);
}
sc.close();
}
public static void solve(Scanner sc){
int n=sc.nextInt();
String str=sc.next();
long arr[]=new long[n];
long sum=0;
for(int i=0;i<n;i++)
{
if(str.charAt(i)=='L')
{
arr[i]= ((n-1-i)-i);
sum+=i;
}
else {
arr[i]=(i-(n-1-i));
sum+=(n-1-i);
}
}
Arrays.sort(arr);
for(int i=n-1;i>=0;i--)
{
if(arr[i]>0)sum+=arr[i];
System.out.print(sum+" ");
}
System.out.println();
}
}
class define{
public static long power( long a,long b)
{
long res = 1;
while (b > 0) {
if ((b & 1)==1)
res = (res * a);
a = a * a;
b >>= 1;
}
return res;
}
public static int power( int a,int b)
{
int res = 1;
while (b > 0) {
if ((b & 1)==1)
res = (res * a);
a = a * a;
b >>= 1;
}
return res;
}
public static void printarray( long a[])
{
for(long i:a)
System.out.print(i+" ");
System.out.println();
}
public static void printarray( int a[])
{
for(int i:a)
System.out.print(i+" ");
System.out.println();
}
public static void printarraylist( ArrayList<Long>a)
{
for(long i:a)
System.out.print(i+" ");
System.out.println();
}
public static void printintarraylist( ArrayList<Integer>ar)
{
for(Integer i:ar)
System.out.print(i+" ");
System.out.println();
}
public static void swap( long a,long b)
{
a=a^b;
b=a^b;
a=a^b;
System.out.println(a+" "+b);
}
public static void swap( int a,int b)
{
a=a^b;
b=a^b;
a=a^b;
System.out.println(a+" "+b);
}
public static int maxxorbetweentwonumber( int a,int b)
{
int store=a^b;
int count=0;
while(store>0)
{
count++;
store=store>>1;
}
int res=1;
int ans=0;
while(count-->0)
{
ans+=res;
res=res<<1;
}
return ans;
}
public static void sort(int arr[])
{
Arrays.sort(arr);
}
public static int[] insertarray(Scanner sc,int n)
{
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
return arr;
}
public static ArrayList<Integer> alldivisor(int n)
{
ArrayList<Integer> arr=new ArrayList<>();
for(int i=1;i*i<=n;i++)
{
if(n%i==0){
arr.add(i);
if(n/i!=i)arr.add(n/i);
}
}
return arr;
}
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int binarysearch(int arr[],int target)
{
int l=0,r=arr.length-1;
while(l<=r)
{
int mid=l+(r-l)/2;
if(arr[mid]==target)
return mid;
else if(arr[mid]>target)
r=mid-1;
else
l=mid+1;
}
return -1;
}
public static int[] arrayinput(Scanner sc,int n)
{
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
return arr;
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn the first test case: $$$k=1$$$: change the direction of $$$1$$$ person to make the line RLR. The total value is $$$2+1+0=3$$$. $$$k=2$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. $$$k=3$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. Note that you have to change the direction of at most $$$k$$$ people. In the second test case, it is optimal to only change the direction of the first person for all $$$k$$$ from $$$1$$$ to $$$5$$$ (that is, make the line RRRLL).
|
Java 17
|
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
|
2b21160840e0a238ade6223d987c8d32
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class main {
static class Person {
int idx;
int sum;
public Person(int idx, int sum) {
this.idx = idx;
this.sum = sum;
}
}
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringBuilder str = new StringBuilder();
int tc = Integer.parseInt(reader.readLine());
while (tc-- > 0) {
int n = Integer.parseInt(reader.readLine());
String line = reader.readLine();
PriorityQueue<Person> pq = new PriorityQueue<>((a, b) -> b.sum - a.sum);
long sum = 0;
for (int i = 1; i <= n; i++) {
int orig = 0;
int opp = 0;
if (line.charAt(i-1) == 'L') {
orig = i-1;
opp = n-i;
} else {
orig = n-i;
opp = i-1;
}
if (orig < opp) {
pq.offer(new Person(i, opp));
}
sum += orig;
}
int k = 1;
while (!pq.isEmpty() && k <= n) {
Person p = pq.poll();
int orig = 0;
if (line.charAt(p.idx-1) == 'L') orig = p.idx-1;
else orig = n-p.idx;
sum -= orig;
sum += p.sum;
str.append(sum + " ");
k++;
}
while (k++<=n) str.append(sum + " ");
str.append("\n");
}
System.out.println(str.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 17
|
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
|
a1f01bca176331e39a38fbd0139a7d2e
|
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 A1{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st;
static final long mod=1000000007;
public static void Solve() throws IOException{
st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
char[] ch=getStr();
List<Integer> al=new ArrayList<>();
List<Integer> ar=new ArrayList<>();
int i;
for(i=0;i<n/2;i++){
if(ch[i]=='L') al.add(i+1);
}
if(n%2!=0) i++;
for(;i<n;i++){
if(ch[i]=='R') ar.add(i+1);
}
long s=0;
Map<Integer,Integer> ah=new HashMap<>();
for(i=0;i<n;i++){
if(ch[i]=='L'){
s+=i;
ah.put(i+1,i);
}
else if(ch[i]=='R'){
s+=(n-i-1);
ah.put(i+1,n-i-1);
}
}
int j=0,k=ar.size()-1;
for(i=1;i<=n;i++){
if(j==al.size() && k==-1) break;
if(j==al.size()){
s-=ah.get(ar.get(k));
s+=ar.get(k)-1;
k--;
}
else if(k==-1){
s-=ah.get(al.get(j));
s+=n-al.get(j);
j++;
}
else{
if(n-al.get(j)>=ar.get(k)-1){
s-=ah.get(al.get(j));
s+=n-al.get(j);
j++;
}
else{
s-=ah.get(ar.get(k));
s+=ar.get(k)-1;
k--;
}
}
bw.write(s+" ");
}
for(;i<=n;i++) bw.write(s+" ");
bw.newLine();
}
/** Main Method**/
public static void main(String[] YDSV) throws IOException{
//int t=1;
int t=Integer.parseInt(br.readLine());
while(t-->0) Solve();
bw.flush();
}
/** Helpers**/
private static char[] getStr()throws IOException{
return br.readLine().toCharArray();
}
private static int Gcd(int a,int b){
if(b==0) return a;
return Gcd(b,a%b);
}
private static long Gcd(long a,long b){
if(b==0) return a;
return Gcd(b,a%b);
}
private static int[] getArrIn(int n) throws IOException{
st=new StringTokenizer(br.readLine());
int[] ar=new int[n];
for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken());
return ar;
}
private static Integer[] getArrInP(int n) throws IOException{
st=new StringTokenizer(br.readLine());
Integer[] ar=new Integer[n];
for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken());
return ar;
}
private static long[] getArrLo(int n) throws IOException{
st=new StringTokenizer(br.readLine());
long[] ar=new long[n];
for(int i=0;i<n;i++) ar[i]=Long.parseLong(st.nextToken());
return ar;
}
private static List<Integer> getListIn(int n) throws IOException{
st=new StringTokenizer(br.readLine());
List<Integer> al=new ArrayList<>();
for(int i=0;i<n;i++) al.add(Integer.parseInt(st.nextToken()));
return al;
}
private static List<Long> getListLo(int n) throws IOException{
st=new StringTokenizer(br.readLine());
List<Long> al=new ArrayList<>();
for(int i=0;i<n;i++) al.add(Long.parseLong(st.nextToken()));
return al;
}
private static long pow_mod(long a,long b) {
long result=1;
while(b!=0){
if((b&1)!=0) result=(result*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return result;
}
private static int lower_bound(int a[],int x){
int l=-1,r=a.length;
while(l+1<r){
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r+1;
}
private static long lower_bound(long a[],long x){
int l=-1,r=a.length;
while(l+1<r){
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r+1;
}
private static int upper_bound(int a[],int x){
int l=-1,r=a.length;
while(l+1<r){
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
private static long upper_bound(long a[],long x){
int l=-1,r=a.length;
while(l+1<r){
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
private static boolean Sqrt(int x){
int a=(int)Math.sqrt(x);
return a*a==x;
}
private static boolean Sqrt(long x){
long a=(long)Math.sqrt(x);
return a*a==x;
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn the first test case: $$$k=1$$$: change the direction of $$$1$$$ person to make the line RLR. The total value is $$$2+1+0=3$$$. $$$k=2$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. $$$k=3$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. Note that you have to change the direction of at most $$$k$$$ people. In the second test case, it is optimal to only change the direction of the first person for all $$$k$$$ from $$$1$$$ to $$$5$$$ (that is, make the line RRRLL).
|
Java 17
|
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
|
7cac3385328818556e4066d3eaeaf020
|
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 Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t--!=0){
int n=sc.nextInt();
char s[]=sc.next().toCharArray();
PriorityQueue<Integer> pq=new PriorityQueue<>(Collections.reverseOrder());
long currSum=0;
for(int i=0;i<n;i++)
{
if(i<(n/2)){
if(s[i]=='R'){
currSum+=n-i-1;
}else{
pq.add(n-2*i-1);
currSum+=i;
}
}else{
if(s[i]=='R'){
currSum+=n-i-1;
pq.add(2*i-n+1);
}else{
currSum+=i;
}
}
}
int i=0;
while(!pq.isEmpty()){
currSum+=pq.remove();
System.out.print(currSum+" ");
i++;
}
for(;i<n;i++) System.out.print(currSum+" ");
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 17
|
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
|
a37112131ce3d238b1f5bbc7a5ae6402
|
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 code4 {
public static void main(String[] args) {
Kattio in=new Kattio();
int t=in.nextInt();
while(t-->0){
int n=in.nextInt();
String s=in.next();
long tot=0;
List<Long> v=new ArrayList<>();
for(int i=0;i<n;i++){
if(s.charAt(i)=='L'){
v.add((long)(n-1-i-i));
tot+=i;
}else{
v.add((long)i+i-n+1);
tot+=n-1-i;
}
}
v.sort(Collections.reverseOrder());
for(int i=0;i<n;i++){
if(v.get(i)>0) tot+=v.get(i);
System.out.print(tot);
System.out.print(' ');
}
System.out.println();
}
}
}
class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// 标准 IO
public Kattio() { this(System.in, System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// 文件 IO
public Kattio(String intput, String output) throws IOException {
super(output);
r = new BufferedReader(new FileReader(intput));
}
// 在没有其他输入时返回 null
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) {}
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public 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 17
|
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
|
f711e99a9d5695916bac4302d950e3cc
|
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 java.io.*;
import java.lang.*;
public class main1{
FastScanner in;
PrintWriter out;
public static void main(String[] arg) {
new main1().run();
}
//////////////SOLVE QUESTIONS HERE/////////////
public void solve() throws IOException {
int testcase=1;
testcase=in.nextInt();
while(testcase-- > 0)
{
int n=in.nextInt();
var 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();
}
}
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 17
|
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
|
b33435fcd3caaae854760e9c454e8b4d
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
private final static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int testCases = sc.nextInt();
for (int testCase = 0; testCase < testCases; testCase++) {
int n = sc.nextInt();
String s = sc.next();
long total = 0;
List<Long> turnAroundScores = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L') {
turnAroundScores.add((long) ((n - i - 1) - i));
total += i;
} else {
turnAroundScores.add((long) (i - (n - i - 1)));
total += n - i - 1;
}
}
turnAroundScores.sort(Collections.reverseOrder());
for (long turnAroundScore:
turnAroundScores) {
if (turnAroundScore > 0) {
total += turnAroundScore;
}
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 17
|
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
|
e1bcf5bc18e909669d83ba0e57fbba8d
|
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 Codeforces2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while (tc-- > 0) {
int n = sc.nextInt();
String s = sc.next();
long total = 0L;
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L') {
arr[i] = (n - i - 1) - i;
total += i;
} else {
arr[i] = i - (n - i - 1);
total += (n - i - 1);
}
}
Arrays.sort(arr);
for (int i = 0; i < n; i++) {
if (arr[n - 1 - i] > 0)
total += arr[n - i - 1];
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 17
|
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
|
49b2ddd1c36b12316f9dcd3a531e75c8
|
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 {
private void solve(InputReader in, PrintWriter pw) {
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
char[] cs = in.next().toCharArray();
long[] ans = new long[n];
int[] a = new int[n];
for (int i = 0; i < n; i++) {
if (cs[i] == 'L') {
a[i] = i;
} else {
a[i] = n - i - 1;
}
ans[0] += a[i];
}
Arrays.sort(a);
for (int i = 0; i < n; i++) {
if (i > 0) {
ans[i] = ans[i - 1];
}
if (a[i] < n - a[i] - 1) {
ans[i] -= a[i];
ans[i] += n - a[i] - 1;
}
}
for (int i = 0; i < n; i++) {
pw.print(ans[i] + " ");
}
pw.println();
}
pw.close();
}
public static void main(String[] args) {
new Main().solve(new InputReader(System.in), new PrintWriter(System.out));
}
}
class InputReader {
private final BufferedReader reader;
private 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();
}
public String nextLine() {
String str;
try {
str = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return str;
}
public boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
String nextLine;
try {
nextLine = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (nextLine == null) {
return false;
}
tokenizer = new StringTokenizer(nextLine);
}
return true;
}
public int nextInt() {
return Integer.parseInt(next());
}
public 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 17
|
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
|
83836880a0b5a9fcb19aceec9d91b038
|
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;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 0; tc < t; ++tc) {
sc.nextInt();
String line = sc.next();
System.out.println(solve(line));
}
sc.close();
}
static String solve(String line) {
long value = 0;
int[] deltas = new int[line.length()];
for (int i = 0; i < line.length(); ++i) {
int leftCount = i;
int rightCount = line.length() - 1 - i;
if (line.charAt(i) == 'L') {
value += leftCount;
deltas[i] = Math.max(0, rightCount - leftCount);
} else {
value += rightCount;
deltas[i] = Math.max(0, leftCount - rightCount);
}
}
int[] sortedDeltas =
Arrays.stream(deltas).boxed().sorted(Comparator.reverseOrder()).mapToInt(x -> x).toArray();
long[] result = new long[line.length()];
for (int i = 0; i < result.length; ++i) {
result[i] = ((i == 0) ? value : result[i - 1]) + sortedDeltas[i];
}
return Arrays.stream(result).mapToObj(String::valueOf).collect(Collectors.joining(" "));
}
}
|
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 17
|
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
|
79accc9a7dfed2e1c3c54e2f2111949c
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import static java.lang.System.out;
public class Round_780_Div_3 {
static MyScanner str = new MyScanner();
//static Scanner str = new Scanner(System.in);
public static void main(String[] args) throws IOException {
int T = i();
while (T-- > 0) {
// out.print("Case #" + i + ": ");
// i++;
solve();
}
}
static void solve() throws IOException {
int n = i();
String s = str.next();
char a[] = s.toCharArray();
long res[] = new long[n];
long cur = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 'L') {
cur += i;
res[i] += n - 1 - i - i;
} else {
cur += (n - i - 1);
res[i] += i - (n - i - 1);
}
}
Arrays.sort(res);
reverseArr(res, 0, n - 1);
for (int i = 0; i < n; i++) {
cur += Math.max(0, res[i]);
res[i] = cur;
}
out.println(Arrays.toString(res).replace("[", "").replace("]", "").replace(",", ""));
}
public static int binarySearch(int[] arr, int need) {
int l = 0;
int r = arr.length;
int res = -1;
while (l < r) {
int mid = l + (r - l) / 2;
if (arr[mid] == need) {
return mid;
} else if (arr[mid] < need) {
l = mid + 1;
} else {
r = mid;
res = mid;
}
}
return res;
}
public static int[] swapInt(int a, int b) {
int temp = a;
a = b;
b = temp;
return new int[]{a, b};
}
public static int i() throws IOException {
return str.nextInt();
}
public static long l() throws IOException {
return str.nextLong();
}
public static double d() throws IOException {
return str.nextDouble();
}
public static String s() throws IOException {
return str.nextLine();
}
public static int[] readArr(int N) throws IOException {
int[] arr = new int[N];
for (int i = 0; i < N; i++)
arr[i] = i();
return arr;
}
public static long[] readArrLong(int N) throws IOException {
long[] arr = new long[N];
for (int i = 0; i < N; i++)
arr[i] = l();
return arr;
}
public static void printArray(int[] arr) {
for (int x : arr)
out.print(x + " ");
out.println();
}
public static void printList(ArrayList<Integer> arr) {
for (int x : arr)
out.print(x + " ");
out.println();
}
public static boolean isPrime(long n) {
if (n < 2) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
long sqrtN = (long) Math.sqrt(n) + 1;
for (long i = 6L; i <= sqrtN; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0) return false;
}
return true;
}
public static String mul(String s, int k) {
String res = "";
while (k-- > 0) {
res += s;
}
return res;
}
public static boolean isRight(int a[]){
for (int i = 1; i < 4; i++) {
if(a[i] == 0){
return false;
}
}
return true;
}
public static boolean isSortedArray(int a[]) {
boolean ok = true;
for (int i = 1; i < a.length; i++) {
if (a[i] < a[i - 1]) {
ok = false;
}
}
return ok;
}
public static boolean isSortedList(ArrayList<Integer> a) {
boolean ok = true;
for (int i = 1; i < a.size(); i++) {
if (a.get(i) < a.get(i - 1)) {
ok = false;
}
}
return ok;
}
public static void yes() {
out.println("YES");
}
public static void no() {
out.println("NO");
}
private static void swapList(int a[][], ArrayList<Integer> w, int n) {
for (int i = 0; i < n; i++) {
int temp = a[i][w.get(0)];
a[i][w.get(0)] = a[i][w.get(1)];
a[i][w.get(1)] = temp;
}
}
public static boolean isans(int n, int a[][]) {
for (int i = 0; i < 5; i++) {
for (int j = i + 1; j < 5; j++) {
int f = 0, s = 0, b = 0, temp = 0;
for (int k = 0; k < n; k++) {
if (a[k][i] == 1 && a[k][j] == 1) b++;
else if (a[k][i] == 1) f++;
else if (a[k][j] == 1) s++;
}
temp = n - (Math.min(n / 2, f) + Math.min(n / 2, s));
if (temp <= b) return true;
}
}
return false;
}
public static void reverseList(ArrayList<Integer> tr, int start, int end) {
int temp;
while (start < end) {
temp = tr.get(start);
tr.set(start, tr.get(end));
tr.set(end, temp);
start++;
end--;
}
}
public static void reverseArr(long[] tr, int start, int end) {
long temp;
while (start < end) {
temp = tr[start];
tr[start] = tr[end];
tr[end] = temp;
start++;
end--;
}
}
static long div(long n) {
for (int i = 2; (long) i * i <= n; i++) {
if (n % i == 0) {
return i;
}
}
return n;
}
static boolean check(String s) {
if (checka(s) && checkb(s)) {
return true;
}
return false;
}
static boolean checka(String s) {
String a[] = s.split("AB");
for (int i = 0; i < a.length; i++) {
if (a[i].equals("B")) {
return false;
}
}
return true;
}
static boolean checkb(String s) {
String a[] = s.split("A");
int cnt = 0;
for (int i = 0; i < a.length; i++) {
if (a[i].equals("")) {
cnt++;
}
}
if (cnt == s.length()) {
return false;
}
return true;
}
public static long fac(long n) {
if (n == 1 || n == 0) return 1;
return n * fac(n - 1);
}
public static boolean isPalindrome(String s) {
int l = 0;
int r = s.length() - 1;
while (l < r) {
if (s.charAt(l) != s.charAt(r)) {
return false;
}
l++;
r--;
}
return true;
}
public static long Gcd(long a, long b) {
if (a < b) return Gcd(b, a);
while (b != 0) {
long w = a % b;
a = b;
b = w;
}
return a;
}
public static long Lcm(long a, long b) {
if (a > b) return (a / Gcd(a, b)) * b;
return (b / Gcd(a, b)) * a;
}
public static int nod(int a, int b) {
while (a != 0 && b != 0) {
if (a > b) {
a %= b;
} else {
b %= a;
}
}
return a + b;
}
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;
}
}
private static class ArraysLong {
static void merge(long arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
long L[] = new long[n1];
long R[] = new long[n2];
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
static void sort(long arr[], int l, int r) {
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
static void sort(long[] arr) {
sort(arr, 0, arr.length - 1);
}
}
private static class ArraysInt {
static void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int[n1];
int R[] = new int[n2];
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
static void sort(int arr[], int l, int r) {
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
static void sort(int[] arr) {
sort(arr, 0, arr.length - 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 17
|
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
|
e503ea7a9165e4b6d5fa0b6079b13348
|
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) {
// long s = System.currentTimeMillis();
// System.out.println(letterCombinations("23"));
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
String str = sc.next();
long sum = 0;
List<Integer> runnSum = new ArrayList<>();
for(int i = 0; i < n; i++){
if(str.charAt(i) == 'L') sum += i;
else sum += n - i - 1;
if(i < (n - i - 1) && str.charAt(i) == 'L') runnSum.add(n - i - 1 - i);
else if(i > (n - i - 1) && str.charAt(i) == 'R') runnSum.add(i - n + i + 1);
}
Collections.sort(runnSum);
Collections.reverse(runnSum);
long s = 0;
long[] seq = new long[n];
for(int i = 0; i < runnSum.size(); i++){
s += runnSum.get(i);
seq[i] = sum + s;
}
for(int i = runnSum.size(); i < n; i++){
seq[i] = sum + s;
}
for(var i: seq)
System.out.print(i + " ");
System.out.println();
}
// System.out.println("Runtime: " + System.currentTimeMillis() - s + "ms");
}
}
|
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 17
|
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
|
d51d9d3868d4d101b994d4c43e850403
|
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.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
import java.util.*;
public class Main
{
public static boolean issorted(int []arr){
for(int i = 1;i<arr.length;i++){
if(arr[i]<arr[i-1]){
return false;
}
}
return true;
}
public static long sum(int []arr){
long sum = 0;
for(int i = 0;i<arr.length;i++){
sum+=arr[i];
}
return sum;
}
public static class pair implements Comparable<pair>{
int val;
int idx;
pair(int val,int idx){
this.val = val;
this.idx = idx;
}
public int compareTo(pair o){
return this.idx - o.idx; // sort increasingly on the basis of x
// return o.x - this.x // sort decreasingly on the basis of x
}
}
public static void swap(int []arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static int gcd(int a, int b){
if (b == 0)
return a;
return gcd(b, a % b);
}
static int []parent = new int[1000];
static int []size = new int[1000];
public static void make(int v){
parent[v] = v;
size[v] = 1;
}
public static int find(int v){
if(parent[v]==v){
return v;
}
else{
return parent[v] = find(parent[v]);
}
}
public static void union(int a,int b){
a = find(a);
b = find(b);
if(a!=b){
if(size[a]>size[b]){
parent[b] = parent[a];
size[b]+=size[a];
}
else{
parent[a] = parent[b];
size[a]+=size[b];
}
}
}
static boolean []visited = new boolean[1000];
public static void dfs(int vertex,ArrayList<ArrayList<Integer>>graph){
if(visited[vertex] == true){
return;
}
System.out.println(vertex);
for(int child : graph.get(vertex)){
// work to be done before entering the child
dfs(child,graph);
// work to be done after exitting the child
}
}
public static void displayint(int []arr){
for(int i = 0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void displaystr(String str){
StringBuilder sb = new StringBuilder(str);
for(int i = 0;i<sb.length();i++){
System.out.print(sb.charAt(i));
}
System.out.println();
}
public static boolean checkbalanceparenthesis(StringBuilder ans){
Stack<Character>st = new Stack<>();
int i = 0;
while(i<ans.length()){
if(ans.charAt(i) == '('){
st.push('(');
}
else{
if(st.size() == 0 || st.peek()!='('){
return false;
}
else{
st.pop();
}
}
}
return st.size() == 0;
}
public static long binaryExp(long a,long b,long m){ /// This is Iterative Version
long res = 1;
while(b>0){
if((b&1)!=0){
res = (res*a)%m;
}
b>>=1;
a = (a*a)%m;
}
return res;
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-->0){
int n = scn.nextInt();
String str = scn.next();
long cost = 0;
for(int i = 0;i<n;i++){
if(str.charAt(i)=='R'){
cost+=(n-1) - i;
}
else{
cost+=i;
}
}
int leftlen = (n-1)/2;
int righlen = ((n-1)/2) + 1;
ArrayList<Integer>left = new ArrayList<>();
ArrayList<Integer>right = new ArrayList<>();
for(int i = 0;i<=leftlen;i++){
if(str.charAt(i) == 'L'){
left.add(i);
}
}
Collections.sort(left);
for(int i = n-1;i>=righlen;i--){
if(str.charAt(i)=='R'){
right.add(i);
}
}
Collections.sort(right,Collections.reverseOrder());
StringBuilder sb = new StringBuilder("");
for(int i = 0;i<n;i++){
if(left.size()==0 && right.size()==0){
sb.append(cost+" ");
}
else if(left.size()!=0 && right.size()==0){
int idx = left.remove(0);
cost-=idx;
cost+=(n-1)-idx;
sb.append(cost+" ");
}
else if(right.size()!=0 && left.size()==0){
int idx = right.remove(0);
cost-=(n-1)-idx;
cost+=idx;
sb.append(cost+" ");
}
else{
int idxl = left.get(0);
int idxr = (n-1) - right.get(0);
if(idxl<idxr){
cost-=idxl;
cost+=(n-1)-idxl;
sb.append(cost+" ");
left.remove(0);
}
else{
cost-=(n-1)-right.get(0);
cost+=right.get(0);
sb.append(cost+" ");
right.remove(0);
}
}
}
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 17
|
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
|
82aa9b1309ad169178c86f9d6efe6c83
|
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 MyClass {
Input in;
public MyClass() {
in = new Input(System.in);
}
public static void main(String[] args) {
MyClass solution = new MyClass();
for (int t = solution.in.nextInt(); t > 0; t--) {
solution.solve();
}
// solution.solve();
}
void solve() {
int length = in.nextInt();
String input = in.nextString();
ArrayList<Integer> values = new ArrayList<>();
for(int i=0;i<length/2;i++) {
if(input.charAt(i) == 'L') {
values.add(length-i-1-i);
}
if(input.charAt(length-i-1) == 'R') {
values.add(length-i-1-i);
}
}
int indice = 0;
long sum = 0;
for(int i=0;i<length;i++) {
if(input.charAt(i) == 'L') {
sum += i;
}
else {
sum += length-i-1;
}
}
for(int i=1; i<=length; i++) {
if(indice < values.size())
sum += values.get(indice);
indice++;
System.out.print(sum+" ");
}
System.out.println();
}
static class Input {
BufferedReader br;
StringTokenizer st;
public Input(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
st = new StringTokenizer("");
}
String nextString() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextString());
}
long nextLong() {
return Long.parseLong(nextString());
}
int[] nextIntArray(int size) {
int[] ans = new int[size];
for (int i = 0; i < size; i++) {
ans[i] = nextInt();
}
return ans;
}
long[] nextLongArray(int size) {
long[] ans = new long[size];
for (int i = 0; i < size; i++) {
ans[i] = nextLong();
}
return ans;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn the first test case: $$$k=1$$$: change the direction of $$$1$$$ person to make the line RLR. The total value is $$$2+1+0=3$$$. $$$k=2$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. $$$k=3$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. Note that you have to change the direction of at most $$$k$$$ people. In the second test case, it is optimal to only change the direction of the first person for all $$$k$$$ from $$$1$$$ to $$$5$$$ (that is, make the line RRRLL).
|
Java 17
|
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
|
9fc850a5683619f30f65961ed8070e2f
|
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 d {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while(t-- > 0) {
int n = input.nextInt();
String str = input.next();
int k = 0;
long max = 0;
long curr = 0;
PriorityQueue<Long> pq = new PriorityQueue<>(Collections.reverseOrder());
if (n % 2 == 0){
for (int i = 0; i < n; i++){
if (i < n/2){
max += n - i - 1;
if (str.charAt(i) == 'L'){
k++;
pq.add(n-i-1L - i);
}
}
else{
max += i;
if (str.charAt(i) == 'R'){
k++;
pq.add(i - n+i+1L);
}
}
if (str.charAt(i) == 'L'){
curr += i;
}
else{
curr += n-i-1;
}
}
}
else {
for (int i = 0; i < n; i++){
if (i == n/2){
curr += n/2;
max += n/2;
continue;
}
if (i < n/2){
max += n - i - 1;
if (str.charAt(i) == 'L'){
pq.add(n-i-1L - i);
k++;
}
}
else{
max += i;
if (str.charAt(i) == 'R'){
pq.add(i - n+i+1L);
k++;
}
}
if (str.charAt(i) == 'L'){
curr += i;
}
else{
curr += n-i-1;
}
}
}
for (int i = 1; i <= n; i++){
if (i >= k){
System.out.print(max + " ");
}
else{
curr += pq.poll();
System.out.print(curr + " ");
}
}
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 17
|
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
|
902e54ae114f33badc9526ff65960789
|
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.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
public class D {
public static PrintWriter out;
public static MyScanner scanner;
private static void code() {
int testcase = scanner.nextInt();
while (testcase-- > 0) {
int n=scanner.nextInt();
ArrayList<Pair<Integer,Integer>> arrayList=new ArrayList<>();
for (int i = 0; i < n; i++) {
arrayList.add(new Pair<>(0,i));
}
String line=scanner.nextLine();
long sum=0;
for (int i = 0; i < n; i++) {
if(line.charAt(i)=='L')
arrayList.get(i).first=i;
else
arrayList.get(i).first=n-i-1;
sum+=arrayList.get(i).first;
}
arrayList.sort(new Comparator<Pair<Integer, Integer>>() {
@Override
public int compare(Pair<Integer, Integer> integerIntegerPair, Pair<Integer, Integer> t1) {
return integerIntegerPair.first.compareTo(t1.first);
}
});
for (int i = 0; i < n; i++) {
var p=arrayList.get(i);
long l_sum=sum-p.first;
if(line.charAt(p.second)=='L')
{
l_sum+=n-p.second-1;
}else {
l_sum+=p.second;
}
sum=Math.max(l_sum,sum);
out.printf("%d ",sum);
}
out.println();
}
}
static class Pair<FIRST,SECOND> {
FIRST first;
SECOND second;
public Pair(FIRST first, SECOND second) {
this.first = first;
this.second = second;
}
public FIRST getFirst() {
return first;
}
public void setFirst(FIRST first) {
this.first = first;
}
public SECOND getSecond() {
return second;
}
public void setSecond(SECOND second) {
this.second = second;
}
}
public static void main(String[] args) throws FileNotFoundException {
// scanner = new MyScanner("./input.txt");
scanner = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out), true);
// out=new java.io.PrintWriter(new java.io.BufferedOutputStream(new java.io.FileOutputStream("./output.text")),true);
code();
out.close();
}
static class MyScanner {
private final BufferedReader bufferedReader;
private StringTokenizer stringTokenizer;
public MyScanner(String path) throws FileNotFoundException {
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
}
public MyScanner() {
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (stringTokenizer == null || !stringTokenizer.hasMoreElements()) {
try {
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return stringTokenizer.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 = bufferedReader.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 17
|
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
|
435a9f38a0e7789b01f0bbb0a457487e
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import java.util.*;
public class Line {
public static void solve(char[] person) {
int i = 0;
int j = person.length - 1;
long points = 0;
for (int k = 0; k < person.length; k++) {
if (person[k] == 'L')
points += k;
if (person[k] == 'R')
points += person.length - 1 - k;
}
int k = 0;
while (i <= j) {
if (i == j) {
// System.out.println(points);
System.out.print(points + " ");
k++;
} else {
if (person[i] == 'L') {
// System.out.println("ran");
points -= i;
points += person.length - 1 - i;
k++;
System.out.print(points + " ");
}
if (person[j] == 'R') {
points += j;
points -= person.length - 1 - j;
k++;
System.out.print(points + " ");
}
}
i++;
j--;
}
while (k++ < person.length)
System.out.print(points + " ");
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n-- > 0) {
int size = sc.nextInt();
char[] person = sc.next().toCharArray();
solve(person);
}
}
}
|
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 17
|
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
|
c11d480c110414e47da3c800c66b788a
|
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 Solution {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
// a(fs, out);
// b(fs, out);
// c(fs, out);
d(fs, out);
out.close();
}
private static void d(FastScanner fs, PrintWriter out) {
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
char[] line = fs.next().toCharArray();
long sum = 0;
for (int i = 0; i < n; ++i) {
sum += line[i] == 'L' ? i : n-i-1;
}
List<Integer> missingRs = new ArrayList<>();
for (int i = 0; i < n/2; ++i) {
if (line[i] == 'L') {
missingRs.add(i);
}
}
List<Integer> missingLs = new ArrayList<>();
int cond = (n % 2 == 0 ? n/2 : n/2 + 1);
for (int i = n - 1; i >= cond; --i) {
if (line[i] == 'R') {
missingLs.add(i);
}
}
// 29 38 45 52 57 62 65 68 69 70
int r = 0, l = 0;
for (int k = 0; k < n; ++k) {
if (r < missingRs.size() && l < missingLs.size()) {
int R = missingRs.get(r);
int L = missingLs.get(l);
if (L > n - R - 1) {
sum -= n - L - 1;
sum += L;
l++;
} else {
sum -= R;
sum += n - R - 1;
r++;
}
} else if (r < missingRs.size()) {
int R = missingRs.get(r);
sum -= R;
sum += n - R - 1;
r++;
} else if (l < missingLs.size()){
int L = missingLs.get(l);
sum -= n - L - 1;
sum += L;
l++;
}
out.print(sum +" ");
}
out.println();
}
}
private static void c(FastScanner fs, PrintWriter out) {
int t = fs.nextInt();
int[] rules = new int[]{3, 1, 0};
while (t-- > 0) {
int n = fs.nextInt();
String[][] words = new String[3][n];
Map<String, Integer> freq = new HashMap<>();
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < n; ++j) {
words[i][j] = fs.next();
freq.put(words[i][j], freq.getOrDefault(words[i][j], 0) + 1);
}
}
for (int i = 0; i < 3; ++i) {
int cnt = 0;
for (int j = 0; j < n; ++j) {
int f = freq.get(words[i][j]);
cnt += rules[f-1];
}
out.print(cnt + " ");
}
out.println();
}
}
private static void b(FastScanner fs, PrintWriter out) {
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
String a = fs.next();
String b = fs.next();
out.println(same(a, b) ? "YES" : "NO");
}
}
private static boolean same(String a, String b) {
for (int i = 0; i < a.length(); ++i) {
if (((a.charAt(i) != 'B' && a.charAt(i) != 'G') ||
(b.charAt(i) != 'B' && b.charAt(i) != 'G')) &&
a.charAt(i) != b.charAt(i)) {
return false;
}
}
return true;
}
private static void a(FastScanner fs, PrintWriter out) {
int t = fs.nextInt();
char[] timur = "Timur".toCharArray();
Arrays.sort(timur);
while (t-- > 0) {
timur(fs, out, timur);
}
}
private static void timur(FastScanner fs, PrintWriter out, char[] timur) {
int n = fs.nextInt();
String s = fs.next();
if (n != 5) {
out.println("NO");
return;
}
char[] input = s.toCharArray();
Arrays.sort(input);
for (int i = 0; i < input.length; ++i) {
if (input[i] != timur[i]) {
out.println("NO");
return;
}
}
out.println("YES");
}
static final int mod=1_000_000_007;
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 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 17
|
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
|
c2e37d1f7995ccc02368af32dd47f2f8
|
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 sc = new Scanner(System.in);
int t = sc.nextInt();
while(t -- > 0) {
int n = sc.nextInt();
String s = sc.next();
char ch [] = s.toCharArray();
long point [] = new long[n];
TreeSet<Integer> l = new TreeSet<>();
TreeSet<Integer> r = new TreeSet<>((a,b)-> b -a);
for(int i = 0; i< n ; i++) {
if(ch[i] == 'L') {
point[i] = i + 1;
l.add(i);
}else {
point[i] = n - i;
r.add(i);
}
}
long sum = Arrays.stream(point).sum();
long ans [] = new long[n];
for(int i = 0 ; i < n ; i++) {
int k = i+ 1;
Integer left = null;
Integer right = null;
if(l.size() > 0) left = l.first();
if(r.size() > 0) right = r.first();
int val1 = -1;
int val2 = -1;
if(left != null &&((n% 2 == 0 && left < n/2) || (n% 2 == 1 && left < n/2) )) val1 = Math.max(val1,n-left);
if(right != null && ((n% 2 == 0 && right >= n/2) || (n%2 == 1 && right > n/2) )) val2 = Math.max(val2, right+ 1);
if(val1 == -1 && val2 == -1) {
ans[i] = sum;
}
else if(val1 > val2) {
l.remove(l.first());
ch[left] = 'R';
long diff = val1 -point[left];
point[left] = val1;
sum += diff;
ans[i] = sum;
}else {
r.remove(r.first());
ch[right] = 'L';
long diff = val2 - point[right];
point[right] = val2;
sum += diff;
ans[i] = sum;
}
ans[i] -= n;
}
for(int i = 0 ; i < n ; i++) {
System.out.print(ans[i]+" ");
}
System.out.println();
}
// System.out.println();
}
static int gcd (int a , int b) {
if(b == 0 ) return a;
return gcd(b, a%b);
}
public static int lowerBound(int arr [],int low, int high, int key) {
while(low <= high) {
int mid = (low + high) /2;
if(arr[mid] >= key) high = mid - 1;
else low = mid + 1;
}
return low;
}
public static long pow(long x, long n, long d) {
if(n == 0 ) return 1 % d;
if(x < 0) return pow(x + d, n , d);
if(n == 1) return x % d;
long ans = 1;
while(n > 0 ){
if(n % 2 == 0){
x = x * x % d;
n = n /2;
}else {
ans = (ans * x) % d;
n = n -1;
}
}
return ans % d;
}
public static int numSetBits(long a) {
int counter = 0;
while(a != 0) {
a = (a&(a-1));
counter++;
}
return counter;
}
private static void swap(int[] ans, int i, int j) {
int temp = ans[i];
ans[i] = ans[j];
ans[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 17
|
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
|
2f154cd2c167a58d1636c9e060c13abf
|
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) {
MyScanner s = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int n = s.nextInt();
while (n > 0) {
n--;
int len = s.nextInt();
long[] dis = new long[len];
String str = s.nextLine();
long ans = 0;
for (int i = 0; i < len; i++) {
if (str.charAt(i) == 'L') {
ans += i;
dis[i] = (len - i - 1) - i;
} else {
ans += (len - i - 1);
dis[i] = i - (len - i - 1);
}
}
Arrays.sort(dis);
for (int k = 1; k <= len; k++) {
if (dis[len - k] > 0) {
ans += dis[len-k];
}
out.print(ans + " ");
}
out.println();
}
out.close();
}
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 17
|
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
|
da782a6efb16b3b14ad509933b65520f
|
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 {
final static boolean multipleTests = true;
Input in;
PrintWriter out;
public D() {
in = new Input(System.in);
out = new PrintWriter(System.out);
}
public static void main(String[] args) {
D solution = new D();
int t = 1;
if (multipleTests) t = solution.in.nextInt();
for (; t > 0; t--) {
solution.solve();
}
solution.out.close();
}
void solve() {
int n = in.nextInt();
char[] s = in.nextString().toCharArray();
long value = 0;
for (int i=0; i<n; i++) {
if (s[i] == 'L') value += i;
else value += n-1-i;
}
int k = 0;
for (int i=0; i<n-1-i; i++) {
if (s[i] == 'L') {
value -= i;
value += n-1-i;
k++;
out.print(value);
out.print(' ');
}
if (s[n-1-i] == 'R') {
value -= i;
value += n-1-i;
k++;
out.print(value);
out.print(' ');
}
}
while (k < n) {
out.print(value);
out.print(' ');
k++;
}
out.println();
}
static class Input {
BufferedReader br;
StringTokenizer st;
public Input(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
st = new StringTokenizer("");
}
String nextString() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextString());
}
long nextLong() {
return Long.parseLong(nextString());
}
double nextDouble() {
return Double.parseDouble(nextString());
}
int[] nextIntArray(int size) {
int[] ans = new int[size];
for (int i = 0; i < size; i++) {
ans[i] = nextInt();
}
return ans;
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn the first test case: $$$k=1$$$: change the direction of $$$1$$$ person to make the line RLR. The total value is $$$2+1+0=3$$$. $$$k=2$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. $$$k=3$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. Note that you have to change the direction of at most $$$k$$$ people. In the second test case, it is optimal to only change the direction of the first person for all $$$k$$$ from $$$1$$$ to $$$5$$$ (that is, make the line RRRLL).
|
Java 17
|
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
|
bb3373b4d9ce1f5171b2d536c746dd45
|
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 int[] array(BufferedReader br,int n) throws IOException{
String [] values = br.readLine().split(" ");
int [] arr = new int[n];
for(int i =0; i<n; i++){
arr[i] = Integer.parseInt(values[i]);
}
return arr;
}
public static void swap(int[] arr, int x, int y){
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
public static void print(int[][] board){
StringBuilder sb = new StringBuilder();
for(int i =0; i<board.length; i++){
for(int j = 0; j<board[0].length; j++){
sb.append(board[i][j] + " ");
}
sb.append("\n");
}
System.out.print(sb);
}
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();
int mid = n/2;
int[] max = new int[n];
int [] actual = new int[n];
Integer[] gap = new Integer[n];
int maxval = n-1;
long cur=0;
for(int i =0; i<n; i++){
if(i<mid){
max[i] = maxval-i;
}
else{
max[i] = i;
}
}
for(int i = 0; i<n; i++){
if(i<mid){
if(str.charAt(i)=='R'){
actual[i] = max[i];
}
else{
actual[i] = maxval-max[i];
}
}
else{
if(str.charAt(i)=='L'){
actual[i] = max[i];
}
else{
actual[i] = maxval-max[i];
}
}
cur+= actual[i];
}
for(int i = 0; i<n; i++){
gap[i] = max[i]- actual[i];
}
Arrays.sort(gap,Collections.reverseOrder());
StringBuilder sb = new StringBuilder();
for(int i =0; i<n; i++){
cur += (long)gap[i];
sb.append(cur + " ");
}
System.out.println(sb);
sb.append("\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 17
|
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
|
191f62d7bb9fcd7d6c3089bd4a74d15d
|
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.*;
import java.text.*;
public class Main{
public static void main(String args[]) throws IOException{
Read sc=new Read();
int n=sc.nextInt();
for(int i=0;i<n;i++){
int a=sc.nextInt();
String s=sc.next();
long count=0;
List<Integer> list=new ArrayList<>();
for(int j=0;j<a;j++){
char c=s.charAt(j);
if(c=='L'){
count+=j;
int d=a-1-j-j;
if(d>0){
list.add(d);
}
}
else{
count+=a-1-j;
int d=j-a+1+j;
if(d>0){
list.add(d);
}
}
}
Collections.sort(list,(aa,b)->b-aa);
for(int j=1;j<=a;j++){
if(j<=list.size()){
count+=list.get(j-1);
}
sc.print(count+" ");
}
sc.println("");
}
//sc.print(0);
sc.bw.flush();
sc.bw.close();
}
}
//记住看数字范围,需要开long吗,需要用BigInteger吗,需要手动处理字符串吗,复杂度数量级控制在1e7或者以下了吗
//开数组的数据范围最高不能超过1e7,数据范围再大就要用哈希表离散化了
//基本数据类型不能自定义sort排序,二维数组就可以了
class Read{
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public Read(){
bf=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
bw=new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextLine() throws IOException{
return bf.readLine();
}
public String next() throws IOException{
while(!st.hasMoreTokens()){
st=new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException{
//确定下一个token只有一个字符的时候再用
return next().charAt(0);
}
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());
}
public float nextFloat() throws IOException{
return Float.parseFloat(next());
}
public byte nextByte() throws IOException{
return Byte.parseByte(next());
}
public short nextShort() throws IOException{
return Short.parseShort(next());
}
public BigInteger nextBigInteger() throws IOException{
return new BigInteger(next());
}
public void println(int a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
return;
}
public void print(int a) throws IOException{
bw.write(String.valueOf(a));
return;
}
public void println(String a) throws IOException{
bw.write(a);
bw.newLine();
return;
}
public void print(String a) throws IOException{
bw.write(a);
return;
}
public void println(long a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
return;
}
public void print(long a) throws IOException{
bw.write(String.valueOf(a));
return;
}
public void println(double a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
return;
}
public void print(double a) throws IOException{
bw.write(String.valueOf(a));
return;
}
public void print(BigInteger a) throws IOException{
bw.write(a.toString());
return;
}
public void print(char a) throws IOException{
bw.write(String.valueOf(a));
return;
}
public void println(char a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
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 17
|
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
|
1b29dbddc13794663df662dc2ec5f843
|
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.awt.*;
public class Main {
public static void main(String[] args) throws IOException {
var in=new FastInput();
StringBuffer res=new StringBuffer();
var t=in.getIntArray(1)[0];
for (int i = 0; i < t; i++) {
var n=in.getAutoIntArray()[0];
var s=in.in.readLine();
long sum=0;
PriorityQueue<Integer> p=new PriorityQueue<>((x,y)->y-x);
for (int j = 0; j < n; j++) {
if(s.charAt(j)=='L'){
sum+=j;
p.add(n-j-1-j);
}else{
sum+=n-j-1;
p.add(j-(n-j-1));
}
}
long[] r=new long[n];
for (int j = 0; j < r.length; j++) {
if(p.peek()>0){
sum+=p.poll();
r[j]=sum;
}else{
r[j]=sum;
}
}
res.append(in.getArrayString(r));
res.append("\n");
}
System.out.println(res.toString().substring(0,res.length()-1));
}
}
class FastInput {
BufferedReader in = null;
public FastInput() {
in = new BufferedReader(new InputStreamReader(System.in));
}
public int[] getAutoIntArray() throws IOException {
String[] data=in.readLine().split(" ");
int[] res=new int[data.length];
for (int i = 0; i < res.length; i++) {
res[i]=Integer.valueOf(data[i]);
}
return res;
}
public int[] getIntArray(int len) throws IOException {
int[] res = new int[len];
String[] data = in.readLine().split(" ");
for (int i = 0; i < res.length; i++) {
res[i] = Integer.valueOf(data[i]);
}
return res;
}
public ArrayList<Integer> getIntArrayList(int len) throws IOException {
ArrayList<Integer> res = new ArrayList<>();
String[] data = in.readLine().split(" ");
for (int i = 0; i < len; i++) {
res.add(Integer.valueOf(data[i]));
}
return res;
}
public Integer[] getIntegerArray(int len) throws IOException {
Integer[] res = new Integer[len];
String[] data = in.readLine().split(" ");
for (int i = 0; i < res.length; i++) {
res[i] = Integer.valueOf(data[i]);
}
return res;
}
public long[] getLongArray(int len) throws IOException {
long[] res = new long[len];
String[] data = in.readLine().split(" ");
for (int i = 0; i < res.length; i++) {
res[i] = Long.valueOf(data[i]);
}
return res;
}
public ArrayList<Long> getLongArrayList(int len) throws IOException {
ArrayList<Long> res = new ArrayList<>();
String[] data = in.readLine().split(" ");
for (int i = 0; i < len; i++) {
res.add(Long.valueOf(data[i]));
}
return res;
}
public double[] getDoubleArray(int len) throws IOException {
double[] res = new double[len];
String[] data = in.readLine().split(" ");
for (int i = 0; i < res.length; i++) {
res[i] = Double.valueOf(data[i]);
}
return res;
}
public int[] getStringToIntArray(int len) throws IOException {
int[] res=new int[len];
String s=in.readLine();
for (int i = 0; i < res.length; i++) {
if(s.charAt(i)=='0'){
res[i]=0;
}else{
res[i]=1;
}
}
return res;
}
public static void printArray(int[] x){
StringBuffer res=new StringBuffer();
for (int i = 0; i < x.length; i++) {
res.append(x[i]);
if(i+1!=x.length)
res.append(" ");
}
System.out.println(res);
}
public static void printArray(long[] x){
if(x==null) {
System.out.println("null");
return ;
}
StringBuffer res=new StringBuffer();
for (int i = 0; i < x.length; i++) {
res.append(x[i]);
if(i+1!=x.length)
res.append(" ");
}
System.out.println(res);
}
public static String getArrayString(long[] x){
if(x==null) {
return "null";
}
StringBuffer res=new StringBuffer();
for (int i = 0; i < x.length; i++) {
res.append(x[i]);
if(i+1!=x.length)
res.append(" ");
}
return res.toString();
}
public static String getArrayString(int[] x){
if(x==null) {
return "null";
}
StringBuffer res=new StringBuffer();
for (int i = 0; i < x.length; i++) {
res.append(x[i]);
if(i+1!=x.length)
res.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 17
|
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
|
8c106c8790f8ca9890501e4082bcba00
|
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 Vaibhav{
static long bit[];
static boolean prime[];
public static long lcm(long x, long y) {return (x * y) / gcd(x, y);}
static class Pair {//implements Comparable<Pair> {
int peldo;
int bachalo;
Pair(int peldo,int bachalo) {
this.peldo = peldo;
this.bachalo=bachalo;
}
/* //public int compareTo(Pair o){
return this.cqm-o.cqm;
}*/
}
//BIT
public static void update(long bit[], int i, int x) {for (; i < bit.length; i += (i & (-i))) {bit[i] += x;}}
public static long sum(int i) {long sum = 0;for (; i > 0; i -= (i & (-i))) {sum += bit[i];}return sum;}
static long power(long x, long y, long p) {if (y == 0) return 1;if (x == 0) return 0;long res = 1l;x = x % p;while (y > 0) {if (y % 2 == 1) res = (res * x) % p;y = y >> 1;x = (x * x) % p;}return res;}
static long power(long x, long y) {if (y == 0) return 1;if (x == 0) return 0;long res = 1;while (y > 0) {if (y % 2 == 1) res = (res * x);y = y >> 1;x = (x * x);}return res;}
static void sort(long[] a) {ArrayList<Long> l = new ArrayList<>();for (long i : a) l.add(i);Collections.sort(l);for (int i = 0; i < a.length; i++) a[i] = l.get(i);}
static class 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[] readlongArray(int n) {long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nextLong();return a;}long nextLong() {return Long.parseLong(next());}}
static void sieveOfEratosthenes(int n) {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;}}}
public static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
public static long gcd(long a, long b) {if (b == 0) {return a;}return gcd(b, a % b);}
static long nCk(int n, int k) {long res = 1;for (int i = n - k + 1; i <= n; ++i) res *= i;for (int i = 2; i <= k; ++i) res /= i;return res;}
static BigInteger bi(String str) {
return new BigInteger(str);
}
// Author - vaibhav_1710
static FastScanner fs = null;
static long ans;
static long mod = 1_000_000_007;
static ArrayList<Integer> al[];
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t =fs.nextInt();
outer: while (t-- > 0) {
int n = fs.nextInt();
String s = fs.next();
long sum=0l;
ArrayList<Long> al = new ArrayList<>();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='L'){
sum += i;
long val = (n-1-i);
val -= i;
al.add(val);
}else{
sum += (n-1-i);
long val = i;
val -= (n-1-i);
al.add(val);
}
}
Collections.sort(al,Collections.reverseOrder());
long pre[] = new long[n];
pre[0] = Math.max(al.get(0),0);
for(int i=1;i<n;i++){
pre[i] = pre[i-1] + Math.max(0,al.get(i));
}
for(int i=0;i<n;i++){
out.print((sum+pre[i])+" ");
}
out.println();
}
out.close();
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn the first test case: $$$k=1$$$: change the direction of $$$1$$$ person to make the line RLR. The total value is $$$2+1+0=3$$$. $$$k=2$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. $$$k=3$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. Note that you have to change the direction of at most $$$k$$$ people. In the second test case, it is optimal to only change the direction of the first person for all $$$k$$$ from $$$1$$$ to $$$5$$$ (that is, make the line RRRLL).
|
Java 17
|
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
|
7c804d68e410f7c3c1fefb5e55f14c4c
|
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;
static long cnt=0;
//problem linkhttps://leetcode.com/discuss/interview-question/algorithms/202924/ascend-online-assessment-product-of-palindromes
static int mod=998244353;
//static ArrayList<ArrayList<Integer>> arr=new ArrayList<>();
static StringBuilder ans=new StringBuilder("");
public static void main(String args[]) throws IOException {
// Your code goes here
br=new BufferedReader(new InputStreamReader(System.in));
int t=Int();
while(t-->0){
int n=Int();
char ch[]=br.readLine().toCharArray();
PriorityQueue<Integer> pq=new PriorityQueue<>(Collections.reverseOrder());
int i;
int dp[]=new int[n];
Arrays.fill(dp,0);
long sum=0;
for(i=0;i<n;i++){
if(ch[i]=='L')
dp[i]=i;
else{
dp[i]=n-i-1;
}
sum+=dp[i];
}
for(i=0;i<n;i++){
if(ch[i]=='L'){
if((n-i-1)>dp[i]){
pq.add(n-i-1-dp[i]);
}
}
else{
if(i>(n-i-1)){
pq.add(i-(n-i-1));
}
}
}
for(i=0;i<n;i++){
if(pq.size()>0){
sum+=(long)pq.poll();
}
ans.append(sum+" ");
}
ans.append("\n");
}
printString(ans.toString());
}
public static void addTO(TreeSet<int[]> max,TreeSet<int[]> min,int k,long maxvalue,long minvalue){
long res=0;
int mid;
mid=min.last()[0];
//System.out.println(mid+" "+maxvalue+" "+minvalue+" "+max.size()+" "+min.size());
long result=((long)maxvalue)-(long)mid*(long)max.size();
result+=Math.abs((long)((k-max.size())*(long)mid -(long)minvalue));
mid=max.first()[0];
res=((long)maxvalue)-(long)mid*(long)max.size();
res+=Math.abs((long)((k-max.size())*(long)mid -(long)minvalue));
ans.append(Math.min(res,result)+" ");
}
public static int memo(int dp[][],char ch[],int i,int j){
if(i==j)
return dp[i][j]=1;
else if(j-i+1==2&&ch[i]==ch[j]){
return dp[i][j]=2;
}
if(ch[i]==ch[j]){
return dp[i][j]=2+memo(dp,ch,i+1,j-1);
}
return dp[i][j]=Math.max(memo(dp,ch,i+1,j),memo(dp,ch,i,j-1));
}
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static void fun(int curr,int count,int x,int y,ArrayList<Integer> pres){
}
// public static mine min(mine x,mine y){
// if(x.a>y.a)
// return y;
// if(y.a>x.a)
// return x;
// if(x.b>y.b)
// return y;
// return x;
// }
public static int[] readInt(int n) throws IOException {
String str[]=br.readLine().split(" ");
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(str[i]);
return a;
}
public static ArrayList<Integer> readList(int n) throws IOException {
String str[]=br.readLine().split(" ");
ArrayList<Integer> arr=new ArrayList<>();
for(int i=0;i<n;i++)
arr.add(Integer.parseInt(str[i]));
return arr;
}
public static char[] readChar(int n) throws IOException {
String str=br.readLine();
char a[]=new char[n];
for(int i=0;i<n;i++)
a[i]=str.charAt(i);
return a;
}
public static long[] readLong(int n) throws IOException {
String str[]=br.readLine().split(" ");
long a[]=new long[n];
for(int i=0;i<n;i++)
a[i]=Long.parseLong(str[i]);
return a;
}
public static void printString(String str){
System.out.println(str);
}
public static void printInt(int str){
System.out.println(str);
}
public static void printLong(long str){
System.out.println(str);
}
public static int Int() throws IOException {
return Integer.parseInt(br.readLine());
}
public static long Long() throws IOException {
return Long.parseLong(br.readLine());
}
public static String[] readString() throws IOException {
return br.readLine().split(" ");
}
}
class mine{
long num;
int index;
public mine(long x,int y){
num=x;
index=y;
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn the first test case: $$$k=1$$$: change the direction of $$$1$$$ person to make the line RLR. The total value is $$$2+1+0=3$$$. $$$k=2$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. $$$k=3$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. Note that you have to change the direction of at most $$$k$$$ people. In the second test case, it is optimal to only change the direction of the first person for all $$$k$$$ from $$$1$$$ to $$$5$$$ (that is, make the line RRRLL).
|
Java 17
|
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
|
43e6761cb48e2d44285b40fd517d0a4b
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @Har_Har_Mahadev
*/
public class D {
public static void process() throws IOException {
int n = sc.nextInt();
String s = sc.next();
long ans = 0;
for(int i = 0; i<n; i++) {
if(s.charAt(i) == 'L')ans+=i;
else {
ans+=(n-i-1);
}
}
int i = 0, j = n-1;
int cc = 0;
int k = 0;
long arr[] = new long[n];
while(i<=j) {
if(cc == 0) {
if(s.charAt(i) == 'L') {
ans-=i;
ans+=(n-i-1);
arr[k++] = ans;
}
i++;
}
else {
if(s.charAt(j) == 'R') {
ans-=(n-j-1);
ans+=(j);
arr[k++] = ans;
}
j--;
}
cc^=1;
}
if(k == 0)arr[k++] = ans;
while(k<n) {
arr[k] = arr[k-1];
k++;
}
for(long e : arr)out.print(e+" ");
out.println();
}
//=============================================================================
//--------------------------The End---------------------------------
//=============================================================================
private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353;
private static int N = 0;
private static void google(int tt) {
System.out.print("Case #" + (tt) + ": ");
}
static FastScanner sc;
static FastWriter out;
public static void main(String[] args) throws IOException {
boolean oj = true;
if (oj) {
sc = new FastScanner();
out = new FastWriter(System.out);
} else {
sc = new FastScanner("input.txt");
out = new FastWriter("output.txt");
}
long s = System.currentTimeMillis();
int t = 1;
t = sc.nextInt();
int TTT = 1;
while (t-- > 0) {
// google(TTT++);
process();
}
out.flush();
// tr(System.currentTimeMillis()-s+"ms");
}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static void tr(Object... o) {
if (!oj)
System.err.println(Arrays.deepToString(o));
}
static class Pair implements Comparable<Pair> {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return Integer.compare(this.x, o.x);
}
/*
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair key = (Pair) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
*/
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static int ceil(int x, int y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long sqrt(long z) {
long sqz = (long) Math.sqrt(z);
while (sqz * 1L * sqz < z) {
sqz++;
}
while (sqz * 1L * sqz > z) {
sqz--;
}
return sqz;
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
public static long gcd(long a, long b) {
if (a > b)
a = (a + b) - (b = a);
if (a == 0L)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int lower_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = -1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > x) {
high = mid - 1;
} else {
ans = mid;
low = mid + 1;
}
}
return ans;
}
public static int upper_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = arr.length;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
static void ruffleSort(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void reverseArray(int[] a) {
int n = a.length;
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
//custom multiset (replace with HashMap if needed)
public static void push(TreeMap<Integer, Integer> map, int k, int v) {
//map[k] += v;
if (!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k) + v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v) {
//assumes map[k] >= v
//map[k] -= v
int lol = map.get(k);
if (lol == v)
map.remove(k);
else
map.put(k, lol - v);
}
// compress Big value to Time Limit
public static int[] compress(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1; //min value
for (int x : ls)
if (!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for (int i = 0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
// Fast Writer
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 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(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(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();
}
}
// Fast Inputs
static class FastScanner {
//I don't understand how this works lmao
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 FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(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[] readArray(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readArrayLong(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public int[][] readArrayMatrix(int N, int M, int Index) {
if (Index == 0) {
int[][] res = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
int[][] res = new int[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
public long[][] readArrayMatrixLong(int N, int M, int Index) {
if (Index == 0) {
long[][] res = new long[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = nextLong();
}
return res;
}
long[][] res = new long[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = 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[] readArrayDouble(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') {
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 17
|
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
|
fa7a65dab71c7d7b75d5421752e08c2e
|
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.awt.Rectangle;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
/**
*
* @author eslam
*/
public class Solution {
// Beginning of the solution
static Kattio input = new Kattio();
static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
static ArrayList<ArrayList<Integer>> powerSet = new ArrayList<>();
static ArrayList<LinkedList<Integer>> allprem = new ArrayList<>();
static ArrayList<LinkedList<String>> allprems = new ArrayList<>();
static ArrayList<Long> luc = new ArrayList<>();
static long mod = (long) (Math.pow(10, 9) + 7);
static int grid[][] = {{0, 0, 1, -1, 1, 1, -1, -1}, {1, -1, 0, 0, 1, -1, 1, -1}};
static int dp[];
static double cmp = 0.000000001;
public static void main(String[] args) throws IOException {
// Kattio input = new Kattio("cbarn");
// BufferedWriter log = new BufferedWriter(new FileWriter("cbarn.txt"));
int test = input.nextInt();
loop:
for (int o = 1; o <= test; o++) {
int n = input.nextInt();
String w = input.next();
long sum = 0;
PriorityQueue<pair> q = new PriorityQueue<>(new Comparator<pair>() {
@Override
public int compare(pair o1, pair o2) {
if (o1.y > o2.y) {
return -1;
} else if (o1.y < o2.y) {
return 1;
} else {
return 0;
}
}
});
for (int i = 0; i < n; i++) {
if (w.charAt(i) == 'L') {
q.add(new pair(i, n - i - 1));
sum += (long) i;
} else {
q.add(new pair(n - i - 1, i));
sum += (long) (n - i - 1);
}
}
long ans[] = new long[n];
int l = 0;
while (!q.isEmpty()) {
pair p = q.poll();
if (p.y > p.x) {
sum -= (long) p.x;
sum += (long) p.y;
}
ans[l] = sum;
l++;
}
for (int i = 0; i < n; i++) {
log.write(ans[i] + " ");
}
log.write("\n");
}
log.flush();
}
static class rec {
long x1;
long x2;
long y1;
long y2;
public rec(long x1, long y1, long x2, long y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public long getArea() {
return (x2 - x1) * (y2 - y1);
}
}
public static int bs(int b[], int va) {
int max = b.length - 1;
int min = 0;
int ans = -1;
while (max >= min) {
int mid = (max + min) / 2;
if (b[mid] >= va) {
ans = b[mid];
max = mid - 1;
} else {
min = mid + 1;
}
}
return ans;
}
static int sumOfRange(int x1, int y1, int x2, int y2, int e, int a[][][]) {
return (a[e][x2][y2] - a[e][x1 - 1][y2] - a[e][x2][y1 - 1]) + a[e][x1 - 1][y1 - 1];
}
public static int[][] bfs(int i, int j, String w[]) {
Queue<pair> q = new ArrayDeque<>();
q.add(new pair(i, j));
int dis[][] = new int[w.length][w[0].length()];
for (int k = 0; k < w.length; k++) {
Arrays.fill(dis[k], -1);
}
dis[i][j] = 0;
while (!q.isEmpty()) {
pair p = q.poll();
int cost = dis[p.x][p.y];
for (int k = 0; k < 4; k++) {
int nx = p.x + grid[0][k];
int ny = p.y + grid[1][k];
if (isValid(nx, ny, w.length, w[0].length())) {
if (dis[nx][ny] == -1 && w[nx].charAt(ny) == '.') {
q.add(new pair(nx, ny));
dis[nx][ny] = cost + 1;
}
}
}
}
return dis;
}
public static void dfs(int node, ArrayList<Integer> a[], boolean vi[], boolean closed[]) {
vi[node] = true;
for (Integer ch : a[node]) {
if (!vi[ch] && !closed[ch]) {
dfs(ch, a, vi, closed);
}
}
}
public static void graphRepresintion(ArrayList<Integer>[] a, int q) throws IOException {
for (int i = 0; i < a.length; i++) {
a[i] = new ArrayList<>();
}
while (q-- > 0) {
int x = input.nextInt();
int y = input.nextInt();
a[x].add(y);
a[y].add(x);
}
}
public static boolean isValid(int i, int j, int n, int m) {
return (i > -1 && i < n) && (j > -1 && j < m);
}
// present in the left and right indices
public static int[] swap(int data[], int left, int right) {
// Swap the data
int temp = data[left];
data[left] = data[right];
data[right] = temp;
// Return the updated array
return data;
}
// Function to reverse the sub-array
// starting from left to the right
// both inclusive
public static int[] reverse(int data[], int left, int right) {
// Reverse the sub-array
while (left < right) {
int temp = data[left];
data[left++] = data[right];
data[right--] = temp;
}
// Return the updated array
return data;
}
// Function to find the next permutation
// of the given integer array
public static boolean findNextPermutation(int data[]) {
// If the given dataset is empty
// or contains only one element
// next_permutation is not possible
if (data.length <= 1) {
return false;
}
int last = data.length - 2;
// find the longest non-increasing suffix
// and find the pivot
while (last >= 0) {
if (data[last] < data[last + 1]) {
break;
}
last--;
}
// If there is no increasing pair
// there is no higher order permutation
if (last < 0) {
return false;
}
int nextGreater = data.length - 1;
// Find the rightmost successor to the pivot
for (int i = data.length - 1; i > last; i--) {
if (data[i] > data[last]) {
nextGreater = i;
break;
}
}
// Swap the successor and the pivot
data = swap(data, nextGreater, last);
// Reverse the suffix
data = reverse(data, last + 1, data.length - 1);
// Return true as the next_permutation is done
return true;
}
public static pair[] dijkstra(int node, ArrayList<pair> a[]) {
PriorityQueue<tri> q = new PriorityQueue<>(new Comparator<tri>() {
@Override
public int compare(tri o1, tri o2) {
if (o1.y > o2.y) {
return 1;
} else if (o1.y < o2.y) {
return -1;
} else {
return 0;
}
}
});
q.add(new tri(node, 0, -1));
pair distance[] = new pair[a.length];
while (!q.isEmpty()) {
tri p = q.poll();
int cost = p.y;
if (distance[p.x] != null) {
continue;
}
distance[p.x] = new pair(p.z, cost);
ArrayList<pair> nodes = a[p.x];
for (pair node1 : nodes) {
if (distance[node1.x] == null) {
tri pa = new tri(node1.x, cost + node1.y, p.x);
q.add(pa);
}
}
}
return distance;
}
public static String revs(String w) {
String ans = "";
for (int i = w.length() - 1; i > -1; i--) {
ans += w.charAt(i);
}
return ans;
}
public static boolean isPalindrome(String w) {
for (int i = 0; i < w.length() / 2; i++) {
if (w.charAt(i) != w.charAt(w.length() - i - 1)) {
return false;
}
}
return true;
}
public static void getPowerSet(Queue<Integer> a) {
int n = a.poll();
if (!a.isEmpty()) {
getPowerSet(a);
}
int s = powerSet.size();
for (int i = 0; i < s; i++) {
ArrayList<Integer> ne = new ArrayList<>();
ne.add(n);
for (int j = 0; j < powerSet.get(i).size(); j++) {
ne.add(powerSet.get(i).get(j));
}
powerSet.add(ne);
}
ArrayList<Integer> p = new ArrayList<>();
p.add(n);
powerSet.add(p);
}
public static int getlo(int va) {
int v = 1;
while (v <= va) {
if ((va&v) != 0) {
return v;
}
v <<= 1;
}
return 0;
}
static long fast_pow(long a, long p, long mod) {
long res = 1;
while (p > 0) {
if (p % 2 == 0) {
a = (a * a) % mod;
p /= 2;
} else {
res = (res * a) % mod;
p--;
}
}
return res;
}
public static int countPrimeInRange(int n, boolean isPrime[]) {
int cnt = 0;
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = i * 2; j <= n; j += i) {
isPrime[j] = false;
}
}
}
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
cnt++;
}
}
return cnt;
}
public static void create(long num) {
luc.add(num);
if (num > power(10, 9)) {
return;
}
create(num * 10 + 4);
create(num * 10 + 7);
}
public static long ceil(long a, long b) {
return (a + b - 1) / b;
}
public static long round(long a, long b) {
if (a < 0) {
return (a - b / 2) / b;
}
return (a + b / 2) / b;
}
public static void allPremutationsst(LinkedList<String> l, boolean visited[], ArrayList<String> st) {
if (l.size() == st.size()) {
allprems.add(l);
}
for (int i = 0; i < st.size(); i++) {
if (!visited[i]) {
visited[i] = true;
LinkedList<String> nl = new LinkedList<>();
for (String x : l) {
nl.add(x);
}
nl.add(st.get(i));
allPremutationsst(nl, visited, st);
visited[i] = false;
}
}
}
public static void allPremutations(LinkedList<Integer> l, boolean visited[], int a[]) {
if (l.size() == a.length) {
allprem.add(l);
}
for (int i = 0; i < a.length; i++) {
if (!visited[i]) {
visited[i] = true;
LinkedList<Integer> nl = new LinkedList<>();
for (Integer x : l) {
nl.add(x);
}
nl.add(a[i]);
allPremutations(nl, visited, a);
visited[i] = false;
}
}
}
public static int binarySearch(long[] a, long value) {
int l = 0;
int r = a.length - 1;
while (l <= r) {
int m = (l + r) / 2;
if (a[m] == value) {
return m;
} else if (a[m] > value) {
r = m - 1;
} else {
l = m + 1;
}
}
return -1;
}
public static void reverse(int l, int r, char ch[]) {
for (int i = 0; i < r / 2; i++) {
char c = ch[i];
ch[i] = ch[r - i - 1];
ch[r - i - 1] = c;
}
}
public static int logK(long v, long k) {
int ans = 0;
while (v > 1) {
ans++;
v /= k;
}
return ans;
}
public static long power(long a, long n) {
if (n == 1) {
return a;
}
long pow = power(a, n / 2);
pow *= pow;
if (n % 2 != 0) {
pow *= a;
}
return pow;
}
public static long get(long max, long x) {
if (x == 1) {
return max;
}
int cnt = 0;
while (max > 0) {
cnt++;
max /= x;
}
return cnt;
}
public static int numOF0(long v) {
long x = 1;
int cnt = 0;
while (x <= v) {
if ((x & v) == 0) {
cnt++;
}
x <<= 1;
}
return cnt;
}
public static int log2(double n) {
int cnt = 0;
while (n > 1) {
n /= 2;
cnt++;
}
return cnt;
}
public static int[] bfs(int node, ArrayList<Integer> a[]) {
Queue<Integer> q = new LinkedList<>();
q.add(node);
int distances[] = new int[a.length];
Arrays.fill(distances, -1);
distances[node] = 0;
while (!q.isEmpty()) {
int parent = q.poll();
ArrayList<Integer> nodes = a[parent];
int cost = distances[parent];
for (Integer node1 : nodes) {
if (distances[node1] == -1) {
q.add(node1);
distances[node1] = cost + 1;
}
}
}
return distances;
}
public static int get(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
public static ArrayList<Integer> primeFactors(int n) {
ArrayList<Integer> a = new ArrayList<>();
while (n % 2 == 0) {
a.add(2);
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
a.add(i);
n /= i;
}
if (n < i) {
break;
}
}
if (n > 2) {
a.add(n);
}
return a;
}
public static ArrayList<Integer> printPrimeFactoriztion(int n) {
ArrayList<Integer> a = new ArrayList<>();
for (int i = 1; i < Math.sqrt(n) + 1; i++) {
if (n % i == 0) {
if (isPrime(i)) {
a.add(i);
n /= i;
i = 0;
} else if (isPrime(n / i)) {
a.add(n / i);
n = i;
i = 0;
}
}
}
return a;
}
// end of solution
public static BigInteger f(long n) {
if (n <= 1) {
return BigInteger.ONE;
}
long t = n - 1;
BigInteger b = new BigInteger(t + "");
BigInteger ans = new BigInteger(n + "");
while (t > 1) {
ans = ans.multiply(b);
b = b.subtract(BigInteger.ONE);
t--;
}
return ans;
}
public static long factorial(long n) {
if (n <= 1) {
return 1;
}
long t = n - 1;
while (t > 1) {
n = mod((mod(n, mod) * mod(t, mod)), mod);
t--;
}
return n;
}
public static long rev(long n) {
long t = n;
long ans = 0;
while (t > 0) {
ans = ans * 10 + t % 10;
t /= 10;
}
return ans;
}
public static boolean isPalindrome(int n) {
int t = n;
int ans = 0;
while (t > 0) {
ans = ans * 10 + t % 10;
t /= 10;
}
return ans == n;
}
static class tri {
int x, y, z;
public tri(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public String toString() {
return x + " " + y + " " + z;
}
}
static boolean isPrime(long num) {
if (num == 1) {
return false;
}
if (num == 2) {
return true;
}
if (num % 2 == 0) {
return false;
}
if (num == 3) {
return true;
}
for (int i = 3; i <= Math.sqrt(num) + 1; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static void prefixSum(int[] a) {
for (int i = 1; i < a.length; i++) {
a[i] = a[i] + a[i - 1];
}
}
public static void suffixSum(long[] a) {
for (int i = a.length - 2; i > -1; i--) {
a[i] = a[i] + a[i + 1];
}
}
static long mod(long a, long b) {
long r = a % b;
return r < 0 ? r + b : r;
}
public static long binaryToDecimal(String w) {
long r = 0;
long l = 0;
for (int i = w.length() - 1; i > -1; i--) {
long x = (w.charAt(i) - '0') * (long) Math.pow(2, l);
r = r + x;
l++;
}
return r;
}
public static String decimalToBinary(long n) {
String w = "";
while (n > 0) {
w = n % 2 + w;
n /= 2;
}
return w;
}
public static boolean isSorted(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] >= a[i + 1]) {
return false;
}
}
return true;
}
public static void print(int[] a) throws IOException {
for (int i = 0; i < a.length; i++) {
log.write(a[i] + " ");
}
log.write("\n");
}
public static void read(int[] a) {
for (int i = 0; i < a.length; i++) {
a[i] = input.nextInt();
}
}
static class pair {
int x;
int y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
}
static class pai {
long x;
int y;
public pai(long x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
}
public static long LCM(long x, long y) {
return x / GCD(x, y) * y;
}
public static long GCD(long x, long y) {
if (y == 0) {
return x;
}
return GCD(y, x % y);
}
public static void simplifyTheFraction(long a, long b) {
long GCD = GCD(a, b);
System.out.println(a / GCD + " " + b / GCD);
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() {
this(System.in, System.out);
}
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(problemName + ".out");
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
String nextLine() {
String str = "";
try {
str = r.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(r.readLine());
}
return st.nextToken();
} catch (Exception e) {
}
return null;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public 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 17
|
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
|
34172e34c68061ef93cb825e6d208d73
|
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 CF1722D extends PrintWriter {
CF1722D() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1722D o = new CF1722D(); o.main(); o.flush();
}
void main() {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
byte[] cc = sc.next().getBytes();
long sum = 0;
int[] kk = new int[n];
for (int i = 0; i < n; i++) {
int cnt = cc[i] == 'L' ? i : n - 1 - i;
sum += cnt;
kk[Math.max(n - 1 - cnt - cnt, 0)]++;
}
long[] aa = new long[n];
for (int k = 0, d = n - 1; d > 0; d--)
while (kk[d]-- > 0)
aa[k++] += d;
for (int k = 1; k < n; k++)
aa[k] += aa[k - 1];
for (int k = 0; k < n; k++)
print(sum + aa[k] + " ");
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 17
|
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
|
ee76120e90c20b5bc7d5b77822c6351b
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import java.util.Scanner;
public class Maxk {
public static void main (String [] args) {
Scanner sc = new Scanner(System.in);
int t= sc.nextInt();
while(t!=0) {
int n = sc.nextInt();
String s = sc.next();
long sum = 0;
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L')
sum = sum + i;
else
sum = sum + n - i - 1;
}
int l = 0;
int r = n - 1;
boolean done = false;
for (int k = 0; k < n; k++) {
int op = 1;
while (l <=r && op != 0) {
if(!done ) {
if (s.charAt(l) != 'R') {
sum = sum + n - l - 1 - l;
op--;
}
done = true;
l++;
}
else {
if (s.charAt(r) != 'L') {
sum = sum + r - (n - r - 1);
op--;
}
done = false;
r--;
}
}
if(k==n-1) System.out.println(sum);
else
System.out.print(sum + " ");
}
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
|
394dec249cc90373fbdbc1d0d10b9b57
|
train_109.jsonl
|
1661871000
|
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.
|
256 megabytes
|
import java.util.Scanner;
public class Line_1722D {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
String word = sc.next();
long sum = 0;
long tries = 0;
for (int j = 0; j < n; j++) {
if (word.charAt(j) != 'L') {
sum += n - j - 1;
} else {
sum += j;
}
}
int maxL = n - 1;
int maxR = 0;
while (maxL >= (n-1)/ 2 && word.charAt(maxL) == 'L')
maxL--;
while (maxR <= (n-1) / 2 && word.charAt(maxR) == 'R')
maxR++;
while (maxL >= (n-1) / 2.0 || maxR <= (n-1) / 2.0) {
if (n - maxL < maxR + 1) {
sum -= n - maxL - 1;
sum += maxL;
tries++;
maxL--;
System.out.print(sum + " ");
} else {
sum += n - maxR - 1;
sum -= maxR;
tries++;
maxR++;
System.out.print(sum + " ");
}
while (maxL >= (n-1)/ 2 && word.charAt(maxL) == 'L')
maxL--;
while (maxR <= (n-1) / 2 && word.charAt(maxR) == 'R')
maxR++;
}
for (int j = 0; j < n - tries; j++) {
System.out.print(sum + " ");
}
System.out.println();
}
}
}
|
Java
|
["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"]
|
2 seconds
|
["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"]
|
NoteIn the first test case: $$$k=1$$$: change the direction of $$$1$$$ person to make the line RLR. The total value is $$$2+1+0=3$$$. $$$k=2$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. $$$k=3$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. Note that you have to change the direction of at most $$$k$$$ people. In the second test case, it is optimal to only change the direction of the first person for all $$$k$$$ from $$$1$$$ to $$$5$$$ (that is, make the line RRRLL).
|
Java 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
|
f8cad66c0f43ab1f371d07406725e3eb
|
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 Hello {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
while (testCases -- > 0)
{
int n = sc.nextInt();
String input = sc.next();
int k = 0;
int i = 0;
int j = input.length() - 1;
long sum = 0;
ArrayList<Integer> changes = new ArrayList<>();
while (i < j)
{
if(input.charAt(i) == 'L') {
k++;
int original = i;
int changed = n - i - 1;
int diff = changed - original;
if(diff != 0)
changes.add(diff);
}
if(input.charAt(j) == 'R') {
int original = n - j - 1;
int changed = j;
int diff = changed - original;
if(diff != 0)
changes.add(diff);
k++;
}
sum += n - i - 1;
sum += j;
i++;
j--;
}
if(n % 2 == 1)
sum += j;
Collections.sort(changes);
long[] prefixSum = new long[changes.size()];
if(k == 0) {
for(int x = 0; x < n; x++)
{
if(x != 0)
System.out.print(" ");
if(n % 2 == 1)
System.out.print(sum);
else
System.out.print(sum);
}
}
else
{
int x = 0;
for(int element : changes)
{
if(x == 0)
prefixSum[x] = element;
else
prefixSum[x] = prefixSum[x - 1] + element;
x++;
}
for(int y = 1; y <= n; y++)
{
if(y != 1)
System.out.print(" ");
if(y >= k) {
System.out.print(sum);
}
else
{
// changes is of length 5 we need 5 changes one gave me 2 one gave me 3 one me 4 one gave me 5
// i can make y changes y < k i can make 10 changes k is 10
System.out.print(sum - prefixSum[k - y - 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
|
c949f0f88af7930a1b88c2d2d36bce57
|
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.StringTokenizer;
public class D_Line {
static Scanner in = new Scanner();
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans = new StringBuilder();
static int testCases, n, m;
static char x[], y[];
static void solve(int t) {
if (n == 1) {
ans.append(0);
} else {
long score = 0;
ArrayList1<Integer> list = new ArrayList1<>();
for (int i = 0; i < n; ++i) {
if (x[i] == 'L') {
score += i;
} else {
score += n - i - 1;
}
if (x[i] == 'L') {
list.add(n - 1 - 2 * i);
} else if (x[i] == 'R') {
list.add(2 * i - n + 1);
}
}
long a[] = new long[list.size()];
int index = 0;
while (!list.isEmpty()) {
a[index++] = list.get(0);
list.popFront();
}
sort(a, 0, a.length - 1);
reverse(a);
long sum = 0;
for (long i : a) {
sum += Math.max(0, i);
ans.append( (sum + score) ).append(" ");
}
/*int sz = a.length;
for (int i = sz; i < n; ++i) {
//sum += a[i];
ans.append((sum + score)).append(" ");
}*/
}
if (t != testCases) {
ans.append("\n");
}
}
public static void main(String[] Amit) throws IOException {
testCases = in.nextInt();
for (int t = 0; t < testCases; ++t) {
n = in.nextInt();
x = in.next().toCharArray();
solve(t + 1);
}
in.close();
out.print(ans.toString());
out.flush();
}
static int search(long a[], long x, int last) {
int i = 0, j = last;
while (i <= j) {
int mid = i + (j - i) / 2;
if (a[mid] == x) {
return mid;
}
if (a[mid] < x) {
i = mid + 1;
} else {
j = mid - 1;
}
}
return -1;
}
static void swap(long a[], int i, int j) {
long temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void reverse(long a[]) {
int n = a.length;
for (int i = 0; i < n / 2; ++i) {
swap(a, i, n - i - 1);
}
}
static long max(long a[], int i, int n, long max) {
if (i > n) {
return max;
}
max = Math.max(a[i], max);
return max(a, i + 1, n, max);
}
static long min(long a[], int i, int n, long max) {
if (i > n) {
return max;
}
max = Math.min(a[i], max);
return max(a, i + 1, n, max);
}
static void printArray(long a[]) {
for (long i : a) {
System.out.print(i + " ");
}
System.out.println();
}
static boolean isSmaller(String str1, String str2) {
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2) {
return true;
}
if (n2 < n1) {
return false;
}
for (int i = 0; i < n1; i++) {
if (str1.charAt(i) < str2.charAt(i)) {
return true;
} else if (str1.charAt(i) > str2.charAt(i)) {
return false;
}
}
return false;
}
static String sub(String str1, String str2) {
if (isSmaller(str1, str2)) {
String t = str1;
str1 = str2;
str2 = t;
}
String str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n1 - n2;
int carry = 0;
for (int i = n2 - 1; i >= 0; i--) {
int sub
= (((int) str1.charAt(i + diff) - (int) '0')
- ((int) str2.charAt(i) - (int) '0')
- carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
} else {
carry = 0;
}
str += String.valueOf(sub);
}
for (int i = n1 - n2 - 1; i >= 0; i--) {
if (str1.charAt(i) == '0' && carry > 0) {
str += "9";
continue;
}
int sub = (((int) str1.charAt(i) - (int) '0')
- carry);
if (i > 0 || sub > 0) {
str += String.valueOf(sub);
}
carry = 0;
}
return new StringBuilder(str).reverse().toString();
}
static String sum(String str1, String str2) {
if (str1.length() > str2.length()) {
String t = str1;
str1 = str2;
str2 = t;
}
String str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n2 - n1;
int carry = 0;
for (int i = n1 - 1; i >= 0; i--) {
int sum = ((int) (str1.charAt(i) - '0')
+ (int) (str2.charAt(i + diff) - '0') + carry);
str += (char) (sum % 10 + '0');
carry = sum / 10;
}
for (int i = n2 - n1 - 1; i >= 0; i--) {
int sum = ((int) (str2.charAt(i) - '0') + carry);
str += (char) (sum % 10 + '0');
carry = sum / 10;
}
if (carry > 0) {
str += (char) (carry + '0');
}
return new StringBuilder(str).reverse().toString();
}
static long detect_sum(int i, long a[], long sum) {
if (i >= a.length) {
return sum;
}
return detect_sum(i + 1, a, sum + a[i]);
}
static String mul(String num1, String num2) {
int len1 = num1.length();
int len2 = num2.length();
if (len1 == 0 || len2 == 0) {
return "0";
}
int result[] = new int[len1 + len2];
int i_n1 = 0;
int i_n2 = 0;
for (int i = len1 - 1; i >= 0; i--) {
int carry = 0;
int n1 = num1.charAt(i) - '0';
i_n2 = 0;
for (int j = len2 - 1; j >= 0; j--) {
int n2 = num2.charAt(j) - '0';
int sum = n1 * n2 + result[i_n1 + i_n2] + carry;
carry = sum / 10;
result[i_n1 + i_n2] = sum % 10;
i_n2++;
}
if (carry > 0) {
result[i_n1 + i_n2] += carry;
}
i_n1++;
}
int i = result.length - 1;
while (i >= 0 && result[i] == 0) {
i--;
}
if (i == -1) {
return "0";
}
String s = "";
while (i >= 0) {
s += (result[i--]);
}
return s;
}
static class Node<T> {
T data;
Node<T> next;
public Node() {
this.next = null;
}
public Node(T data) {
this.data = data;
this.next = null;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
@Override
public String toString() {
return this.getData().toString() + " ";
}
}
static class ArrayList1<T> {
Node<T> head, tail;
int len;
public ArrayList1() {
this.head = null;
this.tail = null;
this.len = 0;
}
int size() {
return len;
}
boolean isEmpty() {
return len == 0;
}
int indexOf(T data) {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
int index = -1, i = 0;
while (temp != null) {
if (temp.getData() == data) {
index = i;
}
i++;
temp = temp.getNext();
}
return index;
}
void add(T data) {
Node<T> newNode = new Node<>(data);
if (isEmpty()) {
head = newNode;
tail = newNode;
len++;
} else {
tail.setNext(newNode);
tail = newNode;
len++;
}
}
void see() {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
while (temp != null) {
System.out.print(temp.getData().toString() + " ");
//out.flush();
temp = temp.getNext();
}
System.out.println();
//out.flush();
}
void inserFirst(T data) {
Node<T> newNode = new Node<>(data);
Node<T> temp = head;
if (isEmpty()) {
head = newNode;
tail = newNode;
len++;
} else {
newNode.setNext(temp);
head = newNode;
len++;
}
}
T get(int index) {
if (isEmpty() || index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
if (index == 0) {
return head.getData();
}
Node<T> temp = head;
int i = 0;
T data = null;
while (temp != null) {
if (i == index) {
data = temp.getData();
}
i++;
temp = temp.getNext();
}
return data;
}
void addAt(T data, int index) {
if (index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> newNode = new Node<>(data);
int i = 0;
Node<T> temp = head;
while (temp.next != null) {
if (i == index) {
newNode.setNext(temp.next);
temp.next = newNode;
}
i++;
temp = temp.getNext();
}
// temp.setNext(temp);
len++;
}
void popFront() {
if (isEmpty()) {
//return;
throw new ArrayIndexOutOfBoundsException();
}
if (head == tail) {
head = null;
tail = null;
} else {
head = head.getNext();
}
len--;
}
void removeAt(int index) {
if (index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
if (index == 0) {
this.popFront();
return;
}
Node<T> temp = head;
int i = 0;
Node<T> n = new Node<>();
while (temp != null) {
if (i == index) {
n.next = temp.next;
temp.next = n;
break;
}
i++;
n = temp;
temp = temp.getNext();
}
tail = n;
--len;
}
void clearAll() {
this.head = null;
this.tail = null;
}
}
static void merge(long a[], int left, int right, int mid) {
int n1 = mid - left + 1, n2 = right - mid;
long L[] = new long[n1];
long R[] = new long[n2];
for (int i = 0; i < n1; i++) {
L[i] = a[left + i];
}
for (int i = 0; i < n2; i++) {
R[i] = a[mid + 1 + i];
}
int i = 0, j = 0, k1 = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
a[k1] = L[i];
i++;
} else {
a[k1] = R[j];
j++;
}
k1++;
}
while (i < n1) {
a[k1] = L[i];
i++;
k1++;
}
while (j < n2) {
a[k1] = R[j];
j++;
k1++;
}
}
static void sort(long a[], int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
sort(a, left, mid);
sort(a, mid + 1, right);
merge(a, left, right, mid);
}
static class Scanner {
BufferedReader in;
StringTokenizer st;
public Scanner() {
in = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
String nextLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
void close() throws IOException {
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
|
c7d81a64186af6a84f909551394011ad
|
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 {
static PrintWriter out;
static int MOD = 1000000007;
static FastReader scan;
/*-------- I/O usaing short named function ---------*/
public static String ns() {
return scan.next();
}
public static int ni() {
return scan.nextInt();
}
public static long nl() {
return scan.nextLong();
}
public static double nd() {
return scan.nextDouble();
}
public static String nln() {
return scan.nextLine();
}
public static void p(Object o) {
out.print(o);
}
public static void ps(Object o) {
out.print(o + " ");
}
public static void pn(Object o) {
out.println(o);
}
/*-------- for output of an array ---------------------*/
static void iPA(int arr[]) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < arr.length; i++) output.append(arr[i] + " ");
out.println(output);
}
static void lPA(long arr[]) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < arr.length; i++) output.append(arr[i] + " ");
out.println(output);
}
static void sPA(String arr[]) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < arr.length; i++) output.append(arr[i] + " ");
out.println(output);
}
static void dPA(double arr[]) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < arr.length; i++) output.append(arr[i] + " ");
out.println(output);
}
/*-------------- for input in an array ---------------------*/
static void iIA(int arr[]) {
for (int i = 0; i < arr.length; i++) arr[i] = ni();
}
static void lIA(long arr[]) {
for (int i = 0; i < arr.length; i++) arr[i] = nl();
}
static void sIA(String arr[]) {
for (int i = 0; i < arr.length; i++) arr[i] = ns();
}
static void dIA(double arr[]) {
for (int i = 0; i < arr.length; i++) arr[i] = nd();
}
/*------------ for taking input faster ----------------*/
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;
}
}
// Method to check if x is power of 2
static boolean isPowerOfTwo(int x) {
return x != 0 && ((x & (x - 1)) == 0);
}
//Method to return lcm of two numbers
static int gcd(int a, int b) {
return a == 0 ? b : gcd(b % a, a);
}
//Method to count digit of a number
static int countDigit(long n) {
return (int) Math.floor(Math.log10(n) + 1);
}
//Method for sorting
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);
}
//Method for checking if a number is prime or not
static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6) if (
n % i == 0 || n % (i + 2) == 0
) return false;
return true;
}
public static void reverse(int a[], int n)
{
int i, k, t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
// printing the reversed array
System.out.println("Reversed array is: \n");
for (k = 0; k < n; k++) {
System.out.println(a[k]);
}
}
public static int binarysearch(int arr[],int left,int right,int num){
int idx=0;
while(left<=right){
int mid=(left+right)/2;
if(arr[mid]>=num){
idx=mid;
if(arr[mid]==num)break;
right=mid-1;
}else{
left=mid+1;
}
}
return idx;
}
public static void main(String[] args) throws java.lang.Exception {
OutputStream outputStream = System.out;
out = new PrintWriter(outputStream);
scan = new FastReader();
//for fast output sometimes
HashSet<Integer> set = new HashSet<>();
StringBuilder sb = new StringBuilder();
int t = ni();
while(t-->0){
int k=ni();
String str=scan.nextLine();
long ini=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='L'){
ini+=i;
}else{
ini+=str.length()-i-1;
}
}
int st=0,en=str.length()-1;
for(int i=1;i<=k;i++){
int val1=-1,val2=-1;
// if(st==str.length())st=str.length()/2+1;
if((st<str.length()/2)&&str.charAt(st)=='L'){
val1=str.length()-st-1;
}
if((en>=str.length()/2)&&str.charAt(en)=='R'){
val2=en;
}
// System.out.println(en+" "+val2);
if((str.length()%2==0&&st>=str.length()/2&&en<str.length()/2)){
System.out.print(ini+" ");
continue;
}
if(str.length()%2!=0&&st>str.length()/2&&en<str.length()/2){
System.out.print(ini+" ");
continue;
}
if(val1==-1&&val2==-1){
st++;
en--;
i--;
continue;
}else{
if(val1==-1){
ini-=str.length()-en-1;
ini+=en;
en--;
st++;
}else if(val2==-1){
ini-=st;
ini+=str.length()-st-1;
st++;
en--;
}else{
if(val1>val2){
ini-=st;
ini+=str.length()-st-1;
st++;
}else{
ini-=str.length()-en-1;
ini+=en;
en--;
}
}
}
System.out.print(ini+" ");
}
System.out.println();
}
out.flush();
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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.