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
sequencelengths 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 | 194d37ce449b18ecee56bf3735e629e1 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes |
import java.io.*;
import java.util.*;
public class cp {
static PrintWriter w = new PrintWriter(System.out);
static FastScanner s = new FastScanner();
static int mod = 1000000007;
static class Edge {
int src;
int wt;
int nbr;
Edge(int src, int nbr, int et) {
this.src = src;
this.wt = et;
this.nbr = nbr;
}
}
class EdgeComparator implements Comparator<Edge> {
@Override
//return samllest elemnt on polling
public int compare(Edge s1, Edge s2) {
if (s1.wt < s2.wt) {
return -1;
} else if (s1.wt > s2.wt) {
return 1;
}
return 0;
}
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static void prime_till_n(boolean[] prime) {
// Create a boolean array
// "prime[0..n]" and
// initialize all entries
// it as true. A value in
// prime[i] will finally be
// false if i is Not a
// prime, else true.
for (int p = 2; p * p < prime.length; p++) {
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * p; i < prime.length; i += p) {
prime[i] = false;
}
}
}
// int l = 1;
// for (int i = 2; i <= n; i++) {
// if (prime[i]) {
// w.print(i+",");
// arr[i] = l;
// l++;
// }
// }
//Time complexit Nlog(log(N))
}
static int noofdivisors(int n) {
//it counts no of divisors of every number upto number n
int arr[] = new int[n + 1];
for (int i = 1; i <= (n); i++) {
for (int j = i; j <= (n); j = j + i) {
arr[j]++;
}
}
return arr[0];
}
static char[] reverse(char arr[]) {
char[] b = new char[arr.length];
int j = arr.length;
for (int i = 0; i < arr.length; i++) {
b[j - 1] = arr[i];
j = j - 1;
}
return b;
}
static long factorial(int n) {
if (n == 0) {
return 1;
}
long su = 1;
for (int i = 1; i <= n; i++) {
su *= (long) i;
}
return su;
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
long[] readArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
static class Vertex {
int x;
int y;
int wt;
public Vertex(int x, int y) {
this.x = x;
this.y = y;
}
public Vertex(int x, int y, int wt) {
this.x = x;
this.y = y;
this.wt = wt;
}
}
public static long power(long x, int n)
{
if (n == 0)
return 1l;
long pow = power(x, n / 2) % mod;
if ((n & 1) == 1) // if `y` is odd
return ((((x%mod) * (pow%mod))%mod) * (pow%mod))%mod;
// otherwise, `y` is even
return ((pow%mod) * (pow%mod))%mod;
}
public static void main(String[] args) {
{
int t = s.nextInt();
// int t = 1;
while (t-- > 0) {
solve();
}
w.close();
}
}
public static void solve() {
String str = s.next();
char[] ch = str.toCharArray();
Map<Character,TreeSet<Integer>> mp = new HashMap<>();
for(int i=0;i<ch.length;i++)
{
if(!mp.containsKey(ch[i]))
{
TreeSet<Integer> set = new TreeSet<>();
set.add(i);
mp.put(ch[i],set);
}
else
{
TreeSet<Integer> set = mp.get(ch[i]);
set.add(i);
mp.put(ch[i],set);
}
}
if(ch[0]<ch[ch.length-1])
{
StringBuilder ans1= new StringBuilder("");long cost =0; int jumps=0; int prev=ch[0];
for(char i= ch[0];i<=ch[ch.length-1];i++)
{
if(mp.containsKey(i))
{
for(int j:mp.get(i))
{
ans1.append(String.valueOf(j+1)+" ");
jumps++;
}
cost += Math.abs(i-prev);
prev=i;
}
}
w.println(cost+" "+jumps);
w.println(ans1.toString());
}
else if(ch[0]>ch[ch.length-1])
{
StringBuilder ans1= new StringBuilder("");
long cost =0; int jumps=0; char prev=ch[0];
for(char i= ch[0];i>=ch[ch.length-1];i--)
{
if(mp.containsKey(i))
{
for(int j:mp.get(i))
{
ans1.append(String.valueOf(j+1)+" ");
jumps++;
}
cost += Math.abs(i-prev);
prev=i;
}
}
w.println(cost+" "+jumps);
w.println(ans1.toString());
}
else
{
StringBuilder ans1= new StringBuilder("");int cost =0; int jumps=0;
if(mp.containsKey(ch[0]))
{
for(int j:mp.get(ch[0]))
{
ans1.append(String.valueOf(j+1)+" ");
jumps++;
}
}
w.println(cost+" "+jumps);
w.println(ans1.toString());
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | eb8f226006f5b86b176352d76cbf1c80 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for (int tNum = 1; tNum <= t; tNum++) {
String str = br.readLine();
int N = str.length();
int start = str.charAt(0) - 'a';
int end = str.charAt(N - 1) - 'a';
List<Integer>[] indexs = new ArrayList[26];
for (int i = 0; i < 26; i++) {
indexs[i] = new ArrayList<>();
}
for (int i = 1; i < N - 1; i++) {
indexs[str.charAt(i) - 'a'].add(i + 1);
}
if (start == end) {
sb.append("0 ").append(indexs[start].size() + 2).append("\n1 ");
for(int idx : indexs[start]) {
sb.append(idx).append(" ");
}
sb.append(N).append("\n");
continue;
}
int total = 2;
for(int i = Math.min(start, end); i <= Math.max(start, end); i++) {
total += indexs[i].size();
}
sb.append(Math.abs(end - start)).append(" ").append(total).append("\n");
sb.append(1).append(" ");
if (start < end) {
for(int i = start; i <= end; i++) {
for(int idx : indexs[i]) {
sb.append(idx).append(" ");
}
}
}
else {
for(int i = start; i >= end; i--) {
for(int idx : indexs[i]) {
sb.append(idx).append(" ");
}
}
}
sb.append(N).append("\n");
}
System.out.print(sb.toString());
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | bfef9b9d60f162fc8608ac41cd16af4b | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | // package javarep;
/*
/$$$$$ /$$$$$$ /$$ /$$ /$$$$$$
|__ $$ /$$__ $$ |$$ |$$ /$$__ $$
| $$| $$ \ $$| $$|$$| $$ \ $$
| $$| $$$$$$$$| $$ / $$/| $$$$$$$$
/ $$ | $$| $$__ $$ \ $$ $$/ | $$__ $$
| $$ | $$| $$ | $$ \ $$$/ | $$ | $$
| $$$$$$/| $$ | $$ \ $/ | $$ | $$
\______/ |__/ |__/ \_/ |__/ |__/
/$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$
| $$__ $$| $$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$$ /$$$| $$_____/| $$__ $$
| $$ \ $$| $$ \ $$| $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$$$ /$$$$| $$ | $$ \ $$
| $$$$$$$/| $$$$$$$/| $$ | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$ | $$$$$$$/
| $$____/ | $$__ $$| $$ | $$| $$|_ $$| $$__ $$| $$__ $$| $$ $$$| $$| $$ $$$| $$| $$__/ | $$__ $$
| $$ | $$ \ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$| $$\ $ | $$| $$ | $$ \ $$
| $$ | $$ | $$| $$$$$$/| $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$| $$ \/ | $$| $$$$$$$$| $$ | $$
|__/ |__/ |__/ \______/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/ |__/|________/|__/ |__/
*/
import java.io.BufferedReader;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.io.*;
public class abc {
static PrintWriter pw;
/// MAIN FUNCTION///
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
pw = new PrintWriter(System.out);
// use arraylist as it uses the concept of dynamic table(amortized analysis)
// Arrays.stream(array).forEach(a -> Arrays.fill(a, 0));
/* List<Integer> l1 = new ArrayList<Integer>(); */
// Random rand = new Random();
// int t = sc.nextInt();
int t = sc.nextInt();
while (t-- > 0) {
// int l=sc.nextInt();
String s = sc.nextLine();
int l = s.length();
ArrayList<Integer>[] idx = new ArrayList[26];
for (int i = 0; i < 26; i++)
idx[i] = new ArrayList<Integer>();
TreeSet<Character> tr = new TreeSet<Character>();
tr.add((char) ('a' - 1));
tr.add((char) ('z' + 1));
for (int i = 0; i < s.length(); i++) {
idx[s.charAt(i) - 'a'].add(i + 1);
tr.add(s.charAt(i));
}
StringBuilder ans = new StringBuilder();
int st = Math.abs(s.charAt(s.length() - 1) - s.charAt(0));
int js = 0;
char lst = s.charAt(s.length() - 1);
for (char c = s.charAt(0);;) {
js += idx[c - 'a'].size();
for (int x : idx[c - 'a'])
ans.append(x + " ");
if (c == lst) {
break;
}
if (c < lst) {
c = tr.higher(c);
} else {
c = tr.lower(c);
}
}
pw.println(st + " " + js);
pw.println(ans);
}
pw.flush();
}
public static int perfectSum(int arr[], int n, int sum) {
int dp[][] = new int[n + 1][sum + 1];
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < sum + 1; j++) {
if (i == 0)
dp[i][j] = 0;
if (j == 0)
dp[i][j] = 1;
}
}
for (int i = 1; i < n + 1; i++) {
for (int j = 1; j < sum + 1; j++) {
if (arr[i - 1] <= j)
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - arr[i - 1]];
else
dp[i][j] = dp[i - 1][j];
dp[i][j] = dp[i][j];
}
}
return dp[n][sum];
}
public static boolean isLsbOne(int n) {
if ((n & 1) != 0)
return true;
return false;
}
public static void debugger() {
Random rand = new Random();
int tst = (int) (Math.abs(rand.nextInt()) % 2 + 1);
pw.println(tst);
while (tst-- > 0) {
int n = (int) (Math.abs(rand.nextInt()) % 5 + 1);
pw.println(n);
for (int i = 0; i < n; i++) {
pw.print((int) (Math.abs(rand.nextInt()) % 6 + 1) + " ");
}
pw.println();
}
}
static int UpperBound(long a[], long x) {// x is the key or target value
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] <= x)
l = m;
else
r = m;
}
return l + 1;
}
static int LowerBound(long a[], long x) { // x is the target value or key
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] >= x)
r = m;
else
l = m;
}
return r;
}
static void recursion(int n) {
if (n == 1) {
pw.print(n + " ");
return;
}
// pw.print(n+" "); gives us n to 1
recursion(n - 1);
// pw.print(n+" "); gives us 1 to n
}
// ch.charAt(i)+"" converts into a char sequence
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;
}
/* CREATED BY ME */
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;
}
void swap(int arr[], int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
public static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
public static boolean isPrime(long n) {
if (n == 2)
return true;
long i = 2;
while (i * i <= n) {
if (n % i == 0)
return false;
i++;
}
return true;
}
static void sort(int arr[]) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : arr)
l.add(i);
Collections.sort(l);
for (int i = 0; i < arr.length; i++)
arr[i] = l.get(i);
}
static void sort(long arr[]) {
ArrayList<Long> l = new ArrayList<>();
for (long i : arr)
l.add(i);
Collections.sort(l);
for (int i = 0; i < arr.length; i++)
arr[i] = l.get(i);
}
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 double max(double x, double y) {
return Math.max(x, y);
}
static int min(int x, int y) {
return Math.min(x, y);
}
static int abs(int x) {
return Math.abs(x);
}
static long abs(long x) {
return Math.abs(x);
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
static long max(long x, long y) {
return Math.max(x, y);
}
static long min(long x, long y) {
return Math.min(x, y);
}
public static class pair {
String x;
int y;
public pair(String a, int b) {
x = a;
y = b;
}
}
public static class Comp implements Comparator<pair> {
public int compare(pair a, pair b) {
int ans = a.x.compareTo(b.x);
// if (ans > 0)
// return 1;
// if (ans < 0)
// return -1;
return ans;
}
}
// modular exponentiation
public static long fastExpo(long a, int n, int mod) {
if (n == 0)
return 1;
else {
if ((n & 1) == 1) {
long x = fastExpo(a, n / 2, mod);
return (((a * x) % mod) * x) % mod;
} else {
long x = fastExpo(a, n / 2, mod);
return (((x % mod) * (x % mod)) % mod) % mod;
}
}
}
public static long modInverse(long n, int p) {
return fastExpo(n, p - 2, p);
}
/*
* public static void extract(ArrayList<Integer> ar, int k, int d) { int c = 0;
* for (int i = 1; i < k; i++) { int x = 0; boolean dm = false; while (x > 0) {
* long dig = x % 10; x = x / 10; if (dig == d) { dm = true; break; } } if (dm)
* ar.add(i); } }
*/
public static int[] prefixfuntion(String s) {
int n = s.length();
int z[] = new int[n];
for (int i = 1; i < n; i++) {
int j = z[i - 1];
while (j > 0 && s.charAt(i) != s.charAt(j))
j = z[j - 1];
if (s.charAt(i) == s.charAt(j))
j++;
z[i] = j;
}
return z;
}
// counts the set(1) bit of a number
public static long countSetBitsUtil(long x) {
if (x <= 0)
return 0;
return (x % 2 == 0 ? 0 : 1) + countSetBitsUtil(x / 2);
}
//tells whether a particular index has which bit of a number
public static int getIthBitsUtil(int x, int y) {
return (x & (1 << y)) != 0 ? 1 : 0;
}
public static void swaper(long x, long y) {
x = x ^ y;
y = y ^ x;
x = x ^ y;
}
public static double decimalPlaces(double sum) {
DecimalFormat df = new DecimalFormat("#.00");
String angleFormated = df.format(sum);
double fin = Double.parseDouble(angleFormated);
return fin;
}
//use collections.swap for swapping
static boolean isSubSequence(String str1, String str2, int m, int n) {
int j = 0;
for (int i = 0; i < n && j < m; i++)
if (str1.charAt(j) == str2.charAt(i))
j++;
return (j == m);
}
static long sum(long n) {
long s2 = 0, max = -1, min = 10;
while (n > 0) {
s2 = (n % 10);
min = min(s2, min);
max = max(s2, max);
n = n / 10;
}
return max * min;
}
static long pow(long base, long power) {
if (power == 0) {
return 1;
}
long result = pow(base, power / 2);
if (power % 2 == 1) {
return result * result * base;
}
return result * result;
}
// return the hash value of a string
static long compute_hash(String s) {
long val = 0;
long p = 31;
long mod = (long) (1000000007);
long pow = 1;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
val = (val + (int) (ch - 'a' + 1) * pow) % mod;
pow = (pow * p) % mod;
}
return val;
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | c24a85710b2d0e6fb9c48474eac94855 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | // package faltu;
import java.util.*;
import java.util.Map.Entry;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
// ***********************MATHS--STARTS************************************************* //
private static ArrayList<Long> get_divisor(long x) {
ArrayList<Long>a=new ArrayList<Long>();
for(long i=2;i*i<=x;i++) {
if(x%i==0) {
a.add((long) i);
if(x/i!=i)a.add(x/i);
}
}
return a;
}
private static int CntOfFactor(long x) {
ArrayList<Long>a=new ArrayList<Long>();
for(long i=1;i*i<=x;i++) {
if(x%i==0) {
a.add((long) i);
if(x/i!=i)a.add(x/i);
}
}
return a.size();
}
static long[] sieve;
static long[] smallestPrime;
public static void sieve()
{
int n=4000000+1;
sieve=new long[n];
smallestPrime=new long[n];
sieve[0]=1;
sieve[1]=1;
for(int i=2;i<n;i++){
sieve[i]=i;
smallestPrime[i]=i;
}
for(int i=2;i*i<n;i++){
if(sieve[i]==i){
for(int j=i*i;j<n;j+=i){
if(sieve[j]==j)sieve[j]=1;
if(smallestPrime[j]==j||smallestPrime[j]>i)smallestPrime[j]=i;
}
}
}
}
static long nCr(long n,long r,long MOD) {
computeFact(n, MOD);
if(n<r)return 0;
if(r==0)return 1;
return fact[(int) n]*mod_inv(fact[(int) r],MOD)%MOD*mod_inv(fact[(int) (n-r)],MOD)%MOD;
}
static long[]fact;
static void computeFact(long n,long MOD) {
fact=new long[(int)n+1];
fact[0]=1;
for(int i=1;i<=n;i++)fact[i]=(fact[i-1]*i%MOD)%MOD;
}
static long bin_expo(long a,long b,long MOD) {
if(b == 0)return 1;
long ans = bin_expo(a,b/2,MOD);
ans = (ans*ans)%MOD;
if(b % 2!=0){
ans = (ans*a)%MOD;
}
return ans%MOD;
}
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 mod_add(long a, long b, long m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
static long mod_mul(long a, long b, long m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
static long mod_sub(long a, long b, long m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
static long mod_inv(long n,long p) {return bin_expo(n,p-2,p);}
static long gcd(long a, long b){if (a == 0) {return b;}return gcd(b % a, a); }
static int gcd(int a, int b){if (a == 0) {return b; }return gcd(b % a, a); }
static long lcm(long a,long b){return (a / gcd(a, b)) * b;}
static long min(long x,long y) {return Math.min(x, y);}static long max(long x,long y) {return Math.max(x, y);}
static int min(int x,int y) {return Math.min(x, y);}static int max(int x,int y) {return Math.max(x, y);}
static ArrayList<String>powof2s;
static void powof2S() {
long i=1;
while(i<(long)2e18) {
powof2s.add(String.valueOf(i));
i*=2;
}
}
static long power(long a, long b){
a %=MOD;long out = 1;
while (b > 0) {
if((b&1)!=0)out = out * a % MOD;
a = a * a % MOD;
b >>= 1;
a*=a;
}
return out;
}
static boolean coprime(int a, long l){return (gcd(a, l) == 1);}
// ****************************MATHS-ENDS*****************************************************
// ***********************BINARY-SEARCH STARTS***********************************************
public static int upperBound(long[] arr, long m, int l, int r) {
while(l<=r) {
int mid=(l+r)/2;
if(arr[mid]<=m) l=mid+1;
else r=mid-1;
}
return l;
}
public static int lowerBound(long[] a, long m, int l, int r) {
while(l<=r) {
int mid=(l+r)/2;
if(a[mid]<m) l=mid+1;
else r=mid-1;
}
return l;
}
public static int lowerBound(ArrayList<Integer> ar,int k){
int s=0,e=ar.size();
while (s!=e){
int mid = s+e>>1;
if (ar.get(mid) <k)s=mid+1;
else e=mid;
}
if(s==ar.size())return -1;
return s;
}
public static int upperBound(ArrayList<Integer> ar,int k){
int s=0,e=ar.size();
while (s!=e){
int mid = s+e>>1;
if (ar.get(mid) <=k)s=mid+1;
else e=mid;
}
if(s==ar.size())return -1;
return s;
}
public static long getClosest(long val1, long val2,long target){if (target - val1 >= val2 - target)return val2; else return val1;}
static void ruffleSort(long[] a) {
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
long oi=r.nextInt(n), temp=a[i];
a[i]=a[(int)oi];
a[(int)oi]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(int[] a){
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n), temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
Arrays.sort(a);
}
int ceilIndex(int input[], int T[], int end, int s){
int start = 0;
int middle;
int len = end;
while(start <= end){
middle = (start + end)/2;
if(middle < len && input[T[middle]] < s && s <= input[T[middle+1]]){
return middle+1;
}else if(input[T[middle]] < s){
start = middle+1;
}else{
end = middle-1;
}
}
return -1;
}
static int lowerLimitBinarySearch(ArrayList<Long> v,long k) {
int n =v.size();
int first = 0,second = n;
while(first <second) {
int mid = first + (second-first)/2;
if(v.get(mid) > k) {
second = mid;
}else {
first = mid+1;
}
}
if(first < n && v.get(first) < k) {
first++;
}
return first; //1 index
}
public static int searchindex(long arr[], long t){int index = Arrays.binarySearch(arr, t);return (index < 0) ? -1 : index;}
public static long[] sort(long[] a) {ArrayList<Long> al = new ArrayList<>();for(int i=0;i<a.length;i++) al.add(a[i]);Collections.sort(al);for(int i=0;i<a.length;i++) a[i]=al.get(i);return a;}
public static int[] sort(int[] a) {ArrayList<Integer> al = new ArrayList<>();for(int i=0;i<a.length;i++) al.add(a[i]);Collections.sort(al);for(int i=0;i<a.length;i++) a[i]=al.get(i);return a;}
// *******************************BINARY-SEARCH ENDS***********************************************
// *********************************GRAPHS-STARTS****************************************************
// *******----SEGMENT TREE IMPLEMENT---*****
// -------------START---------------
void buildTree (int[] arr,int[] tree,int start,int end,int treeNode){
if(start==end){
tree[treeNode]=arr[start];
return;
}
buildTree(arr,tree,start,end,2*treeNode);
buildTree(arr,tree,start,end,2*treeNode+1);
tree[treeNode]=tree[treeNode*2]+tree[2*treeNode+1];
}
void updateTree(int[] arr,int[] tree,int start,int end,int treeNode,int idx,int value){
if(start==end){
arr[idx]=value;
tree[treeNode]=value;
return;
}
int mid=(start+end)/2;
if(idx>mid)updateTree(arr,tree,mid+1,end,2*treeNode+1,idx,value);
else updateTree(arr,tree,start,mid,2*treeNode,idx,value);
tree[treeNode]=tree[2*treeNode]+tree[2*treeNode+1];
}
long query(int[]arr,int[]tree,int start,int end,int treeNode,int qleft,int qright) {
if(start>=qleft&&end<=qright)return tree[treeNode];
if(start>qright||end<qleft)return 0;
int mid=(start+end)/2;
long valLeft=query(arr,tree,start,mid-1,treeNode*2,qleft,qright);
long valRight=query(arr,tree,mid+1,end,treeNode*2+1,qleft,qright);
return valLeft+valRight;
}
// -------------ENDS---------------
//***********************DSU IMPLEMENT START*************************
static int parent[];
static int rank[];
static int[]Size;
static void makeSet(int n){
parent=new int[n];
rank=new int[n];
Size=new int[n];
for(int i=0;i<n;i++){
parent[i]=i;
rank[i]=0;
Size[i]=1;
}
}
static void union(int u,int v){
u=findpar(u);
v=findpar(v);
if(u==v)return;
if(rank[u]<rank[v]) {
parent[u]=v;
Size[v]+=Size[u];
}
else if(rank[v]<rank[u]) {
parent[v]=u;
Size[u]+=Size[v];
}
else{
parent[v]=u;
rank[u]++;
Size[u]+=Size[v];
}
}
private static int findpar(int node){
if(node==parent[node])return node;
return parent[node]=findpar(parent[node]);
}
// *********************DSU IMPLEMENT ENDS*************************
// ****__________PRIMS ALGO______________________****
private static int prim(ArrayList<node>[] adj,int N,int node) {
int key[] = new int[N+1];
int parent[] = new int[N+1];
boolean mstSet[] = new boolean[N+1];
for(int i = 0;i<N;i++) {
key[i] = 100000000;
mstSet[i] = false;
}
PriorityQueue<node> pq = new PriorityQueue<node>(N, new node());
key[node] = 0;
parent[node] = -1;
pq.add(new node( node,key[node]));
for(int i = 0;i<N-1;i++) {
int u = pq.poll().getV();
mstSet[u] = true;
for(node it: adj[u]) {
if(mstSet[it.getV()] == false && it.getW() < key[it.getV()]) {
parent[it.getV()] = u;
key[it.getV()] = (int) it.getW();
pq.add(new node(it.getV(), key[it.getV()]));
}
}
}
int sum=0;
for(int i=1;i<N;i++) {
System.out.println(key[i]);
sum+=key[i];
}
System.out.println(sum);
return sum;
}
// ****____________DIJKSTRAS ALGO___________****
static int[]dist;
static int dijkstra(int u,int n,ArrayList<node>adj[]) {
dist=new int[n];
Arrays.fill(dist,Integer.MAX_VALUE);
dist[u]=0;
PriorityQueue<node>pq=new PriorityQueue<node>(new node());
pq.add(new node(u,0));
while(!pq.isEmpty()) {
node v=pq.poll();
for(node it:adj[v.getV()]) {
if(dist[it.getV()]>it.getW()+dist[v.getV()]) {
dist[it.getV()]=(int) (it.getW()+dist[v.getV()]);
pq.add(new node(it.getV(),dist[it.getV()]));
}
}
}
int sum=0;
for(int i=1;i<n;i++){
System.out.println(dist[i]);
sum+=dist[i];
}
return sum;
}
private static void setGraph(int n,int m){
vis=new boolean[n+1];
indeg=new int[n+1];
// adj=new ArrayList<ArrayList<Integer>>();
// for(int i=0;i<=n;i++)adj.add(new ArrayList<>());
// for(int i=0;i<m;i++){
// int u=s.nextInt(),v=s.nextInt();
// adj.get(u).add(v);
// adj.get(v).add(u);
// }
adj=new ArrayList[n+1];
// backadj=new ArrayList[n+1];
for(int i=0;i<=n;i++){
adj[i]=new ArrayList<Integer>();
// backadj[i]=new ArrayList<Integer>();
}
for(int i=0;i<m;i++){
int u=s.nextInt(),v=s.nextInt();
adj[u].add(v);
adj[v].add(u);
// backadj[v].add(u);
indeg[v]++;
indeg[u]++;
}
// weighted adj
// adj=new ArrayList[n+1];
// for(int i=0;i<=n;i++){
// adj[i]=new ArrayList<node>();
// }
// for(int i=0;i<m;i++){
// int u=s.nextInt(),v=s.nextInt();
// long w=s.nextInt();
// adj[u].add(new node(v,w));
//// adj[v].add(new node(u,w));
// }
}
// *********************************GRAPHS-ENDS****************************************************
static int[][] dirs8 = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
static int[][] dirs4 = {{1,0},{-1,0},{0,1},{0,-1}}; //d-u-r-l
static long MOD=(long) (1e9+7);
static int prebitsum[][];
static boolean[] vis;
static int[]indeg;
// static ArrayList<ArrayList<Integer>>adj;
static ArrayList<Integer> adj[];
static ArrayList<Integer> backadj[];
static FastReader s = new FastReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException
{
// sieve();
// computeFact((int)1e7+1,MOD);
// prebitsum=new int[2147483648][31];
// presumbit(prebitsum);
// powof2S();
//
// try {
int tt = s.nextInt();
// int tt=1;
for(int i=1;i<=tt;i++) {
solver();
}
out.close();
// catch(Exception e) {return;}
}
private static void solver() {
char[]ch=s.next().toCharArray();
int n=ch.length;
int cost=Math.abs((ch[0]-'a')-(ch[n-1]-'a'));
ArrayList<int[]>idxs=new ArrayList<>();
char st=ch[0],end=ch[n-1];
char stt=ch[0],endt=ch[n-1];
if(stt>endt) {
char temp=stt;
stt=endt;
endt=temp;
}
for(int i=0;i<n;i++) {
if(ch[i]>=stt&&ch[i]<=endt)idxs.add(new int[] {ch[i]-'a',i+1});
}
Collections.sort(idxs,new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0]-o2[0]>0)return 1;
else if (o1[0]-o2[0]==0)return o2[1]-o1[1];
else return -1;
}
});
out.println(cost+" "+(idxs.size()));
if(st>end) {
Collections.sort(idxs,new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0]-o2[0]>0)return 1;
else if (o1[0]-o2[0]==0)return o2[1]-o1[1];
else return -1;
}
});
for(int i=idxs.size()-1;i>=0;i--) {
// out.println(idxs.get(i)[0]+" "+idxs.get(i)[1]);
out.print((idxs.get(i)[1])+" ");
}
}
else {
Collections.sort(idxs,new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0]-o2[0]>0)return 1;
else if (o1[0]-o2[0]==0)return o1[1]-o2[1];
else return -1;
}
});
for(int i=0;i<idxs.size();i++) {
// out.println(idxs.get(i)[0]+" "+idxs.get(i)[1]);
out.print((idxs.get(i)[1])+" ");
}
}
out.println();
}
/* *********************BITS && TOOLS &&DEBUG STARTS***********************************************/
static boolean issafe(int i, int j, int r,int c,boolean[][]vis){
if (i < 0 || j < 0 || i >= r || j >= c||vis[i][j]==true)return false;
else return true;
}
static void presumbit(int[][]prebitsum) {
for(int i=1;i<=200000;i++) {
int z=i;
int j=0;
while(z>0) {
if((z&1)==1) {
prebitsum[i][j]+=(prebitsum[i-1][j]+1);
}else {
prebitsum[i][j]=prebitsum[i-1][j];
}
z=z>>1;
j++;
}
}
}
static void countOfSetBit(long[]a) {
for(int j=30;j>=0;j--) {
int cnt=0;
for(long i:a) {
if((i&1<<j)==1)cnt++;
}
// printing the current no set bit in all array element
System.out.println(cnt);
}
}
public static String revStr(String str){String input = str;StringBuilder input1 = new StringBuilder();input1.append(input);input1.reverse();return input1.toString();}
static void printA(long[] x) {for(int i=0;i<x.length;i++)System.out.print(x[i]+" ");System.out.println();}
static void printA(int[] x) {for(int i=0;i<x.length;i++)System.out.print(x[i]+" ");System.out.println();}
static void pc2d(boolean[][] vis) {
int n=vis.length;
int m=vis[0].length;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
System.out.print(vis[i][j]+" ");
}
System.out.println();
}
}
static void pi2d(char[][] a) {
int n=a.length;
int m=a[0].length;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
static void p1d(int[]a) {
for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");
System.out.println();
}
// *****************BITS && TOOLS &&DEBUG ENDS***********************************************
}
// **************************I/O*************************
class FastReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public FastReader(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 long nextLong() {return Long.parseLong(next());}
public double nextDouble() {return Double.parseDouble(next());}
public String nextLine() {String str = "";try {str = reader.readLine();}catch (IOException e) {e.printStackTrace();}return str;}
public int[] rdia(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = nextInt();return a;}
public long[] rdla(int n) {long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nextLong();return a;}
}
class dsu{
int n;
static int parent[];
static int rank[];
static int[]Size;
public dsu(int n) {this.n=n;this.parent=new int[n];this.rank=new int[n];this.Size=new int[n];
for(int i=0;i<n;i++){parent[i]=i;rank[i]=0;Size[i]=1;}
}
static int findpar(int node) {if(node==parent[node])return node;return parent[node]=findpar(parent[node]);}
static void union(int u,int v){
u=findpar(u);v=findpar(v);
if(u!=v) {
if(rank[u]<rank[v]) {parent[u]=v;Size[v]+=Size[u];}
else if(rank[v]<rank[u]) {parent[v]=u;Size[u]+=Size[v];}
else{parent[v]=u;rank[u]++;Size[u]+=Size[v];}
}
}
}
class pair{
int x;int y;
long u,v;
public pair(int x,int y){this.x=x;this.y=y;}
public pair(long u,long v) {this.u=u;this.v=v;}
}
class node implements Comparator<node>{
private int v;
private long w;
node(int _v, long _w) { v = _v; w = _w; }
node() {}
int getV() { return v; }
long getW() { return w; }
@Override
public int compare(node node1, node node2) {
if (node1.w < node2.w) return -1;
if (node1.w > node2.w) return 1;
return 0;
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 3c010528e491f41cbc957cbffcca1ed8 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/* ********* 1e9 ********* */
public class Solution {
int mod = (int) 1e9;
static int mod7 = ((int) 1e9) + 7;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = 1;
t = sc.nextInt();
outer: while (t-- != 0) {
String k = sc.next();
int cost = Math.abs((int) k.charAt(0) - (int) k.charAt(k.length() - 1));
ArrayList<Integer>[] graph = new ArrayList[27];
for (int i = 1; i <= 26; i++) {
graph[i] = new ArrayList<>();
}
for (int i = 0; i < k.length(); i++) {
graph[(int) k.charAt(i) - 96].add(i+1);
}
if (k.charAt(0) < k.charAt(k.length() - 1)) {
List<Integer> q = new ArrayList<>();
for (int i = (int) k.charAt(0) - 96; i <= (int) k.charAt(k.length() - 1) - 96; i++) {
for (int j : graph[i]) {
q.add(j);
}
}
System.out.println(cost + " " + q.size());
for (int i : q)
System.out.print(i + " ");
System.out.println();
} else {
List<Integer> q = new ArrayList<>();
for (int i = (int) k.charAt(0) - 96; i >= (int) k.charAt(k.length() - 1) - 96; i--) {
for (int j : graph[i]) {
q.add(j);
}
}
System.out.println(cost + " " + q.size());
for (int i : q)
System.out.print(i + " ");
System.out.println();
}
}
sc.close();
}
static boolean b = false;
/* ********* 1e9 ********* */
public int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
public int gcd(int a, int b) {
while (b != 0) {
a %= b;
int temp = a;
a = b;
b = temp;
}
return a;
}
public static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int divCount(int n) {
boolean hash[] = new boolean[n + 1];
Arrays.fill(hash, true);
for (int p = 2; p * p < n; p++)
if (hash[p] == true)
for (int i = p * 2; i < n; i += p)
hash[i] = false;
int total = 1;
for (int p = 2; p <= n; p++) {
if (hash[p]) {
int count = 0;
if (n % p == 0) {
while (n % p == 0) {
n = n / p;
count++;
}
total = total * (count + 1);
}
}
}
return total;
}
static int nCr(int n, int r) {
return fact(n) / (fact(r) * fact(n - r));
}
static int fact(int n) {
if (n == 0)
return 1;
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
static int num_digits(int num) {
return (int) (Math.log10(num) + 1);
}
// End
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 022c3d8ff203f90a2cb7e873e2614a97 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | //package codeforce.div3.r820;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import static java.lang.System.out;
import static java.util.stream.Collectors.joining;
/**
* @author pribic (Priyank Doshi)
* @see <a href="https://codeforces.com/contest/1729/problem/C" target="_top">https://codeforces.com/contest/1729/problem/C</a>
* @since 12/09/22 10:12 pm
*/
public class C {
static FastScanner sc = new FastScanner(System.in);
public static void main(String[] args) {
try (PrintWriter out = new PrintWriter(System.out)) {
int T = sc.nextInt();
for (int tt = 1; tt <= T; tt++) {
String str = sc.next();
ArrayList<Integer>[] fr = new ArrayList[26];
for (int i = 0; i < fr.length; i++) {
fr[i] = new ArrayList<>();
}
for (int i = 0; i < str.length(); i++) {
fr[str.charAt(i) - 'a'].add(i + 1);
}
int cost = Math.abs(str.charAt(0) - str.charAt(str.length() - 1));
int stepCnt = 0;
List<Integer> steps = new ArrayList<>();
int i = str.charAt(0) - 'a';
int dir = str.charAt(0) <= str.charAt(str.length() - 1) ? 1 : -1;
int n = str.charAt(str.length() - 1) - 'a';
while (i != n + dir) {
stepCnt += fr[i].size();
steps.addAll(fr[i]);
i += dir;
}
System.out.println(cost + " " + stepCnt);
StringBuilder sb = new StringBuilder();
for (int idx : steps)
sb.append(idx).append(" ");
System.out.println(sb);
}
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f), 32768);
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 92be6988185972dea2870abe1812d12d | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | //package codeforce.div3.r820;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import static java.lang.System.out;
import static java.util.stream.Collectors.joining;
/**
* @author pribic (Priyank Doshi)
* @see <a href="https://codeforces.com/contest/1729/problem/C" target="_top">https://codeforces.com/contest/1729/problem/C</a>
* @since 12/09/22 10:12 pm
*/
public class C {
static FastScanner sc = new FastScanner(System.in);
public static void main(String[] args) {
try (PrintWriter out = new PrintWriter(System.out)) {
int T = sc.nextInt();
for (int tt = 1; tt <= T; tt++) {
String str = sc.next();
ArrayList<Integer>[] fr = new ArrayList[26];
for (int i = 0; i < fr.length; i++) {
fr[i] = new ArrayList<>();
}
for (int i = 1; i < str.length() - 1; i++) {
fr[str.charAt(i) - 'a'].add(i + 1);
}
int cost = Math.abs(str.charAt(0) - str.charAt(str.length() - 1));
int stepCnt = 1;
List<Integer> steps = new ArrayList<>();
steps.add(1);
if (str.charAt(0) <= str.charAt(str.length() - 1)) {
for (int i = str.charAt(0) - 'a'; i <= str.charAt(str.length() - 1) - 'a'; i++) {
stepCnt += fr[i].size();
steps.addAll(fr[i]);
}
} else {
for (int i = str.charAt(0) - 'a'; i >= str.charAt(str.length() - 1) - 'a'; i--) {
stepCnt += fr[i].size();
steps.addAll(fr[i]);
}
}
if (str.length() > 1) {
steps.add(str.length());
stepCnt++;
}
System.out.println(cost + " " + stepCnt);
StringBuilder sb = new StringBuilder();
for (int idx : steps)
sb.append(idx).append(" ");
System.out.println(sb);
}
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f), 32768);
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 1aa824a78a68f982c65fb84f38d2b66d | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | //package codeforce.div3.r820;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import static java.lang.System.out;
import static java.util.stream.Collectors.joining;
/**
* @author pribic (Priyank Doshi)
* @see <a href="https://codeforces.com/contest/1729/problem/C" target="_top">https://codeforces.com/contest/1729/problem/C</a>
* @since 12/09/22 10:12 pm
*/
public class C {
static FastScanner sc = new FastScanner(System.in);
public static void main(String[] args) {
try (PrintWriter out = new PrintWriter(System.out)) {
int T = sc.nextInt();
for (int tt = 1; tt <= T; tt++) {
String str = sc.next();
ArrayList<Integer>[] fr = new ArrayList[26];
for (int i = 0; i < fr.length; i++) {
fr[i] = new ArrayList<>();
}
for (int i = 1; i < str.length() - 1; i++) {
fr[str.charAt(i) - 'a'].add(i + 1);
}
int cost = Math.abs(str.charAt(0) - str.charAt(str.length() - 1));
int stepCnt = 1;
List<Integer> steps = new ArrayList<>();
steps.add(1);
if (str.charAt(0) <= str.charAt(str.length() - 1)) {
for (int i = str.charAt(0) - 'a'; i <= str.charAt(str.length() - 1) - 'a'; i++) {
stepCnt += fr[i].size();
steps.addAll(fr[i]);
}
} else {
for (int i = str.charAt(0) - 'a'; i >= str.charAt(str.length() - 1) - 'a'; i--) {
stepCnt += fr[i].size();
steps.addAll(fr[i]);
}
}
if (str.length() > 1) {
steps.add(str.length());
stepCnt++;
}
System.out.println(cost + " " + stepCnt);
StringBuilder sb = new StringBuilder();
for (int idx : steps)
sb.append(idx + " ");
System.out.println(sb);
}
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f), 32768);
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 9ac977e915fa82d83ea989c0b3c9d2cc | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | //package codeforce.div3.r820;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import static java.lang.System.out;
import static java.util.stream.Collectors.joining;
/**
* @author pribic (Priyank Doshi)
* @see <a href="https://codeforces.com/contest/1729/problem/C" target="_top">https://codeforces.com/contest/1729/problem/C</a>
* @since 12/09/22 10:12 pm
*/
public class C {
static FastScanner sc = new FastScanner(System.in);
public static void main(String[] args) {
try (PrintWriter out = new PrintWriter(System.out)) {
int T = sc.nextInt();
for (int tt = 1; tt <= T; tt++) {
String str = sc.next();
ArrayList<Integer>[] fr = new ArrayList[26];
for (int i = 0; i < fr.length; i++) {
fr[i] = new ArrayList<>();
}
for (int i = 1; i < str.length() - 1; i++) {
fr[str.charAt(i) - 'a'].add(i + 1);
}
int cost = Math.abs(str.charAt(0) - str.charAt(str.length() - 1));
int stepCnt = 1;
List<Integer> steps = new ArrayList<>();
steps.add(1);
if (str.charAt(0) <= str.charAt(str.length() - 1)) {
for (int i = str.charAt(0) - 'a'; i <= str.charAt(str.length() - 1) - 'a'; i++) {
stepCnt += fr[i].size();
steps.addAll(fr[i]);
}
} else {
for (int i = str.charAt(0) - 'a'; i >= str.charAt(str.length() - 1) - 'a'; i--) {
stepCnt += fr[i].size();
steps.addAll(fr[i]);
}
}
if (str.length() > 1) {
steps.add(str.length());
stepCnt++;
}
System.out.println(cost + " " + stepCnt);
for (int idx : steps)
System.out.print(idx + " ");
System.out.println();
}
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f), 32768);
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 333bdc14164fc0d54f69677831a14b59 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF {
static class Pair
{
char c;
int index;
public Pair(char a,int b)
{
c=a;
index=b;
}
}
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)
{
String s=br.readLine();
List<Pair> list=new ArrayList<>();
int cost=Math.abs(s.charAt(s.length()-1)-s.charAt(0));
if(s.charAt(0)>s.charAt(s.length()-1))
{
for(int i=1;i<s.length()-1;i++)
{
if(s.charAt(i)<=s.charAt(0) && s.charAt(i)>=s.charAt(s.length()-1))
{
list.add(new Pair(s.charAt(i), i+1));
}
}
Collections.sort(list,(a,b)->{
return b.c-a.c;
});
System.out.println(cost+" "+(list.size()+2));
System.out.print(1+" ");
for(int i=0;i<list.size();i++)
{
System.out.print(list.get(i).index+" ");
}
System.out.println(s.length());
}
else
{
for(int i=1;i<s.length()-1;i++)
{
if(s.charAt(i)<=s.charAt(s.length()-1) && s.charAt(i)>=s.charAt(0))
{
list.add(new Pair(s.charAt(i), i+1));
}
}
Collections.sort(list,(a,b)->{
return a.c-b.c;
});
System.out.println(cost+" "+(list.size()+2));
System.out.print(1+" ");
for(int i=0;i<list.size();i++)
{
System.out.print(list.get(i).index+" ");
}
System.out.println(s.length());
}
}
}
}
// Aman and garden
// Medium
// There is a garden and Aman is the gardener and he wants to arrange trees in a row
// of length n such that every Kth plant is non-decreasing in height. For example the plant
// at position 1 should be smaller than or equal to the tree planted at position 1+K and plant
// at position 1+K should be smaller than or equal to 1+2*K and so on, this
// should be true for every position(for eg. 2 <= 2+K <= 2+2*K ...).
// Now Aman can change a plant at any position with a plant of any height in order to create
// the required arrangment. He wants to know minimum number of trees he will have to change to get the required arrangement.
// We"ll be given a plants array which represents the height of plants at every position
// and a number K and we need to output the minimum number of plants we will have to change to get the required arrangement.
// Example:
// plants = [5,3,4,1,6,5];
// K=2;
// here plants at index (0,2,4) and (1,3,5) should be non decreasing.
// plants[0]=5;
// plants[2]=4;
// plants[4]=6;
// We can change the plant at index 2 with a plant of height 5(one change).
// Similarly
// plants[1]=3;
// plants[3]=1;
// plants[5]=5;
// We can change the plant at index 3 with a plant of height 4(one change).
// So minimum 2 changes are required.
// Constraints
// 1<=plants.length<=100000
// 1<=plants[i],K<=plants.length
// Format
// Input
// First line contains an integer N representing length of the plant array.
// Next line contains N space separated integers representing height of trees.
// Last line contains an integer K
// Output
// Output the minimum number of changes required.
// Example
// Sample Input
// 6
// 5
// 3
// 4
// 1
// 6
// 5
// 2
// Sample Output
// 2
// Tokyo Drift
// Easy
// There is a racing competition which will be held in your city next weekend. There are N racers who are going to take part in the competition. Each racer is at a given distance from the finish line, which is given in an integer array.
// Due to some safety reasons, total sum of speeds with which racers can drive is restricted with a given limit. Since, cost of organizing such an event depends on how long the event will last, you need to find out the minimum time in which all racers will cross the finishing line, but sum of speeds of all racers should be less than or equal to the given limit.
// If it is impossible to complete the race for all racers within given limit, we have to return -1.
// Note: Speed is defined as Ceil (distance/time), where ceil represents smaller than or equal to. For example if distance is 20 km and time taken to travel is 3 hrs then speed equals ceil(20/3) i.e. 7 km/hr.
// Example: Let us take 4 Racers with Distances from finishing line as [15 km, 25 km, 5 km, 20 km], and the maximum sum of speeds allowed is 12 km/hr. The minimum time in which all racers will reach the finishing line will be 7 hours.
// Racer 1 will have 15 km / 7 hr = 3 km/hr speed
// Racer 2 will have 25 km / 7 hr = 4 km/hr speed
// Racer 3 will have 05 km / 7 hr = 1 km/hr speed
// Racer 4 will have 20 km / 7 hr = 3 km/hr speed
// Hence, the total sum of speeds will be (3 + 4 + 1 + 3) = 11 km/hr which is less than or equal to 12 km/hr.
// Constraints
// 1 <= N <= 100000
// 1 <= racers[i] <= 10000000
// 1 <= Limit <= 10000000
// Format
// Input
// First line contains an integer N representing length of array.
// Next line contains N space seprated integers representing distance from finishing line.
// Last line contains an integer representing limit of sum of speeds with which racers can drive
// Output
// A single line integer representing minimum time in which all racers will cross the finishing line, If not possible print -1
// Example
// Sample Input
// 4
// 15 25 5 20
// 12
// Sample Output
// 7
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 1d22db54dff8f087efe1d8982fe726ff | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.BufferedReader;
import java.io.PrintWriter;
import java.lang.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketOption;
import java.security.spec.RSAOtherPrimeInfo;
import java.util.*;
public class SEC {
static long mod = (long) (1e9 + 7);
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void sort(long[] arr) {
ArrayList<Long> a = new ArrayList<>();
for (long i : arr) {
a.add(i);
}
Collections.sort(a);
for (int i = 0; i < a.size(); i++) {
arr[i] = a.get(i);
}
}
static long highestPowerof2(long x) {
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
public static long fact(long number) {
if (number == 0 || number == 1) {
return 1;
} else {
return number * fact(number - 1);
}
}
public static ArrayList<Long> primeFactors(long n) {
ArrayList<Long> arr = new ArrayList<>();
long count = 0;
while (n % 2 == 0) {
arr.add(2l);
n /= 2;
}
for (long i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
arr.add(i);
n /= i;
}
}
if (n > 2)
arr.add(n);
return arr;
}
static public long[] prime(long number) {
long n = number;
long count = 0;
long even = 0;
for (long i = 2; i <= n / i; i++) {
while (n % i == 0) {
if (i % 2 == 1) {
count++;
} else {
even++;
}
n /= i;
}
}
if (n > 1) {
if (n % 2 == 1) {
count++;
} else {
even++;
}
}
return new long[]{even, count};
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static int search(ArrayList<Integer> arr, int tar) {
int low = 0, hi = arr.size() - 1;
int ans = -1;
while (low <= hi) {
int mid = (low + hi) / 2;
if (arr.get(mid) > tar) {
ans = arr.get(mid);
hi = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
static long gcd(long a, long b) {
// if b=0, a is the GCD
if (b == 0)
return a;
else
return gcd(b, a % b);
}
static long power(long x, long y, long p) {
long res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static int get(long[] arr, long tar) {
int ans = -1;
int l = 0;
int h = arr.length - 1;
while (l <= h) {
int mid = l + (h - l) / 2;
if (arr[mid] <= tar) {
ans = mid;
l = mid + 1;
} else {
h = mid - 1;
}
}
return ans;
}
static void print_Array(int[] arr) {
for (int i : arr) {
System.out.print(i + " ");
}
System.out.println();
}
static boolean con(char ch) {
if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u') return true;
return false;
}
static void swap(char[] ch, int i, int j) {
char c = ch[i];
ch[i] = ch[j];
ch[j] = c;
}
static void swap(long[] ch, int i, int j) {
long c = ch[i];
ch[i] = ch[j];
ch[j] = c;
}
/*
5 12
1 1 1 1 1
2 2 2 1 1
2 2 2 2 2
4 2 2 2 2
3 3 2 2 2
4 3 3 3 3 2 2
8 7 7 7 7 7 7 7
(()) (((()))) (()())
*/
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0) {
char[] ch = sc.next().toCharArray();
int n = ch.length;
StringBuilder ans = new StringBuilder();
HashMap<Character,ArrayDeque<Integer>> fre = new HashMap<>();
for(int i = 0; i < n; i++){
ArrayDeque<Integer> q = fre.getOrDefault(ch[i], new ArrayDeque<>());
q.add(i+1);
fre.put(ch[i],q);
}
int first = ch[0]-'a';
int sec = ch[n-1] - 'a';
long cost = 0;
char pre = ch[0];
long size = 0;
boolean rev = false;
ArrayList<Integer> l =new ArrayList<>();
// System.out.println(first+" "+sec);
if(first <= sec){
for(char a = ch[0]; a <= ch[n-1]; a++){
if(fre.containsKey(a)){
ArrayDeque<Integer> q = fre.get(a);
while(q.size() > 0){
cost += Math.abs((pre-'a'+1) - (a-'a'+1));
pre = a;
size++;
l.add(q.pollFirst());
}
}
}
}else{
pre = ch[n-1];
rev = true;
for(char a = ch[n-1]; a <= ch[0]; a++){
if(fre.containsKey(a)){
// System.out.println(a);
ArrayDeque<Integer> q = fre.get(a);
while(q.size() > 0){
cost += Math.abs((pre-'a'+1) -(a-'a'+1));
pre = a;
size++;
l.add(q.pollLast());
}
}
}
}
out.println(cost+" "+size);
if(rev){
for(int i = l.size()-1; i>=0; i--){
out.print(l.get(i)+" ");
}
}else{
for(int i: l){
out.print(i+" ");
}
}
out.println();
}
out.flush();
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | b0e79f61ffb7c2d614701a353db9107c | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import javax.swing.*;
import java.util.*;
import java.io.*;
public class Balabizoo {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int tt = sc.nextInt();
while(tt-->0){
char[] c = sc.next().toCharArray();
ArrayList<Integer>[] arr = new ArrayList[26];
int j = 0;
for(int i=0; i<26 ;i++)
arr[i] = new ArrayList<>();
for(int i=0; i<c.length ;i++) {
arr[c[i] - 'a'].add(i);
if(i == c.length - 1)
j = c[i]- 'a';
}
int x = (c[0]-'a' >= j)? -1 : 1 , ans = 0 , prev = c[0] - 'a';
ArrayList<Integer> res = new ArrayList<>();
for(int i=c[0]-'a'; i<26 && i>=0 ;i+=x){
boolean f = false;
while(!arr[i].isEmpty()) {
int y = arr[i].remove(0) , z = c[y] - 'a';
if(y == c.length-1)
f = true;
ans += Math.abs(prev - z);
res.add(y+1);
prev = z;
}
if(f)
break;
}
pw.println(ans+" "+res.size());
for(int i : res)
pw.print(i+" ");
pw.println();
}
pw.flush();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws IOException {
br = new BufferedReader(new FileReader(file));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String readAllLines(BufferedReader reader) throws IOException {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
return content.toString();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 790594fdd963300afcdbab52f032f716 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.nio.MappedByteBuffer;
import java.util.*;
import java.util.concurrent.RecursiveTask;
public class Solution {
InputStream is;
FastWriter out;
String INPUT = "";
public static void main(String[] args) throws Exception
{
new Solution().run();// Here run assign I/O calls solve also tell total time invested
}
void solve()
{
int t=ni();
while(t-->0)
{
char a[]=ns().toCharArray();
int b[][]=new int[a.length-2][2];
for(int i=1;i<a.length-1;i++) {
b[i-1][0]=a[i];
b[i-1][1]=i+1;
}
Arrays.sort(b,(x,y)->x[0]-y[0]);
int sum=Math.abs(a[0]-a[a.length-1]), vis=2;
if(a[0]<=a[a.length-1]) {
for (int i = 0; i < b.length; i++) {
if (a[0] <= b[i][0] && b[i][0] <= a[a.length - 1])
vis++;
}
}
else {
for(int i=b.length-1;i>=0;i--) {
if(a[0]>=b[i][0]&&b[i][0]>=a[a.length-1])
vis++;
}
}
System.out.println(sum+" "+vis);
System.out.print(1+" ");
if(a[0]<=a[a.length-1]) {
for (int i = 0; i < b.length; i++) {
if (a[0] <= b[i][0] && b[i][0] <= a[a.length - 1])
System.out.print(b[i][1] + " ");
}
}
else {
for(int i=b.length-1;i>=0;i--) {
if(a[0]>=b[i][0]&&b[i][0]>=a[a.length-1])
System.out.print(b[i][1]+" ");
}
}
System.out.println(a.length);
}
}
void go()
{
}
static boolean prime[];
static int count=0;
void sieve(int n)
{
prime=new boolean[n+1];
Arrays.fill(prime, true);
for(int i=2;i<=Math.sqrt(n);i++)
if(prime[i])
for(int j=i*i;j<=n;j+=i)
prime[j]=false;
}
boolean isPrime(int n)
{
for(int i=2;i<=Math.sqrt(n);i++)
if(n%i==0)
return false;
return true;
}
long gcd(long a,long b)
{
if(a==0)
return b;
return gcd(b%a,a);
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new FastWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private int[] ni(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private long[] nl(int n)
{
long[] a = new long[n];
for(int i = 0;i < n;i++)a[i] = nl();
return a;
}
private Long[] nL(int n) {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private Integer[] nI(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private char[][] ns(int n, int m) {
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[][] ni(int n, int m) {
int[][] map = new int[n][];
for(int i = 0;i < n;i++)map[i] = ni(m);
return map;
}
private long[][] nl(int n, int m) {
long[][] map = new long[n][];
for(int i = 0;i < n;i++)map[i] = nl(m);
return map;
}
private Integer[][] nI(int n, int m) {
Integer[][] map = new Integer[n][];
for(int i = 0;i < n;i++)map[i] = nI(m);
return map;
}
private Long[][] nL(int n, int m) {
Long[][] map = new Long[n][];
for(int i = 0;i < n;i++)map[i] = nL(m);
return map;
}
private int ni() { return (int)nl(); }
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
public static class FastWriter
{
private static final int BUF_SIZE = 1<<13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter(){out = null;}
public FastWriter(OutputStream os)
{
this.out = os;
}
public FastWriter(String path)
{
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b)
{
buf[ptr++] = b;
if(ptr == BUF_SIZE)innerflush();
return this;
}
public FastWriter write(char c)
{
return write((byte)c);
}
public FastWriter write(char[] s)
{
for(char c : s){
buf[ptr++] = (byte)c;
if(ptr == BUF_SIZE)innerflush();
}
return this;
}
public FastWriter write(String s)
{
s.chars().forEach(c -> {
buf[ptr++] = (byte)c;
if(ptr == BUF_SIZE)innerflush();
});
return this;
}
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(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;
}
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(); }
}
public void trnz(int... o)
{
for(int i = 0;i < o.length;i++)if(o[i] != 0)System.out.print(i+":"+o[i]+" ");
System.out.println();
}
// print ids which are 1
public void trt(long... o)
{
Queue<Integer> stands = new ArrayDeque<>();
for(int i = 0;i < o.length;i++){
for(long x = o[i];x != 0;x &= x-1)stands.add(i<<6|Long.numberOfTrailingZeros(x));
}
System.out.println(stands);
}
public void tf(boolean... r)
{
for(boolean x : r)System.out.print(x?'#':'.');
System.out.println();
}
public void tf(boolean[]... b)
{
for(boolean[] r : b) {
for(boolean x : r)System.out.print(x?'#':'.');
System.out.println();
}
System.out.println();
}
public void tf(long[]... b)
{
if(INPUT.length() != 0) {
for (long[] r : b) {
for (long x : r) {
for (int i = 0; i < 64; i++) {
System.out.print(x << ~i < 0 ? '#' : '.');
}
}
System.out.println();
}
System.out.println();
}
}
public void tf(long... b)
{
if(INPUT.length() != 0) {
for (long x : b) {
for (int i = 0; i < 64; i++) {
System.out.print(x << ~i < 0 ? '#' : '.');
}
}
System.out.println();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | dbf9175ff69ec5d5bfeb119a84806f58 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class Main {
static class Reader {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer = new StringTokenizer("");
String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
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 {
reader.close();
}
}
public static void main(String[] args) throws IOException {
Reader sc = new Reader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int TT = sc.nextInt();
while (TT-- > 0) {
char[] s = sc.next().toCharArray();
int n = s.length;
List<Integer> ans = new ArrayList<>();
for (int i = 1; i < n - 1; i++) {
if (s[0] > s[n - 1] && s[i] <= s[0] && s[i] >= s[n - 1]) ans.add(i);
else if (s[0] <= s[n - 1] && s[i] <= s[n - 1] && s[i] >= s[0]) ans.add(i);
}
out.println(Math.abs(s[n - 1] - s[0]) + " " + (ans.size() + 2));
if (s[0] > s[n - 1]) ans.sort((a, b) -> s[b] - s[a]);
else ans.sort(Comparator.comparingInt(a -> s[a]));
out.print(1 + " ");
for (int i : ans) out.print((i + 1) + " ");
out.println(n);
}
out.flush();
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 6a76b4faa6048e495b1de2a6066529d7 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | /*############################################################################################################
########################################## >>>> Diaa12360 <<<< ###############################################
########################################### Just Nothing #################################################
#################################### If You Need it, Fight For IT; #########################################
###############################################.-. 1 5 9 2 .-.################################################
############################################################################################################*/
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Solution {
public static void main(String[] args) throws Exception {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
while (t-- > 0) {
char[] s = in.nextString().toCharArray();
HashMap<Character, ArrayList<Integer>> mp = new HashMap<>();
for (int i = 0; i < s.length; i++) {
if (!mp.containsKey(s[i]))
mp.put(s[i], new ArrayList<>());
mp.get(s[i]).add(i);
}
int d = s[0] < s[s.length - 1] ? 1 : -1;
ArrayList<Integer> ans = new ArrayList<>();
for (char c = s[0]; c != s[s.length - 1] + d; c += d) {
ans.addAll(mp.getOrDefault(c, new ArrayList<>()));
}
int c = 0;
for (int i = 1; i < ans.size(); i++) {
c += Math.abs(s[ans.get(i)] - s[ans.get(i - 1)]);
}
out.print(c);
out.print(' ');
out.println(ans.size());
for (int x : ans) {
out.print((x + 1));
out.print(' ');
}
out.println();
}
out.flush();
}
static int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
static int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
//all primes
static ArrayList<Integer> primes;
static boolean[] primesB;
//sieve algorithm
static void sieve(int n) {
primes = new ArrayList<>();
primesB = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
primesB[i] = true;
}
for (int i = 2; i * i <= n; i++) {
if (primesB[i]) {
for (int j = i * i; j <= n; j += i) {
primesB[j] = false;
}
}
}
for (int i = 0; i <= n; i++) {
if (primesB[i]) {
primes.add(i);
}
}
}
// Function to find gcd of array of
// numbers
static int findGCD(int[] arr, int n) {
int result = arr[0];
for (int element : arr) {
result = gcd(result, element);
if (result == 1) {
return 1;
}
}
return result;
}
private static class FastScanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner() throws IOException {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private short nextShort() throws IOException {
short ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = (short) (ret * 10 + c - '0'); while ((c = read()) >= '0' && c <= '9');
if (neg) return (short) -ret;
return ret;
}
private int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
private char nextChar() throws IOException {
byte c = read();
while (c <= ' ') c = read();
return (char) c;
}
private String nextString() throws IOException {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <= ' ') c = read();
do {
ret.append((char) c);
} while ((c = read()) > ' ');
return ret.toString();
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
}
private static int anInt(String s) {
return Integer.parseInt(s);
}
private static long ll(String s) {
return Long.parseLong(s);
}
}
class Pair<K, V> implements Comparable<Pair<K, V>> {
K first;
V second;
Pair(K f, V s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair<K, V> o) {
return 0;
}
}
class PairInt implements Comparable<PairInt> {
int first;
int second;
PairInt(int f, int s) {
first = f;
second = s;
}
@Override
public int compareTo(PairInt o) {
if (this.first > o.first) {
return 1;
} else if (this.first < o.first) return -1;
else {
if (this.second < o.second) return 1;
else if (this.second == o.second) return -1;
return 0;
}
}
@Override
public String toString() {
return "<" + first + ", " + second + ">";
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 823e06b86572bd588ffa552ffe8af13c | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | /*############################################################################################################
########################################## >>>> Diaa12360 <<<< ###############################################
########################################### Just Nothing #################################################
#################################### If You Need it, Fight For IT; #########################################
###############################################.-. 1 5 9 2 .-.################################################
############################################################################################################*/
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Solution {
public static void main(String[] args) throws Exception {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
while (t-- > 0) {
char[] s = in.nextString().toCharArray();
HashMap<Character, ArrayList<Integer>> mp = new HashMap<>();
for (int i = 0; i < s.length; i++) {
if (!mp.containsKey(s[i]))
mp.put(s[i], new ArrayList<>());
mp.get(s[i]).add(i);
}
int d = s[0] < s[s.length-1] ? 1 : -1;
ArrayList<Integer> ans = new ArrayList<>();
for (char c = s[0]; c != s[s.length - 1] + d ; c += d) {
ans.addAll(mp.getOrDefault(c, new ArrayList<>()));
}
int c = 0;
for (int i = 1; i < ans.size(); i++) {
c += Math.abs(s[ans.get(i)] - s[ans.get(i - 1)]);
}
out.println(c + " " + ans.size());
for (int x: ans) {
out.print((x + 1) + " ");
}
out.println();
}
out.flush();
}
static int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
static int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
//all primes
static ArrayList<Integer> primes;
static boolean[] primesB;
//sieve algorithm
static void sieve(int n) {
primes = new ArrayList<>();
primesB = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
primesB[i] = true;
}
for (int i = 2; i * i <= n; i++) {
if (primesB[i]) {
for (int j = i * i; j <= n; j += i) {
primesB[j] = false;
}
}
}
for (int i = 0; i <= n; i++) {
if (primesB[i]) {
primes.add(i);
}
}
}
// Function to find gcd of array of
// numbers
static int findGCD(int[] arr, int n) {
int result = arr[0];
for (int element : arr) {
result = gcd(result, element);
if (result == 1) {
return 1;
}
}
return result;
}
private static class FastScanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner() throws IOException {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private short nextShort() throws IOException {
short ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = (short) (ret * 10 + c - '0'); while ((c = read()) >= '0' && c <= '9');
if (neg) return (short) -ret;
return ret;
}
private int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
private char nextChar() throws IOException {
byte c = read();
while (c <= ' ') c = read();
return (char) c;
}
private String nextString() throws IOException {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <= ' ') c = read();
do {
ret.append((char) c);
} while ((c = read()) > ' ');
return ret.toString();
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
}
private static int anInt(String s) {
return Integer.parseInt(s);
}
private static long ll(String s) {
return Long.parseLong(s);
}
}
class Pair<K, V> implements Comparable<Pair<K, V>> {
K first;
V second;
Pair(K f, V s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair<K, V> o) {
return 0;
}
}
class PairInt implements Comparable<PairInt> {
int first;
int second;
PairInt(int f, int s) {
first = f;
second = s;
}
@Override
public int compareTo(PairInt o) {
if (this.first > o.first) {
return 1;
} else if (this.first < o.first) return -1;
else {
if (this.second < o.second) return 1;
else if (this.second == o.second) return -1;
return 0;
}
}
@Override
public String toString() {
return "<" + first + ", " + second + ">";
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 6c38456cc2ff73b52f4db3d2646ecd39 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.math.BigInteger;
import java.util.*;
import java.io.*;
public class test1 {
static int m, d, n;
static char[] a;
static long[][][][] dp;
static long sum;
static int sx;
static int sy;
static long[][] memo;
static int c=0;
public static long dp(int i,int j) {
//System.out.println(i+" "+j);
c++;
if(i>n||j>m)return (long)1e12;
if(i==n&&j==m) {
if(Math.abs(sx-i)+Math.abs(sy-j)>d)return 0;
return (long)1e12;
}
if(Math.abs(sx-i)+Math.abs(sy-j)<=d) {
// System.out.println(i+" "+j);
return (long)1e12;
}
if(memo[i-1][j-1]!=-1)return memo[i-1][j-1];
long down=1+dp(i+1,j);
long right=1+dp(i,j+1);
// long cross=1+dp(i+1,j+1);
return memo[i-1][j-1]= Math.min(right,down);
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter sp = new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0) {
String s=sc.next();
HashMap<Character, Stack<Integer>> h=new HashMap<Character, Stack<Integer>>();
char[] x=s.toCharArray();
for(int i=x.length-1;i>=0;i--) {
if(!h.containsKey(x[i])) {
Stack<Integer>a=new Stack<Integer>();
a.push(i);
h.put(x[i], a);
}
else {
Stack<Integer>a=h.get(x[i]);
a.push(i);
h.put(x[i], a);
}
}
StringBuilder ans=new StringBuilder();
int sum=0;
int jump=1;
char st=x[0];
char end=x[x.length-1];
Arrays.sort(x);
int min1=x.length;
int max1=0;
int min2=x.length;
int max2=0;
for(int i=0;i<x.length;i++) {
if(x[i]==st) {
if(i>=max1)max1=i;
if(i<=min1)min1=i;
}
if(x[i]==end) {
if(i>=max2)max2=i;
if(i<=min2)min2=i;
}
}
if(max1>min2) {
Stack<Integer> a=h.get(x[max1]);
ans.append(a.pop()+1+" ");
h.put(x[max1], a);
for(int i=max1;i>min2&&i>0;i--) {
sum+=Math.abs(x[i]-x[i-1]);
jump++;
Stack<Integer> temp=h.get(x[i-1]);
ans.append(temp.pop()+1+" ");
h.put(x[i-1], temp);
}
}
else {
Stack<Integer> a=h.get(x[min1]);
ans.append(a.pop()+1+" ");
h.put(x[min1], a);
for(int i=min1;i<max2&&i<x.length-1;i++) {
sum+=Math.abs(x[i]-x[i+1]);
jump++;
Stack<Integer> temp=h.get(x[i+1]);
//System.out.println(i);
ans.append(temp.pop()+1+" ");
h.put(x[i+1], temp);
}
}
sp.println(sum+" "+jump);
sp.println(ans.toString());
}
sp.flush();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws IOException {
br = new BufferedReader(new FileReader(file));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String readAllLines(BufferedReader reader) throws IOException {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
return content.toString();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
class Pair implements Comparable<Pair> {
long x;
long y;
//long z;
public Pair(long x, long y) {
this.x = x;
this.y = y;
//this.z=z;
}
// @Override
/*public int compareTo(Pair o) {
//return Long.compare(o.z, z);
}*/
public boolean contains(Pair q) {
if (q.x == x || q.x == y || q.y == x || q.y == y)
return true;
return false;
}
public long getx() {
return x;
}
public long gety() {
return y;
}
public String toString() {
return x + " " + y;
}
@Override
public int compareTo(Pair o) {
// TODO Auto-generated method stub
return 0;
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | c4bc378c4006a870447177d533fe9908 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.math.BigInteger;
import java.util.*;
import java.io.*;
public class test1 {
static int m, d, n;
static char[] a;
static long[][][][] dp;
static long sum;
static int sx;
static int sy;
static long[][] memo;
static int c=0;
public static long dp(int i,int j) {
//System.out.println(i+" "+j);
c++;
if(i>n||j>m)return (long)1e12;
if(i==n&&j==m) {
if(Math.abs(sx-i)+Math.abs(sy-j)>d)return 0;
return (long)1e12;
}
if(Math.abs(sx-i)+Math.abs(sy-j)<=d) {
// System.out.println(i+" "+j);
return (long)1e12;
}
if(memo[i-1][j-1]!=-1)return memo[i-1][j-1];
long down=1+dp(i+1,j);
long right=1+dp(i,j+1);
// long cross=1+dp(i+1,j+1);
return memo[i-1][j-1]= Math.min(right,down);
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter sp = new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0) {
String s=sc.next();
HashMap<Character, Stack<Integer>> h=new HashMap<Character, Stack<Integer>>();
char[] x=s.toCharArray();
for(int i=x.length-1;i>=0;i--) {
if(!h.containsKey(x[i])) {
Stack<Integer>a=new Stack<Integer>();
a.push(i);
h.put(x[i], a);
}
else {
Stack<Integer>a=h.get(x[i]);
a.push(i);
h.put(x[i], a);
}
}
//System.out.println(h.get('e').size());
StringBuilder ans=new StringBuilder();
//for(int i:h.get(x[0]))ans+=i+" ";
int sum=0;
int jump=1;
char st=x[0];
char end=x[x.length-1];
Arrays.sort(x);
//for(char i: x)System.out.print(i+" ");
//System.out.println();
int min1=x.length;
int max1=0;
int min2=x.length;
int max2=0;
for(int i=0;i<x.length;i++) {
if(x[i]==st) {
if(i>=max1)max1=i;
if(i<=min1)min1=i;
}
if(x[i]==end) {
if(i>=max2)max2=i;
if(i<=min2)min2=i;
}
}
//System.out.println(max1+" "+min1);
//System.out.println(max2+" "+min2);
if(max1>min2) {
// for(int j:h.get(x[max1])) {
// ans+=(j+1)+" ";
// HashSet<Integer> temp=h.get(x[max1]);
// temp.remove(j);
// h.put(x[max1], temp);
// break;
// }
Stack<Integer> a=h.get(x[max1]);
ans.append(a.pop()+1+" ");
h.put(x[max1], a);
for(int i=max1;i>min2&&i>0;i--) {
sum+=Math.abs(x[i]-x[i-1]);
jump++;
Stack<Integer> temp=h.get(x[i-1]);
ans.append(temp.pop()+1+" ");
h.put(x[i-1], temp);
// for(int j:h.get(x[i-1])) {
// ans+=(j+1)+" ";
// Stack<Integer> temp=h.get(x[i-1]);
// temp.remove(j);
// h.put(x[i-1], temp);
// break;
// }
}
}
else {
Stack<Integer> a=h.get(x[min1]);
ans.append(a.pop()+1+" ");
h.put(x[min1], a);
// for(int j:h.get(x[min1])) {
// ans+=(j+1)+" ";
// HashSet<Integer> temp=h.get(x[min1]);
// temp.remove(j);
// h.put(x[min1], temp);
// break;
// }
for(int i=min1;i<max2&&i<x.length-1;i++) {
sum+=Math.abs(x[i]-x[i+1]);
jump++;
Stack<Integer> temp=h.get(x[i+1]);
//System.out.println(i);
ans.append(temp.pop()+1+" ");
// System.out.println(i);
h.put(x[i+1], temp);
// for(int j:h.get(x[i+1])) {
// ans+=(j+1)+" ";
// HashSet<Integer> temp=h.get(x[i+1]);
// temp.remove(j);
// h.put(x[i+1], temp);
// break;
// }
}
}
sp.println(sum+" "+jump);
sp.println(ans.toString());
}
sp.flush();
}
//c c d e e f o o r s
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws IOException {
br = new BufferedReader(new FileReader(file));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String readAllLines(BufferedReader reader) throws IOException {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
return content.toString();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
class Pair implements Comparable<Pair> {
long x;
long y;
//long z;
public Pair(long x, long y) {
this.x = x;
this.y = y;
//this.z=z;
}
// @Override
/*public int compareTo(Pair o) {
//return Long.compare(o.z, z);
}*/
public boolean contains(Pair q) {
if (q.x == x || q.x == y || q.y == x || q.y == y)
return true;
return false;
}
public long getx() {
return x;
}
public long gety() {
return y;
}
public String toString() {
return x + " " + y;
}
@Override
public int compareTo(Pair o) {
// TODO Auto-generated method stub
return 0;
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | c4625cece8fd8bf3f352ba69f9545cca | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
import java.io.*;
public class q3 {
static FastScanner fs = new FastScanner();
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) {
int t = fs.nextInt();
while (t-- > 0){
String s = fs.next();
List<List<Integer>> lis = new ArrayList<>();
for (int i=0;i<26;i++) lis.add(new ArrayList<>());
int len = s.length();
for (int i=0;i<len;i++) lis.get(s.charAt(i) - 'a').add(i);
pw.print(Math.abs(s.charAt(0) - s.charAt(len - 1)) + " ");
int m = 0;
List<Integer> ans = new ArrayList<>();
ans.add(0);
if (s.charAt(0) <= s.charAt(len - 1)){
for (int i=s.charAt(0)-'a';i<=s.charAt(len - 1)-'a';i++){
m += lis.get(i).size();
for (Integer a : lis.get(i)){
if (a == 0 || a == len - 1) continue;
ans.add(a);
}
}
ans.add(len - 1);
pw.println(m);
for (Integer a : ans) pw.print((a + 1) + " ");
pw.println();
}
else{
for (int i=s.charAt(0)-'a';i>=s.charAt(len - 1)-'a';i--){
m += lis.get(i).size();
for (Integer a : lis.get(i)){
if (a == 0 || a == len - 1) continue;
ans.add(a);
}
}
ans.add(len - 1);
pw.println(m);
for (Integer a : ans) pw.print((a + 1) + " ");
pw.println();
}
}
pw.close();
}
// ----------input function----------
static void sort(int[] a) {
ArrayList<Integer> L = new ArrayList<>();
for (int i : a)
L.add(i);
Collections.sort(L);
for (int i = 0; i < a.length; i++)
a[i] = L.get(i);
}
static class 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());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | f657255fd4225b122866bc93835905ee | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
import java.io.*;
public class q3 {
static FastScanner fs = new FastScanner();
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) {
int t = fs.nextInt();
while (t-- > 0){
String s = fs.next();
List<List<Integer>> lis = new ArrayList<>();
for (int i=0;i<26;i++) lis.add(new ArrayList<>());
for (int i=0;i<s.length();i++){
lis.get(s.charAt(i) - 'a').add(i);
}
if (s.charAt(0) == s.charAt(s.length() - 1)){
pw.println(0 + " " + (lis.get(s.charAt(0) - 'a').size()));
pw.print(1 + " ");
for (Integer aa : lis.get(s.charAt(0) - 'a')){
if (aa == 0 || aa == s.length() - 1) continue;
pw.print(aa + 1 + " ");
}
pw.println(s.length());
}
else if (s.charAt(0) < s.charAt(s.length() - 1)){
pw.print(s.charAt(s.length() - 1) - s.charAt(0) + " ");
int cnt = 0;
for (int i=s.charAt(0)-'a';i<=s.charAt(s.length() - 1) - 'a';i++){
cnt += lis.get(i).size();
}
pw.println(cnt);
pw.print(1 + " ");
for (int i=s.charAt(0)-'a';i<=s.charAt(s.length() - 1) - 'a';i++){
for (Integer aa : lis.get(i)){
if (aa == 0 || aa == s.length() - 1) continue;
pw.print(aa + 1 + " ");
}
}
pw.println(s.length());
}
else{
pw.print(s.charAt(0) - s.charAt(s.length() - 1) + " ");
int cnt = 0;
for (int i=s.charAt(0)-'a';i>=s.charAt(s.length() - 1) - 'a';i--){
cnt += lis.get(i).size();
}
pw.println(cnt);
pw.print(1 + " ");
for (int i=s.charAt(0)-'a';i>=s.charAt(s.length() - 1) - 'a';i--){
for (Integer aa : lis.get(i)){
if (aa == 0 || aa == s.length() - 1) continue;
pw.print(aa + 1 + " ");
}
}
pw.println(s.length());
}
}
pw.close();
}
// ----------input function----------
static void sort(int[] a) {
ArrayList<Integer> L = new ArrayList<>();
for (int i : a)
L.add(i);
Collections.sort(L);
for (int i = 0; i < a.length; i++)
a[i] = L.get(i);
}
static class 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());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 6a3b00d314a8fd74cb811a4b9408e728 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
public class Main {
static int mod=(int)1e9+7;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
char[] c=sc.next().toCharArray();
int n=c.length;
int cost=Math.abs(c[0]-c[n-1]);
ArrayList<Integer> l=new ArrayList<>();
for(int i=1;i<n-1;i++){
if ((c[i] >= c[0] && c[i] <= c[n-1]) || (c[i] <= c[0] && c[i] >= c[n-1])) {
l.add(i);
}
}
if(c[0]<c[n-1]){
Collections.sort(l,(x,y)->c[x]-c[y]);
}
else{
Collections.sort(l,(x,y)->c[y]-c[x]);
}
int moves=l.size()+2;
System.out.println(cost+" "+moves);
System.out.print(1+" ");
for(int i=0;i<l.size();i++){
System.out.print((l.get(i)+1)+" ");
}
System.out.println(n);
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 94e7cf5e700091b086f3000be23c00a0 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class CF_1729C {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
String s = in.next();
int n = s.length();
int cost = Math.abs(s.charAt(0) - s.charAt(n - 1));
char start = s.charAt(0);
char end = s.charAt(n - 1);
boolean decreasing = false;
if (start > end) {
decreasing = true;
}
int considerLetter[] = new int[26];
for (int i = 1; i < n - 1; i++) {
char curr = s.charAt(i);
if (decreasing) {
if (curr <= start && curr >= end) {
considerLetter[curr - 'a']++;
}
} else {
if (curr >= start && curr <= end) {
considerLetter[curr - 'a']++;
}
}
}
int startPos = start - 'a';
int endPos = end - 'a';
if (decreasing) {
int temp = startPos;
startPos = endPos;
endPos = temp;
}
int jumps = 2;
ArrayList<Integer> jumpPos = new ArrayList<Integer>();
for (int i = startPos; i <= endPos; i++) {
if (considerLetter[i] > 0) {
jumps += considerLetter[i];
for (int j = 1; j < n - 1; j++) {
if (s.charAt(j) == (char) (i + 'a'))
jumpPos.add(j + 1);
}
}
}
System.out.println(cost + " " + jumps);
System.out.print("1 ");
if (decreasing) {
for (int i = jumpPos.size() - 1; i >= 0; i--) {
System.out.print(jumpPos.get(i) + " ");
}
} else {
for (int i = 0; i < jumpPos.size(); i++) {
System.out.print(jumpPos.get(i) + " ");
}
}
System.out.println(n);
}
in.close();
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 25ad37ad09b1071ee30e7cff962ad291 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
String s = sc.next();
char[] ch = s.toCharArray();
Map<Character, List<Integer>> map = new HashMap<>();
int i = 0;
for(char c : ch) {
if(!map.containsKey(c)) {
map.put(c, new ArrayList<>());
}
List<Integer> l = map.get(c);
l.add(i++);
}
int dir = ch[0] < ch[s.length() - 1] ? 1 : -1;
List<Integer> l = new ArrayList<>();
for(char c = ch[0]; c != ch[s.length() - 1] + dir; c += dir) {
for(int v : map.getOrDefault(c, new ArrayList<>())) {
l.add(v);
}
}
int cost = 0;
for(i = 1; i < l.size(); i++) {
cost += Math.abs(ch[l.get(i)] - ch[l.get(i - 1)]);
}
System.out.println(cost + " " + l.size());
for(i = 0; i < l.size(); i++) {
System.out.print((l.get(i) + 1) + " ");
}
System.out.println();
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 8d90cd99ebf5fff09168b9b49f7a8263 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
Solve solver = new Solve();
int T = 1;
T = in.nextInt();
for (int i = 1; i <= T; i++) solver.solve(i, in, out);
out.close();
}
static class Solve {
public int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
static final long inf = 0x3f3f3f3f;
static final int N = (int) 2e5 + 7;
static final int mod = (int) 1e9 + 7;
static final int[] monthDay = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public void solve(int testNumber, InputReader in, OutputWriter out) {
//ow.println("Case #"+testNumber+": ");
//write code here
//long startTime = System.currentTimeMillis();
char[] ch = in.next().toCharArray();
int n = ch.length;
char str = ch[0],end = ch[n - 1];
int cost = Math.abs(ch[0] - ch[n - 1]);
Map<Character,List<Integer>> map = new HashMap<>();
int[] cnt = new int[26];
for (int i = 0; i < ch.length; i++) {
List<Integer> l = map.getOrDefault(ch[i], new ArrayList<>());
l.add(i+1);
map.put(ch[i],l);
}
Set<Integer>[] graph = new Set[26];
for (int i = 0; i < 26; i++) {
graph[i] = new HashSet<>();
}
for (Map.Entry<Character, List<Integer>> it : map.entrySet()) {
Character key = it.getKey();
List<Integer> list = it.getValue();
for (Integer itt : list) {
graph[key - 'a'].add(itt);
}
}
Deque<Integer> q = new ArrayDeque<>();
if(str<=end){
for (int i = str - 'a'; i <= end - 'a'; i++) {
if(!graph[i].isEmpty()) for (Integer it : graph[i]) {
q.addLast(it);
}
}
}else{
for (int i = str - 'a'; i >= end - 'a'; i--) {
if(!graph[i].isEmpty()) for (Integer it : graph[i]) {
q.addLast(it);
}
}
}
out.println(cost + " " + q.size());
out.print(1 + " ");
boolean ok1 = true;
while (!q.isEmpty()) {
if(q.peekFirst()==1 || q.peekFirst()==n){
q.pollFirst();
continue;
}
out.print(q.pollFirst() + " ");
}
out.print(n);
out.println();
//long endTime = System.currentTimeMillis();int time=(int) (endTime - startTime);System.out.println("运行时间:" + time + "ms");
//here is end!
}
static void test(int[] nums, OutputWriter out) {
for (int num : nums) out.print(num + " ");
out.println();
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
writer.println();
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
long sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return nextString();
}
public interface SpaceCharFilter {
boolean isSpaceChar(int ch);
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 6484240c9862162f055d58336b458413 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | //created by Toufique on 12/09/2022
import java.io.*;
import java.util.*;
public class Div3_820C {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = in.nextInt();
for (int tt = 0; tt < t; tt++) {
char[] s = in.next().toCharArray();
ArrayList<Long> ans;
if (s[0] < s[s.length - 1])ans = solve(s);
else ans = solve2(s);
long cost = ans.get(0);
int path = ans.size() - 1;
pw.println(cost + " " + path);
for (int i = 1; i < ans.size(); i++) {
pw.print(ans.get(i) + " ");
}
pw.println();
}
pw.close();
}
static ArrayList<Long> solve(char[] s) {
ArrayList<Long> ans = new ArrayList<>();
ArrayList<Pair> ls = new ArrayList<>();
for (int i = 0; i < s.length; i++) ls.add(new Pair(s[i], i + 1));
Collections.sort(ls);
int f = getF(ls, s[0]);
int l = getS(ls, s[s.length - 1]);
long min = 0;
if (f < l) {
for (int i = f; i < l; i++) {
min += Math.abs(((ls.get(i + 1).x - 'a') + 1) - ((ls.get(i).x - 'a') + 1));
}
} else {
for (int i = f; i > l; i--) {
min += Math.abs(((ls.get(i - 1).x - 'a') + 1) - ((ls.get(i).x - 'a') + 1));
}
}
ans.add(min);
if (f < l) {
for (int i = f; i <= l; i++) ans.add((long)ls.get(i).ind);
} else {
for (int i = f; i >= l; i--) ans.add((long)ls.get(i).ind);
}
return ans;
}
static ArrayList<Long> solve2(char[] s) {
ArrayList<Long> ans = new ArrayList<>();
ArrayList<Pair2> ls = new ArrayList<>();
for (int i = 0; i < s.length; i++) ls.add(new Pair2(s[i], i + 1));
Collections.sort(ls);
int f = getFF(ls, s[0]);
int l = getSS(ls, s[s.length - 1]);
long min = 0;
if (f < l) {
for (int i = f; i < l; i++) {
min += Math.abs(((ls.get(i + 1).x - 'a') + 1) - ((ls.get(i).x - 'a') + 1));
}
} else {
for (int i = f; i > l; i--) {
min += Math.abs(((ls.get(i - 1).x - 'a') + 1) - ((ls.get(i).x - 'a') + 1));
}
}
ans.add(min);
if (f < l) {
for (int i = f; i <= l; i++) ans.add((long)ls.get(i).ind);
} else {
for (int i = f; i >= l; i--) ans.add((long)ls.get(i).ind);
}
return ans;
}
static int getF(ArrayList<Pair> ls, char f) {
for (int i = 0; i < ls.size(); i++) {
if (ls.get(i).x == f) return i;
}
return 0;
}
static int getS(ArrayList<Pair> ls, char l) {
for (int i = ls.size() - 1; i >= 0; i--) {
if (ls.get(i).x == l) return i;
}
return 0;
}
static int getFF(ArrayList<Pair2> ls, char f) {
for (int i = 0; i < ls.size(); i++) {
if (ls.get(i).x == f) return i;
}
return 0;
}
static int getSS(ArrayList<Pair2> ls, char l) {
for (int i = ls.size() - 1; i >= 0; i--) {
if (ls.get(i).x == l) return i;
}
return 0;
}
static class Pair implements Comparable<Pair> {
char x;
int ind;
Pair(char x, int ind) {
this.x = x;
this.ind = ind;
}
@Override
public int compareTo(Pair o) {
return Integer.compare(this.x, o.x);
}
@Override
public String toString() {
return "[" + this.x + " " + this.ind + "]";
}
}
static class Pair2 implements Comparable<Pair2> {
char x;
int ind;
Pair2(char x, int ind) {
this.x = x;
this.ind = ind;
}
@Override
public int compareTo(Pair2 o) {
return Integer.compare(o.x, this.x);
}
@Override
public String toString() {
return "[" + this.x + " " + this.ind + "]";
}
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 166b08417bb8c233714e63d7508a0ddf | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main{
public static void main(String[] args) throws Exception {
// write your code here
Scanner sc = new Scanner( System.in );
int t = sc.nextInt();
while(t-->0){
String s = sc.next();
int low = (int)s.charAt(0);
int high = (int)s.charAt(s.length()-1);
if(low>high){
int tt = low;
low = high;
high = tt;
}
ArrayList<ArrayList<Integer>> arr = new ArrayList<>();
for(int i=0; i<26; i++) arr.add(new ArrayList<Integer>());
int len = 0;
for(int i=0; i<s.length(); i++){
int temp = (int)s.charAt(i);
if(low<=temp && temp<=high){
int idx = (int)(s.charAt(i)-'a');
arr.get(idx).add(i+1);
len++;
}
}
if(s.charAt(0)<=s.charAt(s.length()-1)){
int cost = (int)s.charAt(s.length()-1)-(int)s.charAt(0);
System.out.println(cost + " " + len);
for(ArrayList<Integer> row: arr){
for(Integer val: row){
System.out.print(val + " ");
}
}
} else {
int cost = -((int)s.charAt(s.length()-1)-(int)s.charAt(0));
System.out.println(cost + " " + len);
for(int i=arr.size()-1; i>=0; i--){
for(Integer val: arr.get(i)){
System.out.print(val + " ");
}
}
}
System.out.println();
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | d03a06140d24e8177a00a0380ed6df6c | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | /*
Goal: Become better in CP!
Key: Consistency and Discipline
Desire: SDE @ Google USA
Motto: Do what i Love <=> Love what i do
If you don't use your brain 100%, it deteriorates gradually
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class Coder {
static StringBuffer str=new StringBuffer();
static BufferedReader bf;
static PrintWriter pw;
static int n, k;
static char c[];
// Java is not = but it is equals()
// C++ = does not work here always
static void solve(int te) throws Exception{
List<Stack<Integer>> l=new ArrayList<>();
boolean vis[]=new boolean[n];
for(int i=0;i<26;i++) l.add(new Stack<>());
for(int i=n-1;i>=0;i--){
l.get(c[i]-'a').push(i);
}
List<Integer> res=new ArrayList<>();
long ans=0;
for(int i=0;i<n;i++){
// System.out.println(i);
vis[i]=true;
res.add(i);
if(i==n-1) break;
// look for same characters => max. jumps
int le=Math.min(c[i]-'a', c[n-1]-'a');
int ri=Math.max(c[i]-'a', c[n-1]-'a');
if(ri==c[n-1]-'a'){
for(int j=le;j<=ri;j++){
Stack<Integer> st=l.get(j);
int idx=-1;
while(!st.isEmpty()){
if(vis[st.peek()]) st.pop();
else{
idx=st.pop();
break;
}
}
if(idx==-1) continue;
// System.out.println("-"+idx);
ans+=Math.abs(j-(c[i]-'a'));
i=idx-1;
break;
}
}else{
for(int j=ri;j>=le;j--){
Stack<Integer> st=l.get(j);
int idx=-1;
while(!st.isEmpty()){
if(vis[st.peek()]) st.pop();
else{
idx=st.pop();
break;
}
}
if(idx==-1) continue;
ans+=Math.abs(j-(c[i]-'a'));
i=idx-1;
break;
}
}
}
str.append(ans).append(" ").append(res.size()).append("\n");
for(int i=0;i<res.size();i++) str.append(res.get(i)+1).append(" ");
str.append("\n");
}
public static void main(String[] args) throws java.lang.Exception {
boolean lenv=false;
if(lenv){
bf = new BufferedReader(
new FileReader("input.txt"));
pw=new PrintWriter(new
BufferedWriter(new FileWriter("output.txt")));
}else{
bf = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
int q1 = Integer.parseInt(bf.readLine().trim());
for(int te=1;te<=q1;te++) {
c=bf.readLine().trim().toCharArray();
n=c.length;
solve(te);
}
pw.print(str);
pw.flush();
// System.out.println(str);
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 4c893016539201ea9d96d3b2932f7561 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
FastScanner f = new FastScanner();
PrintWriter w = new PrintWriter(System.out);
int T = f.nextInt();
while (T-- > 0) {
char[] c = (" " + f.nextString()).toCharArray();
int n = c.length - 1;
int ans = Math.abs(c[1] - c[n]);
List<int[]> res = new ArrayList<>();
int max = c[1] > c[n] ? c[1] : c[n];
int min = max == c[1] ? c[n] : c[1];
for (int i = 2; i <= n - 1; i++) {
if (c[i] >= min && c[i] <= max) {
if (i != 1 && i != n) res.add(new int[]{c[i], i});
}
}
w.println(ans + " " + (res.size() + 2));
w.print(1 + " ");
if (c[1] < c[n]) Collections.sort(res, (o1, o2) -> o1[0] - o2[0]);
else Collections.sort(res, (o1, o2) -> o2[0] - o1[0]);
for (int i = 0; i < res.size(); i++) {
w.print(res.get(i)[1] + " ");
}
w.println(n);
}
w.flush();
}
private static class FastScanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
private FastScanner() throws IOException {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private short nextShort() throws IOException {
short ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = (short) (ret * 10 + c - '0');
while ((c = read()) >= '0' && c <= '9');
if (neg) return (short) -ret;
return ret;
}
private int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
private char nextChar() throws IOException {
byte c = read();
while (c <= ' ') c = read();
return (char) c;
}
private String nextString() throws IOException {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <= ' ') c = read();
do {
ret.append((char) c);
} while ((c = read()) > ' ');
return ret.toString();
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | bbf89234d36ccb605a5f32dcd7f0f846 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter output = new BufferedWriter(
new OutputStreamWriter(System.out));
int t = Integer.valueOf(in.readLine());
for(int c = 0 ; c < t ; ++c){
char[]s = in.readLine().toCharArray();
int n = s.length;
ArrayList<Integer> letters[] = new ArrayList[26];
for(int i = 0 ; i < 26 ; ++i){
letters[i] = new ArrayList<>();
}
for(int i = 0 ; i < n ; ++i){
letters[s[i]-'a'].add(i + 1);
}
char begin = s[ 0 ];
char end = s[n - 1];
int points = Math.abs(begin - end);
int hops = 0;
if(begin <= end){
for(int j = begin - 'a' ; j <= end - 'a' ; ++j){
for(int k = 0 ; k < letters[j].size() ; ++k){
hops++;
}
}
}else{
for(int j = begin - 'a' ; j >= end - 'a' ; --j){
for(int k = 0 ; k < letters[j].size() ; ++k){
hops++;
}
}
}
output.write(points +" " + hops+"\n");
if(begin <= end){
for(int j = begin - 'a' ; j <= end - 'a' ; ++j){
for(int k = 0 ; k < letters[j].size() ; ++k){
output.write(letters[j].get(k)+" ");
}
}
}else{
for(int j = begin - 'a' ; j >= end - 'a' ; --j){
for(int k = 0 ; k < letters[j].size() ; ++k){
output.write(letters[j].get(k)+" ");
}
}
}
output.write("\n");
}
output.flush();
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | c25213786ed5870e34b7bf25c49c206a | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.TreeSet;
public class Main {
static InputReader sc;
public static void main(String[] args) throws Exception {
sc=new InputReader(System.in);
int T=sc.nextInt();
while(T-->0) {
char[] str=(" "+sc.next()).toCharArray();
HashMap<Character,TreeSet<Integer>> map=new HashMap<>();
for(int i=1;i<str.length;i++) {
if(!map.containsKey(str[i]))
map.put(str[i],new TreeSet<>());
map.get(str[i]).add(i);
}
int cost=Math.abs(str[1]-str[str.length-1]);
LinkedList<Integer> list=new LinkedList<>();
if(str[1]<=str[str.length-1]) {
for(char c=str[1];c<=str[str.length-1];c++) {
if(!map.containsKey(c))
continue;
for(int k:map.get(c))
list.addLast(k);
}
}
else {
for(char c=str[1];c>=str[str.length-1];c--) {
if(!map.containsKey(c))
continue;
for(int k:map.get(c))
list.addLast(k);
}
}
System.out.println(cost+" "+list.size());
for(int k:list)
System.out.print(k+" ");
System.out.println();
}
}
static class InputReader {
BufferedReader br;
public InputReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
}
public int nextInt() throws IOException {
int c = br.read();
while (c <= 32) {
c = br.read();
}
boolean negative = false;
if (c == '-') {
negative = true;
c = br.read();
}
int x = 0;
while (c > 32) {
x = x * 10 + c - '0';
c = br.read();
}
return negative ? -x : x;
}
public long nextLong() throws IOException {
int c = br.read();
while (c <= 32) {
c = br.read();
}
boolean negative = false;
if (c == '-') {
negative = true;
c = br.read();
}
long x = 0;
while (c > 32) {
x = x * 10 + c - '0';
c = br.read();
}
return negative ? -x : x;
}
public String next() throws IOException {
int c = br.read();
while (c <= 32) {
c = br.read();
}
StringBuilder sb = new StringBuilder();
while (c > 32) {
sb.append((char) c);
c = br.read();
}
return sb.toString();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 216fee415fb225850220ee989aafafbd | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | // Java program to illustrate the
// fast input output
import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.StringTokenizer;
public class template{
static FastReader reader = new FastReader();
static FastWriter writer = new FastWriter();
static void tempMap(HashMap<Integer, Integer> map){
for(Map.Entry<Integer,Integer> entry: map.entrySet()){
}
}
static void solve(){
try{
String str = reader.readString();
PriorityQueue<Pair> que;
int n = str.length();
boolean tag = false;
char first = str.charAt(0);
char last = str.charAt(n-1);
if(first < last){
tag = true;
que = new PriorityQueue<>(new Comparator<Pair>(){
public int compare(Pair o1, Pair o2){
return o1.x - o2.x;
}
});
}
else{
que = new PriorityQueue<>(new Comparator<Pair>(){
public int compare(Pair o1, Pair o2){
return o2.x - o1.x;
}
});
}
int count = 2;
for(int i = 1 ; i < str.length()-1 ; i++){
char curr = str.charAt(i);
if(tag){
if(curr >= first && curr <= last){
que.add(new Pair(curr,i+1));
count++;
}
}else{
if(curr <= first && curr >= last){
que.add(new Pair(curr,i+1));
count++;
}
}
}
writer.writeString(Integer.toString((last>first)?last-first:first-last) + " " + Integer.toString(count));
writer.newLine();
writer.writeString(Integer.toString(1) + " ");
while(!que.isEmpty()){
int index = que.poll().y;
writer.writeString(Integer.toString(index) + " ");
}
writer.writeString(Integer.toString(n) + " ");
writer.newLine();
}catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args) throws IOException{
try{
int t = reader.readSingleInt();
while(t-- != 0){
solve();
}
}catch(Exception e){
System.out.println(e);
}
}
public static class Pair{
char x;
int y;
Pair(){}
Pair(char x, int y){
this.x = x;
this.y = y;
}
}
// Fast Reader Class
public static class FastReader {
// Reader object
BufferedReader reader;
// Constructor
public FastReader()
{
// Initialize the reader
reader = new BufferedReader(
new InputStreamReader(
System.in));
if (System.getProperty(
"ONLINE_JUDGE")
== null) {
try {
reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(
"input.txt")));
}
catch (Exception e) {
}
}
}
// String tokenizer
StringTokenizer tokenizer;
// Function to read a
// single integer
public int readSingleInt()
throws IOException
{
return Integer.parseInt(
reader.readLine());
}
// Function to read a
// single long
public long readSingleLong()
throws IOException
{
return Long.parseLong(
reader.readLine());
}
// Function to read a array
// of numsInts integers
// in one line
public int[] readIntArray(int numInts)
throws IOException
{
int[] nums = new int[numInts];
tokenizer
= new StringTokenizer(
reader.readLine());
for (int i = 0;
i < numInts; i++) {
nums[i] = Integer.parseInt(
tokenizer.nextToken());
}
return nums;
}
public long[] readLongArray(int numInts)
throws IOException
{
long[] nums = new long[numInts];
tokenizer
= new StringTokenizer(
reader.readLine());
for (int i = 0;
i < numInts; i++) {
nums[i] = Long.parseLong(
tokenizer.nextToken());
}
return nums;
}
// Function to read string
public String readString()
throws IOException
{
return reader.readLine();
}
}
// Fast Writer Class
public static class FastWriter {
// Writer object
BufferedWriter writer;
// Constructor
public FastWriter()
{
// Initialize the writer
writer = new BufferedWriter(
new OutputStreamWriter(
System.out));
if (System.getProperty(
"ONLINE_JUDGE")
== null) {
try {
writer = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(
"output.txt")));
}
catch (Exception e) {
}
}
}
// Function to write the
// single integer
public void writeSingleInteger(int i)
throws IOException
{
writer.write(Integer.toString(i));
writer.flush();
}
public void newLine()
throws IOException
{
writer.newLine();
writer.flush();
}
// Function to write single long
public void writeSingleLong(long i)
throws IOException
{
writer.write(Long.toString(i));
writer.flush();
}
// Function to write a Integer of
// array with spaces in one line
public void writeIntArrayWithSpaces(int[] nums)
throws IOException
{
for (int i = 0;
i < nums.length; i++) {
writer.write(nums[i]
+ " ");
}
writer.newLine();
writer.flush();
}
// Function to write Integer of
// array without spaces in 1 line
public void writeIntArrayWithoutSpaces(int[] nums)
throws IOException
{
for (int i = 0;
i < nums.length; i++) {
writer.write(
Integer.toString(
nums[i]));
}
writer.newLine();
writer.flush();
}
// Function to write String
public void writeString(String s)
throws IOException
{
writer.write(s);
writer.flush();
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 8a36ba8a72eddb7b46c348bd59457815 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.StringTokenizer;
public class Jumping_on_Tiles
{
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static PrintWriter out;
public static void main (String[] args) throws java.lang.Exception
{
out = new PrintWriter(System.out);
try
{
Scanner obj = new Scanner (System.in);
//Reader obj = new Reader ();
int t = obj.nextInt();
while (t > 0)
{
t--;
String str = obj.next();
char[] ch = str.toCharArray();
int l = str.length(), i, j, k, m = 0;
ArrayList <ArrayList<Integer>> arr = new ArrayList <ArrayList<Integer>>(26);
for (i=0;i<26;i++)
{
arr.add (new ArrayList<Integer>());
}
for (i=0;i<l;i++)
{
int num = (int)ch[i] - 97;
arr.get (num).add(i+1);
}
j = (int)ch[0] - 97;
k = (int)ch[l - 1] - 97;
if (j <= k)
{
for (i=j;i<=k;i++)
{
int n = arr.get(i).size();
if (n > 0)
{
m += n;
}
}
out.println ((k - j) + " " + m);
for (i=j;i<=k;i++)
{
int n = arr.get(i).size();
for (int q=0;q<n;q++)
{
out.print (arr.get(i).get(q) + " ");
}
}
out.println();
}
else
{
for (i=j;i>=k;i--)
{
int n = arr.get(i).size();
if (n > 0)
{
m += n;
}
}
out.println ((j - k) + " " + m);
for (i=j;i>=k;i--)
{
int n = arr.get(i).size();
for (int q=0;q<n;q++)
{
out.print (arr.get(i).get(q) + " ");
}
}
out.println();
}
}
}catch(Exception e){
return;
}
out.flush();
out.close();
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | e3094633aca9d6b14b72fa9ab808476b | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.StringTokenizer;
public class Jumping_on_Tiles
{
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static PrintWriter out;
public static void main (String[] args) throws java.lang.Exception
{
out = new PrintWriter(System.out);
try
{
Scanner obj = new Scanner (System.in);
//Reader obj = new Reader ();
int t = obj.nextInt();
while (t > 0)
{
t--;
String str = obj.next();
char[] ch = str.toCharArray();
int l = str.length(), i, j, k, c = 0, m = 0;
ArrayList <ArrayList<Integer>> arr = new ArrayList <ArrayList<Integer>>(26);
for (i=0;i<26;i++)
{
arr.add (new ArrayList<Integer>());
}
for (i=0;i<l;i++)
{
int num = (int)ch[i] - 97;
arr.get (num).add(i+1);
}
j = (int)ch[0] - 97;
k = (int)ch[l - 1] - 97;
if (j <= k)
{
for (i=j;i<=k;i++)
{
int n = arr.get(i).size();
if (n > 0)
{
m += n;
}
}
out.println ((k - j) + " " + m);
for (i=j;i<=k;i++)
{
int n = arr.get(i).size();
for (int q=0;q<n;q++)
{
out.print (arr.get(i).get(q) + " ");
}
}
out.println();
}
else
{
for (i=j;i>=k;i--)
{
int n = arr.get(i).size();
if (n > 0)
{
m += n;
}
}
out.println ((j - k) + " " + m);
for (i=j;i>=k;i--)
{
int n = arr.get(i).size();
for (int q=0;q<n;q++)
{
out.print (arr.get(i).get(q) + " ");
}
}
out.println();
}
}
}catch(Exception e){
return;
}
out.flush();
out.close();
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | dbedb67311f5167d4f08c36014565181 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String s = br.readLine();
char a = s.charAt(0);
char b = s.charAt(s.length() - 1);
int cost = Math.abs(a - b);
TreeMap<Character, Set<Integer>> map = a < b
? new TreeMap<>()
: new TreeMap<>(Comparator.reverseOrder());
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (a <= c && c <= b || b <= c && c <= a) {
map.computeIfAbsent(c, k -> new LinkedHashSet<>()).add(i + 1);
}
}
int m = 0;
StringJoiner sj = new StringJoiner(" ");
for (Set<Integer> set : map.values()) {
for (int i : set) {
sj.add(String.valueOf(i));
}
m += set.size();
}
System.out.println(cost + " " + m + "\n" + sj);
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | e23251431a601b9d73500299db931555 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.net.SocketTimeoutException;
public class Main {
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
FastWriter fw = new FastWriter();
long tc = sc.nextLong();
// int tc = 1;
while (tc-- > 0) {
String s = sc.nextLine();
char[] cr = s.toCharArray();
int m = 0;
int n = s.length();
int cost = Math.abs(cr[0] - cr[n - 1]);
HashMap<Character, ArrayList<Integer>> hm = new HashMap<>();
for (int i = 0; i < n; i++) {
char c = cr[i];
if (hm.containsKey(c)) {
hm.get(c).add(i);
} else {
ArrayList<Integer> temp = new ArrayList<>();
temp.add(i);
hm.put(c, temp);
}
}
int d = cr[0] > cr[n - 1] ? -1 : 1;
ArrayList<Integer> ans = new ArrayList<Integer>();
for (char a = cr[0]; a != cr[n - 1] + d; a += d) {
if (hm.containsKey(a)) {
for (Integer i : hm.get(a)) {
ans.add(i + 1);
}
}
}
System.out.println(cost + " " + ans.size());
for (Integer k : ans) {
System.out.print(k + " ");
}
System.out.println();
}
}
static int findNext(int s, ArrayList<Integer> st) {
int ind = -1;
int n = st.size();
for (int i = 0; i < n; i++) {
if (st.get(i) < s) {
ind = i;
break;
}
}
return ind;
}
static String reverseString(String str) {
char[] cr = str.toCharArray();
int l = 0;
int r = str.length() - 1;
while (r > l) {
char temp = cr[r];
cr[r] = cr[l];
cr[l] = temp;
r--;
l++;
}
return String.valueOf(cr);
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
// method to return LCM of two numbers
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static int checkOdd(long a) {
return a % 2 == 0 ? 0 : 1;
}
static long pow(long a, long b) {
return (long) Math.pow(a, b);
}
static long min(long a, long b) {
return a >= b ? b : a;
}
static long max(long a, long b) {
return a >= b ? a : b;
}
static int min(int a, int b) {
return a >= b ? b : a;
}
static int max(int a, int b) {
return a >= b ? a : b;
}
// Merge Sort Algorithm
static void mergeSort(long[] arr) {
sort(arr, 0, arr.length - 1);
}
static void sort(long[] arr, int l, int r) {
if (l < r) {
int mid = (l + r) / 2;
sort(arr, mid + 1, r);
sort(arr, l, mid);
merge(arr, l, r, mid);
}
}
static void merge(long[] arr, int l, int r, int mid) {
int i = l;
int j = mid + 1;
long[] temp = new long[r - l + 1];
int ind = 0;
while (i <= mid && j <= r) {
if (arr[i] <= arr[j]) {
// Ascending
temp[ind++] = arr[i++];
} else {
temp[ind++] = arr[j];
j++;
}
}
while (i <= mid)
temp[ind++] = arr[i++];
while (j <= r)
temp[ind++] = arr[j++];
// Actual merge step for the element
ind = 0;
for (int s = l; s <= r; s++) {
arr[s] = temp[ind++];
}
}
// Program to return maximum digit in a number
// using strings
static int maxInNumber(String s) {
int ans = -1;
for (char c : s.toCharArray()) {
ans = Math.max(ans, c - '0');
}
return ans;
}
// Program to return minimum digit in a number
// using strings
static int minInNumber(String s) {
int ans = 10;
for (char c : s.toCharArray()) {
ans = Math.min(ans, c - '0');
}
return ans;
}
// Binary search algorithm for array
// There is a built in binarySearch algorithm in Arrays class
static int binarySearch(int[] arr, int tgt) {
int l = 0;
int r = arr.length - 1;
int mid = l + (r - l) / 2;
;
while (l <= r) {
mid = l + ((r - l) / 2);
if (arr[mid] == tgt) {
return mid;
} else if (tgt < arr[mid]) {
r = mid - 1;
} else if (tgt > arr[mid]) {
l = mid + 1;
}
}
return -1;
}
static int digitCount(long a) {
return String.valueOf(a).length();
}
static long[] arrInpLong(FastReader sc, int size) {
if (size == 0) {
size = sc.nextInt();
}
long[] arr = new long[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextLong();
}
return arr;
}
static int[] arrInpInt(FastReader sc, int size) {
if (size == 0) {
size = sc.nextInt();
}
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
return arr;
}
// Sum of first n integers
static long sumOfNFirst(long n) {
return ((n) * (n + 1)) / 2;
}
// Check if a>=b
static boolean check(long have, long needed) {
return have >= needed;
}
static void printArray(long[] arr) {
for (long l : arr) {
System.out.print(l + " ");
}
}
// Sieve of Eratosthenes
// TC = O(n log log n)
// Sc = O(n)
// Works till n
static void sieve(int n) {
// All initialised as zeroes
boolean[] arr = new boolean[n + 1];
arr[0] = true;
arr[1] = true;
for (int i = 2; i * i <= n; i++) {
// marking of the multiples
if (arr[i] != true && (i * i) <= n) {
for (int k = i * i; k <= n; k += i) {
arr[k] = true;
}
}
}
// Printing out the primes
ArrayList<Integer> ans = new ArrayList<Integer>();
int size = 0;
for (int i = 2; i < n + 1; i++) {
if (arr[i] == false) {
ans.add(i);
}
}
for (int k = 0; k < ans.size(); k += 100) {
System.out.println(ans.get(k));
}
}
static int digitalSum(String s) {
int ans = 0;
for (char c : s.toCharArray()) {
ans += (c - 48);
}
return ans;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 53b88a11f6f741ccfea36eff658e0fda | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class C_Jumping_on_Tiles {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int t = Integer.parseInt(br.readLine());
while(t-- > 0) {
String s = br.readLine();
int cost = 0, m = 1;
if(s.length() == 2) {
sb.append(Math.abs(s.charAt(0) - s.charAt(1))).append(" ").append(2).append("\n");
sb.append("1 2\n");
continue;
}
int[] count = new int[27];
List<Integer>[] idxs = new ArrayList[27];
int len = s.length();
for(int i = 1; i <= 26; i++) {
idxs[i] = new ArrayList<>();
}
for(int i = 1; i < len; i++) {
int n = s.charAt(i) - 'a' + 1;
count[n]++;
if(i != len-1) idxs[n].add(i);
}
int start = s.charAt(0) - 'a' + 1;
int end = s.charAt(len-1) - 'a' + 1;
if(start == end) {
sb.append("0 ").append(count[start]+1).append("\n");
sb.append("1 ");
for(int idx: idxs[start]) {
sb.append(idx+1).append(" ");
}
sb.append(len).append("\n");
}
else {
List<Integer> indexes = new ArrayList<>();
int d = start > end ? -1 : 1;
int prev = start;
for(int i = start; i != end+d; i += d) {
if(count[i] != 0) {
indexes.add(i);
cost += Math.abs(prev - i);
m += count[i];
prev = i;
}
}
sb.append(cost).append(" ").append(m).append("\n");
sb.append(1).append(" ");
for(int index: indexes) {
for(int idx: idxs[index]) {
sb.append(idx+1).append(" ");
}
}
sb.append(len).append("\n");
}
}
System.out.println(sb);
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 1112b9413ef3e6c2698bad6963ea569a | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
public class jumpingTiles{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T>0){
String s = sc.next();
int x = s.charAt(0);
int y = s.charAt(s.length()-1);
int cost = Math.abs(x-y);
HashMap<Character,ArrayList<Integer>> map = new HashMap<>();
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(map.containsKey(c)==false){
map.put(c,new ArrayList<Integer>());
}
map.get(c).add(i+1);
}
// for(Map.Entry<Character,ArrayList<Integer>> entry : map.entrySet()){
// System.out.println(entry.getKey()+" "+entry.getValue());
// }
// char arr[] = new char[s.length()];
// for(int i=0;i<s.length();i++){
// arr[i] = s.charAt(i);
// }
// Arrays.sort(arr);
// for(int i=0;i<arr.length;i++){
// System.out.print(arr[i]+" ");
// }
ArrayList<Integer> ans = new ArrayList<>();
if(s.charAt(0)<=s.charAt(s.length()-1)){
for(char i=s.charAt(0);i<=s.charAt(s.length()-1);i++){
char c = (i);
if(map.containsKey(c)){
for(int j:map.get(c)){
ans.add(j);
}
}
}
}
else if(s.charAt(0)>=s.charAt(s.length()-1)){
for(char i= s.charAt(0);i>=s.charAt(s.length()-1);i--){
char c = i;
if(map.containsKey(c)){
for(int j:map.get(c)){
ans.add(j);
}
}
}
}
System.out.println(cost+" "+ans.size());
for(int k:ans){
System.out.print(k+" ");
}
System.out.println("");
T--;
}
sc.close();
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | b6aa54ada0c16b4a40cc0066c42612ba | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes |
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
/*
*
* @author Sabirov Jakhongir
*
*/
public class Test2 {
static class Move{
int value;
int position;
public Move(int value, int position) {
this.value = value;
this.position = position;
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
while (n -- > 0)
{
String s = cin.next();
int ans = Math.abs(position(s.charAt(0)) - position(s.charAt(s.length() - 1)));
int mn = Math.min(position(s.charAt(0)),position(s.charAt(s.length() - 1)));
int mx = Math.max(position(s.charAt(0)),position(s.charAt(s.length() - 1)));
List<Move> list = new ArrayList<>();
for(int i = 1 ; i < s.length() - 1 ;i ++)
{
int ps = position(s.charAt(i));
if(ps >= mn && ps <= mx){
list.add(new Move(ps,i + 1));
}
}
if(position(s.charAt(0)) > position(s.charAt(s.length() - 1))){
Collections.sort(list, new Comparator<Move>() {
@Override public int compare(Move o1, Move o2) {
return Integer.compare(o2.value,o1.value);
}
});
}else{
Collections.sort(list, new Comparator<Move>() {
@Override public int compare(Move o1, Move o2) {
return Integer.compare(o1.value,o2.value);
}
});
}
int cnt = 1;
int summa = 0;
int start = position(s.charAt(0));
List<Integer> answerList = new ArrayList<>();
answerList.add(1);
for (int i = 0 ; i < list.size() ; i ++) {
if(Math.abs(start - list.get(i).value) + summa <= ans)
{
summa += Math.abs(start - list.get(i).value);
start = list.get(i).value;
cnt ++;
answerList.add(list.get(i).position);
}
}
if(Math.abs(start - position(s.charAt(s.length() - 1))) + summa <= ans){
cnt ++;
summa += Math.abs(start - position(s.charAt(s.length() - 1)));
answerList.add(s.length());
}
System.out.println(summa +" " + cnt);
for (int i = 0; i < answerList.size(); i++) {
System.out.print(answerList.get(i) + " ");
}
System.out.println();
}
}
public static int position(char a){
return (a - 'a') + 1;
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 73e2bd98c9d6ef016d5e0d89b6500bb6 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes |
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
/*
*
* @author Sabirov Jakhongir
*
*/
public class Test2 {
static class Move{
int value;
int position;
public Move(int value, int position) {
this.value = value;
this.position = position;
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
while (n -- > 0)
{
String s = cin.next();
int ans = Math.abs(position(s.charAt(0)) - position(s.charAt(s.length() - 1)));
int mn = Math.min(position(s.charAt(0)),position(s.charAt(s.length() - 1)));
int mx = Math.max(position(s.charAt(0)),position(s.charAt(s.length() - 1)));
List<Move> list = new ArrayList<>();
for(int i = 1 ; i < s.length() - 1 ;i ++)
{
int ps = position(s.charAt(i));
if(ps >= mn && ps <= mx){
list.add(new Move(ps,i + 1));
}
}
if(position(s.charAt(0)) > position(s.charAt(s.length() - 1))){
Collections.sort(list, new Comparator<Move>() {
@Override public int compare(Move o1, Move o2) {
return Integer.compare(o2.value,o1.value);
}
});
}else{
Collections.sort(list, new Comparator<Move>() {
@Override public int compare(Move o1, Move o2) {
return Integer.compare(o1.value,o2.value);
}
});
}
int cnt = 1;
int summa = 0;
int start = position(s.charAt(0));
List<Integer> answerList = new ArrayList<>();
answerList.add(1);
for (int i = 0 ; i < list.size() ; i ++) {
if(Math.abs(start - list.get(i).value) + summa <= ans)
{
summa += Math.abs(start - list.get(i).value);
start = list.get(i).value;
cnt ++;
answerList.add(list.get(i).position);
}else{
break;
}
}
if(Math.abs(start - position(s.charAt(s.length() - 1))) + summa <= ans){
cnt ++;
summa += Math.abs(start - position(s.charAt(s.length() - 1)));
answerList.add(s.length());
}
System.out.println(summa +" " + cnt);
for (int i = 0; i < answerList.size(); i++) {
System.out.print(answerList.get(i) + " ");
}
System.out.println();
}
}
public static int position(char a){
return (a - 'a') + 1;
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | a3370ac5dc431cfd47e11ac07a6c3c5d | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class a {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
while (t-- > 0) {
char[] s = fs.next().toCharArray();
Map<Character, List<Integer>> map = new HashMap<>();
for (int i = 0; i < s.length; ++i) {
map.computeIfAbsent(s[i], x -> new ArrayList<>()).add(i+1);
}
char start = s[0] <= s[s.length-1] ? s[0] : s[s.length-1];
char end = s[0] <= s[s.length-1] ? s[s.length-1] : s[0];
List<Integer> res = new ArrayList<>();
while (start <= end) {
if (map.containsKey(start)) {
List<Integer> x = map.get(start);
if (s[0] > s[s.length-1]) {
Collections.reverse(x);
}
res.addAll(x);
}
start++;
}
if (s[0] > s[s.length-1]) {
Collections.reverse(res);
}
out.print(Math.abs(s[0] - s[s.length-1]));
out.print(" ");
out.println(res.size());
for (int x : res) {
out.print(x + " ");
}
out.println();
}
out.close();
}
private static char getChar(int i) {
return (char)('a' + i - 1);
}
static int lowerBound(List<Integer> a, int l, int r, int target) {
while (l < r) {
int mid = l + (r - l) / 2;
if (target > a.get(mid)) {
l = mid + 1;
} else {
r = mid;
}
}
return l;
}
static int upperBound(List<Integer> a, int l, int r, int target) {
while (l < r) {
int mid = l + (r - l) / 2;
if (a.get(mid) > target) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class 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\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | b1b573db8f7c3b39067e887c63ab4a54 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | /*
Author:-crazy_coder-
*/
import java.io.*;
import java.util.*;
public class cp{
static int ans=0;
static BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
static PrintWriter pw=new PrintWriter(System.out);
public static void main(String[] args)throws Exception{
int T=Integer.parseInt(br.readLine());
// int t=1;
while(T-->0){
solve();
}
pw.flush();
}
public static void solve()throws Exception{
// String[] str=br.readLine().split(" ");
// int n=Integer.parseInt(str[0]);
String str=br.readLine();
int n=str.length();
ArrayList<Pair> p=new ArrayList<>();
if(str.charAt(0)<=str.charAt(n-1)){
for(int i=0;i<n;i++){
if(str.charAt(i)>=str.charAt(0)&&str.charAt(i)<=str.charAt(n-1)){
p.add(new Pair(str.charAt(i),i+1));
}
}
Collections.sort(p);
}else{
for(int i=0;i<n;i++){
if(str.charAt(i)<=str.charAt(0)&&str.charAt(i)>=str.charAt(n-1)){
p.add(new Pair(str.charAt(i),i+1));
}
}
Collections.sort(p,Collections.reverseOrder());
}
pw.println(Math.abs(str.charAt(n-1)-str.charAt(0))+" "+p.size());
for(int i=0;i<p.size();i++){
pw.print(p.get(i).idx+" ");
}
pw.println();
}
public static class Pair implements Comparable<Pair>{
char ch;
int idx;
Pair(char ch,int idx){
this.ch=ch;
this.idx=idx;
}
public int compareTo(Pair o){
return this.ch-o.ch;
}
}
public static int countDigit(int x){
return (int)Math.log10(x)+1;
}
//****************************function to find all factor*************************************************
public static ArrayList<Long> findAllFactors(long num){
ArrayList<Long> factors = new ArrayList<Long>();
for(long i = 1; i <= num/i; ++i) {
if(num % i == 0) {
//if i is a factor, num/i is also a factor
factors.add(i);
factors.add(num/i);
}
}
//sort the factors
Collections.sort(factors);
return factors;
}
//*************************** function to find GCD of two number*******************************************
public static long gcd(long a,long b){
if(b==0)return a;
return gcd(b,a%b);
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | f41ec87d38a15e57ae136813d52794c8 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class A {
public static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
public static void print(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
public static String concat(String s1, String s2) {
return new StringBuilder(s1).append(s2).toString();
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static boolean isPrime(int n) {
if (n <= 1)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
for (int i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i == 0)
return false;
}
return true;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
/*
* logic 12 15 7 9 3
* 3 7 9 12 15
* codeforces 1 2 3 4 5 6 7 8 9 10 3 15 4 5 6 15
* 18 3 5 19 3 3 4 5 5 6 15 15 18 19 1 8 3 4 9 5 2 6 7 10 1 8 3 4 9 5 2 6 7 10
*/
int t1 = sc.nextInt();
outer: while (t1-- > 0) {
char[] c=sc.next().toCharArray();
int n=c.length;
int cost=Math.abs(c[0]-c[n-1]);
ArrayList<Integer> l=new ArrayList<>();
for(int i=1;i<n-1;i++){
if ((c[i] >= c[0] && c[i] <= c[n-1]) || (c[i] <= c[0] && c[i] >= c[n-1])) {
l.add(i);
}
}
if(c[0]<c[n-1]){
Collections.sort(l,(x,y)->c[x]-c[y]);
}
else{
Collections.sort(l,(x,y)->c[y]-c[x]);
}
int moves=l.size()+2;
out.println(cost+" "+moves);
out.print(1+" ");
for(int i=0;i<l.size();i++){
out.print((l.get(i)+1)+" ");
}
out.println(n);
}
out.close();
}
static class Pair implements Comparable<Pair> {
long first;
long second;
public Pair(long first, long second) {
this.first = first;
this.second = second;
}
public int compareTo(Pair p) {
if (first != p.first)
return Long.compare(first, p.first);
else if (second != p.second)
return Long.compare(second, p.second);
else
return 0;
}
}
static class Tuple implements Comparable<Tuple> {
long first;
long second;
long third;
public Tuple(long a, long b, long c) {
first = a;
second = b;
third = c;
}
public int compareTo(Tuple t) {
if (first != t.first)
return Long.compare(first, t.first);
else if (second != t.second)
return Long.compare(second, t.second);
else if (third != t.third)
return Long.compare(third, t.third);
else
return 0;
}
}
static final Random random = new Random();
static void shuffleSort(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int r = random.nextInt(n), temp = a[r];
a[r] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
long[] readArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | f28cb1edc52e9835fe2c0a6994319b42 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException
{
Scanner read = new Scanner(System.in);
int cs = read.nextInt();
read.nextLine();
for(int tc = 1; tc<=cs; tc++)
{
HashMap<Integer, ArrayList<Integer>> ind = new HashMap<>();
ArrayList<Integer> ans = new ArrayList<>();
char [] ch = read.nextLine().toCharArray();
int n = ch.length;
int [] arr = new int[n];
for(int i = 0; i<n; i++)
{
arr[i] = ch[i] - 96;
if(ind.containsKey(arr[i]))
{
ArrayList<Integer> temp = ind.get(arr[i]);
temp.add(i+1);
ind.put(arr[i], temp);
}
else
{
ArrayList<Integer> temp = new ArrayList<>();
temp.add(i+1);
ind.put(arr[i], temp);
}
}
int min = Math.min(arr[0], arr[n-1]);
int max = Math.max(arr[0], arr[n-1]);
int cost = max-min;
int min_ind = ind.get(min).get(0);
int max_ind = ind.get(max).get(0);
if(min_ind < max_ind)
{
for(int i = 1; i<=26; i++)
{
if(i >= min && i <= max && ind.containsKey(i))
{
for(int j = 0; j<ind.get(i).size(); j++)
{
ans.add(ind.get(i).get(j));
}
}
}
}
else
{
for(int i = 26; i>=1; i--)
{
if(i >= min && i <= max && ind.containsKey(i))
{
for(int j = 0; j<ind.get(i).size(); j++)
{
ans.add(ind.get(i).get(j));
}
}
}
}
System.out.println(cost+" "+ans.size());
for(int i = 0; i<ans.size(); i++)
{
System.out.print(ans.get(i)+" ");
}
System.out.println();
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | cfa04120239c36fd7e29827389131b08 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes |
// Working program using Reader Class
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.*;
import java.util.StringTokenizer;
// public class CodeForces_12Sept2022 {
// static class Reader {
// final private int BUFFER_SIZE = 1 << 16;
// private DataInputStream din;
// private byte[] buffer;
// private int bufferPointer, bytesRead;
// public Reader() {
// din = new DataInputStream(System.in);
// buffer = new byte[BUFFER_SIZE];
// bufferPointer = bytesRead = 0;
// }
// public Reader(String file_name) throws IOException {
// din = new DataInputStream(
// new FileInputStream(file_name));
// buffer = new byte[BUFFER_SIZE];
// bufferPointer = bytesRead = 0;
// }
// public String readLine() throws IOException {
// byte[] buf = new byte[64]; // line length
// int cnt = 0, c;
// while ((c = read()) != -1) {
// if (c == '\n') {
// if (cnt != 0) {
// break;
// } else {
// continue;
// }
// }
// buf[cnt++] = (byte) c;
// }
// return new String(buf, 0, cnt);
// }
// public int nextInt() throws IOException {
// int ret = 0;
// byte c = read();
// while (c <= ' ') {
// c = read();
// }
// boolean neg = (c == '-');
// if (neg)
// c = read();
// do {
// ret = ret * 10 + c - '0';
// } while ((c = read()) >= '0' && c <= '9');
// if (neg)
// return -ret;
// return ret;
// }
// public long nextLong() throws IOException {
// long ret = 0;
// byte c = read();
// while (c <= ' ')
// c = read();
// boolean neg = (c == '-');
// if (neg)
// c = read();
// do {
// ret = ret * 10 + c - '0';
// } while ((c = read()) >= '0' && c <= '9');
// if (neg)
// return -ret;
// return ret;
// }
// public double nextDouble() throws IOException {
// double ret = 0, div = 1;
// byte c = read();
// while (c <= ' ')
// c = read();
// boolean neg = (c == '-');
// if (neg)
// c = read();
// do {
// ret = ret * 10 + c - '0';
// } while ((c = read()) >= '0' && c <= '9');
// if (c == '.') {
// while ((c = read()) >= '0' && c <= '9') {
// ret += (c - '0') / (div *= 10);
// }
// }
// if (neg)
// return -ret;
// return ret;
// }
// private void fillBuffer() throws IOException {
// bytesRead = din.read(buffer, bufferPointer = 0,
// BUFFER_SIZE);
// if (bytesRead == -1)
// buffer[0] = -1;
// }
// private byte read() throws IOException {
// if (bufferPointer == bytesRead)
// fillBuffer();
// return buffer[bufferPointer++];
// }
// public void close() throws IOException {
// if (din == null)
// return;
// din.close();
// }
// }
// public static void main(String[] args)
// throws IOException {
// Reader s = new Reader();
// int T = s.nextInt();
// while (T-- > 0) {
// String t = s.readLine();
// List<Character> actual = new ArrayList<Character>();
// for (char ch : t.toCharArray()) {
// actual.add(ch);
// }
// List<Character> order = new ArrayList<Character>();
// for (char ch : t.toCharArray()) {
// order.add(ch);
// }
// Collections.sort(order);
// boolean[] visited = new boolean[order.size()];
// visited[0] = true;
// int start = order.indexOf(actual.get(0));
// int end = order.indexOf(actual.get(t.length() - 1));
// if (t.length() != 1 && start == end) {
// int k = start;
// while (k < t.length() && order.get(k + 1) == order.get(k))
// k++;
// end = k;
// }
// int cost = 0;
// int m = 1;
// String path = "1 ";
// if (start < end) {
// for (int j = start + 1; j <= end; j++) {
// cost += (((int) (order.get(j))) - 97 + 1) - (((int) (order.get(j - 1))) - 97
// + 1);
// m += 1;
// if (visited[actual.indexOf(order.get(j))]) {
// actual.set(actual.indexOf(order.get(j)), '0');
// }
// int o = actual.indexOf(order.get(j)) + 1;
// path += o;
// visited[actual.indexOf(order.get(j))] = true;
// path += " ";
// }
// } else {
// for (int j = start - 1; j >= end; j--) {
// cost += Math.abs((((int) (order.get(j))) - 97 + 1) - (((int) (order.get(j +
// 1))) - 97 + 1));
// m += 1;
// if (visited[actual.indexOf(order.get(j))]) {
// actual.set(actual.indexOf(order.get(j)), '0');
// }
// int o = actual.indexOf(order.get(j)) + 1;
// path += o;
// visited[actual.indexOf(order.get(j))] = true;
// path += " ";
// }
// }
// System.out.println(cost + " " + m);
// System.out.println(path.substring(0, path.length() - 1));
// }
// }
// // 9 4
// // 1 4 3 5
// // public static String whatsTheDecode() {
// // }
// }
public class CodeForces_12Sept2022 {
public static void main(String args[]) {
try (Scanner scan = new Scanner(System.in)) {
int t = scan.nextInt();
while (t-- > 0) {
String s = scan.next();
char a = s.charAt(0);
char b = s.charAt(s.length() - 1);
List<Integer> path = new ArrayList<>();
int cost = 0;
int jumps = 0;
char last = ' ';
if (a <= b) {
for (char i = a; i <= b; i++) {
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == i) {
path.add(j + 1);
jumps++;
if (last >= 'a' && last <= 'z')
cost += i - last;
last = i;
}
}
}
} else {
for (char i = a; i >= b; i--) { // loop from l->k->j->i->....->c
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == i) { // same character hai agar tho uspe jump
path.add(j + 1);
jumps++;
if (last >= 'a' && last <= 'z')
cost += last - i;
last = i;
}
}
}
}
System.out.println(cost + " " + jumps);
StringBuilder sb = new StringBuilder();
for (int i : path) {
sb.append(i).append(" ");
}
// if(flag == 1)
// sb.reverse();
System.out.println(sb.toString().trim());
}
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | f505382af557f2040eaebf77b61b7e71 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
public class Solution {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t-->0) {
String s = scan.next();
char a = s.charAt(0);
char b = s.charAt(s.length()-1);
List<Integer> path = new ArrayList<>();
int cost = 0;
int jumps = 0;
char last = ' ';
if(a <= b) {
for(char i=a; i<=b; i++) {
for(int j=0; j<s.length(); j++) {
if(s.charAt(j) == i) {
path.add(j+1);
jumps++;
if(last >= 'a' && last <= 'z')
cost += i-last;
last = i;
}
}
}
} else {
for(char i=a; i>=b; i--) {
for(int j=0; j<s.length(); j++) {
if(s.charAt(j) == i) {
path.add(j+1);
jumps++;
if(last >= 'a' && last <= 'z')
cost += last-i;
last = i;
}
}
}
}
System.out.println(cost + " " + jumps);
StringBuilder sb = new StringBuilder();
for(int i: path) {
sb.append(i).append(" ");
}
// if(flag == 1)
// sb.reverse();
System.out.println(sb.toString().trim());
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 5075fcadefc2aafe7553922b251c1554 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
public class C {
public static void solve(Scanner sc) {
String s = sc.nextLine();
int n = s.length();
HashMap<Character, ArrayList<Integer>> mp = new HashMap<>();
for(int i = 0;i < s.length(); i++)
{
char c = s.charAt(i);
if(!mp.containsKey(c)) {
mp.put(c,new ArrayList<Integer>());
}
mp.get(c).add(i);
}
ArrayList<Integer> ans = new ArrayList<Integer>();
if(s.charAt(0) < s.charAt(n-1)) {
for(char i = s.charAt(0); i <= s.charAt(n-1); i++) {
char c= (char)i;
if(mp.containsKey(c)) {
for(Integer j : mp.get(c)) {
ans.add(j);
}
}
}
}
else{
for(char i = s.charAt(0); i >= s.charAt(n-1); i--) {
char c = (char)i;
if(mp.containsKey(c)) {
for(Integer j : mp.get(c)) {
ans.add(j);
}
}
}
}
System.out.println(Math.abs(s.charAt(0)-s.charAt(n-1)) + " " + ans.size());
for(Integer i : ans) {
System.out.print((i+1) + " ");
}
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = Integer.parseInt(sc.nextLine());
while(t-->0) {
solve(sc);
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 7e65ea3ee23d3f83e0d3346094e97ea0 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
import java.io.PrintWriter;
public class C {
public static void solve(Scanner sc) {
PrintWriter writer = new PrintWriter(System.out);
String s = sc.nextLine();
int n = s.length();
HashMap<Character, ArrayList<Integer>> mp = new HashMap<>();
char start = s.charAt(0);
char end = s.charAt(n-1);
ArrayList<Integer> ans = new ArrayList<Integer>();
int count = 0;
if(s.charAt(0) < s.charAt(n-1)) {
for(int i = 0;i < s.length(); i++)
{
char c = s.charAt(i);
if(c >= start && c <= end) {
count++;
if(!mp.containsKey(c)) {
mp.put(c,new ArrayList<Integer>());
}
mp.get(c).add(i);
}
}
writer.write(Math.abs(s.charAt(0)-s.charAt(n-1)) + " " + count + "\n");
for(char i = s.charAt(0); i <= s.charAt(n-1); i++) {
char c= (char)i;
if(mp.containsKey(c)) {
for(Integer j : mp.get(c)) {
writer.write((j+1)+ " ");
}
}
}
writer.write("\n");
writer.flush();
}
else{
for(int i = 0;i < s.length(); i++)
{
char c = s.charAt(i);
if(c <= start && c >= end) {
count++;
if(!mp.containsKey(c)) {
mp.put(c,new ArrayList<Integer>());
}
mp.get(c).add(i);
}
}
writer.write(Math.abs(s.charAt(0)-s.charAt(n-1)) + " " + count + "\n");
for(char i = s.charAt(0); i >= s.charAt(n-1); i--) {
char c = (char)i;
if(mp.containsKey(c)) {
for(Integer j : mp.get(c)) {
writer.write((j+1) + " ");
}
}
}
writer.write("\n");
writer.flush();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = Integer.parseInt(sc.nextLine());
while(t-->0) {
solve(sc);
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | a0ac2ee5247c5af14dafe95fd8696cc7 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
static class Reader {
public BufferedReader br;
StringTokenizer st = new StringTokenizer("");
public Reader() {
this(System.in);
}
public Reader(InputStream input) {
br = new BufferedReader(new InputStreamReader(input));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
long nl() {
return Long.parseLong(next());
}
double nd() {
return Double.parseDouble(next());
}
String nextl() {
String str = "";
try {
str = br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
int[] arrin(long num) {
int n = (int) num;
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
long[] arrnl(long num) {
int n = (int) num;
long[] l = new long[n];
for (int i = 0; i < n; i++) l[i] = nl();
return l;
}
}
//<------------------------------------------------WRITER---------------------------->
static class Writer {
static PrintWriter out;
public Writer() {
this(System.out);
}
public Writer(OutputStream outs) {
out = new PrintWriter(outs);
}
public void pl(int i) {
out.println(i);
}
public void pl(long l) {
out.println(l);
}
public void pl(double d) {
out.println(d);
}
public void pl(String s) {
out.println(s);
}
public void p(int i) {
out.print(i);
}
public void p(long l) {
out.print(l);
}
public void p(double d) {
out.print(d);
}
public void p(String s) {
out.print(s);
}
public void p() {
out.println();
}
public void close() {
out.close();
}
}
//----------------------------------------------------------------------------------->
//--------------------------VARIABLES------------------------------------//
static Reader in = new Reader();
static OutputStream outputStream = System.out;
static Writer out = new Writer(outputStream);
static long lmax = Long.MAX_VALUE, lmin = Long.MIN_VALUE;
static int imax = Integer.MAX_VALUE, imin = Integer.MIN_VALUE;
static long mod = 1000000007;
//-----------------------------------------------------------------------//
//--------------------------Red_Hair-----------------------------------//
private static void Red_Hair() throws IOException {
String FILE = "RED";
try { FILE = System.getProperty("user.dir"); }
catch (Exception e) { }
if(new File(FILE).getName().equals("CP")) {
out = new Writer(new FileOutputStream("output.txt"));
in = new Reader(new FileInputStream("input.txt"));
}
}
//-----------------------------------------------------------------------//
public static void main(String[] args) throws IOException {
Red_Hair();
int t = in.ni();
while (t-- > 0)
solve();
out.close();
}
static void solve() throws IOException {
String str = in.nextl();
char[] s1 = str.toCharArray();
List<pr<Character,Integer>> list = new ArrayList<>();
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
for (int i = 0; i < s1.length; i++)
{
if (s1[i] != s1[0] && s1[i] != s1[s1.length - 1]) {
list.add(new pr<Character, Integer>(s1[i], i));
}
}
list.add(new pr<Character, Integer>(s1[0], 0));
list.add(new pr<Character, Integer>(s1[s1.length - 1], s1.length - 1));
for (int i = 0; i < s1.length; i++) {
if (s1[i] == s1[0]) {
list1.add(i);
} else if (s1[i] == s1[s1.length - 1]) {
list2.add(i);
}
}
Collections.sort(list);
int vv1=0;
int ee2=0;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).b == 0) {
vv1 = i;
}
if (list.get(i).b == (s1.length - 1)) {
ee2 = i;
}
}
long mmt = (list1.size() + list2.size() + Math.abs(vv1 - ee2) - 1);
out.pl(Math.abs(s1[0] - s1[s1.length - 1]) + " " + mmt);
for (int i = 0; i < list1.size(); i++) {
out.p((list1.get(i) + 1) + " ");
}
if (vv1 < ee2) {
for (int i = vv1 + 1; i < ee2; i++) {
out.p((list.get(i).b + 1) + " ");
}
} else {
for (int i = vv1 - 1; i > ee2; i--) {
out.p(list.get(i).b + 1 + " ");
}
}
for (int i = 0; i < list2.size(); i++) {
out.p((list2.get(i) + 1 + " "));
}
out.pl("");
}
static class pr <T extends Comparable<T>, V extends Comparable<V>> implements Comparable<pr<T, V>> {
T a;
V b;
public pr(T a, V b) {
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof pr)) return false;
pr<?, ?> pr = (pr<?, ?>) o;
return a.equals(pr.a) && b.equals(pr.b);
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
@Override
public int compareTo(pr o) {
return this.a.compareTo(((T) o.a));
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 413bf21af83134e2dc4c494ebc8cf369 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
public class Sept12{
static long mod = 1000000007L;
static MyScanner sc = new MyScanner();
static int dp[][];
static void solve() {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if(a<(Math.abs(c-b)+c)){
out.println(1);
}else if(a>Math.abs(c-b)+c){
out.println(2);
}else out.println(3);
}
static void solve2(){
int n = sc.nextInt();
String str = sc.nextLine();
ArrayList<Character> ans = new ArrayList<>();
for(int i = 0;i<n;i++){
if(i+3<n && str.charAt(i+3)=='0')
ans.add((char)('a'+Integer.parseInt(str.substring(i,i+1))-1));
else if(i+2<n && str.charAt(i+2)=='0'){
ans.add((char)('a'+Integer.parseInt(str.substring(i,i+2))-1));
i+=2;
}else ans.add((char)('a'+Integer.parseInt(str.substring(i,i+1))-1));
}
for(char c : ans) out.print(c);
out.println();
}
static void solve3() {
String str = sc.nextLine();
int n = str.length();
ArrayList<Pas> ans = new ArrayList<>();
char a = str.charAt(0);
char b = str.charAt(n-1);
if(a>b){
char temp = a;
a = b;
b = temp;
}
for(int i = 1;i<n-1;i++){
if(str.charAt(i)>=a && str.charAt(i)<=b) ans.add(new Pas(str.charAt(i),i+1));
}
Collections.sort(ans);
out.println((b-a)+" "+(ans.size()+2));
Collections.sort(ans);
out.print(1+" ");
if(str.charAt(0)>str.charAt(n-1)){
for(int i = ans.size()-1;i>=0;i--) out.print(ans.get(i).ind+" ");
}else{
for(int i = 0;i<ans.size();i++) out.print(ans.get(i).ind+" ");
}
out.print(n);
out.println();
}
static class Pas implements Comparable<Pas>{
char num;
int ind;
Pas(char n,int i){
num = n;
ind = i;
}
public int compareTo(Pas p){
return this.num - p.num;
}
}
static int size(int n){
int temp = n;
int ans =0;
while(temp>0){
temp/=10;
ans++;
}
return ans;
}
static void solve4(){
}
static void solve5(){
}
static void solve6(){
}
static void solve7(){
}
static void reverse(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--;
}
}
static void swap(char arr[][],int i,int j){
for(int k = j;k>0;k--){
if(arr[i][k]=='.'&& arr[i][k-1]=='*'){
char temp = arr[i][k];
arr[i][k] = arr[i][k-1];
arr[i][k-1] = temp;
}
}
}
static int search(int pre,int suf[],int i,int j){
while(i<=j){
int mid = (i+j)/2;
if(suf[mid]==pre) return mid;
else if(suf[mid]<pre) j = mid-1;
else i = mid+1;
}
return Integer.MIN_VALUE;
}
static long pow(long a, long b) {
if (b == 0) return 1;
long res = pow(a, b / 2);
res = (res * res) % 1_000_000_007;
if (b % 2 == 1) {
res = (res * a) % 1_000_000_007;
}
return res;
}
static int lis(int arr[],int n){
int lis[] = new int[n];
lis[0] = 1;
for(int i = 1;i<n;i++){
lis[i] = 1;
for(int j = 0;j<i;j++){
if(arr[i]>arr[j]){
lis[i] = Math.max(lis[i],lis[j]+1);
}
}
}
int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
max = Math.max(lis[i],max);
}
return max;
}
static boolean isPali(String str){
int i = 0;
int j = str.length()-1;
while(i<j){
if(str.charAt(i)!=str.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
static long gcd(long a,long b){
if(b==0) return a;
return gcd(b,a%b);
}
static String reverse(String str){
char arr[] = str.toCharArray();
int i = 0;
int j = arr.length-1;
while(i<j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
String st = new String(arr);
return st;
}
static boolean isprime(int n){
if(n==1) return false;
if(n==3 || n==2) return true;
if(n%2==0 || n%3==0) return false;
for(int i = 5;i*i<=n;i+= 6){
if(n%i== 0 || n%(i+2)==0){
return false;
}
}
return true;
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
// int t= 1;
while(t-- >0){
// solve();
// solve2();
solve3();
// solve4();
// solve5();
// solve6();
// solve7();
// solve8();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 9fa114949a393234c0a09d4953cc19a5 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long t = scanner.nextLong();
for (int i = 0; i < t; i++) {
String s = scanner.next();
int n = s.length();
Map<Integer, List<Integer>> positions = new HashMap<>();
int first = s.charAt(0) - 'a';
int last = s.charAt(s.length() - 1) - 'a';
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++) {
int pos = s.charAt(j) - 'a';
if (positions.containsKey(pos)) {
positions.get(pos).add(j + 1);
} else {
positions.put(pos, new LinkedList<>(Collections.singletonList(j + 1)));
}
}
long sum = 0;
boolean firstSmallerThanLast = first <= last;
boolean firstEntry = true;
for (int k = first; ; ) {
List<Integer> indices = positions.getOrDefault(k, Collections.emptyList());
sum += indices.size();
for (Integer index : indices) {
if (firstEntry) {
sb.append(index);
firstEntry = false;
} else {
sb.append(" ").append(index);
}
}
if (firstSmallerThanLast) {
k += 1;
} else {
k -= 1;
}
if (k < Math.min(first, last) || k > Math.max(first, last)) {
break;
}
}
System.out.println(String.format("%s %s", Math.abs(last-first), sum));
System.out.println(sb);
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | dbfed4c054ac982f065951650fca6443 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes |
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Practice1 {
public static class UnionFind {
private final int[] p;
public UnionFind(int n) {
p = new int[n+1];
for(int i=1;i<=n;i++) {
p[i]=i;
}
}
public int find(int x) {
return x == 0 ? x : (p[x] = find(p[x]));
}
public void union(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
p[x] = y;
}
}
}
public static void printarr(int[] arr) {
int n=arr.length;
for(int i=0;i<n;i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
// /** Code for Dijkstra's algorithm **/
public static class ListNode {
int vertex, weight;
ListNode(int v,int w) {
vertex = v;
weight = w;
}
int getVertex() { return vertex; }
int getWeight() { return weight; }
}
public static int[] dijkstra(
int V, HashMap<Integer,ArrayList<ListNode> > graph,
int source) {
int[] distance = new int[V];
for (int i = 0; i < V; i++)
distance[i] = Integer.MAX_VALUE;
distance[0] = 0;
PriorityQueue<ListNode> pq = new PriorityQueue<>(
(v1, v2) -> v1.getWeight() - v2.getWeight());
pq.add(new ListNode(source, 0));
while (pq.size() > 0) {
ListNode current = pq.poll();
for (ListNode n :
graph.get(current.getVertex())) {
if (distance[current.getVertex()]
+ n.getWeight()
< distance[n.getVertex()]) {
distance[n.getVertex()]
= n.getWeight()
+ distance[current.getVertex()];
pq.add(new ListNode(
n.getVertex(),
distance[n.getVertex()]));
}
}
}
// If you want to calculate distance from source to
// a particular target, you can return
// distance[target]
return distance;
}
//Methos to return all divisor of a number
static ArrayList<Long> allDivisors(long n) {
ArrayList<Long> al=new ArrayList<>();
long i=2;
while(i*i<=n) {
if(n%i==0) al.add(i);
if(n%i==0&&i*i!=n) al.add(n/i);
i++;
}
return al;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long power(long N,long R)
{
long x=998244353;
if(R==0) return 1;
if(R==1) return N;
long temp= power(N,R/2)%x; // (a*b)%p = (a%p*b%p)*p
temp=(temp*temp)%x;
if(R%2==0){
return temp%x;
}else{
return (N*temp)%x;
}
}
static int[] sort(int[] arr) {
int n=arr.length;
ArrayList<Integer> al=new ArrayList<>();
for(int i=0;i<n;i++) {
al.add(arr[i]);
}
Collections.sort(al);
for(int i=0;i<n;i++) {
arr[i]=al.get(i);
}
return arr;
}
static int[] sortrev(int[] arr) {
int n=arr.length;
ArrayList<Integer> al=new ArrayList<>();
for(int i=0;i<n;i++) {
al.add(arr[i]);
}
Collections.sort(al,Collections.reverseOrder());
for(int i=0;i<n;i++) {
arr[i]=al.get(i);
}
return arr;
}
static long[] sort(long[] arr) {
long n=arr.length;
ArrayList<Long> al=new ArrayList<>();
for(int i=0;i<n;i++) {
al.add(arr[i]);
}
Collections.sort(al);
for(int i=0;i<n;i++) {
arr[i]=al.get(i);
}
return arr;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// method to return LCM of two numbers
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static ArrayList<Integer> allDivisors(int n) {
ArrayList<Integer> al=new ArrayList<>();
int i=2;
while(i*i<=n) {
if(n%i==0) al.add(i);
if(n%i==0&&i*i!=n) al.add(n/i);
i++;
}
return al;
}
static boolean isPrime(int n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
int sqrtN = (int)Math.sqrt(n)+1;
for(int i = 6; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
public static boolean check(String str,int ind,String tar) {
int n1=str.length();
int n2=tar.length();
if(n1-ind<n2) return false;
for(int i=0;i<n2;i++) {
if(str.charAt(ind)!=tar.charAt(i)) return false;
ind++;
}
return true;
}
public static void main (String[] args) {
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
// out.print();
//out.println();
FastReader sc=new FastReader();
int t=sc.nextInt();
while(t-->0) {
String str=sc.nextLine();
int n=str.length();
TreeMap<Integer,Integer> map=new TreeMap<>();
HashMap<Integer,ArrayList<Integer>> map1=new HashMap<>();
for(int i=0;i<n;i++) {
int a= (int)(str.charAt(i)-'a')+1;
map.put(a,map.getOrDefault(a,0)+1);
if(map1.containsKey(a)) {
map1.get(a).add(i+1);
}else {
ArrayList<Integer> li=new ArrayList<>();
li.add(i+1);
map1.put(a,li);
}
}
int min= (int)(str.charAt(0)-'a')+1;;
int max=(int)(str.charAt(n-1)-'a')+1;
int count=0,cost=Math.abs(min-max);
StringBuffer ans=new StringBuffer();
ArrayList<Integer> al=new ArrayList<>();
// out.println("min : "+min+" max : "+max);
// out.println("map : "+map);
if(min==max) {
al.add(min);
}else {
for(Map.Entry<Integer,Integer> m: map.entrySet()) {
int key=m.getKey();
//out.println("key : "+key+" val : "+val);
if(key>=Math.min(min,max)&&key<=Math.max(min,max)) {
al.add(key);
}
}
}
//out.println("al : "+al);
if(min<=max) Collections.sort(al);
else Collections.sort(al,Collections.reverseOrder());
for(Integer i: al) {
for(Integer j: map1.get(i)) {
ans.append(j+" ");
count++;
}
}
out.println(cost+" "+count);
out.println(ans);
}
out.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\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 81732ee53c2e24e65fd9ffd8e9e6d171 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static Comparator<int[]> cmp1 = new Comparator<int[]>() {
@Override
public int compare(int[] o1,int[] o2) {
return Integer.compare(o1[1], o2[1]);
}
};
static Comparator<int[]> cmp2 = new Comparator<int[]>() {
@Override
public int compare(int[] o1,int[] o2) {
return -Integer.compare(o1[1], o2[1]);
}
};
public static void main(String[] args) throws IOException {
Reader read = new Reader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int t = read.nextInt();
read.nextLine();
while(t-->0) {
char[] c = read.nextLine().toCharArray();
int[][] ans = new int[c.length+10][2];
int cnt = 0;
int flag=0;
if(c[0]>c[c.length-1])
flag = 1;
char c1 = (char)Math.max(c[0], c[c.length-1]),c2 = (char)Math.min(c[0], c[c.length-1]);
ans[++cnt][0] = 1;
ans[cnt][1] = c[0]-'a';
for(int i=1;i<c.length-1;i++) {
if(c[i]>=c2&&c[i]<=c1) {
ans[++cnt][0] = i+1;
ans[cnt][1] = c[i]-'a';
}
}
ans[++cnt][0] = c.length;
ans[cnt][1] = c[c.length-1];
if(flag==1)
Arrays.sort(ans,2,cnt,cmp2);
else
Arrays.sort(ans,2,cnt,cmp1);
out.println((c1-c2)+" "+cnt);
for(int i=1;i<=cnt;i++)
out.print(ans[i][0]+" ");
out.println();
}
out.flush();
}
}
class Reader{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer st = new StreamTokenizer(bf);
public int nextInt()throws IOException{
st.nextToken();
return (int)st.nval;
}
public double nextDouble()throws IOException{
st.nextToken();
return st.nval;
}
public String nextLine()throws IOException{
return bf.readLine();
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | a75f7499067bfa86e0dbd04e0df658fe | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.StringTokenizer;
public class C {
static final long mod = (long) 1e9 + 7l;
private static void solve(int t) {
char chr[] = fs.next().toCharArray();
int n = chr.length;
Map<Character, List<Integer>> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.putIfAbsent(chr[i], new ArrayList<>());
map.get(chr[i]).add(i+1);
}
char ss = chr[0];
char ee = chr[n-1];
int bbb = 0;
char prev = ss;
List<Integer> aaa = new ArrayList<>();
aaa.addAll(map.get(ss));
if(ss<ee){
ss++;
while(ss<=ee){
List<Integer> curr = map.get(ss);
if(curr!=null &&!curr.isEmpty()){
aaa.addAll(curr);
bbb+=Math.abs(ss-prev);
prev=ss;
}
ss++;
}
}else{
ss--;
while(ss>=ee){
List<Integer> curr = map.get(ss);
if(curr!=null &&!curr.isEmpty()){
aaa.addAll(curr);
bbb+=Math.abs(ss-prev);
prev=ss;
}
ss--;
}
}
out.println(bbb +" "+aaa.size());
for (int val:aaa) {
out.print(val+" ");
}
out.println();
}
static class Pair implements Comparable<Pair> {
int a;
int b;
Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair p) {
// if (a != p.a)
return a - p.a;
// return b - p.b;
}
@Override
public String toString() {
return "Pair{" +
"a=" + a +
", b=" + b +
'}';
}
}
private static int[] sortByCollections(int[] arr) {
ArrayList<Integer> ls = new ArrayList<>(arr.length);
for (int i = 0; i < arr.length; i++) {
ls.add(arr[i]);
}
Collections.sort(ls);
for (int i = 0; i < arr.length; i++) {
arr[i] = ls.get(i);
}
return arr;
}
public static void main(String[] args) {
fs = new FastScanner();
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
int t = fs.nextInt();
for (int i = 1; i <= t; i++) solve(t);
out.close();
// System.err.println( System.currentTimeMillis() - s + "ms" );
}
static boolean DEBUG = true;
static PrintWriter out;
static FastScanner fs;
static void trace(Object... o) {
if (!DEBUG) return;
System.err.println(Arrays.deepToString(o));
}
static void pl(Object o) {
out.println(o);
}
static void p(Object o) {
out.print(o);
}
static long gcd(long a, long b) {
return (b == 0) ? a : gcd(b, a % b);
}
static int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
static void sieveOfEratosthenes(int n, int factors[]) {
factors[1] = 1;
for (int p = 2; p * p <= n; p++) {
if (factors[p] == 0) {
factors[p] = p;
for (int i = p * p; i <= n; i += p)
factors[i] = p;
}
}
}
static long mul(long a, long b) {
return a * b % mod;
}
static long fact(int x) {
long ans = 1;
for (int i = 2; i <= x; i++) ans = mul(ans, i);
return ans;
}
static long fastPow(long base, long exp) {
if (exp == 0) return 1;
long half = fastPow(base, exp / 2);
if (exp % 2 == 0) return mul(half, half);
return mul(half, mul(half, base));
}
static long modInv(long x) {
return fastPow(x, mod - 2);
}
static long nCk(int n, int k) {
return mul(fact(n), mul(modInv(fact(k)), modInv(fact(n - k))));
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static class _Scanner {
InputStream is;
_Scanner(InputStream is) {
this.is = is;
}
byte[] bb = new byte[1 << 15];
int k, l;
byte getc() throws IOException {
if (k >= l) {
k = 0;
l = is.read(bb);
if (l < 0) return -1;
}
return bb[k++];
}
byte skip() throws IOException {
byte b;
while ((b = getc()) <= 32)
;
return b;
}
int nextInt() throws IOException {
int n = 0;
for (byte b = skip(); b > 32; b = getc())
n = n * 10 + b - '0';
return n;
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | a6e00b02db93de224565d17cca4bf543 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
public class JumpingOnTiles {
public static void main(String[] args) {
InputReader ir = new InputReader(System.in);
StringBuilder sb = new StringBuilder();
int t = ir.nextInt();
while (t > 0) {
String s = ir.next();
HashMap<Character, ArrayList<Integer>> map = new HashMap<>();
ArrayList<Character> list = new ArrayList<>();
char start = s.charAt(0);
char end = s.charAt(s.length() - 1);
int i = 1;
for (char c : s.toCharArray()) {
if (!map.containsKey(c)) {
map.put(c, new ArrayList<>());
list.add(c);
}
map.get(c).add(i);
i++;
}
Collections.sort(list);
int idx = 0;
for (Character c : list) {
if (c == start) break;
idx++;
}
StringBuilder temp = new StringBuilder();
long cost = 0;
int visited = 0;
char last = start;
if (start < end) {
while (idx < list.size() && list.get(idx) <= end) {
cost += Math.abs(last - list.get(idx));
ArrayList<Integer> idxs = map.get(list.get(idx));
for (Integer v : idxs) {
temp.append(v).append(' ');
visited++;
}
last = list.get(idx);
idx++;
}
} else {
while (idx >= 0 && list.get(idx) >= end) {
cost += Math.abs(last - list.get(idx));
ArrayList<Integer> idxs = map.get(list.get(idx));
for (Integer v : idxs) {
temp.append(v).append(' ');
visited++;
}
last = list.get(idx);
idx--;
}
}
sb.append(cost).append(' ').append(visited).append("\n");
sb.append(temp).append("\n");
t--;
}
System.out.print(sb);
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | a93792b10a43863c90db5d1107fba4ef | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | //package kg.my_algorithms.Codeforces;
/*
If you can't Calculate, then Stimulate
*/
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
FastReader fr = new FastReader();
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int testCases = fr.nextInt();
for(int test=1;test<=testCases;test++){
String s = fr.next();
int[] arr = new int[s.length()];
for(int i=0;i<s.length();i++){
arr[i] = s.charAt(i)-'a';
}
int f = arr[0];
int sec = arr[s.length()-1];
Queue<Pair> queue = new PriorityQueue<>(Comparator.comparingInt(a -> a.value));
for(int i=1;i<s.length()-1;i++){
queue.offer(new Pair(i+1,arr[i]));
}
List<Integer> index = new ArrayList<>();
while(!queue.isEmpty()){
Pair p = queue.remove();
if(p.value>=Math.min(arr[0],arr[s.length()-1] ) && p.value<=Math.max(arr[0],arr[s.length()-1] )) index.add(p.index);
}
sb.append(Math.abs(f-sec)).append(" ").append(index.size()+2).append("\n");
sb.append("1 ");
if(f<=sec){
for(int i: index) sb.append(i+ " ");
}
else {
for(int i=index.size()-1;i>=0;i--) sb.append(index.get(i)+" ");
}
sb.append(s.length()).append("\n");
}
output.write(sb.toString());
output.flush();
}
}
class Pair{
int index;
int value;
public Pair(int index, int value) {
this.index = index;
this.value = value;
}
}
//Fast Input
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\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 7c6b16d4ddcb5c55256ce6f6c4d17353 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
import java.io.*;
public class _1729C {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
String s=sc.next();
int minCost=Math.abs(s.charAt(s.length()-1)-s.charAt(0));
Vector<Integer> jumps=new Vector<>();
char start=s.charAt(0);
char end=s.charAt(s.length()-1);
jumps.add(1);
Pair []arr=new Pair[s.length()-2];
for(int i=1;i<s.length()-1;i++) {
arr[i - 1] = new Pair( s.charAt(i), i + 1);
arr[i-1].ch=s.charAt(i);
arr[i-1].idx=(i+1);
}
if(start<end)
Arrays.sort(arr,(a,b)->(a.ch-b.ch));
else
Arrays.sort(arr,(a,b)->(b.ch-a.ch));
for(int i=0;i<arr.length;i++){
// System.out.println((char)arr[i].ch +" yes");
if((char)(arr[i].ch)>=Math.min(start,end) && (char)(arr[i].ch)<=Math.max(start,end))
jumps.add(arr[i].idx);
}
jumps.add(s.length());
System.out.println(minCost+ " "+jumps.size());
for(Integer i : jumps)
System.out.print(i+" ");
System.out.println();
}
}
static class Pair{
int ch='0';
int idx=0;
Pair(int ch,int idx){
ch=this.ch;
idx=this.idx;
}
}
public static int gcd(int a, int b) {
return (a == 0) ? b : gcd(b % a, a);
}
static int power(int x, int y) {
int ans = 1;
while (y > 0) {
if ((y & 1) != 0)
ans *= x;
y = y >> 1;
x *= x;
}
return ans;
}
public static List<Integer> sieve(int n) {
List<Integer> primes = new LinkedList<>();
boolean[] composite = new boolean[n + 1];
for (int i = 2; i * i <= n; i++)
for (int j = i * i; j <= n && !composite[i]; j += i)
composite[j] = true;
for (int j = 2; j < composite.length; j++)
if (!composite[j]) primes.add(j);
return primes;
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | ba1e2c9f5a307876d3a4dd6b9db4a6d5 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
import java.io.*;
public class C{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0){
String S=s.next();
char c[]=S.toCharArray();
int n=c.length;
int cost=Math.abs(c[0]-c[n-1]);
int A[]=new int[n];
for(int i=0;i<n;i++){
A[i]=(int)c[i]-96;
}
int st=(int)c[0]-96;
int end=(int)c[n-1]-96;
if(st==end){
int moves=2;
ArrayList<Integer> arr=new ArrayList<>();
arr.add(1);
for(int i=1;i<n-1;i++){
if(A[i]==st){
moves++;
arr.add(i+1);
}
}
arr.add(n);
System.out.println(cost+" "+moves);
for(int a:arr)System.out.print(a+" ");
System.out.println();
}
else{
if(st<end){
int moves=2;
ArrayList<Integer> arr=new ArrayList<>();
arr.add(1);
PriorityQueue<Pair> pq=new PriorityQueue<>((x,y)-> x.first-y.first);
for(int i=1;i<n-1;i++){
if(A[i]<=end && A[i]>=st)pq.add(new Pair(A[i],i+1));
}
while(!pq.isEmpty()){
moves++;
arr.add(pq.poll().second);
}
arr.add(n);
System.out.println(cost+" "+moves);
for(int a:arr)System.out.print(a+" ");
System.out.println();
}
else{
int moves=2;
ArrayList<Integer> arr=new ArrayList<>();
arr.add(1);
PriorityQueue<Pair> pq=new PriorityQueue<>((x,y)-> y.first-x.first);
for(int i=1;i<n-1;i++){
if(A[i]<=st&&A[i]>=end)pq.add(new Pair(A[i],i+1));
}
while(!pq.isEmpty()){
moves++;
arr.add(pq.poll().second);
}
arr.add(n);
System.out.println(cost+" "+moves);
for(int a:arr)System.out.print(a+" ");
System.out.println();
}
}
}
}
}
class god{
}
class Pair{
int first;
int second;
Pair(int first,int second){
this.first=first;
this.second=second;
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | fb6f5f5062cdd375be9fab924d3d1175 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Solution {
static MyScanner str = new MyScanner();
public static void main(String[] args) throws IOException {
int T = i();
while (T-- > 0) {
solve();
}
}
static void solve() throws IOException {
String s = str.next();
char a = s.charAt(0), b = s.charAt(s.length() - 1);
long cost = Math.abs(b - a), m = 0;
StringBuffer[] sf = new StringBuffer[26];
for (int i = 0; i < 26; i++) {
sf[i] = new StringBuffer();
}
char[] c = s.toCharArray();
for (int i = 0; i < s.length(); i++) {
if ((c[i] >= b && c[i] <= a) || (c[i] <= b && c[i] >= a)) {
sf[c[i] - 'a'].append((i + 1) + " ");
m++;
}
}
StringBuffer res = new StringBuffer();
if(a <= b) {
for (int i = a - 'a'; i <= b - 'a'; i++) {
res.append(sf[i]);
}
} else {
for(int i = a - 'a'; i >= b - 'a'; i--) {
res.append(sf[i]);
}
}
System.out.println(cost + " " + m);
System.out.println(res);
}
public static void out_arr(int[] a) {
StringBuffer sf = new StringBuffer();
for (int i = 0; i < a.length; i++) {
sf.append(a[i] + " ");
}
System.out.println(sf);
}
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 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 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\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | a19ce99ee0b6d9d6f98ef3223b29dd2f | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
int tc = sc.nextInt();
while(tc-->0) {
char[]a = sc.next().toCharArray();
ArrayList<Integer>[]c = new ArrayList[26];
for (int i = 0; i < c.length; i++) {
c[i] = new ArrayList<Integer>();
}
for (int i = 0; i < a.length; i++) {
c[a[i]-'a'].add(i+1);
}
if(a[0]==a[a.length-1]) {
ArrayList<Integer>l = c[a[0]-'a'];
pw.println("0 "+l.size());
pw.print(1+" ");
for(int e:l) {
if(e!=1&&e!=a.length) {
pw.print(e+" ");
}
}
pw.println(a.length);
}else {
int cost = 0;
int prev = a[0] - 'a';
int m = 0;
// pw.println(c[2]);
for (int i = a[0]-'a'; i+'a'+(a[a.length-1]>a[0]?-1:1) != a[a.length-1]; i+=a[a.length-1]>a[0]?1:-1) {
if(c[i].size()!=0) {
cost+= Math.abs(i - prev);
prev = i;
// pw.println(c[i].size());
// pw.println((char)(i+'a'));
m+=c[i].size();
}
}
cost+=Math.abs(a[a.length-1]-'a' - prev);
pw.println(cost+" "+m);
pw.print(1+" ");
for (int i = a[0] - 'a'; i+'a'+(a[a.length-1]>a[0]?-1:1) != a[a.length-1]; i+=a[a.length-1]>a[0]?1:-1) {
if(c[i].size()!=0) {
for(int e:c[i]) {
if(e!=1&&e!=a.length) {
pw.print(e+" ");
}
}
}
}
pw.println(a.length);
}
}
pw.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws FileNotFoundException {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public int[] nextArrint(int size) throws IOException {
int[] a = new int[size];
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
return a;
}
public long[] nextArrlong(int size) throws IOException {
long[] a = new long[size];
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextLong();
}
return a;
}
public int[][] next2dArrint(int rows, int columns) throws IOException {
int[][] a = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
a[i][j] = sc.nextInt();
}
}
return a;
}
public long[][] next2dArrlong(int rows, int columns) throws IOException {
long[][] a = new long[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
a[i][j] = sc.nextLong();
}
}
return a;
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 4898cb1b599b5eb17a3a7bc73e42bcda | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
import java.io.*;
public class myclass
{
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());
}
}
public static void main(String args[])
{
FastReader sc = new FastReader();
int t= sc.nextInt();
while(t-->0)
{
StringBuilder ans = new StringBuilder("");
String s = sc.next();
int n = s.length();
if(s.charAt(0) >= s.charAt(n-1))
{
PriorityQueue<int[]> pq = new PriorityQueue<>((a , b)
-> b[0]!=a[0] ? b[0]-a[0] : a[1]-b[1]);
for(int i=0 ; i<n ; i++)
{
pq.add(new int[]{(int)s.charAt(i) - 96 , i+1});
}
while(!pq.isEmpty() && pq.peek()[0] > (int)s.charAt(0) - 96)
pq.poll();
int count = 0;
while(!pq.isEmpty() && pq.peek()[0] >= (int)s.charAt(n-1) - 96)
{
ans.append(pq.poll()[1]+" ");
count++;
}
System.out.println((int)(s.charAt(0)-s.charAt(n-1)) + " " + count);;
System.out.println(ans);
}
else
{
PriorityQueue<int[]> pq = new PriorityQueue<>((a , b)
-> b[0]!=a[0] ? a[0]-b[0] : a[1]-b[1]);
for(int i=0 ; i<n ; i++)
{
pq.add(new int[]{(int)s.charAt(i) - 96 , i+1});
}
while(!pq.isEmpty() && pq.peek()[0] < (int)s.charAt(0) - 96)
pq.poll();
int count = 0;
while(!pq.isEmpty() && pq.peek()[0] <= (int)s.charAt(n-1) - 96)
{
ans.append(pq.poll()[1]+" ");
count++;
}
System.out.println((int)(s.charAt(n-1)-s.charAt(0)) + " " + count);;
System.out.println(ans);
}
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 6df62ac03cd221c61fac11dfad1b78ee | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
public class A {
static int mod = (int) (1e9 + 7);
public static void main(String[] args) throws IOException {
// Scanner sc = new Scanner(new File("second_hands_input.txt"));
// PrintWriter pw = new PrintWriter("second_hands_output.txt");
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
char[] a = sc.next().toCharArray();
int min = Math.min(a[0],a[a.length-1]);
int max = Math.max(a[0],a[a.length-1]);
int cnt = 0;
ArrayList<pair> arr = new ArrayList<>();
for (int i = 1; i < a.length-1; i++) {
if(a[i]>=min && a[i] <= max) {
cnt++;
arr.add(new pair(i,a[i]));
}
}
pw.println((max-min)+" "+(cnt+2));
Collections.sort(arr);
if(a[0]>a[a.length-1]){
Collections.reverse(arr);
}
pw.print("1 ");
for (pair p : arr) {
pw.print((p.x+1)+" ");
}
pw.print((a.length)+" ");
pw.println();
}
pw.flush();
}
static class pair implements Comparable<pair> {
int x, f;
char y;
public pair(int x, char y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
@Override
public int compareTo(pair o) {
return y-o.y;
}
}
static long modPow(long a, long e, int mod) // O(log e)
{
a %= mod;
long res = 1;
while (e > 0) {
if ((e & 1) == 1)
res = (res * 1l * a) % mod;
a = (a * 1l * a) % mod;
e >>= 1;
}
return res;
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(File s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(s));
}
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 boolean ready() throws IOException {
return br.ready();
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 27189ac872644e2d2bf2b3185a379c41 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class TaskC {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(reader.readLine());
for(int i = 0; i < count; i++) {
String s = reader.readLine();
List<Integer> jumpyTiles = getJumpyTiles(s);
System.out.println(Math.abs(s.charAt(0) - s.charAt(s.length()-1)) + " " + jumpyTiles.size());
String out = jumpyTiles.stream()
.map(j -> j.toString())
.collect(Collectors.joining(" "));
System.out.println(out);
}
reader.close();
}
private static List<Integer> getJumpyTiles(String s) {
List<Integer> output = new ArrayList<>();
List<Integer> intermediate = new ArrayList<>();
char start = s.charAt(0);
char end = s.charAt(s.length()-1);
boolean asc = start <= end;
char trueStart = (char)Math.min(start, end);
char trueEnd = (char)Math.max(start, end);
start = trueStart;
end = trueEnd;
for(int i = 1; i < s.length()-1; i++) {
char c = s.charAt(i);
if(start <= c && c <= end) {
intermediate.add(i);
}
}
intermediate.sort((i1, i2) -> {
{
if(asc) {
return Integer.compare(s.charAt(i1), s.charAt(i2));
} else {
return Integer.compare(s.charAt(i2), s.charAt(i1));
}
}
});
output.add(1);
intermediate.forEach(i -> output.add(i+1));
output.add(s.length());
return output;
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 744488024d93f48cd01fa130da2a5c63 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(in.readLine());
while (t-- > 0) {
// StringTokenizer st1 = new StringTokenizer(in.readLine());
// StringTokenizer st2 = new StringTokenizer(in.readLine());
// int n = Integer.parseInt(st1.nextToken());
// int[] a = new int[n];
// for (int i = 0; i < n; i++) {
// a[i] = Integer.parseInt(st2.nextToken());
// }
String x = in.readLine();
int n = x.length();
List<Integer>[] nums = new List[26];
for (int i=0; i<26; i++)
nums[i] = new ArrayList<>();
for (int i=0; i<n; i++) {
char c = x.charAt(i);
int index = c - 'a';
nums[index].add(i+1);
}
int start = x.charAt(0) - 'a';
int end = x.charAt(n-1) - 'a';
List<Integer> ans = new ArrayList<>();
if (start > end) {
for (int i=start; i>=end; i--)
ans.addAll(nums[i]);
} else {
for (int i=start; i<=end; i++)
ans.addAll(nums[i]);
}
int diffs = 0;
int prev = start + 'a';
for (int index : ans) {
char c = x.charAt(index-1);
diffs += Math.abs(c - prev);
prev = c;
}
out.println(diffs + " " + ans.size());
for (int index : ans)
out.print(index + " ");
out.println();
}
in.close();
out.close();
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | dff57070f0df4e9c95a8e41d1842ae8f | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while (t-->0){
String s=sc.next();
int len=s.length();
char first=s.charAt(0);
char last=s.charAt(len-1);
int m=Math.abs(first-last);
LinkedList<Integer> res=new LinkedList<>();
Map<Character, LinkedList<Integer>> map=new HashMap<>();
for (int i =0; i <len; i++) {
char c=s.charAt(i);
if(!map.containsKey(c)){
map.put(c,new LinkedList<Integer>());
}
map.get(c).add(i+1);
}
if(first<=last){
for (char i =first;i<=last; i++) {
if(map.containsKey(i)){
for(int val:map.get(i)){
res.add(val);
}
}
}
}
else {
for (char i =(char)first; i>=last; i--) {
if(map.containsKey(i)){
for(int val:map.get(i)){
res.add(val);
}
}
}
}
System.out.println(m+" "+res.size());
for(int val:res){
System.out.print(val+" ");
}
System.out.println();
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 8594051dd78a53141b7a8763530dc7cb | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
new Thread(null, () -> new Main().run(), "1", 1 << 23).start();
}
private void run() {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Solution solve = new Solution();
int t = scan.nextInt();
// int t = 1;
for (int qq = 0; qq < t; qq++) {
solve.solve(scan, out);
out.println();
}
out.close();
}
}
class Solution {
/*
* think and coding
*/
static long MOD = (long) (1e9);
double EPS = 0.000_0001;
public void solve(FastReader scan, PrintWriter out) {
String str = scan.nextLine();
char first = str.charAt(0);
char last = str.charAt(str.length() - 1);
HashMap<Character, ArrayList<Integer>> cnt = new HashMap<>();
for (int i = 0; i < str.length(); i++) {
if (!cnt.containsKey(str.charAt(i))) {
ArrayList<Integer> arr = new ArrayList<>();
arr.add(i + 1);
cnt.put(str.charAt(i), arr);
} else {
cnt.get(str.charAt(i)).add(i + 1);
}
}
ArrayList<Integer> ans = new ArrayList<>();
if (first < last) {
for (char ch = first; ch <= last; ++ch) {
if (cnt.containsKey(ch)) {
ans.addAll(cnt.get(ch));
}
}
} else {
for (char ch = first; ch >= last; --ch) {
if (cnt.containsKey(ch)) {
ans.addAll(cnt.get(ch));
}
}
}
out.println(Math.abs(last - first) + " " + ans.size());
ans.forEach(t -> out.print(t + " "));
}
void swap(int i, int j, int[] arr) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
boolean check(int[] a, int[] b) {
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
static class Pair implements Comparable<Pair> {
int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public Pair(Pair p) {
this.a = p.a;
this.b = p.b;
}
@Override
public int compareTo(Pair p) {
return Integer.compare(a, p.a);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return a == pair.a && b == pair.b;
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
@Override
public String toString() {
return "Pair{" + "a=" + a + ", b=" + b + '}';
}
}
}
class FastReader {
private final BufferedReader br;
private StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
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[] initInt(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
long[] initLong(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 3ee477fb7a39a0254323b742fe055df0 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class d{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t--!=0){
char[] x=sc.next().toCharArray();
int n=x.length;
HashMap<Character,ArrayList<Integer>> mp=new HashMap<>();
for(int i=0;i<n;i++){
mp.putIfAbsent(x[i],new ArrayList<Integer>());
mp.get(x[i]).add(i+1);
}
ArrayList<Integer> ans=new ArrayList<>();
if(x[0]<x[n-1]){
for(char c=x[0];c!=x[n-1]+1;c++){
ans.addAll(mp.getOrDefault(c, new ArrayList<>()));
}
}else if(x[0]>x[n-1]){
for(char c=x[0];c!=x[n-1]-1;c--){
ans.addAll(mp.getOrDefault(c, new ArrayList<>()));
}
}else{
ans.addAll(mp.getOrDefault(x[0], new ArrayList<>()));
}
int res=0;
for(int i=1;i<ans.size();i++){
res+=x[ans.get(i)-1]-x[ans.get(i-1)-1];
}
System.out.println(Math.abs(res)+" "+ans.size());
for(int a:ans){
System.out.print(a+" ");
}
System.out.println();
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | da5544d309f2ce99597391f574a2e11b | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.awt.Point;
import java.io.*;
import java.util.*;
import static java.lang.System.*;
import static java.lang.Math.*;
public class CodeF
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Scanner sc= new Scanner(System.in);
int t=Integer.parseInt(br.readLine());
while(t-->0)
{
char ch[] = br.readLine().toCharArray();
char start = ch[0];
char end = ch[ch.length-1];
List<Point> list = new ArrayList<>();
for(int i=0;i<ch.length;i++)
{
char c = ch[i];
if((start<=c && c<=end) || (end<=c && c<=start))
list.add(new Point(c-'a', i+1));
}
boolean up = (start<=end);
Collections.sort(list, new Comparator<Point>(){
@Override
public int compare(Point p1, Point p2)
{
int diff = up? (p1.x-p2.x) : (p2.x-p1.x);
return diff==0? (p1.y-p2.y) : diff;
}
});
StringBuilder ans = new StringBuilder();
ans.append(abs(end-start)).append(' ').append(list.size()).append('\n');
for(Point p : list)
ans.append(p.y).append(' ');
//out.println("max="+max);
out.println(ans);
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | e4bdcb7ae79c68e0a07dc8a7a76b45c3 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
/*
*/
public class C{
static FastReader sc=null;
static boolean sortByIncreasing=true;
public static void main(String[] args) {
sc=new FastReader();
int t=sc.nextInt();
PrintWriter out=new PrintWriter(System.out);
for(int tt=0;tt<t;tt++) {
char a[]=sc.next().toCharArray();
int n=a.length;
int cost=Math.abs(a[n-1]-a[0]);
char first=a[0],last=a[n-1];
if(first>last) {
char temp=first;
first=last;
last=temp;
sortByIncreasing=false;
}
else sortByIncreasing=true;
ArrayList<Pair> pairs=new ArrayList<>();
for(int i=0;i<n;i++)if(a[i]>=first && a[i]<=last)
pairs.add(new Pair(i+1, a[i]));
Collections.sort(pairs);
out.println(cost+" "+pairs.size());
for(Pair e:pairs)out.print(e.id+" ");
out.println();
}
out.close();
}
static class Pair implements Comparable<Pair>{
int id,c;
Pair(int id,int c){
this.id=id;
this.c=c;
}
public int compareTo(Pair o) {
if(this.c==o.c)return this.id-o.id;
return (sortByIncreasing?this.c-o.c:o.c-this.c);
}
}
static int[] ruffleSort(int a[]) {
ArrayList<Integer> al=new ArrayList<>();
for(int i:a)al.add(i);
Collections.sort(al);
for(int i=0;i<a.length;i++)a[i]=al.get(i);
return a;
}
static void print(int a[]) {
for(int e:a) {
System.out.print(e+" ");
}
System.out.println();
}
static class FastReader{
StringTokenizer st=new StringTokenizer("");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
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());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
return a;
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 28e3b6b74db397bc27c4e51544534fb7 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | // 48 to 57
import java.util.*;
public class C1729
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
for(int qq=0;qq<q;qq++)
{
String s = sc.next();
int n = s.length();
char f = s.charAt(0);
char l = s.charAt(n-1);
int ans[] = new int[n];
int k=0;
int p=0;
long sum = 0;
if(f<l)
{
for(char ch = s.charAt(0);ch<=s.charAt(n-1);ch++)
{
p=0;
while(s.indexOf(ch,p)!=-1)
{
ans[k] = s.indexOf(ch,p);
k++;
sum += (int)ch - (int)f;
f = ch;
p = s.indexOf(ch,p) + 1;
}
}
}
else
{
for(char ch = s.charAt(0);ch>=s.charAt(n-1);ch--)
{
p=0;
while(s.indexOf(ch,p)!=-1)
{
ans[k] = s.indexOf(ch,p);
k++;
sum += (int)f - (int)ch;
f = ch;
p = s.indexOf(ch,p) + 1;
}
}
}
// System.out.println(Arrays.toString(ans));
System.out.println(sum+" "+k);
for(int i=0;i<k;i++)
{
System.out.print(ans[i]+1+" ");
}
System.out.println();
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 7e556bce8a46cf55c6c47ff8312d0df7 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class MyClass {
static Scanner in = new Scanner(System.in);
static int testCases, n;
static char s[];
static long a, b, c, mod = 998244353L;
static void solve() {
long x = 1L, y = 0L, mid = 2L;
for(int i = 4; i <= (int)a; i += 2) {
mid = (mid * 2 * (i - 1)) / (i / 2);
x = (mid / 2L) + y;
y = mid - x - 1L;
}
System.out.println(x % mod + " " + y % mod + " 1");
}
static char ch[] = {'-', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z'};
static void B() {
int zero = 0;
for(char i : s) {
if((i - '0') == 0 ) {
++zero;
}
}
StringBuilder ans = new StringBuilder();
if(zero == 0) {
for(char i : s) {
ans.append(ch[i - '0']);
}
System.out.println(ans.toString());
return;
} else {
int i = n - 1;
while(i >= 0) {
if(s[i] != '0') {
ans.append(ch[s[i] - '0']);
--i;
} else {
String t = Integer.parseInt(((s[i - 2] - '0') + "") + ((s[i - 1] - '0') + "") ) + "";
ans.append(ch[Integer.parseInt(t)]);
i -= 3;
}
}
}
ans.reverse();
System.out.println(ans.toString());
}
static void C() {
/*
1 2 3 4 5 -> index
l o g i c -> elements
l i g c -> letter visit sequence
1 4 3 5 -> jumping index
total jump = 4
cost = (l - i) + (i - g) + (g - c)
= |12 - 9| + |9 - 7| + |7 - 3|
= 3 + 2 + 4
= 9
all character have to in range of the first
and the last character of the serise
*/
List<Integer> list = new ArrayList<>();
int cost = 0;
for(int i = 1; i < n - 1; ++i) {
if(s[i] >= s[0] && s[i] <= s[n - 1]) {
list.add(i);
} else if(s[0] >= s[i] && s[n - 1] <= s[i]) {
list.add(i);
}
}
int moves = list.size() + 2;
cost = Math.abs(s[0] - s[n - 1]);
System.out.println(cost + " " + moves);
if(s[0] < s[n - 1]) {
Collections.sort(list, (x, y) -> s[x] - s[y]);
} else {
Collections.sort(list, (x, y) -> s[y] - s[x]);
}
System.out.print(1 + " ");
for(int i : list) {
System.out.print((i + 1) + " ");
}
System.out.print(n);
System.out.println();
}
public static void main(String args[]) {
testCases = in.nextInt();
for(int t = 0; t < testCases; ++t) {
//n = in.nextInt();
s = in.next().toCharArray();
n = s.length;
C();
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 8de9ef4ce8f4350e88e9de20ea2703fc | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes |
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
/**
* @author Nervose.Wu
* @date 2022/10/3 12:27
*/
public final class S820C {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
for (int i = sc.nextInt(); i > 0; i--) {
String cur = sc.next();
List<Integer>[] dict = new List[26];
for (int j = 0; j < 26; j++) {
dict[j] = new ArrayList<>();
}
for (int j = 1; j < cur.length() - 1; j++) {
dict[cur.charAt(j) - 'a'].add(j + 1);
}
List<Integer> res = new ArrayList<>();
res.add(1);
int minCost = Math.abs(cur.charAt(0) - cur.charAt(cur.length() - 1));
if (cur.charAt(0) - cur.charAt(cur.length() - 1) <= 0) {
for (int j = cur.charAt(0) - 'a'; j <= cur.charAt(cur.length() - 1) - 'a'; j++) {
res.addAll(dict[j]);
}
} else {
for (int j = cur.charAt(0) - 'a'; j >= cur.charAt(cur.length() - 1) - 'a'; j--) {
res.addAll(dict[j]);
}
}
res.add(cur.length());
out.println(minCost + " " + res.size());
out.println(res.stream().map(Object::toString).collect(Collectors.joining(" ")));
}
out.close();
}
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\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | d66994a5f74b28d8dfb30574180c904f | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
public class A{
public static void main(String args[])
{
Scanner Sc=new Scanner(System.in);
int t=Sc.nextInt();
while(t-->0){
String s=Sc.next();
int len=s.length();
int start=s.charAt(0)-'a';
int end=s.charAt(len-1)-'a';
int cost=Math.abs(start-end);
int max=Math.max(start,end);
int min=Math.min(start,end);
ArrayList<Pair> arl=new ArrayList<>();
for(int i=1;i<len-1;i++){
int num=s.charAt(i)-'a';
Pair p=new Pair(num,i+1);
arl.add(p);
}
if(start==max){
Collections.sort(arl);
Collections.reverse(arl);
}
else if(start==min){
Collections.sort(arl);
}
int steps=0;
ArrayList<Integer> path=new ArrayList<>();
for(Pair trav:arl)
{
int num=trav.val;
if(num<=max && num>=min){
steps++;
path.add(trav.index);
}
}
steps+=2;
System.out.println(cost+" "+steps);
System.out.print("1 ");
for(Integer trav:path)
System.out.print(trav+" ");
System.out.print(len);
System.out.println();
}
}
static class Pair implements Comparable<Pair>{
int val;
int index;
Pair(int val, int index){
this.val=val;
this.index=index;
}
public int compareTo(Pair o){
return this.val-o.val;
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | b79f29c1046775c95418adf5e120c31d | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 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){
String a = sc.next();
HashMap<Character, ArrayList<Integer>> x = new HashMap<>();
int n = a.length();
for(int i=0; i<n; i++){
if(x.containsKey(a.charAt(i)) == false){
x.put(a.charAt(i), new ArrayList<Integer>());
}
x.get(a.charAt(i)).add(i+1);
}
int sum = (int)Math.abs(a.charAt(0)-a.charAt(n-1));
ArrayList<Integer> ans = new ArrayList<>();
if(a.charAt(0)>a.charAt(n-1)){
for(int i=a.charAt(0); i>=a.charAt(n-1); i--){
char k = (char)i;
if(x.containsKey(k)){
for(int y: x.get(k)){
ans.add(y);
}
}
}
}
else{
for(int i=a.charAt(0); i<=a.charAt(n-1); i++){
char k = (char)i;
if(x.containsKey(k)){
for(int y: x.get(k)){
ans.add(y);
}
}
}
}
System.out.println(sum + " " + ans.size());
for(int i=0; i<ans.size(); i++){
System.out.print(ans.get(i) + " ");
}
System.out.println();
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 197853b5e91b6b00a6c9e347638c0a84 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static FastScanner fs;
public static void main(String[] args) {
fs=new FastScanner();
int t=fs.nextInt();
for( int i =0;i<t;i++){
solve();
}
}
public static void solve(){
String s=fs.nextLine();
int n=s.length();
Vector<Integer>[]v=new Vector[26];
for( int i=0;i<26;i++){
v[i]=new Vector<Integer>();
}
for( int i=0;i<s.length();i++){
v[s.charAt(i)-97].add(i);
}
Vector<Integer>res=new Vector<Integer>();
int ans=Math.abs(s.charAt(0)-s.charAt(n-1));
if(s.charAt(0)<=s.charAt(n-1)){
for( int i=s.charAt(0);i<=s.charAt(n-1);i++){
for (int it:v[i-97]) {
res.add(it);
}
}
}
else{
for( int i=s.charAt(0);i>=s.charAt(n-1);i--){
for (int it:v[i-97]) {
res.add(it);
}
}
}
System.out.print(ans+" "+res.size());
System.out.println();
for( int i=0;i<res.size();i++){
System.out.print((res.elementAt(i)+1)+" ");
}
System.out.println();
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | db725c69988898e411087c57f74fb2bd | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
for (int i = 0; i < t; i++) {
String s = br.readLine();
if(s.charAt(0)<=s.charAt(s.length()-1))
{
int cost = 0;
char curr = s.charAt(0);
ArrayList<Integer> a = new ArrayList<Integer>();
for (char j = s.charAt(0); j <= s.charAt(s.length()-1) ; j++) {
for (int k = 0; k < s.length(); k++) {
if(s.charAt(k)==j)
{
cost+=Math.abs(curr-j);
curr=j;
a.add(k+1);
}
}
}
pw.println(cost + " " + a.size());
boolean first=true;
for(int n: a)
{
if(first)
{
first=!first;
pw.print(n);
}
else
{
pw.print(" " + n);
}
}
pw.println();
}
else
{
int cost = 0;
char curr = s.charAt(0);
ArrayList<Integer> a = new ArrayList<Integer>();
for (char j = s.charAt(0); j >= s.charAt(s.length()-1) ; j--) {
for (int k = 0; k < s.length(); k++) {
if(s.charAt(k)==j)
{
cost+=Math.abs(curr-j);
curr=j;
a.add(k+1);
}
}
}
pw.println(cost + " " + a.size());
boolean first=true;
for(int n: a)
{
if(first)
{
first=!first;
pw.print(n);
}
else
{
pw.print(" " + n);
}
}
pw.println();
}
}
pw.close();
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | b5e335955340329808933a20f5a4ecc7 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
FastScanner input = new FastScanner();
int tc = input.nextInt();
work:
while (tc-- > 0) {
String s =input.next();
char a[] =s.toCharArray();
int letter[] = new int[26];
char first = a[0],last = a[a.length-1];
HashMap<Character,ArrayList<Integer>> map = new HashMap<>();
for (int i = 0; i < a.length; i++) {
letter[a[i]-'a']++;
if(map.containsKey(a[i]))
{
map.get(a[i]).add(i+1);
}
else
{
ArrayList<Integer> ar = new ArrayList<>();
ar.add(i+1);
map.put(a[i], ar);
}
}
Arrays.sort(a);
String ss = new String(a);
if(first>last)
{
StringBuilder rev = new StringBuilder(ss);
String temp = rev.reverse().toString();
a = temp.toCharArray();
}
// System.out.println(a);
int n = a.length;
long cost=0;
long step = 0;
StringBuilder result = new StringBuilder();
for (int i = 0; i <n; i++) {
if(a[i]==first)
{
if(map.containsKey(first))
{
for (Integer integer : map.get(first)) {
result.append(integer+" ");
}
map.remove(first);
}
step+=letter[first-'a'];
letter[first-'a']=0;
while(i+1<n&&a[i+1]!=last)
{
cost+=Math.abs(a[i+1]-a[i]);
step+=letter[a[i+1]-'a'];
letter[a[i+1]-'a']=0;
if(map.containsKey(a[i+1]))
{
for (Integer integer : map.get(a[i+1])) {
result.append(integer+" ");
}
map.remove(a[i+1]);
}
i++;
}
if(map.containsKey(last))
{
for (Integer integer : map.get(last)) {
result.append(integer+" ");
}
map.remove(last);
}
cost+=Math.abs(last-a[i]);
step+=letter[last-'a'];
break;
}
}
System.out.println(cost+" "+step);
System.out.println(result);
}
}
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());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() throws IOException
{
return br.readLine();
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 7089b6f34a33996eef491c2e782c12ca | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
import java.io.*;
public class codechef
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
// String testcases[]=br.readLine().split(" ");
// int t=Integer.parseInt(testcases[0]);
for(int i=0;i<t;i++)
{
String s=sc.next();
HashMap<Character, ArrayList<Integer>> hs=new HashMap<>();
for(int j=0;j<s.length();j++)
{
if(hs.containsKey(s.charAt(j)))
hs.get(s.charAt(j)).add(j+1);
else
{
hs.put(s.charAt(j),new ArrayList<Integer>());
hs.get(s.charAt(j)).add(j+1);
}
}
int ans1=Math.abs(s.charAt(0)-s.charAt(s.length()-1));
ArrayList<Integer> al=new ArrayList<>();
ArrayList<Integer> ans=new ArrayList<>();
int count=0;
if((int)s.charAt(s.length()-1)>=(int)s.charAt(0))
{
for(int j=(int)s.charAt(0);j<=(int)s.charAt(s.length()-1);j++)
{
if(hs.containsKey((char)j))
{
al=hs.get((char)j);
for(int k=0;k<al.size();k++)
{
ans.add(al.get(k));
count++;
}
}
}
}
else
{
for(int j=(int)s.charAt(0);j>=(int)s.charAt(s.length()-1);j--)
{
if(hs.containsKey((char)j))
{
al=hs.get((char)j);
for(int k=0;k<al.size();k++)
{
ans.add(al.get(k));
count++;
}
}
}
}
System.out.println(ans1+" "+count);
for(int j=0;j<ans.size();j++)
System.out.print(ans.get(j)+" ");
System.out.println();
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | a6afe65b3f8d52fd61abc3769c1b60d0 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
public class Test {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i = 0; i < t; i++) {
String s = in.next();
Map<Integer, List<Integer>> map = new HashMap<>();
for(int j = 'a'; j <= 'z'; j++) {
map.put(j, new ArrayList<>());
}
for(int j = 0; j < s.length(); j++) {
map.get((int)s.charAt(j)).add(j);
}
List<Integer> list = new ArrayList<>();
for(int j = s.charAt(0); j != s.charAt(s.length() - 1);) {
list.addAll(map.get(j));
if(s.charAt(0) < s.charAt(s.length() - 1)) {
j++;
} else {
j--;
}
}
list.addAll(map.get((int)s.charAt(s.length() - 1)));
System.out.println(Math.abs(s.charAt(0) - s.charAt(s.length() - 1)) + " " + list.size());
System.out.print(list.get(0) + 1);
for(int j = 1; j < list.size(); j++) {
System.out.print(" " + (list.get(j) + 1));
}
System.out.println();
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 33ab8af72b59d4d4291c99c5b2eec381 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes |
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class C {
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static Scanner scanner=new Scanner(new BufferedInputStream(System.in));
public static void main(String[] args) {
int num = scanner.nextInt();
while (num--!=0){
String t= scanner.next();
boolean f = true;
char min = t.charAt(0);
char max = t.charAt(t.length()-1);
if (max<min){
char tt =min;
min=max;
max=tt;
f=false;
}
int sum= max-min;
List<Integer> list = new ArrayList<>();
for (int i = 1; i < t.length()-1; i++) {
if (t.charAt(i)>=min&&t.charAt(i)<=max){
list.add(i);
}
}
if (f) {
list.sort(Comparator.comparingInt(t::charAt));
}else {
list.sort((a,b)->t.charAt(b)-t.charAt(a));
}
out.println(sum+" "+(list.size()+2));
out.print(1+" ");
for (int i = 0; i < list.size(); i++) {
out.print((list.get(i)+1)+" ");
}
out.println(t.length());
}
out.flush();
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | c9c54a089c53a5722db4ce5454714f71 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class JumpOnTiles {
static class Pair{
char ch;
int idx;
public Pair(char ch,int idx){
this.ch=ch;
this.idx=idx;
}
}
public static void main(String[] args) {
int t;
String s;
Scanner sc = new Scanner(System.in);
t = sc.nextInt();
while(t-->0){
s=sc.next();
Pair[] arr =new Pair[s.length()];
for(int i=0;i<s.length();i++) {
arr[i] = new Pair(s.charAt(i), i);
}
if(s.charAt(0)<=s.charAt(s.length()-1))
{
Arrays.sort(arr,(Pair a,Pair b)->{
if(a.ch!=b.ch)
return a.ch-b.ch;
else
return a.idx-b.idx;
});
}
else{
Arrays.sort(arr,(Pair a,Pair b)->{
if(a.ch!=b.ch)
return b.ch-a.ch;
else
return a.idx-b.idx;
});
}
int count=0;
ArrayList<Integer> ans = new ArrayList<>();
for(int i=0;i<arr.length;i++){
if(arr[i].idx==0){
while(arr[i].idx!=arr.length-1)
{
ans.add(arr[i].idx+1);
i++;
count++;
}
ans.add(arr.length);
}
}
System.out.println(Math.abs((int)(s.charAt(0)-s.charAt(s.length()-1)))+" "+ans.size());
for(int i=0;i<ans.size();i++){
System.out.print(ans.get(i)+" ");
}
System.out.println();
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 7438f413779bfe89677c463b95d8273d | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
public class Sol {
static Scanner rd = new Scanner(System.in);
public static void main(String[] args) {
int tt = rd.nextInt();
while (tt-- > 0) {
new Solution().solve();
}
}
static class Solution {
char[] s;
int n;
void solve() {
s = rd.next().toCharArray();
n = s.length;
int first = s[0], last = s[n - 1];
if (n == 2) {
System.out.printf("%d %d\n", Math.abs(first - last), 2);
System.out.printf("%d %d\n", 1, n);
return;
}
int[][] a = new int[n - 2][2];
for (int i = 0; i < a.length; i++) {
a[i][0] = s[i + 1];
a[i][1] = i + 2;
}
Arrays.sort(a, (o1, o2) -> {
return o1[0] - o2[0];
});
int cur = first, res = 0;
StringBuilder sb = new StringBuilder();
sb.append(1).append(' ');
if (first > last) {
reverse(a);
// Utils.print2DArray(a);
for (int i = 0; i < a.length; i++) {
if (a[i][0] <= cur) {
if (a[i][0] >= last) {
cur = a[i][0];
res++;
sb.append(a[i][1]).append(' ');
} else {
break;
}
}
}
} else {
for (int i = 0; i < a.length; i++) {
if (cur <= a[i][0]) {
if (a[i][0] <= last) {
cur = a[i][0];
res++;
sb.append(a[i][1]).append(' ');
} else {
break;
}
}
}
}
sb.append(n);
System.out.printf("%d %d\n", Math.abs(first - last), res + 2);
System.out.println(sb.toString());
}
void reverse(int[][] a) {
int i = 0, j = a.length - 1;
while (i < j) {
int[] t = a[i];
a[i] = a[j];
a[j] = t;
i++;
j--;
}
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | a232601c1ad758989f1e563a85775bdc | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
public class MainClass {
public static long mod = 1000_000_000 + 7;
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int testCases = in.nextInt();
while (testCases-- > 0) {
String s = in.next();
Map<Integer, List<Integer>> map = new HashMap<>();
int start = s.charAt(0) - 'a';
int end = s.charAt(s.length() - 1) - 'a';
for (int i = 0; i < s.length(); i++) {
map.putIfAbsent(s.charAt(i) - 'a', new ArrayList<>());
map.get(s.charAt(i) - 'a').add(i + 1);
}
List<Integer> ans = new ArrayList<>();
if (start <= end) {
for (int i = start; i <= end; i++) {
if (map.get(i) != null && !map.get(i).isEmpty()) {
ans.addAll(map.get(i));
}
}
} else {
for (int i = start; i >= end; i--) {
if (map.get(i) != null && !map.get(i).isEmpty()) {
ans.addAll(map.get(i));
}
}
}
out.println(Math.abs(end - start) + " " + ans.size());
for (int i : ans) {
out.print(i + " ");
}
out.println();
}
out.close();
}
static class DisjointUnionSets {
int[] rank, parent;
int n;
public DisjointUnionSets(int n) {
rank = new int[n];
parent = new int[n];
this.n = n;
makeSet();
}
void makeSet() {
for (int i = 0; i < n; i++) {
rank[i] = 1;
parent[i] = i;
}
}
int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
int getRank(int x) {
return rank[find(x)];
}
void union(int x, int y) {
int xRoot = find(x), yRoot = find(y);
if (xRoot == yRoot)
return;
if (rank[xRoot] < rank[yRoot]) {
parent[xRoot] = yRoot;
rank[yRoot] = rank[xRoot] + rank[yRoot];
} else {
parent[yRoot] = xRoot;
rank[xRoot] = rank[xRoot] + rank[yRoot];
}
}
}
public static int gcd(int a, int b) {
if (b == 0) return a;
else if (a == 0) return b;
else if (b < a) return gcd(b, a % b);
return gcd(a % b, b);
}
public static long maxNum(long... x) {
long ans = -9223372036854775808L;
for (long p : x) {
ans = Math.max(p, ans);
}
return ans;
}
public static int maxNum(int... x) {
int ans = -2147483648;
for (int p : x) {
ans = Math.max(p, ans);
}
return ans;
}
public static long modPower(long x, long m, long mod) {
long ans = 1;
while (m > 0) {
if (m % 2 == 1) {
ans = (ans * x) % mod;
}
x = (x * x) % mod;
m = m / 2;
}
return ans;
}
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 long nextLong() {
return Long.parseLong(next());
}
}
public static class Pair<K extends Comparable<K>, V extends Comparable<V>> implements Comparable<Pair<K, V>> {
private K p1;
private V p2;
public Pair(K p1, V p2) {
this.p1 = p1;
this.p2 = p2;
}
public K getP1() {
return p1;
}
public void setP1(K p1) {
this.p1 = p1;
}
public V getP2() {
return p2;
}
public void setP2(V p2) {
this.p2 = p2;
}
@Override
public int compareTo(Pair<K, V> o) {
if (p1.compareTo(o.getP1()) == 0) {
return p2.compareTo(o.getP2());
}
return p1.compareTo(o.getP1());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equals(p1, pair.p1) && Objects.equals(p2, pair.p2);
}
@Override
public int hashCode() {
return Objects.hash(p1, p2);
}
}
static <T extends Comparable<T>> int lowerbound(List<T> arr, T element) {
int size = arr.size();
if (element.compareTo(arr.get(size - 1)) >= 0)
return -1;
if (element.compareTo(arr.get(0)) < 0)
return 0;
int low = 0;
int high = size - 1;
while (low < high) {
int mid = (low + high) / 2;
if (element.compareTo(arr.get(mid)) >= 0) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
// static classes for segment tree
public static class SegmentTree {
private SegmentTreeNode head;
public SegmentTree(int start, int end) {
head = new SegmentTreeNode(start, end);
}
public SegmentTreeNode getHead() {
return head;
}
public void setHead(SegmentTreeNode head) {
this.head = head;
}
}
public static class SegmentTreeNode {
private int start;
private int end;
private SegmentTreeNode leftChildNode;
private SegmentTreeNode rightChildNode;
private long value;
private List<SegmentTreeUpdateObject> pendingUpdates = new ArrayList<>();
public SegmentTreeNode(int start, int end) {
this.start = start;
this.end = end;
if (end - start >= 1) {
int middle = start + (end - start) / 2;
leftChildNode = new SegmentTreeNode(start, middle);
rightChildNode = new SegmentTreeNode(middle + 1, end);
}
}
public SegmentTreeNode getLeftChildNode() {
return leftChildNode;
}
public void setLeftChildNode(SegmentTreeNode leftChildNode) {
this.leftChildNode = leftChildNode;
}
public SegmentTreeNode getRightChildNode() {
return rightChildNode;
}
public void setRightChildNode(SegmentTreeNode rightChildNode) {
this.rightChildNode = rightChildNode;
}
public long getValue() {
return value;
}
public void setValue(long value) {
this.value = value;
}
public List<SegmentTreeUpdateObject> getPendingUpdates() {
return pendingUpdates;
}
public void addPendingUpdates(SegmentTreeUpdateObject pendingUpdate) {
pendingUpdate.applyUpdate(this);
this.pendingUpdates.add(pendingUpdate);
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public void updateChildren() {
for (SegmentTreeUpdateObject updateObject : pendingUpdates) {
if (updateObject.shouldApply(leftChildNode)) {
leftChildNode.addPendingUpdates(updateObject.getNewInstance(leftChildNode.getStart(), leftChildNode.getEnd()));
}
}
for (SegmentTreeUpdateObject updateObject : pendingUpdates) {
if (updateObject.shouldApply(rightChildNode)) {
rightChildNode.addPendingUpdates(updateObject.getNewInstance(rightChildNode.getStart(), rightChildNode.getEnd()));
}
}
pendingUpdates = new ArrayList<>();
}
public long query(int start, int end) {
if (start == this.getStart() && end == this.getEnd()) {
return value;
} else {
long ans = 0;
this.updateChildren();
if (start <= leftChildNode.getEnd()) {
ans += leftChildNode.query(start, Math.min(end, leftChildNode.getEnd()));
}
if (end >= rightChildNode.getStart()) {
ans += rightChildNode.query(Math.max(rightChildNode.getStart(), start), end);
}
return ans;
}
}
}
// change as per need
public static class SegmentTreeUpdateObject {
private int start;
private int end;
private int startValue;
private long value;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public long getValue() {
return value;
}
public void setValue(long value) {
this.value = value;
}
public int getStartValue() {
return startValue;
}
public void setStartValue(int startValue) {
this.startValue = startValue;
}
public void applyUpdate(SegmentTreeNode node) {
node.setValue(node.getValue() + this.value);
}
public boolean shouldApply(SegmentTreeNode node) {
int currentStart = this.getStart();
int currentEnd = this.getEnd();
int tmpStart = Math.max(node.getStart(), currentStart);
int tmpEnd = Math.min(node.getEnd(), currentEnd);
return tmpStart <= tmpEnd;
}
public SegmentTreeUpdateObject getNewInstance(int start, int end) {
int currentStart = this.getStart();
int currentEnd = this.getEnd();
int currentStartValue = this.getStartValue();
long currentValue;
if (start > currentStart) {
currentStartValue += (start - currentStart);
}
start = Math.max(start, currentStart);
end = Math.min(end, currentEnd);
if (start > end) {
return null;
}
currentValue = ((long) (end - start + 2) * (end - start + 1)) / 2 + (long) (currentStartValue - 1) * (end - start + 1);
SegmentTreeUpdateObject segmentTreeUpdateObjectTemp = new SegmentTreeUpdateObject();
segmentTreeUpdateObjectTemp.setEnd(end);
segmentTreeUpdateObjectTemp.setStart(start);
segmentTreeUpdateObjectTemp.setValue(currentValue);
segmentTreeUpdateObjectTemp.setStartValue(currentStartValue);
return segmentTreeUpdateObjectTemp;
}
@Override
public String toString() {
return "SegmentTreeUpdateObject{" +
"start=" + start +
", end=" + end +
", startValue=" + startValue +
", value=" + value +
'}';
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | d333ad03e825491d58a179bf120c7e81 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
public class Main {
static class Result {
int cost;
int m;
int[] sequence;
}
static Map<Character, List<Integer>> getCharIndexesMap(String s) {
Map<Character, List<Integer>> map = new HashMap<>();
for(int i = 0; i < s.length(); i++) {
char cur = s.charAt(i);
List<Integer> charIndexes = map.getOrDefault(cur, new ArrayList<>());
charIndexes.add(i);
map.put(cur, charIndexes);
}
return map;
}
static Result getSequence(String s) {
Result result = new Result();
Map<Character, List<Integer>> charIndexesMap = getCharIndexesMap(s);
int cost = 0;
char end = s.charAt(s.length() - 1);
char lastChar = s.charAt(0);
List<Integer> sequence = new ArrayList<>();
if(end >= lastChar) {
for(char cur = lastChar; cur <= end; cur++) {
List<Integer> charIndexes = charIndexesMap.getOrDefault(cur, new ArrayList<>());
for(int i = 0; i < charIndexes.size(); i++) {
cost += Math.abs(cur - lastChar);
sequence.add(charIndexes.get(i));
lastChar = cur;
}
}
} else {
for(char cur = lastChar; cur >= end; cur--) {
List<Integer> charIndexes = charIndexesMap.getOrDefault(cur, new ArrayList<>());
for(int i = 0; i < charIndexes.size(); i++) {
cost += Math.abs(cur - lastChar);
sequence.add(charIndexes.get(i));
lastChar = cur;
}
}
}
result.cost = cost;
result.m = sequence.size();
result.sequence = new int[sequence.size()];
for(int i = 0; i < sequence.size(); i++) {
result.sequence[i] = sequence.get(i) + 1;
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
scanner.nextLine();
while(t-- > 0) {
String s = scanner.nextLine();
Result result = getSequence(s);
System.out.println(result.cost + " " + result.m);
for(int i = 0; i < result.sequence.length - 1; i++) {
System.out.print(result.sequence[i] + " ");
}
System.out.println(result.sequence[result.sequence.length - 1]);
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | e89329e006609862575b924727daa4cd | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
try{
FastReader read=new FastReader();
StringBuilder sb = new StringBuilder();
int t=read.nextInt();
for(int k=1;k<=t;k++)
{
//sb.append("Case #"+k+": ");
String str = read.next();
ArrayList<ArrayList<Integer>> al = new ArrayList<ArrayList<Integer>>();
for(int i=0;i<27;i++)
{
al.add(new ArrayList<>());
}
int ans = Math.abs(str.charAt(0)-str.charAt(str.length()-1));
for(int i=0;i<str.length();i++)
{
al.get(str.charAt(i)-96).add(i+1);
}
sb.append(ans+" ");
StringBuilder sb1 = new StringBuilder();
int count=0;
if(str.charAt(0)<=str.charAt(str.length()-1))
{
for(int i=str.charAt(0)-96;i<=str.charAt(str.length()-1)-96;i++)
{
for(int j=0;j<al.get(i).size();j++)
{
sb1.append(al.get(i).get(j)+" ");
count++;
}
}
}
else
{
for(int i=str.charAt(0)-96;i>=str.charAt(str.length()-1)-96;i--)
{
for(int j=0;j<al.get(i).size();j++)
{
sb1.append(al.get(i).get(j)+" ");
count++;
}
}
}
sb.append(count);
sb.append('\n');
sb.append(sb1);
sb.append('\n');
}
System.out.println(sb);
}
catch(Exception e)
{return;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void sort(int[] A) {
int n = A.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = A[i];
int randomPos = i + rnd.nextInt(n - i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A) {
int n = A.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = A[i];
int randomPos = i + rnd.nextInt(n - i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(ArrayList<Long> A,char ch) {
int n = A.size();
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = A.get(i);
int randomPos = i + rnd.nextInt(n - i);
A.set(i,A.get(randomPos));
A.set(randomPos,tmp);
}
Collections.sort(A);
}
static void sort(ArrayList<Integer> A) {
int n = A.size();
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = A.get(i);
int randomPos = i + rnd.nextInt(n - i);
A.set(i,A.get(randomPos));
A.set(randomPos,tmp);
}
Collections.sort(A);
}
static String sort(String s) {
Character ch[] = new Character[s.length()];
for (int i = 0; i < s.length(); i++) {
ch[i] = s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st = new StringBuffer("");
for (int i = 0; i < s.length(); i++) {
st.append(ch[i]);
}
return st.toString();
}
}
class pair implements Comparable<pair>
{
int X,Y,C;
pair(int s,int e,int c)
{
X=s;
Y=e;
C=c;
}
public int compareTo(pair p )
{
return this.C-p.C;
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | e85e22712df23dcc5840f90ab2d93973 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args)throws IOException {
FastScanner scan = new FastScanner();
PrintWriter output = new PrintWriter(System.out);
int t = scan.nextInt();
for(int tt = 0;tt<t;tt++) {
char arr[] = scan.next().toCharArray();
int n = arr.length;
TreeMap<Character, ArrayList<Integer>> treeMap;
if(arr[0] <= arr[n-1]) {
treeMap = new TreeMap<>();
}
else {
treeMap = new TreeMap<>(Comparator.reverseOrder());
}
int m = 0;
for(int i = 1;i<n-1;i++) {
if(treeMap.containsKey(arr[i])) {
m++;
treeMap.get(arr[i]).add(i+1);
}
else if(isInBetween(arr[0], arr[n-1], arr[i])) {
m++;
treeMap.put(arr[i], new ArrayList<>());
treeMap.get(arr[i]).add(i+1);
}
}
int cost = Math.abs(arr[n-1]-arr[0]);
output.println(cost + " " + (m+2));
output.print(1 + " ");
for(Character key : treeMap.keySet()) {
for(Integer value : treeMap.get(key)) {
output.print(value + " ");
}
}
output.print(n);
output.println();
}
output.flush();
}
public static boolean isInBetween(char l, char r, char val) {
if(val >= l && val <= r) return true;
else return (val <= l && val >= r);
}
public static int[] sort(int arr[]) {
List<Integer> list = new ArrayList<>();
for(int i:arr) list.add(i);
Collections.sort(list);
for(int i = 0;i<list.size();i++) arr[i] = list.get(i);
return arr;
}
public static int gcd(int a, int b) {
if(a == 0) return b;
return gcd(b%a, a);
}
public static void printArray(int arr[]) {
for(int i:arr) System.out.print(i+" ");
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());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | e78f6e955d42ae1ab68ebbcfa39eee22 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class C1729 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t=0; t<T; t++) {
char[] S = in.next().toCharArray();
char start = S[0];
char end = S[S.length-1];
List<Point> list = new ArrayList<>();
for (int i=0; i<S.length; i++) {
char c = S[i];
if ((start <= c && c <= end) || (end <= c && c <= start)) {
list.add(new Point(c-'a', i+1));
}
}
boolean up = (start <= end);
Collections.sort(list, new Comparator<Point>() {
@Override
public int compare(Point p1, Point p2) {
int diff = up ? (p1.x - p2.x) : (p2.x - p1.x);
return (diff == 0) ? (p1.y - p2.y) : diff;
}
});
StringBuilder out = new StringBuilder();
out.append(Math.abs(end-start)).append(' ').append(list.size()).append('\n');
for (Point p : list) {
out.append(p.y).append(' ');
}
System.out.println(out);
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 73331d4f5f114a3077364b27a5af5071 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class C {
public static void main(String[]args) throws IOException {
Scanner sc=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0) {
String s=sc.next();
ArrayList<Integer>[]idx=new ArrayList[26];
for(int i=0;i<26;i++)idx[i]=new ArrayList<Integer>();
TreeSet<Character>tr=new TreeSet<Character>();
tr.add((char) ('a'-1));
tr.add((char) ('z'+1));
for(int i=0;i<s.length();i++) {
idx[s.charAt(i)-'a'].add(i+1);
tr.add(s.charAt(i));
}
StringBuilder ans=new StringBuilder();
int st=Math.abs(s.charAt(s.length()-1)-s.charAt(0));
int js=0;
char lst=s.charAt(s.length()-1);
for(char c=s.charAt(0);;) {
js+=idx[c-'a'].size();
for(int x:idx[c-'a'])ans.append(x+" ");
if(c==lst) {
break;
}
if(c<lst) {
c=tr.higher(c);
}else {
c=tr.lower(c);
}
}
out.println(st+" "+js);out.println(ans);
}
out.close();
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public boolean hasNext() {return st.hasMoreTokens();}
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 String nextLine() throws IOException {return br.readLine();}
public boolean ready() throws IOException {return br.ready(); }
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 5d22f5399781d05af5d57ebdde5a5830 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.HashMap;
import java.util.Queue;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Collections;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Arrays;
import java.math.BigInteger;
public class Main{
private static FastReader fs;
public static void main(String[] args) throws Exception{
fs = new FastReader();
int test = 1;
test =fs.nextInt();
while(test-- > 0) solve();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
int[] readArray(int n){
int ar[] = new int[n];
for(int i=0; i<n; i++) ar[i] = fs.nextInt();
return ar;
}
int gcd(int a, int b){
int result = Math.min(a, b);
while (result > 0) {
if (a % result == 0 && b % result == 0) {
break;
}
result--;
}
return result;
}
}
// use fs as scanner
// snippets -> sieve, power, Node , lowerbound, upperbound, readarray
public static void solve(){
String str = fs.nextLine();
HashMap<Character, Integer> map = new HashMap<>();
HashMap<Character, List<Integer>> mapToList = new HashMap<>();
int ind = 1;
for(char ch : str.toCharArray()){
map.put(ch, map.getOrDefault(ch, 0) + 1);
if( mapToList.containsKey(ch) ){
mapToList.get(ch).add(ind);
}
else{
List<Integer> tt = new ArrayList<>();
tt.add(ind);
mapToList.put(ch, tt);
}
ind++;
}
char a[] ="abcdefghijklmnopqrstuvwxyz".toCharArray();
int start = str.charAt(0) - 'a';
int end = str.charAt(str.length()-1) - 'a';
int jumps = 0;
StringBuilder ans = new StringBuilder();
if(start <= end){
while( start <= end ){
if(map.containsKey(a[start])){
jumps += map.get(a[start]);
for(int ele : mapToList.get(a[start])){
ans.append(ele + " ");
// System.out.println(a[start] + " -> " + ele + " ");
}
}
start++;
}
}
else{
while( start >= end ){
if(map.containsKey(a[start])){
jumps += map.get(a[start]);
for(int ele : mapToList.get(a[start])){
ans.append(ele + " ");
// System.out.println(a[start] + " -> " + ele + " ");
}
}
start--;
}
}
System.out.print(Math.abs(str.charAt(0) - str.charAt(str.length() - 1)));
System.out.println(" "+ jumps);
System.out.println(ans);
}
}
/*
do something when you are stuck
and be calm
*/ | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | da1bb0f52614cef2f48d60e887565d5a | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.Scanner;
public class C_Jumping_on_Tiles {
static Scanner in = new Scanner(System.in);
static int testCases, n;
static char a[];
static void solve() {
ArrayList1<Integer> list[] = new ArrayList1[27];
for(int i = 0; i <= 26; ++i) {
list[i] = new ArrayList1<>();
}
for(int i = 0; i < n; ++i) {
list[a[i] - 'a'].add(i + 1);
}
long cost = Math.abs(a[n - 1] - a[0]);
ArrayList1<Integer> jump = new ArrayList1<>();
if(a[n - 1] >= a[0]) {
for(int i = (a[0] - 'a'); i <= (a[n - 1] - 'a'); ++i) {
while(!list[i].isEmpty()) {
jump.add(list[i].get(0));
list[i].popFront();
}
}
} else {
for(int i = (a[0] - 'a'); i >= (a[n - 1] - 'a'); --i ) {
while(!list[i].isEmpty()) {
jump.add(list[i].get(0));
list[i].popFront();
}
}
}
System.out.println(cost + " " + jump.size());
jump.see();
}
public static void main(String [] amit) {
testCases = in.nextInt();
for(int t = 0; t < testCases; ++t) {
a = in.next().toCharArray();
n = a.length;
solve();
}
}
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;
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | d63264015a864fc0ffd69a1d9bfbdbe1 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 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.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Sparsh Sanchorawala
*/
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);
CJumpingOnTiles solver = new CJumpingOnTiles();
solver.solve(1, in, out);
out.close();
}
static class CJumpingOnTiles {
public void solve(int testNumber, InputReader s, PrintWriter w) {
int t = s.nextInt();
while (t-- > 0) {
char[] a = s.next().toCharArray();
ArrayList<Integer>[] adj = new ArrayList[26];
for (int i = 0; i < 26; i++)
adj[i] = new ArrayList<>();
for (int i = 0; i < a.length; i++) {
adj[a[i] - 'a'].add(i);
}
ArrayList<Integer> res = new ArrayList<>();
if (a[0] <= a[a.length - 1]) {
for (char c = a[0]; c <= a[a.length - 1]; c++) {
for (int i : adj[c - 'a'])
res.add(i);
}
} else {
for (char c = a[0]; c >= a[a.length - 1]; c--) {
for (int i : adj[c - 'a'])
res.add(i);
}
}
w.println(Math.abs(a[0] - a[a.length - 1]) + " " + res.size());
for (int i : res)
w.print((i + 1) + " ");
w.println();
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return nextString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 55c75f20ba419aa0c8e2831878a9f96e | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int T=fs.nextInt();
for (int tt=0; tt<T; tt++) {
char[] line=fs.next().toCharArray();
ArrayList<Integer>[] indeciesOf=new ArrayList[26];
for (int i=0; i<26; i++) {
indeciesOf[i]=new ArrayList<>();
}
ArrayList<Integer> res=new ArrayList<>();
for (int i=0; i<line.length; i++) {
if (line[i]<=Math.max(line[0], line[line.length-1]) && line[i]>=Math.min(line[0], line[line.length-1])) {
res.add(i);
}
}
Collections.sort(res, (a, b) -> {
if (line[a]!=line[b]) {
return Integer.compare(Math.abs(line[a]-line[0]), Math.abs(line[b]-line[0]));
}
return Integer.compare(a, b);
});
int cost=Math.abs(line[0]-line[line.length-1]);
out.println(cost+" "+res.size());
for (int i:res) {
out.print(i+1+" ");
}
out.println();
}
out.close();
}
static final Random random=new Random();
static final int mod=1_000_000_007;
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long add(long a, long b) {
return (a+b)%mod;
}
static long sub(long a, long b) {
return ((a-b)%mod+mod)%mod;
}
static long mul(long a, long b) {
return (a*b)%mod;
}
static long exp(long base, long exp) {
if (exp==0) return 1;
long half=exp(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static long[] factorials=new long[2_000_001];
static long[] invFactorials=new long[2_000_001];
static void precompFacts() {
factorials[0]=invFactorials[0]=1;
for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i);
invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2);
for (int i=invFactorials.length-2; i>=0; i--)
invFactorials[i]=mul(invFactorials[i+1], i+1);
}
static long nCk(int n, int k) {
return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k]));
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static 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\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 1c5ae097d4f5dbf6f99d84143274bc7c | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes |
import java.util.*;
public class code {
static class pair{
char ch;
int x;
int fl;
public pair(char a,int x,int fl){
this.ch=a;
this.x=x;
this.fl=fl;
}
}
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int t= sc.nextInt();
while(t-->0){
solve();
}
}
static void solve() {
StringBuilder s = new StringBuilder(sc.next());
int n = s.length();
boolean fl = true;
if (s.charAt(0) > s.charAt(n - 1)) {
fl = false;
}
if (fl) {
char last = s.charAt(n - 1);
char first = s.charAt(0);
ArrayList<pair> a = new ArrayList<>();
for (int i = 0; i < n; i++) {
a.add(new pair(s.charAt(i), i, n - i - 1));
}
int ans = 0;
Collections.sort(a, (aa, b) -> aa.ch - b.ch);
ArrayList<Integer> ans1 = new ArrayList<>();
char pre = first;
int i = 0;
while (i < a.size()) {
char x = a.get(i).ch;
int pos = a.get(i).x;
if (x >= first && x <= last) {
ans1.add(pos + 1);
ans += (x - pre);
pre = x;
}
i++;
}
System.out.println(ans + " " + ans1.size());
for (int j = 0; j < ans1.size(); j++) {
System.out.print(ans1.get(j) + " ");
}
System.out.println();
}
else {
char last = s.charAt(n - 1);
char first = s.charAt(0);
ArrayList<pair> a = new ArrayList<>();
for (int i = 0; i < n; i++) {
a.add(new pair(s.charAt(i), i, n - i - 1));
}
int ans = 0;
Collections.sort(a, (aa, b) -> b.ch-aa.ch);
ArrayList<Integer> ans1 = new ArrayList<>();
char pre = first;
int i = 0;
while (i < a.size()) {
char x = a.get(i).ch;
int pos = a.get(i).x;
if (x <= first && x >= last) {
ans1.add(pos + 1);
ans += (x - pre);
pre = x;
}
i++;
}
System.out.println(ans*-1 + " " + ans1.size());
for (int j = 0; j < ans1.size(); j++) {
System.out.print(ans1.get(j) + " ");
}
System.out.println();
}
}
}
//9 4
// 1 4 3 5
// 16 10
// 1 8 3 4 9 5 2 6 7 10
// 1 2
// 1 3
// 0 11
// 1 8 10 4 3 5 7 2 9 6 11
// 3 10
// 1 9 5 4 7 3 8 6 2 10
// 5 2
// 1 2
//
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | d7831e708e3b35090baa09690310a305 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
public static void solve(InputReader in, PrintWriter out) {
int t = in.nextInt();
while (t-- > 0) {
String st = in.next();
int len = st.length();
List<List<Integer>> paths = new ArrayList<>(26);
for (int i = 0; i < 26; i++) {
paths.add(new LinkedList<>());
}
for (int i = 0; i < len; i++) {
paths.get(st.charAt(i) - 'a').add(i + 1);
}
List<Integer> ans = new LinkedList<>();
for (int index: paths.get(st.charAt(0) - 'a')) {
ans.add(index);
}
if (st.charAt(0) < st.charAt(len - 1)) {
for (int i = 0; i < 26; i++) {
if (i > (st.charAt(0) - 'a') && i < (st.charAt(len - 1) - 'a')) {
for (int index: paths.get(i)) {
ans.add(index);
}
}
}
} else if (st.charAt(len - 1) < st.charAt(0)){
for (int i = 25; i >= 0; i--) {
if (i < (st.charAt(0) - 'a') && i > (st.charAt(len - 1) - 'a')) {
for (int index: paths.get(i)) {
ans.add(index);
}
}
}
}
if (st.charAt(len - 1) != st.charAt(0)) {
for (int index : paths.get(st.charAt(len - 1) - 'a')) {
ans.add(index);
}
}
out.println(Math.abs(st.charAt(0) - st.charAt(len - 1)) + " " + ans.size());
for (int path: ans) {
out.print(path + " ");
}
out.println();
}
}
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
solve(in, out);
out.close();
}
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 long nextLong() {
return Long.parseLong(next());
}
String nextLine() throws Exception{
String str = "";
try{
str = reader.readLine();
}catch (IOException e){
throw new Exception(e.toString());
}
return str;
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 13ecca558af8446067bee0c994839024 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class C_1729 {
public static void main(String[] args)
{
FastReader sc = new FastReader();
PrintWriter out=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0) {
String s=sc.nextLine();
int n=s.length();
int arr[][]=new int[n+1][27];
for(int i=1;i<=n;i++) {
int ind=(int)s.charAt(i-1)-96;
arr[i][ind]=i;
}
Arrays.sort(arr, (a, b)->b[0]-a[0]);
int st=s.charAt(0)-96, end=s.charAt(n-1)-96;
int[] res=new int[n];
int res_ind=0;
if(st<end) {
for(int i=st;i<=end;i++) {
for(int j=1;j<=n;j++) {
if(arr[j][i]>0) {
res[res_ind++]=j;
}
}
}
out.println(end-st+" "+res_ind);
for(int i=0;i<res_ind;i++) out.print(res[i]+" ");
}
else {
for(int i=st;i>=end;i--) {
for(int j=1;j<=n;j++) {
if(arr[j][i]>0) {
res[res_ind++]=j;
}
}
}
out.println(st-end+" "+res_ind);
for(int i=0;i<res_ind;i++) out.print(res[i]+" ");
}
out.println();
}
out.flush();
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return (str);
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 0d0ca61e9a5d58af630b772e63c1a2f7 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
public class JumpOnTiles{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0){
t--;
char[] s = sc.next().toCharArray();
int n = s.length;
TreeMap<Integer, ArrayList> map;
String order;
if(s[n-1]>s[0]){
map = new TreeMap<Integer, ArrayList>();
order = "asc";
} else{
map = new TreeMap<Integer, ArrayList>(Collections.reverseOrder());
order = "dsc";
}
int steps=2; int cost=Math.abs(s[0]-s[n-1]); //boolean flag=false; int prev=0;
for(int i=1; i<n-1; i++){
int num = (int)s[i];
if(map.containsKey(num)){
ArrayList<Integer> al = new ArrayList<>();
al = map.get(num);
al.add(i+1);
map.put(num, al);
} else{
ArrayList<Integer> al = new ArrayList<>();
al.add(i+1);
map.put(num, al);
}
if(order.compareTo("asc")==0){
if(num>=s[0] && num<=s[n-1]){
steps++;
}
} else{
if(num<=s[0] && num>=s[n-1]){
steps++;
}
}
}
System.out.println(cost +" "+ steps);
System.out.print(1+" ");
for(Map.Entry<Integer, ArrayList> entry: map.entrySet()){
if(order.compareTo("asc")==0){
if(entry.getKey()>=s[0] && entry.getKey()<=s[n-1]){
for(int i=0; i<entry.getValue().size(); i++){
System.out.print(entry.getValue().get(i)+" ");
}
}
} else{
if(entry.getKey()<=s[0] && entry.getKey()>=s[n-1]){
for(int i=0; i<entry.getValue().size(); i++){
System.out.print(entry.getValue().get(i)+" ");
}
}
}
}
System.out.println(n);
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 05fd099da2d1674e5c3f681ec704b47b | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class E {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int T = in.nextInt();
for(int tt = 0; tt<T; tt++) {
char [] line = in.next().toCharArray();
Pair [] pair = new Pair[26];
for(int i = 0; i<26; i++) {
pair[i] = new Pair();
}
for(int i = 0; i<line.length; i++) {
pair[line[i]-97].indices.add(i);
}
int sz = line.length;
ArrayList<Integer> ans = new ArrayList<>();
if(line[0]<line[sz-1]) {
for(int i = line[0]-97; i<=line[sz-1]-97; i++) {
ans.addAll(pair[i].indices);
}
}else {
for(int i = line[0]-97; i>=line[sz-1]-97; i--) {
ans.addAll(pair[i].indices);
}
}
int minCost = Math.abs((line[0]-97)-(line[sz-1]-97));
int steps = ans.size();
System.out.println(minCost+" "+steps);
for(int a: ans) {
System.out.print(a+1+" ");
}
System.out.println();
}
}
static class Pair{
ArrayList<Integer> indices;
public Pair() {
indices = new ArrayList<>();
}
}
}
| Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | d2b1664255676a63d07080a54efd5581 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.lang.reflect.Parameter;
import java.math.BigInteger;
import java.util.*;
public class codeMaster {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int tc = sc.nextInt();
while(tc-->0){
char[] arr = sc.next().toCharArray();
int n = arr.length;
int ans = Math.abs(arr[0] - arr[n - 1]);
Pair[] pairs = new Pair[n];
for(int i = 0; i<n; i++)pairs[i] = new Pair(arr[i], i + 1);
ArrayList<Pair> steps = new ArrayList<>();
if(arr[0] >= arr[n - 1]){
for(int i = 1; i<n - 1; i++){
if(pairs[i].x <= arr[0] && pairs[i].x >= arr[n - 1]){
steps.add(pairs[i]);
}
}
steps.sort((a, b) -> (arr[0] - a.x) - (arr[0] - b.x));
}else{
for(int i = 1; i<n - 1; i++){
if(pairs[i].x >= arr[0] && pairs[i].x <= arr[n - 1]){
steps.add(pairs[i]);
}
}
steps.sort((a, b) -> (a.x - arr[0]) - (b.x - arr[0]));
}
pw.println(ans + " " + (steps.size() + 2));
pw.print(1 + " ");
for(Pair x: steps)pw.print(x.y + " ");
pw.println(n);
}
pw.flush();
}
static class SegmentTree{
long[] tree;
int N;
public SegmentTree(long[] arr){
N = arr.length;
tree = new long[2*N - 1];
build(tree, arr);
}
public void build(long[] tree, long[] arr){
for(int i = N-1, j = 0; i<tree.length; i++, j++)tree[i] = arr[j];
for(int i = tree.length - 1, j = i - 1, k = N-2; k>=0; i -= 2, j-= 2, k--){
tree[k] = tree[i] + tree[j];
}
}
public void update(int idx, int val){
tree[idx + N - 2] = val;
boolean f = true;
int i = idx + N - 2;
int j = i - 1;
if(i % 2 != 0){
i++;
j++;
}
for(int k = (tree.length - N - 1) - ((tree.length - 1 - i)/2); k>=0; ){
tree[k] = tree[i] + tree[j];
f = !f;
i = k;
j = k - 1;
if(k % 2 != 0){
i++;
j++;
}
k = (tree.length - N - 1) - ((tree.length - 1 - i)/2);
}
}
}
public static boolean isSorted(int[] arr){
boolean f = true;
for(int i = 1; i<arr.length; i++){
if(arr[i] < arr[i - 1]){
f = false;
break;
}
}
return f;
}
public static int binary_Search(long key, long[] arr){
int low = 0;
int high = arr.length;
int mid = (low + high) / 2;
while(low <= high){
mid = low + (high - low) / 2;
if(arr[mid] == key)break;
else if(arr[mid] > key) high = mid - 1;
else low = mid + 1;
}
return mid;
}
public static int differences(int n, int test){
int changes = 0;
while(test > 0){
if(test % 10 != n % 10)changes++;
test/=10;
n/=10;
}
return changes;
}
static int maxSubArraySum(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0,start = 0,
end = 0, s = 0;
for (int i = 0; i < size; i++)
{
max_ending_here += a[i];
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
start = s;
end = i;
}
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return start;
}
static int maxSubArraySum2(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0,start = 0,
end = 0, s = 0;
for (int i = 0; i < size; i++)
{
max_ending_here += a[i];
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
start = s;
end = i;
}
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return end;
}
public static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static class Pair { //implements Comparable<Pair>
char x;
int y;
public Pair(char x, int y){
this.x = x;
this.y = y;
}
// public int compareTo(Pair x){
// if(this.x != x.x)return this.x - x.x;
// else return this.y - x.y;
// }
public String toString(){
return "("+this.x + ", " + this.y + ")";
}
}
public static class Tuple implements Comparable<Tuple>{
int x;
int y;
int z;
public Tuple(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
// public int compareTo(Tuple x){
// if(this.z == x.z){
// return (this.x + this.y) - (x.x + x.y);
// } else return this.z - x.z;
// }
public int compareTo(Tuple x){
if(this.x == x.x){
return this.y - x.y;
} else return this.x - x.x;
}
public String toString(){
return "("+this.x + ", " + this.y + "," + this.z + ")";
}
}
public static class Tuple2 implements Comparable<Tuple2>{
int x;
int y;
int z;
public Tuple2(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
// public int compareTo(Tuple x){
// if(this.z == x.z){
// return (this.x + this.y) - (x.x + x.y);
// } else return this.z - x.z;
// }
public int compareTo(Tuple2 x){
if(this.y == x.y){
return this.x - x.x;
} else return this.y - x.y;
}
public String toString(){
return "("+this.x + ", " + this.y + "," + this.z + ")";
}
}
public static boolean isSubsequence(char[] arr, String s){
boolean ans = false;
for(int i = 0, j = 0; i<arr.length; i++){
if(arr[i] == s.charAt(j)){
j++;
}
if(j == s.length()){
ans = true;
break;
}
}
return ans;
}
public static void sortIdx(long[]a,long[]idx) {
mergesortidx(a, idx, 0, a.length-1);
}
static void mergesortidx(long[] arr,long[]idx,int b,int e) {
if(b<e) {
int m=b+(e-b)/2;
mergesortidx(arr,idx,b,m);
mergesortidx(arr,idx,m+1,e);
mergeidx(arr,idx,b,m,e);
}
return;
}
static void mergeidx(long[] arr,long[]idx,int b,int m,int e) {
int len1=m-b+1,len2=e-m;
long[] l=new long[len1];
long[] lidx=new long[len1];
long[] r=new long[len2];
long[] ridx=new long[len2];
for(int i=0;i<len1;i++) {
l[i]=arr[b+i];
lidx[i]=idx[b+i];
}
for(int i=0;i<len2;i++) {
r[i]=arr[m+1+i];
ridx[i]=idx[m+1+i];
}
int i=0,j=0,k=b;
while(i<len1 && j<len2) {
if(l[i]<=r[j]) {
arr[k++]=l[i++];
idx[k-1]=lidx[i-1];
}
else {
arr[k++]=r[j++];
idx[k-1]=ridx[j-1];
}
}
while(i<len1) {
idx[k]=lidx[i];
arr[k++]=l[i++];
}
while(j<len2) {
idx[k]=ridx[j];
arr[k++]=r[j++];
}
return;
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String 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 | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | 164c5452756db0e5a427a4c61e3b6b67 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String str = br.readLine();
int start = 97, end = 122; //ascii values of a and z
Map<Integer, Stack<Integer>> map = new LinkedHashMap<>();
for (int i = 0; i < str.length(); i++) {
int asciiVal = (int) str.charAt(i);
if (i == 0) start = asciiVal;
else if (i == str.length() - 1) end = asciiVal;
Stack<Integer> indices = null;
if (map.containsKey(asciiVal)) indices = map.get(asciiVal);
else indices = new Stack<>();
indices.push(i + 1);
map.put(asciiVal, indices);
}
//if start and end are equal
if (start == end) {
System.out.println("0 " + (map.get(start).size()));
for (int i : map.get(start)) {
System.out.print(i + " ");
}
System.out.println();
}
//if target is on left hand side
else if (end < start) {
int totalCost = 0, totalJumps = 0, prevVal = start;
StringBuilder ans = new StringBuilder();
for (int i = start; i >= end; i--) {
Stack<Integer> indices = map.get(i);
if (indices == null) continue;
for (int j : indices) {
totalCost += prevVal - i;
totalJumps += 1;
ans.append(j + " ");
prevVal = i;
}
map.remove(i);
}
System.out.println(totalCost + " " + totalJumps);
System.out.println(ans);
}
//if target is on right hand side
else if (start < end) {
int totalCost = 0, totalJumps = 0, prevVal = start;
StringBuilder ans = new StringBuilder();
for (int i = start; i <= end; i++) {
Stack<Integer> indices = map.get(i);
if (indices == null) continue;
for (int j : indices) {
totalCost += i - prevVal;
totalJumps += 1;
ans.append(j + " ");
prevVal = i;
}
map.remove(i);
}
System.out.println(totalCost + " " + totalJumps);
System.out.println(ans);
}
}
br.close();
}
/**
* method to print int value in console output
**/
private static void debug(int value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("int value = " + value);
}
}
/**
* method to print int value in console output with a text message
**/
private static void debug(int value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print long value in console output
**/
private static void debug(long value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("long value = " + value);
}
}
/**
* method to print long value in console output with a text message
**/
private static void debug(long value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print String value in console output
**/
private static void debug(String value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("String value = " + value);
}
}
/**
* method to print String value in console output with a text message
**/
private static void debug(String value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print character value in console output
**/
private static void debug(char value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("Character value = " + value);
}
}
/**
* method to print character value in console output with a text message
**/
private static void debug(char value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print double value in console output
**/
private static void debug(double value) {
if (System.getProperty("ONLINE_JUDGE") == null) {
System.out.println("Double value = " + value);
}
}
/**
* method to print double value in console output with a text message
**/
private static void debug(double value, String message) {
if (System.getProperty("ONLINE_JUDGE") == null) {
if (message.charAt(message.length() - 1) != ' ') message += " ";
System.out.println(message + "" + value);
}
}
/**
* method to print integer type array value in console output
**/
private static void debug(int[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* method to print long type array value in console output
**/
private static void debug(long[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* method to print long type array value in console output
**/
private static void debug(String[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* method to print char type array value in console output
**/
private static void debug(char[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* method to print double type array value in console output
**/
private static void debug(double[] arr) {
if (System.getProperty("ONLINE_JUDGE") == null) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
if (i < n - 1) System.out.print(arr[i] + ", ");
else System.out.print(arr[i]);
}
System.out.println("]");
}
}
/**
* please ignore the below code as it's just used for
* taking faster input in java
*/
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null) return;
din.close();
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 8 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | a15177d02658a9ddcd82fcfe5b5e7343 | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
int t;
t=sc.nextInt();
first:
while(t-->0)
{
String st =sc.next();
char ch[]=st.toCharArray();
ArrayList<Movie> list = new ArrayList<Movie>();
Movie m1=new Movie((int)ch[0],1);
int n=st.length(),len=0;
//for calculating length
if(ch[0]>ch[n-1])
{
for(int i=0;i<n;i++)
if(ch[0]>=ch[i]&&ch[i]>=ch[n-1])
len++;
}
else
{
for(int i=0;i<n;i++)
if(ch[0]<=ch[i]&&ch[i]<=ch[n-1])
len++;
}
for(int i=1;i<st.length()-1;i++)
{
list.add(new Movie((int)ch[i],i+1));
}
Movie m2=new Movie((int)ch[st.length()-1],st.length());
Collections.sort(list);
int s=m1.getName();
int l=m2.getName();
//System.out.println(s+" "+l);
System.out.println(Math.abs(m2.getName()-m1.getName())+" "+len);
if(s>l)
{
int i=list.size()-1;
System.out.print("1 ");
while(i!=-1)
{
Movie m=list.get(i);
i--;
if(m.getName()<=s&&m.getName()>=l)
System.out.print(m.getYear()+" ");
}
System.out.print(ch.length+" ");
}
else
{
int i=0;
System.out.print("1 ");
while(i!=list.size())
{
Movie m=list.get(i);
if(m.getName()>=s&&m.getName()<=l)
System.out.print(m.getYear()+" ");
i++;
}
System.out.print(ch.length+" ");
// System.out.println(s+" "+l);
}
System.out.println();
}
}
}
class Movie implements Comparable<Movie>
{
private int name;
private int year;
// Used to sort movies by year
public int compareTo(Movie m)
{
return this.name - m.name;
}
// Constructor
public Movie(int ch, int yr)
{
this.name = ch;
this.year = yr;
}
// Getter methods for accessing private data
public int getName() { return name; }
public int getYear() { return year; }
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 17 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output | |
PASSED | f94cf325e515e2eb810356edead2d86d | train_110.jsonl | 1662993300 | Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Prog {
public static void main(String[] args) throws Exception {
//FileInputStream inputStream = new FileInputStream("input.txt");
//FileOutputStream outputStream = new FileOutputStream("output.txt");
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver(in, out);
for (int i = 0; i < args.length; i++)
out.println(args[i]);
int t = 1;
//t = in.nextInt();
while (t-- > 0)
solver.solve(1, in, out);
out.close();
}
static class Solver {
static InputReader in;
static PrintWriter out;
Solver(InputReader in, PrintWriter out) {
this.in = in;
this.out = out;
}
static int countS(char i, char j) {
return Math.abs((int)i - (int)j);
}
public class Pair {
public char c;
public int i;
public Pair(char c, int i) {
this.c = c;
this.i = i;
}
}
public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException, CloneNotSupportedException {
int q = in.nextInt();
while (q-- > 0) {
String s = in.next();
int n = s.length();
char f = s.charAt(0), l = s.charAt(n - 1);
int ANS = countS(f, l);
var posSym = new HashMap<Character, ArrayList<Integer>>();
List<Integer> ans = new ArrayList<>();
int d = f < l ? 1 : -1;
for (int i = 0; i < n; i++) {
char ch = s.charAt(i);
posSym.computeIfAbsent(ch, k -> new ArrayList<>());
posSym.get(ch).add(i + 1);
}
for (int i = (int)f; i != d + (int)l; i += d) {
if (posSym.get((char)i) != null)
ans.addAll(posSym.get((char) i));
}
out.println(ANS + " " + ans.size());
for (int i : ans)
out.print(i + " ");
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 long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public float nextFloat() {
return Float.parseFloat(next());
}
}
} | Java | ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"] | 1 second | ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"] | NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$. | Java 17 | standard input | [
"constructive algorithms",
"strings"
] | d17c9f91504e1d4c4eae7294bf09dcfc | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,100 | The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them. | standard output |
Subsets and Splits