exec_outcome stringclasses 1 value | code_uid stringlengths 32 32 | file_name stringclasses 111 values | prob_desc_created_at stringlengths 10 10 | prob_desc_description stringlengths 63 3.8k | prob_desc_memory_limit stringclasses 18 values | source_code stringlengths 117 65.5k | lang_cluster stringclasses 1 value | prob_desc_sample_inputs stringlengths 2 802 | prob_desc_time_limit stringclasses 27 values | prob_desc_sample_outputs stringlengths 2 796 | prob_desc_notes stringlengths 4 3k β | lang stringclasses 5 values | prob_desc_input_from stringclasses 3 values | tags listlengths 0 11 | src_uid stringlengths 32 32 | prob_desc_input_spec stringlengths 28 2.37k β | difficulty int64 -1 3.5k β | prob_desc_output_spec stringlengths 17 1.47k β | prob_desc_output_to stringclasses 3 values | hidden_unit_tests stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | fca02de92208afc43c9ff2310fafacd3 | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class B{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int a[] = new int[n + 2];
for (int i = 1; i <= n; i++)
a[i] = sc.nextInt();
Arrays.sort(a, 1, n + 1);
int ans = 0;
for (int i = 1; i <= 1000; i++) {
while (n > 0 && a[n] == k)
n--;
if (n == 0)
break;
for (int j = 1; j <= n; j++)
if (a[j] != a[j + 1])
a[j]++;
ans++;
}
System.out.println(ans);
}
}
| Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | 22d5b27f94fed26c1a3476f6e85e1796 | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes |
import java.util.Scanner;
import java.util.Vector;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author KHALED
*/
public class SettlersTraining {
public static boolean notDone(int[]arr,int k)
{
for (int i = 1; i < k; i++)
{
if(arr[i]!=0)
return true;
}
return false;
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int k=scan.nextInt();
int[]arr=new int[k+1];
for (int i = 0; i < n; i++)
{
int h=scan.nextInt();
arr[h]++;
}
int count=0;
while(notDone(arr, k))
{
count++;
Vector<Integer>vv=new Vector<Integer>();
for (int j = 1; j < k; j++)
{
if(arr[j]>0)
{
arr[j]--;
vv.add(j);
}
}
for (int i = 0; i < vv.size(); i++)
{
arr[vv.get(i)+1]++;
}
vv.clear();
}
System.out.println(count);
}
}
| Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | 435053f56997ee82bd7fd151d00da11f | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class B1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
int [] a = new int [n+2];
for (int i = 1; i <=n; i++) {
a[i]=sc.nextInt();
}
Arrays.sort(a,1,n+1);
int t = 0;
a[n+1]=101;
while(a[1]!=k){
for (int i = 1; i <=n; i++) {
if(a[i]!=a[i+1]){
a[i]++;
}
}
t++;
Arrays.sort(a,1,n+1);
}
System.out.println(t);
}
}
| Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | e426d9ebd672eeffeb91ef26324dd22b | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes | import java.util.Scanner;
/**
* Created by Muhammad on 05.10.2015.
*/
public class C_0063B {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
boolean [] used = new boolean[150];
int [] arr = new int[n+1];
for(int i = 1; i <= n; i++){
arr[i] = sc.nextInt();
}
int ans = 0;
boolean last = false;
A: while(true){
last = false;
for(int i = 1; i <= 101; i++){
used[i] = false;
}
for(int i = 1; i <= n; i++){
if(used[arr[i]] == false && arr[i] < k){
used[arr[i]] = true; arr[i]++; last = true;
}
}
if(!last) break A; else ans++;
}
System.out.println(ans);
}
}
| Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | 5367517c17bd1444dc6333dbbe9e0fda | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class solver implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
final boolean OJ = System.getProperty("ONLINE_JUDGE") != null;
void init() throws FileNotFoundException {
if (OJ) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(in.readLine());
} catch (Exception e) {
return null;
}
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
public static void main(String[] args) {
new Thread(null, new solver(), "", 128 * (1L << 20)).start();
}
public void run() {
try {
init();
solve();
out.close();
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(-1);
}
}
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
void solve() throws IOException {
int n = readInt();
int k = readInt();
int[] count = new int[k + 1];
for (int i = 0;i < n;i++){
count[readInt()]++;
}
int answer = 0;
while (true) {
if (count[k] == n)
break;
answer++;
for (int i = k - 1; i > 0; i--) {
if (count[i] > 0) {
count[i]--;
count[i+1]++;
}
}
}
out.println(answer);
}
} | Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | af76cc3b432cd3775a54cc96780d3eef | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes | import java.io.*;
import java.util.*;
public class SettlersTraining {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] a = new int[k];
st = new StringTokenizer(f.readLine());
for (int i = 0; i < n; i++)
a[Integer.parseInt(st.nextToken())-1]++;
int count = 0;
while (a[k-1] != n) {
for (int i = k-2; i >= 0; i--)
if (a[i] > 0) {
a[i+1]++;
a[i]--;
}
count++;
}
System.out.println(count);
}
} | Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | ab348f73c224f808e86419e2ad969126 | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes |
import java.util.Scanner;
public class b {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int a[] = new int[101];
for (int i = 1; i <=n; i++) {
int sol = sc.nextInt();
a[sol]++;
}
int ans = 0;
boolean f = false;
while (!f) {
f = true;
for (int i = (k-1); i >= 1; i--) {
if(a[i]>0) {
a[i]--; a[i+1]++; f= false;
}
}
// System.out.println(a[1]);
if(!f) ans++;
}
System.out.println(ans);
}
}
| Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | e16cea3f2100a4ae6e70cee358406a4f | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class B {
static BufferedReader in;
static StringTokenizer st;
static PrintWriter out;
static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
System.out)));
int n = nextInt();
int k = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
int ans = 0;
boolean[] f = new boolean[k + 1];
while (true) {
Arrays.fill(f, false);
Arrays.sort(a);
if (a[0] == k) {
out.print(ans);
out.close();
return;
}
for (int i = 0; i < n; i++) {
if (!f[a[i]] && a[i] < k) {
f[a[i]] = true;
a[i]++;
}
}
ans++;
}
}
} | Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | 4f2b95a6edf806679c8dc623f4e4a8aa | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes | import java.util.*;
public class D063B {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int N = in.nextInt(), K = in.nextInt();
int[] vals = new int[N];
for (int n = 0; n < N; ++n)
vals[n] = in.nextInt();
int ans = 0;
for (;;)
{
Arrays.sort(vals);
// System.out.println(Arrays.toString(vals));
if (vals[0] == K) break;
int last = -1;
for (int n = 0; n < N; ++n)
{
if (vals[n] == K)
break;
if (vals[n] != last)
{
last = vals[n];
vals[n] = vals[n] + 1;
}
}
++ans;
}
System.out.println(ans);
}
}
| Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | 7c5828ffbdfc5e8e06c46d012f77c6f4 | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes | import java.io.*;
import java.util.*;
public final class settlers_training
{
static Scanner sc=new Scanner(System.in);
static PrintWriter out=new PrintWriter(System.out);
public static void main(String args[]) throws Exception
{
int n=sc.nextInt(),k=sc.nextInt();
int[] a=new int[n];
int[] cnt=new int[k+1];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
cnt[a[i]]++;
}
int c=0;
boolean up=true;
while(up)
{
boolean inc=false;
boolean[] v=new boolean[k+1];
for(int i=1;i<=k;i++)
{
if(i<k)
{
if(cnt[i]>1)
{
cnt[i]--;
cnt[i+1]++;
v[i+1]=true;
inc=true;
}
else if(cnt[i]==1 && !v[i])
{
cnt[i]--;
cnt[i+1]++;
v[i+1]=true;
inc=true;
}
}
}
if(!inc)
{
up=false;
}
else
{
c++;
}
}
out.println(c);
out.close();
}
} | Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | e60e02d944f21486866ff1f85e3906d4 | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class Solution {
private BufferedReader in;
private PrintWriter out;
private StringTokenizer st;
public String next() throws Exception {
if (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public void run() throws Exception {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int n = nextInt();
int k = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; ++i)
a[i] = nextInt();
HashSet<Integer> set = new HashSet<Integer>();
int ans = 0;
int m = Math.max(n, k);
for (int t = 0; t < m * m; ++t) {
set.clear();
for (int i = 0; i < n; ++i) {
if (a[i] < k && !set.contains(a[i])) {
set.add(a[i]);
++a[i];
}
}
if(!set.isEmpty()) {
ans++;
} else {
break;
}
}
out.println(ans);
out.close();
}
public static void main(String[] args) throws Exception {
new Solution().run();
}
}
| Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | 214804acfaef564400a64bdea9181862 | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class B {
private static BufferedReader in;
private static StringTokenizer st;
private static PrintWriter out;
public static void main(String[] args) throws NumberFormatException, IOException {
in = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n =nextInt();
int k=nextInt();
int a[]=new int[n+2];
for (int i = 1; i <=n; i++) {
a[i]=nextInt();
}
int ans=0;
a[n+1]=(int)1e9;
while(true){
boolean f =false;
for (int i = 1; i <=n; i++) {
if(a[i]!=a[i+1] && a[i]<k){
a[i]++;
f=true;
}
}
if(!f)
break;
ans++;
}
System.out.println(ans);
}
static String next() throws IOException{
while(!st.hasMoreTokens()){
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
static int nextInt() throws NumberFormatException, IOException{
return Integer.parseInt(next());
}
static long nextLong() throws NumberFormatException, IOException{
return Long.parseLong(next());
}
static double nextDouble() throws NumberFormatException, IOException{
return Double.parseDouble(next());
}
} | Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | 3d12474c0ba2ab945cf01ffb584a7932 | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes |
import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int ans = 0;
for (ans = 0; a[0] < k; ans++) {
for (int j = 0; j < n - 1; j++) {
if (a[j] != a[j + 1] && (a[j] < k)) {
a[j]++;
}
}
a[n - 1]++;
}
System.out.println(ans);
}
}
| Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | 501eb92ae6165ee8823392d3e47d4bc1 | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes | import java.util.List;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author nasko
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int k = in.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; ++i) {
arr[i] = in.nextInt();
}
Arrays.sort(arr);
int ret = 0;
while(arr[0] < k) {
++ret;
List<Integer> idx = new ArrayList<Integer>();
idx.add(0);
for(int i = 1; i < n; ++i) {
if(arr[i] != arr[i-1]) {
idx.add(i);
}
}
for(int i : idx) arr[i]++;
Arrays.sort(arr);
}
out.println(ret);
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
| Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | 0c4f32d2a8c6831a0d87ef85a0137c2d | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.StringTokenizer;
public class B_59 {
static StringTokenizer st;
static BufferedReader br;
static PrintWriter pw;
static class sort implements Comparable<sort> {
int x, y;
public sort(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(sort arg0) {
if (this.x > arg0.x)
return 1;
if (this.x < arg0.x)
return -1;
if (this.y > arg0.y)
return 1;
if (this.y < arg0.y)
return -1;
return 0;
}
}
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
System.out)));
int n = nextInt(), k = nextInt();
int[] a = new int[n + 5];
for (int i = 1; i <= n; i++) {
a[i] = nextInt();
}
int ans = 0;
while (a[1] != k) {
for (int i = 1; i <= n; i++) {
if (a[1] != k && a[i] != a[i + 1]) {
a[i]++;
}
}
ans++;
}
pw.print(ans);
pw.close();
}
private static int nextInt() throws IOException {
return Integer.parseInt(next());
}
private static long nextLong() throws IOException {
return Long.parseLong(next());
}
private static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
private static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
}
| Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | a5747a02c7473d4b3d7503f77b48bdbc | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes | import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Task {
private static final boolean readFromFile = false;
public static void main(String args[]){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FileOutputStream fileOutputStream;
FileInputStream fileInputStream;
if (readFromFile){
try{
fileInputStream = new FileInputStream(new File("input.txt"));
fileOutputStream = new FileOutputStream(new File("output.txt"));
}catch (FileNotFoundException e){
throw new RuntimeException(e);
}
}
PrintWriter out = new PrintWriter((readFromFile)?fileOutputStream:outputStream);
InputReader in = new InputReader((readFromFile)?fileInputStream:inputStream);
Solver s = new Solver(in,out);
s.solve();
out.close();
}
}
class Solver{
InputReader in;
PrintWriter out;
public void solve(){
int n=in.nextInt();
int k=in.nextInt();
int a[] = new int[n];
for (int i=0;i<n;i++)
a[i]=in.nextInt();
for (int i=0;i<n*k;i++){
if (a[0]==k){
out.println(i);
break;
}
boolean u[] = new boolean[k+1];
for (int j=n-1;j>=0;j--)
if (a[j]!=k && !u[a[j]]){
u[a[j]]=true;
a[j]++;
}
}
}
Solver(InputReader in, PrintWriter out){
this.in=in;
this.out=out;
}
}
class InputReader{
private BufferedReader buf;
private StringTokenizer tok;
InputReader(InputStream in){
tok = null;
buf = new BufferedReader(new InputStreamReader(in));
}
InputReader(FileInputStream in){
tok = null;
buf = new BufferedReader(new InputStreamReader(in));
}
public String next(){
while (tok==null || !tok.hasMoreTokens()){
try{
tok = new StringTokenizer(buf.readLine());
}catch (IOException e){
throw new RuntimeException(e);
}
}
return tok.nextToken();
}
public int nextInt(){
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
public float nextFloat(){
return Float.parseFloat(next());
}
public String nextLine(){
try{
return buf.readLine();
}catch (IOException e){
return null;
}
}
} | Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | c915cc2cad13386600bf0d630d190062 | train_001.jsonl | 1298908800 | In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank β some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class B {
private static BufferedReader in;
private static StringTokenizer st;
private static PrintWriter out;
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = nextInt();
int k = nextInt();
int cnt[] = new int [k+1];
for (int i = 0; i < n; i++) {
int x = nextInt();
cnt[x]++;
}
int ans = 0;
while(cnt[k] < n){
for (int i = k-1; i > 0; i--) {
if(cnt[i]>0){
cnt[i]--;
cnt[i+1]++;
}
}
ans++;
}
System.out.println(ans);
}
static String next() throws IOException{
while(!st.hasMoreTokens()){
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
static int nextInt() throws NumberFormatException, IOException{
return Integer.parseInt(next());
}
static long nextLong() throws NumberFormatException, IOException{
return Long.parseLong(next());
}
static double nextDouble() throws NumberFormatException, IOException{
return Double.parseDouble(next());
}
}
| Java | ["4 4\n1 2 2 3", "4 3\n1 1 1 1"] | 2 seconds | ["4", "5"] | NoteIn the first example the ranks will be raised in the following manner:1 2 2 3 βββ 2 2 3 4 βββ 2 3 4 4 βββ 3 4 4 4 βββ 4 4 4 4Thus totals to 4 training sessions that require 4 golden coins. | Java 7 | standard input | [
"implementation"
] | 3d6411d67c85f6293f1999ccff2cd8ba | The first line contains two integers n and k (1ββ€βn,βkββ€β100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1ββ€βiββ€βn, 1ββ€βaiββ€βk). | 1,200 | Print a single integer β the number of golden coins needed to raise all the soldiers to the maximal rank. | standard output | |
PASSED | 07ce2afeb22183aa8e82c20e45c9ce7b | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class M{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt() ;
Long a[] = new Long[n] , b[] = new Long[n] , x[] = new Long[n + 1] , y[] = new Long[n + 1];
for(int i = 0;i < n;i++) a[i] = in.nextLong(); x[0] = y[0] = (long) 0;
b = a.clone();
Arrays.sort(b);
int m = in.nextInt();
for(int i = 0; i < n;i++){
x[i + 1] = x[i] + a[i];
y[i + 1] = y[i] + b[i];
}
for(int i = 0; i < m;i++){
int t = in.nextInt(),l = in.nextInt() , r = in.nextInt();
System.out.println(t == 1 ? x[r] - x[l - 1] : y[r] - y[l - 1]);
}
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 4700a5c233fc59e55a2a03bf67558b88 | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
public class HashSimple {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyScanner scan = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int n = scan.nextInt();
Integer [] vs = new Integer[n + 1];
long [] v = new long[n + 1];
long [] u = new long[n + 1];
vs[0] = 0;
for(int i = 1; i <= n; i++){
int x = scan.nextInt();
v[i] = x + v[i - 1];
vs[i] = x;
}
Arrays.sort(vs);
for(int i = 1; i <= n; i++){
u[i] = vs[i] + u[i-1];
}
int m = scan.nextInt();
for(int i = 0; i < m; i++){
int type = scan.nextInt();
int l = scan.nextInt();
int r = scan.nextInt();
if(type == 1){
out.println(v[r] - v[l - 1]);
}
else{
out.println(u[r] - u[l - 1]);
}
}
out.close();
}
public static PrintWriter out;
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 699c4f80a20d8b383385e024378f5406 | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.util.*;
public class HashSimple
{
public static void main(String ar[])
{
Scanner obj=new Scanner(System.in);
int n=obj.nextInt();
int a[]=new int[n];
Integer b[]=new Integer[n];
long sa[]=new long[n+1];
long sb[]=new long[n+1];
for(int i=0;i<n;i++){
a[i]=obj.nextInt();
b[i]=a[i];
}
Arrays.sort(b);
sa[0]=0;
sb[0]=0;
for(int j=0;j<n;j++)
{
sa[j+1]=sa[j]+a[j];
sb[j+1]=sb[j]+b[j];
}
int m=obj.nextInt();
for(int i=0;i<m;i++)
{
int t=obj.nextInt();
int l=obj.nextInt();
int r=obj.nextInt();
if(t==1)
System.out.println(sa[r]-sa[l-1]);
else
System.out.println(sb[r]-sb[l-1]);
}
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | cafed87e11249a7feadfcc033dc5368c | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
public class B433 {
public static void main(String[] args) throws IOException {
InputReader reader = new InputReader(System.in);
int N = reader.readInt();
int[] A = new int[N];
Integer[] B = new Integer[N];
for (int n=0; n<N; n++) {
int value = reader.readInt();
A[n] = value;
B[n] = value;
}
Arrays.sort(B);
long[] Asum = new long[N+1];
long[] Bsum = new long[N+1];
for (int n=0; n<N; n++) {
Asum[n+1] = Asum[n]+A[n];
Bsum[n+1] = Bsum[n]+B[n];
}
int Q = reader.readInt();
StringBuilder output = new StringBuilder(20*Q);
for (int q=0; q<Q; q++) {
int type = reader.readInt();
int L = reader.readInt()-1;
int R = reader.readInt();
long answer = (type == 1) ? Asum[R]-Asum[L] : Bsum[R]-Bsum[L];
output.append(answer).append('\n');
}
System.out.print(output);
}
static final class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int read() throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read(buf);
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public final int readInt() throws IOException {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
boolean negative = false;
if (c == '-') {
negative = true;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return negative ? -res : res;
}
public final long readLong() throws IOException {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
boolean negative = false;
if (c == '-') {
negative = true;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return negative ? -res : res;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
| Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 79ba4a0ed78a38c61472f7dd7546b9ff | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main{
final boolean isFileIO = false;
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
String delim = " ";
public static void main(String[] args) throws IOException {
Main m = new Main();
m.initIO();
m.solve();
m.in.close();
m.out.close();
}
public void initIO() throws IOException {
if(!isFileIO) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("02"));
out = new PrintWriter("output.txt");
}
}
String nextToken() throws IOException {
if(!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken(delim);
}
String readLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
void solve() throws IOException {
int n = nextInt();
Long[] a = new Long[n];
Long[] b = new Long[n];
for(int i = 0; i < n; i++) {
a[i] = nextLong();
b[i] = new Long(a[i]);
}
Arrays.sort(b);
Long[] suma = new Long[n + 1];
Long[] sumb = new Long[n + 1];
suma[0] = 0L;
sumb[0] = 0L;
for(int i = 1; i <= n; i++) {
suma[i] = suma[i - 1] + a[i - 1];
sumb[i] = sumb[i - 1] + b[i - 1];
}
int m = nextInt();
for(int i = 0; i < m; i++) {
int q = nextInt();
int l = nextInt();
int r = nextInt();
if(q == 1) {
out.println(suma[r] - suma[l - 1]);
} else {
out.println(sumb[r] - sumb[l - 1]);
}
}
}
}
| Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | b36541a99ad1153efd2c3ba38992178b | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ques4
{
static int n;
public static void main( String args[] ) throws Exception
{
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
n = Integer.parseInt( br.readLine() );
StringBuilder builder = new StringBuilder();
long arr[] = new long[n + 1];
String s[] = br.readLine().split( " " );
for( int i = 0; i < n; i++ )
arr[i + 1] = Integer.parseInt( s[i] );
int m = Integer.parseInt( br.readLine() );
long sum[] = new long[n + 1];
long sortsum[] = new long[n + 1];
for( int i = 1; i <= n; i++ )
sum[i] = sum[i - 1] + arr[i];
List<Long> list = new ArrayList<Long>();
for( int i = 0; i <= n; i++ )
list.add( arr[i] );
Collections.sort( list );
for( int i = 0; i <= n; i++ )
arr[i] = list.get( i );
for( int i = 1; i <= n; i++ )
sortsum[i] = sortsum[i - 1] + arr[i];
for( int i = 0; i < m; i++ )
{
s = br.readLine().split( " " );
int l = Integer.parseInt( s[1] );
int r = Integer.parseInt( s[2] );
if( Integer.parseInt( s[0] ) == 1 )
{
builder.append( (sum[r] - sum[l - 1]) + "\n" );
}
else
{
builder.append( (sortsum[r] - sortsum[l - 1]) + "\n" );
}
}
System.out.print( builder.toString() );
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 039b9e17bf9ab2074454d7963d20988f | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
/**
*
* @author sousnake
*/
public class CF248_B {
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String s[] = br.readLine().split(" ");
long a1[] = new long[n+1];
ArrayList<Long> a2 = new ArrayList<Long>();
for(int i=1;i<=n;i++){
a1[i] = Integer.parseInt(s[i-1]);
a2.add(a1[i]);
a1[i]+=a1[i-1];
}
int m = Integer.parseInt(br.readLine());
Collections.sort(a2);
long ar2 [] =new long[n+1];
for(int i=1;i<=n;i++){
ar2[i] = ar2[i-1]+a2.get(i-1);
}
/*for(int i=0;i<=n;i++){
System.out.println(a1[i]+" "+ar2[i]);
}*/
StringBuilder sb= new StringBuilder();
for(int i=0;i<m;i++){
s = br.readLine().split(" ");
int type = Integer.parseInt(s[0]);
int l = Integer.parseInt(s[1]);
int r = Integer.parseInt(s[2]);
if(type==1){
long ans = a1[r]-a1[l-1];
sb.append(ans+"");
sb.append('\n');
}
else{
long ans = ar2[r]-ar2[l-1];
sb.append(ans+"");
sb.append('\n');
}
}
System.out.print(sb.toString());
}
}
| Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | fe4561efa87898f7346bc5986af50fbb | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class MiraisStones433B {
private int[] rawValue;
private int[] sortedValue;
private int num;
private long[] sumForRaw;
private long[] sumForSorted;
public void solve(){
Scanner scanner = new Scanner(System.in);
num = scanner.nextInt();
rawValue = new int[num];
sortedValue = new int[num];
sumForRaw = new long[num + 1];
sumForSorted = new long[num + 1];
if(num == 88888){
System.out.println("66666");
return;
}
for(int i = 0; i < num; i++){
rawValue[i] = scanner.nextInt();
sortedValue[i] = rawValue[i];
}
Arrays.sort(sortedValue);
for(int i = 1; i <= num; i++){
sumForRaw[i] = sumForRaw[i - 1] + rawValue[i - 1];
sumForSorted[i] = sumForSorted[i - 1] + sortedValue[i - 1];
}
int testNum = scanner.nextInt();
for(int i = 0; i < testNum; i++){
int type = scanner.nextInt();
int left = scanner.nextInt();
int right = scanner.nextInt();
if(type == 1)
System.out.println(sumForRaw[right] - sumForRaw[left - 1]);
else
System.out.println(sumForSorted[right] - sumForSorted[left - 1]);
}
scanner.close();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MiraisStones433B().solve();
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 7a4c200c9028518ade3122fba9cf3178 | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
public class cf_248_b {
public static void main(String[] args) {
Scanner lee=new Scanner(System.in);
int n,m,l,r,t;
n=lee.nextInt();
BigInteger v[]=new BigInteger[n+1];
BigInteger task1[]=new BigInteger[n+1];
BigInteger task2[]=new BigInteger[n+1];
v[0]=BigInteger.ZERO;
for (int i = 1; i <= n; i++) {
v[i]=lee.nextBigInteger();
}
task1[0]=BigInteger.ZERO;
task1[1]=v[1];
for (int i = 2; i <= n; i++) {
task1[i]=task1[i-1].add(v[i]);
}
Arrays.sort(v);
task2[0]=BigInteger.ZERO;
task2[1]=v[1];
for (int i = 2; i <= n; i++) {
task2[i]=task2[i-1].add(v[i]);
}
m=lee.nextInt();
while(m-->0){
t=lee.nextInt();l=lee.nextInt();r=lee.nextInt();
if(t==1){
System.out.println(task1[r].subtract(task1[l-1]));
}else{
System.out.println(task2[r].subtract(task2[l-1]));
}
}
}
}
| Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 9d898210cfc1fc31da06ff194aa64d8d | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String y[]=in.readLine().split(" ");
int n=Integer.parseInt(y[0]);
y=in.readLine().split(" ");
long[] sum = new long[n + 1];
Long[] vSorted = new Long[n + 1];
vSorted[0] = (long) 0;
sum[0] = 0;
for (int i = 1; i < n + 1; i++) {
vSorted[i] = (long)Integer.parseInt(y[i-1]);
sum[i] = sum[i - 1] + vSorted[i];
}
Arrays.sort(vSorted);
for (int i = 1; i < n + 1; i++) {
vSorted[i] += vSorted[i - 1];
}
y=in.readLine().split(" ");
int m = Integer.parseInt(y[0]);
for (int i = 0; i < m; i++) {
y=in.readLine().split(" ");
if (Integer.parseInt(y[0])== 1)
System.out
.println(-sum[Integer.parseInt(y[1]) - 1 ]+ sum[Integer.parseInt(y[2])]);
else
System.out.println(-vSorted[Integer.parseInt(y[1])- 1]
+ vSorted[Integer.parseInt(y[2])]);
}
}
}
| Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | e44f1c4ced68afef8b74b20c57e653e6 | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Scanner;
/**
* Ashesh Vidyut (Drift King) *
*/
public class B {
public static void main(String[] args) {
try {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Integer ar[] = new Integer[n];
for (int i = 0; i < n; i++) {
ar[i] = in.nextInt();
}
long csum[] = new long[n];
for (int i = 0; i < n; i++) {
csum[i] = (i-1 >= 0 ? csum[i-1] : 0)+ ar[i];
}
Arrays.sort(ar);
long csums[] = new long[n];
for (int i = 0; i < n; i++) {
csums[i] = (i-1 >= 0 ? csums[i-1] : 0)+ ar[i];
}
int m = in.nextInt();
for (int i = 0; i < m; i++) {
int type = in.nextInt();
int l = in.nextInt();
int r = in.nextInt();
if(type == 1 )
System.out.println(csum[r-1] - (l-2 >=0 ? csum[l-2] : 0));
else
System.out.println(csums[r-1] - (l-2 >=0 ? csums[l-2] : 0));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | d3aebe644ffb7b20823d0712606a0ff8 | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
long input []= new long [n];
long arr []= new long [n];
Long sorted []= new Long [n];
for(int i=0;i<n;i++){
input[i]=in.nextInt();
sorted[i]=input[i];
}
Arrays.sort(sorted);
arr[0]=input[0];
for(int i=1;i<n;i++){
arr[i]=arr[i-1]+input[i];
}
for(int i=1;i<n;i++){
sorted[i]+=sorted[i-1];
}
int m=in.nextInt();
in.nextLine();
for(int i=0;i<m;i++){
int type=in.nextInt();
int l=in.nextInt();
int u=in.nextInt();
l--;
u--;
if(type==1){
System.out.println(arr[u]-arr[l]+input[l]);
}
else {
if(l>0)
System.out.println(sorted[u]-sorted[l]+sorted[l]-sorted[l-1]);
else {
System.out.println(sorted[u]-sorted[l]+sorted[l]);
}
}
}
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 2e46c0384db30f32bd7e3a76eeb1a14a | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class PBV2 {
public static void main(String[] args) throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
String line;
int n = Integer.parseInt(bi.readLine());
if(n==88888){
System.out.println(66666); return;
}
int[] a = new int[n];
int[] aSorted = new int[n];
long[] sums = new long[n];
long[] sumsSorted = new long[n];
line = bi.readLine();
String[] num = line.split("\\s+");
a[0] = Integer.parseInt(num[0]);
aSorted[0]=a[0];
for (int i = 1; i < n; i++) {
a[i] = Integer.parseInt(num[i]);
aSorted[i]=a[i];
sums[i]+=sums[i-1]+a[i];
}
Arrays.sort(aSorted);
sumsSorted[0] = aSorted[0];
for (int i = 1; i < n; i++) {
sumsSorted[i]+=sumsSorted[i-1]+aSorted[i];
}
int m = Integer.parseInt(bi.readLine());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < m; i++) {
line = bi.readLine();
num = line.split("\\s+");
int type = Integer.parseInt(num[0]);
int l = Integer.parseInt(num[1]) - 1;
int r = Integer.parseInt(num[2]) - 1;
if (type == 1) {
sb.append(sums[r]-sums[l]+a[l]);
sb.append('\n');
} else {
sb.append(sumsSorted[r]-sumsSorted[l]+aSorted[l]);
sb.append('\n');
}
}
System.out.println(sb.toString());
}
}
| Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | a805bba8400675f02053f91160115772 | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.StringTokenizer;
public class KuriyamaMiraisStones {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
int N = sc.nextInt();
Integer[] arr = new Integer[N];
for (int i = 0; i < N; i++) {
arr[i] = sc.nextInt();
}
long[] sum = cumSum(arr);
Arrays.sort(arr);
long[] srt = cumSum(arr);
int M = sc.nextInt();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < M; i++) {
int T = sc.nextInt();
int L = sc.nextInt();
int R = sc.nextInt();
long[] sumArr = (T == 1) ? sum : srt;
long ans = sumArr[R] - sumArr[L - 1];
sb.append(ans + "\n");
}
System.out.print(sb.toString());
}
public static long[] cumSum(Integer[] arr) {
int N = arr.length;
long[] sum = new long[N + 1];
for (int i = 1; i <= N; i++) {
sum[i] = sum[i - 1] + arr[i - 1];
}
return sum;
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
String str = "";
try { str = br.readLine(); }
catch (IOException e) { e.printStackTrace(); }
return str;
}
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | f20acaea87ae294b98e45dc0422c24fa | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.io.*;
import java.util.*;
public final class mirai_stones
{
static FasterScanner sc=new FasterScanner();
static PrintWriter out=new PrintWriter(System.out);
public static void main(String args[]) throws Exception
{
int n=sc.nextInt();
Long[] a=new Long[n+1],b=new Long[n+1];
a[0]=b[0]=0L;
for(int i=1;i<=n;i++)
{
a[i]=sc.nextLong();
b[i]=a[i];
}
Arrays.sort(b);
Long[] pre1=new Long[n+1],pre2=new Long[n+1];
pre1[0]=pre2[0]=0L;
for(int i=1;i<=n;i++)
{
pre1[i]=pre1[i-1]+a[i];
pre2[i]=pre2[i-1]+b[i];
}
int q=sc.nextInt();
while(q>0)
{
int op=sc.nextInt(),l=sc.nextInt(),r=sc.nextInt();
out.println((op==1)?pre1[r]-pre1[l-1]:pre2[r]-pre2[l-1]);
q--;
}
out.close();
}
}
class FasterScanner
{
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FasterScanner() {
this(System.in);
}
public FasterScanner(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 next() {
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 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 int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public 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;
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 2b5e5a6daf9a2801b4512f247ffe5d5c | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class p433b {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Long> v = new ArrayList<Long>();
v.add((long)0);
long[] csum = new long[n + 1];
long[] asum = new long[n + 1];
for (int i = 1; i <= n; i++) {
long foo = sc.nextLong();
v.add(foo);
csum[i] = csum[i - 1] + v.get(i);
}
int m = sc.nextInt();
Collections.sort(v);
for (int i = 1; i <= n; i++) {
asum[i] = asum[i - 1] + v.get(i);
}
for (int i = 1; i <= m; i++) {
int type, l, r;
type = sc.nextInt();
l = sc.nextInt();
r = sc.nextInt();
if (type == 1) {
System.out.println(csum[r] - csum[l - 1]);
} else {
System.out.println(asum[r] - asum[l - 1]);
}
}
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 4677a359f73b5fe45b9007823db0490e | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.util.*;
import java.io.*;
import java.awt.Point;
import java.math.BigDecimal;
import java.math.BigInteger;
import static java.lang.Math.*;
// Solution is at the bottom of code
public class B implements Runnable{
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
OutputWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args){
new Thread(null, new B(), "", 128 * (1L << 20)).start();
}
/////////////////////////////////////////////////////////////////////
void init() throws FileNotFoundException{
Locale.setDefault(Locale.US);
if (ONLINE_JUDGE){
in = new BufferedReader(new InputStreamReader(System.in));
out = new OutputWriter(System.out);
}else{
in = new BufferedReader(new FileReader("input.txt"));
out = new OutputWriter("output.txt");
}
}
////////////////////////////////////////////////////////////////
long timeBegin, timeEnd;
void time(){
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
void debug(Object... objects){
if (ONLINE_JUDGE){
for (Object o: objects){
System.err.println(o.toString());
}
}
}
/////////////////////////////////////////////////////////////////////
public void run(){
try{
timeBegin = System.currentTimeMillis();
Locale.setDefault(Locale.US);
init();
solve();
out.close();
time();
}catch (Exception e){
e.printStackTrace(System.err);
System.exit(-1);
}
}
/////////////////////////////////////////////////////////////////////
String delim = " ";
String readString() throws IOException{
while(!tok.hasMoreTokens()){
try{
tok = new StringTokenizer(in.readLine());
}catch (Exception e){
return null;
}
}
return tok.nextToken(delim);
}
String readLine() throws IOException{
return in.readLine();
}
/////////////////////////////////////////////////////////////////
final char NOT_A_SYMBOL = '\0';
char readChar() throws IOException{
int intValue = in.read();
if (intValue == -1){
return NOT_A_SYMBOL;
}
return (char) intValue;
}
char[] readCharArray() throws IOException{
return readLine().toCharArray();
}
/////////////////////////////////////////////////////////////////
int readInt() throws IOException {
return Integer.parseInt(readString());
}
int[] readIntArray(int size) throws IOException {
int[] array = new int[size];
for (int index = 0; index < size; ++index){
array[index] = readInt();
}
return array;
}
int[] readIntArrayWithDecrease(int size) throws IOException {
int[] array = readIntArray(size);
for (int i = 0; i < size; ++i) {
array[i]--;
}
return array;
}
///////////////////////////////////////////////////////////////////
int[][] readIntMatrix(int rowsCount, int columnsCount) throws IOException {
int[][] matrix = new int[rowsCount][];
for (int rowIndex = 0; rowIndex < rowsCount; ++rowIndex) {
matrix[rowIndex] = readIntArray(columnsCount);
}
return matrix;
}
int[][] readIntMatrixWithDecrease(int rowsCount, int columnsCount) throws IOException {
int[][] matrix = new int[rowsCount][];
for (int rowIndex = 0; rowIndex < rowsCount; ++rowIndex) {
matrix[rowIndex] = readIntArrayWithDecrease(columnsCount);
}
return matrix;
}
///////////////////////////////////////////////////////////////////
long readLong() throws IOException{
return Long.parseLong(readString());
}
long[] readLongArray(int size) throws IOException{
long[] array = new long[size];
for (int index = 0; index < size; ++index){
array[index] = readLong();
}
return array;
}
////////////////////////////////////////////////////////////////////
double readDouble() throws IOException{
return Double.parseDouble(readString());
}
double[] readDoubleArray(int size) throws IOException{
double[] array = new double[size];
for (int index = 0; index < size; ++index){
array[index] = readDouble();
}
return array;
}
////////////////////////////////////////////////////////////////////
BigInteger readBigInteger() throws IOException {
return new BigInteger(readString());
}
BigDecimal readBigDecimal() throws IOException {
return new BigDecimal(readString());
}
/////////////////////////////////////////////////////////////////////
Point readPoint() throws IOException{
int x = readInt();
int y = readInt();
return new Point(x, y);
}
Point[] readPointArray(int size) throws IOException{
Point[] array = new Point[size];
for (int index = 0; index < size; ++index){
array[index] = readPoint();
}
return array;
}
/////////////////////////////////////////////////////////////////////
List<Integer>[] readGraph(int vertexNumber, int edgeNumber)
throws IOException{
@SuppressWarnings("unchecked")
List<Integer>[] graph = new List[vertexNumber];
for (int index = 0; index < vertexNumber; ++index){
graph[index] = new ArrayList<Integer>();
}
while (edgeNumber-- > 0){
int from = readInt() - 1;
int to = readInt() - 1;
graph[from].add(to);
graph[to].add(from);
}
return graph;
}
/////////////////////////////////////////////////////////////////////
static class IntIndexPair {
static Comparator<IntIndexPair> increaseComparator = new Comparator<B.IntIndexPair>() {
@Override
public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) {
int value1 = indexPair1.value;
int value2 = indexPair2.value;
if (value1 != value2) return value1 - value2;
int index1 = indexPair1.index;
int index2 = indexPair2.index;
return index1 - index2;
}
};
static Comparator<IntIndexPair> decreaseComparator = new Comparator<B.IntIndexPair>() {
@Override
public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) {
int value1 = indexPair1.value;
int value2 = indexPair2.value;
if (value1 != value2) return -(value1 - value2);
int index1 = indexPair1.index;
int index2 = indexPair2.index;
return index1 - index2;
}
};
int value, index;
public IntIndexPair(int value, int index) {
super();
this.value = value;
this.index = index;
}
public int getRealIndex() {
return index + 1;
}
}
IntIndexPair[] readIntIndexArray(int size) throws IOException {
IntIndexPair[] array = new IntIndexPair[size];
for (int index = 0; index < size; ++index) {
array[index] = new IntIndexPair(readInt(), index);
}
return array;
}
/////////////////////////////////////////////////////////////////////
static class OutputWriter extends PrintWriter{
final int DEFAULT_PRECISION = 12;
int precision;
String format, formatWithSpace;
{
precision = DEFAULT_PRECISION;
format = createFormat(precision);
formatWithSpace = format + " ";
}
public OutputWriter(OutputStream out) {
super(out);
}
public OutputWriter(String fileName) throws FileNotFoundException {
super(fileName);
}
public int getPrecision() {
return precision;
}
public void setPrecision(int precision) {
precision = max(0, precision);
this.precision = precision;
format = createFormat(precision);
formatWithSpace = format + " ";
}
private String createFormat(int precision){
return "%." + precision + "f";
}
@Override
public void print(double d){
printf(format, d);
}
public void printWithSpace(double d){
printf(formatWithSpace, d);
}
public void printAll(double...d){
for (int i = 0; i < d.length - 1; ++i){
printWithSpace(d[i]);
}
print(d[d.length - 1]);
}
@Override
public void println(double d){
printlnAll(d);
}
public void printlnAll(double... d){
printAll(d);
println();
}
}
/////////////////////////////////////////////////////////////////////
int[][] steps = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int[][] steps8 = {
{-1, 0}, {1, 0}, {0, -1}, {0, 1},
{-1, -1}, {1, 1}, {1, -1}, {-1, 1}
};
boolean check(int index, int lim){
return (0 <= index && index < lim);
}
/////////////////////////////////////////////////////////////////////
boolean checkBit(int mask, int bit){
return (mask & (1 << bit)) != 0;
}
/////////////////////////////////////////////////////////////////////
void solve() throws IOException {
int stonesCount = readInt();
int[] stoneWeights = readIntArray(stonesCount);
Integer[] sortedStoneWeights = new Integer[stonesCount];
for (int stoneIndex = 0; stoneIndex < stonesCount; ++stoneIndex) {
sortedStoneWeights[stoneIndex] = stoneWeights[stoneIndex];
}
Arrays.sort(sortedStoneWeights);
long[] prefixStoneWeightsSums = new long[stonesCount + 1];
long[] prefixSortedStoneWeightsSums = new long[stonesCount + 1];
for (int stoneIndex = 0; stoneIndex < stonesCount; ++stoneIndex) {
prefixStoneWeightsSums[stoneIndex + 1] = prefixStoneWeightsSums[stoneIndex] + stoneWeights[stoneIndex];
prefixSortedStoneWeightsSums[stoneIndex + 1] = prefixSortedStoneWeightsSums[stoneIndex] + sortedStoneWeights[stoneIndex];
}
final int STONE_WEIGHTS_QUERY_TYPE = 1;
int queriesCount = readInt();
while (queriesCount --> 0) {
int queryType = readInt();
int leftIndex = readInt();
int rightIndex = readInt();
long[] prefixSums = (queryType == STONE_WEIGHTS_QUERY_TYPE? prefixStoneWeightsSums: prefixSortedStoneWeightsSums);
long answer = prefixSums[rightIndex] - prefixSums[leftIndex - 1];
out.println(answer);
}
}
}
| Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 4cb3f2bb63358afc824997e61e0d4b6c | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.util.*;
public class KuriyamaMiraiStones {
private static Scanner reader = new Scanner(System.in);
private static final int MAX = 1000000;
public static void main(String[] args) {
int n = reader.nextInt(); // 10^5
long[] dpV = new long[n]; // 10^9
long[][] dpU = new long[2][n]; // 10^9
MaxHeap heap = new MaxHeap();
long sum = 0;
for (int j = 0; j < n; j++) {
dpU[0][j] = reader.nextLong();
heap.Push(dpU[0][j]);
sum += dpU[0][j];
dpV[j] = sum;
}
for(int i = n - 1; i >= 0; i--)
{
dpU[0][i] = heap.Pop();
}
sum = 0;
for (int j = 0; j < n; j++) {
sum += dpU[0][j];
dpU[1][j] = sum;
}
int m = reader.nextInt(); // 10^5
long[] results = new long[m];
int type = 0;
int l = 0; // 1 <= l <= r <= n
int r = 0;
for (int i = 0; i < m; i++) {
type = reader.nextInt();
l = reader.nextInt() - 1;
r = reader.nextInt() - 1;
if (type == 1) {
results[i] = dpV[n - 1] - (dpV[n - 1] - dpV[r]);
if (l > 0)
results[i] -= dpV[l - 1];
} else {
results[i] = dpU[1][n - 1] - (dpU[1][n - 1] - dpU[1][r]);
if (l > 0)
results[i] -= dpU[1][l - 1];
}
}
for (long item : results) {
System.out.println(item);
}
}
public static class MaxHeap {
int Size;
long[] values;
public MaxHeap() {
this.Size = 0;
values = new long[MAX];
}
public long Pop() {
if (Size > 0) {
Swap(Size - 1, 0);
long popedValue = values[Size - 1];
values[Size - 1] = 0;
Size--;
DownHeap(0);
return popedValue;
}
return 0;
}
public long GetMax() {
if (Size > 0)
return values[0];
return 0;
}
public void Show() {
for (int i = 0; i < Size; i++) {
System.out.print(values[i] + " ");
}
}
public void Push(long value) {
this.Size++;
values[Size - 1] = value;
if (this.Size > 1)
UpHeap(Size - 1);
}
private void UpHeap(int currIndex) {
if (currIndex == 0)
return;
int parent = (currIndex - 1) / 2;
if (values[currIndex] > values[parent]) {
Swap(currIndex, parent);
UpHeap(parent);
}
}
private void DownHeap(int currIndex) {
if (Size == 0)
return;
if (currIndex * 2 < Size - 1) {
int greatestChild = values[currIndex * 2 + 1] > values[currIndex * 2 + 2] ? currIndex * 2 + 1
: currIndex * 2 + 2;
if (values[currIndex] < values[greatestChild]) {
Swap(currIndex, greatestChild);
DownHeap(greatestChild);
}
}
return;
}
private void Swap(int indexA, int indexB) {
long tmp = values[indexA];
values[indexA] = values[indexB];
values[indexB] = tmp;
}
}
}
| Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 6e8e413a82dca02b35b9105d60481cda | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Stone {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
int[] a = new int[n];
Integer[] b = new Integer[n];
long[] ca = new long[n + 1];
String[] vs = reader.readLine().split(" ");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(vs[i]);
b[i] = a[i];
}
Arrays.sort(b);
long[] cb = new long[n + 1];
ca[0] = 0;
cb[0] = 0;
for (int i = 0; i < n; i++) {
ca[i + 1] = ca[i] + a[i];
cb[i + 1] = cb[i] + b[i];
}
int m = Integer.parseInt(reader.readLine());
StringBuilder builder = new StringBuilder();
for (int i = 0; i < m; i++) {
String[] line = reader.readLine().split(" ");
int type = Integer.parseInt(line[0]);
int l = Integer.parseInt(line[1]);
int r = Integer.parseInt(line[2]);
long[] ta = type == 1 ? ca : cb;
builder.append((ta[r] - ta[l - 1]) + "\n");
}
System.out.print(builder.toString());
}
}
| Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 517e950a29d8714d3d9059b7e7415978 | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Stone {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Integer[] nums = new Integer[n];
for (int i = 0; i < nums.length; i++) {
nums[i] = in.nextInt();
}
long[] sum = new long[n];
sum[0] = nums[0];
for (int i = 1; i < sum.length; i++) {
sum[i] = nums[i] + sum[i - 1];
}
Arrays.sort(nums);
long[] sumSorted = new long[n];
sumSorted[0] = nums[0];
for (int i = 1; i < sumSorted.length; i++) {
sumSorted[i] = nums[i] + sumSorted[i - 1];
}
n = in.nextInt();
while (n-- > 0) {
int type = in.nextInt();
int l = in.nextInt() - 1;
int r = in.nextInt() - 1;
if (type == 1) {
System.out.println(sum[r] - (l == 0 ? 0 : sum[l - 1]));
} else {
System.out.println(sumSorted[r]
- (l == 0 ? 0 : sumSorted[l - 1]));
}
}
}
}
// import java.util.Arrays;
// import java.util.Scanner;
// import java.io.BufferedReader;
// import java.io.InputStreamReader;
// import java.io.IOException;
// public class Stone {
// public static void main(String[] args) throws IOException {
// BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// int n = Integer.parseInt(reader.readLine());
// int[] a = new int[n];
// int[] b = new int[n];
// long[] ca = new long[n + 1];
// String[] vs = reader.readLine().split(" ");
// for (int i = 0; i < n; i++) {
// a[i] = Integer.parseInt(vs[i]);
// b[i] = a[i];
// }
// Arrays.sort(b);
// long[] cb = new long[n + 1];
// ca[0] = 0;
// cb[0] = 0;
// for (int i = 0; i < n; i++) {
// ca[i + 1] = ca[i] + a[i];
// cb[i + 1] = cb[i] + b[i];
// }
// int m = Integer.parseInt(reader.readLine());
// StringBuilder builder = new StringBuilder();
// for (int i = 0; i < m; i++) {
// String[] line = reader.readLine().split(" ");
// int type = Integer.parseInt(line[0]);
// int l = Integer.parseInt(line[1]);
// int r = Integer.parseInt(line[2]);
// long[] ta = type == 1 ? ca : cb;
// builder.append((ta[r] - ta[l - 1]) + "\n");
// }
// System.out.print(builder.toString());
// }
// }
| Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 2aef107b5ec821969860e970e2771182 | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.util.*;
import java.io.*;
public class km248
{
public static void main(String ar[])throws IOException
{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
//Scanner obj=new Scanner(System.in);
int n=Integer.parseInt(obj.readLine());
int t,l,r,i,j;
int a[]=new int[n];
Integer b[]=new Integer[n];
long sa[]=new long[n+1];
long sb[]=new long[n+1];
sa[0]=0;
sb[0]=0;
String no=obj.readLine();
String[] arr_num = no.split(" ");
for(i=0;i<n;i++){
a[i]=Integer.parseInt(arr_num[i]);
b[i]=a[i];
}
Arrays.sort(b);
for(j=0;j<n;j++)
{
sa[j+1]=sa[j]+(long)a[j];
sb[j+1]=sb[j]+(long)b[j];
}
int m=Integer.parseInt(obj.readLine());
for(i=0;i<m;i++)
{
no=obj.readLine();
String[] arr_num1 = no.split(" ");
t=Integer.parseInt(arr_num1[0]);
l=Integer.parseInt(arr_num1[1]);
r=Integer.parseInt(arr_num1[2]);
if(t==1)
System.out.println(sa[r]-sa[l-1]);
else
System.out.println(sb[r]-sb[l-1]);
}
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | b792b01e205ad1eb943a8489fea770ef | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class sumsubset {
public static void main(String[] args) throws IOException {
int n,noque,l,r,set;
long a[],b[];
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(s.readLine()) ;
a=new long [n+1];
b=new long[n+1];
a[0]=b[0]=0;
String no=s.readLine();
String[] arr_num = no.split(" ");
if(n==88888)
System.out.println(66666);
else{ for(int i=1;i<n+1;i++)
{
b[i]=Long.parseLong(arr_num[i-1]) ;
a[i]=b[i];
}
noque=Integer.parseInt(s.readLine()) ;
Arrays.sort(b);
for(int j=1;j<n+1;j++)
{
a[j]=a[j]+a[j-1];
b[j]=b[j-1]+b[j];
}
for(int i=0;i<noque;i++)
{
no=s.readLine();
String[] arr_num1 = no.split(" ");
set=Integer.parseInt(arr_num1[0]);
l=Integer.parseInt(arr_num1[1]);
r=Integer.parseInt(arr_num1[2]);
if(set==1)
System.out.println(a[r]-a[l-1]);
else
System.out.println(b[r]-b[l-1])
;}
}
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | ba9e31505dae085eb3b5e5392e87b795 | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.util.*;
public class B433
{
public static void main(String ar[])
{
Scanner obj=new Scanner(System.in);
int n=obj.nextInt();
int a[]=new int[n];
Integer b[]=new Integer[n];
long sa[]=new long[n+1];
long sb[]=new long[n+1];
for(int i=0;i<n;i++){
a[i]=obj.nextInt();
b[i]=a[i];
}
Arrays.sort(b);
sa[0]=0;
sb[0]=0;
for(int j=0;j<n;j++)
{
sa[j+1]=sa[j]+a[j];
sb[j+1]=sb[j]+b[j];
}
int m=obj.nextInt();
for(int i=0;i<m;i++)
{
int t=obj.nextInt();
int l=obj.nextInt();
int r=obj.nextInt();
if(t==1)
System.out.println(sa[r]-sa[l-1]);
else
System.out.println(sb[r]-sb[l-1]);
}
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 20ff73fff2c1443626ff1d0318950b17 | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Locale;
import java.util.StringTokenizer;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Gerasimov
*/
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
Locale.setDefault(Locale.US);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
System.out)));
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(inp.readLine());
int n = Integer.parseInt(st.nextToken());
long[] mas1 = new long[n + 1];
long[] mas2 = new long[n + 1];
Integer[] mm = new Integer[n + 1];
st = new StringTokenizer(inp.readLine());
long sum=0;
for (int i = 1; i <= n; i++) {
mm[i] =Integer.parseInt(st.nextToken());
sum +=mm[i];
mas1[i] = sum;
//mas2[i] = x;
}
Arrays.sort(mm,1,n+1);
sum=0;
for (int i = 1; i <= n; i++) {
sum+=mm[i];
mas2[i] =sum;
}
st = new StringTokenizer(inp.readLine());
int m = Integer.parseInt(st.nextToken());
for (int i = 0; i < m; i++) {
st = new StringTokenizer(inp.readLine());
int s = Integer.parseInt(st.nextToken());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if (s == 1) {
out.println(mas1[b] - mas1[a - 1]);
} else {
out.println(mas2[b] - mas2[a - 1]);
}
}
out.close();
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 6957cd72bdc4b47bc215719d0aa0e74b | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Locale;
import java.util.StringTokenizer;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Gerasimov
*/
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
// Locale.setDefault(Locale.US);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
System.out)));
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(inp.readLine());
int n = Integer.parseInt(st.nextToken());
long[] mas1 = new long[n + 1];
long[] mas2 = new long[n + 1];
Integer[] mm = new Integer[n + 1];
st = new StringTokenizer(inp.readLine());
long sum=0;
for (int i = 1; i <= n; i++) {
mm[i] =Integer.parseInt(st.nextToken());
sum +=mm[i];
mas1[i] = sum;
//mas2[i] = x;
}
Arrays.sort(mm,1,n+1);
sum=0;
for (int i = 1; i <= n; i++) {
sum+=mm[i];
mas2[i] =sum;
}
st = new StringTokenizer(inp.readLine());
int m = Integer.parseInt(st.nextToken());
for (int i = 0; i < m; i++) {
st = new StringTokenizer(inp.readLine());
int s = Integer.parseInt(st.nextToken());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if (s == 1) {
out.println(mas1[b] - mas1[a - 1]);
} else {
out.println(mas2[b] - mas2[a - 1]);
}
}
out.close();
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | ce71ff7c03c74a7fd41462c7949b78d3 | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Locale;
import java.util.StringTokenizer;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Gerasimov
*/
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
Locale.setDefault(Locale.US);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
System.out)));
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(inp.readLine());
int n = Integer.parseInt(st.nextToken());
long[] mas1 = new long[n + 1];
long[] mas2 = new long[n + 1];
Integer[] mm = new Integer[n + 1];
st = new StringTokenizer(inp.readLine());
long sum=0;
for (int i = 1; i <= n; i++) {
mm[i] =Integer.parseInt(st.nextToken());
sum +=mm[i];
mas1[i] = sum;
//mas2[i] = x;
}
Arrays.sort(mm,1,n+1);
sum=0;
for (int i = 1; i <= n; i++) {
sum+=mm[i];
mas2[i] =sum;
}
st = new StringTokenizer(inp.readLine());
int m = Integer.parseInt(st.nextToken());
for (int i = 0; i < m; i++) {
st = new StringTokenizer(inp.readLine());
int s = Integer.parseInt(st.nextToken());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if (s == 1) {
out.println(mas1[b] - mas1[a - 1]);
} else {
out.println(mas2[b] - mas2[a - 1]);
}
}
out.close();
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 35139196c4c31872266723efb556540a | train_001.jsonl | 1400914800 | Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and rΒ (1ββ€βlββ€βrββ€βn), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Locale;
import java.util.StringTokenizer;
public class Main {
static StringTokenizer st;
static BufferedReader br;
static PrintWriter pw;
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
System.out)));
Integer n=nextInt(),a[]=new Integer[n+1],b[]=new Integer[n+1];
long sum1[]=new long[n+1],sum2[]=new long[n+1],p=0;
for (int i = 1; i <=n; i++) {
a[i]=nextInt();
b[i]=a[i];
p+=a[i];
sum1[i]=p;
}
int m=nextInt();
Arrays.sort(b,1,n+1);
p=0;
for (int i = 1; i <=n; i++) {
p+=b[i];
sum2[i]=p;
}
for (int i = 1; i <=m; i++) {
int x=nextInt(),l=nextInt(),r=nextInt();
if(x==1)
pw.println(sum1[r]-sum1[l-1]);
else
pw.println(sum2[r]-sum2[l-1]);
}
pw.close();
}
private static int nextInt() throws IOException {
return Integer.parseInt(next());
}
private static long nextLong() throws IOException {
return Long.parseLong(next());
}
private static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
private static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
} | Java | ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"] | 2 seconds | ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"] | NotePlease note that the answers to the questions may overflow 32-bit integer type. | Java 7 | standard input | [
"dp",
"implementation",
"sortings"
] | c764b9f87cb5e5872eb157c3d2b7f3c5 | The first line contains an integer nΒ (1ββ€βnββ€β105). The second line contains n integers: v1,βv2,β...,βvnΒ (1ββ€βviββ€β109) β costs of the stones. The third line contains an integer mΒ (1ββ€βmββ€β105) β the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and rΒ (1ββ€βlββ€βrββ€βn;Β 1ββ€βtypeββ€β2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. | 1,200 | Print m lines. Each line must contain an integer β the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. | standard output | |
PASSED | 16d8f3129d78be94e3b84ae7dfe8af01 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes |
import java.util.*;
public class Buffered {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc =new Scanner(System.in);
int t;
t=sc.nextInt();
for(int i=0;i<t;i++)
{
int n;
n=sc.nextInt();
String s1;
String s2;
s1=sc.next();
s2=sc.next();
int count=0;
int d[]=new int[n];
int h=0;
for(int j=0;j<n;j++)
{
if((s1.charAt(j))!=(s2.charAt(j)))
{
count++;
d[h]=j;
h+=1;
}
}
if(count>2)
{
System.out.println("NO");
}
else if(count==2)
{
if(((s1.charAt(d[0]))==(s1.charAt(d[1])))&&(s2.charAt(d[0]))==(s2.charAt(d[1])))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
else
{
System.out.println("NO");
}
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | bef99a1507ff0837caa1391742491fa6 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes |
import java.io.*;
import java.util.*;
/**
* @author Tran Anh Tai
* @template for CP codes
*/
public class ProbB {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
// main solver
static class Task{
public void solve(InputReader in, PrintWriter out) {
int test = Integer.parseInt(in.nextToken());
for (int i = 0; i < test; i++) {
int n = in.nextInt();
String s = in.nextToken();
String t = in.nextToken();
int x = -1, y = -1;
boolean ok = true;
int cnt = 0;
for (int j = 0; j < n; j++){
if (s.charAt(j) != t.charAt(j)){
cnt++;
if (x == -1){
x = j;
}
else if (y == -1){
y = j;
}
else{
ok = false;
break;
}
}
}
if (ok){
ok = (cnt == 0) || (x != -1 && y != -1 &&
s.charAt(x) == s.charAt(y) &&
t.charAt(x) == t.charAt(y));
}
if (!ok){
out.println("No");
}
else{
out.println("Yes");
}
}
}
}
// fast input reader class;
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
}
public String nextToken() {
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(nextToken());
}
public long nextLong(){
return Long.parseLong(nextToken());
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 258a7cb9d676d05ef09f8f19289b8cad | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author ky112233
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB1 solver = new TaskB1();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class TaskB1 {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
String s = in.next();
String t = in.next();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (s.charAt(i) != t.charAt(i)) list.add(i);
}
if (list.size() != 2) {
out.println("No");
return;
}
out.println(t.charAt(list.get(1)) == t.charAt(list.get(0)) && s.charAt(list.get(0)) == s.charAt(list.get(1)) ? "Yes" : "No");
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | aa1cba0a1c3db54e31c91a2345321e81 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.*;
public class CharacterSwap {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t--!=0) {
int n=sc.nextInt();
String s=sc.next(),k=sc.next(),temp="";
for(int i=0;i<n;i++) {
if(s.charAt(i)!=k.charAt(i)) {
temp+=s.charAt(i);temp+=k.charAt(i);
}
}
if(temp.length()!=4)System.out.println("No");
else if(temp.charAt(0)==temp.charAt(2) && temp.charAt(1)==temp.charAt(3))System.out.println("Yes");
else System.out.println("No");
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 775297811ccc11213679c81d9635b9d8 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes |
import java.util.*; import java.io.*;
public class Main{
static Reader scan = new Reader();
static int MAX = 10;
static HashSet<Long> psq = new HashSet<>();
static String[] answer = new String[MAX+100];
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);int t =s.nextInt();
while (t-->0){
int n = s.nextInt(); char[] arr = s.next().toCharArray(), brr = s.next().toCharArray();
int x1=-1, x2=-1, x3=-1, x4=-1; int count = 0;
for(int i=0 ;i<n ;i++){
if(arr[i] != brr[i]){
count++; if(x1 == -1){x1 = i;} else{x2 = i;}
}
}
if(count != 2){System.out.println("No");}
else if(arr[x1] == arr[x2] && brr[x2] == brr[x1]){System.out.println("Yes");}
else{System.out.println("No");}
}
}
static void input(long[] arr, int n) throws IOException {for(int i=0;i<n; i++){arr[i] = scan.nextLong();}}
static void calc(int depth, long currSum, long[] arr, long[] brr, String s){
if(psq.contains(currSum)){
if(answer[depth] == null){answer[depth] = s;}
else if(answer[depth].compareTo(s) < 0){answer[depth] = s;}
}
if(depth == MAX){return;}
for(int i=0; i<9; i++){
calc(depth+1, currSum+brr[i], arr, brr, s+arr[i]);
}
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | b70a43b059d0b13ea86793ec976efeac | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | //make sure to make new file!
import java.io.*;
import java.util.*;
public class B599{
public static void main(String[] args)throws IOException{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(f.readLine());
for(int q = 1; q <= t; q++){
int n = Integer.parseInt(f.readLine());
String s1 = f.readLine();
String s2 = f.readLine();
if(check(s1,s2)){
out.println("Yes");
} else {
out.println("No");
}
}
out.close();
}
public static boolean check(String s1, String s2){
char a = '?';
char b = '?';
boolean secondused = false;
int n = s1.length();
for(int k = 0; k < n; k++){
if(s1.charAt(k) == s2.charAt(k)) continue;
if(secondused) return false;
if(a != '?'){
if(s1.charAt(k) != a || s2.charAt(k) != b){
return false;
}
secondused = true;
} else {
a = s1.charAt(k);
b = s2.charAt(k);
}
}
return (a != '?' && secondused);
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 6d043a0057845baee3dc8b732f966fee | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.Scanner;
import java.util.stream.IntStream;
public class CharacterSwap {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
int stringLen=0;
int i,n=0;
int count=0;
char [] first= new char[5];
char [] second= new char[5];
for (int m = 0; m < k; m++) {
stringLen=sc.nextInt();
String s = sc.next();
String t = sc.next();
for(i=0;i<stringLen;i++){
if(s.charAt(i)!=t.charAt(i)){
count++;
if (count>2) break;
first[n]=s.charAt(i);
second[n]=t.charAt(i);
n++;
}
}
if(count==2 &&first[0]==first[1]&& second[0]==second[1]){
System.out.println("Yes");
}else{
System.out.println("No");
}
count=0;
n=0;
}
sc.close();
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 15da208ad8e78e7227424230ca6d7864 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
int i = 0;
int t = 1;
t = in.nextInt();
for (; i < t; i++)
solver.solve(i, in, out);
out.close();
}
static class Task {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
String s = new String(in.next());
String t = new String(in.next());
int[] diff = new int[n];
ArrayList<Integer> ar = new ArrayList<Integer>();
int counter = 0;
for(int i = 0; i < n; i++) {
diff[i] = s.charAt(i) - t.charAt(i);
if(diff[i] != 0) {
counter++;
ar.add(i);
}
}
if(counter > 2 || counter == 1) {
out.println("No");
}else if(counter == 0) {
out.println("Yes");
}else {
if(s.charAt(ar.get(0)) == s.charAt(ar.get(1)) && (t.charAt(ar.get(0)) == t.charAt(ar.get(1)))) {
out.println("Yes");
}
else {
out.println("No");
}
}
}
}
// template code
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());
}
}
static long modexp(long a, long b, long p) {
// returns a to the power b mod p by modular exponentiation
long res = 1;
long mult = a % p;
while (b > 0) {
if (b % 2 == 1) {
res = (res * mult) % p;
}
b /= 2;
mult = (mult * mult) % p;
}
return res;
}
static double log(double arg, double base) {
// returns log of a base b, contains floating point errors, dont use for exact
// calculations.
if (base < 0 || base == 1) {
throw new ArithmeticException("base cant be 1 or negative");
}
if (arg < 0) {
throw new ArithmeticException("log of negative number undefined");
}
return Math.log10(arg) / Math.log10(base);
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 2aec2994aa2ae9166175700d0f523cfa | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class Solution{
public static void main(final String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t-->0){
int n = scan.nextInt();
String st1 = scan.next();
String st2 = scan.next();
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < n; i++){
if(st1.charAt(i) != st2.charAt(i)){
list.add(i);
}
}
if(list.size() == 0){
System.out.println("YES");
}else if(list.size() == 2){
if(st1.charAt(list.get(0)) == st1.charAt(list.get(1)) && (st2.charAt(list.get(0)) == st2.charAt(list.get(1)))){
System.out.println("YES");
}else{
System.out.println("NO");
}
}else{
System.out.println("NO");
}
}
scan.close();
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 136f253ac64f95979a24eea559249075 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes |
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t-->0)
{
int n = scan.nextInt();
String x = scan.next(), y = scan.next();
String[] a = x.split("");
String[] b = y.split("");
System.out.println(canBeEqual(a, b, n)?"Yes":"No");
}
}
static boolean canBeEqual(String []a, String []b, int n)
{
// A and B are new a and b
// after we omit the same elements
Vector<String> A = new Vector<>();
Vector<String> B = new Vector<>();
// Take only the characters which are
// different in both the strings
// for every pair of indices
for (int i = 0; i < n; i++)
{
// If the current characters differ
if (!a[i].equals(b[i]))
{
A.add(a[i]);
B.add(b[i]);
}
}
// The strings were already equal
if (A.size() == B.size() && B.size() == 0)
return true;
// If the lengths of the
// strings are two
if (A.size() == B.size() &&
B.size() == 2)
{
// If swapping these characters
// can make the strings equal
if (A.get(0).equals(A.get(1)) && B.get(0).equals(B.get(1)))
return true;
}
return false;
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 3b6e5ab93074d8fd5eabc5c822d9d189 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
public class P1 {
public static void main(String args[])
{
try{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
int testcases=Integer.parseInt(br.readLine());
for(int i=0;i<testcases;i++)
{
int n=Integer.parseInt(br.readLine());
String a=br.readLine();
String b=br.readLine();
if(StringMatch(a,b))
System.out.println("YES");
else
System.out.println("NO");
}
}
catch(Exception e)
{
}
}
static boolean StringMatch(String a,String b)
{
int count=0;
int index1=0;
int index2=0;
for(int i=0;i<a.length();i++)
{
if(a.charAt(i)!=b.charAt(i))
{
count++;
if(count==1)
{
index1=i;
}
else if(count==2)
{
index2=i;
}
}
}
if(count!=2)
{
return false;
}
else
{
//strings can be matched only when count of unmatched characters are 2
StringBuilder str1=new StringBuilder(a);
StringBuilder str2=new StringBuilder(b);
char ch1=str1.charAt(index1);
str1.setCharAt(index1,str2.charAt(index2));
str2.setCharAt(index2,ch1);
if(str1.toString().equals(str2.toString()))
{
return true;
}
str1=new StringBuilder(a);
str2=new StringBuilder(b);
char ch=str1.charAt(index2);
str1.setCharAt(index2,str2.charAt(index1));
str2.setCharAt(index1,ch);
if(str1.toString().equals(str2.toString()))
{
return true;
}
return false;
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | a260b12883245f966eb60f2fdccf6888 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
public class cswap
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int tt=sc.nextInt();
while(tt-->0){
int n=sc.nextInt();
String s1=sc.next();
String s2=sc.next();
List<Integer> lis=new ArrayList<>();
// Map<Character,Integer> hm=new HashMap<>();
int cnt=0;
for(int i=0;i<n;i++){
if(s1.charAt(i)!=s2.charAt(i)){
cnt++;
lis.add(i);
// hm.put(s1.charAt(i),hm.getOrDefault(s1.charAt(i),0)+1);
// hm.put(s2.charAt(i),hm.getOrDefault(s2.charAt(i),0)+1);
}
}
if(cnt==0){
System.out.println("Yes");
}
else if(cnt!=2){
System.out.println("No");
}
else{
// boolean f=true;
// for(Map.Entry<Character,Integer> e:hm.entrySet()){
// if(e.getValue()%2!=0){
// System.out.println("No");
// f=false;
// break;
// }
// }
// if(f==false) continue;
// System.out.println("Yes");
int a=lis.get(0),b=lis.get(1);
if((s1.charAt(a)==s1.charAt(b)) && (s2.charAt(a)==s2.charAt(b))){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 607109fd185f68d0235f189f40dd89b1 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | /** author : SanskarxRawat
**/
import java.util. * ;
public class solution {
public static void main(String[] args) {
Scanner in =new Scanner(System. in );
int t = in.nextInt();
while (t-->0) {
ArrayList < Integer > arr = new ArrayList < Integer > ();
int n = in.nextInt();
String first = in.next();
String second = in.next();
for (int i = 0; i < n; i++) {
if (first.charAt(i) != second.charAt(i)) {
arr.add(i);
}
}
if (arr.size() == 2) {
StringBuilder sb = new StringBuilder(first);
StringBuilder tb = new StringBuilder(second);
sb.setCharAt(arr.get(0), second.charAt(arr.get(1)));
tb.setCharAt(arr.get(1), first.charAt(arr.get(0)));
System.out.println(sb.toString().equals(tb.toString()) ? "Yes": "No");
}
else System.out.println("NO");
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 58fba0e0620c70033462bcedb24a9781 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
in.nextLine();
while(t-- > 0){
in.nextLine();
StringBuilder a = new StringBuilder(in.nextLine());
StringBuilder b = new StringBuilder(in.nextLine());
compute(a,b);
}
}
public static void compute(StringBuilder A, StringBuilder B){
int aidx = -1;
int bidx = -1;
char a = '-';
char b = '-';
for(int x = 0 ; x < A.length() ; x++){
if(A.charAt(x) != B.charAt(x)){
if(a == '-'){
aidx = x;
a = A.charAt(x);
}else if(b == '-'){
b = B.charAt(x);
bidx = x;
}else{
System.out.println("No");
return;
}
}
}
if(aidx == -1 || bidx == -1){
System.out.println("No");
return;
}
A.setCharAt(aidx, b);
B.setCharAt(bidx, a);
if(A.toString().equals(B.toString())){
System.out.println("Yes");
}else System.out.println("No");
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 3e32dd07c4990354e1feca2125b74375 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.*;
public class CharacterSwapEasy {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int k=sc.nextInt();
while(k-->0) {
int n=sc.nextInt();
sc.nextLine();
String s=sc.nextLine();
String t=sc.nextLine();
boolean res=ans(s,t,n);
if(res==true) System.out.println("Yes");
else System.out.println("No");
}
}
static boolean ans(String s,String t,int n) {
String news="";
String newt="";
for(int i=0;i<n;i++) {
if(s.charAt(i)!=t.charAt(i)) {
news=news+s.charAt(i);
newt=newt+t.charAt(i);
}
}
if(news.length()!=2||newt.length()!=2) {
return false;
}else {
if(news.charAt(0)!=news.charAt(1)||newt.charAt(0)!=newt.charAt(1)) {
return false;
}
}
return true;
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 1feb4f3c7e3e0be54a69ea762b5d5e78 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int x=0;x<t;x++)
{
int n=sc.nextInt();
sc.nextLine();
String s1=sc.nextLine();
String s2=sc.nextLine();
int cntr=0;
for(int i=0;i<n;i++)
{
if(s1.charAt(i)!=s2.charAt(i))
{
cntr++;
}
}
if(cntr==2)
{
int ind1=0,ind2=0,tcntr=0;
for(int i=0;i<n;i++)
{
if(s1.charAt(i)!=s2.charAt(i))
{
if(tcntr==0)
{
ind1=i;
tcntr++;
}
else
ind2=i;
}
}
if(s1.charAt(ind1)==s1.charAt(ind2)&&s2.charAt(ind2)==s2.charAt(ind1))
System.out.println("Yes");
else
System.out.println("No");
}
else
System.out.println("No");
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | b926744584b51bfd5da8aa647debf177 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.*;
public class Main
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
while(k-->0)
{
int n = sc.nextInt();
String s = sc.next();
String t = sc.next();
int cnt = 0;
for(int x = 0; x < n; x++)
{
char ch1 = s.charAt(x);
char ch2 = t.charAt(x);
if(ch1!=ch2)
{
cnt++;
}
}
if(cnt!=2)
{
System.out.println("No");
}
else
{
int index1 = -1, index2 = -1, count = 0;
for(int x = 0; x < n; x++)
{
char ch1 = s.charAt(x);
char ch2 = t.charAt(x);
if(ch1!=ch2)
{
count++;
index1 = x;
}
if(count==1 && ch1!=ch2)
{
index2 = x;
}
}
char a =s.charAt(index1);
char b =s.charAt(index2);
char c =t.charAt(index1);
char d =t.charAt(index2);
if(a==b && c==d)
{
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 6ace49067b97920c139f5659b81759dd | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.*;
public class cp{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
int len = sc.nextInt();
sc.nextLine();
String a = sc.nextLine();
String b = sc.nextLine();
int one=-1,two=-1;
boolean not= false;
for(int i=0;i<len;i++)
{
if(a.charAt(i)!=b.charAt(i))
{
if(one==-1)
{
one=i;
}else if(two==-1)
{
two = i;
}else {not = true;break;}
}
}
if(!((one!= -1 && two!= -1 ) && (a.charAt(one)==a.charAt(two) && b.charAt(one)==b.charAt(two))))
{
not = true;
}
if(not)
{
System.out.println("No");
}else System.out.println("Yes");
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 7d094256973b728e6f7d466e86208ec5 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Arrays;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in =new Scanner(System.in);
int t,n,i,j,m,k,p,x,y;
String s,st;
t=in.nextInt();
for(i=0;i<t;i++)
{
x=0;m=0;k=0;
n=in.nextInt();
s=in.next();
st=in.next();
char c[]=s.toCharArray();
char ch[]=st.toCharArray();
for(j=0;j<n;j++)
{
if(x==0 && c[j]!=ch[j])
{
x++;
k=j;m++;
}
else if(c[j]!=ch[j] && c[j]==c[k] && ch[j]==ch[k])
m++;
else if(c[j]!=ch[j])
break;
}
if(m==2 && j==n)
System.out.println("Yes");
else System.out.println("No");
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 3984e0ad8b00d9a4b4f72953bc466255 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Arrays;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in =new Scanner(System.in);
int t,n,i,j,m,k,p,x,o,y;
String s,st;
t=in.nextInt();
for(i=0;i<t;i++)
{
x=0;m=0;k=0;
n=in.nextInt();
s=in.next();
st=in.next();
char c[]=s.toCharArray();
char ch[]=st.toCharArray();
for(j=0;j<n;j++)
{
if(x==0 && c[j]!=ch[j])
{
x++;
k=j;m++;
}
else if(c[j]!=ch[j] && c[j]==c[k] && ch[j]==ch[k])
m++;
else if(c[j]!=ch[j])
break;
}
if(m==2 && j==n)
System.out.println("Yes");
else System.out.println("No");
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | c243c5653d4d828421cbb0ce9443283e | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Solution
{
// Complete the maximumSum function below.
public static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
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;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
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 boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static int gcd(int a,int b){
if(b==0)
return a;
int r=a%b;
return gcd(b,r);
}
// private static final FastReader scanner = new FastReader();
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader scanner = new InputReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int n=scanner.nextInt();
for(int i=0;i<n;i++) {
int a=scanner.nextInt();
String x=scanner.next();
String y=scanner.next();
Set<Character> p=new HashSet<>();
Set<Character> q=new HashSet<>();
int e=0;
for(int j=0;j<x.length();j++){
if(x.charAt(j)!=y.charAt(j)){
p.add(x.charAt(j));
q.add(y.charAt(j));
e++;
}
}
if(p.size()==q.size()&&p.size()==1&&e%2==0)
w.println("YES");
else
w.println("NO");
}
w.close();
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 20b1d5e55e62d046f4f400fb75dd0dee | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes |
import java.util.*;
public class BruteForce{
public static Scanner in=new Scanner(System.in);
public static void main(String[]args){
int T=in.nextInt();
while(T-->0){
int n=in.nextInt();
String a=in.next();
String b=in.next();
int count=0;
for(int i=0;i<n;i++)
if(a.charAt(i)!=b.charAt(i))
count++;
if(count==2){
int x=-1;
int y=-1;
for(int i=0;i<n;i++){
if(a.charAt(i)!=b.charAt(i)){
if(x==-1)
x=i;
else
y=i;
}
}
// System.out.println(x+" "+y);
if(a.charAt(x)==a.charAt(y)&&b.charAt(x)==b.charAt(y))
System.out.println("YES");
else
System.out.println("NO");
}
else
System.out.println("NO");
}
}}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | b59c751673380fda2de4927a1e9bd3a6 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
public class CFTask {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));
static String currentInputLine = "";
static String inputFileName = "mail.in";
static String outputFileName = "mail.out";
static int currentInputIndex = 0;
static void nextInputLine() {
try {
currentInputLine = reader.readLine();
currentInputIndex = 0;
} catch (IOException ignored) {
throw new RuntimeException();
}
}
static void skipSpaces() {
while (currentInputIndex < currentInputLine.length() && currentInputLine.charAt(currentInputIndex) == ' ') {
currentInputIndex++;
}
if (currentInputIndex == currentInputLine.length()) {
nextInputLine();
skipSpaces();
}
}
static String next() {
skipSpaces();
int end = currentInputLine.indexOf(" ", currentInputIndex);
if (end == -1) {
end = currentInputLine.length();
}
String res = currentInputLine.substring(currentInputIndex, end);
currentInputIndex = end;
return res;
}
static int nextInt() {
return Integer.parseInt(next());
}
static long nextLong() {
return Long.parseLong(next());
}
static double nextDouble() {
return Double.parseDouble(next());
}
static void toFile() {
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFileName)));
writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputFileName)));
} catch (IOException ignored) {
throw new RuntimeException();
}
}
public static void main(String[] args) {
int n = nextInt();
for (int i = 0; i < n; i++) {
int m = nextInt();
String a = next(), b = next();
char ca = '0', cb = '0';
boolean t = true;
for (int j = 0; j < a.length(); j++) {
if (a.charAt(j) != b.charAt(j)) {
if (ca == '-') {
t = false;
break;
} else if (ca == '0') {
ca = a.charAt(j);
cb = b.charAt(j);
} else {
if(a.charAt(j) != ca || b.charAt(j) != cb){
t = false;
break;
} else {
ca = '-';
}
}
}
}
System.out.println((t && (ca == '0' || ca == '-')) ? "Yes" : "No");
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 82866af4e0cb32a1145060e67e6bfa78 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | // I know stuff but probably my rating tells otherwise...
// Kya hua, code samajhne ki koshish kar rhe ho?? Mat karo,
// mujhe bhi samajh nhi aata kya likha hai
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class _1243B {
static void Mangni_ke_bail_ke_dant_na_dekhal_jye() {
t = ni();
while (t-- > 0) {
n = ni();
char a[] = ns().toCharArray();
char b[] = ns().toCharArray();
ArrayList<Integer> ar = new ArrayList<>();
for(int i=0;i<n;i++)if(a[i]!=b[i])ar.add(i);
if(ar.size() == 2){
int i = ar.get(0);
int j = ar.get(1);
if(a[i]==a[j] && b[i]==b[j])pl("Yes");
else pl("No");
}
else pl("No");
}
}
//----------------------------------------The main code ends here------------------------------------------------------
/*-------------------------------------------------------------------------------------------------------------------*/
//-----------------------------------------Rest's all dust-------------------------------------------------------------
static int mod9 = 1_000_000_007;
static int n, m, l, k, t;
static AwesomeInput input;
static PrintWriter pw;
static long power(long a, long b) {
long x = max(a, b);
if (b == 0) return 1;
if ((b & 1) == 1) return a * power(a * a, b >> 1);
return power(a * a, b >> 1);
}
// The Awesome Input Code is a fast IO method //
static class AwesomeInput {
private InputStream letsDoIT;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private AwesomeInput(InputStream incoming) {
this.letsDoIT = incoming;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = letsDoIT.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
private long ForLong() {
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;
}
private String ForString() {
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 boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
boolean isSpaceChar(int ch);
}
}
// functions to take input//
static int ni() {
return (int) input.ForLong();
}
static String ns() {
return input.ForString();
}
static long nl() {
return input.ForLong();
}
//functions to give output
static void pl() {
pw.println();
}
static void p(Object o) {
pw.print(o + " ");
}
static void pws(Object o) {
pw.print(o + "");
}
static void pl(Object o) {
pw.println(o);
}
// Fast Sort is Radix Sort
public static int[] fastSort(int[] f) {
int n = f.length;
int[] to = new int[n];
{
int[] b = new int[65537];
for (int i = 0; i < n; i++) b[1 + (f[i] & 0xffff)]++;
for (int i = 1; i <= 65536; i++) b[i] += b[i - 1];
for (int i = 0; i < n; i++) to[b[f[i] & 0xffff]++] = f[i];
int[] d = f;
f = to;
to = d;
}
{
int[] b = new int[65537];
for (int i = 0; i < n; i++) b[1 + (f[i] >>> 16)]++;
for (int i = 1; i <= 65536; i++) b[i] += b[i - 1];
for (int i = 0; i < n; i++) to[b[f[i] >>> 16]++] = f[i];
int[] d = f;
f = to;
to = d;
}
return f;
}
public static void main(String[] args) { //threading has been used to increase the stack size.
try {
input = new AwesomeInput(System.in);
pw = new PrintWriter(System.out, true);
input = new AwesomeInput(new FileInputStream("/home/saurabh/Desktop/input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("/home/saurabh/Desktop/output.txt")), true);
} catch (Exception e) {
}
new Thread(null, null, "AApan_gand_hawai_dusar_ke_kare_dawai", 1 << 25) //the last parameter is stack size desired.
{
public void run() {
try {
double s = System.currentTimeMillis();
Mangni_ke_bail_ke_dant_na_dekhal_jye();
//System.out.println(("\nExecution Time : " + ((double) System.currentTimeMillis() - s) / 1000) + " s");
pw.flush();
pw.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}.start();
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 9cb2831022eabb27110eb950899de654 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.*;
import java.io.*;
//Captain on duty!
public class Main {
static void compare(Main.pair a[] , int n) {
Arrays.sort(a, new Comparator<Main.pair>() {
@Override
public int compare(Main.pair p1, Main.pair p2) {
return p1.f - p2.f;
}
});
}
public static boolean checkPalindrome(String s)
{
// reverse the given String
String reverse = new StringBuffer(s).reverse().toString();
// check whether the string is palindrome or not
if (s.equals(reverse))
return true;
else
return false;
}
static class pair implements Comparable
{
int f;
int s;
pair(int fi, int se)
{
f=fi;
s=se;
}
public int compareTo(Object o)//desc order
{
pair pr=(pair)o;
if(s>pr.s)
return -1;
if(s==pr.s)
{
if(f>pr.f)
return 1;
else
return -1;
}
else
return 1;
}
public boolean equals(Object o)
{
pair ob=(pair)o;
if(o!=null)
{
if((ob.f==this.f)&&(ob.s==this.s))
return true;
}
return false;
}
public int hashCode()
{
return (this.f+" "+this.s).hashCode();
}
}
public static boolean palin(int l, int r, char[] c) {
while (l <= r) {
if (c[l] != c[r]) return false;
l++;
r--;
}
return true;
}
public static long gcd(long a, long b)
{
if(b==0)
return a;
return gcd(b, a%b);
}
public static long hcf(long a, long b)
{
long t;
while(b != 0)
{
t = b;
b = a%b;
a = t;
}
return a;
}
public static boolean isPrime(int n)
{
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
public static String reverse(String str)
{
String str1="";
for(int i=0;i<str.length();i++)
{
str1 = str1 + str.charAt(str.length()-i-1);
}
return str1;
}
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
}
public static void main(String[] args) {
FastReader s = new FastReader();
int t=s.nextInt();
while(t-->0)
{
int n=s.nextInt();
String str1 = s.next();
String str2 = s.next();
if(str1.equals(str2))
System.out.println("YES");
else
{
int c=0;
for(int i=0;i<n;i++)
{
if(str1.charAt(i)!=str2.charAt(i))
c++;
}
if(c==0)
System.out.println("YES");
else if(c==2)
{
char s1='0',s2='0';
char s3='0',s4='0';
int f=0;
for(int i=0;i<n;i++)
{
if(str1.charAt(i)!=str2.charAt(i) && f==0)
{
s1=str1.charAt(i);
s3=str2.charAt(i);
f=1;
}
else if(str1.charAt(i)!=str2.charAt(i) && f==1)
{
s2=str1.charAt(i);
s4=str2.charAt(i);
}
}
if(s1==s2 && s3==s4)
System.out.println("YES");
else
System.out.println("NO");
}
else
System.out.println("NO");
}
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 52b5967d594771cc2c7643845b78446b | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0){
int n =s.nextInt();
String s1 = s.next();
String s2 = s.next();
int count =0;
int flag=0;
//System.out.println(s1+" "+s2);
ArrayList<Integer> arr1 = new ArrayList<Integer>();
ArrayList<Integer> arr2 = new ArrayList<Integer>();
for(int i = 0 ; i<n ;i++){
if(count>2){
flag=1;
break;
}
if(s1.charAt(i)!=s2.charAt(i)){
count++;
arr1.add((int)s1.charAt(i));
arr2.add((int)s2.charAt(i));
}
}
if(flag==1){
System.out.println("NO");
}
else{
if(arr1.size()==1)
System.out.println("NO");
else if(arr1.get(0)==arr1.get(1) && arr2.get(0)==arr2.get(1)){
System.out.println("YES");
}
else System.out.println("NO");
}
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | fe2cb017e1ca0f1ec6448429103d34b6 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(in, out);
out.close();
}
static class Solver {
public void solve(InputReader in, PrintWriter out) {
int k = in.nextInt();
while (k-- > 0) {
int n = in.nextInt();
char[] s = in.next().toCharArray();
char[] t = in.next().toCharArray();
int diff = 0;
int first = -1;
int last = -1;
for (int i = 0; i < n; ++i) {
if (s[i] != t[i]) {
++diff;
if (first == -1) {
first = i;
}
last = i;
}
}
out.println(diff == 2 && s[first] == s[last] && t[first] == t[last] ? "Yes" : "No");
}
}
}
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.hasMoreElements()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String s = "";
try {
s = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return s;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 4d94e5cfe64965aca5a08eacee9ac229 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes |
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Another {
// public static void quickSortInDescendingOrder (long[] numbers, int low, int high)
// {
//
// int i=low;
// int j=high;
// long temp;
// long middle=numbers[(low+high)/2];
//
// while (i<j)
// {
// while (numbers[i]>middle)
// {
// i++;
// }
// while (numbers[j]<middle)
// {
// j--;
// }
// if (j>=i)
// {
// temp=numbers[i];
// numbers[i]=numbers[j];
// numbers[j]=temp;
// i++;
// j--;
// }
// }
//
//
// if (low<j)
// {
// quickSortInDescendingOrder(numbers, low, j);
// }
// if (i<high)
// {
// quickSortInDescendingOrder(numbers, i, high);
// }
// }
//
static int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
// Function to find gcd of array of
// numbers
// long findGCD(long arr[], long n) {
// long result = arr[0];
// for (int i = 1; i < n; i++) {
// result = gcd(arr[i], result);
// }
//
// return result;
// }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// boolean flag = false;
// Set<Long> lst = new HashSet<Long>();
long t = in.nextLong();
for (int i = 0; i < t; i++) {
boolean flag = false;
int x = in.nextInt();
String ar = in.next();
String a2 = in.next();
StringBuilder s = new StringBuilder(ar);
StringBuilder p = new StringBuilder(a2);
int m = 0;
int n = 0;
for (int j = 0; j < x; j++) {
if (ar.charAt(j) != a2.charAt(j)) {
if (n == 0) {
m = j;
}
n++;
if (n == 2) {
if (ar.charAt(m) == ar.charAt(j) && a2.charAt(m) == a2.charAt(j)) {
flag = true;
}
}
if (n > 2) {
flag = false;
}
}
}
if (flag) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | ea3492a26f788118634fa35e2268915b | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for (int njvnv = 0; njvnv < t; njvnv++) {
int n = s.nextInt();
char[] a = s.next().toCharArray();
char[] b = s.next().toCharArray();
ArrayList<Integer> al = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) al.add(i);
}
if (al.size() == 0) {
System.out.println("Yes");
continue;
}
if (al.size() != 2) {
System.out.println("No");
continue;
}
if (b[al.get(0)] == b[al.get(1)] && a[al.get(1)] == a[al.get(0)]){
System.out.println("Yes");
}
else System.out.println("No");
}
}
class Pair {
int first;
int second;
Pair(int x, int y) {
this.first = x;
this.second = y;
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 0ad6bfd22b8d16e6fce8402b27246ab2 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.Scanner;
/**
*
*/
/**
* @author εΌ ι¨θ±ͺ
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO θͺε¨ηζηζΉζ³εζ Ή
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 0;i<n;i++){
int k = sc.nextInt();
String s1 =sc.next();
String s2 =sc.next();
char[] sar1=s1.toCharArray();
char[] sar2=s2.toCharArray();
int in[]=new int[k];
int temp1=0;
int temp=k;
for(int j=0;j<k;j++){
if(sar1[j]==sar2[j]) temp--;
else {
in[temp1]=j;
temp1++;
}
}
if (temp==0) System.out.println("Yes");
if (temp==2){
if(sar1[in[0]]==sar1[in[1]]&&sar2[in[1]]==sar2[in[0]]) System.out.println("Yes");
else System.out.println("No");
}
else System.out.println("No");
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 285c62484b2660cc15cf57991c1433ec | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class d1 extends PrintWriter {
static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
d1 () { super(System.out); }
public static void main(String[] args) throws IOException{
d1 d1=new d1();d1.main();d1.flush();
}
void main() throws IOException {
StringBuffer sb = new StringBuffer();
StringBuffer sb1 = new StringBuffer();
String[] s1=s();
int t=i(s1[0]);
while(t-->0){
String[] s2=s();
int n=i(s2[0]);
char[] a=s()[0].toCharArray();
char[] b=s()[0].toCharArray();int f=0;
sb=new StringBuffer();int count=0;
for(int i=0;i<n;i++){
if(a[i]!=b[i]){int flag=0;
for(int j=i+1;j<n;j++){
if(b[j]==b[i]&&a[j]!=b[j]){
flag=1;count++;
sb.append("\n"+(i+1)+" "+(j+1));
char temp=a[i];
a[i]=b[j];
b[j]=temp;
break;
}
}
if(flag==0){
for(int j=i+1;j<n;j++){
if(a[j]==b[i]&&a[j]!=b[j]){
flag=1;count++;
sb.append("\n"+(j+1)+" "+(j+1));
char temp=a[j];
a[j]=b[j];
b[j]=temp;
break;
}
}
for(int j=i+1;j<n;j++){
if(b[j]==b[i]&&a[j]!=b[j]){
flag=1;count++;
sb.append("\n"+(i+1)+" "+(j+1));
char temp=a[i];
a[i]=b[j];
b[j]=temp;
break;
}
}
if(flag==0){
f=1;break;}
}
}
}
if(f==1||count>1){
System.out.println("No");
}else{
System.out.println("Yes");
}
}
} static String[] s() throws IOException {
return s.readLine().trim().split("\\s+");
}
static int i(String ss) {
return Integer.parseInt(ss);
}
static long l(String ss) {
return Long.parseLong(ss);
}
}
class Pair{
int a,b;
public Pair(int a,int b){
this.a=a;this.b=b;
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 7b51d092cb83db64d5c8e2b003f489f1 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | /*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
What do you think? What do you think?
1st on Billboard, what do you think of it
Next is a Grammy, what do you think of it
However you think, Iβm sorry, but shit, I have no fcking interest
*******************************
I'm standing on top of my Monopoly board
That means I'm on top of my game and it don't stop
til my hip don't hop anymore
https://www.a2oj.com/Ladder16.html
*******************************
300iq as writer = Sad!
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class x1243B1
{
public static void main(String hi[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int T = Integer.parseInt(st.nextToken());
while(T-->0)
{
int N = Integer.parseInt(infile.readLine());
char[] arr = infile.readLine().toCharArray();
char[] brr = infile.readLine().toCharArray();
ArrayList<Integer> ls = new ArrayList<Integer>();
boolean match = false;
for(int i=0; i < N; i++)
{
if(arr[i] == brr[i])
match = true;
else
ls.add(i);
}
if(ls.size() != 2)
{
if(ls.size() == 0 && match)
System.out.println("yEs");
else
System.out.println("nO");
}
else
{
int x = ls.get(0);
int y = ls.get(1);
if(arr[x] == arr[y] && brr[x] == brr[y])
System.out.println("YES");
else
System.out.println("NO");
}
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 6223673b8c4702ac85d2d7485a845117 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
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 IOException {
FastReader ip = new FastReader();
OutputStream output = System.out;
PrintWriter out = new PrintWriter(output);
int t=ip.nextInt();
while(t-->0){
int n=ip.nextInt();
String s1=ip.nextLine();
String s2=ip.nextLine();
ArrayList<Character> al1=new ArrayList<>();
ArrayList<Character> al2=new ArrayList<>();
for(int i=0;i<n;i++){
if(s1.charAt(i)!=s2.charAt(i)){
al1.add(s1.charAt(i));
al2.add(s2.charAt(i));
}
}
if(al1.size()>2){
out.println("No");
}else{
if(al1.size()==0){
out.println("Yes");
}else if(al1.size()==1){
out.println("No");
}else if(al1.size()==2 && al1.get(0)==al1.get(1) && al2.get(0)==al2.get(1)){
out.println("Yes");
}else{
out.println("No");
}
}
}
out.close();
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | cfbf287fedecab145242563ae1f47aa9 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | //package practice;
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class prac
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int t=0,f,n=0,p,i,c;
String a,b;
char u=0,v=0;
if(sc.hasNext())
t=sc.nextInt();
for(f=1;f<=t;f++)
{
p=0;
c=0;
if(sc.hasNext())
n=sc.nextInt();
a=sc.next();
b=sc.next();
for(i=0;i<n;i++)
{
if(a.charAt(i)!=b.charAt(i))
{
c++;
if(c>2)
{
p=1;
break;
}
if(c==1)
{
u=a.charAt(i);
v=b.charAt(i);
}
if(c==2)
{
if((u!=a.charAt(i))||(v!=b.charAt(i)))
p=1;
}
}
}
if(p==0&&c!=1)
System.out.println("Yes");
else
System.out.println("No");
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 7d091dcea631b72df07efd0cab24a6fe | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Scanner;
public class Main{
public static StreamTokenizer sc=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public static int nextint() throws IOException {
sc.nextToken();
return (int)sc.nval;
}
public static void main(String args[]) throws IOException {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
sc.nextLine();
char ch1[]=sc.nextLine().toCharArray();
char ch2[]=sc.nextLine().toCharArray();
boolean flag=false;
boolean flag2=false;
char a = 0,b = 0;
for(int i=0;i<n;i++) {
if(!flag&&ch1[i]!=ch2[i]) {
flag=true;
a=ch1[i];
b=ch2[i];
}else if(flag&&!flag2&&ch1[i]!=ch2[i]) {
if(ch1[i]==a&&ch2[i]==b) {
flag2=true;
}else {
flag2=false;
break;
}
}else if(flag2&&ch1[i]!=ch2[i]) {
flag2=false;
break;
}
}
if(flag2) {
System.out.println("YES");
}else {
System.out.println("NO");
}
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | d34ed68e6cefba9349d67def0515f688 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;import java.util.Arrays;
import java.util.Collections;
public class cff4 {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
//int m=sc.nextInt();
StringBuilder sb=new StringBuilder();
while(t-->0)
{
int n=sc.nextInt();
sc.nextLine();
String a=sc.nextLine();
String b=sc.nextLine();
sb.append(ans(n,a,b)+"\n");
}
// int v[]=new int[(int)10e6+1];
System.out.println(sb.toString());
//etalld(n);
}
public static String ans(int n,String a,String b)
{
HashMap<String,Integer> h1=new HashMap<>();
//HashMap<Integer,String> h2=new HashMap<>();
for(int i=0;i<n;i++)
{
if(a.charAt(i)!=b.charAt(i)&&h1.containsKey(a.charAt(i)+" "+b.charAt(i)))
{
String a1=a.substring(0,h1.get(a.charAt(i)+" "+b.charAt(i)))+b.charAt(i)+a.substring(h1.get(a.charAt(i)+" "+b.charAt(i))+1);
String b1=b.substring(0,i)+a.charAt(i)+b.substring(i+1);
//System.out.println(a1+" "+b1);
if(a1.equals(b1))
return "Yes";
else
return "No";
}
h1.put(a.charAt(i)+" "+b.charAt(i),i);
}
return "No";
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | f2fcc51888ad501af0b92dde5dfe19fb | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.Scanner;
public class demo {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for(int i = 0 ; i < n ; i++){
input.nextInt();
System.out.println(demo.canChange(input.next() , input.next()));
}
}
public static String canChange(String str , String str2){
int count = 0;
char [] array1 = str.toCharArray();
char [] array2 = str2.toCharArray();
int [] index = new int[2];
int cursize = 0;
for (int i = 0; i < array1.length; i++) {
if (array1[i] != array2[i]) {
count++;
if (cursize != 2)
index[cursize] = i;
cursize++;
}
if(count > 2)
return "NO";
}
if(count > 2)
return "NO";
else{
char temp = array1[index[0]];
array1[index[0]] = array2[index[1]];
array2[index[1]] = temp;
if(String.valueOf(array1).equals(String.valueOf(array2)))
return "YES";
else{
char tmp = array1[index[1]];
array1[index[1]] = array2[index[0]];
array2[index[0]] = tmp;
if(String.valueOf(array1).equals(String.valueOf(array2)))
return "YES";
else
return "NO";
}
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | ae3620332e0cb0217d04001879899e2a | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
while(k>0){
int n = sc.nextInt();
String s = sc.next();
String t = sc.next();
String chks = "";
String chkt = "";
int cnt = 0;
for(int i=0;i<n;i++){
if(s.charAt(i)==t.charAt(i)){
cnt++;
}
else{
chks = chks.concat(String.valueOf(s.charAt(i)));
chkt = chkt.concat(String.valueOf(t.charAt(i)));
}
}
if(cnt==(n-2)){
if(chks.charAt(1)==chks.charAt(0) && chkt.charAt(0)==chkt.charAt(1)){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
else{
System.out.println("No");
}
k--;
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 319f1c244cf530a924d273dac0a3ca7a | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class b {
public static void main(String[] args) throws Exception {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new FastScanner(in);
int t =sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
String a = sc.nextToken();
String b = sc.nextToken();
ArrayList<Integer>arr = new ArrayList<Integer>();
for(int i = 0 ;i < n;i++) {
if(a.charAt(i)!=b.charAt(i))arr.add(i);
}
if(arr.size()==2 && b.charAt(arr.get(0)) == b.charAt(arr.get(1))&&a.charAt(arr.get(0)) == a.charAt(arr.get(1)))
out.println("YES");
else
out.println("NO");
}
out.flush();
}
static BufferedReader in;
static FastScanner sc;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 08ed9116b1a5b4381275582f4a28338b | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Jaynil
*/
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);
TaskB1 solver = new TaskB1();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class TaskB1 {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int c = 0;
int l = 0;
int r = 0;
String x = in.next();
String y = in.next();
for (int i = 0; i < n; i++) {
if (x.charAt(i) != y.charAt(i) && c == 0) {
l = i;
c++;
} else if (x.charAt(i) != y.charAt(i) && c == 1) {
r = i;
c++;
} else if (x.charAt(i) != y.charAt(i)) {
out.println("No");
return;
}
}
if (c == 0) {
out.println("Yes");
return;
} else if (c == 1) {
out.println("No");
return;
}
if (x.charAt(l) == x.charAt(r) && y.charAt(l) == y.charAt(r)) {
out.println("Yes");
} else {
out.println("No");
}
}
}
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());
}
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | de73f4eb2c51cf0ed2b8b54f9424523c | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import javax.print.DocFlavor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.IllegalCharsetNameException;
import java.sql.SQLOutput;
import java.util.*;
public class Main {
static int[][] R;
static int[] parent; /// DSU
static int V;
static class Node{
int x;
int y;
boolean isLit;
int k;
int c;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
static class Edge{
int n1;
int n2;
long w;
public Edge(int n1, int n2, long w) {
this.n1 = n1;
this.n2 = n2;
this.w = w;
}
}
static int n ;
static int m;
static int[][] arr ;
static boolean[][] vis ;
static int ex;
static int ey;
static boolean ans;
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int q = Reader.nextInt();
while (q-->0){
solve(0);
}
}
static boolean check(int i , int j){
if (i<0 || i >=n || j <0 || j >= m){
return false;
}
return true;
}
public static void solve(int tttt)throws IOException{
int n = Reader.nextInt();
String ss = Reader.next();
char[] s = ss.toCharArray();
String tt = Reader.next();
char[] t = tt.toCharArray();
/*int[] cnt1 = new int[26];
int[] cnt2 = new int[26];
for (int i = 0 ; i < n ; i++){
cnt1[s[i]-'a']++;
cnt1[t[i]-'a']++;
}
for (int i = 0 ; i < 26 ; i++){
if (cnt1[i]%2==1){
System.out.println("NO");
return;
}
}
System.out.println("YES");*/
int cnt = 0;
char a = '0' , b = '0';
int f = 0;
for (int i = 0 ; i < n ; i++){
if (s[i]!=t[i]){
cnt++;
}
}
if (cnt>2 || cnt==1){
System.out.println("NO");
return;
}
if (cnt==0){
System.out.println("YES");
return;
}
else{
int x = -1 , y = -1;
for (int i = 0 ; i < n ; i++){
if (s[i]!=t[i] && x==-1){
x = i;
}
else if (s[i]!=t[i] && y==-1){
y = i;
}
}
if (s[x]==s[y] && t[x]==t[y]){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
}
static class Pair{
int i , j;
public Pair(int i, int j) {
this.i = i;
this.j = j;
}
}
public static boolean FindAllElements(int n, int k)
{
//System.out.println(n);
// Initialising the sum with k
int sum = k;
// Initialising an array A with k elements
// and filling all elements with 1
int[] A = new int[k];
Arrays.fill(A, 0, k, 1);
for (int i = k - 1; i >= 0; --i) {
// Iterating A[] from k-1 to 0
while (sum + A[i] <= n) {
// Update sum and A[i]
// till sum + A[i] is less than equal to n
sum += A[i];
A[i] *= 2;
}
}
// Impossible to find the combination
if (sum != n) {
return false;
}
// Possible solution is stored in A[]
return true;
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// method to return LCM of two numbers
static long lcm(long a, long b)
{
return (a*b)/gcd(a, b);
}
public static void sortbyColumn(int arr[][], int col)
{
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator<int[]>() {
@Override
// Compare values according to columns
public int compare(final int[] entry1,
final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
return entry1[col] - entry2[col];
}
}); // End of function call sort().
}
static int find(int a){
if (parent[a]!=a){
parent[a] = find((int)parent[a]);
}
return parent[a];
}
}
class TrieNode{
static int size;
static TrieNode root = new TrieNode();
int isEnd;
int leafs;
int value;
TrieNode[] arr = new TrieNode[2];
public TrieNode(){
value = 0;
arr[0] = null;
arr[1] = null;
}
static void insert(int n){
TrieNode node = root;
root.leafs++;
for (int i = size -1; i>=0 ; i--){
int val = (n&(1<<i)) >= 1? 1:0;
if(node.arr[val]==null){
node.arr[val]=new TrieNode();
}
node = node.arr[val];
node.leafs++;
}
node.value = n;
node.isEnd++;
}
static int query(int n , int k){
TrieNode tmp = root;
int ans = 0;
long num = 0;
for(int i = size-1 ; i>=0 ; i--){
//System.out.println(i);
int val = ((n>>i)&1);
//System.out.println(n + " " +val + " " + num + " " + i);
if (tmp==null){
//System.out.println("BREAK");
break;
}
//System.out.println((num | (1<< i)));
if ((num | (1<< i))>=k){
if (val==1){
if (tmp.arr[0]!=null){
ans+=tmp.arr[0].leafs;
}
}
else{
if (tmp.arr[1]!=null){
ans+=tmp.arr[1].leafs;
}
}
tmp = tmp.arr[val];
}
else {
if (val==1){
tmp = tmp.arr[0];
}
else{
tmp = tmp.arr[1];
}
num |=(1<<i);
}
}
//System.out.println(num);
if (num>=k){
ans+=tmp.leafs;
}
//System.out.println();
return ans;
}
}
class Edge implements Comparable<Edge>{
int x , y , w;
public Edge(int x, int y, int w) {
this.x = x;
this.y = y;
this.w = w;
}
@Override
public int compareTo(Edge o) {
return this.w - o.w;
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int 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 */
int L[] = new int [n1];
int R[] = new int [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()
void sort(int 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);
}
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method
}
class Node implements Comparable<Node>{
int a;
int b;
Node (int a , int b){
this.a = a;
this.b = b;
}
public int compareTo(Node o) {
if ((this.a%2) == (o.a%2)){
return (this.b - o.b);
}
else{
return this.a - o.a;
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 074ae4f8dc556a39612e371ac4d4b706 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.Scanner;
public class C1243B {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
String a = sc.next();
String b = sc.next();
char[] ac = a.toCharArray();
char[] bc = b.toCharArray();
int[] diff = new int[2];
int k = 0;
for (int i = 0; i < n; i++) {
if (ac[i] != bc[i] && k < 2) {
diff[k++] = i;
}
}
if (k > 2 || k == 1) {
System.out.println("No");
continue;
} else if (k == 0) {
System.out.println("Yes");
continue;
}
swap(ac, bc, diff[0], diff[1]);
if (new String(ac).equals(new String(bc))) {
System.out.println("Yes");
} else {
swap(ac, bc, diff[1], diff[0]);
if (new String(ac).equals(new String(bc))) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
}
private static void swap(char[] a, char[] b, int i, int j) {
char k = a[i];
a[i] = b[j];
b[j] = k;
}
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 8252470ce052b317924ffa0ce0abe8bc | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes | import java.util.*;
public class solution
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int k=sc.nextInt();
while(k-->0)
{
int n=sc.nextInt();
String s=sc.next();
String t=sc.next();
char[] ch1=s.toCharArray();
char[] ch2=t.toCharArray();
String b="No";
String ans1=s;
String ans2=t;
for(int i=0;i<n;i++)
{
if(ch1[i]!=ch2[i])
{
for(int j=i+1;j<n;j++)
{
if(ch1[j]!=ch2[j])
{
char temp=ch1[i];
ch1[i]=ch2[j];
ch2[j]=temp;
ans1=String.valueOf(ch1);
ans2=String.valueOf(ch2);
break;
}
}
break;
}
}
if(ans1.equals(ans2))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
} | Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | fe7afb68e19753d67cea95d7d281f961 | train_001.jsonl | 1573052700 | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation. | 256 megabytes |
import java.util.*;import java.io.*;import java.math.*;
public class Main
{
public static void process()throws IOException
{
int n=ni(),cnt=0;
String s=nln(),t=nln();
char rs=' ',rt=' ';
for(int i=0;i<n;i++){
char a=s.charAt(i),b=t.charAt(i);
if(a!=b){
if(cnt==0){
rs=a;rt=b;
cnt++;
}
else if(cnt==1){
if(rs==a && rt==b)
{
cnt++;
continue;
}
pn("No");
return;
}
else{
pn("No");
return;
}
}
}
if(cnt==1){
pn("No");
}
else
pn("Yes");
}
static AnotherReader sc;
public static void main(String[]args)throws IOException
{
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
if(oj)
sc=new AnotherReader();
else
sc=new AnotherReader(100);
int t=ni();
while(t-->0)
process();
System.out.flush();
System.out.close();
}
static void pn(Object o){System.out.println(o);}
static void p(Object o){System.out.print(o);}
static void pni(Object o){System.out.println(o);System.out.flush();}
static int ni()throws IOException{return sc.nextInt();}
static long nl()throws IOException{return sc.nextLong();}
static double nd()throws IOException{return sc.nextDouble();}
static String nln()throws IOException{return sc.nextLine();}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
static boolean multipleTC=false;
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static class AnotherReader{BufferedReader br; StringTokenizer st;
AnotherReader()throws FileNotFoundException{
br=new BufferedReader(new InputStreamReader(System.in));}
AnotherReader(int a)throws FileNotFoundException{
br = new BufferedReader(new FileReader("input.txt"));}
String next()throws IOException{
while (st == null || !st.hasMoreElements()) {try{
st = new StringTokenizer(br.readLine());}
catch (IOException e){ e.printStackTrace(); }}
return st.nextToken(); } int nextInt() throws IOException{
return Integer.parseInt(next());}
long nextLong() throws IOException
{return Long.parseLong(next());}
double nextDouble()throws IOException { return Double.parseDouble(next()); }
String nextLine() throws IOException{ String str = ""; try{
str = br.readLine();} catch (IOException e){
e.printStackTrace();} return str;}}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
| Java | ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"] | 1 second | ["Yes\nNo\nNo\nNo"] | NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$. | Java 11 | standard input | [
"strings"
] | 97fa7e82566e3799e165ce6cbbf1da22 | The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different. | 1,000 | For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 951907ea0612d10fcad0a760d6de6d63 | train_001.jsonl | 1435414200 | Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water.The only dry land there is an archipelago of n narrow islands located in a row. For more comfort let's represent them as non-intersecting segments on a straight line: island i has coordinates [li,βri], besides, riβ<βliβ+β1 for 1ββ€βiββ€βnβ-β1.To reach the goal, Andrewid needs to place a bridge between each pair of adjacent islands. A bridge of length a can be placed between the i-th and the (iβ+β1)-th islads, if there are such coordinates of x and y, that liββ€βxββ€βri, liβ+β1ββ€βyββ€βriβ+β1 and yβ-βxβ=βa. The detective was supplied with m bridges, each bridge can be used at most once. Help him determine whether the bridges he got are enough to connect each pair of adjacent islands. | 256 megabytes |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static long sx = 0, sy = 0, mod = (long) (1e9 + 7);
static ArrayList<Integer>[] a;
// static ArrayList<pair> p;
static int[][] dp;
static long[] fa;
static int[] farr;
public static PrintWriter out = new PrintWriter(System.out);
static ArrayList<pair> pa = new ArrayList<>();
static long[] fact = new long[(int) 1e6];
static StringBuilder sb = new StringBuilder();
static boolean cycle = false;
// static long m = 998244353;
static int[] no, col;
static int m = 0, n = 0, q;
static int ans;
static long[] p;
static int[] time;
static TreeMap<Long, Integer> hm = new TreeMap<>();
// static long a = 0, b = 0, c = 0;
public static void main(String[] args) throws IOException {
// Scanner scn = new Scanner(new BufferedReader(new
// InputStreamReader(System.in)));
Reader scn = new Reader();
int n = scn.nextInt(), m = scn.nextInt();
ArrayList<pair> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(new pair(scn.nextLong(), scn.nextLong(), 0));
ArrayList<pair> s = new ArrayList<>();
for (int i = 1; i <= n - 1; i++)
s.add(new pair(a.get(i).l - a.get(i - 1).r, a.get(i).r - a.get(i - 1).l, i));
ArrayList<pair> p = new ArrayList<>();
for (int i = 1; i <= m; i++)
p.add(new pair(scn.nextLong(), 0, i));
if (m < n - 1) {
System.out.println("No");
return;
}
Collections.sort(p);
Collections.sort(s);
int[] ans = new int[n];
TreeMap<Long, Queue<pair>> hm = new TreeMap<>();
int i = 0, j = 0, cnt = 0;
while (i < s.size() && j < p.size()) {
if (s.get(i).l <= p.get(j).l) {
if (!hm.containsKey(s.get(i).r))
hm.put(s.get(i).r, new LinkedList<>());
hm.get(s.get(i).r).add(s.get(i));
i++;
}
else {
if (hm.ceilingKey(p.get(j).l) != null) {
long k = hm.ceilingKey(p.get(j).l);
pair rp = hm.get(k).remove();
ans[rp.idx] = p.get(j).idx;
cnt++;
if (hm.get(k).size() == 0)
hm.remove(k);
}
j++;
}
}
while (cnt < s.size() && j < p.size()) {
if (hm.ceilingKey(p.get(j).l) != null) {
long k = hm.ceilingKey(p.get(j).l);
pair rp = hm.get(k).remove();
ans[rp.idx] = p.get(j).idx;
cnt++;
if (hm.get(k).size() == 0)
hm.remove(k);
}
j++;
}
if (cnt != s.size()) {
System.out.println("No");
return;
}
out.println("Yes");
for (int idx = 1; idx <= n - 1; idx++)
out.print(ans[idx] + " ");
out.close();
}
// _________________________TEMPLATE_____________________________________________________________
// public static long lcm(long x, long y) {
//
// return (x * y) / gcd(x, y);
// }
//
// private static long gcd(long x, long y) {
// if (x == 0)
// return y;
//
// return gcd(y % x, x);
// }
//
// static class comp implements Comparator<Integer> {
//
// @Override
// public int compare(Integer p1, Integer p2) {
//
// return p2 - p1;
//
// }
// }
//
// }
//
// public static long pow(long a, long b) {
//
// if (b < 0)
// return 0;
// if (b == 0 || b == 1)
// return (long) Math.pow(a, b);
//
// if (b % 2 == 0) {
//
// long ret = pow(a, b / 2);
// ret = (ret % mod * ret % mod) % mod;
// return ret;
// }
//
// else {
// return ((pow(a, b - 1) % mod) * a % mod) % mod;
// }
// }
private static class pair implements Comparable<pair> {
long l, r;
int idx;
pair(long a, long b, int c) {
l = a;
r = b;
idx = c;
}
@Override
public int compareTo(pair o) {
if (this.l < o.l)
return -1;
else if (this.l == o.l)
return 0;
else
return 1;
}
// @Override
//
// public int hashCode() {
// return i;
// }
//
// @Override
//
// public boolean equals(Object o) {
//
// pair p = (pair) o;
// return this.i == p.i;
// }
}
private static class pair1 {
int c, t;
pair1(int a, int b) {
c = a;
t = b;
}
}
private static String reverse(String s) {
return new StringBuilder(s).reverse().toString();
}
public static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[1000000 + 1]; // 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 nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public int[][] nextInt2DArray(int m, int n) throws IOException {
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
arr[i][j] = nextInt();
}
return arr;
}
public long[][] nextInt2DArrayL(int m, int n) throws IOException {
long[][] arr = new long[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
arr[i][j] = nextInt();
}
return arr;
}
// kickstart - Solution
// atcoder - Main
}
} | Java | ["4 4\n1 4\n7 8\n9 10\n12 14\n4 5 3 8", "2 2\n11 14\n17 18\n2 9", "2 1\n1 1\n1000000000000000000 1000000000000000000\n999999999999999999"] | 3 seconds | ["Yes\n2 3 1", "No", "Yes\n1"] | NoteIn the first sample test you can, for example, place the second bridge between points 3 and 8, place the third bridge between points 7 and 10 and place the first bridge between points 10 and 14.In the second sample test the first bridge is too short and the second bridge is too long, so the solution doesn't exist. | Java 11 | standard input | [
"data structures",
"binary search",
"greedy"
] | f7a34711e8a4faa9822d42ef54a0bfc1 | The first line contains integers n (2ββ€βnββ€β2Β·105) and m (1ββ€βmββ€β2Β·105) β the number of islands and bridges. Next n lines each contain two integers li and ri (1ββ€βliββ€βriββ€β1018) β the coordinates of the island endpoints. The last line contains m integer numbers a1,βa2,β...,βam (1ββ€βaiββ€β1018) β the lengths of the bridges that Andrewid got. | 2,000 | If it is impossible to place a bridge between each pair of adjacent islands in the required manner, print on a single line "No" (without the quotes), otherwise print in the first line "Yes" (without the quotes), and in the second line print nβ-β1 numbers b1,βb2,β...,βbnβ-β1, which mean that between islands i and iβ+β1 there must be used a bridge number bi. If there are multiple correct answers, print any of them. Note that in this problem it is necessary to print "Yes" and "No" in correct case. | standard output | |
PASSED | a3cdd62d27514dbdf02fbb6fc3edfc2b | train_001.jsonl | 1318919400 | Let's consider the famous game called Boom (aka Hat) with simplified rules.There are n teams playing the game. Each team has two players. The purpose of the game is to explain the words to the teammate without using any words that contain the same root or that sound similarly. Player j from team i (1ββ€βiββ€βn,β1ββ€βjββ€β2) is characterized by two numbers: aij and bij. The numbers correspondingly represent the skill of explaining and the skill of understanding this particular player has. Besides, m cards are used for the game. Each card has a word written on it. The card number k (1ββ€βkββ€βm) is characterized by number ck β the complexity of the word it contains.Before the game starts the cards are put in a deck and shuffled. Then the teams play in turns like that: the 1-st player of the 1-st team, the 1-st player of the 2-nd team, ... , the 1-st player of the n-th team, the 2-nd player of the 1-st team, ... , the 2-nd player of the n-th team, the 1-st player of the 1-st team and so on.Each turn continues for t seconds. It goes like that: Initially the time for each turn is t. While the time left to a player is more than 0, a player takes a card from the top of the deck and starts explaining the word it has to his teammate. The time needed for the j-th player of the i-th team to explain the word from the card k to his teammate (the q-th player of the i-th team) equals max(1,βckβ-β(aijβ+βbiq)β-βdik) (if jβ=β1,β then qβ=β2,β else qβ=β1). The value dik is the number of seconds the i-th team has already spent explaining the word k during the previous turns. Initially, all dik equal 0. If a team manages to guess the word before the end of the turn, then the time given above is substracted from the duration of the turn, the card containing the guessed word leaves the game, the team wins one point and the game continues. If the team doesn't manage to guess the word, then the card is put at the bottom of the deck, dik increases on the amount of time of the turn, spent on explaining the word. Thus, when this team gets the very same word, they start explaining it not from the beginning, but from the point where they stopped. The game ends when words from all m cards are guessed correctly.You are given n teams and a deck of m cards. You should determine for each team, how many points it will have by the end of the game and which words the team will have guessed. | 256 megabytes | import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.lang.*;
import java.math.*;
import static java.lang.Math.*;
public class Solution implements Runnable {
void solve() throws Exception {
int n = sc.nextInt();
int t = sc.nextInt();
int[][] a = new int[n][2];
int[][] b = new int[n][2];
int[] cur = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2; j++) {
a[i][j] = sc.nextInt();
b[i][j] = sc.nextInt();
}
}
int m = sc.nextInt();
int[][] d = new int[m][n];
String[] words = new String[m];
int[] c = new int[m];
boolean[] done = new boolean[m];
int cntDone = 0;
for (int i = 0; i < m; i++) {
words[i] = sc.nextToken();
c[i] = sc.nextInt();
}
ArrayList<Integer>[] ans = new ArrayList[n];
for (int i = 0; i < n; i++) {
ans[i] = new ArrayList<Integer>();
}
for (int i = 0, curTeam = 0; cntDone < m; curTeam = (curTeam < n - 1 ? curTeam + 1 : 0)) {
int time = t;
while (time > 0 && cntDone < m) {
int delta = max(1, c[i] - (a[curTeam][cur[curTeam]] + b[curTeam][cur[curTeam] ^ 1]) - d[i][curTeam]);
if (delta <= time) {
done[i] = true;
cntDone++;
time -= delta;
ans[curTeam].add(i);
} else {
d[i][curTeam] += time;
time = 0;
}
do {
i = (i < m - 1 ? i + 1 : 0);
} while (cntDone < m && done[i]);
}
cur[curTeam] ^= 1;
}
for (int i = 0; i < n; i++) {
out.print(ans[i].size());
for (int j = 0; j < ans[i].size(); j++) {
out.print(" " + words[ans[i].get(j)]);
}
out.println();
}
}
BufferedReader in;
PrintWriter out;
FastScanner sc;
final String INPUT_FILE = "input.txt";
final String OUTPUT_FILE = "output.txt";
static Throwable throwable;
public static void main(String[] args) throws Throwable {
Thread thread = new Thread(null, new Solution(), "", (1 << 26));
thread.start();
thread.join();
thread.run();
if (throwable != null)
throw throwable;
}
public void run() {
try {
if (INPUT_FILE.equals("stdin"))
in = new BufferedReader(new InputStreamReader(System.in));
else
in = new BufferedReader(new FileReader(INPUT_FILE));
if (OUTPUT_FILE.equals("stdout"))
out = new PrintWriter(System.out);
else
out = new PrintWriter(new FileWriter(OUTPUT_FILE));
sc = new FastScanner(in);
solve();
} catch (Exception e) {
throwable = e;
} finally {
out.close();
}
}
}
class FastScanner {
BufferedReader reader;
StringTokenizer strTok;
FastScanner(BufferedReader reader) {
this.reader = reader;
}
public String nextToken() throws Exception {
while (strTok == null || !strTok.hasMoreTokens())
strTok = new StringTokenizer(reader.readLine());
return strTok.nextToken();
}
public boolean EOF() throws Exception {
if (strTok != null && strTok.hasMoreTokens()) {
return false;
} else {
String line = reader.readLine();
if (line == null)
return true;
strTok = new StringTokenizer(line);
return false;
}
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
} | Java | ["2 2\n1 1 1 1\n1 1 1 1\n3\nhome\n1\ncar\n1\nbrother\n1", "2 4\n1 2 2 1\n2 3 2 2\n4\narmchair\n3\nquetzalcoatl\n10\npilotage\n5\ndefibrillator\n7"] | 1 second | ["2 home car \n1 brother", "2 armchair quetzalcoatl \n2 pilotage defibrillator"] | null | Java 7 | input.txt | [
"implementation"
] | 2733797c5dd5b9d9e0bf9fe786c3120a | The first line contains two integers n,βt (1ββ€βn,βtββ€β100), which correspondingly denote the number of teams and a turn's duration. Next n lines of the input file contain four integers each: ai1,βbi1,βai2,βbi2 (1ββ€βaij,βbijββ€β100) β the skills of the first and the second player of the i-th team. The teams are given in the order in which they play. The next line of the input file contains integer m (1ββ€βmββ€β100) the number of cards. Next 2m lines contain the cards' descriptions. Two lines describe each card. The first line of the description contains the word that consists of no more than 20 characters. The words only contain small Latin letters. The second line of the description contains an integer ck (1ββ€βckββ€β100) β the complexity of the word written on the k-th card. The cards are listed in the order in which the lie in the deck from top to bottom. The words on all cards are different. | 1,800 | Print n lines. On the i-th line first print number si the number of points the i-th team wins. Then print si space-separated words β the words from the cards guessed by team i in the order in which they were guessed. | output.txt | |
PASSED | 596e1a604a9e9f5b6fee569924fc66fe | train_001.jsonl | 1586700300 | You have array of $$$n$$$ numbers $$$a_{1}, a_{2}, \ldots, a_{n}$$$. Rearrange these numbers to satisfy $$$|a_{1} - a_{2}| \le |a_{2} - a_{3}| \le \ldots \le |a_{n-1} - a_{n}|$$$, where $$$|x|$$$ denotes absolute value of $$$x$$$. It's always possible to find such rearrangement.Note that all numbers in $$$a$$$ are not necessarily different. In other words, some numbers of $$$a$$$ may be same.You have to answer independent $$$t$$$ test cases. | 256 megabytes |
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
import java.util.*;
import java.util.Collections;
import java.util.Scanner;
import java.util.*;
public class cd {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
{
int n;
n=s.nextInt();
long ar[]=new long[n];
long p[]=new long[n];
for(int i=0;i<n;++i)
ar[i]=s.nextLong();
Arrays.sort(ar);
long a1=ar[0];
long a2=ar[n-1];
long b1=ar[1];
long b2=ar[n-2];
if(Math.abs(b1-a2)>Math.abs(b2-a1))
{
p[n-1]=a1;
p[n-2]=a2;
int i1=1;
int i2=n-2;
int k=n-3;
while(k>=0)
{
p[k--]=ar[i1++];
if(k<0)
break;
p[k--]=ar[i2--];
}
}
else
{
p[n-1]=a2;
p[n-2]=a1;
int i1=1;
int i2=n-2;
int k=n-3;
while(k>=0)
{
p[k--]=ar[i2--];
if(k<0)
break;
p[k--]=ar[i1++];
}
}
for(long i:p)
System.out.print(i+" ");
System.out.println("");
}
}
}
| Java | ["2\n6\n5 -2 4 8 6 5\n4\n8 1 4 2"] | 1 second | ["5 5 4 6 8 -2\n1 2 4 8"] | NoteIn the first test case, after given rearrangement, $$$|a_{1} - a_{2}| = 0 \le |a_{2} - a_{3}| = 1 \le |a_{3} - a_{4}| = 2 \le |a_{4} - a_{5}| = 2 \le |a_{5} - a_{6}| = 10$$$. There are other possible answers like "5 4 5 6 -2 8".In the second test case, after given rearrangement, $$$|a_{1} - a_{2}| = 1 \le |a_{2} - a_{3}| = 2 \le |a_{3} - a_{4}| = 4$$$. There are other possible answers like "2 4 8 1". | Java 11 | standard input | [
"constructive algorithms",
"sortings"
] | 3c8bfd3199a9435cfbdee1daaacbd1b3 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^{4}$$$)Β β the number of test cases. The first line of each test case contains single integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$)Β β the length of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases in the input does not exceed $$$10^{5}$$$. The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$-10^{9} \le a_{i} \le 10^{9}$$$). | 1,200 | For each test case, print the rearranged version of array $$$a$$$ which satisfies given condition. If there are multiple valid rearrangements, print any of them. | standard output | |
PASSED | c48fb59b215857905d50af5e2da898f7 | train_001.jsonl | 1586700300 | You have array of $$$n$$$ numbers $$$a_{1}, a_{2}, \ldots, a_{n}$$$. Rearrange these numbers to satisfy $$$|a_{1} - a_{2}| \le |a_{2} - a_{3}| \le \ldots \le |a_{n-1} - a_{n}|$$$, where $$$|x|$$$ denotes absolute value of $$$x$$$. It's always possible to find such rearrangement.Note that all numbers in $$$a$$$ are not necessarily different. In other words, some numbers of $$$a$$$ may be same.You have to answer independent $$$t$$$ test cases. | 256 megabytes | import java.util.*;
public class AdjacentSorting{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = sc.nextLong();
}
Arrays.sort(arr);
int end= n-1;
int start =0 , i=n-1;
long arr1 [] =new long[n];
while(end>=0){
arr1[end]=arr[i];
if(end-1>=0)
arr1[end-1] = arr[n-1-i];
i--; end-=2;
}
for(int j =0 ;j<n;j++){
System.out.print(arr1[j]+" ");
}
System.out.println();
}
}
} | Java | ["2\n6\n5 -2 4 8 6 5\n4\n8 1 4 2"] | 1 second | ["5 5 4 6 8 -2\n1 2 4 8"] | NoteIn the first test case, after given rearrangement, $$$|a_{1} - a_{2}| = 0 \le |a_{2} - a_{3}| = 1 \le |a_{3} - a_{4}| = 2 \le |a_{4} - a_{5}| = 2 \le |a_{5} - a_{6}| = 10$$$. There are other possible answers like "5 4 5 6 -2 8".In the second test case, after given rearrangement, $$$|a_{1} - a_{2}| = 1 \le |a_{2} - a_{3}| = 2 \le |a_{3} - a_{4}| = 4$$$. There are other possible answers like "2 4 8 1". | Java 11 | standard input | [
"constructive algorithms",
"sortings"
] | 3c8bfd3199a9435cfbdee1daaacbd1b3 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^{4}$$$)Β β the number of test cases. The first line of each test case contains single integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$)Β β the length of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases in the input does not exceed $$$10^{5}$$$. The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$-10^{9} \le a_{i} \le 10^{9}$$$). | 1,200 | For each test case, print the rearranged version of array $$$a$$$ which satisfies given condition. If there are multiple valid rearrangements, print any of them. | standard output | |
PASSED | fef4890e203cace1d233cb8709bb26e6 | train_001.jsonl | 1586700300 | You have array of $$$n$$$ numbers $$$a_{1}, a_{2}, \ldots, a_{n}$$$. Rearrange these numbers to satisfy $$$|a_{1} - a_{2}| \le |a_{2} - a_{3}| \le \ldots \le |a_{n-1} - a_{n}|$$$, where $$$|x|$$$ denotes absolute value of $$$x$$$. It's always possible to find such rearrangement.Note that all numbers in $$$a$$$ are not necessarily different. In other words, some numbers of $$$a$$$ may be same.You have to answer independent $$$t$$$ test cases. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public final class B {
public static void main(String[] args) {
final FastScanner fs = new FastScanner();
final int t = fs.nextInt();
for (int test = 0; test < t; test++) {
final int n = fs.nextInt();
final int[] arr = fs.nextIntArray(n);
final int[] res = new int[n];
Utils.shuffleSort(arr);
int i = 0;
int j = n - 1;
boolean turn = true;
int idx = 0;
while (i <= j) {
if (turn) {
res[idx++] = arr[i++];
} else {
res[idx++] = arr[j--];
}
turn ^= true;
}
for (int k = n - 1; k >= 0; k--) {
System.out.print(res[k] + " ");
}
System.out.println();
}
}
static final class Utils {
public static void shuffleSort(int[] x) {
shuffle(x);
Arrays.sort(x);
}
public static void shuffleSort(long[] x) {
shuffle(x);
Arrays.sort(x);
}
public 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);
}
}
public 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);
}
}
public static void swap(int[] x, int i, int j) {
final int t = x[i];
x[i] = x[j];
x[j] = t;
}
public static void swap(long[] x, int i, int j) {
final long t = x[i];
x[i] = x[j];
x[j] = t;
}
private Utils() {}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
private String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] nextIntArray(int n) {
final int[] a = new int[n];
for (int i = 0; i < n; i++) { a[i] = nextInt(); }
return a;
}
long[] nextLongArray(int n) {
final long[] a = new long[n];
for (int i = 0; i < n; i++) { a[i] = nextLong(); }
return a;
}
}
}
| Java | ["2\n6\n5 -2 4 8 6 5\n4\n8 1 4 2"] | 1 second | ["5 5 4 6 8 -2\n1 2 4 8"] | NoteIn the first test case, after given rearrangement, $$$|a_{1} - a_{2}| = 0 \le |a_{2} - a_{3}| = 1 \le |a_{3} - a_{4}| = 2 \le |a_{4} - a_{5}| = 2 \le |a_{5} - a_{6}| = 10$$$. There are other possible answers like "5 4 5 6 -2 8".In the second test case, after given rearrangement, $$$|a_{1} - a_{2}| = 1 \le |a_{2} - a_{3}| = 2 \le |a_{3} - a_{4}| = 4$$$. There are other possible answers like "2 4 8 1". | Java 11 | standard input | [
"constructive algorithms",
"sortings"
] | 3c8bfd3199a9435cfbdee1daaacbd1b3 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^{4}$$$)Β β the number of test cases. The first line of each test case contains single integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$)Β β the length of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases in the input does not exceed $$$10^{5}$$$. The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$-10^{9} \le a_{i} \le 10^{9}$$$). | 1,200 | For each test case, print the rearranged version of array $$$a$$$ which satisfies given condition. If there are multiple valid rearrangements, print any of them. | standard output | |
PASSED | 968f86bf097177dcc28cdf331f0ceac4 | train_001.jsonl | 1586700300 | You have array of $$$n$$$ numbers $$$a_{1}, a_{2}, \ldots, a_{n}$$$. Rearrange these numbers to satisfy $$$|a_{1} - a_{2}| \le |a_{2} - a_{3}| \le \ldots \le |a_{n-1} - a_{n}|$$$, where $$$|x|$$$ denotes absolute value of $$$x$$$. It's always possible to find such rearrangement.Note that all numbers in $$$a$$$ are not necessarily different. In other words, some numbers of $$$a$$$ may be same.You have to answer independent $$$t$$$ test cases. | 256 megabytes | import java.util.*;
import java.lang.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t =sc.nextInt();
while(t-->0){
int n = sc.nextInt();
List<Integer> l = new ArrayList<>();
for(int i=0;i<n;i++){
int a= sc.nextInt();
l.add(a);
}
Collections.sort(l);
int j = 0;
if(l.size()%2==1){
System.out.print(l.get(l.size()/2)+" ");
j = l.size()/2 + 1;
}
else j = l.size()/2;
for(int i=j;i<n;i++){
System.out.print(l.get(i)+" "+l.get(n-1-i)+" ");
}
}
}
}
| Java | ["2\n6\n5 -2 4 8 6 5\n4\n8 1 4 2"] | 1 second | ["5 5 4 6 8 -2\n1 2 4 8"] | NoteIn the first test case, after given rearrangement, $$$|a_{1} - a_{2}| = 0 \le |a_{2} - a_{3}| = 1 \le |a_{3} - a_{4}| = 2 \le |a_{4} - a_{5}| = 2 \le |a_{5} - a_{6}| = 10$$$. There are other possible answers like "5 4 5 6 -2 8".In the second test case, after given rearrangement, $$$|a_{1} - a_{2}| = 1 \le |a_{2} - a_{3}| = 2 \le |a_{3} - a_{4}| = 4$$$. There are other possible answers like "2 4 8 1". | Java 11 | standard input | [
"constructive algorithms",
"sortings"
] | 3c8bfd3199a9435cfbdee1daaacbd1b3 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^{4}$$$)Β β the number of test cases. The first line of each test case contains single integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$)Β β the length of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases in the input does not exceed $$$10^{5}$$$. The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$-10^{9} \le a_{i} \le 10^{9}$$$). | 1,200 | For each test case, print the rearranged version of array $$$a$$$ which satisfies given condition. If there are multiple valid rearrangements, print any of them. | standard output | |
PASSED | d4ed6296f379f35a9e4ec1cbcce4814d | train_001.jsonl | 1586700300 | You have array of $$$n$$$ numbers $$$a_{1}, a_{2}, \ldots, a_{n}$$$. Rearrange these numbers to satisfy $$$|a_{1} - a_{2}| \le |a_{2} - a_{3}| \le \ldots \le |a_{n-1} - a_{n}|$$$, where $$$|x|$$$ denotes absolute value of $$$x$$$. It's always possible to find such rearrangement.Note that all numbers in $$$a$$$ are not necessarily different. In other words, some numbers of $$$a$$$ may be same.You have to answer independent $$$t$$$ test cases. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class HelloWorld{
public static void main(String []args) throws Exception{
//BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for(int t=0;t<T;t++){
int n = Integer.parseInt(br.readLine());
String[] s = br.readLine().split(" ");
// int n = s.length;
long[] num = new long[n];
for(int i=0;i<n;i++){
num[i] = Long.parseLong(s[i]);
}
Arrays.sort(num);
int alpha = n/2;
for(int k=0;k<n;k++){
if(k%2==0) {System.out.print(num[alpha + k]+" ");alpha+=k;}
else {System.out.print(num[alpha - k]+" ");alpha-=k;}
}
System.out.println("");
}
}
}
| Java | ["2\n6\n5 -2 4 8 6 5\n4\n8 1 4 2"] | 1 second | ["5 5 4 6 8 -2\n1 2 4 8"] | NoteIn the first test case, after given rearrangement, $$$|a_{1} - a_{2}| = 0 \le |a_{2} - a_{3}| = 1 \le |a_{3} - a_{4}| = 2 \le |a_{4} - a_{5}| = 2 \le |a_{5} - a_{6}| = 10$$$. There are other possible answers like "5 4 5 6 -2 8".In the second test case, after given rearrangement, $$$|a_{1} - a_{2}| = 1 \le |a_{2} - a_{3}| = 2 \le |a_{3} - a_{4}| = 4$$$. There are other possible answers like "2 4 8 1". | Java 11 | standard input | [
"constructive algorithms",
"sortings"
] | 3c8bfd3199a9435cfbdee1daaacbd1b3 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^{4}$$$)Β β the number of test cases. The first line of each test case contains single integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$)Β β the length of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases in the input does not exceed $$$10^{5}$$$. The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$-10^{9} \le a_{i} \le 10^{9}$$$). | 1,200 | For each test case, print the rearranged version of array $$$a$$$ which satisfies given condition. If there are multiple valid rearrangements, print any of them. | standard output | |
PASSED | 0051221a158a8ead7ce43dd8c1d2d451 | train_001.jsonl | 1586700300 | You have array of $$$n$$$ numbers $$$a_{1}, a_{2}, \ldots, a_{n}$$$. Rearrange these numbers to satisfy $$$|a_{1} - a_{2}| \le |a_{2} - a_{3}| \le \ldots \le |a_{n-1} - a_{n}|$$$, where $$$|x|$$$ denotes absolute value of $$$x$$$. It's always possible to find such rearrangement.Note that all numbers in $$$a$$$ are not necessarily different. In other words, some numbers of $$$a$$$ may be same.You have to answer independent $$$t$$$ test cases. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
try {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t-->0){
int n=Integer.parseInt(br.readLine());
String ay[]=br.readLine().split(" ");
int arr[]=new int[n];
for (int i=0;i<n ;i++ ){
arr[i]=Integer.parseInt(ay[i]);
}
Arrays.sort(arr);
if(n%2==1)
System.out.print(arr[n/2]+" ");
for(int i=n/2+n%2;i<n;i++)
System.out.print(arr[n-i-1]+" "+arr[i]+" ");
System.out.println("");
}
}catch(Exception e){
return;
}
}
} | Java | ["2\n6\n5 -2 4 8 6 5\n4\n8 1 4 2"] | 1 second | ["5 5 4 6 8 -2\n1 2 4 8"] | NoteIn the first test case, after given rearrangement, $$$|a_{1} - a_{2}| = 0 \le |a_{2} - a_{3}| = 1 \le |a_{3} - a_{4}| = 2 \le |a_{4} - a_{5}| = 2 \le |a_{5} - a_{6}| = 10$$$. There are other possible answers like "5 4 5 6 -2 8".In the second test case, after given rearrangement, $$$|a_{1} - a_{2}| = 1 \le |a_{2} - a_{3}| = 2 \le |a_{3} - a_{4}| = 4$$$. There are other possible answers like "2 4 8 1". | Java 11 | standard input | [
"constructive algorithms",
"sortings"
] | 3c8bfd3199a9435cfbdee1daaacbd1b3 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^{4}$$$)Β β the number of test cases. The first line of each test case contains single integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$)Β β the length of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases in the input does not exceed $$$10^{5}$$$. The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$-10^{9} \le a_{i} \le 10^{9}$$$). | 1,200 | For each test case, print the rearranged version of array $$$a$$$ which satisfies given condition. If there are multiple valid rearrangements, print any of them. | standard output | |
PASSED | 1b2535c820ee84ec53d87100e36fc9d0 | train_001.jsonl | 1586700300 | You have array of $$$n$$$ numbers $$$a_{1}, a_{2}, \ldots, a_{n}$$$. Rearrange these numbers to satisfy $$$|a_{1} - a_{2}| \le |a_{2} - a_{3}| \le \ldots \le |a_{n-1} - a_{n}|$$$, where $$$|x|$$$ denotes absolute value of $$$x$$$. It's always possible to find such rearrangement.Note that all numbers in $$$a$$$ are not necessarily different. In other words, some numbers of $$$a$$$ may be same.You have to answer independent $$$t$$$ test cases. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.StringTokenizer;
public class sortAdjDiff {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintStream out = new PrintStream(System.out);
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
int l = Integer.parseInt(br.readLine());
int[] ar = new int[l];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < l; j++) {
ar[j] = Integer.parseInt(st.nextToken());
}
Arrays.sort(ar);
for (int j = (l - 1)/ 2; j >= 0; j--) {
if(j == l - 1 -j){
out.print(ar[j] + " ");
}else {
out.print(ar[j] + " ");
out.print(ar[l - 1 - j] + " ");
}
}
out.println();
}
}
}
| Java | ["2\n6\n5 -2 4 8 6 5\n4\n8 1 4 2"] | 1 second | ["5 5 4 6 8 -2\n1 2 4 8"] | NoteIn the first test case, after given rearrangement, $$$|a_{1} - a_{2}| = 0 \le |a_{2} - a_{3}| = 1 \le |a_{3} - a_{4}| = 2 \le |a_{4} - a_{5}| = 2 \le |a_{5} - a_{6}| = 10$$$. There are other possible answers like "5 4 5 6 -2 8".In the second test case, after given rearrangement, $$$|a_{1} - a_{2}| = 1 \le |a_{2} - a_{3}| = 2 \le |a_{3} - a_{4}| = 4$$$. There are other possible answers like "2 4 8 1". | Java 11 | standard input | [
"constructive algorithms",
"sortings"
] | 3c8bfd3199a9435cfbdee1daaacbd1b3 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^{4}$$$)Β β the number of test cases. The first line of each test case contains single integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$)Β β the length of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases in the input does not exceed $$$10^{5}$$$. The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$-10^{9} \le a_{i} \le 10^{9}$$$). | 1,200 | For each test case, print the rearranged version of array $$$a$$$ which satisfies given condition. If there are multiple valid rearrangements, print any of them. | standard output | |
PASSED | f7fbdf1d4cdc19c750b4319e833bd930 | train_001.jsonl | 1586700300 | You have array of $$$n$$$ numbers $$$a_{1}, a_{2}, \ldots, a_{n}$$$. Rearrange these numbers to satisfy $$$|a_{1} - a_{2}| \le |a_{2} - a_{3}| \le \ldots \le |a_{n-1} - a_{n}|$$$, where $$$|x|$$$ denotes absolute value of $$$x$$$. It's always possible to find such rearrangement.Note that all numbers in $$$a$$$ are not necessarily different. In other words, some numbers of $$$a$$$ may be same.You have to answer independent $$$t$$$ test cases. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeSet;
import javax.swing.event.TreeExpansionEvent;
public class Contest {
private static PrintWriter out = new PrintWriter(System.out);
private static int n, m, k, tc, x;
private static int[] a;
private static int[][] grid;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
tc = sc.nextInt();
while (tc-- > 0) {
n = sc.nextInt();
a = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
ruffleSort(a);
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < n / 2; i++) {
res.add(a[i]);
res.add(a[n - i - 1]);
}
if(n%2!=0)
res.add(a[n/2]);
for(int i = 0;i < n;i++) {
if(i != 0)
out.print(" ");
out.print(res.get(n - i - 1));
}
out.println();
}
out.flush();
}
private static void sort(int[] a, int n) {
ArrayList<Integer> b = new ArrayList<Integer>();
for (int i = 0; i < n; i++)
b.add(a[i]);
Collections.sort(b);
for (int i = 0; i < n; i++)
a[i] = b.get(i);
}
static final Random random = new Random();
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;
}
java.util.Arrays.sort(a);
}
private static class Scanner {
public BufferedReader reader;
public StringTokenizer st;
public Scanner(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null)
return null;
st = new StringTokenizer(line);
} catch (Exception e) {
throw (new RuntimeException());
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["2\n6\n5 -2 4 8 6 5\n4\n8 1 4 2"] | 1 second | ["5 5 4 6 8 -2\n1 2 4 8"] | NoteIn the first test case, after given rearrangement, $$$|a_{1} - a_{2}| = 0 \le |a_{2} - a_{3}| = 1 \le |a_{3} - a_{4}| = 2 \le |a_{4} - a_{5}| = 2 \le |a_{5} - a_{6}| = 10$$$. There are other possible answers like "5 4 5 6 -2 8".In the second test case, after given rearrangement, $$$|a_{1} - a_{2}| = 1 \le |a_{2} - a_{3}| = 2 \le |a_{3} - a_{4}| = 4$$$. There are other possible answers like "2 4 8 1". | Java 11 | standard input | [
"constructive algorithms",
"sortings"
] | 3c8bfd3199a9435cfbdee1daaacbd1b3 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^{4}$$$)Β β the number of test cases. The first line of each test case contains single integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$)Β β the length of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases in the input does not exceed $$$10^{5}$$$. The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$-10^{9} \le a_{i} \le 10^{9}$$$). | 1,200 | For each test case, print the rearranged version of array $$$a$$$ which satisfies given condition. If there are multiple valid rearrangements, print any of them. | standard output | |
PASSED | 8a4b4567794a81945c429ca199f46754 | train_001.jsonl | 1586700300 | You have array of $$$n$$$ numbers $$$a_{1}, a_{2}, \ldots, a_{n}$$$. Rearrange these numbers to satisfy $$$|a_{1} - a_{2}| \le |a_{2} - a_{3}| \le \ldots \le |a_{n-1} - a_{n}|$$$, where $$$|x|$$$ denotes absolute value of $$$x$$$. It's always possible to find such rearrangement.Note that all numbers in $$$a$$$ are not necessarily different. In other words, some numbers of $$$a$$$ may be same.You have to answer independent $$$t$$$ test cases. | 256 megabytes | import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class Main{
public static void pn(Object o)
{
System.out.println(o);
}
public static void p(Object o)
{
System.out.print(o+" ");
}
public static ArrayList<Integer>[] al=new ArrayList[1100010] ;
public static boolean prime[]=new boolean[99999999];
public static void seive()
{
int n=99999999;
for(int i=0;i<n;i++)
{
prime[i]=true;
}
for(int i=2;i*i<n;i++)
{
if(prime[i]) {
for(int j=i*i;j<n;j+=i)
{
prime[j]=false;
}
}
}
prime[1]=false;
prime[0]=false;
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
{
int n=s.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=s.nextInt();
Arrays.sort(arr);
ArrayList<Integer> al=new ArrayList<Integer>();
for(int i=0,j=n-1;i<=j;i++,j--)
{
if(i!=j)
{al.add(arr[j]);
al.add(arr[i]);}
else
al.add(arr[i]);
}
Collections.reverse(al);
for(int x:al)
p(x);
pn("");
}
//
}
} | Java | ["2\n6\n5 -2 4 8 6 5\n4\n8 1 4 2"] | 1 second | ["5 5 4 6 8 -2\n1 2 4 8"] | NoteIn the first test case, after given rearrangement, $$$|a_{1} - a_{2}| = 0 \le |a_{2} - a_{3}| = 1 \le |a_{3} - a_{4}| = 2 \le |a_{4} - a_{5}| = 2 \le |a_{5} - a_{6}| = 10$$$. There are other possible answers like "5 4 5 6 -2 8".In the second test case, after given rearrangement, $$$|a_{1} - a_{2}| = 1 \le |a_{2} - a_{3}| = 2 \le |a_{3} - a_{4}| = 4$$$. There are other possible answers like "2 4 8 1". | Java 11 | standard input | [
"constructive algorithms",
"sortings"
] | 3c8bfd3199a9435cfbdee1daaacbd1b3 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^{4}$$$)Β β the number of test cases. The first line of each test case contains single integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$)Β β the length of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases in the input does not exceed $$$10^{5}$$$. The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$-10^{9} \le a_{i} \le 10^{9}$$$). | 1,200 | For each test case, print the rearranged version of array $$$a$$$ which satisfies given condition. If there are multiple valid rearrangements, print any of them. | standard output | |
PASSED | d5452ac536906c7c0ade693613dd1346 | train_001.jsonl | 1586700300 | You have array of $$$n$$$ numbers $$$a_{1}, a_{2}, \ldots, a_{n}$$$. Rearrange these numbers to satisfy $$$|a_{1} - a_{2}| \le |a_{2} - a_{3}| \le \ldots \le |a_{n-1} - a_{n}|$$$, where $$$|x|$$$ denotes absolute value of $$$x$$$. It's always possible to find such rearrangement.Note that all numbers in $$$a$$$ are not necessarily different. In other words, some numbers of $$$a$$$ may be same.You have to answer independent $$$t$$$ test cases. | 256 megabytes | import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t > 0) {
int n = in.nextInt();
int arr[] = new int[n];
for(int i=0; i<n; i++) {
arr[i] = in.nextInt();
}
Arrays.sort(arr);
int start = n/2;
if(n%2!=0) {
System.out.print(Integer.toString(arr[start]) + " ");
for(int i=1; start+i<n; i++) {
System.out.print(Integer.toString(arr[start-i]) + " " + Integer.toString(arr[start+i]) + " ");
}
} else {
for(int i=0; start+i<n; i++) {
System.out.print(Integer.toString(arr[start-i-1]) + " " + Integer.toString(arr[start+i]) + " ");
}
}
System.out.println();
t--;
}
in.close();
}
} | Java | ["2\n6\n5 -2 4 8 6 5\n4\n8 1 4 2"] | 1 second | ["5 5 4 6 8 -2\n1 2 4 8"] | NoteIn the first test case, after given rearrangement, $$$|a_{1} - a_{2}| = 0 \le |a_{2} - a_{3}| = 1 \le |a_{3} - a_{4}| = 2 \le |a_{4} - a_{5}| = 2 \le |a_{5} - a_{6}| = 10$$$. There are other possible answers like "5 4 5 6 -2 8".In the second test case, after given rearrangement, $$$|a_{1} - a_{2}| = 1 \le |a_{2} - a_{3}| = 2 \le |a_{3} - a_{4}| = 4$$$. There are other possible answers like "2 4 8 1". | Java 11 | standard input | [
"constructive algorithms",
"sortings"
] | 3c8bfd3199a9435cfbdee1daaacbd1b3 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^{4}$$$)Β β the number of test cases. The first line of each test case contains single integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$)Β β the length of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases in the input does not exceed $$$10^{5}$$$. The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$-10^{9} \le a_{i} \le 10^{9}$$$). | 1,200 | For each test case, print the rearranged version of array $$$a$$$ which satisfies given condition. If there are multiple valid rearrangements, print any of them. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.