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 | b2c7676392cb27f0ff59827c3f87e593 | 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.PrintWriter;
import java.util.*;
public class c {
static int n;
static char[] arr;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
for (int ci = 0; ci < t; ci++) {
StringBuilder sb = new StringBuilder();
arr = in.next().toCharArray();
n = arr.length;
ArrayList<Integer>[] lists = new ArrayList[26];
for (int i = 0; i < 26; i++) lists[i] = new ArrayList<>();
for (int i = 0; i < n; i++) {
lists[arr[i] - 'a'].add(i);
}
ArrayList<Integer> path = new ArrayList<>();
int start = arr[0] - 'a';
int end = arr[n - 1] - 'a';
int dir = start < end ? 1 : -1;
for (int c = start; ; c += dir) {
path.addAll(lists[c]);
if (c == end) break;
}
out.printf("%d %d\n", Math.abs(end - start), path.size());
for (int x : path) out.print((x + 1) + " ");
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 11 | 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 | b6447382de36b2ae6c37b8cf41f13957 | 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 com.rajan.codeforces.contests.contest820;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ProblemC {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int tt = Integer.parseInt(reader.readLine());
while (tt-- > 0) {
String s = reader.readLine();
int n = s.length();
int cost = Math.abs(s.charAt(0) - s.charAt(n - 1));
char first = s.charAt(0), last = s.charAt(n - 1);
List<Integer> path = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (s.charAt(i) >= Math.min(first, last) && s.charAt(i) <= Math.max(last, first)) {
path.add(i);
}
}
if (first <= last)
Collections.sort(path, (x, y) -> Integer.compare(s.charAt(x), s.charAt(y)));
else
Collections.sort(path, (x, y) -> Integer.compare(s.charAt(y), s.charAt(x)));
boolean isFirstUpdated = false, isLastUpdated = false;
for (int i = 0; i < path.size(); i++) {
int j = path.get(i);
if (s.charAt(j) == first && j == 0 && !isFirstUpdated) {
isFirstUpdated = true;
int temp = path.get(0);
path.set(0, j);
path.set(i, temp);
} else if (s.charAt(j) == last && j == n - 1 && !isLastUpdated) {
isLastUpdated = true;
int temp = path.get(path.size() - 1);
path.set(path.size() - 1, j);
path.set(i, temp);
}
}
writer.write(String.format("%d %d\n", cost, path.size()));
int total = 0;
for (int i = 0; i < path.size(); i++) {
writer.write((i == 0 ? "" : " ") + (path.get(i) + 1));
if (i > 0) {
total += Math.abs(s.charAt(path.get(i)) - s.charAt(path.get(i - 1)));
}
}
writer.write("\n");
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 11 | 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 | 7abc404a3ad625e1e7dfe7527f4976af | 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 long mod = 1000000007;
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
int t = sc.nextInt();
while( t-- > 0) {
String str=sc.next();
int minDist=Math.abs(str.charAt(0)-str.charAt(str.length()-1));
int[]chmap=new int[26];
int n=str.length();
int c=0;
for(char ch='a';ch<='z';ch++){
chmap[ch-'a']=c;
c++;
}
HashMap<Character,ArrayList<Integer>>strMap=new HashMap<>();
int[]freq=new int[26];
for(int i=0;i<n;i++){
char ch=str.charAt(i);
if(strMap.containsKey(ch)){
strMap.get(ch).add(i);
}else{
strMap.put(ch,new ArrayList<>());
strMap.get(ch).add(i);
}
freq[ch-'a']++;
}
if(str.charAt(0)>=str.charAt(n-1)){
int st=chmap[str.charAt(0)-'a'];
int ed=chmap[str.charAt(n-1)-'a'];
int steps=0;
ArrayList<Integer>ans=new ArrayList<>();
for(int i=st;i>=ed;i--){
int val=chmap[i];
int fre=freq[val];
char ch=(char)('a'+val);
if(fre>=1){
ArrayList<Integer>idxs=strMap.get(ch);
for(int v:idxs){
ans.add(v+1);
}
}
steps+=fre;
}
out.println(minDist+" "+ans.size());
for(int val:ans){
out.print(val+" ");
}
out.println();
}else{
int st=chmap[str.charAt(0)-'a'];
int ed=chmap[str.charAt(n-1)-'a'];
int steps=0;
ArrayList<Integer>ans=new ArrayList<>();
for(int i=st;i<=ed;i++){
int val=chmap[i];
int fre=freq[val];
char ch=(char)('a'+val);
if(fre>=1){
ArrayList<Integer>idxs=strMap.get(ch);
for(int v:idxs){
ans.add(v+1);
}
}
steps+=fre;
}
out.println(minDist+" "+ans.size());
for(int val:ans){
out.print(val+" ");
}
out.println();
}
}
out.flush();
}
public static class Pair implements Comparable<Pair>{
int val;
int idx;
Pair(int val,int idx){
this.val=val;
this.idx=idx;
}
public int compareTo(Pair o){
if(this.val==o.val){
return this.idx-o.idx;
}else{
return this.val-o.val;
}
}
}
/*
* WARNING -> DONT EVER USE ARRAYS.SORT() IN ANY WAY.
* A B C are easy just dont give up , you can do it!
* FIRST AND VERY IMP -> READ AND UNDERSTAND THE QUESTION VERY CAREFULLY.
* WARNING -> DONT CODE BULLSHIT , ALWAYS CHECK THE LOGIC ON MULTIPLE TESTCASES AND EDGECASES BEFORE.
* SECOND -> TRY TO FIND RELEVENT PATTERN SMARTLY.
* WARNING -> IF YOU THINK YOUR SOLUION IS JUST DUMB DONT SUBMIT IT BEFORE RECHECKING ON YOUR END.
*/
public static boolean ifpowof2(long n ) {
return ((n&(n-1)) == 0);
}
static boolean isprime(long x ) {
if( x== 2) {
return true;
}
if( x%2 == 0) {
return false;
}
for( long i = 3 ;i*i <= x ;i+=2) {
if( x%i == 0) {
return false;
}
}
return true;
}
static boolean[] sieveOfEratosthenes(long n) {
boolean prime[] = new boolean[(int)n + 1];
for (int i = 0; i <= n; i++) {
prime[i] = true;
}
for (long p = 2; p * p <= n; p++) {
if (prime[(int)p] == true) {
for (long i = p * p; i <= n; i += p)
prime[(int)i] = false;
}
}
return prime;
}
public static int[] nextLargerElement(int[] arr, int n) {
Stack<Integer> stack = new Stack<>();
int rtrn[] = new int[n];
rtrn[n-1] = -1;
stack.push( n-1);
for( int i = n-2 ;i >= 0 ; i--){
int temp = arr[i];
int lol = -1;
while( !stack.isEmpty() && arr[stack.peek()] <= temp){
if(arr[stack.peek()] == temp ) {
lol = stack.peek();
}
stack.pop();
}
if( stack.isEmpty()){
if( lol != -1) {
rtrn[i] = lol;
}
else {
rtrn[i] = -1;
}
}
else{
rtrn[i] = stack.peek();
}
stack.push( i);
}
return rtrn;
}
static void mysort(int[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
int loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
static void mySort(long[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
long loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
static long rightmostsetbit(long n) {
return n&-n;
}
static long leftmostsetbit(long n)
{
long k = (long)(Math.log(n) / Math.log(2));
return k;
}
static HashMap<Long,Long> primefactor( long n){
HashMap<Long ,Long> hm = new HashMap<>();
long temp = 0;
while( n%2 == 0) {
temp++;
n/=2;
}
if( temp!= 0) {
hm.put( 2L, temp);
}
long c = (long)Math.sqrt(n);
for( long i = 3 ; i <= c ; i+=2) {
temp = 0;
while( n% i == 0) {
temp++;
n/=i;
}
if( temp!= 0) {
hm.put( i, temp);
}
}
if( n!= 1) {
hm.put( n , 1L);
}
return hm;
}
static ArrayList<Integer> allfactors(int abs) {
HashMap<Integer,Integer> hm = new HashMap<>();
ArrayList<Integer> rtrn = new ArrayList<>();
for( int i = 2 ;i*i <= abs; i++) {
if( abs% i == 0) {
hm.put( i , 0);
hm.put(abs/i, 0);
}
}
for( int x : hm.keySet()) {
rtrn.add(x);
}
if( abs != 0) {
rtrn.add(abs);
}
return rtrn;
}
public static int[][] prefixsum( int n , int m , int arr[][] ){
int prefixsum[][] = new int[n+1][m+1];
for( int i = 1 ;i <= n ;i++) {
for( int j = 1 ; j<= m ; j++) {
int toadd = 0;
if( arr[i-1][j-1] == 1) {
toadd = 1;
}
prefixsum[i][j] = toadd + prefixsum[i][j-1] + prefixsum[i-1][j] - prefixsum[i-1][j-1];
}
}
return prefixsum;
}
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 11 | 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 | 91896a9d5a6f8027ec16b47764cd8cad | 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.sql.Array;
import java.util.StringTokenizer;
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException
{
FastReader in=new FastReader();
int t=in.nextInt();
for(int j=0;j<t;j++)
{
String str=in.next();
char[] arr=str.toCharArray();
int n=str.length();
int cost=Math.abs(arr[0]-arr[n-1]);
ArrayList<Integer> ans=new ArrayList<>();
for(int i=1;i<arr.length-1;i++){
char ch=arr[i];
if(ch>=Math.min(arr[0],arr[n-1]) && ch<=Math.max(arr[0],arr[n-1]))
{
ans.add(i);
}
}
Collections.sort(ans,(x,y)->arr[x]-arr[y]);
if(arr[0]>arr[n-1]) Collections.reverse(ans);
System.out.println(cost+" "+(ans.size()+2));
System.out.print(1+" ");
for(Integer val:ans){
System.out.print((val+1)+" ");
}
System.out.print(arr.length);
System.out.println();
}
}
}
class Node implements Comparable<Node>{
int index;
Character ch;
public Node(int index,char ch)
{
this.index=index+1;
this.ch=ch;
}
@Override
public int compareTo(Node n){
return this.ch-n.ch;
}
}
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 11 | 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 | f9b29247536f86b97e39df7ebea481b2 | 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.sql.Array;
import java.util.StringTokenizer;
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException
{
FastReader in=new FastReader();
int t=in.nextInt();
for(int j=0;j<t;j++)
{
String str=in.next();
char[] arr=str.toCharArray();
int n=str.length();
int cost=Math.abs(arr[0]-arr[n-1]);
PriorityQueue<Node> q=new PriorityQueue<>();
for(int i=1;i<arr.length-1;i++){
char ch=arr[i];
if(ch>=Math.min(arr[0],arr[n-1]) && ch<=Math.max(arr[0],arr[n-1]))
{
Node node=new Node(i,arr[i]);
q.add(node);
}
}
ArrayList<Integer> ans=new ArrayList<>();
while(!q.isEmpty()) ans.add(q.poll().index);
if(arr[0]>arr[n-1]) Collections.reverse(ans);
System.out.println(cost+" "+(ans.size()+2));
System.out.print(1+" ");
for(Integer i:ans){
System.out.print(i+" ");
}
System.out.print(arr.length);
System.out.println();
}
}
}
class Node implements Comparable<Node>{
int index;
Character ch;
public Node(int index,char ch)
{
this.index=index+1;
this.ch=ch;
}
@Override
public int compareTo(Node n){
return this.ch-n.ch;
}
}
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 11 | 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 | 08b2f12725e872f2af6de8bec21fc736 | 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 {
static Scanner sc = new Scanner(System.in);
static HashSet<Character> set;
public static void main(String args[]) {
int t = sc.nextInt();
for(int i=1;i<=t;i++){
solve();
}
}
public static void solve() {
String s = sc.next();
int n = s.length();
int[][] arr = new int[n+1][2];
for(int i=1;i<=n;i++){
char ch = s.charAt(i-1);
arr[i][0]=i;
arr[i][1]=(ch-97+1);
}
Arrays.sort(arr,(a,b) -> a[1]-b[1]);
int numfi = s.charAt(0)-97+1;
int numli = s.charAt(s.length()-1)-97+1;
if(numfi>numli){
Arrays.sort(arr,(a,b) ->a[1]==b[1]?b[0]-a[0]:a[1]-b[1]);
}
List<Integer> seq = new ArrayList<>();
int tiles=0;
for(int i=0;i<n+1;i++){
int idx = arr[i][0];
if(idx==1){
while(i<arr.length){
seq.add(arr[i][0]);
// sum+=arr[i][1]-arr[i-1][1];
if(arr[i][0]==n)break;
tiles++;
i++;
}
}
else if(idx==n){
while(i<arr.length){
seq.add(arr[i][0]);
// sum+=arr[i][1]-arr[i-1][1];
if(arr[i][0]==1)break;
tiles++;
i++;
}
Collections.reverse(seq);
}
}
// System.out.println(numfi+" "+numli);
int val = Math.abs(numfi-numli);
System.out.println(val+" "+(tiles+1));
for(int el:seq){
System.out.print(el+" ");
}
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 11 | 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 | af2a6cb5dacff71644d1b7ffd77bf53f | 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 final PrintWriter out =new PrintWriter(System.out);
static final FastReader sc = new FastReader();
//I invented a new word!Plagiarism!
//Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them
//What do Alexander the Great and Winnie the Pooh have in common? Same middle name.
//I finally decided to sell my vacuum cleaner. All it was doing was gathering dust!
//ArrayList<Integer> a=new ArrayList <Integer>();
//PriorityQueue<Integer> pq=new PriorityQueue<>();
//char[] a = s.toCharArray();
// char s[]=sc.next().toCharArray();
public static boolean sorted(int a[])
{
int n=a.length,i;
int b[]=new int[n];
for(i=0;i<n;i++)
b[i]=a[i];
Arrays.sort(b);
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
return false;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
int tes=sc.nextInt();
while(tes-->0)
{
char s[]=sc.next().toCharArray();
int n=s.length,i,max=s[0],min=s[n-1],j;
System.out.print(Math.abs(max-min)+" ");
ArrayList<Integer> arr=new ArrayList<>();
if(min>max)
{
arr.add(1);
for(i=max;i<=min;i++)
{
for(j=1;j<n-1;j++)
{
int c=s[j];
if(c==i)
arr.add(j+1);
}
}
arr.add(n);
}
else{
arr.add(1);
for(i=max;i>=min;i--)
{
for(j=1;j<n-1;j++)
{
int c=s[j];
if(c==i)
arr.add(j+1);
}
}
arr.add(n);
}
System.out.println(arr.size());
for(int it:arr)
System.out.print(it+" ");
System.out.println();
}
}
public static int first(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x)
return mid;
else if (x > arr.get(mid))
return first(arr, (mid + 1), high, x, n);
else
return first(arr, low, (mid - 1), x, n);
}
return -1;
}
public static int last(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x)
return mid;
else if (x < arr.get(mid))
return last(arr, low, (mid - 1), x, n);
else
return last(arr, (mid + 1), high, x, n);
}
return -1;
}
public static int lis(int[] arr) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>=x) {
al.add(arr[i]);
}else {
int v = upper_bound(al, 0, al.size(), arr[i]);
al.set(v, arr[i]);
}
}
return al.size();
}
public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get((int)mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long mod=1000000007;
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["6\n\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 11 | 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 | 603470f4311043d1578c30458ef9589b | 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 javax.print.attribute.HashAttributeSet;
//import org.graalvm.compiler.phases.graph.FixedNodeProbabilityCache;
//import org.graalvm.compiler.phases.graph.FixedNodeProbabilityCache;
import java.io.*;
import java.math.*;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
public class Main {
private static class MyScanner {
private static final int BUF_SIZE = 2048;
BufferedReader br;
private MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
private boolean isSpace(char c) {
return c == '\n' || c == '\r' || c == ' ';
}
String next() {
try {
StringBuilder sb = new StringBuilder();
int r;
while ((r = br.read()) != -1 && isSpace((char)r));
if (r == -1) {
return null;
}
sb.append((char) r);
while ((r = br.read()) != -1 && !isSpace((char)r)) {
sb.append((char)r);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
static class Reader{
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static long mod = (long)(1e9 + 7);
static void sort(long[] arr ) {
ArrayList<Long> al = new ArrayList<>();
for(long e:arr) al.add(e);
Collections.sort(al);
for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i);
}
static void sort(int[] arr ) {
ArrayList<Integer> al = new ArrayList<>();
for(int e:arr) al.add(e);
Collections.sort(al);
for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i);
}
static void sort(char[] arr) {
ArrayList<Character> al = new ArrayList<Character>();
for(char cc:arr) al.add(cc);
Collections.sort(al);
for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i);
}
static long mod_mul( long... a) {
long ans = a[0]%mod;
for(int i = 1 ; i<a.length ; i++) {
ans = (ans * (a[i]%mod))%mod;
}
return ans;
}
static long mod_sum( long... a) {
long ans = 0;
for(long e:a) {
ans = (ans + e)%mod;
}
return ans;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void print(long[] arr) {
System.out.println("---print---");
for(long e:arr) System.out.print(e+" ");
System.out.println("-----------");
}
static void print(int[] arr) {
System.out.println("---print---");
for(long e:arr) System.out.print(e+" ");
System.out.println("-----------");
}
static boolean[] prime(int num) {
boolean[] bool = new boolean[num];
for (int i = 0; i< bool.length; i++) {
bool[i] = true;
}
for (int i = 2; i< Math.sqrt(num); i++) {
if(bool[i] == true) {
for(int j = (i*i); j<num; j = j+i) {
bool[j] = false;
}
}
}
if(num >= 0) {
bool[0] = false;
bool[1] = false;
}
return bool;
}
static long modInverse(long a, long m)
{
long g = gcd(a, m);
return power(a, m - 2);
}
static long lcm(long a , long b) {
return (a*b)/gcd(a, b);
}
static int lcm(int a , int b) {
return (int)((a*b)/gcd(a, b));
}
static long power(long x, long y){
if(y<0) return 0;
long m = mod;
if (y == 0) return 1; long p = power(x, y / 2) % m; p = (int)((p * (long)p) % m);
if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); }
static class Combinations{
private long[] z; // factorial
private long[] z1; // inverse factorial
private long[] z2; // incerse number
private long mod;
public Combinations(long N , long mod) {
this.mod = mod;
z = new long[(int)N+1];
z1 = new long[(int)N+1];
z[0] = 1;
for(int i =1 ; i<=N ; i++) z[i] = (z[i-1]*i)%mod;
z2 = new long[(int)N+1];
z2[0] = z2[1] = 1;
for (int i = 2; i <= N; i++)
z2[i] = z2[(int)(mod % i)] * (mod - mod / i) % mod;
z1[0] = z1[1] = 1;
for (int i = 2; i <= N; i++)
z1[i] = (z2[i] * z1[i - 1]) % mod;
}
long fac(long n) {
return z[(int)n];
}
long invrsNum(long n) {
return z2[(int)n];
}
long invrsFac(long n) {
return z1[(int)n];
}
long ncr(long N, long R)
{ if(R<0 || R>N ) return 0;
long ans = ((z[(int)N] * z1[(int)R])
% mod * z1[(int)(N - R)])
% mod;
return ans;
}
}
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++) {
parent[i] = i;
}
}
int find(int x)
{
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[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;
else if (rank[yRoot] < rank[xRoot])
parent[yRoot] = xRoot;
else
{
parent[yRoot] = xRoot;
rank[xRoot] = rank[xRoot] + 1;
}
}
}
static int max(int... a ) {
int max = a[0];
for(int e:a) max = Math.max(max, e);
return max;
}
static long max(long... a ) {
long max = a[0];
for(long e:a) max = Math.max(max, e);
return max;
}
static int min(int... a ) {
int min = a[0];
for(int e:a) min = Math.min(e, min);
return min;
}
static long min(long... a ) {
long min = a[0];
for(long e:a) min = Math.min(e, min);
return min;
}
static int[] KMP(String str) {
int n = str.length();
int[] kmp = new int[n];
for(int i = 1 ; i<n ; i++) {
int j = kmp[i-1];
while(j>0 && str.charAt(i) != str.charAt(j)) {
j = kmp[j-1];
}
if(str.charAt(i) == str.charAt(j)) j++;
kmp[i] = j;
}
return kmp;
}
/************************************************ Query **************************************************************************************/
/***************************************** Sparse Table ********************************************************/
static class SparseTable{
private long[][] st;
SparseTable(long[] arr){
int n = arr.length;
st = new long[n][25];
log = new int[n+2];
build_log(n+1);
build(arr);
}
private void build(long[] arr) {
int n = arr.length;
for(int i = n-1 ; i>=0 ; i--) {
for(int j = 0 ; j<25 ; j++) {
int r = i + (1<<j)-1;
if(r>=n) break;
if(j == 0 ) st[i][j] = arr[i];
else st[i][j] = min(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] );
}
}
}
public long gcd(long a ,long b) {
if(a == 0) return b;
return gcd(b%a , a);
}
public long query(int l ,int r) {
int w = r-l+1;
int power = log[w];
return min(st[l][power],st[r - (1<<power) + 1][power]);
}
private int[] log;
void build_log(int n) {
log[1] = 0;
for(int i = 2 ; i<=n ; i++) {
log[i] = 1 + log[i/2];
}
}
}
/******************************************************** Segement Tree *****************************************************/
static class SegmentTree{
long[] tree;
long[] arr;
int n;
SegmentTree(long[] arr){
this.n = arr.length;
tree = new long[4*n+1];
this.arr = arr;
buildTree(0, n-1, 1);
}
void buildTree(int s ,int e ,int index ) {
if(s == e) {
tree[index] = arr[s];
return;
}
int mid = (s+e)/2;
buildTree( s, mid, 2*index);
buildTree( mid+1, e, 2*index+1);
tree[index] = gcd(tree[2*index] , tree[2*index+1]);
}
long query(int si ,int ei) {
return query(0 ,n-1 , si ,ei , 1 );
}
private long query( int ss ,int se ,int qs , int qe,int index) {
if(ss>=qs && se<=qe) return tree[index];
if(qe<ss || se<qs) return (long)(0);
int mid = (ss + se)/2;
long left = query( ss , mid , qs ,qe , 2*index);
long right= query(mid + 1 , se , qs ,qe , 2*index+1);
return min(left, right);
}
public void update(int index , int val) {
arr[index] = val;
update(index , 0 , n-1 , 1);
}
private void update(int id ,int si , int ei , int index) {
if(id < si || id>ei) return;
if(si == ei ) {
tree[index] = arr[id];
return;
}
if(si > ei) return;
int mid = (ei + si)/2;
update( id, si, mid , 2*index);
update( id , mid+1, ei , 2*index+1);
tree[index] = Math.min(tree[2*index] ,tree[2*index+1]);
}
}
/* ***************************************************************************************************************************************************/
// static MyScanner sc = new MyScanner(); // only in case of less memory
static Reader sc = new Reader();
static int TC;
static StringBuilder sb = new StringBuilder();
static PrintWriter out=new PrintWriter(System.out);
public static void main(String args[]) throws IOException {
int tc = 1;
tc = sc.nextInt();
TC = 0;
for(int i = 1 ; i<=tc ; i++) {
TC++;
// sb.append("Case #" + i + ": " ); // During KickStart && HackerCup
TEST_CASE();
}
System.out.print(sb);
}
static void TEST_CASE() {
String str = sc.next();
char[] arr = str.toCharArray();
int n = str.length();
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for(int i = 0 ;i<26 ; i++)
adj.add(new ArrayList<>());
for(int i = 0 ;i<n ; i++) {
adj.get(arr[i]-'a').add(i);
}
ArrayList<Integer> al = new ArrayList<>();
if(arr[0]>arr[n-1]) {
for(int i = arr[0]-'a';i>=arr[n-1]-'a';i--) {
for(int e:adj.get(i)) al.add(e);
}
}else {
for(int i = arr[0]-'a';i<=arr[n-1]-'a';i++) {
for(int e:adj.get(i)) al.add(e);
}
}
long tot = 0;
for(int i = 1 ; i<al.size();i++) tot += Math.abs(arr[al.get(i)] - arr[al.get(i-1)]);
sb.append(tot +" "+al.size()+"\n");
for(int e:al) sb.append((e+1)+" ");
sb.append("\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 11 | 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 | c6ad10781c9e23ec99ece2f6c2398d5f | 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.*;
import static java.lang.Math.*;
public class Main { public static void main(String[] args) { new MainClass().execute(); } }
class MainClass extends PrintWriter {
MainClass() { super(System.out, true); }
boolean cases = true;
// Solution
void solveIt(int testCaseNo) {
char a[] = sc.next().toCharArray();
int n = a.length;
boolean sm = false;
if (a[0] > a[n - 1]) sm = true;
Queue<Integer>[] map = new LinkedList[26];
for (int i = 0; i < 26; i++) { map[i] = new LinkedList<>(); }
for (int i = 0; i < n; i++) map[a[i] - 'a'].add(i);
List<Integer> al = new ArrayList<>();
for (int i = 0;;) {
map[a[i] - 'a'].remove();
al.add(i + 1);
if (i == n - 1) break;
int min = 26;
if (sm) {
for (int j = a[i] - 'a'; j >= 0; j--) {
if (map[j].size() > 0) {
min = j;
break;
}
}
} else {
for (int j = a[i] - 'a'; j < 26; j++) {
if (map[j].size() > 0) {
min = j;
break;
}
}
}
i = map[min].peek();
}
println(abs(a[0] - a[n - 1]) + " " + al.size());
for (int i : al) print(i + " ");
println();
}
void solve() {
int caseNo = 1;
if (cases) for (int T = sc.nextInt(); T > 1; T--, caseNo++) { solveIt(caseNo); }
solveIt(caseNo);
}
void execute() {
long S = System.currentTimeMillis();
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
this.sc = new FastIO();
solve();
out.flush();
long G = System.currentTimeMillis();
sc.tr(G - S + "ms");
}
class FastIO {
private boolean endOfFile() {
if (bufferLength == -1) return true;
int lptr = ptrbuf;
while (lptr < bufferLength) {
// _|_
if (!isThisTheSpaceCharacter(inputBufffferBigBoi[lptr++])) return false;
}
try {
is.mark(1000);
while (true) {
int b = is.read();
if (b == -1) {
is.reset();
return true;
} else if (!isThisTheSpaceCharacter(b)) {
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private byte[] inputBufffferBigBoi = new byte[1024];
int bufferLength = 0, ptrbuf = 0;
private int justReadTheByte() {
if (bufferLength == -1) throw new InputMismatchException();
if (ptrbuf >= bufferLength) {
ptrbuf = 0;
try {
bufferLength = is.read(inputBufffferBigBoi);
} catch (IOException e) {
throw new InputMismatchException();
}
if (bufferLength <= 0) return -1;
}
return inputBufffferBigBoi[ptrbuf++];
}
private boolean isThisTheSpaceCharacter(int c) { return !(c >= 33 && c <= 126); }
private int skipItBishhhhhhh() {
int b;
while ((b = justReadTheByte()) != -1 && isThisTheSpaceCharacter(b));
return b;
}
private double nextDouble() { return Double.parseDouble(next()); }
private char nextChar() { return (char) skipItBishhhhhhh(); }
private String next() {
int b = skipItBishhhhhhh();
StringBuilder sb = new StringBuilder();
while (!(isThisTheSpaceCharacter(b))) {
sb.appendCodePoint(b);
b = justReadTheByte();
}
return sb.toString();
}
private char[] readCharArray(int n) {
char[] buf = new char[n];
int b = skipItBishhhhhhh(), p = 0;
while (p < n && !(isThisTheSpaceCharacter(b))) {
buf[p++] = (char) b;
b = justReadTheByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] readCharMatrix(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++) map[i] = readCharArray(m);
return map;
}
private int[] readIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
private long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
private int nextInt() {
int num = 0, b;
boolean minus = false;
while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if (b == '-') {
minus = true;
b = justReadTheByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = justReadTheByte();
}
}
private long nextLong() {
long num = 0;
int b;
boolean minus = false;
while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if (b == '-') {
minus = true;
b = justReadTheByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = justReadTheByte();
}
}
private void tr(Object... o) {
if (INPUT.length() != 0) System.out.println(Arrays.deepToString(o));
}
}
InputStream is;
PrintWriter out;
String INPUT = "";
final int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE;
final long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE;
final long mod = (long) 1e9 + 7;
FastIO sc;
}
// And I wish you could sing along, But this song is a joke, and the melody I
// wrote, wrong | 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 11 | 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 | c7114e9e3b4f51c1306c18da71ec5917 | 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.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
// int a = 1;
int t;
t = in.nextInt();
// t = 1;
while (t > 0) {
// out.print("Case #"+(a++)+": ");
solver.call(in,out);
t--;
}
out.close();
}
static class TaskA {
public void call(InputReader in, PrintWriter out) {
String s = in.next();
int n = s.length();
answer[] arr = new answer[n];
for (int i = 0; i < n; i++) {
arr[i] = new answer(s.charAt(i), i);
}
Arrays.sort(arr);
int ans = Math.abs(s.charAt(0) - s.charAt(n - 1));
int a = -1, b = -1;
List<Integer> list = new ArrayList<>();
if(s.charAt(0) > s.charAt(n - 1)){
list.add(1);
for (int i = n - 1; i >= 0; i--) {
if(arr[i].a == s.charAt(0)){
a = i;
break;
}
}
for (int i = 0; i < n; i++) {
if(arr[i].a == s.charAt(n - 1)){
b = i;
break;
}
}
for (int i = a; i >=b; i--) {
if(arr[i].b == 0 || arr[i].b == n - 1){
continue;
}
list.add(arr[i].b + 1);
}
list.add(n);
}
else{
for (int i = 0; i < n; i++) {
if(a == -1) {
if (arr[i].a == s.charAt(0)){
a = 1;
}
}
if(a == 1){
if(arr[i].a > s.charAt(n - 1)){
a = -1;
}
}
if(a != -1){
list.add(arr[i].b + 1);
}
}
}
out.println(ans +" "+list.size());
for (int i = 0; i < list.size(); i++) {
out.print(list.get(i) + " ");
}
out.println();
}
}
static long mod = 1_000_000_007;
static long mul(long a, long b) {
return a * b % mod;
}
static long exp(long base, long pow) {
if (pow == 0) return 1;
long half = exp(base, pow / 2);
if (pow % 2 == 0) return mul(half, half);
return mul(half, mul(half, base));
}
static int gcd(int a, int b )
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static class answer implements Comparable<answer>{
int b;
char a;
public answer(char a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer o) {
int d = Character.compare(this.a, o.a);
if(d == 0){
return Integer.compare(this.b, o.b);
}
return d;
}
}
static class answer1 implements Comparable<answer1>{
int a, b, c;
public answer1(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public answer1(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer1 o) {
if(this.b == o.b){
return Integer.compare(this.a, o.a);
}
return Integer.compare(this.b, o.b);
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (Long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static final Random random=new Random();
static void shuffleSort(int[] a) {
int n = a.length;
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 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());
}
}
} | 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 11 | 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 | f70cd9f6c93c498c625bda0a3bcee439 | 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 Gfg
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner ( System.in);
int t=sc.nextInt();
for( int i=0;i<t;i++){
String s=sc.next();
int n=s.length();
int a[]=new int[n];
String alp="abcdefghijklmnopqrstuvwxyz";
for (int j=0;j<n;j++){
for (int k=0;k<alp.length();k++){
if(s.charAt(j)==alp.charAt(k)){
a[j]=k+1;
break;
}
}
}
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
for (int j=0;j<n;j++){
map.put(a[j],new ArrayList<Integer>());
}
for (int j=0;j<n;j++){
map.get(a[j]).add(j+1);
}
int fir=a[0];
int last=a[n-1];
Arrays.sort(a,0,n-1);
int x=0;
int y=0;
int count =1;
int flag=0;
if(fir<last){
for(int j=0;j<n-1;j++){
if (a[j]==fir){
x=j;
flag=1;
break;
}
}
for(int j=x;j<n-1;j++){
if (a[j]<=last){
count+=1;
}
}
}
else if (fir >last){
for (int j=n-1;j>=0;j--){
if (a[j]==fir ){
y=j;
flag=2;
break;
}
}
for (int j=y;j>=0;j--){
if(a[j]>=last){
count+=1;
}
}
}
else {
for (int j=0;j<n-1;j++){
if(a[j]==fir){
count+=1;
}
flag=3;
}
}
int mk=0;
if (fir>=last){
mk=fir-last;
}
else {
mk=last-fir;
}
int c=0;
int d=0;
System.out.println(mk + " " + count);
if (flag==1){
for(int j=x;j<n-1;j++){
if (a[j]<=last){
c=0;
d=j+1;
System.out.print(map.get(a[j]).get(c)+ " ");
while (d<n-1 && a[d]==a[j]){
c=c+1;
System.out.print(map.get(a[j]).get(c)+ " ");
d=d+1;
}
j=d-1;
}
}
System.out.print(n);
}
else if (flag==2){
for (int j=y;j>=0;j--){
if(a[j]>=last){
c=0;
d=j-1;
System.out.print(map.get(a[j]).get(c)+ " ");
while (d>=0 && a[d]==a[j]){
c=c+1;
System.out.print(map.get(a[j]).get(c)+ " ");
d=d-1;
}
j=d+1;
}
}
System.out.print(n);
}
else {
for (int j=0;j<n-1;j++){
if (a[j]==fir){
c=0;
d=j+1;
System.out.print(map.get(a[j]).get(c)+ " ");
while (d<n-1 && a[d]==a[j]){
c=c+1;
System.out.print(map.get(a[j]).get(c)+ " ");
d=d+1;
}
j=d-1;
}
}
System.out.print(n);
}
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 11 | 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 | b0ab595761720126befa927218ce94b5 | 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_JumpingOnTiles {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numCases = sc.nextInt();
for(int currCase = 0; currCase <numCases; currCase++) {
String input = sc.next();
List<Pair> allChars = new ArrayList<>();
char firstChar = input.charAt(0);
char lastChar = input.charAt(input.length()-1);
boolean sortDown = false;
for(int pos = 1; pos<input.length()-1; pos++) {
char posChar = input.charAt(pos);
if(firstChar <= lastChar ) {
if(posChar < firstChar || posChar > lastChar) {
continue;
}
}
if(firstChar > lastChar) {
sortDown = true;
if(posChar > firstChar || posChar < lastChar) {
continue;
}
}
allChars.add(new Pair(posChar, pos+1));
}
Collections.sort(allChars, new Comparator<Pair>() {
@Override
public int compare(Pair o1, Pair o2) {
if(o1.val == o2.val) {
return o1.pos - o2.pos;
}
else {
return o1.val - o2.val;
}
}
});
if(sortDown) {
Collections.reverse(allChars);
}
StringBuilder output = new StringBuilder();
output.append(1).append(" ");
for(Pair aChar : allChars) {
output.append(aChar.pos).append(" ");
}
output.append(input.length()).append(" ");
output.deleteCharAt(output.length()-1);
int minCost = Math.abs((int)(firstChar - lastChar));
int jumps = allChars.size()+2;
System.out.println(minCost + " " + jumps);
System.out.println(output);
}
}
static class Pair {
char val;
int pos;
public Pair(char val, int pos) {
this.val = val;
this.pos = pos;
}
}
}
| 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 11 | 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 | 91e2bcb4fa934d5a400a10bbd7786ff2 | 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;
import java.util.*;
import java.net.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.io.*;
public class A {
static class Node {
int id1;
int id2;
Node(int v1, int w1){
this.id1= v1;
this.id2=w1;
}
Node(){}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
}
static char p='a';
static boolean[] seiveofEratoSthenes(int n) {
boolean[] isPrime= new boolean[n+1];
Arrays.fill(isPrime, true);
isPrime[0]=false;
isPrime[1]= false;
for(int i=2;i*i<=n;i++) {
for(int j=2*i; j<=n;j=j+i) {
isPrime[j]= false;
}
}
return isPrime;
}
static int in = 2;
// Function check whether a number
// is prime or not
public static boolean isPrime(int n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static class SortingComparator implements Comparator<Node>{
@Override
public int compare(Node p1, Node p2) {
// int n = p1.id1-p2.id1;
// if(n!=0)return n;
return p1.id2-p2.id2;
}
}
static int pp =1;
static long[] dp = new long[500001];
public static void main(String[] args) throws UnknownHostException {
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t--!=0) {
String s = sc.next();
HashSet<Character> hs = new HashSet<>();
int n = s.length();
p = s.charAt(0);
char min = (char)Math.min(s.charAt(0), s.charAt(n-1));
char max = (char)Math.max(s.charAt(0), s.charAt(n-1));
ArrayList<Integer> al = new ArrayList<>();
HashMap<Integer, Character> hm = new HashMap<>();
ArrayList<Node> sort = new ArrayList<>();
// System.out.println(Math.abs(Character.getNumericValue(s.charAt(0))-Character.getNumericValue(s.charAt(s.length()-1))));
for(int i=0; i<s.length(); i++) {
hm.put(i, s.charAt(i));
}
for(int i=1; i<s.length()-1; i++) {
if(s.charAt(i)<=max && s.charAt(i)>=min) {
sort.add(new Node(i+1, (int)Math.abs(s.charAt(0)-s.charAt(i))));
}
}
Collections.sort(sort, new SortingComparator());
for(Node e: sort) {
// System.out.println(e.id2);
al.add(e.id1);
}
System.out.println(Math.abs(Character.getNumericValue(s.charAt(0))
-Character.getNumericValue(s.charAt(s.length()-1)))+" "+(al.size()+2));
System.out.print(1+" ");
for(int e: al) {
System.out.print(e+" ");
}
System.out.print(n);
System.out.println();
}
out.close();
}
static long nextPowerOf2(long N)
{
// if N is a power of two simply return it
if ((N & (N - 1)) == 0)
return N;
return 0x4000000000000000L
>> (Long.numberOfLeadingZeros(N) - 2);
}
public static int[] solve(int[] arr, int[] it) {
HashMap<Integer, Integer> hm = new HashMap<>();
for(int i=0; i<arr.length; i++) {
hm.put(arr[i], hm.getOrDefault(arr[i], 0)+1);
}
for(int i=0; i<arr.length; i++) {
it[i]= hm.get(arr[i]);
}
return it;
}
public static void rec(int l, int e, int[] arr) {
if(l>e || e>=arr.length || l<=0)return;
int mid = (e-l+1)%2==0?(e+l-1)/2:(e+l)/2;
if(arr[mid]==0 ) {
arr[mid]=pp;
pp++;
// if(pp>1)return;
}
if(e-mid-1<=mid-l-1) {rec(l, mid-1, arr);
rec(mid+1, e, arr);
}
else {
rec(mid+1, e, arr);
rec(l, mid-1, arr);
}
}
static double fact(double n)
{
int i=1;
double fact=1;
while(i<=n)
{
fact=fact*i;
i++;
}
return fact;
}
static double combination(int n,int r)
{
double com=fact(n)/(fact(n-r)*fact(r));
return com;
}
static boolean digit(long n) {
long ans = n;
while(n>0) {
long rem = n%10;
if(rem!=0 && ans%rem!=0)return false;
n=n/10;
}
return true;
}
static int nCr(int n, int r)
{
return fact(n) / (fact(r) *
fact(n - r));
}
// Returns factorial of n
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l, Collections.reverseOrder());
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static boolean isPalindrome(char[] arr, int i, int j) {
while(i<j) {
if(arr[i]!=arr[j])return false;
i++;
j--;
}
return true;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static int max =0;
static void dfs(int i, boolean[] vis , ArrayList<ArrayList<Integer>> adj) {
max = Math.max(max, i);
vis[i]= true;
for(int e: adj.get(i)) {
if(vis[e]==false) {
dfs(e, vis, adj);
}
}
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static double pow(int a, int b) {
long res= 1;
while(b>0) {
if((b&1)!=0) {
res= (res*a);
}
a= (a*a);
b= b>>1;
}
return res;
}
static void permute(String s , String answer, HashSet<String> hs)
{
if (s.length() == 0)
{
hs.add(answer);
return;
}
for(int i = 0 ;i < s.length(); i++)
{
char ch = s.charAt(i);
String left_substr = s.substring(0, i);
String right_substr = s.substring(i + 1);
String rest = left_substr + right_substr;
permute(rest, answer + ch, hs);
}
}
}
| Java | ["6\n\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 11 | 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 | 2b23d8ddab11c374ba3b8b9d4e7f0141 | 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 c[] = s.toCharArray();
Arrays.sort(c);
int n = s.length();
Map<Character, ArrayList<Integer>> m = new HashMap<>();
for(int i=0;i<n;i++){
m.put(s.charAt(i), new ArrayList<>());
}
for(int i=0;i<n;i++){
m.get(s.charAt(i)).add(i+1);
}
char x = s.charAt(0);
char y = s.charAt(n-1);
if(x>y){
x = s.charAt(n-1);
y = s.charAt(0);
}
ArrayList<Integer> res = new ArrayList<>();
Set<Character> set = new HashSet<>();
for(int i=0;i<n;i++){
if(set.contains(c[i])){
continue;
}
if(m.get(c[i]).contains(1) || m.get(c[i]).contains(n)){
if(s.charAt(0)>s.charAt(n-1)){
Collections.reverse(m.get(c[i]));
}
}
if(c[i]<=y && c[i]>=x){
res.addAll(m.get(c[i]));
}
set.add(c[i]);
}
// System.out.println(res);
System.out.println(y-x+" "+(res.size()));
if(s.charAt(0)>s.charAt(n-1)){
for(int i=res.size()-1;i>=0;i--){
System.out.print(res.get(i)+" ");
}
}
else{
for(int i:res){
System.out.print(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 11 | 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 | 0836cc7e180ca1e4783dec71356d55fd | 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.sql.SQLOutput;
import java.util.*;
public class C {
static class Pair implements Comparable{
char c;
int i;
public Pair(char a, int b){
c=a;
i=b;
}
@Override
public int compareTo(Object o) {
Pair p=(Pair)o;
if(this.c!=p.c) return this.c-p.c;
else return p.i-this.i;
}
}
public static void main(String [] args){
Scanner sc= new Scanner(System.in);
int t=sc.nextInt();
for(;t>0;t--){
ArrayList<Pair> res= new ArrayList();
String s=sc.next();
char o=s.charAt(0);
char f=s.charAt(s.length()-1);
int saltos=0;
for(int i=0;i<s.length()-1;i++){
char c=s.charAt(i);
// System.out.println(o+" "+c+" "+f);
if(i!=0 && ((o<=c && c<=f) || (f<=c && c<=o))){
saltos++;
// System.out.println(i);
res.add(new Pair(c,i+1));
}
}
Collections.sort(res);
System.out.println(Math.abs(o-f)+" "+(saltos+2));
System.out.print(1+" ");
if(o<=f){
for(int i=0;i<saltos;i++){
System.out.print(res.get(i).i+" ");
}
}
else{
for(int i=saltos-1;i>=0;i--){
System.out.print(res.get(i).i+" ");
}
}
System.out.println(s.length());
}
}
}
| 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 11 | 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 | 9df1340c1eb28d3aedc5be29c5cbbc8e | 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 HelloWorld {
// static class Pair{
// char a;
// int index;
// Pair(char a,int index){
// this.a=a;
// this.index=index;
// }
// }
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
String s=sc.next();
char a='a',b='a';
if(s.charAt(0)==s.charAt(s.length()-1)){
//both equal
a=s.charAt(0);
b=s.charAt(s.length()-1);
}else if(s.charAt(0)<s.charAt(s.length()-1)){
//as it is
a=s.charAt(0);
b=s.charAt(s.length()-1);
}else{
b=s.charAt(0);
a=s.charAt(s.length()-1);
}
String x=s.substring(1,s.length()-1);
List<Integer> list=new ArrayList<>();
for(int i=0;i<x.length();i++){
char c=x.charAt(i);
if(a<=c && c<=b){
list.add(i+1);
}
}
Collections.sort(list,(a1,b1)->s.charAt(a1)-s.charAt(b1));
//System.out.println(list);
System.out.print((int)(b-a)+" "+(2+list.size()));
System.out.println();
System.out.print("1 ");
if(s.charAt(0)<s.charAt(s.length()-1)){
for(int i=0;i<list.size();i++){
System.out.print((1+list.get(i))+" ");
}
}else{
for(int i=list.size()-1;i>=0;i--){
System.out.print((1+list.get(i))+" ");
}
}
System.out.print(s.length());
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 11 | 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 | d693bb9f4a21e15b4517d3bb95c4609b | 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.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
public class st48 {
public static void main(String[] args) throws java .lang.Exception{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
int a=Integer.parseInt(br.readLine());
// int x[]= {2020,2021};
// boolean arr[][]=new boolean [1000001][3];
// for(int row=0;row<arr[0].length;row++) {
// for(int col=0;col<arr.length;col++) {
// if(row==0&&col==0) {
// arr[col][row]=false;
// }
// else if(row==0) {
// arr[col][row]=false;
// }
// else if(col==0) {
// arr[col][row]=true;
// }
// else {
// if(col<x[row-1]) {
// arr[col][row]=arr[col][row-1];
// }
// else {
// arr[col][row]=(arr[col][row-1])||(arr[col-x[row-1]][row]);
// }
// }
// }
// }
while(a--!=0) {
//int b=Integer.parseInt(br.readLine());
// int k=Integer.parseInt(br.readLine());
String m1=br.readLine();
//String [] s1=m1.split("\\s");
// int [] arr1=new int [3];
// for(int i=0;i<3;i++) {
// arr1[i]=Integer.parseInt(s1[i]);
//
// }
//
// int k=Integer.parseInt(br.readLine());
// String m2=br.readLine();
// String [] s2=m2.split("\\s");
//
//
// int [] arr2=new int [b];
// for(int i=0;i<b;i++) {
// arr2[i]=Integer.parseInt(s2[i]);
// }
HashMap<Character,Integer> hm=new HashMap<>();
int j=1;
char ch='a';
for(int i=0;i<26;i++) {
int y=(int) ch;
int z=y+i;
char c=(char) z;
hm.put(c,(j+i));
}
char first=m1.charAt(0);
char last=m1.charAt(m1.length()-1);
TreeMap<Character,ArrayList<String>> tm=new TreeMap<>();
if(first>last) {
tm=new TreeMap<>(Collections.reverseOrder());
}
String ans=""+1;
for(int i=0;i<26;i++) {
int k=(int) ch;
k=k+i;
char c=(char) k;
tm.put(c,new ArrayList<>());
}
for(int i=1;i<m1.length()-1;i++) {
char c=m1.charAt(i);
tm.get(c).add(i+1+"");
}
String ans1="";
int jumps=0;
int total=0;
int initial=hm.get(m1.charAt(0));
// System.out.println(tm);
if(last>first) {
for(char c:tm.keySet()) {
if(c>=first&&c<=last) {
int xy=tm.get(c).size();
String s="";
// System.out.println(xy);
int v=hm.get(c);
jumps=jumps+xy;
if(xy>0) {
total=total+Math.abs(initial-v);
initial=v;
s=String.join(" ", tm.get(c));
ans=ans+" "+s;
}
}
}
}
else {
for(char c:tm.keySet()) {
if(c<=first&&c>=last) {
// System.out.println(c);
int xy=tm.get(c).size();
String s="";
int v=hm.get(c);
// System.out.println(xy);
jumps=jumps+xy;
if(xy>0) {
total=total+Math.abs(initial-v);
initial=v;
s=String.join(" ", tm.get(c));
ans1=ans1+" "+s;
}
// System.out.println(total);
}
}
ans=ans+ans1;
}
total=total+Math.abs(initial-hm.get(m1.charAt(m1.length()-1)));
ans=ans+" "+m1.length();
System.out.println(total+" "+(jumps+2));
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 11 | 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 | e857b59554b8268af1b80262eeb4d9f2 | 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 | /***** ---> :) Vijender Srivastava (: <--- *****/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static FastReader sc =new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=(long)32768;
static StringBuilder sb = new StringBuilder();
/* start */
public static void main(String [] args)
{
int testcases = 1;
testcases = i();
// calc();
while(testcases-->0)
{
solve();
}
out.flush();
out.close();
}
static void solve()
{
char ch[] = inputC();
int n = ch.length;
int ans = Math.abs((ch[n-1]-ch[0]));
List<Integer> list = new ArrayList<>();
for (int i = 1; i < n-1; i++) {
if ((ch[i] >= ch[0] && ch[i] <= ch[n-1]) || (ch[i] <= ch[0] && ch[i] >= ch[n-1])) {
list.add(i);
}
}
if (ch[0] < ch[n-1])
Collections.sort(list, (x, y) -> ch[x] - ch[y] );
else
Collections.sort(list, (x, y) -> ch[y] - ch[x] );
pl(ans+" "+(list.size()+2));
p("1 ");
for(int i=0;i<list.size();i++)p((list.get(i)+1)+" ");
pl(n);
}
/* end */
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
// print code start
static void p(Object o)
{
out.print(o);
}
static void pl(Object o)
{
out.println(o);
}
static void pl()
{
out.println("");
}
// print code end //
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static char[] inputC()
{
String s = sc.nextLine();
return s.toCharArray();
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static long[] putL(long a[]) {
long A[]=new long[a.length];
for(int i=0;i<a.length;i++) {
A[i]=a[i];
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static long power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) ;
y = y >> 1;
x = (x * x);
}
return res;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long[] sort(long a[]) {
ArrayList<Long> arr = new ArrayList<>();
for(long i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
static int[] sort(int a[])
{
ArrayList<Integer> arr = new ArrayList<>();
for(Integer i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
//pair class
private static class Pair implements Comparable<Pair> {
long first, second;
public Pair(long f, long s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair p) {
if (first < p.first)
return 1;
else if (first > p.first)
return -1;
else {
if (second > p.second)
return 1;
else if (second < p.second)
return -1;
else
return 0;
}
}
}
// segment t start
static long seg[] ;
static void build(long a[], int v, int tl, int tr) {
if (tl == tr) {
seg[v] = a[tl];
} else {
int tm = (tl + tr) / 2;
build(a, v*2, tl, tm);
build(a, v*2+1, tm+1, tr);
seg[v] = Math.min(seg[v*2] , seg[v*2+1]);
}
}
static long query(int v, int tl, int tr, int l, int r) {
if (l > r || tr < tl)
return Integer.MAX_VALUE;
if (l == tl && r == tr) {
return seg[v];
}
int tm = (tl + tr) / 2;
return (query(v*2, tl, tm, l, min(r, tm))+ query(v*2+1, tm+1, tr, max(l, tm+1), r));
}
static void update(int v, int tl, int tr, int pos, long new_val) {
if (tl == tr) {
seg[v] = new_val;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v*2, tl, tm, pos, new_val);
else
update(v*2+1, tm+1, tr, pos, new_val);
seg[v] = Math.min(seg[v*2] , seg[v*2+1]);
}
}
// segment t end //
} | Java | ["6\n\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 11 | 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 | 7d7e6f3bb56d6abbfb91cd37f72f465b | 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.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class C_Jumping_on_Tiles{
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader() ;
int t = sc.nextInt();
while(t-->0) {
char[] ch = sc.next().toCharArray();
int len = ch.length;
int minCost = Math.abs(ch[len-1] - ch[0]);
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i < len-1; i++)
{
if ((ch[i] >= ch[0] && ch[i] <= ch[len-1]) || (ch[i] <= ch[0] && ch[i] >= ch[len-1]))
{
list.add(i);
}
}
// System.out.println(list);
if (ch[0] < ch[len-1])
Collections.sort(list, (x, y) -> ch[x] - ch[y] );
else
Collections.sort(list, (x, y) -> ch[y] - ch[x] );
// System.out.println(list);
System.out.println(minCost+" "+(list.size()+2));
System.out.print(1+" ");
for (int i=0; i<list.size(); i++)
{
System.out.print((list.get(i)+1)+" ");
}
System.out.println(len);
//HASHMAP MAP NO REPETITION
//HashMap <Long,Long> hm = new HashMap<Long,Long> ();
//h1.put(1,4);
//h1.forEach((k,v)->System.out.println(v+" and "+k));
//System.out.println(h1.getOrDefault(100,1));
// if(Integer.parseInt(buf+s[j])<26)
// {
// buf=s[j]+buf;
// }
// else if(Integer.parseInt(buf+s[j])>26)
// {
// ans+=(char)(Integer.parseInt(buf)+96);
// buf=s[j]+"";
// }
//ARRAYLIST ARRAY ALTERNATIVE
//ArrayList<Long> al = new ArrayList<Long> ();
// ArrayList<ArrayList<Long>> al = new ArrayList<> ();
// for(int i=0;i<3;i++)
// {
// al.add(new ArrayList<>());
// }
//al.add(sc.nextLong());
//al.forEach((x) -> System.out.println(x*x));
//al.removeIf(x->(x%2==0));
//ORDERED IN ASCENDING
//TreeMap <Long,Long> tm = new TreeMap <Long,Long>();
//tm.put(10,8);
//System.out.println(tm.subMap(2,5));
// Start here
//long a = sc.nextLong();
}
}
// static final class Utils {
// private static class Shuffler {
// private static void shuffle(int[] x) {
// final Random r = new Random();
// for (int i = 0; i <= x.length - 2; i++) {
// final int j = i + r.nextInt(x.length - i);
// swap(x, i, j);
// }
// }
// private static void shuffle(long[] x) {
// final Random r = new Random();
// for (int i = 0; i <= x.length - 2; i++) {
// final int j = i + r.nextInt(x.length - i);
// swap(x, i, j);
// }
// }
// private static void swap(int[] x, int i, int j) {
// final int t = x[i];
// x[i] = x[j];
// x[j] = t;
// }
// private static void swap(long[] x, int i, int j) {
// final long t = x[i];
// x[i] = x[j];
// x[j] = t;
// }
// }
// public static void shuffleSort(int[] arr) {
// Shuffler.shuffle(arr);
// Arrays.sort(arr);
// }
// public static void shuffleSort(long[] arr) {
// Shuffler.shuffle(arr);
// Arrays.sort(arr);
// }
// private Utils() {}
// }
static class FastReader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
FastReader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
FastReader(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 {
final byte[] buf = new byte[1024]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
break;
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() throws IOException {
int b;
//noinspection StatementWithEmptyBody
while ((b = read()) != -1 && isSpaceChar(b)) {}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final 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 int[] nextIntArray(int n) throws IOException {
final int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') { c = read(); }
final 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[] nextLongArray(int n) throws IOException {
final long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') { c = read(); }
final 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 {
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 11 | 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 | 1f07a88e6fd99613252872cf87110ac2 | 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.text.DecimalFormat;
import java.util.*;
public class Rough {
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int tc = s.nextInt();
for (int t = 1; t <= tc; t++) {
String a=s.next();
int n=a.length();
int max=Math.max(a.charAt(0), a.charAt(n-1));
int min=Math.min(a.charAt(n-1),a.charAt(0));
ArrayList<pair>ar =new ArrayList<>();
ArrayList<pair1>ar1 =new ArrayList<>();
if(min==a.charAt(0)) {
for(int i=1;i<n-1;i++) {
if(a.charAt(i)>=min&&a.charAt(i)<=max){
ar.add(new pair(a.charAt(i),i+1));
}
}
Collections.sort(ar);
pw.println((max-min)+" "+(ar.size()+2));
pw.print(1+" ");
for(int i=0;i<ar.size();i++)pw.print(ar.get(i).b+" ");
pw.print(n);
pw.println();
}
else {
for(int i=1;i<n-1;i++) {
if(a.charAt(i)>=min&&a.charAt(i)<=max){
ar1.add(new pair1(a.charAt(i),i+1));
}
}
Collections.sort(ar1);
pw.println((max-min)+" "+(ar1.size()+2));
pw.print(1+" ");
for(int i=0;i<ar1.size();i++)pw.print(ar1.get(i).b+" ");
pw.print(n);
pw.println();
}
//for(int i=0;i<ar.size();i++)pw.println((char)ar.get(i).a+" "+ar.get(i).b);
//for(int i=0;i<ar.size();i++)pw.println((char)ar.get(i).a+" "+ar.get(i).b);
}
pw.close();
}
static class pair implements Comparable<pair>{
int a;
int b;
public pair(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(pair o) {
if(this.a!=o.a)return Integer.compare(this.a, o.a);
else if(this.a==o.a)return Integer.compare(this.b, o.b);
return 0;
}
}
static class pair1 implements Comparable<pair1>{
int a;
int b;
public pair1(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(pair1 o) {
if(this.a!=o.a)return -1*Integer.compare(this.a, o.a);
else if(this.a==o.a)return Integer.compare(this.b, o.b);
return 0;
}
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
// LCM
public static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static int len(int x) {
String n = "" + x;
return n.length();
}
static boolean ispalin(String in) {
StringBuilder sb = new StringBuilder(in);
if (sb.reverse().toString().equals(in))
return true;
else
return false;
}
static void maping(String key, int val, HashMap<String, Integer> hm) {
if (hm.containsKey(key)) {
int valu = hm.get(key) + 1;
hm.put(key, valu);
} else
hm.put(key, val);
}
static final Random ran = new Random();
static void sort(int[] ar) {
int n = ar.length;
for (int i = 0; i < n; i++) {
int doer = ran.nextInt(n), temp = ar[doer];
ar[doer] = ar[i];
ar[i] = temp;
}
Arrays.sort(ar);
}
static void sort(long[] ar) {
int n = ar.length;
for (int i = 0; i < n; i++) {
int doer = ran.nextInt(n);
long temp = ar[doer];
ar[doer] = ar[i];
ar[i] = temp;
}
Arrays.sort(ar);
}
} | 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 11 | 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 | 6deb928847f4e843e9c5824211bf80f3 | 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.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
solve();
}
private static void solve() {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
// write any code from here//
// int n = sc.nextInt();
Map<Character, Queue<Integer>> map = new HashMap<>();
String s = sc.nextLine();
int n = s.length();
char chars[] = s.toCharArray();
for (int i = 1; i < n-1; i++) {
Queue<Integer> l = map.getOrDefault(chars[i], new LinkedList<>());
l.add(i + 1);
map.put(chars[i], l);
}
char first = chars[0];
char last = chars[n - 1];
boolean reverse = false;
if (first > last) {
char temp = first;
first = last;
last = temp;
reverse = true;
}
Arrays.sort(chars);
int startIdx = -1, endIdx = -2;
for (int i = 0; i < n; i++) {
if (first == chars[i]) {
startIdx = i;
break;
}
}
for (int i = n - 1; i >= 0; --i) {
if (last == chars[i]) {
endIdx = i;
break;
}
}
int cost = last - first;
List<Integer> ans = new ArrayList<>();
int idx = startIdx+1;
while (idx <= endIdx-1) {
ans.add(map.get(chars[idx]).remove());
idx++;
}
int size = ans.size();
System.out.println(cost + " " + (size+2));
System.out.print(1+" ");
if (reverse) {
for (int i = ans.size() - 1; i >= 0; --i) {
System.out.print(ans.get(i) + " ");
}
} else {
for (int x : ans) {
System.out.print(x + " ");
}
}
System.out.print(n+" ");
System.out.println();
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if (st.hasMoreTokens()) {
str = st.nextToken("\n");
} else {
str = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["6\n\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 11 | 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 | fb5ab203a6400a0861b521f8200d8a9e | 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 Nitin Bhakar
*
*/
import java.io.*;
import java.util.*;
public class Codeforces{
static long mod = 1000000007L;
// map.put(a[i],map.getOrDefault(a[i],0)+1);
// map.putIfAbsent;
static int ninf = Integer.MIN_VALUE;
static int inf = Integer.MAX_VALUE;
static void swap(int[] a,int i,int j) {int temp = a[i]; a[i] = a[j]; a[j] = temp;}
static void priArr(int[] a) {for(int i=0;i<a.length;i++) out.print(a[i] + " ");out.println();}
static long gcd(long a,long b){if(b==0) return a; return gcd(b,a%b);}
static int gcd(int a,int b){ if(b==0) return a;return gcd(b,a%b);}
private static void sieve(boolean[] a, int n) {a[1] = false; for(int i=2;i*i<=n;i++) { if(a[i]) { for(int j=i*i;j<=n;j+=i) a[j] = false;}}}
static MyScanner sc = new MyScanner();
//<----------------------------------------------WRITE HERE------------------------------------------->
static void solve(){
String s = sc.next();
int n = s.length();
Pair[] a = new Pair[n];
Map<Integer,List<Integer>> map = new HashMap<>();
for(int i=1;i<n-1;i++) {
int v = s.charAt(i)-'a';
map.putIfAbsent(v, new ArrayList<>());
map.get(v).add(i);
}
int start = s.charAt(0)-'a';
int end = s.charAt(n-1)-'a';
StringBuilder ans = new StringBuilder();
ans.append(1+" ");
int sum = 0;
int steps = 0;
int prev = start;
if(end>=start) {
for(int i=start;i<=end;i++) {
if(map.containsKey(i)) {
List<Integer> temp = map.get(i);
steps += temp.size();
sum += i-prev;
prev = i;
for(int k: temp) ans.append((k+1)+" ");
}
}
sum += end-prev;
}
else {
for(int i=start;i>=end;i--) {
if(map.containsKey(i)) {
List<Integer> temp = map.get(i);
steps += temp.size();
sum += prev-i;
prev = i;
for(int k: temp) ans.append((k+1)+" ");
}
}
sum += prev-end;
}
out.println(sum+" "+(steps+2));
ans.append(n+" ");
out.println(ans);
}
//<----------------------------------------------WRITE HERE------------------------------------------->
static class Pair implements Comparable<Pair> {
int v1;
int v2;
Pair(int v,int f){
v1 = v;
v2 = f;
}
public int compareTo(Pair p){
return this.v1-p.v1;
}
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
// int t= 1;
while(t-- >0){
solve();
}
// 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();
}
public 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);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 11 | 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 | ec5ed610f1f422691e1755c332c79c56 | 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 {
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static boolean isok(long x, long h, long k) {
long sum = 0;
if (h > k) {
long t1 = h - k;
long t = t1 * k;
sum += (k * (k + 1)) / 2;
sum += t - (t1 * (t1 + 1) / 2);
} else {
sum += (h * (h + 1)) / 2;
}
if (sum < x) {
return true;
}
return false;
}
public static boolean binary_search(long[] a, long k) {
long low = 0;
long high = a.length - 1;
long mid = 0;
while (low <= high) {
mid = low + (high - low) / 2;
if (a[(int) mid] == k) {
return true;
} else if (a[(int) mid] < k) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return false;
}
public static long lowerbound(long a[], long ddp) {
long low = 0;
long high = a.length;
long mid = 0;
while (low < high) {
mid = low + (high - low) / 2;
if (a[(int) mid] == ddp) {
return mid;
}
if (a[(int) mid] < ddp) {
low = mid + 1;
} else {
high = mid;
}
}
// if(low + 1 < a.length && a[(int)low + 1] <= ddp){
// low++;
// }
if (low == a.length && low != 0) {
low--;
return low;
}
if (a[(int) low] > ddp && low != 0) {
low--;
}
return low;
}
public static long upperbound(long a[], long ddp) {
long low = 0;
long high = a.length;
long mid = 0;
while (low < high) {
mid = low + (high - low) / 2;
if (a[(int) mid] <= ddp) {
low = mid + 1;
} else {
high = mid;
}
}
if (low == a.length) {
return a.length - 1;
}
return low;
}
// public static class pair implements Comparable<pair> {
// long w;
// long h;
// public pair(long w, long h) {
// this.w = w;
// this.h = h;
// }
// public int compareTo(pair b) {
// if (this.w != b.w)
// return (int) (this.w - b.w);
// else
// return (int) (this.h - b.h);
// }
// }
public static class pair {
long w;
long h;
public pair(long w, long h) {
this.w = w;
this.h = h;
}
}
public static class trinary {
long a;
long b;
long c;
public trinary(long a, long b, long c) {
this.a = a;
this.b = b;
this.c = c;
}
}
public static long lowerboundforpairs(pair a[], long pr) {
long low = 0;
long high = a.length;
long mid = 0;
while (low < high) {
mid = low + (high - low) / 2;
if (a[(int) mid].w <= pr) {
low = mid + 1;
} else {
high = mid;
}
}
// if(low + 1 < a.length && a[(int)low + 1] <= ddp){
// low++;
// }
// if(low == a.length && low != 0){
// low--;
// return low;
// }
// if(a[(int)low].w > pr && low != 0){
// low--;
// }
return low;
}
public static pair[] sortpair(pair[] a) {
Arrays.sort(a, new Comparator<pair>() {
public int compare(pair p1, pair p2) {
return (int) p1.w - (int) p2.w;
}
});
return a;
}
public static boolean ispalindrome(String s) {
long i = 0;
long j = s.length() - 1;
boolean is = false;
while (i < j) {
if (s.charAt((int) i) == s.charAt((int) j)) {
is = true;
i++;
j--;
} else {
is = false;
return is;
}
}
return is;
}
public static void sort(long[] arr) {
ArrayList<Long> a = new ArrayList<>();
for (long i : arr) {
a.add(i);
}
Collections.sort(a);
for (int i = 0; i < a.size(); i++) {
arr[i] = a.get(i);
}
}
public static void sortForObjecttypes(pair[] arr) {
ArrayList<pair> a = new ArrayList<>();
for (pair i : arr) {
a.add(i);
}
Collections.sort(a, new Comparator<pair>() {
@Override
public int compare(pair a, pair b) {
return (int) (a.h - b.h);
}
});
for (int i = 0; i < a.size(); i++) {
arr[i] = a.get(i);
}
}
public static long power(long base, long pow, long mod) {
long result = base;
long temp = 1;
while (pow > 1) {
if (pow % 2 == 0) {
result = ((result % mod) * (result % mod)) % mod;
pow /= 2;
} else {
temp = temp * base;
pow--;
}
}
result = ((result % mod) * (temp % mod));
// System.out.println(result);
return result;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
void readArr(int[] ar, int n) {
for (int i = 0; i < n; i++) {
ar[i] = nextInt();
}
}
}
public static int bSearchDiff(long[] a, int low, int high) {
int mid = low + ((high - low) / 2);
int hight = high;
int lowt = low;
while (lowt < hight) {
mid = lowt + (hight - lowt) / 2;
if (a[high] - a[mid] <= 5) {
hight = mid;
} else {
lowt = mid + 1;
}
}
return high - lowt + 1;
}
public static void solve(FastReader sc, PrintWriter w) throws Exception {
String s = sc.nextLine();
int[] a = new int[s.length()];
HashMap<Integer, Vector<Integer>> hm = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
a[i] = s.charAt(i) - 'a' + 1;
if (!hm.containsKey(a[i])) {
hm.put(a[i], new Vector<>());
}
Vector<Integer> temp = hm.get(a[i]);
temp.add(i + 1);
hm.put(a[i], temp);
}
Vector<Integer> ans = new Vector<>();
int cost = 0;
ArrayList<pair> al = new ArrayList<>();
for (int i = Math.min(a[0], a[s.length() - 1]); i <= Math.max(a[0], a[s.length() - 1]); i++) {
if (hm.containsKey(i)) {
for (Integer t : hm.get(i)) {
if (a[t - 1] != a[0] && a[t - 1] != a[s.length() - 1]) {
al.add(new pair(a[t - 1], t));
}
ans.add(t);
}
}
}
Collections.sort(al, new Comparator<pair>() {
@Override
public int compare(pair a, pair b) {
return (int) (a.w - b.w);
}
});
for (int i = 1; i < ans.size(); i++) {
cost += (a[ans.get(i) - 1] - a[ans.get(i - 1) - 1]);
}
System.out.println(cost + " " + ans.size());
StringBuilder order = new StringBuilder();
for (Integer k : hm.get(a[0])) {
order.append(k + " ");
}
if (a[0] < a[s.length() - 1]) {
for (int i = 0; i < al.size(); i++) {
order.append(al.get(i).h + " ");
}
} else {
for (int i = al.size() - 1; i >= 0; i--) {
order.append(al.get(i).h + " ");
}
}
if (a[0] != a[s.length() - 1]) {
for (Integer k : hm.get(a[s.length() - 1])) {
order.append(k + " ");
}
}
System.out.println(order.toString());
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter w = new PrintWriter(System.out);
long o = sc.nextLong();
while (o > 0) {
solve(sc, w);
o--;
}
w.close();
}
} | Java | ["6\n\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 11 | 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 | f3806684c9c1e3215036f431ff0d39b6 | 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.*;
import java.sql.Time;
public final class Main{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int mod = 1000000007;
static class Pair {
int a, b;
Pair(int a, int b){
this.a = a;
this.b = b;
}
}
static int inputInteger() throws IOException {
int n = Integer.parseInt(br.readLine().trim());
return n;
}
static int[] inputLine(int n) throws IOException {
String[] arr = br.readLine().trim().split(" ");
int[] curr = new int[n];
for (int i = 0; i < n; i++) {
curr[i] = Integer.parseInt(arr[i]);
}return curr;
}
static int[] inputLine() throws IOException {
String[] arr = br.readLine().trim().split(" ");
int n = arr.length;
int[] curr = new int[n];
for (int i = 0; i < n; i++) {
curr[i] = Integer.parseInt(arr[i]);
}return curr;
}
static String inputString() throws IOException {
return br.readLine().trim();
}
static int binSearchJustGreaterOrEqual(int[] arr, int l, int r, int x){
int res = -1;
while(l<=r){
int mid = l + (r-l)/2;
if(arr[mid] >= x){
res = mid;
r = mid - 1;
}else{
l = mid + 1;
}
}
return res;
}
static int binSearchJustSmallerOrEqual(int[] arr, int l, int r, int x){
int res = -1;
while(l<=r){
int mid = l + (r-l)/2;
if(arr[mid] <= x){
res = mid;
l = mid + 1;
}else{
r = mid - 1;
}
}
return res;
}
static int binSearchJustGreater(int[] arr, int l, int r, int x){
int res = -1;
while(l<=r){
int mid = l + (r-l)/2;
if(arr[mid] > x){
res = mid;
r = mid - 1;
}else{
l = mid + 1;
}
}
return res;
}
static int binSearchJustSmaller(int[] arr, int l, int r, int x){
int res = -1;
while(l<=r){
int mid = l + (r-l)/2;
if(arr[mid] < x){
res = mid;
l = mid + 1;
}else{
r = mid - 1;
}
}
return res;
}
static void solve(int n, int[] arr){
List<Pair> pairs = new ArrayList<>();
for (int i = 0; i < n; i++) {
pairs.add(new Pair(arr[i], i));
}
int min = Math.abs(arr[n-1] - arr[0]);
ArrayList<Integer> res = new ArrayList<>();
if(arr[0] > arr[n-1]){
pairs.sort((a, b) -> (b.a - a.a));
res.add(1);
for(var p : pairs){
if(p.a <= arr[0] && p.a >= arr[n-1]){
if(p.b != 0 && p.b != (n-1))
res.add(p.b+1);
}
}
res.add(n);
}
else if(arr[0] == arr[n-1]){
res.add(1);
for(var p : pairs){
if(p.a == arr[0]){
if(p.b != 0 && p.b != (n-1))
res.add(p.b+1);
}
}
res.add(n);
}
else{
pairs.sort((a, b) -> (a.a - b.a));
res.add(1);
for(var p : pairs){
if(p.a <= arr[n-1] && p.a >= arr[0]){
if(p.b != 0 && p.b != (n-1))
res.add(p.b+1);
}
}
res.add(n);
}
System.out.println(min + " " +res.size());
for (Integer integer : res) {
System.out.print(integer+" ");
}System.out.println();
}
public static void main(String[] args) throws IOException {
int t = 1;
t = inputInteger();
while(t-- != 0){
String str = inputString();
int n = str.length();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
int curr = str.charAt(i) - 'a';
arr[i] = curr+1;
}
solve(n, 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 11 | 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 | c77eff5a0511bc11dbd431da928b7fb6 | 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 com.sun.security.jgss.GSSUtil;
import java.awt.*;
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.util.List;
// Author : Praduman Singh
public class MainClass {
static FastScanner fs;
static FastWriter fw;
static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null;
private static final int[][] kdir = new int[][]{{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}};
private static final int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
private static final int iMax = (int) (1e9 + 100), iMin = (int) (-1e9 - 100);
private static final long lMax = (long) (1e18 + 100), lMin = (long) (-1e18 - 100);
private static final int mod1 = (int) (1e9 + 7);
private static final int mod2 = 998244353;
public static void main(String[] args) throws IOException {
fs = new FastScanner();
fw = new FastWriter();
int t = 1;
t = fs.nextInt();
while (t-- > 0) {
solve();
}
fw.out.close();
}
private static void solve() {
String s = fs.next();
int n = s.length();
int st = s.charAt(0)-'a', en = s.charAt(n-1)-'a';
int x = Math.min(st,en), y = Math.max(st,en);
ArrayList<Integer> stor = new ArrayList<>();
for(int j=1; j<n-1; j++){
int z = s.charAt(j)-'a';
if(z<=y&&z>=x){
stor.add(j+1);
}
}
if(st<en){
Collections.sort(stor, (l,r) -> s.charAt(l-1)-s.charAt(r-1));
} else {
Collections.sort(stor, (r,l) -> s.charAt(l-1)-s.charAt(r-1));
}
fw.out.println((y-x) + " "+ (2+stor.size()));
fw.out.print("1 ");
for(int yu: stor){
fw.out.print(yu+" ");
}
fw.out.println(n);
}
// public static int[] sortArray(int[] arr){
// List<Integer> al = new ArrayList<>();
// for(int val : arr) al.add(val);
// Collections.sort(al);
// int i = 0;
// int[] res = new int[al.size()];
// for(int val : al)res[i++] = val;
// return res;
// }
private static class UnionFind {
private final int[] parent;
private final int[] rank;
UnionFind(int n) {
parent = new int[n + 5];
rank = new int[n + 5];
for (int i = 0; i <= n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
private int find(int i) {
if (parent[i] == i)
return i;
return parent[i] = find(parent[i]);
}
private void union(int a, int b) {
a = find(a);
b = find(b);
if (a != b) {
if (rank[a] < rank[b]) {
int temp = a;
a = b;
b = temp;
}
parent[b] = a;
if (rank[a] == rank[b])
rank[a]++;
}
}
}
private static class Calc_nCr {
private final long[] fact;
private final long[] invfact;
private final int p;
Calc_nCr(int n, int prime) {
fact = new long[n + 5];
invfact = new long[n + 5];
p = prime;
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = (i * fact[i - 1]) % p;
}
invfact[n] = pow(fact[n], p - 2, p);
for (int i = n - 1; i >= 0; i--) {
invfact[i] = (invfact[i + 1] * (i + 1)) % p;
}
}
private long nCr(int n, int r) {
if (r > n || n < 0 || r < 0) return 0;
return (((fact[n] * invfact[r]) % p) * invfact[n - r]) % p;
}
}
private static long gcd(long a, long b) {
return (b == 0 ? a : gcd(b, a % b));
}
private static long lcm(long a, long b) {
return ((a * b) / gcd(a, b));
}
private static long pow(long a, long b, int mod) {
long result = 1;
while (b > 0) {
if ((b & 1L) == 1) {
result = (result * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long ceilDiv(long a, long b) {
return ((a + b - 1) / b);
}
private static long getMin(long... args) {
long min = lMax;
for (long arg : args)
min = Math.min(min, arg);
return min;
}
private static long getMax(long... args) {
long max = lMin;
for (long arg : args)
max = Math.max(max, arg);
return max;
}
private static boolean isPalindrome(String s, int l, int r) {
int i = l, j = r;
while (j - i >= 1) {
if (s.charAt(i) != s.charAt(j))
return false;
i++;
j--;
}
return true;
}
private static List<Integer> primes(int n) {
boolean[] primeArr = new boolean[n + 5];
Arrays.fill(primeArr, true);
for (int i = 2; (i * i) <= n; i++) {
if (primeArr[i]) {
for (int j = i * i; j <= n; j += i) {
primeArr[j] = false;
}
}
}
List<Integer> primeList = new ArrayList<>();
for (int i = 2; i <= n; i++) {
if (primeArr[i])
primeList.add(i);
}
return primeList;
}
private static int noOfSetBits(long x) {
int cnt = 0;
while (x != 0) {
x = x & (x - 1);
cnt++;
}
return cnt;
}
private static boolean isPerfectSquare(long num) {
long sqrt = (long) Math.sqrt(num);
return ((sqrt * sqrt) == num);
}
private static class Pair<U, V> {
private final U first;
private final V second;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return first.equals(pair.first) && second.equals(pair.second);
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
@Override
public String toString() {
return "(" + first + ", " + second + ")";
}
private Pair(U ff, V ss) {
this.first = ff;
this.second = ss;
}
}
private static void randomizeIntArr(int[] arr, int n) {
Random r = new Random();
for (int i = (n - 1); i > 0; i--) {
int j = r.nextInt(i + 1);
swapInIntArr(arr, i, j);
}
}
private static void randomizeLongArr(long[] arr, int n) {
Random r = new Random();
for (int i = (n - 1); i > 0; i--) {
int j = r.nextInt(i + 1);
swapInLongArr(arr, i, j);
}
}
private static void swapInIntArr(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
private static void swapInLongArr(long[] arr, int a, int b) {
long temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
private static int[] readIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = fs.nextInt();
return arr;
}
private static long[] readLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = fs.nextLong();
return arr;
}
private static List<Integer> readIntList(int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(fs.nextInt());
return list;
}
private static List<Long> readLongList(int n) {
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(fs.nextLong());
return list;
}
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner() throws IOException {
if (checkOnlineJudge)
this.br = new BufferedReader(new FileReader("src/input.txt"));
else
this.br = new BufferedReader(new InputStreamReader(System.in));
this.st = new StringTokenizer("");
}
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException err) {
err.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
if (st.hasMoreTokens()) {
return st.nextToken("").trim();
}
try {
return br.readLine().trim();
} catch (IOException err) {
err.printStackTrace();
}
return "";
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
private static class FastWriter {
PrintWriter out;
FastWriter() throws IOException {
if (checkOnlineJudge)
out = new PrintWriter(new FileWriter("src/output.txt"));
else
out = new PrintWriter(System.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 11 | 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 | 91467c4612ca873682583681df97d144 | 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 class Pair
{
int i;
int v;
Pair(int i,int v)
{
this.i=i;
this.v=v;
}
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
scn.nextLine();
for(int w=1;w<=t;w++)
{
String s = scn.nextLine();
int fans = Math.abs((int)s.charAt(s.length()-1)-(int)s.charAt(0));
ArrayList<Pair> ll = new ArrayList<>();
int i=1;
for(char c : s.toCharArray())
{
int val = c-96;
ll.add(new Pair(i,val));
i++;
}
Collections.sort(ll,(a,b)->{
if(a.v!=b.v)return a.v-b.v;
return a.i-b.i;
}
);
int si=-1;
i=0;
for(Pair pp : ll)
{
if(pp.i==1)
{si=i;}
i++;
}
int ei=0;
i=0;
for(Pair pp : ll)
{
if(pp.i==s.length())
{ei=i;}
i++;
}
ArrayList<Integer> ans = new ArrayList<>();
//System.out.println(si+" "+ei);
if(si>ei)
{
ans.add(ll.get(si).i);
for(int a=si+1;a<s.length();a++)
{
if(ll.get(a).v==ll.get(si).v)
{ans.add(ll.get(a).i);}
else
break;
}
for(int a=si-1;a>ei;a--)
{
Pair get = ll.get(a);
ans.add(get.i);
}
for(int a=ei-1;a>=0;a--)
{
if(ll.get(a).v==ll.get(ei).v)
{ans.add(ll.get(a).i);}
else
break;
}
ans.add(ll.get(ei).i);
}
else
{
for(int a=si;a<=ei;a++)
{
Pair get = ll.get(a);
ans.add(get.i);
}
}
System.out.println(fans+" "+ans.size());
for(int j : ans)
{
System.out.print(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 11 | 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 | 7dcf46dec20832fad3acdae740685a9f | 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 minCost;
static int maxJump;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-->0) {
minCost=0;
maxJump=0;
String s = scn.next();
char[] arr = s.toCharArray();
ArrayList<Integer> ans = path(arr);
System.out.println(minCost + " " + maxJump);
for(int x:ans) {
System.out.print(x+" ");
}
System.out.println();
}
}
public static ArrayList<Integer> path(char[] arr) {
boolean[] visited = new boolean[26];
ArrayList<Integer> ans = new ArrayList<>();
HashMap<Character,ArrayList<Integer>> map = new HashMap<>();
for(int i=0;i<arr.length;i++) {
if(!map.containsKey(arr[i])) {
map.put(arr[i],new ArrayList<>());
}
map.get(arr[i]).add(i+1);
}
// int maxJumps=0;
// int minCost =0;
int start = arr[0];
int end = arr[arr.length-1];
//System.out.println(start + " " + end);
//int prev = start;
if(start<=end) {
for(int i=start;i<=end;i++) {
if(map.isEmpty()) {
break;
}
if(map.containsKey((char)i)) {
List<Integer> list = map.get((char)i);
minCost+=i-start;
maxJump+=list.size();
for(int x : list) {
ans.add(x);
}
map.remove((char)i);
start=i;
}
}
}else {
for(int i=start;i>=end;i--) {
if(map.isEmpty()) {
break;
}
if(map.containsKey((char)i)) {
List<Integer> list = map.get((char)i);
minCost+=start-i;
maxJump+=list.size();
for(int x : list) {
ans.add(x);
}
map.remove((char)i);
start=i;
}
}
}
return 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 11 | 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 | 02cd4a173f6ea8bf0a8b1171acffac23 | 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 minCost;
static int maxJumps;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-->0) {
minCost=0;
maxJumps=0;
String s = scn.next();
char[] arr = s.toCharArray();
ArrayList<Integer> ans = path(arr);
System.out.println(minCost + " " + maxJumps);
for(int x:ans) {
System.out.print(x+" ");
}
System.out.println();
}
}
public static ArrayList<Integer> path(char[] arr) {
boolean[] visited = new boolean[26];
ArrayList<Integer> ans = new ArrayList<>();
HashMap<Character,ArrayList<Integer>> map = new HashMap<>();
for(int i=0;i<arr.length;i++) {
if(!map.containsKey(arr[i])) {
map.put(arr[i],new ArrayList<>());
}
map.get(arr[i]).add(i+1);
}
// int maxJumps=0;
// int minCost =0;
int start = arr[0];
int end = arr[arr.length-1];
//System.out.println(start + " " + end);
//int prev = start;
if(start<=end) {
for(int i=start;i<=end;i++) {
if(map.isEmpty()) {
break;
}
if(map.containsKey((char)i)) {
List<Integer> list = map.get((char)i);
minCost+=i-start;
maxJumps+=list.size();
for(int x : list) {
ans.add(x);
}
map.remove((char)i);
start=i;
}
}
}else {
for(int i=start;i>=end;i--) {
if(map.isEmpty()) {
break;
}
if(map.containsKey((char)i)) {
List<Integer> list = map.get((char)i);
minCost+=start-i;
maxJumps+=list.size();
for(int x : list) {
ans.add(x);
}
map.remove((char)i);
start=i;
}
}
}
return 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 11 | 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 | 85cc60224c81d4b06d4c435d0db37265 | 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 ans1 {
// class comp implements Comparator<Pair>{
// public int compare(Pair p1, Pair p2) {
// if (p1.al < p2.al)return 1;
// else if (p1.al > p1.al)return -1;
// return 0;
// }
// }
public static class TreeNode{
int num;
char col;
TreeNode(int num,char col){
this.num = num;
this.col = col;
}
}
public static class Pair{
char al;
int index;
Pair(char al,int index){
this.al = al;
this.index = index;
}
}
public static class Pairs{
int b;
int w;
Pairs(int b,int w){
this.b = b;
this.w = w;
}
}
static class comp implements Comparator<Pair>{
public int compare(Pair p1, Pair p2) {
if (p1.al < p2.al)return 1;
else if (p1.al > p2.al)return -1;
return 0;
}
}
static class comp2 implements Comparator<Pair>{
public int compare(Pair p1, Pair p2) {
if (p1.al > p2.al)return 1;
else if (p1.al < p2.al)return -1;
return 0;
}
}
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
String s = sc.next();
char [] arr = s.toCharArray();
char first = arr[0];
char last = arr[arr.length -1];
int cost = Math.abs(first - last);
PriorityQueue<Pair> pq = new PriorityQueue<>(new comp());
PriorityQueue<Pair> pq2 = new PriorityQueue<>(new comp2());
if(first>last){
for(int i=1;i<arr.length-1;i++){
Pair rem = new Pair(arr[i],i);
if((arr[i]<=first && arr[i]>=last) || (arr[i]>=first && arr[i]<=last)){
pq.add(rem);
}
}
System.out.println(cost + " " + (pq.size() + 2));
int [] ans = new int[pq.size() + 2];
ans[0] = 1;
ans[ans.length-1] = arr.length;
int count = 1;
while(!pq.isEmpty()){
// System.out.println(pq.peek().al + " " + (pq.peek().index + 1));
ans[count] = pq.remove().index + 1;
count++;
}
for(int a:ans)System.out.print(a + " ");
}else{
for(int i=1;i<arr.length-1;i++){
Pair rem = new Pair(arr[i],i);
if((arr[i]<=first && arr[i]>=last) || (arr[i]>=first && arr[i]<=last)){
pq2.add(rem);
}
}
System.out.println(cost + " " + (pq2.size() + 2));
int [] ans = new int[pq2.size() + 2];
ans[0] = 1;
ans[ans.length-1] = arr.length;
int count = 1;
while(!pq2.isEmpty()){
// System.out.println(pq.peek().al + " " + (pq.peek().index + 1));
ans[count] = pq2.remove().index + 1;
count++;
}
for(int a:ans)System.out.print(a + " ");
}
}
}
// public static boolean boundary(int i,int j)
public static void reverseArray(int []arr){
int i=0,j=arr.length-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
public static int CeilIndex(int A[], int l, int r, int key){
while (r - l > 1) {
int m = l + (r - l) / 2;
if (A[m] >= key)
r = m;
else
l = m;
}
return r;
}
static int LongestIncreasingSubsequenceLength(int A[], int size)
{
// Add boundary case, when array size is one
int[] tailTable = new int[size];
int len; // always points empty slot
tailTable[0] = A[0];
len = 1;
for (int i = 1; i < size; i++) {
if (A[i] < tailTable[0])
// new smallest value
tailTable[0] = A[i];
else if (A[i] > tailTable[len - 1])
// A[i] wants to extend largest subsequence
tailTable[len++] = A[i];
else
// A[i] wants to be current end candidate of an existing
// subsequence. It will replace ceil value in tailTable
tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i];
}
return len;
}
// public static boolean recur(int [][] m,ArrayList<Pair> check){
// boolean reached = false;
// for(int k=0;k<check.size();k++){
// int i = check.get(k).i;
// int j = check.get(k).j;
// if(i==0 && j==0)reached = true;
// // System.out.println("i" + i + "j" + j);
// if(!boundary(m, i, j))return false;
// }
// if(reached)return true;
// ArrayList<Pair> temp = new ArrayList<>();
// for(int k=0;k<check.size();k++){
// int i = check.get(k).i;
// int j = check.get(k).j-1;
// Pair ob1 = new temp().new Pair(i,j);
// temp.add(ob1);
// }
// boolean left = recur(m,temp);
// if(left)return true;
// temp = new ArrayList<>();
// for(int k=0;k<check.size();k++){
// int i = check.get(k).i-1;
// int j = check.get(k).j;
// Pair ob1 = new temp().new Pair(i,j);
// temp.add(ob1);
// }
// boolean up = recur(m,temp);
// if(up || left)return true;
// return false;
// }
// public static boolean boundary(int [][] m,int i,int j){
// if(i<0 || j<0 ||i>=m.length || j>m[0].length)return false;
// return true;
// }
}
| 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 11 | 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 | 8642d868d07d96756c55c79f545e5fc6 | 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 math9 {
static Scanner sc = new Scanner(System.in);
public static void solve() {
String s = sc.next();
Map<Integer, List<Integer>> map1 = new HashMap<>();
List<Integer> l = new LinkedList<>();
for (int i = 0; i < s.length(); i++) {
int j = (int) s.charAt(i);
l.add(j);
List<Integer> l1 = map1.getOrDefault(j, new LinkedList<Integer>());
l1.add(i + 1);
map1.put(j, l1);
}
int inc = 0;
if (l.get(0) < l.get(l.size() - 1))
inc = 1;
else
inc = -1;
int count = 0;
StringBuilder str = new StringBuilder("");
int max = Math.max(l.get(0), l.get(l.size() - 1));
int min = Math.min(l.get(0), l.get(l.size() - 1));
for (int i = l.get(0); i >= min && i <= max; i += inc) {
if (map1.containsKey(i)) {
for (int lk : map1.get(i)) {
str.append(lk + " ");
count++;
}
}
}
System.out.println((max - min) + " " + count);
System.out.print(str);
}
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
solve();
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 11 | 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 | 3903019b72a9375af50f0c17a160c8aa | 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.print.attribute.HashDocAttributeSet;
import java.util.*;
import java.util.stream.Collectors;
public class JumpingOnTiles {
private void start(){
Scanner sc= new Scanner(System.in);
int t= sc.nextInt();
sc.nextLine();
// Comparator<Node> asc = new Comparator<Node>() {
// @Override
// public int compare(Node o1, Node o2) {
// return o1.ch - o2.ch;
// }
// };
// Comparator<Node> des = new Comparator<Node>() {
// @Override
// public int compare(Node o1, Node o2) {
// return o2.ch-o1.ch;
// }
// };
while(t-- > 0){
String s = sc.nextLine();
// ArrayList<Node> list = new ArrayList<>();
int n = s.length();
char[] arr = s.toCharArray();
boolean inc = arr[0] <= arr[n-1];
HashMap<Character, List<Integer>> map = new HashMap<>();
for(int i=1;i<n-1;i++){
List<Integer> l;
if(map.containsKey(arr[i])){
l = map.get(arr[i]);
}
else{
l = new ArrayList<>();
}
l.add(i+1);
map.put(arr[i], l);
}
List<Integer> res = new ArrayList<>();
res.add(1);
if(inc){
for(char ch=arr[0]; ch<=arr[n-1];ch++){
if(map.containsKey(ch))
res.addAll(map.get(ch));
}
}
else{
for(char ch=arr[0]; ch>=arr[n-1];ch--){
if(map.containsKey(ch))
res.addAll(map.get(ch));
}
}
res.add(n);
int cost = Math.abs(arr[0]-arr[n-1]);
System.out.println(cost+" "+res.size());
for(int i: res)
System.out.print(i+" ");
System.out.println();
}
}
public static void main(String[] args){
JumpingOnTiles obj = new JumpingOnTiles();
obj.start();
}
}
| 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 11 | 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 | b34b3738ae47f4e71dab3d1df658ea38 | 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 jumpingOnTilesCF {
public static void main (String[]args) {
Scanner scan = new Scanner (System.in);
int testCases = scan.nextInt();
while (testCases > 0) {
String str = scan.next();
int length = str.length();
int firstASCII = (int)str.charAt(0);
int secondASCII = (int)str.charAt(length - 1);
PriorityQueue <Pair> pq = new PriorityQueue <Pair>();
for (int i = 1; i < length - 1; i++) {
int curAscii = (int)str.charAt(i);
int index = i;
boolean good = false;
if (curAscii >= firstASCII && curAscii <= secondASCII) {
good = true;
}
if (curAscii >= secondASCII && curAscii <= firstASCII) {
good = true;
}
if (good) {
Pair addingPair = new Pair (Math.abs(curAscii - firstASCII), index + 1);
pq.add(addingPair);
}
}
int distance = Math.abs(firstASCII - secondASCII);
System.out.println(distance + " " + (pq.size() + 2));
System.out.print("1 ");
while (pq.size() > 0) {
Pair curPair = pq.poll();
int curIndex = curPair.index;
System.out.print(curIndex + " ");
}
System.out.print(length);
System.out.println("");
testCases--;
}
}
private static class Pair implements Comparable<Pair> {
int ascii;
int index;
public Pair (int ascii, int index) {
this.ascii = ascii;
this.index = index;
}
public int compareTo (Pair p) {
return (ascii - p.ascii);
}
}
}
| 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 11 | 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 | 4ba17f9c42e364a78247d473bdb55e9f | 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.nio.Buffer;
import java.util.*;
import javax.lang.model.util.ElementScanner6;
public class codeforces {
static Map<Integer, Integer> joes = new HashMap<>();
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void toString(long[] j)
{
System.out.println(Arrays.toString(j));
}
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int j = Integer.parseInt(line);
while(j>0)
{
StringBuilder bro = new StringBuilder();
line = br.readLine();
bro.append(Math.abs(line.charAt(0)-line.charAt(line.length()-1)));
char firstLetter = line.charAt(0);
char lastLetter = line.charAt(line.length()-1);
if(firstLetter<=lastLetter)
{
int count = 0;
StringBuilder bruhh = new StringBuilder();
for(char lessThan = firstLetter; lessThan<=lastLetter; lessThan++)
{
int inde1 = line.indexOf(lessThan);
while(inde1>=0)
{
bruhh.append(inde1 +1 + " ");
count++;
inde1 = line.indexOf(lessThan, inde1+1);
}
}
bro.append(" " + count);
System.out.println(bro);
System.out.println(bruhh);
}
else
{
int count = 0;
StringBuilder bruhh = new StringBuilder();
for(char lessThan = firstLetter; lessThan>=lastLetter; lessThan--)
{
int inde1 = line.indexOf(lessThan);
while(inde1>=0)
{
bruhh.append(inde1 +1 + " ");
count++;
inde1 = line.indexOf(lessThan, inde1+1);
}
}
bro.append(" " + count);
System.out.println(bro);
System.out.println(bruhh);
}
j--;
}
}
}
/*int[] bruh = new int[j*2];
int i = 0;
while(j>0)
{
line = br.readLine();
values = line.split(" ");
joes.put(Integer.parseInt(values[0]),Integer.parseInt(values[2]));
joes.put(Integer.parseInt(values[1]),-Integer.parseInt(values[2]));
bruh[i] = Integer.parseInt(values[0]);
i++;
bruh[i] = Integer.parseInt(values[1]);
i++;
j--;
}
Arrays.sort(bruh);
int sum = 0;
int max = 0;
for(int k = 0; k<bruh.length; k++)
{
sum+=joes.get(bruh[k]);
max = Math.max(sum, max);
}
System.out.println(max); */
/*BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
Map<Integer, Integer> joes = new HashMap<>();
String[] values = line.split(" ");
int j = Integer.parseInt(values[0]);
int m = 3;
int[] bruh = new int[j];
line = br.readLine();
values= line.split(" ");
int[] finale = new int[j];
for(int i = 0; i<j; i++)
{
bruh[i] = Integer.parseInt(values[i]);
}
line = br.readLine();
values= line.split(" ");
for(int k = 0; k<j; k++)
{
finale[k] = Integer.parseInt(values[k]);
}
while(m>0)
{
int[] man = new int[j];
for(int z = 0; z<bruh.length; z++)
{
man[z] = finale[bruh[z]-1];
}
finale = man;
m--;
}
for(int y = 0; y<finale.length; y++)
{
System.out.println(finale[y]);
} */ | Java | ["6\n\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 11 | 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 | 74c45ab0addd236a7d7a2aa57b040cc6 | 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 practice {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new PrintWriter(System.out)));
StringBuilder sb = new StringBuilder();
int t = Integer.parseInt(br.readLine());
while (t --> 0) {
String s = br.readLine();
int n = s.length();
int arr[][] = new int[n][2];
for (int i = 0; i < n; i++) {
arr[i][0] = s.charAt(i) - 'a';
arr[i][1] = i;
}
List<Integer> list = new ArrayList<>();
if (s.charAt(0) <= s.charAt(n - 1)) {
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? a[0] - b[0] : a[0] - b[0]);
for (int i = 0; i < n; i++) {
if (arr[i][1] == 0) {
for (int j = i; j < n; j++) {
list.add(arr[j][1] + 1);
if (arr[j][1] == n - 1) break;
}
}
}
} else {
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
for (int i = n - 1; i >= 0; i--) {
if (arr[i][1] == 0) {
for (int j = i; j >= 0; j--) {
list.add(arr[j][1] + 1);
if (arr[j][1] == n - 1) break;
}
}
}
}
sb.append(Math.abs((s.charAt(0) - 'a' + 1) - (s.charAt(n - 1) - 'a' + 1))).append(" ").append(list.size()).append("\n");
for (int x : list) sb.append(x).append(" ");
sb.append("\n");
}
pw.println(sb.toString().trim());
pw.close();
br.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 11 | 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 | 12660d1889806a8a2c012d7905daaef4 | 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 codeforces_solution{
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 st = s.charAt(0)-'a', en = s.charAt(n-1)-'a';
int x = Math.min(st,en), y = Math.max(st,en);
ArrayList<Integer> stor = new ArrayList<>();
for(int j=1; j<n-1; j++){
int z = s.charAt(j)-'a';
if(z<=y&&z>=x){
stor.add(j+1);
}
}
if(st<en){
Collections.sort(stor, (l,r) -> s.charAt(l-1)-s.charAt(r-1));
} else {
Collections.sort(stor, (r,l) -> s.charAt(l-1)-s.charAt(r-1));
}
System.out.println((y-x) + " "+ (2+stor.size()));
System.out.print("1 ");
for(int yu: stor){
System.out.print(yu+" ");
}
System.out.println(n);
}
}
}
/*
System.out.print();
Collections.sort(stor, (r,l) -> s.charAt(l-1)-s.charAt(r-1));
// to sort numbers aphabetically
*/
| 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 11 | 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 | b74f720fdd2bf11d1914f9faf71c1888 | 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 long mod = (int)1e9+7;
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
int t=sc.nextInt();
while(t-->0)
{
String str=sc.next();
HashMap<Character,ArrayList<Integer>> map=new HashMap<>();
for(int i=0;i<str.length();i++) {
char ch=str.charAt(i);
if(!map.containsKey(ch)) {
map.put(ch, new ArrayList<>());
}
map.get(ch).add(i+1);
}
boolean f=true;
if((str.charAt(0)-'a')>(str.charAt(str.length()-1)-'a')) {
f=false;
}
if(f) {
int ans=0;
int count=0;
int prev=0;
int sa=str.charAt(0);
prev=sa;
int da=str.charAt(str.length()-1);
ArrayList<Integer> list=new ArrayList<>();
while(sa <=da) {
char ch=(char)(sa);
if(map.containsKey(ch)) {
count+=map.get(ch).size();
for(int val:map.get(ch)) {
list.add(val);
}
ans+=ch-prev;
prev=ch;
}
sa++;
}
//count+=map.get(str.charAt(0)).size();
out.println(ans+" "+count);
for(int idx:list) {
out.print(idx+" ");
}
out.println();
}
else {
int ans=0;
int count=0;
int prev=0;
int sa=str.charAt(0);
prev=sa;
int da=str.charAt(str.length()-1);
ArrayList<Integer> list=new ArrayList<>();
while(sa >=da) {
char ch=(char)(sa);
if(map.containsKey(ch)) {
count+=map.get(ch).size();
for(int val:map.get(ch)) {
list.add(val);
}
ans+=prev-ch;
prev=ch;
}
sa--;
}
//count+=map.get(str.charAt(0)).size();
out.println(ans+" "+count);
for(int idx:list) {
out.print(idx+" ");
}
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());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
static class FenwickTree
{
//Binary Indexed Tree
//1 indexed
public int[] tree;
public int size;
public FenwickTree(int size)
{
this.size = size;
tree = new int[size+5];
}
public void add(int i, int v)
{
while(i <= size)
{
tree[i] += v;
i += i&-i;
}
}
public int find(int i)
{
int res = 0;
while(i >= 1)
{
res += tree[i];
i -= i&-i;
}
return res;
}
public int find(int l, int r)
{
return find(r)-find(l-1);
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
private static long mergeAndCount(int[] arr, int l,
int m, int r)
{
// Left subarray
int[] left = Arrays.copyOfRange(arr, l, m + 1);
// Right subarray
int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l;long swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swaps += (m + 1) - (l + i);
}
}
while (i < left.length)
arr[k++] = left[i++];
while (j < right.length)
arr[k++] = right[j++];
return swaps;
}
// Merge sort function
private static long mergeSortAndCount(int[] arr, int l,
int r)
{
// Keeps track of the inversion count at a
// particular node of the recursion tree
long count = 0;
if (l < r) {
int m = (l + r) / 2;
// Total inversion count = left subarray count
// + right subarray count + merge count
// Left subarray count
count += mergeSortAndCount(arr, l, m);
// Right subarray count
count += mergeSortAndCount(arr, m + 1, r);
// Merge count
count += mergeAndCount(arr, l, m, r);
}
return count;
}
static void my_sort(long[] arr)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list);
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static void reverse_sorted(int[] arr)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list , Collections.reverseOrder());
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static int LowerBound(int a[], int x) { // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value
int l=-1,r=list.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(list.get(m)<=x) l=m;
else r=m;
}
return l+1;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static class Queue_Pair implements Comparable<Queue_Pair> {
int first , second;
public Queue_Pair(int first, int second) {
this.first=first;
this.second=second;
}
public int compareTo(Queue_Pair o) {
return Integer.compare(o.first, first);
}
}
static void leftRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
static void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n-1] = temp;
}
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters to compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
static boolean palindrome_array(char arr[], int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
return false;
else
return true;
}
static boolean allElementsEqual(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
static boolean allElementsDistinct(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
public static void reverse_Long(long[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
long temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
static boolean isSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static boolean isReverseSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] < a[i + 1]) {
return false;
}
}
return true;
}
static int[] rearrangeEvenAndOdd(int arr[], int n)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
int[] array = list.stream().mapToInt(i->i).toArray();
return array;
}
static long[] rearrangeEvenAndOddLong(long arr[], int n)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
long[] array = list.stream().mapToLong(i->i).toArray();
return array;
}
static boolean isPrime(long n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (long i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static long getSum(long n)
{
long sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcdLong(long a, long b)
{
if (b == 0)
return a;
return gcdLong(b, a % b);
}
static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static int countDigit(int n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
}
| 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 11 | 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 | 3c71d414c165fdee4f8664b432f621c5 | 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.image.AreaAveragingScaleFilter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.net.http.HttpClient;
import java.util.*;
public class C {
static class Pair1{
Character key=0;
Integer val=0;
public Pair1(char key,int val){
this.key=key;
this.val=val;
}
}
static class SortPair implements Comparator<Pair1>{
@Override
public int compare(Pair1 o1, Pair1 o2) {
return o1.key-o2.key;
}
}
public static void main(String[] args) {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
while (t-- > 0) {
char ch[]= fs.next().toCharArray();
ArrayList<Pair1> al=new ArrayList<>();
int n=ch.length;
int first=ch[0]-'0';
int last=ch[n-1]-'0';
int max=Math.max(first,last);
int min=Math.min(last,first);
int cost=Math.abs(last-first);
int cnt=2;
for(int i=1;i<ch.length-1;i++){
int val=ch[i]-'0';
if(val>=min && val<=max){
al.add(new Pair1(ch[i],i+1));
cnt++;
}
}
Collections.sort(al,new SortPair());
if(min==last)
Collections.reverse(al);
out.println(cost+" "+(cnt));
out.print(1+" ");
for(Pair1 i:al)
out.print(i.val+" ");
out.print(n);
out.println();
}
out.close();
}
/* HELPER FUNCTION's */
static int floorSearch(int arr[], int low, int high, int x)
{
if (low > high)
return 0;
if (x >= arr[high])
return high;
int mid = (low + high) / 2;
if (arr[mid] == x)
return mid;
if (mid > 0 && arr[mid - 1] <= x && x < arr[mid])
return mid - 1;
if (x < arr[mid])
return floorSearch(arr, low, mid - 1, x);
return floorSearch(arr, mid + 1, high, x);
}
static int ceilSearch(int arr[], int low, int high, int x)
{
int i;
if(x <= arr[low])
return low;
for(i = low; i < high; i++)
{
if(arr[i] == x)
return i;
if(arr[i] < x && arr[i+1] >= x)
return i+1;
}
return arr.length-1;
}
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 void sortL(long[] a,int x) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
if(x==0) {
Collections.sort(l);
}
if(x==1){
Collections.sort(l,Collections.reverseOrder());
}
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
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;
}
/* fast exponentiation */
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));
}
/* end of fast exponentiation */
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]));
}
/* sort ascending and descending both */
static void sort(int[] a,int x) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
if(x==0) {
Collections.sort(l);
}
if(x==1){
Collections.sort(l,Collections.reverseOrder());
}
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
/* sort String acc. to character */
static String sortString(String s){
char ch[]=s.toCharArray();
Arrays.sort(ch);
String s1=String.valueOf(ch);
return s1;
}
// lcm(a,b) * gcd(a,b) = a * b
public static long _lcm(long a, long b) {
return (a / _gcd(a, b)) * b;
}
// euclidean algorithm time O(max (loga ,logb))
public static long _gcd(long a, long b) {
while (a > 0) {
long x = a;
a = b % a;
b = x;
}
return b;
}
/* Pair Class implementation */
// static class Pair<K, V> {
// K ff;
// V ss;
//
// public Pair(K ff, V ss) {
// this.ff = ff;
// this.ss = ss;
// }
//
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (o == null || this.getClass() != o.getClass()) return false;
// Pair<?, ?> pair = (Pair<?, ?>) o;
// return ff.equals(pair.ff) && ss.equals(pair.ss);
// }
//
// @Override
// public int hashCode() {
// return Objects.hash(ff, ss);
// }
//
// @Override
// public String toString() {
// return ff.toString() + " " + ss.toString();
// }
//
//
// }
/* pair class ends here */
/* fast input output class */
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[] readArrayL(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| 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 11 | 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 | a57b4ebefd227cb4a11234ea7598aee9 | 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.image.AreaAveragingScaleFilter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.net.http.HttpClient;
import java.util.*;
public class C {
static class Pair1{
Character key=0;
Integer val=0;
public Pair1(char key,int val){
this.key=key;
this.val=val;
}
}
static class SortPair implements Comparator<Pair1>{
@Override
public int compare(Pair1 o1, Pair1 o2) {
return o1.key-o2.key;
}
}
public static void main(String[] args) {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
while (t-- > 0) {
// int n = fs.nextInt();
String s=fs.next();
char ch[]=s.toCharArray();
check(ch);
}
out.close();
}
static void check(char ch[]){
ArrayList<Pair1> al=new ArrayList<>();
int n=ch.length;
int first=ch[0]-'0';
int last=ch[n-1]-'0';
int max=Math.max(first,last);
int min=Math.min(last,first);
int cost=Math.abs(last-first);
int cnt=2;
for(int i=1;i<ch.length-1;i++){
int val=ch[i]-'0';
if(val>=min && val<=max){
al.add(new Pair1(ch[i],i+1));
cnt++;
}
}
Collections.sort(al,new SortPair());
if(min==last)
Collections.reverse(al);
System.out.println(cost+" "+(cnt));
System.out.print(1+" ");
for(Pair1 i:al)
System.out.print(i.val+" ");
System.out.print(n);
System.out.println();
}
/* HELPER FUNCTION's */
static int floorSearch(int arr[], int low, int high, int x)
{
if (low > high)
return 0;
if (x >= arr[high])
return high;
int mid = (low + high) / 2;
if (arr[mid] == x)
return mid;
if (mid > 0 && arr[mid - 1] <= x && x < arr[mid])
return mid - 1;
if (x < arr[mid])
return floorSearch(arr, low, mid - 1, x);
return floorSearch(arr, mid + 1, high, x);
}
static int ceilSearch(int arr[], int low, int high, int x)
{
int i;
if(x <= arr[low])
return low;
for(i = low; i < high; i++)
{
if(arr[i] == x)
return i;
if(arr[i] < x && arr[i+1] >= x)
return i+1;
}
return arr.length-1;
}
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 void sortL(long[] a,int x) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
if(x==0) {
Collections.sort(l);
}
if(x==1){
Collections.sort(l,Collections.reverseOrder());
}
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
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;
}
/* fast exponentiation */
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));
}
/* end of fast exponentiation */
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]));
}
/* sort ascending and descending both */
static void sort(int[] a,int x) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
if(x==0) {
Collections.sort(l);
}
if(x==1){
Collections.sort(l,Collections.reverseOrder());
}
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
/* sort String acc. to character */
static String sortString(String s){
char ch[]=s.toCharArray();
Arrays.sort(ch);
String s1=String.valueOf(ch);
return s1;
}
// lcm(a,b) * gcd(a,b) = a * b
public static long _lcm(long a, long b) {
return (a / _gcd(a, b)) * b;
}
// euclidean algorithm time O(max (loga ,logb))
public static long _gcd(long a, long b) {
while (a > 0) {
long x = a;
a = b % a;
b = x;
}
return b;
}
/* Pair Class implementation */
// static class Pair<K, V> {
// K ff;
// V ss;
//
// public Pair(K ff, V ss) {
// this.ff = ff;
// this.ss = ss;
// }
//
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (o == null || this.getClass() != o.getClass()) return false;
// Pair<?, ?> pair = (Pair<?, ?>) o;
// return ff.equals(pair.ff) && ss.equals(pair.ss);
// }
//
// @Override
// public int hashCode() {
// return Objects.hash(ff, ss);
// }
//
// @Override
// public String toString() {
// return ff.toString() + " " + ss.toString();
// }
//
//
// }
/* pair class ends here */
/* fast input output class */
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[] readArrayL(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| 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 11 | 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 | f088f7de00682ddb9e450121981f57c8 | 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 tc=sc.nextInt();
for(int i=0; i<tc; i++) {
HashMap<Integer,List<Integer>> map=new HashMap<>();
String s=sc.next();
int start=s.charAt(0)-'0';
int end=s.charAt(s.length()-1)-'0';
int diff=start-end;
int jumps=0;
for(int j=0; j<s.length(); j++) {
int curr=(int)s.charAt(j)-'0';
if((diff>=0 && (curr<=start && curr>=end)) || (diff<0 && (curr>=start && curr<=end))) {
if(map.get(curr)==null) {
List<Integer> tempL=new ArrayList<>();
tempL.add(j+1);
map.put(curr, tempL);
}
else map.get(curr).add(j+1);
jumps++;
}
}
System.out.println(Math.abs(diff)+" "+jumps);
if(diff>=0) {
for(int j=start; j>=end; j--) {
if(map.get(j)!=null) {
List<Integer> temp=map.get(j);
for(Integer k:temp) System.out.print(k+" ");
}
}
}
else {
for(int j=start; j<=end; j++) {
if(map.get(j)!=null) {
List<Integer> temp=map.get(j);
for(Integer k:temp) System.out.print(k+" ");
}
}
}
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 11 | 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 | 412872ce1f2db34c1611f4310785e04c | 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 JumpingOnTiles {
// For fast input output
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
try {
br = new BufferedReader(new FileReader("input.txt"));
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
} catch (Exception e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// end of fast i/o code
public static void main(String[] args) {
FastReader s = new FastReader();
int t = s.nextInt();
while (t-- > 0) {
String st = s.next();
int n = st.length();
HashMap<Character, ArrayList<Integer>> map = new HashMap<>();
for (int i = 0; i < n; i++) {
char ch = st.charAt(i);
if (map.containsKey(ch)) {
ArrayList<Integer> list = map.get(ch);
list.add(i + 1);
map.put(ch, list);
} else {
ArrayList<Integer> list = new ArrayList<>();
list.add(i + 1);
map.put(ch, list);
}
}
int cost = Math.abs(st.charAt(0) - st.charAt(n - 1));
char ch1 = st.charAt(0);
char ch2 = st.charAt(n - 1);
ArrayList<Integer> ans = new ArrayList<>();
if (ch1 <= ch2) {
for (char c = ch1; c <= ch2; c++) {
if (map.containsKey(c)) {
for (int i : map.get(c)) {
ans.add(i);
}
}
}
} else {
// System.out.println(ch2 + " " + ch1);
for (char c = ch1; c >= ch2; c--) {
// System.out.println(c);
if (map.containsKey(c)) {
for (int i : map.get(c)) {
ans.add(i);
}
}
}
//Collections.reverse(ans);
}
System.out.println(cost + " " + ans.size());
for (int i : ans) {
System.out.print(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 11 | 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 | 6c32e4effdb451486924e89969bede51 | 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 Answer {
public static int BinarySearchFirst(Integer[] a, int val) {
int l = 0;
int r = a.length-1;
int mid, ans = 0;
while(l <= r) {
mid = l + (r-l)/2;
if(a[mid] >= val) {
if(a[mid] == val) {
ans = mid;
}
r = mid-1;
}else {
l = mid+1;
}
}
return ans;
}
public static int BinarySearchLast(Integer[] a, int val) {
int l = 0;
int r = a.length-1;
int mid, ans = 0;
while(l <= r) {
mid = l + (r-l)/2;
if(a[mid] <= val) {
if(a[mid] == val) {
ans = mid;
}
l = mid+1;
}else {
r = mid-1;
}
}
return ans;
}
static class Element implements Comparable<Element>{
char c;
int val;
public Element(char c, int val) {
this.c = c;
this.val = val;
}
public String toString() {
return c + " " + val;
}
public int compareTo(Element a) {
if(this.c == a.c) return 0;
else if(this.c > a.c) return 1;
else return -1;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0) {
String s = sc.next();
ArrayList<Element> e = new ArrayList<>();
for(int i = 0; i < s.length(); i++) {
e.add(new Element(s.charAt(i), i+1));
}
Collections.sort(e);
Integer[] arr = new Integer[s.length()];
for(int i = 0; i < s.length(); i++) {
arr[i] = s.charAt(i) - 'a' + 1;
}
Arrays.sort(arr);
int start = BinarySearchFirst(arr, s.charAt(0) - 'a' + 1);
int end = BinarySearchLast(arr, s.charAt(s.length()-1) - 'a' + 1);
if(start >= end) {
start = BinarySearchLast(arr, s.charAt(0) - 'a' + 1);
end = BinarySearchFirst(arr, s.charAt(s.length()-1) - 'a' + 1);
}
long cost = 0;
for(int i = Math.min(start, end); i < Math.max(start, end); i++) {
cost += arr[i+1] - arr[i];
}
pw.println(cost + " " + (Math.abs(end - start)+1));
int[] ans = new int[Math.abs(end - start)+1];
if(start < end) {
int j = 0;
for(int i = start; i <= end; i++) {
ans[j++] = e.get(i).val;
}
}else {
int j = 0;
for(int i = start; i >= end; i--) {
ans[j++] = e.get(i).val;
}
}
if(ans[0] != 1) {
for(int i = 1; i < ans.length; i++) {
if(ans[i] == 1) {
int tmp = ans[0];
ans[0] = ans[i];
ans[i] = tmp;
break;
}
}
}
if(ans[ans.length-1] != s.length()) {
for(int i = ans.length-2; i >= 0; i--) {
if(ans[i] == s.length()) {
int tmp = ans[ans.length-1];
ans[ans.length-1] = ans[i];
ans[i] = tmp;
break;
}
}
}
for(Integer x: ans) {
pw.print(x + " ");
}
pw.println();
}
sc.close();
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 11 | 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 | 46ba22a505357e83a4ba8a8f616395e8 | 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 rounds.round_820;
import java.io.*;
import java.util.*;
/*
* tags:
* link->
*/
public class c {
static FastScanner fs = new FastScanner();
static PrintWriter fop = new PrintWriter(System.out);
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
try {
solve();
// char now = 12 + 'a';
// System.out.println(now);
} catch (Exception e) {
System.out.println(e);
return;
}
}
static void solve() throws IOException {
int T = fs.nextInt();
next:while (T-- > 0) {
String s = fs.next();
int n = s.length();
Boolean inverted = false;
Map<Character, PriorityQueue<Integer>> ind = new TreeMap<Character, PriorityQueue<Integer>>();
int finalCost = Math.abs(s.charAt(n - 1) - s.charAt(0));
// calculate number of alphabets that come under first and last alphabet
char first, last;
first = s.charAt(0);
last = s.charAt(n - 1);
if (first > last) inverted = true;
ArrayList<Integer> revInd = new ArrayList<>();
for (int i = 0; i < n; i++) {
char cur = s.charAt(i);
if (inverted) {
if (cur <= first && cur >= last) {
if (!ind.containsKey(cur)) {
ind.put(cur, new PriorityQueue<Integer>());
}
ind.get(cur).add(i);
}
} else {
if (cur >= first && cur <= last) {
if (!ind.containsKey(cur)) {
ind.put(cur, new PriorityQueue<Integer>());
}
ind.get(cur).add(i);
}
}
}
// ind has pq of indeces and char, which are under range of first and last
// considering from>to {reverse traversal} ie inverted
//we need to put it int he decreasing sorted order of the alphabetical order
int count = 0;
int f = first - 'a';
int l = last - 'a';
if (inverted) {
for (int i = f; i >= l; i--) {
char now = (char) (i + 'a');
if (ind.containsKey(now)) {
PriorityQueue<Integer> cur = ind.get(now);
while (!cur.isEmpty()) {
revInd.add(cur.poll());
count++;
}
}
}
} else {
for (int i = f; i <= l; i++) {
char now = (char) (i + 'a');
if (ind.containsKey(now)) {
PriorityQueue<Integer> cur = ind.get(now);
while (!cur.isEmpty()) {
revInd.add(cur.poll());
count++;
}
}
}
}
//end of logic i
// for (Map.Entry<Character, PriorityQueue<Integer>> entry : ind.entrySet()) {
// PriorityQueue<Integer> pq = entry.getValue();
// while (!pq.isEmpty()) {
// count++;
// revInd.add(pq.poll());
// }
// }
sb.append(finalCost + " " + count + "\n");
// if (!inverted) {
for (int i = 0; i < revInd.size(); i++) {
sb.append(revInd.get(i) + 1 + " ");
}
// } else {
// for (int i = revInd.size() - 1; i >= 0; i--) {
// sb.append(revInd.get(i) + 1 + " ");
// }
// }
sb.append("\n");
}
fop.println(sb.toString());
fop.flush();
fop.close();
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
public static class Pair {
int a;
int b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
}
}
/* test cases
*/
| 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 11 | 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 | 5f23e8736b616acf662e95372ef0894b | 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.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
/* Name of the class has to be "Main" only if the class is public. */
public class C {
static int count = 0;
public static void main(String[] args) throws Exception {
// your code goes here
FastScanner sc = new FastScanner();
int t = sc.nextInt();
while (t-- > 0) {
String s = sc.next();
solve(s);
}
}
static class Pair implements Comparable<Pair>{
int x;
int y;
Pair(int x,int y){
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return o.x-this.x;
}
}
private static void solve(String s) {
int n = s.length();
if(s.charAt(0)<s.charAt(n-1)){
int amount = s.charAt(n-1)-s.charAt(0);
int min =2;
ArrayList<Pair>ls = new ArrayList<>();
for(int i=1;i<n-1;i++){
if(s.charAt(i)>=s.charAt(0)&&s.charAt(i)<=s.charAt(n-1)){
min++;
ls.add(new Pair(s.charAt(i)-'a'+1,i+1));
}
}
Collections.sort(ls,Collections.reverseOrder());
System.out.println(amount+" "+min);
System.out.print(1+" ");
for(Pair p:ls){
System.out.print(p.y+" ");
}
System.out.print(n+" ");
System.out.println();
}else if(s.charAt(0)>s.charAt(n-1)){
int amount = s.charAt(0)-s.charAt(n-1);
int min =2;
ArrayList<Pair>ls = new ArrayList<>();
for(int i=1;i<n-1;i++){
if(s.charAt(i)<=s.charAt(0)&&s.charAt(i)>=s.charAt(n-1)){
min++;
ls.add(new Pair(s.charAt(i)-'a'+1,i+1));
}
}
Collections.sort(ls);
System.out.println(amount+" "+min);
System.out.print(1+" ");
for(Pair p:ls){
System.out.print(p.y+" ");
}
System.out.print(n+" ");
System.out.println();
}else{
int amount = 0;
int min =1;
ArrayList<Integer>ls = new ArrayList<>();
ls.add(1);
for(int i=1;i<n;i++){
if(s.charAt(i)==s.charAt(0)){
min++;
ls.add(i+1);
}
}
System.out.println(amount+" "+min);
for(int x:ls){
System.out.print(x+" ");
}
System.out.println();
}
}
public static boolean isPrime ( long n){
if (n < 2) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
long sqrtN = (long) Math.sqrt(n) + 1;
for (long i = 6L; i <= sqrtN; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0) return false;
}
return true;
}
public static long gcd ( long a, long b){
if (a > b)
a = (a + b) - (b = a);
if (a == 0L)
return b;
return gcd(b % a, a);
}
public static ArrayList<Integer> findDiv ( int N){
//gens all divisors of N
ArrayList<Integer> ls1 = new ArrayList<Integer>();
ArrayList<Integer> ls2 = new ArrayList<Integer>();
for (int i = 1; i <= (int) (Math.sqrt(N) + 0.00000001); i++)
if (N % i == 0) {
ls1.add(i);
ls2.add(N / i);
}
Collections.reverse(ls2);
for (int b : ls2)
if (b != ls1.get(ls1.size() - 1))
ls1.add(b);
return ls1;
}
public static void sort ( int[] arr){
//because Arrays.sort() uses quicksort which is dumb
//Collections.sort() uses merge sort
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
for (int i = 0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public static long power ( long x, long y, long p){
//0^0 = 1
long res = 1L;
x = x % p;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y >>= 1;
x = (x * x) % p;
}
return res;
}
static class FastScanner {
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
}
}
| Java | ["6\n\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 11 | 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 | 0e63f17762874d82535eab2caf187c51 | 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.Map.Entry;
import static java.lang.Math.*;
/**
*
*
* @author adnan
**/
@SuppressWarnings("unchecked")
public class CodeForces {
final static String no = "NO";
final static String yes = "YES";
final static int maxV = Integer.MAX_VALUE;
final static int minV = Integer.MIN_VALUE;
final static int mod = (int) (1e9 + 7);
static public PrintWriter pw = new PrintWriter(System.out);
static public IO sc = new IO();
static Scanner fc = new Scanner(System.in);
public static void main(String[] args) {
solve();
pw.flush();
}
//////////////////////////////////////////////////////////////////////////
private static void solve() {
int t = sc.nextInt();
HashMap<Character, Integer> hm = new HashMap<>();
for (int i = 0; i < 26; i++) {
hm.put((char) ('a' + i), i + 1);
}
in: while (t-- > 0) {
char[]ch = sc.next().toCharArray();
int n = ch.length;
List<Integer> []ls = new List[26];
for(int i = 0;i<26;i++) {
ls[i] = new ArrayList<>();
}
for(int i = 0; i <n; i++) {
ls[ch[i] - 'a'].add(i+1);
}
char lf = ch[0];
char rg = ch[n-1];
int diff = lf > rg ? -1 : 1;
List<Integer> ans = new ArrayList<>();
for(char c = lf;;c+=diff) {
for(int x : ls[c-'a']) {
ans.add(x);
}
if(c==rg)break;
}
pw.println((int)abs(rg - lf) +" " + ans.size());
for(int x : ans)
{
pw.print(x+" ");
}
pw.println();
}
}
///////////////////////////////////////////////////////////////////////////
static void debug(int[] ar) {
for (var x : ar) {
pw.print(x + " ");
}
pw.println();
}
static class Three {
int x, y, z;
public Three(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
static int floorPowerOf2(int n) {
int p = (int) (Math.log(n) / Math.log(2));
return (int) Math.pow(2, p);
}
static int ceilPowerOf2(int n) {
int p = (int) (Math.log(n) / Math.log(2));
return (int) Math.pow(2, p + 1);
}
static List<Integer> printDivisors(int n) {
// Note that this loop runs till square root
List<Integer> sl = new ArrayList<>();
for (int i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
// If divisors are equal, print only one
if (n / i == i)
sl.add(i);
else // Otherwise print both
{
sl.add(i);
sl.add(n / i);
}
}
}
return sl;
}
static int max(int... val) {
int res = Integer.MIN_VALUE;
for (int i : val) {
res = Math.max(i, res);
}
return res;
}
static class Pair {
int x, y, z;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
Pair(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
/*
* lcm/gcd
*
*
*
*
*
*
*
*/
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static void sort(int[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i)
continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
}
static void sort(long[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i)
continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
}
static void sortDec(int[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i)
continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
int l = 0;
int r = n - 1;
while (l < r) {
arr[l] ^= arr[r];
arr[r] ^= arr[l];
arr[l] ^= arr[r];
l++;
r--;
}
}
static void sortDec(long[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i)
continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
int l = 0;
int r = n - 1;
while (l < r) {
arr[l] ^= arr[r];
arr[r] ^= arr[l];
arr[l] ^= arr[r];
l++;
r--;
}
}
static class IO {
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());
}
long[] longArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
double nextDouble() {
return Double.parseDouble(next());
}
}
class SegmentTree {
int[] tree;
SegmentTree(int n) {
tree = new int[n];
}
void build(int[] arr, int node, int start, int end) {
if (start == end) {
tree[node] = arr[start];
} else {
int mid = (start + end) / 2;
build(arr, 2 * node + 1, start, mid);
build(arr, 2 * node + 2, mid + 1, end);
tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
}
}
void update(int[] arr, int node, int index, int val, int start, int end) {
if (start == end) {
tree[node] += val;
arr[start] += val;
} else {
int mid = (start + end) / 2;
if (start <= index && index <= mid) {
update(arr, 2 * node + 1, index, val, start, mid);
} else {
update(arr, 2 * node + 2, index, val, mid + 1, end);
}
tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
}
}
int query(int node, int start, int end, int left, int right) {
if (right < start || end < left) {
return 0;
}
if (left <= start && end <= right) {
return tree[node];
}
int mid = (start + end) / 2;
int p1 = query(2 * node + 1, start, mid, left, right);
int p2 = query(2 * node + 2, mid + 1, end, left, right);
return p1 + p2;
}
}
} | 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 11 | 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 | 3c79c128fe09794e5264e0e95506906a | 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 A {
// static int k;
public static void main(String[] args) throws IOException {
in.init(false); // submit mode
// in.init(true); // debug mode
int n = in.readInt();
// ArrayList<Integer> arr = new ArrayList<>(10);
// arr.s
for (int i = 0; i < n; i++) {
sol(1);
}
in.dinit();
}
private static void sol(int testset) throws IOException {
String s = in.readLine();
int n = s.length();
int changed = 1;
if (s.charAt(0) > s.charAt(s.length() - 1)) {
changed = 0;
}
char c[] = s.toCharArray();
char low = c[0];
char high = c[c.length - 1];
PriorityQueue<int[]> pq;
if (changed == 0) {
pq = new PriorityQueue<>((a, b) -> {
return b[0] - a[0];
});
} else {
pq = new PriorityQueue<>((a, b) -> {
return a[0] - b[0];
});
}
for (int i = 0; i < c.length; i++) {
char r = c[i];
if (changed == 1) {
if (c[i] >= low && c[i] <= high) {
int t = c[i] - 'a' + 1;
pq.add(new int[] { t, i });
}
} else {
if (c[i] >= high && c[i] <= low) {
int t = c[i] - 'a' + 1;
pq.add(new int[] { t, i });
}
}
}
StringBuilder ans = new StringBuilder();
int prev = 0;
int start = 0;
int sum = 0;
int mark[]=null;
while (!pq.isEmpty()) {
int tt[] = pq.poll();
if(tt[1]==n-1)
{
mark= tt;
continue;
}
if (start == 0) {
prev = tt[0];
} else {
sum += Math.abs(tt[0] - prev);
prev = tt[0];
}
ans.append(tt[1] + 1 + " ");
start++;
}
sum+=Math.abs(mark[0]-prev);
in.out.println(sum + " " + (start+1));
String annm = ans.toString().trim();
annm+=" "+(mark[1]+1);
// return annm;
in.out.println(annm);
// in.out.println(ans.reverse());
}
}
class in {
static BufferedReader br;
static PrintWriter out;
static StringTokenizer st;
static String yes = "YES";
static String no = "NO";
public static void init(boolean test) throws IOException {
if (test == true) {
br = new BufferedReader(new FileReader("in"));
out = new PrintWriter(new OutputStreamWriter(System.out));
} else {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
}
}
public static void init(String in) throws IOException {
br = new BufferedReader(new FileReader(in));
out = new PrintWriter(new FileWriter("out.txt"));
// out.close();
}
// public static void init(boolean f) throws IOException {
// if (f == true) {
// br = new BufferedReader(new FileReader(in));
// out = new PrintWriter(new FileWriter("out.txt"));
// }
// else {
// br = new BufferedReader(new InputStreamReader(System.in));
// out = new PrintWriter(new OutputStreamWriter(System.out));
// }
// // out.close();
// }
static void print(String s) {
out.println(s);
}
static int[] arr(int n) throws IOException {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.readInt();
}
return arr;
}
public static void dinit() throws IOException {
br.close();
out.close();
}
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}
static long readLong() throws IOException {
return Long.parseLong(next());
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
static double readDouble() throws IOException {
return Double.parseDouble(next());
}
static char readCharacter() throws IOException {
return next().charAt(0);
}
static String readLine() throws IOException {
return br.readLine().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 11 | 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 | 140093b2059a3585d2e9cd21346fa6ee | 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.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.CollationElementIterator;
import java.util.*;
//changed
import javax.lang.model.type.IntersectionType;
import javax.print.attribute.standard.PrinterResolution;
import javax.swing.text.html.CSS;
import javax.swing.tree.TreeNode;
public class codeforces {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static boolean isKthBitSet(int n,
int k) {
if (((n >> (k)) &
1) > 0)
return true;
return false;
}
static int fact(int a) {
int f = 1;
for (int i = 2; i <= a; i++)
f *= i;
return f;
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
public static class TrieNode {
HashMap<Character, TrieNode> children;
String w;
TrieNode() {
children = new HashMap<>();
w = "";
}
}
static TrieNode root = new TrieNode();
public static void insert(String word) {
TrieNode curr = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
TrieNode node = curr.children.get(ch);
if (node == null) {
node = new TrieNode();
curr.children.put(ch, node);
}
curr = node;
curr.w = word;
}
curr.w = word;
}
public static String prefix(String word) {
TrieNode curr = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
TrieNode node = curr.children.get(ch);
if (node == null)
return "";
curr = node;
}
return curr.w;
}
static class st {
static int size;
static long min[][];
static void init(int n) {
size = 1;
while (size < n)
size *= 2;
min = new long[2 * size][2];
}
static void build(int arr[]) {
build(arr, 0, 0, size);
}
static long merge(long l[], long r[]) {
if (l[0] < r[0])
return l[1];
else if (r[0] < l[0])
return r[1];
else
return l[1] + r[1];
}
static void build(int arr[], int x, int lx, int rx) {
if (rx - lx == 1) {
if (lx < arr.length) {
min[x][0] = arr[lx];
min[x][1] = 1;
}
return;
}
int m = (lx + rx) / 2;
build(arr, 2 * x + 1, lx, m);
build(arr, 2 * x + 2, m, rx);
min[x][0] = Math.min(min[2 * x + 1][0], min[2 * x + 2][0]);
min[x][1] = merge(min[2 * x + 1], min[2 * x + 2]);
}
static void set(int i, int v) {
set(i, v, 0, 0, size);
}
static void set(int i, int v, int x, int lx, int rx) {
if (rx - lx == 1) {
min[x][0] = v;
return;
}
int m = (lx + rx) / 2;
if (i < m)
set(i, v, 2 * x + 1, lx, m);
else
set(i, v, 2 * x + 2, m, rx);
min[x][0] = Math.min(min[2 * x + 1][0], min[2 * x + 2][0]);
min[x][1] = merge(min[2 * x + 1], min[2 * x + 2]);
}
static long[] min(int l, int r) {
return min(l, r, 0, 0, size);
}
static long[] min(int l, int r, int x, int lx, int rx) {
if (lx >= r || l >= rx)
return new long[]{Long.MAX_VALUE, 0};
if (lx >= l && rx <= r)
return new long[]{min[x][0], min[x][1]};
int m = (lx + rx) / 2;
long s1[] = min(l, r, 2 * x + 1, lx, m);
long s2[] = min(l, r, 2 * x + 2, m, rx);
return new long[]{Math.min(s1[0], s2[0]), merge(s1, s2)};
}
}
public static void main(String[] args) throws IOException {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
InputReader sc = new InputReader(System.in);
int t=sc.nextInt();
while(t-->0) {
String s=sc.next();
int n=s.length();
int x=s.charAt(0);
int y=s.charAt(n-1);
int cost=Math.abs(x-y);
int arr[]=new int[n+1];
int max=Math.max(x,y);
int min=Math.min(x,y);
if(n==2)
{
System.out.println(cost+" "+2);
System.out.println(1+" "+2);
continue;
}
if(x<y) {
List<Integer> al=solve(s,min,max);
System.out.println(cost + " " + al.size());
for (int i = 0; i < al.size(); i++)
System.out.print(al.get(i) + " ");
System.out.println();
}
else
{
StringBuilder sb=new StringBuilder(s);
sb.reverse();
List<Integer> al=solve(sb.toString(),min,max);
System.out.println(cost + " " + al.size());
for (int i = al.size()-1; i >=0; i--) {
if(i==0 || i==al.size()-1)
System.out.print(al.get(al.size()-1-i)+ " ");
else
System.out.print((n+1)-al.get(i)+ " ");
}
System.out.println();
}
}
}
public static List<Integer> solve(String s, int min, int max)
{
int n=s.length();
char ch[]=s.toCharArray();
HashMap<Character,List<Integer>> hm=new HashMap<>();
for(int i=1;i<n-1;i++)
{
if(!hm.containsKey(ch[i]))
hm.put(ch[i],new ArrayList<>());
hm.get(ch[i]).add(i+1);
}
ArrayList<Integer> al=new ArrayList<>();
Arrays.sort(ch,1,ch.length-1);
int count=0;
al.add(1);
for(int i=1;i<n-1;i++)
{
char c=ch[i];
if(c>=min && c<=max) {
List<Integer> arr=hm.get(c);
al.add(arr.get(arr.size()-1));
arr.remove(arr.size()-1);
if(arr.size()==0)
hm.remove(c);
else
hm.put(c,arr);
count++;
}
}
al.add(n);
return al;
}
static class InputReader {
private InputStream in;
private byte[] buffer = new byte[1024];
private int curbuf;
private int lenbuf;
public InputReader(InputStream in) {
this.in = in;
this.curbuf = this.lenbuf = 0;
}
public boolean hasNextByte() {
if (curbuf >= lenbuf) {
curbuf = 0;
try {
lenbuf = in.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return false;
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[curbuf++];
else
return -1;
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private void skip() {
while (hasNextByte() && isSpaceChar(buffer[curbuf]))
curbuf++;
}
public boolean hasNext() {
skip();
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (!isSpaceChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[][] nextCharMap(int n, int m) {
char[][] map = new char[n][m];
for (int i = 0; i < n; i++)
map[i] = next().toCharArray();
return map;
}
}
}
class bs{
public static boolean search(long arr[],long tar)
{
int i=0;
int j=arr.length-1;
while(j>=i)
{
int mid=i+(j-i)/2;
if(arr[mid]==tar)
return true;
else if(arr[mid]>tar)
j=mid-1;
else
i=mid+1;
}
return false;
}
public static int closestleft(long arr[],long tar)
{
int l=-1;
int r=arr.length;
while(r>l+1)
{
int m=l+(r-l)/2;
if(arr[m]<=tar)
l=m;
else
r=m;
}
return l;
}
public static int closestright(long arr[],long tar)
{
int l=-1;
int r=arr.length;
while(r>l+1)
{
int m=l+(r-l)/2;
if(arr[m]<tar)
l=m;
else
r=m;
}
return r;
}
}
class MergeSort {
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
static void merge(long arr[], int l, int m, int r) {
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long[n1];
long R[] = new long[n2];
/*Copy data to temp arrays*/
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];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
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++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
static void sort(long arr[], int l, int r) {
if (l < r) {
// Find the middle point
int m = (l + r) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
}
// class st {
// static int size;
// static long[][] sums;
// static long NEUTRAL=0;
// public static void init(int n)
// {
// size=1;
// while(size<n)
// size*=2;
// sums=new long[2*size][27];
// }
// static int m;
// public static long single(long v)
// {
// return 1;
// }
// public static long[] merge(long a[],long b[])
// {
// if(a.length==0 && b.length==0)
// return new long[0];
// if(a.length==0)
// return b;
// if(b.length==0)
// return a;
// for(int i=0;i<27;i++)
// b[i]=b[i]+a[i];
// return b;
// }
// public static void build(long[] a,int x,int lx,int rx)
// {
// if(rx-lx==1)
// {
// long z=a[lx];
// sums[x][(int)z-96]=1;
// return;
// }
// int mid=(lx+rx)/2;
// build(a,2*x+1,lx,mid);
// build(a,2*x+2,mid,rx);
// sums[x]=merge(sums[2*x+1],sums[2*x+2]);
// }
// public static void build(long[] a)
// {
// build(a,0,0,size);
// }
//
// public static void set(int i,long v,int x,int lx,int rx,long a[],char c)
// {
// if(rx-lx==1)
// {
// long z=a[i];
// sums[x][(int)z-96]=0;
// sums[x][(int)c-96]=1;
// return;
// }
// int m=(lx+rx)/2;
// if(i<m)
// set(i,v,2*x+1,lx,m,a,c);
// else
// set(i,v,2*x+2,m,rx,a,c);
// sums[x]=merge(sums[2*x+1],sums[2*x+2]);
// }
// public static void set(int i,long v,long a[],char c)
// {
// set(i,v,0,0,size,a,c);
// }
// public static long[] calc(int l,int r)
// {
// return calc(l,r,0,0,size);
// }
// public static long[] calc(int l,int r,int x,int lx,int rx)
// {
// if(lx>=r || rx<=l)
// return new long[0];
// if(lx>=l && rx<=r)
// return sums[x];
// int mid=(lx+rx)/2;
// long s1[]=calc(l,r,2*x+1,lx,mid);
// long s2[]=calc(l,r,2*x+2,mid,rx);
// return merge(s1,s2);
// }
// /*
// public static int find_above(long v,int l,int x,int lx,int rx)
// {
// if(sums[x]<v)
// return -1;
// if(rx<=l)
// return -1;
// if(rx-lx==1)
// return lx;
// int m=(lx+rx)/2;
// int res=find_above(v,l,2*x+1,lx,m);
// if(res==-1)
// res=find_above(v,l,2*x+2,m,rx);
// return res;
// }
// public static int find_above(long v,int l)
// {
// return find_above(v,l,0,0,size);
// }
// public static int find(long k,int x,int lx,int rx,int n)
// {
// if(rx-lx==1) {
// if(sums[x]==1)
// return lx;
// else
// return -1;
// }
// int m=(lx+rx)/2;
// long sl=sums[2*x+1];
// if(k<sl)
// return find(k,2*x+1,lx,m,n);
// else
// return find(k-sl,2*x+2,m,rx,n);
// }
// public static int find(long k,int n)
// {
// return find(k,0,0,size,n);
// }*/
//}
class MathLib{
public static int[] sieveEratostheness(int n) {
if (n <= 32) {
int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
for (int i = 0; i < primes.length; i++) {
if (n < primes[i]) {
return Arrays.copyOf(primes, i);
}
}
return primes;
}
int u = n + 32;
double lu = Math.log(u);
int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)];
ret[0] = 2;
int pos = 1;
int[] isnp = new int[(n + 1) / 32 / 2 + 1];
int sup = (n + 1) / 32 / 2 + 1;
int[] tprimes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
for (int tp : tprimes) {
ret[pos++] = tp;
int[] ptn = new int[tp];
for (int i = (tp - 3) / 2; i < tp << 5; i += tp) ptn[i >> 5] |= 1 << (i & 31);
for (int j = 0; j < sup; j += tp) {
for (int i = 0; i < tp && i + j < sup; i++) {
isnp[j + i] |= ptn[i];
}
}
}
// 3,5,7
// 2x+3=n
int[] magic = {0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14};
int h = n / 2;
for (int i = 0; i < sup; i++) {
for (int j = ~isnp[i]; j != 0; j &= j - 1) {
int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27];
int p = 2 * pp + 3;
if (p > n) break;
ret[pos++] = p;
if ((long) p * p > n) continue;
for (int q = (p * p - 3) / 2; q <= h; q += p) isnp[q >> 5] |= 1 << q;
}
}
return Arrays.copyOf(ret, pos);
}
static boolean[] sieveOfEratosthenes(int n) {
// 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.
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
public static int getMax(int[] inputArray){
int maxValue = inputArray[0];
for(int i=1;i < inputArray.length;i++){
if(inputArray[i] > maxValue){
maxValue = inputArray[i];
}
}
return maxValue;
}
// Method for getting the minimum value
public static int getMin(int[] inputArray){
int minValue = inputArray[0];
for(int i=1;i<inputArray.length;i++){
if(inputArray[i] < minValue){
minValue = inputArray[i];
}
}
return minValue;
}
static int countSetBits(long n)
{
int count = 0;
while (n > 0) {
count += n & 1;
n >>= 1;
}
return count;
}
static long binpow(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1)>0)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
static ArrayList<Integer> printDivisors(int n)
{
ArrayList<Integer> al=new ArrayList<>();
for (int i=2; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
// If divisors are equal, print only one
if (n/i == i)
al.add(i);
else // Otherwise print both
{
al.add(i);
al.add(n/i);
}
}
}
return al;
}
static long fastPow(long a, long b, long mod) {
if(b == 0)
return 1L;
long val = fastPow(a, b/2, mod);
if(b % 2 == 0)
return val * val % mod;
else
return val * val % mod * a % mod;
}
static int LowerBound(int a[], int x) { // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(int a[], int x) {// x is the key or target value
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
private static long safe_mod(long x, long m){
x %= m;
if(x<0) x += m;
return x;
}
private static long[] inv_gcd(long a, long b){
a = safe_mod(a, b);
if(a==0) return new long[]{b,0};
long s=b, t=a;
long m0=0, m1=1;
while(t>0){
long u = s/t;
s -= t*u;
m0 -= m1*u;
long tmp = s; s = t; t = tmp;
tmp = m0; m0 = m1; m1 = tmp;
}
if(m0<0) m0 += b/s;
return new long[]{s,m0};
}
public static long gcd(long a, long b){
a = java.lang.Math.abs(a);
b = java.lang.Math.abs(b);
return inv_gcd(a, b)[0];
}
public static long lcm(long a, long b){
a = java.lang.Math.abs(a);
b = java.lang.Math.abs(b);
return a / gcd(a,b) * b;
}
public static long pow_mod(long x, long n, int m){
assert n >= 0;
assert m >= 1;
if(m == 1)return 0L;
x = safe_mod(x, m);
long ans = 1L;
while(n > 0){
if((n&1) == 1) ans = (ans * x) % m;
x = (x*x) % m;
n >>>= 1;
}
return ans;
}
static long fact[]=new long[1000001];
static long inv_fact[]=new long[1000001];
static int p=1000000007;
public static void precomp()
{
fact[0]=fact[1]=1;
for(int i=2;i<=fact.length-1;i++)
{
fact[i]=(fact[i-1]*i)%p;
}
inv_fact[(int)1e6]=modpow(fact[(int)1e6],p-2);
for(int i=fact.length-2;i>=0;i--)
{
inv_fact[i]=(inv_fact[i+1]*(i+1))%p;
}
}
public static boolean summ(int n,int a,int b)
{
while(n>0)
{
if(n%10!=a && n%10!=b)return false;
n/=10;
}
return true;
}
public static long modpow(long x,long n)
{
long res=1;
while(n>0)
{
if(n%2!=0)
{
res=(res*x)%p;n--;
}
else
{
x=(x*x)%p;n/=2;
}
}
return res;
}
public static long ncr(int n,int r)
{
if(r>n || r<0 || n<0)return 0;
return fact[n]*inv_fact[r]%p*inv_fact[n-r]%p;
}
public static void bipartite(int u,boolean vis[],int color[],ArrayList<Integer>[] al)
{
vis[u]=true;
for(int v:al[u])
{
if(!vis[v])
{
if(color[v]==-1) {
color[v]=1-color[u];
bipartite(v,vis,color,al);
}
}
}
}
public static long[] crt(long[] r, long[] m){
assert(r.length == m.length);
int n = r.length;
long r0=0, m0=1;
for(int i=0; i<n; i++){
assert(1 <= m[i]);
long r1 = safe_mod(r[i], m[i]), m1 = m[i];
if(m0 < m1){
long tmp = r0; r0 = r1; r1 = tmp;
tmp = m0; m0 = m1; m1 = tmp;
}
if(m0%m1 == 0){
if(r0%m1 != r1) return new long[]{0,0};
continue;
}
long[] ig = inv_gcd(m0, m1);
long g = ig[0], im = ig[1];
long u1 = m1/g;
if((r1-r0)%g != 0) return new long[]{0,0};
long x = (r1-r0) / g % u1 * im % u1;
r0 += x * m0;
m0 *= u1;
if(r0<0) r0 += m0;
//System.err.printf("%d %d\n", r0, m0);
}
return new long[]{r0, m0};
}
public static long floor_sum(long n, long m, long a, long b){
long ans = 0;
if(a >= m){
ans += (n-1) * n * (a/m) / 2;
a %= m;
}
if(b >= m){
ans += n * (b/m);
b %= m;
}
long y_max = (a*n+b) / m;
long x_max = y_max * m - b;
if(y_max == 0) return ans;
ans += (n - (x_max+a-1)/a) * y_max;
ans += floor_sum(y_max, a, m, (a-x_max%a)%a);
return ans;
}
public static java.util.ArrayList<Long> divisors(long n){
java.util.ArrayList<Long> divisors = new ArrayList<>();
java.util.ArrayList<Long> large = new ArrayList<>();
for(long i=1; i*i<=n; i++) if(n%i==0){
divisors.add(i);
if(i*i<n) large.add(n/i);
}
for(int p=large.size()-1; p>=0; p--){
divisors.add(large.get(p));
}
return divisors;
}
} | 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 11 | 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 | 9c2960ce40d8bddc56a98a0d91b5cc01 | 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 PrintWriter out;
static Kioken sc;
static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null;
public static void main(String[] args) throws FileNotFoundException {
if (checkOnlineJudge) {
out = new PrintWriter("E:/CF_V2/output.txt");
sc = new Kioken(new File("E:/CF_V2/input.txt"));
} else {
out = new PrintWriter((System.out));
sc = new Kioken();
}
int tt = 1;
tt = sc.nextInt();
while (tt-- > 0) {
solve();
}
out.flush();
out.close();
}
public static void solve() {
// int n = sc.nextInt();
char[] c = sc.nextLine().toCharArray();
int n = c.length;
char start = c[0];
char end = c[n - 1];
// start = (char)Math.min(c[0], c[n - 1]);
// end = (char)Math.max(c[0], c[n - 1]);
List<int[]> ll = new ArrayList<>();
for(int i = 0; i < n; i++){
ll.add(new int[]{c[i] - 'a', i});
}
ll.sort((a,b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
if(start > end){
ll.sort((a,b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]);
}
// for(int[] i: arr){
// out.println(Arrays.toString(i));
// }
int startIdx = -1, endIdx = -1;
for(int i = 0; i < n; i++){
if(ll.get(i)[0] == (start - 'a')){
// searching for end
startIdx = i;
int ee = end - 'a';
for(int j = i + 1; j < n; j++){
if(ll.get(j)[0] == ee){
for(int k = j; k < n; k++){
if(ll.get(k)[0] == ee){
endIdx = Math.max(endIdx, k);
}else break;
}
break;
}
}
break;
}
}
if(startIdx == -1 || endIdx == -1){
out.println("Error");
return;
}
int ee = end - 'a';
int ss = start - 'a';
// out.println(" start " + startIdx + " " + endIdx + " " + start + " " + end);
out.println(Math.abs((ee - ss)) + " " + (endIdx - startIdx + 1));
List<Integer> ans = new ArrayList<>();
for(int i = startIdx; i <= endIdx; i++){
ans.add((ll.get(i)[1]+1));
}
// if(c[0] > c[n - 1]){
// Collections.reverse(ans);
// }
for(int i: ans){
out.print(i + " ");
}
out.println();
}
public static long gcd(long a, long b) {
while (b != 0) {
long rem = a % b;
a = b;
b = rem;
}
return a;
}
static long MOD = 1000000007;
static void reverseSort(int[] arr){List<Integer> list = new ArrayList<>();for (int i=0; i<arr.length; i++){list.add(arr[i]);}Collections.sort(list, Collections.reverseOrder());for (int i = 0; i < arr.length; i++){arr[i] = list.get(i);}}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void sort(long[] a){
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class Kioken {
// FileInputStream br = new FileInputStream("input.txt");
BufferedReader br;
StringTokenizer st;
Kioken(File filename) {
try {
FileReader fr = new FileReader(filename);
br = new BufferedReader(fr);
st = new StringTokenizer("");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
Kioken() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean hasNext() {
String next = null;
try {
next = br.readLine();
} catch (Exception e) {
}
if (next == null || next.length() == 0) {
return false;
}
st = new StringTokenizer(next);
return true;
}
public int[] readArrayInt(int n){
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = nextInt();
}
return arr;
}
public long[] readArrayLong(int n){
long[] arr = new long[n];
for(int i = 0; i < n; i++){
arr[i] = nextLong();
}
return arr;
}
}
} | Java | ["6\n\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 11 | 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 | 6f8b6b0d1ecf9f3b06ec7a6184286853 | 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.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
public class Solution{
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static class Pair{
int s;
int end;
public Pair(int a, int e) {
end = e;
s = a;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
public static void main(String[] args) throws Exception
{
FastReader scn = new FastReader();
OutputStream outputStream = System.out;
OutputWriter output = new OutputWriter(outputStream);
int t = scn.nextInt();
long mod = 1000000007L;
// String[] pal = {"0000", "0110","0220","0330","0440","0550","1001","1111","1221","1331","1441","1551","2002","2112","2222","2332"};
while(t>0) {
String s = scn.next();
int n = s.length();
List<Integer> a = new ArrayList<>();
// a.add(0);
List<List<Integer>> set = new ArrayList<>();
for(int i=0;i<26;i++) {
set.add(new ArrayList<>());
}
if(s.charAt(0)>=s.charAt(n-1)) {
for(int i=1;i<n-1;i++) {
if(s.charAt(i)<=s.charAt(0) && s.charAt(i)>=s.charAt(n-1)) {
set.get((s.charAt(i)-'a')).add(i+1);
}
}
a.add(1);
for(char c = s.charAt(0);c>=s.charAt(n-1);c--) {
for(int idx: set.get(c-'a')) {
a.add(idx);
}
}
a.add(n);
}else {
for(int i=1;i<n-1;i++) {
if(s.charAt(i)>=s.charAt(0) && s.charAt(i)<=s.charAt(n-1)) {
set.get((s.charAt(i)-'a')).add(i+1);
}
}
a.add(1);
for(char c = s.charAt(0);c<=s.charAt(n-1);c++) {
for(int idx: set.get(c-'a')) {
a.add(idx);
}
}
a.add(n);
}
output.println(Math.abs(s.charAt(n-1)-s.charAt(0))+" "+a.size());
for(int x: a) {
output.print(x+" ");
}
output.println();
t--;
}
output.close();
}
static int CeilIndex(int A[], int l, int r, int key)
{
while (r - l > 1) {
int m = l + (r - l) / 2;
if (A[m] >= key)
r = m;
else
l = m;
}
return r;
}
static int lis(int A[], int size)
{
// Add boundary case, when array size is one
int[] tailTable = new int[size];
int len; // always points empty slot
tailTable[0] = A[0];
len = 1;
for (int i = 1; i < size; i++) {
if (A[i] < tailTable[0])
// new smallest value
tailTable[0] = A[i];
else if (A[i] > tailTable[len - 1])
// A[i] wants to extend largest subsequence
tailTable[len++] = A[i];
else
// A[i] wants to be current end candidate of an existing
// subsequence. It will replace ceil value in tailTable
tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i];
}
return len;
}
public static int checkL(char[][] a, int r, int c, boolean[][] mark) {
int cnt=0;
for(int i=Math.max(r-1,0);i<=Math.min(r+2,a.length-1);i++) {
for(int j= Math.max(c-1,0);j<=Math.min(c+2,a[0].length-1);j++) {
if(a[i][j]=='*') {
cnt++;
}
}
}
char ch11 = a[r][c];
char ch12 = a[r][c+1];
char ch21 = a[r+1][c];
char ch22 = a[r+1][c+1];
int n = a.length;
int m = a[0].length;
if(cnt==4) {
if(ch22!='*' && r+2<n && c+2<m && a[r+2][c+2]=='*') {
cnt--;
}else if(ch21!='*' && r+2<n && c-1>=0 && a[r+2][c-1]=='*') {
cnt--;
}else if(ch11!='*' && r-1>=0 && c-1>=0 && a[r-1][c-1]=='*') {
cnt--;
}else if(ch12!='*' && r-1>=0 && c+2<m && a[r-1][c+2]=='*') {
cnt--;
}
}
if(cnt==0) {
return 1;
}
if(cnt>3) {
return 4;
}
if(cnt<3) {
return 2;
}
if((ch11=='*' && ch12=='*' && ch22=='*' && ch21!='*')|| (ch11=='*' && ch21=='*' && ch22=='*' && ch12!='*') || (ch12=='*'&&ch22=='*'&&ch21=='*'&&ch11!='*')||(ch11=='*'&&ch12=='*'&&ch21=='*'&&ch22!='*')) {
for(int i=r;i<r+2;i++) {
for(int j=c;j<c+2;j++) {
if(a[i][j]=='*') {
mark[i][j] = true;
}
}
}
return 1;
}
return 1;
}
public static void addTime(int h, int m, int ah, int am) {
int rm = m+am;
int rh = (h+ah+(rm/60))%24;
rm = rm%60;
}
public static long power(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
} | 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 11 | 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 | e2980fe047c30187b1a3eca9421ea1c0 | 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.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
public class Solution{
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static class Pair{
int s;
int end;
public Pair(int a, int e) {
end = e;
s = a;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
public static void main(String[] args) throws Exception
{
FastReader scn = new FastReader();
OutputStream outputStream = System.out;
OutputWriter output = new OutputWriter(outputStream);
int t = scn.nextInt();
long mod = 1000000007L;
// String[] pal = {"0000", "0110","0220","0330","0440","0550","1001","1111","1221","1331","1441","1551","2002","2112","2222","2332"};
while(t>0) {
String s = scn.next();
int n = s.length();
List<Integer> a = new ArrayList<>();
// a.add(0);
List<HashSet<Integer>> set = new ArrayList<>();
for(int i=0;i<26;i++) {
set.add(new HashSet<>());
}
if(s.charAt(0)>=s.charAt(n-1)) {
for(int i=1;i<n-1;i++) {
if(s.charAt(i)<=s.charAt(0) && s.charAt(i)>=s.charAt(n-1)) {
set.get((s.charAt(i)-'a')).add(i+1);
}
}
a.add(1);
for(char c = s.charAt(0);c>=s.charAt(n-1);c--) {
for(int idx: set.get(c-'a')) {
a.add(idx);
}
}
a.add(n);
}else {
for(int i=1;i<n-1;i++) {
if(s.charAt(i)>=s.charAt(0) && s.charAt(i)<=s.charAt(n-1)) {
set.get((s.charAt(i)-'a')).add(i+1);
}
}
a.add(1);
for(char c = s.charAt(0);c<=s.charAt(n-1);c++) {
for(int idx: set.get(c-'a')) {
a.add(idx);
}
}
a.add(n);
}
output.println(Math.abs(s.charAt(n-1)-s.charAt(0))+" "+a.size());
for(int x: a) {
output.print(x+" ");
}
output.println();
t--;
}
output.close();
}
static int CeilIndex(int A[], int l, int r, int key)
{
while (r - l > 1) {
int m = l + (r - l) / 2;
if (A[m] >= key)
r = m;
else
l = m;
}
return r;
}
static int lis(int A[], int size)
{
// Add boundary case, when array size is one
int[] tailTable = new int[size];
int len; // always points empty slot
tailTable[0] = A[0];
len = 1;
for (int i = 1; i < size; i++) {
if (A[i] < tailTable[0])
// new smallest value
tailTable[0] = A[i];
else if (A[i] > tailTable[len - 1])
// A[i] wants to extend largest subsequence
tailTable[len++] = A[i];
else
// A[i] wants to be current end candidate of an existing
// subsequence. It will replace ceil value in tailTable
tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i];
}
return len;
}
public static int checkL(char[][] a, int r, int c, boolean[][] mark) {
int cnt=0;
for(int i=Math.max(r-1,0);i<=Math.min(r+2,a.length-1);i++) {
for(int j= Math.max(c-1,0);j<=Math.min(c+2,a[0].length-1);j++) {
if(a[i][j]=='*') {
cnt++;
}
}
}
char ch11 = a[r][c];
char ch12 = a[r][c+1];
char ch21 = a[r+1][c];
char ch22 = a[r+1][c+1];
int n = a.length;
int m = a[0].length;
if(cnt==4) {
if(ch22!='*' && r+2<n && c+2<m && a[r+2][c+2]=='*') {
cnt--;
}else if(ch21!='*' && r+2<n && c-1>=0 && a[r+2][c-1]=='*') {
cnt--;
}else if(ch11!='*' && r-1>=0 && c-1>=0 && a[r-1][c-1]=='*') {
cnt--;
}else if(ch12!='*' && r-1>=0 && c+2<m && a[r-1][c+2]=='*') {
cnt--;
}
}
if(cnt==0) {
return 1;
}
if(cnt>3) {
return 4;
}
if(cnt<3) {
return 2;
}
if((ch11=='*' && ch12=='*' && ch22=='*' && ch21!='*')|| (ch11=='*' && ch21=='*' && ch22=='*' && ch12!='*') || (ch12=='*'&&ch22=='*'&&ch21=='*'&&ch11!='*')||(ch11=='*'&&ch12=='*'&&ch21=='*'&&ch22!='*')) {
for(int i=r;i<r+2;i++) {
for(int j=c;j<c+2;j++) {
if(a[i][j]=='*') {
mark[i][j] = true;
}
}
}
return 1;
}
return 1;
}
public static void addTime(int h, int m, int ah, int am) {
int rm = m+am;
int rh = (h+ah+(rm/60))%24;
rm = rm%60;
}
public static long power(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
} | 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 11 | 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 | 7d80137d86c525518202916a71e40bca | 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 Himanshu
**/
import java.util.*;
import java.io.*;
public class C1729 {
static final int ALPHABET_SIZE = 26;
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
Reader s = new Reader();
int t = s.i();
while (t-- > 0) {
char [] arr = s.s().toCharArray();
int n = arr.length;
// List<Integer> list = new ArrayList<>();
// char first = arr[0];
// char second = arr[n-1];
// for (int i=1;i<n-1;i++) {
// char c = arr[i];
// if ((c >= first && c <= second) || (c <= first && c >= second)) {
// list.add(i);
// }
// }
// Collections.sort(list,(a,b) -> (arr[a] == arr[b]) ? a-b : arr[a]-arr[b]);
//
if (arr[0] > arr[n-1]) {
pairs [] a = new pairs[n];
for (int i=0;i<n;i++)
a[i] = new pairs(arr[i],i+1);
Arrays.sort(a,new pairs());
int src = getIndex(a,arr[0],n,1);
int dest = getIndex(a,arr[n-1],n,n);
int len = Math.abs(src-dest) + 1;
int val = Math.abs(arr[n-1]-arr[0]);
out.println(val + " " + len);
for (int i = src; i >= dest; i--)
out.print(a[i].second + " ");
} else {
pair [] a = new pair[n];
for (int i=0;i<n;i++)
a[i] = new pair(arr[i],i+1);
Arrays.sort(a,new pair());
int src = getIndex(a,arr[0],n,1);
int dest = getIndex(a,arr[n-1],n,n);
int len = Math.abs(src-dest) + 1;
int val = Math.abs(arr[n-1]-arr[0]);
out.println(val + " " + len);
for (int i = src; i <= dest; i++)
out.print(a[i].second + " ");
}
out.println();
}
out.flush();
}
private static int getIndex(pair [] a , char c, int n, int idx) {
for (int i=0;i<n;i++) {
pair p = a[i];
if (p.first == c && p.second == idx) return i;
}
return -1;
}
private static int getIndex(pairs [] a , char c, int n, int idx) {
for (int i=0;i<n;i++) {
pairs p = a[i];
if (p.first == c && p.second == idx) return i;
}
return -1;
}
public static void shuffle(long[] arr) {
int n = arr.length;
Random rand = new Random();
for (int i = 0; i < n; i++) {
long temp = arr[i];
int randomPos = i + rand.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = temp;
}
}
private static long phi(long n) {
long result = n;
for (long i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0)
n /= i;
result -= result / i;
}
}
if (n > 1)
result -= result / n;
return result;
}
private static int gcd(int a, int b) {
if(b == 0) return a;
return gcd(b,a%b);
}
public static long nCr(long[] fact, long[] inv, int n, int r, long mod) {
if (n < r)
return 0;
return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod;
}
private static void factorials(long[] fact, long[] inv, long mod, int n) {
fact[0] = 1;
inv[0] = 1;
for (int i = 1; i <= n; ++i) {
fact[i] = (fact[i - 1] * i) % mod;
inv[i] = power(fact[i], mod - 2, mod);
}
}
private static long power(long a, long n, long p) {
long result = 1;
while (n > 0) {
if (n % 2 == 0) {
a = (a * a) % p;
n /= 2;
} else {
result = (result * a) % p;
n--;
}
}
return result;
}
private static long power(long a, long n) {
long result = 1;
while (n > 0) {
if (n % 2 == 0) {
a = (a * a);
n /= 2;
} else {
result = (result * a);
n--;
}
}
return result;
}
private static long query(long[] tree, int in, int start, int end, int l, int r) {
if (start >= l && r >= end) return tree[in];
if (end < l || start > r) return 0;
int mid = (start + end) / 2;
long x = query(tree, 2 * in, start, mid, l, r);
long y = query(tree, 2 * in + 1, mid + 1, end, l, r);
return x + y;
}
private static void update(int[] arr, long[] tree, int in, int start, int end, int idx, int val) {
if (start == end) {
tree[in] = val;
arr[idx] = val;
return;
}
int mid = (start + end) / 2;
if (idx > mid) update(arr, tree, 2 * in + 1, mid + 1, end, idx, val);
else update(arr, tree, 2 * in, start, mid, idx, val);
tree[in] = tree[2 * in] + tree[2 * in + 1];
}
private static void build(int[] arr, long[] tree, int in, int start, int end) {
if (start == end) {
tree[in] = arr[start];
return;
}
int mid = (start + end) / 2;
build(arr, tree, 2 * in, start, mid);
build(arr, tree, 2 * in + 1, mid + 1, end);
tree[in] = (tree[2 * in + 1] + tree[2 * in]);
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String s() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long l() {
int c = read();
while (isSpaceChar(c)) c = read();
int 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 int i() {
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 double d() throws IOException {
return Double.parseDouble(s());
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int[] arr(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = i();
}
return ret;
}
public long[] arrLong(int n) {
long[] ret = new long[n];
for (int i = 0; i < n; i++) {
ret[i] = l();
}
return ret;
}
}
static class TrieNode {
TrieNode[] children = new TrieNode[ALPHABET_SIZE];
boolean isLeaf;
boolean isPalindrome;
public TrieNode() {
isLeaf = false;
isPalindrome = false;
for (int i = 0; i < ALPHABET_SIZE; i++)
children[i] = null;
}
}
// static class pairLong implements Comparator<pairLong> {
// long first, second;
//
// pairLong() {
// }
//
// pairLong(long first, long second) {
// this.first = first;
// this.second = second;
// }
//
// @Override
// public int compare(pairLong p1, pairLong p2) {
// if (p1.first == p2.first) {
// if(p1.second > p2.second) return 1;
// else return -1;
// }
// if(p1.first > p2.first) return 1;
// else return -1;
// }
// }
static class pair implements Comparator<pair> {
int second;
char first;
pair() {
}
pair(char first, int second) {
this.first = first;
this.second = second;
}
@Override
public int compare(pair p1, pair p2) {
if (p1.first == p2.first) {
return p1.second - p2.second;
}
if (p1.first > p2.first) return 1;
else return -1;
}
}
static class pairs implements Comparator<pairs> {
int second;
char first;
pairs() {
}
pairs(char first, int second) {
this.first = first;
this.second = second;
}
@Override
public int compare(pairs p1, pairs p2) {
if (p1.first == p2.first) {
return p2.second - p1.second;
}
if (p1.first > p2.first) return 1;
else return -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 11 | 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 | d40217fc3a6a5d6fabb466b7cfec4b0c | 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 File {
public static void main(String[] args) {
sc=new Scanner(System.in);
if(singleTestCase==0){
int t=sc.nextInt();
while(t-->0){
solve();
}
}
else{
solve();
}
}
static Scanner sc;
static int singleTestCase=0;
public static void solve(){
String s=sc.next();
// char []arr=s.toCharArray();
// Arrays.sort(arr);
int cost=Math.abs(s.charAt(0)-s.charAt(s.length()-1));
int cc=0;
Map<Character,List<Integer>> map=new HashMap<>();
for(int i=0;i<s.length();i++){
char tmp=s.charAt(i);
if(map.containsKey(tmp)){
map.get(tmp).add(i+1);
}
else{
List<Integer> ind=new ArrayList<>();
ind.add(i+1);
map.put(tmp,ind);
}
}
List<Integer> indi=new ArrayList<>();
if(s.charAt(s.length()-1)>s.charAt(0)){
for(char i=s.charAt(0);i<=s.charAt(s.length()-1);i++){
if(map.containsKey(i)){
cc+=map.get(i).size();
indi.addAll(map.get(i));
}
}
System.out.println(cost+" "+cc);
for(int i=0;i<indi.size();i++){
System.out.print(indi.get(i)+" ");
}
System.out.println();
}
else{
// System.out.println(map);
for(char i=s.charAt(0);i>=s.charAt(s.length()-1);i--){
if(map.containsKey(i)){
cc+=map.get(i).size();
indi.addAll(map.get(i));
}
}
System.out.println(cost+" "+cc);
for(int i=0;i<indi.size();i++){
System.out.print(indi.get(i)+" ");
}
System.out.println();
}
}
public static boolean isIn(int x,int y,int n,int m){
if(x>=0 && x<n && y>=0 && y<m){
return true;
}
else{
return false;
}
}
public static void printArrayInLine(int []arr){
for (int i : arr) {
System.out.print(i + " ");
}
System.out.println();
}
public static int sumOfArray(int []arr){
int sum=0;
for (int j : arr) {
sum += j;
}
return sum;
}
public static void printIntMatrixInLine(int [][]arr,int isOneIndexed,String space){
for (int i=isOneIndexed;i<arr.length;i++) {
for(int j=isOneIndexed;j<arr[0].length;j++){
System.out.print(arr[i][j]+space);
}
System.out.println();
}
System.out.println();
}
public static void printCharMatrixInLine(char [][]arr,int isOneIndexed,String space){
for (int i=isOneIndexed;i<arr.length;i++) {
for(int j=isOneIndexed;j<arr[0].length;j++){
System.out.print(arr[i][j]+space);
}
System.out.println();
}
System.out.println();
}
public static int[] takeInputArray(long length){
int []arr=new int[(int)length];
for(int i=0;i<length;i++){
arr[i]= sc.nextInt();
}
return arr;
}
public static long[] takeLongArrayInput(long length){
long []arr=new long[(int)length];
for(int i=0;i<length;i++){
arr[i]= sc.nextLong();
}
return arr;
}
public static String[] takeStringInputArray(long length){
String []arr=new String[(int)length];
for(int i=0;i<length;i++){
arr[i]= sc.next();
}
return arr;
}
public static boolean isPalindrome(String str){
for(int i=0,j=str.length()-1;j>i;i++,j--){
if(str.charAt(i)!=str.charAt(j)){
return false;
}
}
return true;
}
} | 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 11 | 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 | 494dd5cdcaa9ef637ef766ad6822e97b | 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 static java.lang.Math.*;
import static java.lang.System.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
// import java.util.*;
public class Q3{
// static class pair implements Comparable<pair>
// {
// int index;
// char ch;
// pair(int i, char c)
// {
// index = i;
// ch = c;
// }
// @Override
// public int compareTo(Q3.pair o) {
// return ch-o.ch;
// }
// }
public static void main(String[] args)throws IOException {
// Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
for(int z=0;z<t;z++){
String s = br.readLine();
ArrayList<ArrayList<Integer>> index = new ArrayList<ArrayList<Integer>>();
for(int i=0;i<26;i++){
index.add(new ArrayList<>());
}
int m = 2;
int cost = 0;
int x = s.charAt(0)-'a';
int y = s.charAt(s.length()-1)-'a';
cost = abs(x-y);
int a = x<=y?x:y;
int b = x>=y?x:y;
for(int i =1;i<s.length()-1;i++)
{
char temp = s.charAt(i);
int val = temp-'a';
if(val<=b && val>=a)
{
m++;
index.get(temp-'a').add(i);
}
}
out.println(cost + " " + m);
StringBuffer answer = new StringBuffer();
if(x<y)
{
for(int i=0;i<index.size();i++)
{
for(Integer j : index.get(i) )
{
answer.append((j+1)+ " ");
}
} }
else{
for(int i=index.size()-1;i>=0;i--)
{
for(Integer j : index.get(i) )
{
answer.append((j+1)+ " ");
}
} }
out.println("1 " + answer + s.length());
}
}
} | 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 11 | 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 | 97a90175c1437bbcfdd302ed94a52047 | 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 class pair{
long x ;long y ;
pair(long x ,long y )
{
this.x=x;
this.y =y;
}
}
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 prlong(Object object) throws IOException {
bw.append("" + object);
}
public void prlongln(Object object) throws IOException {
prlong(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
//dsu
static int[] parent,rank;
public static void dsu(int n){
parent = new int[n]; rank = new int[n];
for(int i =0;i<n;i++){
parent[i]=i; rank[i]=1;
}
}
public static int find(int i){
if(i==parent[i] ) return i;
return parent[i]=find(parent[i]);
}
static int f(int arr[],int i )
{
if(i==arr[i])
return i ;
return arr[i]=f(arr,arr[i]);
}
public static void merge(int i, int j){
i=find(i);
j=find(j);
if(rank[i]>=rank[j]){
rank[i]+=rank[j];
parent[j]=i;
}
else {
rank[j]+=rank[i];
parent[i]=j;
}
}
public static int help(boolean[] v){
HashSet<Integer> s = new HashSet<>();
for(int e=1;e<v.length;e++){
int i=find(e);
if(v[i]) s.add(i);
}
// System.out.print(Arrays.toString(parent));
return s.size();
}
static int help(int[][] arr, int i, int n, int[] heigh) {
if(arr[i][0]==-1&&arr[i][1]==-1)
return 0;
if(arr[i][0]==-1)
{
return heigh[arr[i][1]]-1;
}
else if(arr[i][1]==-1)
return heigh[arr[i][0]]-1;
else
{
int op1 =heigh[arr[i][1]]-1+help(arr, arr[i][0], n, heigh);
int op2 =heigh[arr[i][0]]-1+help(arr, arr[i][1], n, heigh);
return Math.max(op1,op2);
}
}
static int heights(int arr[][], int i , int heig[])
{
if(arr[i][0]==-1&&arr[i][1]==-1)
return 0;
if(heig[i]!=-1)
return heig[i];
if(arr[i][0]==-1)
return heig[i]=1+heights(arr,arr[i][1],heig);
if(arr[i][1]==-1)
return heig[i]=1+heights(arr,arr[i][0],heig);
else return heig[i]= 1+heights(arr,arr[i][1],heig)+heights(arr,arr[i][0],heig);
}
public static void main(String[] args) {
try {
FastReader in = new FastReader();
FastWriter out = new FastWriter();
long testCases =in.nextLong();
fl:
while (testCases-- > 0) {
String s =in.next();
int n =s.length();
int arr[][]=new int[n-2][2];
for(int i =0;i<n-2;i++)
{
arr[i][0]=s.charAt(i+1)-'a';
arr[i][1]=i+2;
}
Arrays.sort(arr,(a,b)->Integer.compare(a[0],b[0]));
ArrayList<Integer>ans =new ArrayList<>();
int temp =s.charAt(0)-'a';ans.add(1);
if(s.charAt(0)-'a'<s.charAt(n-1)-'a')
{
long sum =0;
for(int i =0;i<n-2;i++)
{
if(arr[i][0]>=s.charAt(0)-'a'&&arr[i][0]<=s.charAt(n-1)-'a'){
ans.add(arr[i][1]);
sum =sum+arr[i][0]-temp;
temp=arr[i][0];
}
}
sum =sum+(s.charAt(n-1)-'a')-temp;
ans.add(n);
System.out.println(sum+" "+ans.size());
for(int i :ans)
System.out.print(i+" ");
System.out.println();
continue fl;
}
long sum =0;
temp=s.charAt(n-1)-'a';
for(int i =n-3;i>=0;i--)
{
if(arr[i][0]<=s.charAt(0)-'a'&&arr[i][0]>=s.charAt(n-1)-'a'){
ans.add(arr[i][1]);
sum =sum+arr[i][0]-temp;
temp=arr[i][0];
}
}
sum =sum+(s.charAt(0)-'a')-temp;
ans.add(n);
System.out.println(sum+" "+ans.size());
for(int i :ans)
System.out.print(i+" ");
System.out.println();
}
out.close();
} catch (Exception e) {
return;
}
}
} | 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 11 | 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 | 66a51d7318df0af44563a8f5794feafe | 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.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
public class Main {
static final long MOD1=1000000007;
static final long MOD=998244353;
static final int NTT_MOD1 = 998244353;
static final int NTT_MOD2 = 1053818881;
static final int NTT_MOD3 = 1004535809;
static long MAX = 1000000000000000000l;//10^18
static int index = 2;
public static void main(String[] args){
PrintWriter out = new PrintWriter(System.out);
InputReader sc=new InputReader(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
char[] cs = sc.next().toCharArray();
int n = cs.length;
int u = Math.min(c(cs[0]), c(cs[n - 1]));
int v = Math.max(c(cs[0]), c(cs[n - 1]));
int num = 0;
for (int j = 0; j < n; j++) {
if(c(cs[j]) >= u && c(cs[j]) <= v) num++;
}
out.println((v - u)+" "+num);
StringBuilder sb = new StringBuilder();
if (c(cs[0]) > c(cs[n - 1])) {
for (int j = v; j >= u; j--) {
for (int j2 = 0; j2 < n; j2++) {
if(c(cs[j2]) == j) sb.append(((j2 + 1)+" "));
}
}
}else {
for (int j = u; j <= v; j++) {
for (int j2 = 0; j2 < n; j2++) {
if(c(cs[j2]) == j) sb.append(((j2 + 1)+" "));
}
}
}
out.println(sb);
}
out.flush();
}
static int c(char a) {
return (int)a - (int)'a';
}
static class InputReader {
private InputStream in;
private byte[] buffer = new byte[1024];
private int curbuf;
private int lenbuf;
public InputReader(InputStream in) {
this.in = in;
this.curbuf = this.lenbuf = 0;
}
public boolean hasNextByte() {
if (curbuf >= lenbuf) {
curbuf = 0;
try {
lenbuf = in.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return false;
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[curbuf++];
else
return -1;
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private void skip() {
while (hasNextByte() && isSpaceChar(buffer[curbuf]))
curbuf++;
}
public boolean hasNext() {
skip();
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (!isSpaceChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public double[] nextDoubleArray(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++)
a[i] = nextDouble();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[][] nextCharMap(int n, int m) {
char[][] map = new char[n][m];
for (int i = 0; i < n; i++)
map[i] = next().toCharArray();
return map;
}
}
} | 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 11 | 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 | 77701fb2e6beda076fc32dd75c8be717 | 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.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int tests = Integer.parseInt(sc.next());
for (int t = 0; t < tests; t++) {
String s = sc.next();
List<Integer> result = new ArrayList<>();
int cost;
if (s.charAt(s.length() - 1) >= s.charAt(0)) {
cost = s.charAt(s.length() - 1) - s.charAt(0);
for (char letter = s.charAt(0); letter <= s.charAt(s.length() - 1); letter++) {
for(int i=0; i< s.length(); i++) {
if (s.charAt(i)== letter) {
result.add(i+1);
}
}
}
} else {
cost = s.charAt(0) - s.charAt(s.length() - 1);
for (char letter = s.charAt(0); letter >= s.charAt(s.length() - 1); letter--) {
for(int i=0; i< s.length(); i++) {
if (s.charAt(i)== letter) {
result.add(i+1);
}
}
}
}
pw.println(cost +" "+ result.size());
for(int i=0; i< result.size(); i++) {
pw.print(result.get(i)+ " ");
}
pw.println();
}
sc.close();
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 11 | 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 | 9be0c6a19f212ec77abdb3c01fab7e3e | 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.*;
import java.math.*;
public class Codechef{
public static void main(String[] args) throws IOException {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
Scanner sc=new Scanner(System.in);
long t=fs.nextLong();
while(t-->0){
StringBuilder s=new StringBuilder("");
s.append(fs.next());
char start=s.charAt(0);
char end=s.charAt(s.length()-1);
char a='a';
char b='b';
if(start<=end){
a=start;
b=end;
}
else{
a=end;
b=start;
}
ArrayList<Integer>[] arr=new ArrayList[b-a+1];
for(int i=0;i<Math.abs(b-a+1);i++){
arr[i]=new ArrayList<>();
}
long count=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)>=a&&s.charAt(i)<=b){
arr[s.charAt(i)-a].add(i+1);
count++;
}
}
out.println(b-a+" "+count);
if(start<=end){
for(int i=0;i<b-a+1;i++){
if(arr[i].size()!=0)
for(int j=0;j<arr[i].size();j++)
out.print(arr[i].get(j)+" ");
}
}
else{
for(int i=start-end;i>=0;i--){
if(arr[i].size()!=0)
for(int j=0;j<arr[i].size();j++)
out.print(arr[i].get(j)+" ");
}
}
out.println();
}
out.close();
}
public static long fact(long number) {
long f = 1;
long j = 1;
while(j <= number) {
f = f * j;
j++;
}
return f;
}
/* HELPER FUNCTION's */
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;
}
/* fast exponentiation */
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));
}
/* end of fast exponentiation */
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);
}
// lcm(a,b) * gcd(a,b) = a * b
public static long _lcm(long a, long b) {
return (a / _gcd(a, b)) * b;
}
// euclidean algorithm time O(max (loga ,logb))
public static long _gcd(long a, long b) {
while (a > 0) {
long x = a;
a = b % a;
b = x;
}
return b;
}
/* Pair Class implementation */
static class Pair<K, V> {
K ff;
V ss;
public Pair(K ff, V ss) {
this.ff = ff;
this.ss = ss;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || this.getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return ff.equals(pair.ff) && ss.equals(pair.ss);
}
@Override
public int hashCode() {
return Objects.hash(ff, ss);
}
@Override
public String toString() {
return ff.toString() + " " + ss.toString();
}
}
/* pair class ends here */
/* fast input output class */
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[] readArrayL(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| 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 11 | 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 | d1d234c6c1c4553271489484a29f6e73 | 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.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws Exception {
try (BufferedInputStream in = new BufferedInputStream(System.in);
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) {
Scanner sc = new Scanner(in).useLocale(Locale.US);
int T = sc.nextInt();
for (int t = 0; t < T; t++) {
char[] cs = sc.next().toCharArray();
ArrayList<Integer>[] d = new ArrayList[((int) 'z') + 1];
for (int i = 1; i < cs.length - 1; i++) {
int c = cs[i];
if (d[c] == null) d[c] = new ArrayList<>();
d[c].add(i);
}
List<Integer> path = new ArrayList<>();
boolean reverse = cs[0] > cs[cs.length - 1];
int start = reverse ? cs.length - 1 : 0;
int end = reverse ? 0 : cs.length - 1;
path.add(start);
for (int i = cs[start]; i <= cs[end]; i++) {
if (d[i] != null) path.addAll(d[i]);
}
path.add(end);
if (reverse) Collections.reverse(path);
out.println(String.format("%s %s", Math.abs(cs[cs.length - 1] - cs[0]), path.size()));
for (Integer p : path) out.print((p + 1) + " ");
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 11 | 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 | 3797a34585a52c38b57c839a565cb643 | 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.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws Exception {
try (BufferedInputStream in = new BufferedInputStream(System.in);
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) {
Scanner sc = new Scanner(in).useLocale(Locale.US);
int T = sc.nextInt();
for (int t = 0; t < T; t++) {
char[] cs = sc.next().toCharArray();
ArrayList<Integer>[] d = new ArrayList[((int) 'z') + 1];
for (int i = 1; i < cs.length - 1; i++) {
int c = cs[i];
if (d[c] == null) d[c] = new ArrayList<>();
d[c].add(i);
}
int cost = 0;
List<Integer> path = new ArrayList<>();
boolean reverse = cs[0] > cs[cs.length - 1];
int start = reverse ? cs.length - 1 : 0;
int end = reverse ? 0 : cs.length - 1;
path.add(start);
for (int i = cs[start]; i <= cs[end]; i++) {
if (d[i] != null) path.addAll(d[i]);
cost++;
}
path.add(end);
cost--;
if (reverse) Collections.reverse(path);
out.println(String.format("%s %s", cost, path.size()));
for (Integer p : path) out.print((p + 1) + " ");
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 11 | 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 | 9a6558eaaaba3793b18c2696c7542c8b | 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 MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// System.out.println((int)('s'-'c'));
int t = sc.nextInt();
sc.nextLine();
while(t>0){
t--;
String s = sc.nextLine();
int n = s.length();
ArrayList<Integer> [] arr = new ArrayList [26];
for(int i=0;i<26;i++){
arr[i] = new ArrayList<Integer>();
}
if(s.charAt(0)>s.charAt(n-1)){
int m=2;
for(int i=1;i<n-1;i++){
if(s.charAt(i)<=s.charAt(0) && s.charAt(n-1)<=s.charAt(i)){
m++;
arr[s.charAt(i)-'a'].add(i);
}
}
int cost = s.charAt(0)-s.charAt(n-1);
System.out.println(cost+" "+m);
System.out.print(1+" ");
for(int i=25;i>=0;i--){
for(Integer p : arr[i]){
System.out.print(p+1+" ");
}
}
System.out.println(n);
}
else{
int m=2;
for(int i=1;i<n-1;i++){
if(s.charAt(i)>=s.charAt(0) && s.charAt(n-1)>=s.charAt(i)){
m++;
arr[s.charAt(i)-'a'].add(i);
}
}
int cost = s.charAt(n-1)-s.charAt(0);
System.out.println(cost+" "+m);
System.out.print(1+" ");
for(int i=0;i<26;i++){
for(Integer p : arr[i]){
System.out.print(p+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 11 | 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 | 1b6347d430efe6576520626b47e4d0b0 | 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.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
for (int t = 0; t < T; t++) {
char[] arr = br.readLine().toCharArray();
int n = arr.length;
List<Integer>[] list = new ArrayList[26];
for (int i = 0; i < 26; i++) {
list[i] = new ArrayList<>();
}
int s = (arr[0] - 'a');
int e = (arr[n - 1] - 'a');
for (int i = 0; i < n; i++) {
list[arr[i] - 'a'].add(i + 1);
}
List<Integer> path = new ArrayList<>();
if (s <= e) {
for (int j = s; j <= e; j++) {
path.addAll(list[j]);
}
} else {
for (int j = s; j >= e; j--) {
path.addAll(list[j]);
}
}
sb.append(Math.abs(s - e)).append(" ").append(path.size()).append("\n");
for (Integer p : path) {
sb.append(p).append(" ");
}
sb.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 11 | 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 | 18f57d49cb66546448c6348a3ea9e896 | 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 div3C820 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t-- > 0){
char c [] = scan.next().toCharArray();
int n = c.length;
ArrayList<Point> inds = new ArrayList<>();
for(int i = 0; i < n; i++){
inds.add(new Point(c[i] - 'a' + 1, i+1));
}
int a = inds.get(0).x;
int b = inds.get(inds.size()-1).x;
inds.remove(0);
inds.remove(inds.size()-1);
Collections.sort(inds);
ArrayList<Integer> ans = new ArrayList<>();
ans.add(1);
int dist = 0;
int cur = a;
if(a > b){
Collections.reverse(inds);
for(int i = 0; i < inds.size(); i++){
if(inds.get(i).x <= a && inds.get(i).x >= b){
ans.add(inds.get(i).y);
dist += cur-inds.get(i).x;
cur = inds.get(i).x;
}
}
dist += cur-b;
} else{
for(int i = 0; i < inds.size(); i++){
if(inds.get(i).x >= a && inds.get(i).x <= b){
ans.add(inds.get(i).y);
dist += inds.get(i).x - cur;
cur = inds.get(i).x;
}
}
dist += b-cur;
}
ans.add(n);
System.out.println(dist + " " + ans.size());
for(int i = 0; i < ans.size(); i++){
System.out.print(i == ans.size()-1 ? ans.get(i) : ans.get(i) + " ");
}
System.out.println();
}
}
static class Point implements Comparable<Point>{
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(Point o) {
return x - o.x;
}
}
}
| 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 11 | 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 | 9650b72e06b9f18a51e26e25b923e905 | 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 Codeforces
{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int test=sc.nextInt();
while(test-->0)
{
String s=sc.next();
int n=s.length();
ArrayList<ArrayList<Integer>> list=new ArrayList<>();
for(int i=0;i<26;i++)
{
list.add(new ArrayList<Integer>());
}
for(int i=1;i<n-1;i++)
{
char ch=s.charAt(i);
list.get(ch-'a').add(i+1);
}
char min=(char)Math.min(s.charAt(0),s.charAt(n-1));
char max=(char)Math.max(s.charAt(0),s.charAt(n-1));
//System.out.println(min+" "+max);
int start=min-'a';
int end=max-'a';
if(s.charAt(0)==min){
StringBuilder ans=new StringBuilder();
int pathcnt=0;
ans.append(1+" ");
pathcnt++;
for(int i=start;i<=end;i++)
{
int cnt=list.get(i).size();
for(int itr=0;itr<cnt;itr++)
{
ans.append(list.get(i).get(itr)+" ");
pathcnt++;
}
}
ans.append(n);
out.write((end-start)+" "+(pathcnt+1)+"\n");
out.write(ans+"\n");
}
else
{
StringBuilder ans=new StringBuilder();
int pathcnt=0;
ans.append(1+" ");
pathcnt++;
for(int i=end;i>=start;i--)
{
int cnt=list.get(i).size();
for(int itr=0;itr<cnt;itr++)
{
ans.append(list.get(i).get(itr)+" ");
pathcnt++;
}
}
ans.append(n);
out.write((end-start)+" "+(pathcnt+1)+"\n");
out.write(ans+"\n");
}
}
out.flush();
}
public static int lower_bound(int array[], int key)
{
int index = Arrays.binarySearch(array, key);
if (index < 0) return Math.abs(index) - 1;
else {
while (index > 0) {
if (array[index - 1] == key)
index--;
else
return index;
}
return index;
}
}
public static boolean canintersect(long xi,long yi,long xj,long yj)
{
return ((xi>=xj) && (xi<=yj))||((xj>=xi)&&(xj<=yi));
}
public static int getFirstSetBitPos(int n)
{
return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1;
}
public static boolean reverse(int[] array)
{
int n=array.length;
for(int i=0;i<n/2;i++)
{
int temp=array[i];
array[i]=array[n-i-1];
array[n-i-1]=temp;
}
return true;
}
public static int countDigit(long n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
public static String dectobin32(int x)
{
StringBuilder result = new StringBuilder();
for (int i = 31; i >= 0 ; i--)
{
int mask = 1 << i;
result.append((x & mask) != 0 ? 1 : 0);
}
//Integer.parseInt(result,2)
return result.toString();
}
public static boolean issorted(int arr[], int n)
{
if (n == 0 || n == 1)
return true;
for (int i = 1; i < n; i++)
if (arr[i - 1] > arr[i])
return false;
return true;
}
public static HashMap<Long,Long> primeFactors(long n)
{
HashMap<Long,Long> map = new HashMap<>();
long c = 2;
while (n > 1) {
if (n % c == 0) {
map.put(c,map.getOrDefault(c,0L)+1);
n /= c;
}
else
c++;
}
return map;
}
}
class Pair implements Comparable<Pair> {
long first,second;
public Pair(long first,long second)
{
this.first =first;
this.second=second;
}
public int compareTo(Pair b)
{
//first element in increasing order
if (this.first!=b.first)
return (this.first<b.first)?-1:1;
else
return this.second<b.second?-1:1;
//second element in incresing order
}
}
| 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 11 | 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 | adf220ef3014e491cb16fe735b3bc4b0 | 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 Aa {
static class Pair{
int value ;
int index ;
Pair(int value , int index){
this.value = value ;
this.index = index ;
}
}
public static int solve(int arr[],int n ) {
Arrays.sort(arr);
int max1 = arr[arr.length-1];
int max2 = arr[arr.length-2];
int min1 = arr[0] ;
int min2 = arr[1] ;
System.out.println(Arrays.toString(arr));
return max1+max2-min1-min2;
}
static void sort(int[] arr)
{
//because Arrays.sort() uses quicksort which is dumb
//Collections.sort() uses merge sort
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public static void main(String args[]) {
FastReader sc = new FastReader();
PrintWriter w = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0) {
String s = sc.next();
int n = s.length() ;
ArrayList<Pair> arr = new ArrayList<>();
for(int i = 0 ;i< n-2; i++) {
arr.add(new Pair(s.charAt(i+1)-97 +1,i+2));
}
Collections.sort(arr,new Comparator<Pair>() {
public int compare(Pair a, Pair b) {
if(a.value == b.value)
return a.index - b.index;
return a.value-b.value ;
}
});
// for(Pair p : arr) {
// System.out.println(p.value + " " +p.index);
// }
int cost = 0 ;
ArrayList<Integer> ans = new ArrayList<>();
if(s.charAt(0)>s.charAt(n-1)) {
ans.add(1);
int i= arr.size()-1;
while(i>=0) {
if(arr.get(i).value <= s.charAt(0)-97 +1 && arr.get(i).value>=s.charAt(n-1)-97 +1) {
ans.add(arr.get(i).index);
}
i--;
}
ans.add(s.length());
}
else {
ans.add(1);
int i= 0 ;
while(i<arr.size()) {
if(arr.get(i).value>=s.charAt(0)-97 +1 && arr.get(i).value <= s.charAt(n-1)-97 +1) {
ans.add(arr.get(i).index);
}
i++;
}
ans.add(s.length());
}
w.println(Math.abs(s.charAt(0)-s.charAt(n-1)) + " " + ans.size());
for(int e : ans) {
w.print(e + " ");
}
w.println();
}
w.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 {
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 11 | 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 | aaace801006b84d060164895dafc996d | 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.util.Scanner;
import java.io.*;
import java.math.BigInteger;
import java.util.stream.*;
import java.util.ArrayList;
import java.lang.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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.io.StreamTokenizer;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
static long mod = (long)(1e9) + 7;
static int max_num = (int)1e5 + 5;
public static void main(String[] args) {
FastReader in = new FastReader();
FastWriter out = new FastWriter();
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while (t-- > 0) {
String str = scn.next();
int pari = str.length() - 1;
int[][] arr = new int[str.length()][2];
for (int i = 1 ; i < arr.length - 1; i++) {
char ch = str.charAt(i);
int jj = (int)ch;
int jj1 = jj - 96;
arr[i][0] = jj1;
arr[i][1] = i + 1;
// System.out.println(arr[i][0]+" " + arr[i][1]);
}
int st = 0;
char ch1 = str.charAt(0);
int jj2 = (int)ch1;
int jj4 = jj2 - 96;
char ch2 = str.charAt(pari);
int jj3 = (int)ch2;
int jj5 = jj3 - 96;
Arrays.sort(arr, (a, b) -> a[0] - b[0]);
// System.out.println("imp"+jj4);
// System.out.print(jj5);
ArrayList<Integer> list = new ArrayList<>();
// if (jj4 >= jj5) {
// while(arr[i][0] != (int)str.charAt(pari)-96){
for (int i = 0 ; i < arr.length ; i++) {
if(jj4 >= jj5){
if (arr[i][0] <= jj4 && arr[i][0] >= jj5) {
list.add(arr[i][1]);
}
}
else{
if (arr[i][0] >= jj4 && arr[i][0] <= jj5) {
list.add(arr[i][1]);
}
}
}
if(jj4 > jj5){
Collections.reverse(list);
}
System.out.print(Math.abs(jj4-jj5) + " ");
System.out.println(list.size() + 2);
System.out.print(1 + " ");
for(int i = 0 ; i < list.size() ; i++){
System.out.print(list.get(i)+ " ");
}
System.out.println(str.length());
}
}
public static void solve(String s) {
}
// agar 2d array ke ek coloumn ko sort karna h to -> Arrays.sort(arr, (a, b) -> a[0] - b[0]);
// chote a ka ascii code 97
// 0 ka ascii code 48
// bade A ka ascii code 65
// int ko char array m karne h to-> char[] arr = (n+"").toCharArray();
// String ko integer banane ke lia -> Integer.parseInt("10");
// dp[2][n] tej chalta h dp[n][2] se
public static int countDigit(long n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
public static boolean checkPalin(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;
}
public static int maxInArray(int[] arr) {
int max = arr[0];
for (int i = 0 ; i < arr.length ; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
static long square(long n)
{
if (n == 0)
return 0;
if (n < 0)
n = -n;
long x = n >> 1;
if (n % 2 != 0)
return ((square(x) << 2) + (x << 2) + 1);
else
return (square(x) << 2);
}
static int square(int n)
{
if (n == 0)
return 0;
if (n < 0)
n = -n;
int x = n >> 1;
if (n % 2 != 0)
return ((square(x) << 2) + (x << 2) + 1);
else
return (square(x) << 2);
}
public static void printString(String str) {
System.out.println(str);
}
public static void printCharArray(char[] arr) {
for (int i = 0 ; i < arr.length ; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static <K, V> K getKey(Map<K, V> map, V value) {
for (Map.Entry<K, V> entry : map.entrySet()) {
if (value.equals(entry.getValue())) {
return entry.getKey();
}
}
return null;
}
public static void printArray(int[] arr) {
for (int i = 0 ; i < arr.length ; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void print2dArray(int[][] arr){
for(int i = 0 ; i < arr.length ; i++){
for(int j = 0 ; j < arr[0].length ; j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
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 highestPowerof2(int n) {
int res = 0;
for (int i = n; i >= 1; i--) {
if ((i & (i - 1)) == 0) {
res = i;
break;
}
}
return res;
}
public static int countElementInArray(int[] arr, int k) {
int cc = 0;
for (int i = 0 ; i < arr.length ; i++ ) {
if (arr[i] == k) {
cc++;
}
}
return cc;
}
public static long integerFromBinary(String str) {
long j = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '1') {
j = j + (long)Math.pow(2, str.length() - 1 - i);
}
}
return (long) j;
}
static int minInArray(int[] arr) {
int min = arr[0];
for (int i = 0 ; i < arr.length ; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
static void printN() {
System.out.println("No");
}
static void printY() {
System.out.println("Yes");
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for (int v : a) {
c0[(v & 0xff) + 1]++;
c1[(v >>> 8 & 0xff) + 1]++;
c2[(v >>> 16 & 0xff) + 1]++;
c3[(v >>> 24 ^ 0x80) + 1]++;
}
for (int i = 0; i < 0xff; i++) {
c0[i + 1] += c0[i];
c1[i + 1] += c1[i];
c2[i + 1] += c2[i];
c3[i + 1] += c3[i];
}
int[] t = new int[n];
for (int v : a)t[c0[v & 0xff]++] = v;
for (int v : t)a[c1[v >>> 8 & 0xff]++] = v;
for (int v : a)t[c2[v >>> 16 & 0xff]++] = v;
for (int v : t)a[c3[v >>> 24 ^ 0x80]++] = v;
return a;
}
static int[] EvenOddArragement(int nums[])
{
int i1 = 0, i2 = nums.length - 1;
while (i1 < i2) {
while (nums[i1] % 2 == 0 && i1 < i2) {
i1++;
}
while (nums[i2] % 2 != 0 && i2 > i1) {
i2--;
}
int temp = nums[i1];
nums[i1] = nums[i2];
nums[i2] = temp;
}
return nums;
}
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();
}
}
static int gcd(int a, int b) {
while (b != 0) {
int t = a;
a = b;
b = t % b;
}
return a;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static int DigitSum(int n)
{
int r = 0, sum = 0;
while (n >= 0)
{
r = n % 10;
sum = sum + r;
n = n / 10;
}
return sum;
}
static boolean checkPerfectSquare(int number)
{
double sqrt = Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static boolean isPowerOfTwo(int n)
{
if (n == 0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static boolean isPrime2(int n)
{
if (n <= 1)
{
return false;
}
if (n == 2)
{
return true;
}
if (n % 2 == 0)
{
return false;
}
for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
static String minLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for (int i = 0; i < n; i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[0];
}
static String maxLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for (int i = 0; i < n; i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[arr.length - 1];
}
static class P implements Comparable<P> {
int i, j;
public P(int i, int j) {
this.i = i;
this.j = j;
}
public int compareTo(P o) {
return Integer.compare(i, o.i);
}
}
static class pair {
int i, j;
pair(int x, int y) {
i = x;
j = y;
}
}
static int binary_search(int a[], int value)
{
int start = 0;
int end = a.length - 1;
int mid = start + (end - start) / 2;
while (start <= end)
{
if (a[mid] == value)
{
return mid;
}
if (a[mid] > value)
{
end = mid - 1;
}
else
{
start = mid + 1;
}
mid = start + (end - start) / 2;
}
return -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 11 | 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 | 03ebd98a12ac38a41b396f973f1dbfef | 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.util.Scanner;
import java.io.*;
import java.math.BigInteger;
import java.util.stream.*;
import java.util.ArrayList;
import java.lang.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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.io.StreamTokenizer;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
static long mod = (long)(1e9) + 7;
static int max_num = (int)1e5 + 5;
public static void main(String[] args) {
FastReader in = new FastReader();
FastWriter out = new FastWriter();
Scanner scn = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
String str = in.next();
int pari = str.length() - 1;
int[][] arr = new int[str.length()][2];
for (int i = 1 ; i < arr.length - 1; i++) {
char ch = str.charAt(i);
int jj = (int)ch;
int jj1 = jj - 96;
arr[i][0] = jj1;
arr[i][1] = i + 1;
// System.out.println(arr[i][0]+" " + arr[i][1]);
}
int st = 0;
char ch1 = str.charAt(0);
int jj2 = (int)ch1;
int jj4 = jj2 - 96;
char ch2 = str.charAt(pari);
int jj3 = (int)ch2;
int jj5 = jj3 - 96;
Arrays.sort(arr, (a, b) -> a[0] - b[0]);
// System.out.println("imp"+jj4);
// System.out.print(jj5);
ArrayList<Integer> list = new ArrayList<>();
// if (jj4 >= jj5) {
// while(arr[i][0] != (int)str.charAt(pari)-96){
for (int i = 0 ; i < arr.length ; i++) {
if(jj4 >= jj5){
if (arr[i][0] <= jj4 && arr[i][0] >= jj5) {
list.add(arr[i][1]);
}
}
else{
if (arr[i][0] >= jj4 && arr[i][0] <= jj5) {
list.add(arr[i][1]);
}
}
}
if(jj4 > jj5){
Collections.reverse(list);
}
System.out.print(Math.abs(jj4-jj5) + " ");
System.out.println(list.size() + 2);
System.out.print(1 + " ");
for(int i = 0 ; i < list.size() ; i++){
System.out.print(list.get(i)+ " ");
}
System.out.println(str.length());
// while(arr[st][0] != (int)str.charAt(pari)-96){
// System.out.print(arr[st][1] + " ");
// st--;
// }
// for (int i = st - 1 ; i >= 0 ; i--) {
// System.out.print(arr[i][1] + " ");
// if (arr[i][0] == (int)str.charAt(pari) - 96) {
// break;
// }
// }
// }
// }
// else {
// // while (arr[i][0] != (int)str.charAt(pari) - 96) {
// for (int i = 0 ; i < arr.length ; i++) {
// if (arr[i][0] == jj4) {
// st = i + 1;
// break;
// }
// }
// // while(arr[st][0] != (int)str.charAt(pari)-96){
// // System.out.print(arr[st][1] + " ");
// // st--;
// // }
// for (int i = st - 1 ; i < str.length() ; i++) {
// System.out.print(arr[i][1] + " ");
// if (arr[i][0] == (int)str.charAt(pari) - 96) {
// break;
// }
// }
// }
// System.out.println();
}
}
public static int countDigit(long n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
public static void solve(String s) {
int n = s.length();
int[][] dp = new int[n + 1][n + 1];
for (int len = 2; len <= n; len += 2) {
for (int l = 1 ; l < n - len + 1 ; l++) {
int r = l + len;
dp[l][r] = 1;
{
int res = -1;
if (dp[l + 1][r - 1] != 0)
res = Math.max(res, dp[l + 1][r - 1]);
else
res = Math.max(res, comp(s.charAt(l), s.charAt(r - 1)));
if (dp[l + 2][r] != 0)
res = Math.max(res, dp[l + 2][r]);
else
res = Math.max(res, comp(s.charAt(l), s.charAt(l + 1)));
dp[l][r] = Math.min(dp[l][r], res);
}
{
int res = -1;
if (dp[l + 1][r - 1] != 0)
res = Math.max(res, dp[l + 1][r - 1]);
else
res = Math.max(res, comp(s.charAt(r - 1), s.charAt(l)));
if (dp[l][r - 2] != 0)
res = Math.max(res, dp[l][r - 2]);
else
res = Math.max(res, comp(s.charAt(r - 1), s.charAt(r - 2)));
dp[l][r] = Math.min(dp[l][r], res);
}
}
}
if (dp[0][n] == -1)
System.out.println("Alice");
else if (dp[0][n] == 0)
System.out.println("Draw");
else
System.out.println("Bob");
}
public static int comp(char c, char d) {
return c < d ? -1 : (c > d ? 1 : 0);
}
public static boolean checkPalin(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;
}
public static int maximum(int[] arr) {
int max = arr[0];
for (int i = 0 ; i < arr.length ; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
static long square(long n)
{
if (n == 0)
return 0;
if (n < 0)
n = -n;
long x = n >> 1;
if (n % 2 != 0)
return ((square(x) << 2) + (x << 2) + 1);
else
return (square(x) << 2);
}
public static void printString(String str) {
System.out.println(str);
}
public static void printCharArray(char[] arr) {
for (int i = 0 ; i < arr.length ; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
// int ko char array m karne ke lia char[] arr = (n+"").toCharArray(); likhneka
public static <K, V> K getKey(Map<K, V> map, V value) {
for (Map.Entry<K, V> entry : map.entrySet()) {
if (value.equals(entry.getValue())) {
return entry.getKey();
}
}
return null;
}
public static void printArray(int[] arr) {
for (int i = 0 ; i < arr.length ; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void swap(int[] arr) {
for (int i = 0 ; i < arr.length ; i = i + 2) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
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 highestPowerof2(int n) {
int res = 0;
for (int i = n; i >= 1; i--) {
if ((i & (i - 1)) == 0) {
res = i;
break;
}
}
return res;
}
public static int countElement(int[] arr, int k) {
int cc = 0;
for (int i = 0 ; i < arr.length ; i++ ) {
if (arr[i] == k) {
cc++;
}
}
return cc;
}
public static long integerfrmbinary(String str) {
long j = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '1') {
j = j + (long)Math.pow(2, str.length() - 1 - i);
}
}
return (long) j;
}
static int maxInArray(int[] arr) {
int max = arr[0];
for (int i = 0 ; i < arr.length ; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
static int minInArray(int[] arr) {
int min = arr[0];
for (int i = 0 ; i < arr.length ; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
static void printN() {
System.out.println("No");
}
static void printY() {
System.out.println("Yes");
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for (int v : a) {
c0[(v & 0xff) + 1]++;
c1[(v >>> 8 & 0xff) + 1]++;
c2[(v >>> 16 & 0xff) + 1]++;
c3[(v >>> 24 ^ 0x80) + 1]++;
}
for (int i = 0; i < 0xff; i++) {
c0[i + 1] += c0[i];
c1[i + 1] += c1[i];
c2[i + 1] += c2[i];
c3[i + 1] += c3[i];
}
int[] t = new int[n];
for (int v : a)t[c0[v & 0xff]++] = v;
for (int v : t)a[c1[v >>> 8 & 0xff]++] = v;
for (int v : a)t[c2[v >>> 16 & 0xff]++] = v;
for (int v : t)a[c3[v >>> 24 ^ 0x80]++] = v;
return a;
}
static int[] EvenOddArragement(int nums[])
{
int i1 = 0, i2 = nums.length - 1;
while (i1 < i2) {
while (nums[i1] % 2 == 0 && i1 < i2) {
i1++;
}
while (nums[i2] % 2 != 0 && i2 > i1) {
i2--;
}
int temp = nums[i1];
nums[i1] = nums[i2];
nums[i2] = temp;
}
return nums;
}
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();
}
}
static int gcd(int a, int b) {
while (b != 0) {
int t = a;
a = b;
b = t % b;
}
return a;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static int DigitSum(int n)
{
int r = 0, sum = 0;
while (n >= 0)
{
r = n % 10;
sum = sum + r;
n = n / 10;
}
return sum;
}
static boolean checkPerfectSquare(int number)
{
double sqrt = Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static boolean isPowerOfTwo(int n)
{
if (n == 0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static boolean isPrime2(int n)
{
if (n <= 1)
{
return false;
}
if (n == 2)
{
return true;
}
if (n % 2 == 0)
{
return false;
}
for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
static String minLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for (int i = 0; i < n; i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[0];
}
static String maxLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for (int i = 0; i < n; i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[arr.length - 1];
}
static class P implements Comparable<P> {
int i, j;
public P(int i, int j) {
this.i = i;
this.j = j;
}
public int compareTo(P o) {
return Integer.compare(i, o.i);
}
}
static class pair {
int i, j;
pair(int x, int y) {
i = x;
j = y;
}
}
static int binary_search(int a[], int value)
{
int start = 0;
int end = a.length - 1;
int mid = start + (end - start) / 2;
while (start <= end)
{
if (a[mid] == value)
{
return mid;
}
if (a[mid] > value)
{
end = mid - 1;
}
else
{
start = mid + 1;
}
mid = start + (end - start) / 2;
}
return -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 11 | 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 | 6be550d5ded18105962f19ed89bd7e05 | 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.*;
import java.util.*;
public class ComdeFormces {
static int dp[][];
static boolean mnans;
static int x[]= {-1,0,0,1,1,1,-1,-1};
static int y[]= {0,1,-1,0,1,-1,1,-1};
static int ans[];
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
FastReader sc=new FastReader();
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
// OutputStream out = new BufferedOutputStream ( System.out );
int t=sc.nextInt();
int tc=1;
while(t--!=0) {
char a[]=sc.next().toCharArray();
if(a[0]<=a[a.length-1]) {
ArrayList<pair> ar=new ArrayList<>();
for(int i=1;i<a.length-1;i++) {
if(a[0]<=a[i] && a[i]<=a[a.length-1])ar.add(new pair(a[i],i));
}
Collections.sort(ar,(aa,bb)->aa.a-bb.a);
int total=a[a.length-1]-a[0];
log.write(total+" "+(ar.size()+2)+"\n");
log.write(1+" ");
for(int i=0;i<ar.size();i++) {
log.write((ar.get(i).b+1)+" ");
}
log.write((a.length)+" ");
log.write("\n");
}
else {
ArrayList<pair> ar=new ArrayList<>();
for(int i=1;i<a.length-1;i++) {
if(a[a.length-1]<=a[i] && a[i]<=a[0])ar.add(new pair(a[i],i));
}
Collections.sort(ar,(aa,bb)->bb.a-aa.a);
int total=a[0]-a[a.length-1];
log.write(total+" "+(ar.size()+2)+"\n");
log.write(1+" ");
for(int i=0;i<ar.size();i++) {
log.write((ar.get(i).b+1)+" ");
}
log.write((a.length)+" ");
log.write("\n");
}
}
log.flush();
}
static void fill(char a[][],boolean visx[],boolean visy[],int r,int c,int k,int n) {
for(int i=r;i<n;i+=k) {
for(int j=c;j<n;j+=k) {
a[i][j]='X';
visy[j]=true;
}
for(int j=c;j>=0;j-=k) {
a[i][j]='X';
visy[j]=true;
}
visx[i]=true;
}
for(int i=r;i>=0;i-=k) {
for(int j=c;j<n;j+=k) {
a[i][j]='X';
visy[j]=true;
}
for(int j=c;j>=0;j-=k) {
a[i][j]='X';
visy[j]=true;
}
visx[i]=true;
}
}
static int[] manacher_odd(String ss) {
int n = ss.length();
ss = "$" + ss + "^";
char s[]=ss.toCharArray();
int p[]=new int[n+2];
int l = 1, r = 1;
for(int i = 1; i <= n; i++) {
p[i] = Math.max(0, Math.min(r - i, p[l + (r - i)]));
while(s[i - p[i]] == s[i + p[i]]) {
p[i]++;
}
if(i + p[i] > r) {
l = i - p[i]; r = i + p[i];
}
}
return p;
}
static int mod=998244853;
static int[] lps(int a[],String s) {
int i=1;
int j=0;
a[0]=0;
while(i<s.length()) {
if(s.charAt(i)==s.charAt(j)) {
a[i]=j+1;
i++;
j++;
}
else {
if(j!=0) {
j=a[j-1];
}
else {
a[i]=0;
i++;
}
}
}
return a;
}
static int[] zed(char a[]) {
int z[]=new int[a.length];
int l=0;
int r=0;
for(int i=0;i<a.length;i++) {
if(i>r) {
l=r=i;
while(r<a.length && a[r]==a[r-l])r++;
z[i]=r-l;
r--;
}
else {
int k1=i-l;
if(z[k1]<r-i+1) {
z[i]=z[k1];
}
else {
l=i;
while(r<a.length && a[r]==a[r-l])r++;
z[i]=r-l;
r--;
}
}
}
return z;
}
public static class pair2{
int a,b,c,d;
public pair2(int a,int b,int c,int d) {
this.a=a;
this.b=b;
this.c=c;
this.d=d;
}
}
static boolean dfs(ArrayList<ArrayList<Integer>> ar,int src,int pr,HashSet<Integer> hs) {
int cnt=0;
boolean an=false;
for(int k:ar.get(src)) {
if(k==pr)continue;
boolean p=dfs(ar,k,src,hs);
an|=p;
if(p)cnt++;
}
if(cnt>1)mnans=false;
if(hs.contains(src))an=true;
return an;
}
static int find(int el,int p[]) {
if(p[el]<0)return el;
return p[el]=find(p[el],p);
}
static boolean union(int a,int b,int p[]) {
int p1=find(a,p);
int p2=find(b,p);
if(p1>=0 && p1==p2)return false;
else {
if(p[p1]<p[p2]) {
p[p1]+=p[p2];
p[p2]=p1;
}
else {
p[p2]+=p[p1];
p[p1]=p2;
}
return true;
}
}
public static void build(int a[][],int b[]) {
for(int i=0;i<b.length;i++) {
a[i][0]=b[i];
}
int jmp=2;
while(jmp<=b.length) {
for(int j=0;j<b.length;j++) {
int ind=(int)(Math.log(jmp/2)/Math.log(2));
int ind2=(int)(Math.log(jmp)/Math.log(2));
if(j+jmp-1<b.length) {
a[j][ind2]=Math.max(a[j][ind],a[j+(jmp/2)][ind]);
}
}
jmp*=2;
}
// for(int i=0;i<a.length;i++) {
// for(int j=0;j<33;j++) {
// System.out.print(a[i][j]+" ");
// }
// System.out.println();
// }
}
public static void build2(int a[][],int b[]) {
for(int i=0;i<b.length;i++) {
a[i][0]=b[i];
}
int jmp=2;
while(jmp<=b.length) {
for(int j=0;j<b.length;j++) {
int ind=(int)(Math.log(jmp/2)/Math.log(2));
int ind2=(int)(Math.log(jmp)/Math.log(2));
if(j+jmp-1<b.length) {
a[j][ind2]=Math.min(a[j][ind],a[j+(jmp/2)][ind]);
}
}
jmp*=2;
}
// for(int i=0;i<a.length;i++) {
// for(int j=0;j<33;j++) {
// System.out.print(a[i][j]+" ");
// }
// System.out.println();
// }
}
public static int serst(int a[][],int i,int j) {
int len=j-i+1;
int hp=1;
int tlen=len>>=1;
// System.out.println(tlen);
while(tlen!=0) {
tlen>>=1;
hp<<=1;
}
// System.out.println(hp);
int ind=(int)(Math.log(hp)/Math.log(2));
int i2=j+1-hp;
return Math.max(a[i][ind], a[i2][ind]);
}
public static int serst2(int a[][],int i,int j) {
int len=j-i+1;
int hp=1;
int tlen=len>>=1;
// System.out.println(tlen);
while(tlen!=0) {
tlen>>=1;
hp<<=1;
}
// System.out.println(hp);
int ind=(int)(Math.log(hp)/Math.log(2));
int i2=j+1-hp;
return Math.min(a[i][ind], a[i2][ind]);
}
static void update(long f[],long upd,int ind) {
int vl=ind;
while(vl<f.length) {
f[vl]+=upd;
int tp=~vl;
tp++;
tp&=vl;
vl+=tp;
}
}
static long ser(long f[],int ind) {
int vl=ind;
long sm=0;
while(vl!=0) {
sm+=f[vl];
int tp=~vl;
tp++;
tp&=vl;
vl-=tp;
}
return sm;
}
public static void radixSort(int a[]) {
int n=a.length;
int res[]=new int[n];
int p=1;
for(int i=0;i<=8;i++) {
int cnt[]=new int[10];
for(int j=0;j<n;j++) {
a[j]=res[j];
cnt[(a[j]/p)%10]++;
}
for(int j=1;j<=9;j++) {
cnt[j]+=cnt[j-1];
}
for(int j=n-1;j>=0;j--) {
res[cnt[(a[j]/p)%10]-1]=a[j];
cnt[(a[j]/p)%10]--;
}
p*=10;
}
}
static int bits(long n) {
int ans=0;
while(n!=0) {
if((n&1)==1)ans++;
n>>=1;
}
return ans;
}
public static int kadane(int a[]) {
int sum=0,mx=Integer.MIN_VALUE;
for(int i=0;i<a.length;i++) {
sum+=a[i];
mx=Math.max(mx, sum);
if(sum<0) sum=0;
}
return mx;
}
public static int m=(int)(1e9+7);
public static int mul(int a, int b) {
return ((a%m)*(b%m))%m;
}
public static long mul(long a, long b) {
return ((a%m)*(b%m))%m;
}
public static int add(int a, int b) {
return ((a%mod)+(b%mod))%mod;
}
public static long add(long a, long b) {
return ((a%m)+(b%m))%m;
}
//debug
public static <E> void p(E[][] a,String s) {
System.out.println(s);
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[0].length;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void p(int[] a,String s) {
System.out.print(s+"=");
for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");
System.out.println();
}
public static void p(long[] a,String s) {
System.out.print(s+"=");
for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");
System.out.println();
}
public static <E> void p(E a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(ArrayList<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(LinkedList<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(HashSet<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(Stack<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(Queue<E> a,String s){
System.out.println(s+"="+a);
}
//utils
static ArrayList<Integer> divisors(int n){
ArrayList<Integer> ar=new ArrayList<>();
for (int i=2; i<=Math.sqrt(n); i++){
if (n%i == 0){
if (n/i == i) {
ar.add(i);
}
else {
ar.add(i);
ar.add(n/i);
}
}
}
return ar;
}
static ArrayList<Integer> prime(int n){
ArrayList<Integer> ar=new ArrayList<>();
int cnt=0;
boolean pr=false;
while(n%2==0) {
ar.add(2);
n/=2;
}
for(int i=3;i*i<=n;i+=2) {
pr=false;
while(n%i==0) {
n/=i;
ar.add(i);
pr=true;
}
}
if(n>2) ar.add(n);
return ar;
}
static long gcd(long a,long b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static int gcd(int a,int b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static long factmod(long n,long mod) {
if(n==0)return 0;
long ans=1;
long temp=1;
while(temp<=n) {
ans=((ans%mod)*((temp)%mod))%mod;
temp++;
}
return ans%mod;
}
static int ncr(int n, int r){
if(r>n-r)r=n-r;
int ans=1;
for(int i=0;i<r;i++){
ans*=(n-i);
ans/=(i+1);
}
return ans;
}
public static class trip{
int a;
int b;
int c;
public trip(int a,int b,int c) {
this.a=a;
this.b=b;
this.c=c;
}
// public int compareTo(trip q) {
// return this.b-q.b;
// }
}
static void mergesort(int[] a,int start,int end) {
if(start>=end)return ;
int mid=start+(end-start)/2;
mergesort(a,start,mid);
mergesort(a,mid+1,end);
merge(a,start,mid,end);
}
static void merge(int[] a, int start,int mid,int end) {
int ptr1=start;
int ptr2=mid+1;
int b[]=new int[end-start+1];
int i=0;
while(ptr1<=mid && ptr2<=end) {
if(a[ptr1]<=a[ptr2]) {
b[i]=a[ptr1];
ptr1++;
i++;
}
else {
b[i]=a[ptr2];
ptr2++;
i++;
}
}
while(ptr1<=mid) {
b[i]=a[ptr1];
ptr1++;
i++;
}
while(ptr2<=end) {
b[i]=a[ptr2];
ptr2++;
i++;
}
for(int j=start;j<=end;j++) {
a[j]=b[j-start];
}
}
public static class FastReader {
BufferedReader b;
StringTokenizer s;
public FastReader() {
b=new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(s==null ||!s.hasMoreElements()) {
try {
s=new StringTokenizer(b.readLine());
}
catch(IOException e) {
e.printStackTrace();
}
}
return s.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str="";
try {
str=b.readLine();
}
catch(IOException e) {
e.printStackTrace();
}
return str;
}
boolean hasNext() {
if (s != null && s.hasMoreTokens()) {
return true;
}
String tmp;
try {
b.mark(1000);
tmp = b.readLine();
if (tmp == null) {
return false;
}
b.reset();
} catch (IOException e) {
return false;
}
return true;
}
}
public static class pair{
int a;
int b;
public pair(int a,int b) {
this.a=a;
this.b=b;
}
// public int compareTo(pair b) {
// return this.a-b.a;
//
// }
// // public int compareToo(pair b) {
// return this.b-b.b;
// }
@Override
public String toString() {
return "{"+this.a+" "+this.b+"}";
}
}
static long pow(long a, long pw) {
long temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*temp;
}
public static int md=998244353;
static long mpow(long a, long pw) {
long temp;
if(pw==0)return 1;
temp=pow(a,pw/2)%md;
if(pw%2==0)return mul(temp,temp);
return mul(a,mul(temp,temp));
}
static int pow(int a, int pw) {
int temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*temp;
}
} | 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 11 | 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 | 9d5237f248f8c853305ba487078d0dec | 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 s = sc.next();
int[][] idx = new int[s.length()][2];
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
int temp = (int)(ch - 'a' + 1);
idx[i][0] = temp;
idx[i][1] = i + 1;
}
boolean flag = s.charAt(0) <= s.charAt(s.length() - 1);
Arrays.sort(idx, new Comparator<int[]>() {
@Override
public int compare(int[] f, int[] s){
if(f[0] < s[0]) return -1;
else if(f[0] > s[0]) return 1;
else {
if(flag){
if(f[1] < s[1]) return -1;
else if(f[1] > s[1]) return 1;
else return 0;
}else{
if(f[1] < s[1]) return 1;
else if(f[1] > s[1]) return -1;
else return 0;
}
}
}
});
int hi = Math.max(s.charAt(0), s.charAt(s.length() - 1)) - 'a' + 1;
int lo = Math.min(s.charAt(0), s.charAt(s.length() - 1)) - 'a' + 1;
List<Integer> l = new ArrayList<>();
if(flag){
for(int i = 0; i < s.length(); i++){
if(idx[i][0] >= lo && idx[i][0] <= hi){
l.add(idx[i][1]);
}
}
}else{
for(int i = s.length() - 1; i >= 0; i--){
if(idx[i][0] >= lo && idx[i][0] <= hi){
l.add(idx[i][1]);
}
}
}
System.out.print(hi - lo + " " + l.size());
System.out.println();
for(int n : l){
System.out.print(n + " ");
}
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 11 | 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 | b884bf40e30edbad20ee31c89af7d124 | 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 final class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
while(t-- > 0) {
String s = sc.nextLine();
int start = s.charAt(0) - 'a';
int end = s.charAt(s.length() - 1) - 'a';
List<Integer>[] list = new ArrayList[26];
for(int i = 0; i < 26; i++) {
list[i] = new ArrayList<>();
}
for(int i = 0; i < s.length(); i++) {
list[s.charAt(i) - 'a'].add(i);
}
int sum = 0;
if(start > end) {
for(int i = start; i >= end; i--) sum += list[i].size();
System.out.println(start - end + " " + sum);
for(int i = start; i >= end; i--) {
for(Integer position : list[i]) System.out.print(position + 1 + " ");
}
}
else {
for(int i = start; i <= end; i++) sum += list[i].size();
System.out.println(end - start + " " + sum);
for(int i = start; i <= end; i++) {
for(Integer position : list[i]) System.out.print(position + 1 + " ");
}
}
System.out.println();
}
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 11 | 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 | 123d65528b588df027a5fda35d53c7cf | 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 Shubham Chaudhari
*/
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
static FastReader in=new FastReader();
static final Random random=new Random();
static long mod=1000000007L;
static HashMap<String,Long>map=new HashMap<>();
public static void main(String[] args) {
int t=in.nextInt();
StringBuilder res=new StringBuilder();
loop:
while(t-->0){
String s=in.next();
char ch[]=s.toCharArray();
int cost =abs(ch[s.length()-1]-ch[0]);
ArrayList<Integer>list=new ArrayList<>();
for(int i=1;i<s.length()-1;i++){
if((s.charAt(i)>=s.charAt(0)&&s.charAt(i)<=s.charAt(s.length()-1))&&(s.charAt(0)<=s.charAt(s.length()-1))){
list.add(i);
}
else if((s.charAt(i)<=s.charAt(0)&&s.charAt(i)>=s.charAt(s.length()-1))&&(s.charAt(0)>=s.charAt(s.length()-1))){
list.add(i);
}
}
if(s.charAt(0)<s.charAt(s.length()-1)){
Collections.sort(list,(x,y) -> ch[x]-ch[y]);
}
else{
Collections.sort(list,(x,y) -> ch[y]-ch[x]);
}
res.append(cost+" "+(list.size()+2)+"\n");
res.append(1+" ");
for(int i=0;i<list.size();i++){
res.append(list.get(i)+1+" ");
}
res.append(s.length()+" \n");
}
System.out.println(res);
}
static class Pair implements Comparable<Pair>{
int a,b;
public Pair(int a,int b){
this.a=a;
this.b=b;
}
@Override
public int compareTo(Pair o) {
if(a>o.a){
return 1;
}else if(a==o.a){
return 0;
}
return -1;
}
}
static boolean isPalindrome(int n){
int tmp=n;
int tmp2=0;
while (n>0){
tmp2=tmp2*10+n%10;
n/=10;
}
return tmp==tmp2;
}
static long calculateSum(long n)
{
if(n<0)
{
return 0;
}
return n*(n+1)/2;
}
static void ruffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static int gcd(int a,int b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static int abs(int a)
{
if(a<0)
return -1*a;
return a;
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int [] readintarray(int n) {
int res [] = new int [n];
for(int i = 0; i<n; i++)res[i] = nextInt();
return res;
}
long [] readlongarray(int n) {
long res [] = new long [n];
for(int i = 0; i<n; i++)res[i] = nextLong();
return res;
}
}
static class Node implements Comparable<Node>
{
int val;
long cost;
public Node(int val,long cost)
{
this.val=val;
this.cost=cost;
}
public int compareTo(Node x)
{
return Long.compare(this.cost,x.cost);
}
}
static class UWGraph{
public static ArrayList<WNode> graph[];
public int V;
public static PriorityQueue<WNode> pq;
public UWGraph(int V){
graph=new ArrayList[V];
for(int i=0;i<V;i++){
graph[i]=new ArrayList<WNode>();
}
this.V=V;
}
public void addEdge(int v,int u,int w){
graph[v].add(new WNode(u,w));
graph[u].add(new WNode(v,w));
}
public int[] dijkstra(int src)
{
int[] distance = new int[V];
for (int i = 0; i < V; i++)
distance[i] = Integer.MAX_VALUE;
distance[src] = 0;
pq = new PriorityQueue<>();
pq.add(new WNode(src, 0));
boolean done[]=new boolean[V];
while (pq.size() > 0) {
WNode current = pq.poll();
if(done[current.getVertex()])
continue;
done[current.getVertex()]=true;
for (WNode n : graph[current.getVertex()]) {
if (distance[current.getVertex()] + n.getWeight() < distance[n.getVertex()]) {
distance[n.getVertex()] = n.getWeight() + distance[current.getVertex()];
pq.add(new WNode(n.getVertex(), distance[n.getVertex()]));
}
}
}
return distance;
}
}
static class WNode implements Comparable<WNode>{
public int vertex, weight;
WNode(int v, int w)
{
vertex = v;
weight = w;
}
int getVertex() { return vertex; }
int getWeight() { return weight; }
@Override
public int compareTo(WNode o) {
return weight-o.weight;
}
}
static class UNGraph{
public ArrayList<Integer> graph[];
public int V;
public UNGraph(int V){
graph=new ArrayList[V];
for(int i=0;i<V;i++){
graph[i]=new ArrayList<Integer>();
}
this.V=V;
}
public void addEdge(int v,int u){
graph[v].add(u);
graph[u].add(v);
}
}
static class SegmentTree{
long st[];
int minCount;
long minVal;
public SegmentTree(long arr[],int n){
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
minCount=0;
//Maximum size of segment tree
int max_size = 2 * (int) Math.pow(2, x) - 1;
st = new long[max_size]; // Memory allocation
constructSTUtil(arr, 0, n - 1, 0);
}
long constructSTUtil(long arr[], int ss, int se, int si)
{
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
int mid = getMid(ss, se);
st[si] = Math.min(constructSTUtil(arr, ss, mid, si * 2 + 1),
constructSTUtil(arr, mid + 1, se, si * 2 + 2));
return st[si];
}
int getMid(int s, int e) {
return s + (e - s) / 2;
}
long updateValueUtil(int ss, int se, int i, long diff, int si)
{
// Base Case: If the input index lies outside the range of
// this segment
if (i < ss || i > se)
return st[si];
// If the input index is in range of this node, then update the
// value of the node and its children
if (se != ss) {
int mid = getMid(ss, se);
st[si] = Math.min(
updateValueUtil(ss, mid, i, diff, 2 * si + 1),
updateValueUtil(mid + 1, se, i, diff, 2 * si + 2));
}
else{
st[si]=diff;
}
return st[si];
}
// The function to update a value in input array and segment tree.
// It uses updateValueUtil() to update the value in segment tree
void updateValue(long arr[], int n, int i, int new_val)
{
// Check for erroneous input index
if (i < 0 || i > n - 1) {
return;
}
// Get the difference between new value and old value
long diff = new_val;
// Update the value in array
arr[i] = new_val;
// Update the values of nodes in segment tree
updateValueUtil(0, n - 1, i, diff, 0);
}
// End of template
public long getSum(int n,int l, int r){
minCount=0;
minVal= getSumUtil(0,n-1,l,r,0);
return minVal;
}
public long getSumUtil(int ss, int se,int l, int r, int si){
int mid = getMid(ss,se);
if(si>=st.length){
return Integer.MAX_VALUE;
}
if(ss>=l&&se<=r){
if(ss==se && minVal==st[si]){
minCount++;
}
getSumUtil(ss,mid,l,r,2*si+1);
getSumUtil(mid+1,se,l,r,2*si+2);
return st[si];
}
if (se < l || ss > r)
return Integer.MAX_VALUE;
if(ss==se && minVal==st[si]){
minCount++;
}
return Math.min(getSumUtil(ss,mid,l,r,2*si+1),getSumUtil(mid+1,se,l,r,2*si+2));
}
void printSegmentTree(){
for(long x: st){
System.out.print(x+" ");
}
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 11 | 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 | 68f8e8d501ac2d5d0eba2d73fcc88207 | 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 | // Jai Shree Ram ⛳⛳⛳
// Jai Bajrang Bali
// Jai Saraswati maa
// Har Har Mahadev
// Thanks Kalash Shah :)
import java.math.BigInteger;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.Character.isUpperCase;
public class A {
static FastScanner sn = new FastScanner();
public static void main(String[] args) {
int T = sn.nextInt();
while (T-- > 0) {
solve();
}
}
public static void solve() {
String t = sn.next();
int n = t.length();
long cost = 0;
long m = 0;
char[] arr = t.toCharArray();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = (int) arr[i] - 96;
}
if (a[0] < a[n - 1]) {
PriorityQueue<Pair> p = new PriorityQueue<>();
for (int i = 1; i < n - 1; i++) {
if (a[i] >= a[0] && a[i] <= a[n - 1]) {
p.add(new Pair(i, a[i]));
}
}
System.out.println((a[n - 1] - a[0]) + " " + (p.size() + 2));
System.out.print(1 + " ");
while (p.size() > 0) {
Pair temp = p.remove();
System.out.print((temp.ind + 1) + " ");
}
System.out.println(n);
} else {
PriorityQueue<Pair> p = new PriorityQueue<>(Collections.reverseOrder());
for (int i = 1; i < n - 1; i++) {
if (a[i] <= a[0] && a[i] >= a[n - 1]) {
p.add(new Pair(i, a[i]));
}
}
System.out.println((a[0] - a[n - 1]) + " " + (p.size() + 2));
System.out.print(1 + " ");
while (p.size() > 0) {
Pair temp = p.remove();
System.out.print((temp.ind + 1) + " ");
}
System.out.println(n);
}
}
//----------------------------------------------------------------------------------//
public static void mergesort(long[] arr, int l, int r) {
if (l >= r) {
return;
}
int mid = l + (r - l) / 2;
mergesort(arr, l, mid);
mergesort(arr, mid + 1, r);
Merge(arr, l, mid, r);
}
static long getFirstSetBitPos(long n) {
return (long) ((Math.log10(n & -n)) / Math.log10(2)) + 1;
}
static class Pair implements Comparable<Pair> {
int ind;
int ch;
Pair(int a, int ch) {
this.ind = a;
this.ch = ch;
}
public int compareTo(Pair o) {
if (this.ch == o.ch) {
return this.ind - o.ind;
}
return this.ch - o.ch;
}
public String toString() {
return "";
}
}
static int countSetBits(long n) {
int count = 0;
while (n > 0) {
count += n & 1;
n >>= 1;
}
return count;
}
public static void Merge(long[] arr, int l, int mid, int r) {
long[] B = new long[r - l + 1];
int i = l;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= r) {
if (arr[i] <= arr[j]) {
B[k++] = arr[i++];
} else {
B[k++] = arr[j++];
}
}
while (i <= mid) {
B[k++] = arr[i++];
}
while (j <= r) {
B[k++] = arr[j++];
}
for (int i1 = 0, j1 = l; i1 < B.length; i1++, j1++) {
arr[j1] = B[i1];
}
}
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());
}
char nextChar() {
return next().charAt(0);
}
long[] readArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
int[] scan(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
void print(int[] a) {
for (int j : a) {
System.out.print(j + " ");
}
}
void printl(long[] a) {
for (long j : a) {
System.out.print(j + " ");
}
}
long nextLong() {
return Long.parseLong(next());
}
}
static class Debug {
public static void debug(long a) {
System.out.println("--> " + a);
}
public static void debug(long a, long b) {
System.out.println("--> " + a + " + " + b);
}
public static void debug(char a, char b) {
System.out.println("--> " + a + " + " + b);
}
public static void debug(int[] array) {
System.out.print("Array--> ");
System.out.println(Arrays.toString(array));
}
public static void debug(char[] array) {
System.out.print("Array--> ");
System.out.println(Arrays.toString(array));
}
public static void debug(HashMap<Integer, Integer> map) {
System.out.print("Map--> " + map.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 11 | 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 | a857a4f7e373998d27f98b694eb026f1 | 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.Collection;
import java.util.Collections;
import java.util.Scanner;
public class C {
public static class Par implements Comparable{
public char letra;
public int pos;
public Par(char c, int p) {
letra = c;
pos = p;
}
@Override
public int compareTo(Object o) {
if(this.letra==((Par)o).letra) return this.pos-((Par)o).pos;
return this.letra-((Par)o).letra;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
StringBuilder out = new StringBuilder();
for(; t>0; t--) {
String s = sc.next();
//System.out.println(s);
ArrayList<Par> lista = new ArrayList<>(s.length());
for(int i=0; i<s.length(); i++) {
lista.add(new Par(s.charAt(i), i+1));
}
Collections.sort(lista);
int saltos = 1;
int dis = s.charAt(0)-s.charAt(s.length()-1);
boolean ini = false;
boolean fin = false;
StringBuilder out2 = new StringBuilder("1");
if(s.charAt(0)<=s.charAt(s.length()-1)) {
dis = s.charAt(s.length()-1)-s.charAt(0);
for(Par p:lista) {
if(ini) {
if(p.letra==s.charAt(s.length()-1)) {
fin = true;
saltos++;
out2.append(" "+p.pos);
}
else {
if(fin) break;
saltos++;
out2.append(" "+p.pos);
}
}
else if(p.letra==s.charAt(0)) ini = true;
}
}
else {
Collections.reverse(lista);
for(Par p:lista) {
if(ini) {
if(p.pos!=1) {
if (p.letra == s.charAt(s.length() - 1)) {
fin = true;
saltos++;
if (p.pos != s.length()) out2.append(" "+p.pos);
} else {
if (fin) break;
saltos++;
out2.append(" "+p.pos);
}
}
}
else if(p.letra==s.charAt(0)) {
ini = true;
if(p.pos!=1) {
out2.append(" "+p.pos);
saltos++;
}
}
}
out2.append(" "+s.length());
}
out.append(dis+" "+saltos+"\n"+out2+"\n");
}
System.out.print(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 11 | 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 | 2c31a0e3b6f444448d322fb934d6bdd3 | 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.nio.charset.StandardCharsets;
import java.util.*;
public class jumping
{
static class Pair{
int val,ind;
Pair(int val,int ind)
{
this.val=val;
this.ind=ind;
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int tc=sc.nextInt();
while(tc-->0)
{
String s=sc.next();
char[] alpha= {
'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'
};
int st=(s.charAt(0)-'a'+1);
int en=(s.charAt(s.length()-1)-'a'+1);
if(en>st)
{
int c=0;
// System.out.println(st+" "+en);
for(int i=1;i<s.length()-1;i++)
{
if((s.charAt(i)-'a'+1)<=en && (s.charAt(i)-'a'+1)>=st)
c++;
}
System.out.println(en-st+" "+(c+2));
ArrayList<Pair> list=new ArrayList<>();
for(int i=1;i<s.length()-1;i++)
{
if(en>=(s.charAt(i)-'a'+1) && st<=(s.charAt(i)-'a'+1))
{
list.add(new Pair((s.charAt(i)-'a'+1),(i+1)));
}
}
Collections.sort(list, (a, b) -> a.val-b.val);
System.out.print(1+" ");
for(Pair i:list)
{
System.out.print(i.ind+" ");
}
System.out.println(s.length());
}
else
{
int c=0;
for(int i=1;i<s.length()-1;i++)
{
if((s.charAt(i)-'a'+1)<=st && (s.charAt(i)-'a'+1)>=en)
c++;
}
System.out.println(st-en+" "+(c+2));
ArrayList<Pair> list=new ArrayList<>();
for(int i=1;i<s.length()-1;i++)
{
if(st>=(s.charAt(i)-'a'+1) && en<=(s.charAt(i)-'a'+1))
{
list.add(new Pair((s.charAt(i)-'a'+1),(i+1)));
}
}
Collections.sort(list, (a, b) -> b.val-a.val);
System.out.print(1+" ");
for(Pair i:list)
{
System.out.print(i.ind+" ");
}
System.out.println(s.length());
}
}
}
}
| 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 11 | 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 | 9658c3cbf3760e8a36df7bbaf0e131ed | 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.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.util.*;
public class CodeForces_820 {
static class pair{
int a,b;
pair(int a,int b){
this.a=a;this.b=b;
}
}
public static void main(String[] args)throws Exception{
FastScanner in = new FastScanner();OutputStream out = new BufferedOutputStream ( System.out );
int t= in.nextInt();
while (t-->0){
String s=in.nextLine();int n=s.length();
pair[] a=new pair[n];
for(int i=0;i<n;i++){
a[i]=new pair((int)(s.charAt(i))-96,i+1);
}
int init=a[0].a;int end=a[n-1].a;
int cost=Math.abs(a[n-1].a-a[0].a);
ArrayList<Integer>ans=new ArrayList<>();
// for( pair p: a)
// System.out.println(p.a+" "+p.b);
if(init<=end){
Arrays.sort(a,new pairSortAsc());
//System.out.println("Case1");
for(int i=0;i<n;i++){
if(a[i].a>=init && a[i].a<=end)
ans.add(a[i].b);
}
}
else{
//System.out.println("Case 2");
Arrays.sort(a,new pairSortDesc());
for(int i=n-1;i>=0;i--){
if(a[i].a<=init && a[i].a>=end){
ans.add(a[i].b);
}
}
}
System.out.println(cost+" "+ans.size());
for(int k: ans)
System.out.print(k+" ");
System.out.println();
}
out.flush();
}
static class pairSortAsc implements Comparator<pair>{
public int compare(pair o1,pair o2){
int val=o1.a-o2.a;
if(val==0)
return o1.b-o2.b;
return val;
}
}
static class pairSortDesc implements Comparator<pair>{
public int compare(pair o1,pair o2){
int val=o1.a-o2.a;
if(val==0)
return o2.b-o1.b;
return val;
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["6\n\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 11 | 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 | 52e6cb79dfb6d015f7f8fb68bd83798e | 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 Algorithm;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static class Idx implements Comparable<Idx>{
int idx;
int num;
Idx(int num, int idx){
this.num = num;
this.idx = idx;
}
@Override
public int compareTo(Idx o) {
return this.num <= o.num ? 1 : -1;
}
}
static class Idx2 implements Comparable<Idx2>{
int idx;
int num;
Idx2(int num, int idx){
this.num = num;
this.idx = idx;
}
@Override
public int compareTo(Idx2 o) {
return this.num >= o.num ? 1 : -1;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int T = Integer.parseInt(st.nextToken());
for (int test = 1; test <= T; test++) {
st = new StringTokenizer(br.readLine());
String str = st.nextToken();
int start = str.charAt(0) - 'a' + 1;
int end = str.charAt(str.length()-1) - 'a' + 1;
if(start<=end) {
PriorityQueue<Idx2> pq = new PriorityQueue<>();
int cnt = 2;
for(int i = 1;i<=str.length()-2;i++) {
int num = str.charAt(i) - 'a' + 1;
if(start<=num&&num<=end) {
cnt++;
pq.add(new Idx2(num,i+1));
}
}
bw.write(Math.abs(start-end)+" "+cnt+"\n");
bw.write(1+" ");
while(!pq.isEmpty()) {
Idx2 idx = pq.poll();
bw.write(idx.idx+" ");
}
bw.write(str.length()+"\n");
}//증감
else {
PriorityQueue<Idx> pq = new PriorityQueue<>();
int cnt = 2;
for(int i = 1;i<=str.length()-2;i++) {
int num = str.charAt(i) - 'a' + 1;
//bw.write(test+" "+num+"\n");
if(start>=num&&num>=end) {
cnt++;
pq.add(new Idx(num,i+1));
}
}
bw.write(Math.abs(start-end)+" "+cnt+"\n");
bw.write(1+" ");
while(!pq.isEmpty()) {
Idx idx = pq.poll();
bw.write(idx.idx+" ");
}
bw.write(str.length()+"\n");
}
}
bw.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 11 | 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 | a4edfb5e0bd897597d288f1a57e5ed7c | 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.sql.Array;
import java.util.*;
public class Main {
static int globalVariable = 123456789;
static String author = "pl728 on codeforces";
public static void main(String[] args) {
FastReader sc = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
MathUtils mathUtils = new MathUtils();
ArrayUtils arrayUtils = new ArrayUtils();
int tc = sc.ni();
while (tc-- != 0) {
String s = sc.ns();
int n = s.length();
int cost = index(s.charAt(n-1)) - index(s.charAt(0));
int steps = 1;
Map<Integer, List<Integer>> treeMap = new TreeMap<>();
if (cost < 0) {
for(int i = 1; i < n; i++) {
if(index(s.charAt(i)) <= index(s.charAt(0))
&& index(s.charAt(i)) >= index(s.charAt(n-1))) {
treeMap.putIfAbsent(index(s.charAt(0)) - index(s.charAt(i)), new ArrayList<>());
treeMap.get(index(s.charAt(0)) - index(s.charAt(i))).add(i+1);
steps++;
}
}
} else {
for(int i = 1; i < n; i++) {
if(index(s.charAt(i)) >= index(s.charAt(0))
&& index(s.charAt(i)) <= index(s.charAt(n-1))) {
treeMap.putIfAbsent(index(s.charAt(i)) - index(s.charAt(0)), new ArrayList<>());
treeMap.get(index(s.charAt(i)) - index(s.charAt(0))).add(i+1);
steps++;
}
}
}
int[] output = new int[steps];
output[0] = 1;
int i = 1;
for(int k : treeMap.keySet()) {
List<Integer> list = treeMap.get(k);
for(int l : list) {
output[i++] = l;
}
}
System.out.println(Math.abs(cost) + " " + steps);
for(int k = 0; k < steps; k++) {
System.out.print(output[k] + " ");
}
System.out.println();
}
}
static int index(char c) {
return (int) c - 96;
}
static class FastReader {
/**
* Uses BufferedReader and StringTokenizer for quick java I/O
* get next int, long, double, string
* get int, long, double, string arrays, both primitive and wrapped object when array contains all elements
* on one line, and we know the array size (n)
* next: gets next space separated item
* nextLine: returns entire line as space
*/
BufferedReader br;
StringTokenizer st;
public FastReader() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
// to parse something else:
// T x = T.parseT(fastReader.next());
public int ni() {
return Integer.parseInt(next());
}
public String ns() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public String[] readStringArrayLine(int n) {
String line = "";
try {
line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line.split(" ");
}
public String[] readStringArrayLines(int n) {
String[] result = new String[n];
for (int i = 0; i < n; i++) {
result[i] = this.next();
}
return result;
}
public int[] readIntArray(int n) {
int[] result = new int[n];
for (int i = 0; i < n; i++) {
result[i] = this.ni();
}
return result;
}
public long[] readLongArray(int n) {
long[] result = new long[n];
for (int i = 0; i < n; i++) {
result[i] = this.nl();
}
return result;
}
public Integer[] readIntArrayObject(int n) {
Integer[] result = new Integer[n];
for (int i = 0; i < n; i++) {
result[i] = this.ni();
}
return result;
}
public long nl() {
return Long.parseLong(next());
}
public char[] readCharArray(int n) {
return this.ns().toCharArray();
}
}
static class MathUtils {
public MathUtils() {
}
public long gcdLong(long a, long b) {
if (a % b == 0)
return b;
else
return gcdLong(b, a % b);
}
public long lcmLong(long a, long b) {
return a * b / gcdLong(a, b);
}
}
static class ArrayUtils {
public ArrayUtils() {
}
public static int[] reverse(int[] a) {
int n = a.length;
int[] b = new int[n];
int j = n;
for (int i = 0; i < n; i++) {
b[j - 1] = a[i];
j = j - 1;
}
return b;
}
public int sumIntArrayInt(int[] a) {
int ans = 0;
for (int i = 0; i < a.length; i++) {
ans += a[i];
}
return ans;
}
public long sumLongArrayLong(int[] a) {
long ans = 0;
for (int i = 0; i < a.length; i++) {
ans += a[i];
}
return ans;
}
}
public static int lowercaseToIndex(char c) {
return (int) c - 97;
}
}
| 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 11 | 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 | 46010772b9400e4e6def440000c0fcea | 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 Main {
static FastReader scan = new FastReader();
static PrintWriter sout = new PrintWriter(System.out);
public static void main(String[] args) {
int n = scan.nextInt();
while (n-->0)
A();
sout.close();
}
static void A(){
String s = scan.next();
int n = s.length();
int check = Math.abs(s.charAt(0)-s.charAt(n-1));
ArrayList<Integer> list = new ArrayList<>();
// list.add(1);
if (s.charAt(0)>s.charAt(n-1)){
for (int i = s.charAt(0); i >= s.charAt(n - 1); i--) {
for (int j = 1; j < n-1; j++) {
if ( s.charAt(j) == i ){
list.add(j);
}
}
}
}else{
for (int i = s.charAt(0); i <= s.charAt(n-1); i++) {
for (int j = 1; j < n-1; j++) {
if (s.charAt(j) == i)
// sout.print((char)i );
list.add(j);
}
}
// sout.println();
}
list.add(s.length()-1);
sout.println(check+" "+(list.size()+1));
sout.print(1+" ");
for (int m: list) {
sout.print(1+m+" ");
}
sout.println();
}
static boolean isPalindrome(String str){
StringBuilder sb = new StringBuilder(str);
return str.equals(sb.reverse().toString());
}
static class Pair{
int x;
char y;
public Pair(int x , char y){
this.x = x;
this.y = y;
}
@Override
public String toString(){
return new String(x +" "+y);
}
}
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 11 | 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 | 7f618d38b3348224049d99018fa5e5cc | 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.Map.Entry;
import java.util.stream.Stream;
import java.util.stream.Collectors;
public class fastreader {
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
String line = null;
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
public long nextLong(){
return Long.parseLong(next());
}
}
public static void main(String []args) {
// finalAnswer.append(1).append('\n');
// long startTime = System.nanoTime();
// finalAnswer.append(1).append('\n');
// System.out.println(finalAnswer);
// long stopTime = System.nanoTime();
// System.out.println(stopTime - startTime);
// out.println();
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
StringBuilder finalAnswer=new StringBuilder();
int t=sc.nextInt();
char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
outer:
while(t-->0) {
String s=sc.next();
int arr[]=new int[s.length()];
for(int i=0;i<s.length();i++) {
arr[i]=s.charAt(i)-'a';
}
int a=Math.min(arr[0],arr[s.length()-1]),b=Math.max(arr[0],arr[s.length()-1]);
int count=0;
List<Character> list=new ArrayList<>();
List<Integer> pos=new ArrayList<>();
pos.add(1);
list.add(s.charAt(0));
for(int i=1;i<s.length()-1;i++) {
if((s.charAt(i)-'a'>=a && s.charAt(i)-'a'<=b)) {
list.add(s.charAt(i));
pos.add(i+1);
}
}
list.add(s.charAt(s.length()-1));
pos.add(s.length());
// System.out.println(list);
// Map<Integer,Character> map=new HashMap<>();
// for(int i=0;i<pos.size();i++) {
//// System.out.println(pos+" pos");
// map.put(pos.get(i),list.get(i));
// }
// for (Map.Entry entry : map.entrySet()) {
// System.out.print("Key: " + entry.getKey());
// System.out.println(" Value: " + entry.getValue());
// }
// Map<Integer,Character> sortMap=sortByValue(map);
// Stream<Map.Entry<Integer,Character>> sorted =map.entrySet().stream()
// .sorted(Map.Entry.comparingByValue());
// System.out.println("/////");
// for (Map.Entry entry : sortMap.entrySet()) {
// System.out.print("Key: " + entry.getKey());
// System.out.println(" Value: " + entry.getValue());
// }
out.println(b-a+" "+pos.size());
Collections.sort(list);
// System.out.println(list);
// List<Integer> more=new ArrayList<>();
if(arr[0]>arr[s.length()-1]) {
int max=(list.get(list.size()-1)-'a');
// System.out.println(list.get(0)-'a');
int min=(list.get(0)-'a');
// System.out.println(max+" "+min);
char c='a';
for(int i=max;i>=min;i--) {
c=alphabet[i];
// System.out.println(c);
for(int j=0;j<s.length();j++) {
if(s.charAt(j)==c) {
out.print(j+1+" ");
}
}
}
out.println();
}
else {
int max=(list.get(list.size()-1)-'a');
// System.out.println(list.get(0)-'a');
int min=(list.get(0)-'a');
// System.out.println(max+" "+min);
char c='a';
for(int i=min;i<=max;i++) {
c=alphabet[i];
// System.out.println(c);
for(int j=0;j<s.length();j++) {
if(s.charAt(j)==c) {
out.print(j+1+" ");
}
}
}
out.println();
}
out.println();
}
// System.out.println(finalAnswer);
// out.println(finalAnswer);
out.flush();
out.close();
}
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
List<Entry<K, V>> list = new ArrayList<>(map.entrySet());
list.sort(Entry.comparingByValue());
Map<K, V> result = new LinkedHashMap<>();
for (Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
}
| 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 11 | 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 | 28d017ed3256579de36a8309668fad52 | 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 Solutions {
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int testCases = Integer.parseInt(br.readLine());
while(testCases-->0){
String s = br.readLine();
int n = s.length();
HashMap<Integer,List<Integer>> map = new HashMap<>();
for(int i=0;i<n;i++) {
int a=(s.charAt(i)-'a')+1;
if(!map.containsKey(a)) map.put(a, new ArrayList<>());
map.get(a).add(i+1);
}
int fact=1;
int st=(s.charAt(0)-'a')+1;
int end=(s.charAt(n-1)-'a')+1;
if(st > end) fact=-1;
List<Integer> list = new ArrayList<>();
boolean b = true;
int cost = 0;
int prev = -1;
while(b) {
if(map.containsKey(st)) {
if(prev != -1) cost = cost + Math.abs(prev-st);
Collections.sort(map.get(st));
list.addAll(map.get(st));
prev = st;
}
st+=fact;
if(fact==-1 && st < end) break;
else if(fact == 1 && st>end) break;
}
out.println(cost+" "+list.size());
for(int elem : list) out.print(elem+" ");
out.println();
}
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 11 | 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 | 0fa5be9a49e4118e5cb0b114eaa49de6 | 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.*;
import java.util.*;
/*
author : Multi-Thread
*/
public class C {
//public class Main {
// static int INF = 998244353;
static int INF = (int) 1e9 + 7;
static int MAX = Integer.MAX_VALUE;
static int MIN = Integer.MIN_VALUE;
public static void main(String[] args) {
int test = fs.nextInt();
// int test = 1;
for (int cases = 0; cases < test; cases++) {
// solve();
final_answer.append(solve() + "\n");
}
out.print(final_answer.toString());
out.flush();
}
static StringBuilder final_answer = new StringBuilder();
static String solve() {
char ar[] = fs.next().toCharArray();
ArrayList<ArrayList<Integer>> al = new ArrayList<>();
for (int i = 0; i < 26; i++) {
al.add(new ArrayList<>());
}
for (int i = 0; i < ar.length; i++) {
al.get(ar[i] - 'a').add(i + 1);
}
for (int i = 0; i < 26; i++)
Collections.sort(al.get(i));
ArrayList<Integer> moves = new ArrayList<Integer>();
int cost = 0, jump = 0;
char min = (char) (Integer.min(ar[0] - 'a', ar[ar.length - 1] - 'a') + 'a');
char max = (char) (Integer.max(ar[0] - 'a', ar[ar.length - 1] - 'a') + 'a');
if (ar[0] < ar[ar.length - 1]) {
for (char x = min; x <= max; x++) {
moves.addAll(al.get(x - 'a'));
}
} else {
for (char x = max; x >= min; x--) {
moves.addAll(al.get(x - 'a'));
}
}
cost = Math.abs(ar[0] - ar[ar.length - 1]);
jump = moves.size();
StringBuilder ss = new StringBuilder();
ss.append(cost + " " + jump + "\n");
for (Integer z : moves)
ss.append(z + " ");
return ss.toString();
}
static class Pair {
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
public String toString() {
return "[" + first + "," + second + "]";
}
}
static class LongPair {
long first;
long second;
LongPair(long a, long b) {
this.first = a;
this.second = b;
}
public String toString() {
return "[" + first + "," + second + "]";
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class 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() {
writer.print("\n");
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
private static final FastReader fs = new FastReader();
private static final OutputWriter out = new OutputWriter(System.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 11 | 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 | 42c965ddcf74cd58cc754dfb38ec742f | 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 long mod = (int)1e9+7;
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
int t=sc.nextInt();
// int t=1;
while(t-->0)
{
String st = sc.next();
char arr[] = st.toCharArray();
int n = arr.length;
HashMap<Character , ArrayList<Integer>> map = new HashMap<>();
int sum = 0;
int covered = 0;
int charTaken = 0;
for(int i=1;i<n-1;i++)
{
if(!map.containsKey(arr[i]))
{
map.put(arr[i] , new ArrayList<>());
}
map.get(arr[i]).add(i + 1);
}
if(arr[0] < arr[n - 1])
{
for(char x=arr[0];x<=arr[n - 1];x++)
{
if(map.containsKey(x))
{
charTaken += map.get(x).size();
}
}
out.println((arr[n - 1] - arr[0])+" "+(charTaken+2));
out.print(1+" ");
for(char x=arr[0];x<=arr[n-1];x++)
{
if(map.containsKey(x))
{
for(Integer y:map.get(x))
{
out.print(y+" ");
}
}
}
out.println(n);
}
else
{
for(char x=arr[0];x>=arr[n - 1];x--)
{
if(map.containsKey(x))
{
charTaken += map.get(x).size();
}
}
out.println((arr[0] - arr[n - 1])+" "+(charTaken+2));
out.print(1+" ");
for(char x=arr[0];x>=arr[n-1];x--)
{
if(map.containsKey(x))
{
for(Integer y:map.get(x))
{
out.print(y+" ");
}
}
}
out.println(n);
}
}
out.flush();
}
static int upper(ArrayList<Pair> list , int k)
{
int l = 0 , r = list.size() - 1;
while(l <= r)
{
int mid = (l + r) >> 1;
if(list.get(mid).first >= k)
{
r = mid - 1;
}
else
{
l = mid + 1;
}
}
return (l >= list.size()) ? list.size() - 1 : l;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
static class FenwickTree
{
//Binary Indexed Tree
//1 indexed
public int[] tree;
public int size;
public FenwickTree(int size)
{
this.size = size;
tree = new int[size+5];
}
public void add(int i, int v)
{
while(i <= size)
{
tree[i] += v;
i += i&-i;
}
}
public int find(int i)
{
int res = 0;
while(i >= 1)
{
res += tree[i];
i -= i&-i;
}
return res;
}
public int find(int l, int r)
{
return find(r)-find(l-1);
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
private static long mergeAndCount(int[] arr, int l,
int m, int r)
{
// Left subarray
int[] left = Arrays.copyOfRange(arr, l, m + 1);
// Right subarray
int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l;long swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swaps += (m + 1) - (l + i);
}
}
while (i < left.length)
arr[k++] = left[i++];
while (j < right.length)
arr[k++] = right[j++];
return swaps;
}
// Merge sort function
private static long mergeSortAndCount(int[] arr, int l,
int r)
{
// Keeps track of the inversion count at a
// particular node of the recursion tree
long count = 0;
if (l < r) {
int m = (l + r) / 2;
// Total inversion count = left subarray count
// + right subarray count + merge count
// Left subarray count
count += mergeSortAndCount(arr, l, m);
// Right subarray count
count += mergeSortAndCount(arr, m + 1, r);
// Merge count
count += mergeAndCount(arr, l, m, r);
}
return count;
}
static void my_sort(long[] arr)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list);
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static void reverse_sorted(int[] arr)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list , Collections.reverseOrder());
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static int LowerBound(ArrayList<Pair> a, int x) { // x is the target value or key
int l=-1,r=a.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(a.get(m).first>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(ArrayList<Pair> list, int x) {// x is the key or target value
int l=-1,r=list.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(list.get(m).first<=x) l=m;
else r=m;
}
return l+1;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static class Queue_Pair implements Comparable<Queue_Pair> {
int first , second;
public Queue_Pair(int first, int second) {
this.first=first;
this.second=second;
}
public int compareTo(Queue_Pair o) {
return Integer.compare(o.first, first);
}
}
static void leftRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
static void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n-1] = temp;
}
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters to compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
static boolean palindrome_array(char arr[], int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
return false;
else
return true;
}
static boolean allElementsEqual(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
static boolean allElementsDistinct(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
public static void reverse_Long(long[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
long temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
static boolean isSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static boolean isReverseSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] < a[i + 1]) {
return false;
}
}
return true;
}
static int[] rearrangeEvenAndOdd(int arr[], int n)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
int[] array = list.stream().mapToInt(i->i).toArray();
return array;
}
static long[] rearrangeEvenAndOddLong(long arr[], int n)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
long[] array = list.stream().mapToLong(i->i).toArray();
return array;
}
static boolean isPrime(long n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (long i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static long getSum(long n)
{
long sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcdLong(long a, long b)
{
if (b == 0)
return a;
return gcdLong(b, a % b);
}
static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static int countDigit(int n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
}
class Pair
{
int first;
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 11 | 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 | 2085dfa0d3d2b84284e182ddc611aab2 | 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 Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int t = in.nextInt();
while (t-- > 0){
String s = in.next();
int n = s.length();
int[] a = new int[n];
int[] b = new int[27];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(String.valueOf(s.charAt(i) - 'a'));
b[a[i]]++;
}
int sum = 0;
int jump = 0;
if(a[0] < a[n-1]){
int q = a[0];
for (int i = a[0]; i <= a[n-1]; i++) {
if(b[i] != 0) {
sum += i - q;
q = i;
jump += b[i];
}
}
out.println(sum+" "+jump);
int [] c = new int[n];
int js = 0;
for (int i = a[0]; i <= a[n-1]; i++) {
for (int j = 0; j < n; j++) {
if (a[j] == i){
c[js] = j+1;
js++;
}
}
}
for (int i = 0; i < n; i++) {
if(c[i] != 0) {
out.print(c[i] + " ");
}
}
}else{
int q = a[n-1];
for (int i = a[n-1]; i <= a[0]; i++) {
if(b[i] != 0) {
sum += i - q;
q = i;
jump += b[i];
}
}
out.println(sum+" "+jump);
int [] c = new int[n];
int js = 0;
for (int i = a[0]; i >= a[n-1]; i--) {
for (int j = 0; j < n; j++) {
if (a[j] == i){
c[js] = j+1;
js++;
}
}
}
for (int i = 0; i < n; i++) {
if(c[i] != 0) {
out.print(c[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;
}
boolean hasNext()
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
try
{
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e)
{
return false;
// TODO: handle exception
}
}
return true;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine()
{
String str = null;
try
{
str = reader.readLine();
} catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public BigInteger nextBigInteger()
{
return new BigInteger(next());
}
public BigDecimal nextBigDecimal()
{
return new BigDecimal(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 11 | 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 | d5749f11ead2cfa9789f925f3093332c | 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 JumpOnTile {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
String skip = sc.nextLine();
while (t-- > 0) {
String s = sc.nextLine();
int n = s.length();
int res = Math.abs(s.charAt(0) - s.charAt(n - 1));
List<Integer> count[] = new ArrayList[26];
for (int i = 0; i < 26; ++i) {
count[i] = new ArrayList<>();
}
for (int i = 0; i < n; ++i) {
count[s.charAt(i) - 'a'].add(i);
}
StringBuilder ans = new StringBuilder();
int no = 0;
if (s.charAt(0) < s.charAt(n - 1)) {
for (char c = s.charAt(0); c <= s.charAt(n - 1); ++c) {
for (int i = 0; i < count[c - 'a'].size(); ++i) {
if (ans.length() != 0) ans.append(" ");
int x = count[c - 'a'].get(i);
ans.append(x + 1);
++no;
}
}
}
else {
for (char c = s.charAt(0); c >= s.charAt(n - 1); --c) {
for (int i = 0; i < count[c - 'a'].size(); ++i) {
if (ans.length() != 0) ans.append(" ");
int x = count[c - 'a'].get(i);
ans.append(x + 1);
++no;
}
}
}
System.out.println(res + " " + no);
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 11 | 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 | 7ec56d3ed951a0e2cc34ff8f7a7cd78e | 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.*;
import java.math.BigInteger;
import java.math.*;
public class Div3_C
{
static FastReader sc= new FastReader();
//static PrintWriter out= new PrintWriter(System.out);
/*
******************************************************************************************************************
******************************************************************************************************************
******************************************************************************************************************
*****************************************************************************************************************
************************************SSSSSSSSSSSSSSSSSSSSSSSSSS****************************************************
************************************SSSSSSSSSSSSSSSSSSSSSSSSSS****************************************************
************************************SSSSSSSSSSSSSSSSSSSSSSSSSS****************************************************
************************************SSS***************************************************************************
************************************SSS***************************************************************************
************************************SSS***************************************************************************
************************************SSSSSSSSSSSSSSSSSSSSSSSSSS****************************************************
************************************SSSSSSSSSSSSSSSSSSSSSSSSSS****************************************************
************************************SSSSSSSSSSSSSSSSSSSSSSSSSS****************************************************
***********************************************************SSS****************************************************
***********************************************************SSS****************************************************
************************************SSSSSSSSSSSSSSSSSSSSSSSSSS****************************************************
************************************SSSSSSSSSSSSSSSSSSSSSSSSSS****************************************************
************************************SSSSSSSSSSSSSSSSSSSSSSSSSS****************************************************
******************************************************************************************************************
******************************************************************************************************************
******************************************************************************************************************
******************************************************************************************************************
*/
static Boolean prime[] = new Boolean[10000002];
static void seive(int n)
{
int count=0;
for(int p = 2; p*p <=n; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[p] == null)
{
// Update all multiples of p
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
}
public static void main(String args[])
{
PrintWriter out = new PrintWriter(System.out);
//long start=System.currentTimeMillis();
int test=sc.nextInt();
while(test>0)
{
HashMap<Character,ArrayList<Integer>> map=new HashMap();
String s=sc.nextLine();
for(int i=0; i<s.length(); i++)
{
if(map.get(s.charAt(i))==null)
map.put(s.charAt(i),new ArrayList());
map.get(s.charAt(i)).add(i);
}
//System.out.println(map);
char start=s.charAt(0);
char end=s.charAt(s.length()-1);
if(start==end)
{
System.out.println(0+" "+map.get(start).size());
for(int i=0; i<map.get(start).size(); i++)
System.out.print((map.get(start).get(i)+1)+" ");
System.out.println();
}
else if(start<end)
{
int sum=0;
int size=0;
char last=start;
int min=Math.abs(end-start);
for(char ch=start; ch<=end; ch++)
{
if(map.get(ch)!=null)
{
size=size+map.get(ch).size();
sum=sum+(ch-last)*map.get(ch).size();
last=ch;
}
}
System.out.println(Math.min(min,sum)+" "+size);
for(char ch=start; ch<=end; ch++)
{
if(map.get(ch)!=null)
{
for(int j=0; j<map.get(ch).size(); j++)
{
System.out.print((map.get(ch).get(j)+1)+" ");
}
}
}
System.out.println();
}
else
{
int sum=0;
int size=0;
char last=start;
int min=Math.abs(end-start);
for(char ch=start; ch>=end; ch--)
{
if(map.get(ch)!=null)
{
size=size+map.get(ch).size();
sum=sum+Math.abs(ch-last)*map.get(ch).size();
last=ch;
}
}
System.out.println(Math.min(sum,min)+" "+size);
for(char ch=start; ch>=end; ch--)
{
if(map.get(ch)!=null)
{
for(int j=0; j<map.get(ch).size(); j++)
{
System.out.print((map.get(ch).get(j)+1)+" ");
}
}
}
System.out.println();
}
test--;
}
long end=System.currentTimeMillis();
//System.out.println((end-start)+" "+"m/s");
}
static long gcd(long a, long b){ // GCD fun....
return b==0?a:gcd(b,a%b);
}
static int fact(int num){ // Factorial of int...12
int point=1;
for(int i=1; i<=num; i++)
{
point=point*i;
}
return point;
}
static void fact(BigInteger num){ // Factorial of BigInteger(>10^4)..2second
BigInteger one=BigInteger.valueOf(1);
BigInteger m=BigInteger.valueOf(1);
BigInteger i=one;
for(i=BigInteger.valueOf(1); num.compareTo(i)>=0; i=i.add(BigInteger.valueOf(1)))
m=m.multiply(i);
System.out.println(m);
}
static long fact(long num){ // lfactorial of Long....(<=16)
long point=1;
for(long i=1; i<=num; i++)
{
point=point*i;
}
return point;
}
static void input(long arr[][],int n,int m){ //long 2d-array input...
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
arr[i][j]=sc.nextLong();
}
}
}
static void input(int arr[][],int n,int m){ //int 2d-array input...
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
arr[i][j]=sc.nextInt();
}
}
}
static void input(String arr[][],int n,int m){ //String 2d-array input...
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
arr[i][j]=sc.nextLine();
}
}
}
static void input(long arr[],int n){ //long 1d array input
for(int i=0; i<n; i++)
{
arr[i]=sc.nextLong();
}
}
static void input(int arr[],int n){ //int 1d array input
for(int i=0; i<n; i++)
{
arr[i]=sc.nextInt();
}
}
static void input(char arr[],int n){ //char 1d array input
for(int i=0; i<n; i++)
{
arr[i]=sc.next().charAt(0);
}
}
private static int countDigits(int l) { //count of digit..Integer
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) { // count of Long Digit..
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;
}
static class FastReader { // Fast input Reader class ...Jaaan
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 s="";
try {
s=br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return s;
}
}
static void print(long a[]) { //print long array
int n=a.length;
for(int i=0;i<n;i++) System.out.print(a[i]+" ");
System.out.println();
}
static void print(int a[]) { //print int array
int n=a.length;
for(int i=0;i<n;i++) System.out.print(a[i]+" ");
System.out.println();
}
static void print(String a[]) { //print String array
int n=a.length;
for(int i=0;i<n;i++) System.out.print(a[i]+" ");
System.out.println();
}
static void print(double a[]) { //print Double array
int n=a.length;
for(int i=0;i<n;i++) System.out.print(a[i]+" ");
System.out.println();
}
static void print(char a[]) { //print Char array
int n=a.length;
for(int i=0;i<n;i++) System.out.print(a[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 11 | 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 | 07f6036cfec97bd7c027f5d8ae78b653 | 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_Round_820_Div3 {
static FastScanner fs;
static FastWriter fw;
static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null;
private static final int[][] kdir = new int[][]{{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}};
private static final int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
private static final int iMax = Integer.MAX_VALUE, iMin = Integer.MIN_VALUE;
private static final long lMax = Long.MAX_VALUE, lMin = Long.MIN_VALUE;
private static final int mod1 = (int) (1e9 + 7);
private static final int mod2 = 998244353;
public static void main(String[] args) throws IOException {
fs = new FastScanner();
fw = new FastWriter();
int t = 1;
t = fs.nextInt();
for (int tt = 1; tt <= t; tt++) {
// fw.out.print("Case #" + tt + ": ");
solve();
}
fw.out.close();
}
private static void solve() {
String str = fs.nextLine();
int n = str.length();
List<int[]> list = new ArrayList<>();
for (int i = 1; i < n - 1; i++) {
int ascii = str.charAt(i) - 'a';
list.add(new int[]{i + 1, ascii});
}
list.sort(Comparator.comparingInt(xx -> xx[1]));
int ascii_f = str.charAt(0) - 'a', ascii_s = str.charAt(n - 1) - 'a';
int ff = Math.min(ascii_f, ascii_s);
int ss = Math.max(ascii_f, ascii_s);
List<Integer> seq = new ArrayList<>();
int cnt = 2;
for (int[] x : list) {
if (x[1] < ff) continue;
if (x[1] > ss) break;
cnt++;
seq.add(x[0]);
}
fw.out.println((ss - ff) + " " + cnt);
fw.out.print(1 + " ");
if (ascii_f <= ascii_s) {
for (int num : seq) fw.out.print(num + " ");
} else {
for (int i = seq.size() - 1; i >= 0; i--)
fw.out.print(seq.get(i) + " ");
}
fw.out.print(n);
fw.out.println();
}
private static class UnionFind {
private final int[] parent;
private final int[] rank;
UnionFind(int n) {
parent = new int[n + 5];
rank = new int[n + 5];
for (int i = 0; i <= n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
private int find(int i) {
if (parent[i] == i)
return i;
return parent[i] = find(parent[i]);
}
private void union(int a, int b) {
a = find(a);
b = find(b);
if (a != b) {
if (rank[a] < rank[b]) {
int temp = a;
a = b;
b = temp;
}
parent[b] = a;
if (rank[a] == rank[b])
rank[a]++;
}
}
}
private static class SCC {
private final int n;
private final List<List<Integer>> adjList;
SCC(int _n, List<List<Integer>> _adjList) {
n = _n;
adjList = _adjList;
}
private List<List<Integer>> getSCC() {
List<List<Integer>> ans = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
boolean[] vis = new boolean[n];
for (int i = 0; i < n; i++) {
if (!vis[i]) dfs(i, adjList, vis, stack);
}
vis = new boolean[n];
List<List<Integer>> rev_adjList = rev_graph(n, adjList);
while (!stack.isEmpty()) {
int curr = stack.pop();
if (!vis[curr]) {
List<Integer> scc_list = new ArrayList<>();
dfs2(curr, rev_adjList, vis, scc_list);
ans.add(scc_list);
}
}
return ans;
}
private void dfs(int curr, List<List<Integer>> adjList, boolean[] vis, Stack<Integer> stack) {
vis[curr] = true;
for (int x : adjList.get(curr)) {
if (!vis[x]) dfs(x, adjList, vis, stack);
}
stack.add(curr);
}
private void dfs2(int curr, List<List<Integer>> adjList, boolean[] vis, List<Integer> scc_list) {
vis[curr] = true;
scc_list.add(curr);
for (int x : adjList.get(curr)) {
if (!vis[x]) dfs2(x, adjList, vis, scc_list);
}
}
}
private static List<List<Integer>> rev_graph(int n, List<List<Integer>> adjList) {
List<List<Integer>> ans = new ArrayList<>();
for (int i = 0; i < n; i++) ans.add(new ArrayList<>());
for (int i = 0; i < n; i++) {
for (int x : adjList.get(i)) {
ans.get(x).add(i);
}
}
return ans;
}
private static class Calc_nCr {
private final long[] fact;
private final long[] invfact;
private final int p;
Calc_nCr(int n, int prime) {
fact = new long[n + 5];
invfact = new long[n + 5];
p = prime;
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = (i * fact[i - 1]) % p;
}
invfact[n] = pow_with_mod(fact[n], p - 2, p);
for (int i = n - 1; i >= 0; i--) {
invfact[i] = (invfact[i + 1] * (i + 1)) % p;
}
}
private long nCr(int n, int r) {
if (r > n || n < 0 || r < 0) return 0;
return (((fact[n] * invfact[r]) % p) * invfact[n - r]) % p;
}
}
private static int random_between_two_numbers(int l, int r) {
Random ran = new Random();
return ran.nextInt(r - l) + l;
}
private static long gcd(long a, long b) {
return (b == 0 ? a : gcd(b, a % b));
}
private static long lcm(long a, long b) {
return ((a * b) / gcd(a, b));
}
private static long pow(long a, long b) {
long result = 1;
while (b > 0) {
if ((b & 1L) == 1) {
result = (result * a);
}
a = (a * a);
b >>= 1;
}
return result;
}
private static long pow_with_mod(long a, long b, int mod) {
long result = 1;
while (b > 0) {
if ((b & 1L) == 1) {
result = (result * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long ceilDiv(long a, long b) {
return ((a + b - 1) / b);
}
private static long getMin(long... args) {
long min = lMax;
for (long arg : args)
min = Math.min(min, arg);
return min;
}
private static long getMax(long... args) {
long max = lMin;
for (long arg : args)
max = Math.max(max, arg);
return max;
}
private static boolean isPalindrome(String s, int l, int r) {
int i = l, j = r;
while (j - i >= 1) {
if (s.charAt(i) != s.charAt(j))
return false;
i++;
j--;
}
return true;
}
private static List<Integer> primes(int n) {
boolean[] primeArr = new boolean[n + 5];
Arrays.fill(primeArr, true);
for (int i = 2; (i * i) <= n; i++) {
if (primeArr[i]) {
for (int j = i * i; j <= n; j += i) {
primeArr[j] = false;
}
}
}
List<Integer> primeList = new ArrayList<>();
for (int i = 2; i <= n; i++) {
if (primeArr[i])
primeList.add(i);
}
return primeList;
}
private static int noOfSetBits(long x) {
int cnt = 0;
while (x != 0) {
x = x & (x - 1);
cnt++;
}
return cnt;
}
private static int sumOfDigits(long num) {
int cnt = 0;
while (num > 0) {
cnt += (num % 10);
num /= 10;
}
return cnt;
}
private static int noOfDigits(long num) {
int cnt = 0;
while (num > 0) {
cnt++;
num /= 10;
}
return cnt;
}
private static boolean isPerfectSquare(long num) {
long sqrt = (long) Math.sqrt(num);
return ((sqrt * sqrt) == num);
}
private static class Pair<U, V> {
private final U first;
private final V second;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return first.equals(pair.first) && second.equals(pair.second);
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
@Override
public String toString() {
return "(" + first + ", " + second + ")";
}
private Pair(U ff, V ss) {
this.first = ff;
this.second = ss;
}
}
private static void randomizeIntArr(int[] arr, int n) {
Random r = new Random();
for (int i = (n - 1); i > 0; i--) {
int j = r.nextInt(i + 1);
swapInIntArr(arr, i, j);
}
}
private static void randomizeLongArr(long[] arr, int n) {
Random r = new Random();
for (int i = (n - 1); i > 0; i--) {
int j = r.nextInt(i + 1);
swapInLongArr(arr, i, j);
}
}
private static void swapInIntArr(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
private static void swapInLongArr(long[] arr, int a, int b) {
long temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
private static int[] readIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = fs.nextInt();
return arr;
}
private static long[] readLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = fs.nextLong();
return arr;
}
private static List<Integer> readIntList(int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(fs.nextInt());
return list;
}
private static List<Long> readLongList(int n) {
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(fs.nextLong());
return list;
}
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner() throws IOException {
if (checkOnlineJudge)
this.br = new BufferedReader(new FileReader("src/input.txt"));
else
this.br = new BufferedReader(new InputStreamReader(System.in));
this.st = new StringTokenizer("");
}
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException err) {
err.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
if (st.hasMoreTokens()) {
return st.nextToken("").trim();
}
try {
return br.readLine().trim();
} catch (IOException err) {
err.printStackTrace();
}
return "";
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
private static class FastWriter {
PrintWriter out;
FastWriter() throws IOException {
if (checkOnlineJudge)
out = new PrintWriter(new FileWriter("src/output.txt"));
else
out = new PrintWriter(System.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 11 | 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 | 48b6a753d8462fe0eab95d1255086fa4 | 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.HashSet;
import java.util.Scanner;
public class pb4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0 ){
char[] str = sc.next().toCharArray();
int n = str.length;
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for (int i = 0; i<= 27 ; i++){
adj.add(new ArrayList<>());
}
for (int i = 0; i < n ; i++){
int num = str[i] - 'a';
adj.get(num).add(i+1);
}
int start = str[0] - 'a';
int end = str[n-1] - 'a';
ArrayList<Integer> ans = new ArrayList<>();
int diff = Math.abs(start - end) ;
if (start < end){
while (start <= end){
ans.addAll(adj.get(start));
start++;
}
}
else{
while (start >= end){
ans.addAll(adj.get(start));
start--;
}
}
System.out.println( diff + " " + (ans.size()));
for (int num : ans){
System.out.print(num + " ");
}
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 11 | 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 | b03274361dde1659d0e846054269a982 | 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 {
static Scanner scn = new Scanner(System.in);
static class Node {
int x;
int y;
Node(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
int t = scn.nextInt();
while(t-->0) {
int mod = (int)1e9 + 7;
//int n = scn.nextInt();
//we have to keep checking the third letter of the string
//if the third character after char is 0 then that means that the next two nums make up a char
//if not then the curr single num makes up the char
//the 3rd letter should be checked within the constraints
//we have to check the next to next char, if that';s 0 then the 1st condition is followed
String str = scn.next();
int tiles = 2;
int score =0;
ArrayList<Node> list = new ArrayList<>();
int n = str.length();
score = Math.abs(str.charAt(str.length() -1) - str.charAt(0)); // score has been recorded
for(int i = 1 ; i<n-1 ; i++){
if((str.charAt(i)>=str.charAt(0) && str.charAt(i)<=str.charAt(str.length() - 1))){
tiles+=1;
list.add(new Node(i+1,str.charAt(i)));
continue;
}
if((str.charAt(i)<=str.charAt(0) && str.charAt(i)>=str.charAt(str.length() - 1))){
tiles+=1;
list.add(new Node(i+1,str.charAt(i)));
continue;
}
}
if(str.charAt(0)<str.charAt(n-1)) {
list.sort((o1, o2)
-> o1.y - o2.y);
}
else{
list.sort((o1, o2)
-> o2.y - o1.y);
}
//list.add(n);
System.out.println(score + " " + tiles);
System.out.print(1 + " ");
for(Node element : list){
System.out.print(element.x + " ");
}
System.out.print(n + " ");
System.out.println();
// long pow = expo(b,c,mod-1);
// long ans = expo(a,pow,mod) * (1%mod);
// System.out.println(ans);
}
}
static long expo(long x ,long n,long mod){
if(n == 0){
return 1;
}
if(n==1){
return x;
}
if(n%2==0){
//for even integers
long num = expo(x,n/2,mod);
return ((num%mod)*(num%mod))%mod;
}
else{
//for odd integers
long num = expo(x,(n-1)/2,mod);
num = ((num%mod)*(num%mod))%mod;
return ((num%mod)*(x%mod))%mod;
}
}
} | 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 11 | 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 | dd46c42bed0205e097ca95ac568e1a33 | 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 Codeforces12Sep3 {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
while(T-- > 0) {
String s = br.readLine();
int n = s.length();
int st = s.charAt(0) - 'a' + 1;
int en = s.charAt(n - 1) - 'a' + 1;
int arr[][] = new int[n][2];
for(int i = 0; i < n; i++) {
arr[i][0] = i + 1;
arr[i][1] = s.charAt(i) - 'a' + 1;
}
Arrays.sort(arr, (a, b) -> Integer.compare(a[1], b[1]));
int cost = Math.abs(st - en);
int stInd = -1;
for(int i = 0; i < n; i++) {
if(arr[i][0] == 1) {
stInd = i;
}
}
List<Integer> li = new ArrayList<>();
if(st <= en) {
for(int i = stInd; i < n && arr[i][1] <= en; i++) {
li.add(arr[i][0]);
}
}
else {
int j = stInd + 1;
li.add(arr[stInd][0]);
while(j < n && arr[j][1] == arr[j - 1][1]) {
li.add(arr[j][0]);
j++;
}
j = stInd - 1;
while(j > 0 && arr[j][1] != en) {
li.add(arr[j][0]);
j--;
}
int temp = j;
j = j - 1;
while(j >= 0 && arr[j][1] == en) {
li.add(arr[j][0]);
j--;
}
li.add(arr[temp][0]);
}
System.out.println(cost + " " + li.size());
for(Integer e: li) {
System.out.print(e + " ");
}
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 11 | 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 | f7e03080d03ced78055d3b3252586fad | 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.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-- >0) {
String s=sc.next();
int nums[]=new int[s.length()];
for(int i=0;i<s.length();i++) {
nums[i]=(int)(s.charAt(i)-'a'+1);
}
HashMap<Integer,ArrayList<Integer>> mp=new HashMap<>();
for(int i=0;i<nums.length;i++) {
if(!mp.containsKey(nums[i])) {
ArrayList<Integer> list=new ArrayList<>();
list.add(i+1);
mp.put(nums[i], list);
}
else {
ArrayList<Integer> list=mp.get(nums[i]);
list.add(i+1);
mp.put(nums[i], list);
}
}
int a=nums[0];
int b=nums[nums.length-1];
ArrayList<Integer> ans=new ArrayList<>();
if(a<b) {
for(int i=a;i<=b;i++) {
if(mp.containsKey(i)) {
ArrayList<Integer> temp=mp.get(i);
for(int j=0;j<temp.size();j++) ans.add(temp.get(j));
}
}
}
else if(b<a) {
for(int i=b;i<=a;i++) {
if(mp.containsKey(i)) {
ArrayList<Integer> temp=mp.get(i);
for(int j=temp.size()-1;j>=0;j--) ans.add(temp.get(j));
}
}
Collections.reverse(ans);
}
else {
if(mp.containsKey(a)) {
ArrayList<Integer> temp=mp.get(a);
for(int j=0;j<temp.size();j++) ans.add(temp.get(j));
}
}
System.out.print((int)Math.abs(a-b));
System.out.println(" "+ans.size());
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 | b5877f5fda80012e6b6af8f6ec30d797 | 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();
Map<Integer,LinkedList<Integer>> tm;
sc.nextLine();
int l=(int)(s.charAt(0))-96;
int r=(int)(s.charAt(s.length()-1))-96;
if(l>r)
tm=new TreeMap<>(Collections.reverseOrder());
else
tm=new TreeMap<>();
int count=2;
LinkedList<Integer> ls=new LinkedList<>();
ls.add(1);
tm.put(l,ls);
for(int i=1;i<s.length()-1;i++)
{
int num=(int)s.charAt(i)-96;
if((l<r && num>=l && num<=r) || (l>=r && num<=l && num>=r))
{
if(tm.containsKey(num)){
tm.get(num).addLast(i+1);
}
else{
ls=new LinkedList<>();
ls.add(i+1);
tm.put(num,ls);
}
count++;
}
}
if(tm.containsKey(r))
tm.get(r).add(s.length());
else
{
ls=new LinkedList<>();
ls.add(s.length());
tm.put(r,ls);
}
System.out.println(Math.abs(r-l)+" "+count);
for( Map.Entry<Integer,LinkedList<Integer>> m : tm.entrySet()){
LinkedList<Integer> list=new LinkedList<>();
list=m.getValue();
while(!list.isEmpty()){
System.out.print(list.poll()+" ");
}
}
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 | 8b4edf09337179ed8dbf040324d12949 | 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.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
public class app {
public static long m;
static int mod = 998244353;
static int inf = (int) 1e9;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
//int t=1;
Gr1zler:
for (int op = 0; op < t; op++) {
StringBuilder scc = new StringBuilder();
String a = sc.next();
int df = a.charAt(0);
int gf = a.charAt(a.length() - 1);
Map<Integer, ArrayList<Integer>> fv = new HashMap<>();
Stack<Integer> jj = new Stack<>();
int dff = 0;
for (int i = 1; i < a.length()-1; i++) {
// System.out.println((int)a.charAt(i)+" "+Math.min(df,gf));
if ((int) a.charAt(i) >= Math.min(df, gf) && Math.max(df, gf) >= (int) a.charAt(i)) {
if (!fv.containsKey((int)a.charAt(i))) {
fv.put((int)a.charAt(i), new ArrayList<>());
}
fv.get((int)a.charAt(i)).add(i + 1);
dff++;
}
}
System.out.println((int) (Math.abs(df - gf)) + " " + (dff+2));
scc.append(1+" ");
if (df <=gf) {
for (int i=df;i<=gf;i++) {
if(fv.containsKey((i))) {
for (int j = 0; j < fv.get((i)).size(); j++) {
scc.append(fv.get((i)).get(j) + " ");
}
}
}
} else {
for (int i=df;i>=gf;i--) {
if(fv.containsKey((i))) {
for (int j = 0; j < fv.get((i)).size(); j++) {
scc.append(fv.get((i)).get(j) + " ");
}
}
}
}
scc.append(a.length()+" ");
System.out.println(scc.toString());
}
out.close();
}
//out.println(Arrays.toString(d).replace(",","").replace("]","").replace("[",""));
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static void sort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static void sort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public int[] nextArrInt(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextArrLong(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextArrIntSorted(int n) throws IOException {
int[] a = new int[n];
Integer[] a1 = new Integer[n];
for (int i = 0; i < n; i++)
a1[i] = nextInt();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[i].intValue();
return a;
}
public long[] nextArrLongSorted(int n) throws IOException {
long[] a = new long[n];
Long[] a1 = new Long[n];
for (int i = 0; i < n; i++)
a1[i] = nextLong();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[i].longValue();
return a;
}
}
} | Java | ["6\n\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 | 2d2271991eb953cdc846de7afd88dc0b | 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.*;
/* Name of the class has to be "Main" only if the class is public. */
public class codeforces
{
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
public static void main (String[] args)
{
// your code goes here
Scanner s = new Scanner(System.in);
int t=s.nextInt();
for(int z=0;z<t;z++)
{
String a = s.next();
int x=0;
int n=a.length();
int ans= Math.abs((a.charAt(0)-'a') - (a.charAt(n-1)-'a'));
int arr[] = new int[a.length()];
HashMap<Integer,Integer> h = new HashMap<>();
HashMap<Integer,Integer> hm = new HashMap<>();
if(a.charAt(0) > a.charAt(a.length()-1)) {
for (int i = 0; i < a.length(); i++) {
hm.put(i +1, (a.charAt(i) - 'a'));
}
h = sortByValue(hm);
arr[x++]=n;
for (Map.Entry<Integer, Integer> entry : h.entrySet()) {
if (entry.getValue() <= (a.charAt(0) - 'a') && entry.getValue() >= (a.charAt(n - 1) - 'a') && entry.getKey()!=1 && entry.getKey()!=n) {
arr[x] = entry.getKey();
x++;
}
}
arr[x++]=1;
System.out.println(ans + " " + x);
for(int i=x-1;i>=0;i--)
System.out.print(arr[i] + " ");
System.out.println();
}
else{
for (int i = 0; i < a.length(); i++) {
hm.put(i +1, (a.charAt(i) - 'a'));
}
h = sortByValue(hm);
arr[x++] = 1;
for (Map.Entry<Integer, Integer> entry : h.entrySet())
{
if(entry.getValue()>= (a.charAt(0) - 'a') && entry.getValue()<= (a.charAt(n-1) - 'a') && entry.getKey()!=1 && entry.getKey()!=n) {
arr[x]=entry.getKey();
x++;
}
}
arr[x++]=n;
System.out.println(ans + " " + x);
for(int i=0;i<x;i++)
System.out.print(arr[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 | 6474f2bb220f26633ddd5b6167dd5f4d | 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.InputStreamReader;
import java.util.*;
public class Solution {
private FastReader reader;
private StringBuilder sb;
private static final int TEN_POW_FIVE = (int)1e5;
private static final int MOD = (int)1e9+7;
public Solution() {
this.reader = new FastReader();
this.sb = new StringBuilder();
}
private void solve() throws Exception {
int t = reader.getInt();
while(t-->0) {
String s = reader.getString();
solve(s, s.length());
}
reader.closeReader();
}
private void solve(String s, int size) {
Map<Character, List<Integer>> map = new HashMap<>();
for(int i=0; i<size; i++) {
char c = s.charAt(i);
if(!map.containsKey(c)) {
map.put(c, new ArrayList<>());
}
map.get(c).add(i);
}
char start = s.charAt(0);
char end = s.charAt(size-1);
boolean isMin = false;
if(start>end) {
isMin = true;
char temp = start;
start = end;
end = temp;
for(char c='a'; c<='z'; c++) {
if(map.containsKey(c)) {
Collections.reverse(map.get(c));
}
}
}
List<Integer> res = new ArrayList<>();
res.addAll(map.get(start));
char cur = (char)(start+1);
int jumps = map.get(start).size();
int minDist = end-start;
while(cur<=end) {
if (map.containsKey(cur)) {
List<Integer> indexes = map.get(cur);
jumps += indexes.size();
res.addAll(indexes);
}
cur++;
}
if(isMin) {
Collections.reverse(res);
}
sb.append(minDist).append(" ").append(jumps).append("\n");
for(int i=0; i<res.size(); i++) {
sb.append(res.get(i)+1).append(" ");
}
sb.append("\n");
}
private void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private int min(Integer... minArr) {
int min = minArr[0];
for(int i=1; i<minArr.length; i++) {
min = Math.min(min, minArr[i]);
}
return min;
}
private int max(Integer... maxArr) {
int max = maxArr[0];
for(int i=1; i<maxArr.length; i++) {
max = Math.max(max, maxArr[i]);
}
return max;
}
private void printArr(int[] arr, int n, int incrementer) {
for(int i=0; i<n; i++) {
sb.append(arr[i]+incrementer).append(" ");
}
sb.append("\n");
}
public static void main(String[] args) {
try {
Solution solution = new Solution();
solution.solve();
solution.printOp();
} catch(Exception e) {
e.printStackTrace();
}
}
private void printOp() {
System.out.println(sb.toString());
}
static class FastReader {
private BufferedReader inputReader;
private StringTokenizer st;
public FastReader() {
inputReader = new BufferedReader(new InputStreamReader(System.in));
}
private String getNext() throws Exception {
if(st==null || !st.hasMoreTokens()) {
st = new StringTokenizer(inputReader.readLine());
}
return st.nextToken();
}
public String[] getLine(String delimiter) throws Exception {
return inputReader.readLine().split(delimiter);
}
public String getString() throws Exception {
return getNext();
}
public int getInt() throws Exception {
return Integer.parseInt(getNext());
}
public long getLong() throws Exception {
return Long.parseLong(getNext());
}
public double getDouble() throws Exception {
return Double.parseDouble(getNext());
}
public int[] getIntArr(int n) throws Exception {
int[] arr = new int[n];
for(int i=0; i<n; i++) {
arr[i] = getInt();
}
return arr;
}
public long[] getLongArr(int n) throws Exception {
long[] arr = new long[n];
for(int i=0; i<n; i++) {
arr[i] = getLong();
}
return arr;
}
public double[] getDoubleArr(int n) throws Exception {
double[] arr = new double[n];
for(int i=0; i<n; i++) {
arr[i] = getDouble();
}
return arr;
}
public String[] getStrArr(int n) throws Exception {
String[] arr = new String[n];
for(int i=0; i<n; i++) {
arr[i] = getString();
}
return arr;
}
public void closeReader() throws Exception {
inputReader.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 | 8cf6f88fd2e29acd7335bef50b3dd3b7 | 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.IOException;
import java.io.InputStream;
import java.util.*;
public class JumpingOnTiles {
static int lowestCost;
static int maxSteps;
static LinkedList<Integer> path;
public static void dfs(String s, int current, boolean[] visited, LinkedList<Integer> pathTillHere, int currentCost, int threshold){
if (currentCost<=threshold) {
if (current == s.length() - 1) {
if (currentCost < lowestCost || (currentCost == lowestCost && pathTillHere.size() > maxSteps)) {
lowestCost = currentCost;
maxSteps = pathTillHere.size();
path = new LinkedList<Integer>(pathTillHere);
path.addLast(current + 1);
}
} else {
visited[current] = true;
pathTillHere.addLast(current + 1);
for (int i = 0; i < s.length(); i++) {
if (visited[i] == false)
dfs(s, i, visited, pathTillHere, currentCost + Math.abs(s.charAt(i) - s.charAt(current)), threshold);
}
visited[current] = false;
pathTillHere.pollLast();
}
}
}
public static void main(String[] args)throws Exception{
FastReader f=new FastReader(System.in);
int t=f.nextInt();
while (t-->0){
String s=f.nextString();
HashMap<Character, LinkedList<Integer>> charMap=new HashMap<Character, LinkedList<Integer>>();
for (int i=0;i<s.length();i++){
char c=s.charAt(i);
LinkedList<Integer> indexList=charMap.getOrDefault(c, new LinkedList<Integer>());
indexList.addLast(i);
if (!charMap.containsKey(c))
charMap.put(c, indexList);
}
char start=s.charAt(0);
char end=s.charAt(s.length()-1);
lowestCost=0;
maxSteps=0;
path=new LinkedList<Integer>();
char previousChar = start;
if (start<end) {
for (char c = start; c <= end; c++) {
if (charMap.containsKey(c)) {
lowestCost += Math.abs(previousChar - c);
maxSteps += charMap.get(c).size();
previousChar = c;
}
}
System.out.println(lowestCost+" "+maxSteps);
for (char c=start;c<=end;c++){
if (charMap.containsKey(c)){
LinkedList<Integer> l = charMap.get(c);
for (Integer j:l){
System.out.print((j+1)+" ");
}
}
}
System.out.println();
} else {
for (char c = start; c >= end; c--) {
if (charMap.containsKey(c)) {
lowestCost += Math.abs(previousChar - c);
maxSteps += charMap.get(c).size();
previousChar = c;
}
}
System.out.println(lowestCost+" "+maxSteps);
for (char c=start;c>=end;c--){
if (charMap.containsKey(c)){
LinkedList<Integer> l = charMap.get(c);
for (Integer j:l){
System.out.print((j+1)+" ");
}
}
}
System.out.println();
}
/*lowestCost=Integer.MAX_VALUE;
maxSteps=Integer.MIN_VALUE;
path=new LinkedList<Integer>();
dfs(s, 0, new boolean[s.length()], new LinkedList<Integer>(), 0, Math.abs(s.charAt(0)-s.charAt(s.length()-1)));
System.out.println(lowestCost+" "+(maxSteps+1));
for (int i=0;i<path.size();i++){
System.out.print(path.get(i)+" ");
}
System.out.println();*/
}
}
}
class FastReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int pnumChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (pnumChars == -1) {
throw new InputMismatchException();
}
if (curChar >= pnumChars) {
curChar = 0;
try {
pnumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (pnumChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public String next() {
return nextString();
}
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 == ',') {
c = read();
}
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();
int 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 double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double 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 {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
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 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 | 56da5fd62db673a91fddfe857dea7937 | 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 C1 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
sc.nextLine();
for(int p=0;p<N;p++){
char[] ch=sc.next().toCharArray();
int n=ch.length;
int cost=Math.abs(ch[0]-ch[n-1]);
int jump=0;
List<Integer> list=new ArrayList<>();
for(int i=0;i<n;i++){
if(ch[i]>=Math.min(ch[0],ch[n-1]) && ch[i]<=Math.max(ch[0],ch[n-1]))
list.add(i);
}
Collections.sort(list,(a,b)->{
if(ch[a]!=ch[b])
return Integer.compare(Math.abs(ch[a]-ch[0]),Math.abs(ch[b]-ch[0]));
else
return Integer.compare(a,b);
});
System.out.println(cost+" "+list.size());
for(int i:list) {
System.out.print(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 | 53c48aaae740859063423519ea5dab94 | 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 C1 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
sc.nextLine();
for(int p=0;p<N;p++){
char[] ch=sc.next().toCharArray();
int n=ch.length;
int cost=Math.abs(ch[0]-ch[n-1]);
int jump=0;
List<Integer> list=new ArrayList<>();
for(int i=0;i<n;i++){
if(ch[i]>=Math.min(ch[0],ch[n-1]) && ch[i]<=Math.max(ch[0],ch[n-1]))
list.add(i);
}
Collections.sort(list,(a,b)->{
if(ch[a]!=ch[b])
return Integer.compare(Math.abs(ch[a]-ch[0]),Math.abs(ch[b]-ch[0]));
return Integer.compare(a,b);
});
System.out.println(cost+" "+list.size());
for(int i:list) {
System.out.print(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 | ec51346de9370529b03cda0938d7c005 | 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 exercie {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int t = sc.nextInt();
while(t-- > 0){
String s = sc.next();
List<Integer> array = new ArrayList<>();
int max = Math.max(s.charAt(0) - 'a', s.charAt(s.length() - 1) - 'a');
int min = Math.min(s.charAt(0) - 'a', s.charAt(s.length() - 1) - 'a');
for(int i = 1; i < s.length() - 1; i++){
if(s.charAt(i) - 'a' >= min && s.charAt(i) - 'a' <= max){
array.add(i + 1);
}
}
if(s.charAt(0) > s.charAt(s.length() - 1)){
Collections.sort(array, (a,b) ->{
return (s.charAt(b - 1) - 'a') - (s.charAt(a - 1) - 'a');
});
}else{
Collections.sort(array, (a,b) ->{
return (s.charAt(a - 1) - 'a') - (s.charAt(b - 1) - 'a');
});
}
System.out.println((max - min) + " " + (array.size() + 2));
System.out.print(1 + " ");
for(int i: array){
System.out.print(i + " ");
}
System.out.print(s.length());
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 | 979c0e8761155b7b6522917dd2cd4b0e | 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 exercie {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int t = sc.nextInt();
while(t-- > 0){
String g = sc.next();
int[] array = new int[g.length()];
int same = 0;
for(int i = 0; i < g.length(); i++){
array[i] = g.charAt(i) - 96;
}
PriorityQueue<Integer> pq = new PriorityQueue<>( (a,b) -> {
if(array[0] - array[array.length - 1] > 0){
return array[b] - array[a];
}else{
return array[a] - array[b];
}
});
if(array[0] > array[array.length - 1]){
for (int i = 0; i < array.length - 1; i++){
if(array[0] >= array[i] && array[i] >= array[array.length - 1]){
pq.add(i);
if(array[i] == array[0] && i != 0){
same++;
}
}
}
}
else{
for (int i = 0; i < array.length - 1; i++){
if(array[0] <= array[i] && array[i] <= array[array.length - 1]){
pq.add(i);
}
if(array[i] == array[0] && i != 0){
same++;
}
}
}
List<Integer> list= new ArrayList<>();
while(!pq.isEmpty()){
list.add(pq.poll());
}
int check = 0;
for(int i = 0; i < list.size() - 1; i++){
check += Math.abs(array[list.get(i)] - array[list.get(i+1)]);
}
check += Math.abs( array[list.get(list.size() - 1)] - array[array.length - 1] );
if(check == Math.abs(array[0] - array[array.length - 1])){
System.out.println((int)Math.abs(array[0] - array[array.length - 1]) + " " + (list.size() + 1) );
for(int i: list){
System.out.print((i+1) + " ");
}
System.out.print(array.length);
System.out.println();
}
else{
System.out.println((int)Math.abs(array[0] - array[array.length - 1]) + " " + (same + 2) );
for(int i = 0; i <= same ; i++){
System.out.print((list.get(i)+1) + " ");
}
System.out.print(array.length);
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 | a0b2dd2f09d898ed901717b019189960 | 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.Comparator;
import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
in.nextLine();
while (t-- > 0) {
String s = in.nextLine();
char[] str = s.toCharArray();
int[][] arr = new int[s.length()][2];
for (int i = 0; i < s.length(); i++) {
arr[i][0] = str[i];
arr[i][1] = i + 1;
}
int n = s.length();
if(arr[0][0]>arr[n-1][0]){
int[] tmp = arr[0];
arr[0] = arr[n-1];
arr[n-1] = tmp;
}
Arrays.sort(arr, 0, n, Comparator.comparingInt(a -> a[0]));
// Arrays.sort(arr, 1, n, Comparator.comparingInt(a -> a[0]));
// Arrays.sort(arr, 0, n-1, Comparator.comparingInt(a -> a[0]));
Arrays.sort(arr, 1, n-1, Comparator.comparingInt(a -> a[0]));
// for (int i = 0; i < n; i++) {
// System.out.println(arr[i][0]+" "+arr[i][1]);
// }
int start = -1;
int end = -1;
for (int i = 0; i < n; i++) {
if (arr[i][1] == 1) {
start = i;
}
if (arr[i][1] == n) {
end = i;
}
}
int cost = Math.abs(arr[start][0] - arr[end][0]);
int m = Math.abs(start - end) + 1;
if (start == end) {
m = 0;
}
System.out.println(cost + " " + m);
if (start < end) {
for (int i = start; i < end; i++) {
System.out.print(arr[i][1] + " ");
}
System.out.println(arr[end][1]);
} else {
for (int i = start; i > end; i--) {
System.out.print(arr[i][1] + " ");
}
System.out.println(arr[end][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 | cf79bcd62f0aa3394d6f0b3bacef3ae4 | 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;
static PrintWriter pw;
public static void main(String[] args) throws IOException {
sc = new Scanner(System.in);
pw = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
String s = sc.next();
char st = s.charAt(0), en = s.charAt(s.length() - 1);
Pair[] arr = new Pair[s.length()];
for (int i = 1; i <= s.length(); i++) {
arr[i - 1] = new Pair(s.charAt(i - 1), i);
}
Arrays.sort(arr);
// pw.println(Arrays.toString(arr));
int cnt = 2;
StringBuilder sb = new StringBuilder();
int i = 0;
sb.append(1 + " ");
if (en >= st) {
while (i < s.length() && arr[i].c != st) {
i++;
}
while (i < s.length() && arr[i].c <= en) {
if (arr[i].idx != s.length() && arr[i].idx != 1) {
cnt++;
sb.append(arr[i].idx + " ");
}
i++;
}
} else {
i = s.length() - 1;
while (i >= 0 && arr[i].c != st) {
i--;
}
while (i >= 0 && arr[i].c >= en) {
if (arr[i].idx != s.length() && arr[i].idx != 1) {
cnt++;
sb.append(arr[i].idx + " ");
}
i--;
}
}
sb.append(s.length());
pw.println(Math.abs(st - en) + " " + cnt);
pw.println(sb);
}
pw.close();
}
static class Pair implements Comparable<Pair> {
char c;
int idx;
public Pair(char c, int i) {
this.c = c;
idx = i;
}
@Override
public int compareTo(Pair o) {
return c - o.c;
}
@Override
public String toString() {
return c + " " + idx;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String readAllLines(BufferedReader reader) throws IOException {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
return content.toString();
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | 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 | e0a72bdd26b437e35e82b1d6d474383c | 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 {
private static final FastScanner scanner = new FastScanner();
public static void main(String[] args) {
int t = scanner.nextInt();
while (t-- != 0) {
String str = scanner.next();
List<int[]> sorted = new ArrayList<>(str.length() + 1);
for (int i = 0; i < str.length(); ++i) {
sorted.add(new int[] { (int) str.charAt(i), i });
}
int first = str.charAt(0);
int last = str.charAt(str.length() - 1);
sorted.sort((a, b) -> {
int compare = Integer.compare(a[0], b[0]);
if (compare != 0) {
return compare;
} else {
compare = Integer.compare(a[1], b[1]);
if (first > last) {
return -compare;
}
return compare;
}
});
List<Integer> moves = new ArrayList<>(str.length() + 1);
int cost = Math.abs(first - last);
int min = Math.min(first, last);
int max = Math.max(first, last);
for (int i = 0; i < sorted.size(); ++i) {
int[] character = sorted.get(i);
if (character[0] >= min && character[0] <= max) {
moves.add(character[1] + 1);
}
}
System.out.println(cost + " " + moves.size());
print(moves, first > last);
}
}
private static void print(List<Integer> arr, boolean reverse) {
StringBuilder builder = new StringBuilder();
boolean first = true;
for (int i = 0; i < arr.size(); ++i) {
if (first) {
first = false;
} else {
builder.append(' ');
}
builder.append(arr.get(reverse ? arr.size() - 1 - i : i));
}
System.out.println(builder);
}
private static void print(int[] arr) {
StringBuilder builder = new StringBuilder();
boolean first = true;
for (int element : arr) {
if (first) {
first = false;
} else {
builder.append(' ');
}
builder.append(element);
}
System.out.println(builder);
}
static class FastScanner {
private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public 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 |
Subsets and Splits