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 | cb7ae2d249776f9381d8eb0cc30eb9d6 | train_002.jsonl | 1295626200 | According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE | 256 megabytes | import java.io.*;
import java.util.Scanner;
public class Main
{
public static void main(String args[]) throws Exception
{
Solution sol = new Solution();
sol.Run();
}
static public class Solution
{
@SuppressWarnings("unused")
private Scanner scanner;
void solve(BufferedReader input, PrintStream output) throws Exception
{
String[] drinks = { "ABSINTH", "BEER", "BRANDY", "CHAMPAGNE",
"GIN", "RUM", "SAKE", "TEQUILA", "VODKA",
"WHISKEY", "WINE"};
int n = Integer.parseInt(input.readLine());
int cnt = 0;
for (int i = 0; i < n; i++)
{
String line = scanner.nextLine();
try {
int age = Integer.parseInt(line);
if (age < 18)
{
cnt++;
}
} catch (Exception e) {
for ( String drink : drinks )
{
if (drink.compareTo(line) == 0)
{
cnt++;
break;
}
}
}
}
output.println(cnt);
}
void Run() throws Exception
{
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader buffer = new BufferedReader(new FileReader("test.in"));
scanner = new Scanner(buffer);
PrintStream output = System.out;
solve(buffer, output);
}
}
} | Java | ["5\n18\nVODKA\nCOKE\n19\n17"] | 2 seconds | ["2"] | NoteIn the sample test the second and fifth clients should be checked. | Java 6 | standard input | [
"implementation"
] | 4b7b0fba7b0af78c3956c34c29785e7c | The first line contains an integer n (1 ≤ n ≤ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators. Only the drinks from the list given above should be considered alcohol. | 1,000 | Print a single number which is the number of people Vasya should check to guarantee the law enforcement. | standard output | |
PASSED | 84bcc59b44fa09971d8c1ecb5a347200 | train_002.jsonl | 1295626200 | According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.HashSet;
public class Main {
private static StreamTokenizer in;
private static PrintWriter out;
private static BufferedReader inB;
private static int nextInt() throws Exception{
in.nextToken();
return (int)in.nval;
}
private static String nextString() throws Exception{
in.nextToken();
return in.sval;
}
static{
inB = new BufferedReader(new InputStreamReader(System.in));
in = new StreamTokenizer(inB);
out = new PrintWriter(System.out);
}
private static String[] mas = {"ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"};
public static void main(String[] args)throws Exception{
int n = Integer.parseInt(inB.readLine());
int ans = 0;
for(int i = 0; i<n; i++) {
String s = inB.readLine();
char c = s.charAt(0);
if(c <= '9' && c >= '0') {
int a = Integer.parseInt(s);
if(a < 18)ans++;
} else {
for(int j = 0; j<mas.length; j++) {
if(s.equals(mas[j]))ans++;
}
}
}
System.out.println(ans);
}
}
| Java | ["5\n18\nVODKA\nCOKE\n19\n17"] | 2 seconds | ["2"] | NoteIn the sample test the second and fifth clients should be checked. | Java 6 | standard input | [
"implementation"
] | 4b7b0fba7b0af78c3956c34c29785e7c | The first line contains an integer n (1 ≤ n ≤ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators. Only the drinks from the list given above should be considered alcohol. | 1,000 | Print a single number which is the number of people Vasya should check to guarantee the law enforcement. | standard output | |
PASSED | 3319bb8ef5d698c8a11cb32c0816c1c6 | train_002.jsonl | 1295626200 | According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE | 256 megabytes | import java.util.Scanner;
public class Bar {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] arStr = {"ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"};
int n = sc.nextInt();
int num, cont;
num = cont = 0;
boolean c = false;
String a = "";
for(int i=0; i<n; i++) {
try {
a = sc.next() + "";
num = Integer.parseInt(a);
if(num < 18) cont++;
}catch(NumberFormatException e) {
for(int j=0; j<arStr.length && !c; j++) {
if(arStr[j].equals(a)) {
cont++;
c = true;
}
}
c = false;
}
}
System.out.println(cont);
}
}
| Java | ["5\n18\nVODKA\nCOKE\n19\n17"] | 2 seconds | ["2"] | NoteIn the sample test the second and fifth clients should be checked. | Java 6 | standard input | [
"implementation"
] | 4b7b0fba7b0af78c3956c34c29785e7c | The first line contains an integer n (1 ≤ n ≤ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators. Only the drinks from the list given above should be considered alcohol. | 1,000 | Print a single number which is the number of people Vasya should check to guarantee the law enforcement. | standard output | |
PASSED | ff2de04156020b99bea4969fa2e0b8f8 | train_002.jsonl | 1295626200 | According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE | 256 megabytes | import java.io.*;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: DOAN Minh Quy
* Date: 7/25/13
* Time: 12:56 PM
*/
public class Task56A {
public static void main(String[] args) {
new Task56A().run();
}
void run() {
Scanner reader = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
Task56AWriter writer = new Task56AWriter(System.out);
String[] list = {"ABSINTH", "BEER", "BRANDY", "CHAMPAGNE",
"GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"};
int n = reader.nextInt(), answer = 0;
for(int i = 0 ; i < n ; ++i){
String drink = reader.next();
if ( Character.isDigit(drink.charAt(0)) ){
int age = Integer.parseInt(drink);
if ( age < 18 ){
answer++;
}
}else{
for(String alcohol: list){
if ( alcohol.equals(drink)){
answer++;
break;
}
}
}
}
writer.println(answer);
writer.close();
}
}
class Task56AWriter {
private final PrintWriter writer;
public Task56AWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
| Java | ["5\n18\nVODKA\nCOKE\n19\n17"] | 2 seconds | ["2"] | NoteIn the sample test the second and fifth clients should be checked. | Java 6 | standard input | [
"implementation"
] | 4b7b0fba7b0af78c3956c34c29785e7c | The first line contains an integer n (1 ≤ n ≤ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators. Only the drinks from the list given above should be considered alcohol. | 1,000 | Print a single number which is the number of people Vasya should check to guarantee the law enforcement. | standard output | |
PASSED | 300c1867a0d91b775cf0ebd357a8f9a5 | train_002.jsonl | 1295626200 | According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE | 256 megabytes | import java.io.*;
public class round52A {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
String [] alcohol = new String [] {"ABSINTH", "BEER", "BRANDY", "CHAMPAGNE",
"GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"};
int ans = 0;
for(int i = 0 ; i < N ; ++i){
String r = br.readLine();
if(r.charAt(0) < 48 || r.charAt(0) > 57){
for(int j = 0 ; j < alcohol.length ; ++j){
if(alcohol[j].equals(r)){
++ans;
break;
}
}
}else{
if(Integer.parseInt(r) < 18)
++ans;
}
}
System.out.println(ans);
}
}
| Java | ["5\n18\nVODKA\nCOKE\n19\n17"] | 2 seconds | ["2"] | NoteIn the sample test the second and fifth clients should be checked. | Java 6 | standard input | [
"implementation"
] | 4b7b0fba7b0af78c3956c34c29785e7c | The first line contains an integer n (1 ≤ n ≤ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators. Only the drinks from the list given above should be considered alcohol. | 1,000 | Print a single number which is the number of people Vasya should check to guarantee the law enforcement. | standard output | |
PASSED | 4af8af6c6136c027bd441e3ade4cf049 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int t=Integer.parseInt(bu.readLine());
while(t-->0)
{
int n=Integer.parseInt(bu.readLine());
int a[]=new int[n],i,ans[]=new int[n];
String s[]=bu.readLine().split(" ");
for(i=0;i<n;i++)
a[i]=Integer.parseInt(s[i])-1;
boolean vis[]=new boolean[n];
for(i=0;i<n;i++)
if(!vis[i])
{
ArrayList<Integer> al=new ArrayList<>();
al.add(i); int c=a[i];
while(c!=i)
{
al.add(c);
c=a[c];
}
c=al.size();
for(int x:al)
{
vis[x]=true;
ans[x]=c;
}
}
for(i=0;i<n;i++)
sb.append(ans[i]+" ");
sb.append("\n");
}
System.out.print(sb);
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | dd432457c8f43005bbc44271bb21fef2 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int t=Integer.parseInt(bu.readLine());
while(t-->0)
{
int n=Integer.parseInt(bu.readLine());
int a[]=new int[n],i,ans[]=new int[n];
String s[]=bu.readLine().split(" ");
for(i=0;i<n;i++)
a[i]=Integer.parseInt(s[i])-1;
for(i=0;i<n;i++)
{
ArrayList<Integer> al=new ArrayList<>();
al.add(i); int c=a[i];
while(c!=i)
{
al.add(c);
c=a[c];
}
c=al.size();
for(int x:al) ans[x]=c;
}
for(i=0;i<n;i++)
sb.append(ans[i]+" ");
sb.append("\n");
}
System.out.print(sb);
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 009c753c268d29177b10126cc3edc967 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
public class booksez2 {
public static void main(String[] args) {
Scanner iangay = new Scanner(System.in);
int tc = iangay.nextInt();
for (int i = 0; i < tc; i++) {
int kids = iangay.nextInt();
int[] books = new int[kids];
for (int j = 0; j < kids; j++) {
books[j] = iangay.nextInt() - 1;
}
DSU dsu = new DSU(kids);
for (int j = 0; j < kids; j++) {
dsu.union(j, books[j]);
}
for (int j = 0; j < kids; j++) {
System.out.print(dsu.sizes[dsu.find(books[j])]+" ");
}
System.out.println();
}
}
}
class DSU {
int[] sizes, parent, rank;
DSU (int length) {
sizes = new int[length]; parent = new int[length]; rank = new int[length];
for (int i = 0; i < length; i++) { parent[i] = i; sizes[i] = 1; rank[i] = 1; }
}
int find(int x) {
if (parent[x] == x) { return x; }
else { parent[x] = find(parent[x]); }
return parent[x];
}
void union(int a, int b) {
int aRoot = find(a); int bRoot = find(b);
if (aRoot == bRoot) { return; }
if (rank[aRoot] > rank[bRoot]) {
parent[bRoot] = aRoot;
sizes[aRoot] += sizes[bRoot];
}
else if (rank[aRoot] < rank[bRoot]) {
parent[aRoot] = bRoot;
sizes[bRoot] += sizes[aRoot];
}
else {
parent[aRoot] = bRoot;
sizes[bRoot] += sizes[aRoot];
rank[bRoot]++;
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 4f15933f22e08c588b91ff7305813f4d | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
public class booksez {
public static void main(String[] args) {
Scanner iangay = new Scanner(System.in);
int tc = iangay.nextInt();
for (int i = 0; i < tc; i++) {
int kids = iangay.nextInt();
int[] books = new int[kids];
for (int j = 0; j < kids; j++) { books[j] = iangay.nextInt() - 1; }
// Integer[] days = new Integer[kids]; Collections.nCopies(kids, -1).toArray(days);
for (int j = 0; j < kids; j++) {
int steps = 1; int next = books[j];
while (next != j) { steps++; next = books[next]; }
// days[j] = steps;
System.out.print(steps+" ");
}
System.out.println();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | db3d76182e9854bf5a60687ee449dbbe | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | /*
//// ///// /// ////// // // //// // /// // // ///// // // //////
/ // // //// / // // // // // //// // // // // // /
/ // //// ///// / ////// // // // ///// // // // /// ////// /
/ // // // // / // // // // // // // // // // // // // /
//// //// // // / // // // //// // // ////// ////// // // /
*/
import java.util.*;
public class Euler1
{
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
int T = 0, N = 0;
if (sc.hasNextInt())
T = sc.nextInt();
while (T-- > 0)
{
if (sc.hasNextInt())
N = sc.nextInt();
int arr[] = new int[N];
for (int i = 0; i < N; i++)
arr[i] = sc.nextInt();
int cnt[] = new int[N], temp = 0;
for (int i = 0; i < N; i++)
{
cnt[i] = 1;
int a[] = new int[N];
for (int j = 0; j < N; j++)
a[j] = arr[j];
if (a[i] == i+1)
continue;
else
{
while (a[i] != i+1)
{
temp = a[a[i] - 1];
a[a[i] - 1] = a[i];
a[i] = temp;
cnt[i]++;
}
}
}
for (int i = 0; i < N; i++)
System.out.print(cnt[i] + " ");
System.out.println();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | caf0bcfcd55bd6fa6b7fe12a3517feba | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Solution {
public static void main(String args[]) throws IOException {
InputReader inp = new InputReader(System.in);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
solve(inp, out);
inp.bufferedReader.close();
out.close();
}
public static void solve(InputReader in, PrintWriter out) throws IOException {
int t = in.nextInt();
int n;
int arr[];
int count;
int bookLocation;
for (int i = 0; i < t; i++) {
n = in.nextInt();
arr = new int[n];
for (int j = 0; j < n; j++) {
arr[j] = in.nextInt();
}
for (int j = 1; j <= n; j++) {
count = 0;
bookLocation = j;
while (true) {
bookLocation = arr[bookLocation - 1];
count++;
if (bookLocation == j) {
break;
}
}
out.print(count + " ");
}
out.println();
}
}
}
class InputReader {
public BufferedReader bufferedReader;
public StringTokenizer stringTokenizer;
public InputReader(InputStream in) {
this.bufferedReader = new BufferedReader(new InputStreamReader(in));
this.stringTokenizer = null;
}
public String next() {
if(this.stringTokenizer == null || !this.stringTokenizer.hasMoreTokens()) {
try {
this.stringTokenizer = new StringTokenizer(bufferedReader.readLine());
} catch (IOException e) {
System.out.print(e.getStackTrace());
}
}
return this.stringTokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(this.next());
}
public long nextLong() {
return Long.parseLong(this.next());
}
public double nextDouble() {
return Double.parseDouble(this.next());
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 971b79327ccb9344d121928653049a80 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
public class Main {
static BufferedReader input = new BufferedReader(
new InputStreamReader(System.in)
);
public static void main(String[] args) throws IOException {
short tests = Short.parseShort(input.readLine());
while (tests-->0){
input.readLine();
String[] x = input.readLine().split(" ");
HashMap<Short, Short> books = new HashMap<>(x.length);
for (int i = 0; i < x.length; i++)
books.put((short) (i + 1), Short.parseShort(x[i]));
solve(books);
}
}
public static void solve(HashMap<Short, Short> books) {
for (short i = 1; i <= books.size(); i++) {
short current = books.get(i);
int turns = 1;
while (current != i) {
current = books.get(current);
turns++;
}
System.out.print(turns + " ");
}
System.out.println();
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 7d2f077b187ca31bc0251c442a5e2f30 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | /* package whatever; // 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 Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0){
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
for(int i=1;i<=n;i++){
int key=i; int ans=1;int temp=a[i-1];
while(key!=temp){
temp=a[temp-1];
ans++;
}
System.out.print(ans+" ");
}
System.out.println();
t--;
}
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 259d607dd06b5da9e627b31c5cc95df3 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.*;
public class A
{
static int [] d , p;
static boolean [] vis ;
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner() ;
PrintWriter out = new PrintWriter(System.out) ;
int q = sc.nextInt() ;
while(q-->0)
{
int n = sc.nextInt() ;
p = new int [n] ;
d = new int [n] ;
vis = new boolean[n] ;
for(int i = 0 ; i < n ; i ++)
p[i] = sc.nextInt() - 1 ;
for(int i = 0 ; i < n ;i++)
if(!vis[i])
{
int u = p[i] ;
int ans = 1 ;
while (u != i) {
ans++;
u = p[u] ;
}
u = i ;
while (!vis[u])
{
vis[u] = true ;
d[u] = ans ;
u = p[u] ;
}
}
for(int x : d)
out.print(x + " ");
out.println();
}
out.flush();
}
static class Scanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) ;
StringTokenizer st ;
String next() throws Exception
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine()) ;
return st.nextToken() ;
}
int nextInt() throws Exception
{
return Integer.parseInt(next()) ;
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 36019fa4df8e99f2cd49dd642537e601 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | //package hiougyf;
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
public class Solution {
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int a[]=new int[n+1];
for(int i=0;i<n;i++) a[i+1]=sc.nextInt();
for(int j=1;j<=n;j++) {
int ans=1;
int i=a[j];
while(i!=j) {
i=a[i];
ans++;
}
System.out.print(ans+" ");
}
System.out.println();
}
}
public static int upperBoundConsideringValueAlso(int[] array,
int length, int value)
{
//number of values greater than or equal to value
int low = 0;
int high = length-1;
int ans=high+1;
while (low <=high)
{
int mid = (low+(high-low)/2);
if (value > array[mid])
{
low = mid + 1;
}
else if(array[mid]>=value)
{
ans=mid;
high = mid-1;
}
}
return length-ans;
}
public static int floorSqrt(int x)
{
// Base Cases
if (x == 0 || x == 1)
return x;
// Do Binary Search for floor(sqrt(x))
int start = 1, end = x, ans=0;
while (start <= end)
{
int mid = (start + end) / 2;
// If x is a perfect square
if (mid*mid == x)
return mid;
// Since we need floor, we update answer when mid*mid is
// smaller than x, and move closer to sqrt(x)
if (mid*mid < x)
{
start = mid + 1;
ans = mid;
}
else // If mid*mid is greater than x
end = mid-1;
}
return ans;
}
static int div(int n,int b) {
int g=-1;
for(int i=2;i<=Math.sqrt(n);i++) {
if(n%i==0&&i!=b) {
g=i;
break;
}
}
return g;
}
public static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static int lcm(int a, int b)
{
return (a*b)/gcd(a, b);
}
public static boolean isPrime1(int n) {
if (n <= 1) {
return false;
}
if (n == 2) {
return true;
}
for (int i = 2; i <= Math.sqrt(n) + 1; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
if (n == 2) {
return true;
}
if (n % 2 == 0) {
return false;
}
for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 480b042dc27e78e9d1d1002b5ab42422 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class BookOwner {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt() - 1;
for (int i = 0; i < n; i++) {
int j = i;
int ans = 1;
while (a[j] != i) {
ans++;
j = a[j];
}
out.print(ans+" ");
}
out.println();
}
sc.close();
out.flush();
out.close();
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 3ed523f08c36b09e9209d0242fd7b23e | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes |
import java.util.Scanner;
public class Solution1249B1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-- > 0)
{
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; ++i)
{
arr[i] = scanner.nextInt();
arr[i]--;
}
int[] ans = new int[n];
for (int i = 0; i < n; ++i)
{
int cnt = 1;
int pos = arr[i];
while (pos != i) {
cnt++;
pos = arr[pos];
}
ans[i] = cnt;
}
for (int i : ans) System.out.printf("%d ", i);
System.out.println();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 029e3fab88a03d9b0d86bf14a1a70ffc | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<String> answer = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String requests = scanner.nextLine();
for (int i = 0; i<Integer.parseInt(requests); i++){
String kids = scanner.nextLine();
String kidsQueue = scanner.nextLine();
answer.add(findDaysCount(kidsQueue));
}
for (String teams:answer)
System.out.println(teams);
}
private static String findDaysCount(String kidsQueue) {
StringBuilder s = new StringBuilder();
String[] kidsQueueArray = kidsQueue.split(" ");
for (String kid:kidsQueueArray){
int daysCount =0;
String tmpKid = kid;
do {
tmpKid=kidsQueueArray[Integer.parseInt(tmpKid)-1];
daysCount++;
}while (!tmpKid.equals(kid));
s.append(daysCount).append(" ");
}
return s.toString();
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 7b1dc41ac1dd5208f42d799ccde6d34d | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
import java.io.*;
public class B{
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new
InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
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;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
static class Pair{
int l,r;
Pair(int l,int r){
this.l=l;
this.r=r;
}
}
static class My_Comparator implements Comparator<Pair>{
public int compare(Pair p1,Pair p2){
//return 1 if p1>p2, 0 if they are equal, or -1 if p1<p2
return 0;
}
}
// long minstep = Long.MAX_VALUE;
public static void main(String args[]){
try{
FastScanner sc=new FastScanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int p[]=new int[n+1];
for(int i=1; i<=n; i++){
p[i]=sc.nextInt();
}
int r[]=new int[n+1];
for(int i=1; i<=n; i++){
if(r[i]==0){
int k=p[i];
ArrayList<Integer> set=new ArrayList<Integer>();
set.add(i);
while(k!=i){
set.add(k);
k=p[k];
}
int size = set.size();
for(int j=0; j<set.size(); j++){
r[set.get(j)]=size;
}
}
}
for(int i=1; i<=n; i++){
System.out.print(r[i]+" ");
}
System.out.println();
}
}
catch(Exception e){}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 4628d20ff8a92921a0cca082189edad9 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
public class S {
public static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
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();
}
for(int i = 0 ; i < n ; i++) {
int x = -1;
int d = 0;
int y = i;
while(x != i + 1) {
x = arr[y];
y = x - 1;
d++;
}
System.out.print(d + " ");
}
System.out.println();
}
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | d28f3069a227fd119edfa56a247f0d90 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | // package com.company;
import java.util.*;
import java.io.*;
import java.lang.*;
/*
** @author jigar_nainuji
*/
public class Main{
public static void main(String args[]){
PrintWriter out=new PrintWriter(System.out);
InputReader in=new InputReader(System.in);
TASK solver = new TASK();
int t = in.nextInt();
// int t=1;
for(int i=0;i<t;i++)
{
solver.solve(in,out,i);
}
out.close();
}
static class TASK{
private Map<Integer, Node> map = new HashMap<>();
class Node {
int data;
Node parent;
int rank;
}
/**
* Create a set with only one element.
*/
public void makeSet(int data) {
Node node = new Node();
node.data = data;
node.parent = node;
node.rank = 0;
map.put(data, node);
}
/**
* Combines two sets together to one.
* Does union by rank
*
* @return true if data1 and data2 are in different set before union else false.
*/
public boolean union(int data1, int data2) {
Node node1 = map.get(data1);
Node node2 = map.get(data2);
Node parent1 = findSet(node1);
Node parent2 = findSet(node2);
//if they are part of same set do nothing
if (parent1.data == parent2.data) {
return false;
}
//else whoever's rank is higher becomes parent of other
if (parent1.rank >= parent2.rank) {
//increment rank only if both sets have same rank
parent1.rank = (parent1.rank == parent2.rank) ? parent1.rank + 1 : parent1.rank;
parent2.parent = parent1;
} else {
parent1.parent = parent2;
}
return true;
}
/**
* Finds the representative of this set
*/
public int findSet(int data) {
return findSet(map.get(data)).data;
}
/**
* Find the representative recursively and does path
* compression as well.
*/
private Node findSet(Node node) {
Node parent = node.parent;
if (parent == node) {
return parent;
}
node.parent = findSet(node.parent);
return node.parent;
}
void solve(InputReader in,PrintWriter out,int testNumber) {
int n = in.nextInt();
for(int i=1;i<=n;i++)
makeSet(i);
for(int i=1;i<=n;i++)
union(i,in.nextInt());
Map<Integer,Integer> map = new HashMap<>();
for(int i=1;i<=n;i++)
{
int x = findSet(i);
if(map.containsKey(x))
{
int y = map.get(x);
y++;
map.remove(x);
map.put(x,y);
}
else
map.put(x,1);
}
for(int i=1;i<=n;i++)
{
int x = findSet(i);
System.out.print(map.get(x)+" ");
}
System.out.println();
}
}
static class pair{
int sum;
int i;
pair(int sum,int i)
{
this.sum=sum;
this.i=i;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
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);
}
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | e084ab36513d8f316b323aa683436db5 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0){
int n = Integer.parseInt(br.readLine().trim());
int[] arr = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i= 0;i<n;i++){
arr[i] = Integer.parseInt(st.nextToken());
}
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
int currHolder = arr[i];
int dayCount = 1;
while(currHolder!= i+1){
dayCount++;
currHolder = arr[currHolder-1];
}
ans[i] = dayCount;
}
StringBuilder str = new StringBuilder("");
for(int i : ans){
str.append(i+" ");
}
System.out.println(str.toString());
}
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | be228a11f5de41a16c43f68b2408295b | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.Scanner;
public class pen {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int runs = scan.nextInt();
for(int i = 0;i<runs;i++){
int people = scan.nextInt();
int[] array = new int[people];
for(int x = 0;x<people;x++){
array[x]=scan.nextInt()-1;
}
for(int w = 0;w<array.length;w++){
int count = 0;
int number = -1;
while(number!=w){
if(count == 0){
number=array[w];
count++;
}
else {
number=array[number];
count++;
}
if(number==w){
System.out.println(count);
break;
}
}
}
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 7a7c62ae06fcf555bf0cb73219a182f0 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | //package pkg1249b1;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int q=sc.nextInt();
while(q-->0)
{
int n=sc.nextInt();
int array[]=new int[n+1];
for(int i=1;i<=n;i++)
{
array[i]=sc.nextInt();
}
for(int i=1;i<=n;i++)
{
int count=0;
int j=array[i];
while(j!=i)
{
j=array[j];
count++;
}
System.out.print(count+1+" ");
}
System.out.println();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | bc3933919de7e44ffbf977acfadb21a4 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes |
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
/**
*
* @author Auby
*/
public class P2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int queries = scanner.nextInt();
Set<Integer> studentsSkills;
for (int i = 0; i < queries; i++) {
int kids = scanner.nextInt();
int[] giveTo = new int[kids];
int[] books = new int[kids];
studentsSkills = new HashSet<>();
for (int j = 0; j < kids; j++) {
books[j] = j;
giveTo[j] = scanner.nextInt() - 1;
}
int[] result = findTime(books, giveTo);
for (int j = 0; j < result.length; j++) {
System.out.print(result[j]);
if (j < result.length - 1) {
System.out.print(" ");
}
}
System.out.println("");
}
scanner.close();
}
private static int[] findTime(int[] books, int[] giveTo) {
int[] result = new int[books.length];
for (int i = 0; i < books.length; i++) {
int position = books[i];
int moves = 0;
do {
position = giveTo[position];
moves++;
} while (books[i] != position);
result[i] = moves;
}
return result;
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 1a66c544df8c15dfc478d3c8c75b301e | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
import java.lang.Math;
import java.lang.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int t,n;
t=in.nextInt();
for(int i=0;i<t;i++)
{
n=in.nextInt();
int p[]=new int[n];
int a[]=new int[n];
/*int bk=new int[n];
for(int j=0;j<n;j++)
{
bk[j]=j;
}*/
for(int j=0;j<n;j++)
{
p[j]=in.nextInt()-1;
}
for(int j=0;j<n;j++)
{
int count=j,k=p[j],pt=0;
while(k!=count)
{
pt=k;
k=p[pt];
a[j]++;
}
}
for(int j=0;j<n;j++)
{
System.out.print(a[j]+1+" ");
}
System.out.println();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 90fcc1f6fa086a27a626a6de94365c18 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
static PrintWriter out;
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
out=new PrintWriter(System.out);
}
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;
}
}
// SHIVAM GUPTA :
// ASCII = 48 + i ;
static Set<Character> set1 ; static Set<Character> set2 ;
public static void main (String[] args) throws java.lang.Exception
{
FastReader scn = new FastReader() ;
int t= scn.nextInt() ;
for(int k = 1; k <= t ; k++)
{
int n = scn.nextInt() ;
int[] arr = new int[n+1] ;
boolean[] visited = new boolean[n+1] ;
int[] ans = new int[n+1] ;
for(int i = 1 ; i < n+1; i++)
{
arr[i] = scn.nextInt() ;
if(arr[i] == i)
{
visited[i] = true ;ans[i] = 1 ;
}
}
int count = 0 ;
for( int i = 1; i < ( n+ 1) ; i++ )
{
if(visited[i] == false)
{
int c = i ;
ArrayList<Integer> cycle = new ArrayList<Integer>() ;
cycle.add(i) ;
count++ ;
visited[i] = true ;
int j = arr[i] ;
while( j != i)
{
cycle.add(j) ;
count++ ;
visited[j] = true ;
j = arr[j] ;
}
for(Integer x : cycle)
{
ans[x] = count ;
}
count = 0 ;
}
}
for(int i = 1 ; i < (n+1) ; i++ )
{
out.print(ans[i] +" ");
}
out.println() ;
}
out.flush() ;
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | a7bea746fddcddc4e832cb9ccc50c5d1 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
while (q-- > 0) {
int n = in.nextInt();
final int[] p = new int[n];
int[] now = new int[n], days = new int[n], daysCopy = new int[n];
for (int i = 0; i < n; i++) {
p[i] = in.nextInt() - 1;
now[i] = p[i];
if (i == now[i]) {
days[i] = 1;
daysCopy[i] = 1;
}
}
Arrays.sort(daysCopy);
int j = 2;
while (daysCopy[0] == 0) {
int[] after = new int[n];
for (int i = 0; i < n; i++) {
if (days[i] == 0) {
after[i] = p[now[i]];
if (i == after[i]) {
days[i] = j;
}
}
}
now = after;
j++;
daysCopy = days.clone();
Arrays.sort(daysCopy);
}
for (int i = 0; i < n; i++) {
System.out.print(days[i] + " ");
}
System.out.println();
}
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 4bfb7b2cd5853d82798ba47bcf9529f9 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes |
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class Cf131 implements Runnable
{
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private 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 void main(String args[]) throws Exception
{
new Thread(null, new Cf131(),"Main",1<<27).start();
}
static class Pair
{
int z;
int o;
Pair(int z,int o)
{
this.z=z;
this.o=o;
}
}
static class Edge implements Comparable<Edge>
{
int end, wght;
public Edge(int end, int wght)
{
this.end = end;
this.wght = wght;
}
public int compareTo(Edge edge)
{
return this.wght - edge.wght;
}
}
public void run()
{
InputReader sc = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
Integer[] a = new Integer[n];
HashMap<Integer,Integer> hm =new HashMap<Integer,Integer>();
for(int i=0;i<n;i++){a[i]=sc.nextInt();}
for(int i=0;i<n;i++){
int ans = 0;
int ind = i;
for(int j=0;j<n;j++){
if(a[ind]==i+1){
ans = j+1;
w.print(ans+" ");
break;
}
else{
ind = a[ind]-1;
}
}
}
w.println();
}
w.flush();
w.close();
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 77323a8f3ef1630fbfb8b265e910bd31 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
public class p11 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
sc.nextLine();
int ans[][] = new int[T][];
for (int test = 0; test < T; test++) {
int n = sc.nextInt();
sc.nextLine();
StringTokenizer str = new StringTokenizer(sc.nextLine(), " ");
int ar[] = new int[n];
for (int x = 0; x < n; x++) {
ar[x] = Integer.parseInt(str.nextToken()) - 1;
}
int c[] = new int[n];
for (int x = 0; x < n; x++) {
int b = x;
int i = ar[b];
int count = 1;
while (i != x) {
b = i;
i = ar[b];
count++;
}
c[x] = count;
}
ans[test] = c;
}
for (int test = 0; test < T; test++) {
for (int y = 0; y < ans[test].length; y++) {
System.out.print(ans[test][y] + " ");
}
System.out.println();
}
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 59dbca23a41d9025ae454638fab39b64 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ar {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws java.lang.Exception {
FastReader scn = new FastReader();
int q=scn.nextInt();
for(int i=0;i<q;i++){
int n = scn.nextInt();
int[] arr = new int[n];
String ans="";
for(int j=0;j<n;j++){
arr[j]=scn.nextInt();
}
for(int j=0;j<n;j++){
if(j==0){
ans=""+func(arr,j+1);
}else{
ans+=" "+func(arr,j+1);
}
}
System.out.print(ans+"\n");
}
}
static int func(int[] arr, int num){
if(arr[num-1]==num)return 1;
int tmp=num;
int day=1;
while(tmp!=arr[num-1]){
num=arr[num-1];
day++;
}
return day;
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | dd473a9faee5902ad82bbc439d3d9214 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes |
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int tcase = in.nextInt();
while(tcase-->0){
int count = in.nextInt();
int flag=0;
int [] list = new int[count+1];
int temp=0;
List<Integer> list2 = new ArrayList<>();
for(int i=1 ; i<=count ; i++)
list[i]= in.nextInt();
for(int i=1 ; i<=count ; i++){
temp = list[i];
while(true){
if(temp==i){
flag++;
break;
}else{
temp=list[temp];
flag++;
}
}
list2.add(flag);
flag=0;
}
for(int i=0 ; i<count ; i++)
System.out.print(list2.get(i)+" ");
System.out.println();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | fc26c2beeef4b73eb7d6cc63bc07f348 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.Scanner;
/**
*
* @author DELL
*/
public class Codechef {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int q=sc.nextInt();
for(int i=0;i<q;i++)
{
int n=sc.nextInt();
int ar[]=new int[n];
for(int j=0;j<n;j++)
ar[j]=sc.nextInt();
for(int j=0;j<n;j++)
{
int t=j;
int sum=1;
while(ar[t]!=j+1)
{
t=ar[t]-1;
sum++;
}
System.out.print(sum+" ");
}
System.out.println();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | b16cf0f8acc030fa5e8aa55a8ceea491 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 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 q=0;q<t;q++)
{
int n=sc.nextInt();
int arr[]=new int[n];
int ans[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
int in=i;
int count=0;
do
{
in=arr[in]-1;
count++;
}while(i!=in);
ans[i]=count;
}
for(int i=0;i<n;i++)
{
System.out.println(ans[i]+" ");
}
System.out.println();
}
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | d4ed1e5363049ad571b83e509b62ec63 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes |
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
int [][] res = new int[q][];
for (int i = 0; i < q; ++i) {
int N = in.nextInt();
int[] test = new int[N];
for (int j = 0; j < N; ++j)
test[j] = in.nextInt() - 1;
res[i] = cycles(test);
}
for (int i = 0; i < q; ++i) {
for (int j = 0; j < res[i].length; ++j) {
System.out.print(res[i][j] + " ");
}
System.out.println();
}
in.close();
}
private static int[] cycles(int[] perm) {
int [] c = new int[perm.length];
for (int i = 0; i < perm.length; ++i)
c[i] = -1;
for (int i = 0; i < perm.length; ++i) {
if (c[i] == -1) {
int day = 1;
int next = perm[i];
while (perm[next] != perm[i]) {
next = perm[next];
++day;
}
c[i] = day;
next = perm[i];
while (perm[next] != perm[i]) {
c[i] = day;
next = perm[next];
}
}
}
return c;
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | a6e361fd7874124aa55c20ccd556a543 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes |
import java.util.*;
public class test {
private static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int t = sc.nextInt();
while (t > 0) {
int n=sc.nextInt();
int ar[]=new int[n+1];
ar[0]=0;
for(int i=1;i<=n;i++)
ar[i]=sc.nextInt();
int ans[]=new int[n];
for(int i=1;i<=n;i++)
{
int x=ar[i];
//ArrayList<Integer> al=new ArrayList<Integer>();
int initial=ar[x];
int start=ar[x];
int c=1;
while(initial!=ar[start])
{
start=ar[start];
++c;
}
ans[i-1]=c;
}
for (int i : ans) {
System.out.print(i+" ");
}
t--;
}
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | b9bc16f1fe3132c31a9689255010f9d3 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
public class Main {
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();
}
}
static class Disjoint_Set{
int n;//n represents number of nodes
int[] parent;
int[] rank;
public Disjoint_Set(int n) {
this.n = n;
parent = new int[n];
rank = new int[n];
}
public void initialize(int[] parent) {
for (int i = 0; i < this.n; i++) {
parent[i] = i;
}
}
//find() using path compression
public int find(int x){
if(x==parent[x]) return x;
else{
parent[x]=find(parent[x]);
return parent[x];
}
}
//find union by RANK
public int[] union(int x,int y){
int x_rep = find(x);
int y_rep = find(y);
if(x_rep == y_rep);
else if(rank[x_rep]<rank[y_rep]) parent[x_rep] = y_rep;
else if(rank[x_rep]>rank[y_rep]) parent[y_rep] = x_rep;
else{
parent[y_rep] = x_rep;
rank[x_rep]++;
}
return parent;
}
}
public static void main(String[] args) throws IOException {
Reader r = new Reader();
int q = r.nextInt();
while(q-->0){
int n = r.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++) a[i] = r.nextInt()-1;
for(int i=0;i<n;i++){
int temp = a[i];
int count = 0;
while(temp!=i){
count++;
temp = a[temp];
}
count++;
System.out.print(count + " ");
}
System.out.println();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | a49934e7c2143a08afbf1bae09068099 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | /**
* @author egaeus
* @mail sebegaeusprogram@gmail.com
* @veredict Accepted
* @url <https://codeforces.com/problemset/problem/1249/B2>
* @category math
* @date 22/10/2019
**/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static java.lang.Integer.parseInt;
public class CF1249B2 {
public static void main(String args[]) throws Throwable {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
for (int Q=parseInt(in.readLine()), q = 0; q++<Q; ) {
int N = parseInt(in.readLine());
int[] arr = new int[N];
int[] res = new int[N];
StringTokenizer st = new StringTokenizer(in.readLine());
for(int i=0;i<arr.length;i++)
arr[i]=parseInt(st.nextToken())-1;
boolean[] vis = new boolean[N];
for(int i=0;i<N;i++) {
if(!vis[i]) {
LinkedList<Integer> cola = new LinkedList<>();
TreeSet<Integer> nums = new TreeSet<>();
nums.add(i);
cola.add(i);
vis[i]=true;
for(;!cola.isEmpty();) {
int v = cola.poll();
if(!vis[arr[v]]) {
nums.add(arr[v]);
vis[arr[v]]=true;
cola.add(arr[v]);
}
}
for(int k:nums)
res[k]=nums.size();
}
}
sb.append(IntStream.of(res).mapToObj(a -> a+" ").collect(Collectors.joining(" "))).append("\n");
}
System.out.print(new String(sb));
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | fcb6a5d18a3c43c0439421596d0615e2 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class BooksExchange {
static long MOD = 1_000_000_007;
static long pow(long x, long y, long mod) {
long res = 1;
x = x % mod;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
static String binStr(long n, int len) { // reversed: 1 2 4 8
StringBuilder out = new StringBuilder();
while (n > 0) {
out.append(n % 2);
n /= 2;
}
while (out.length() < len) {
out.append(0);
}
return out.toString();
}
static class Tuple2 implements Comparable<Tuple2> {
int a, b;
Tuple2(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Tuple2 o) {
return this.a + this.b - o.a - o.b;
}
}
static class Tuple3 implements Comparable<Tuple3> {
int a, b, ind;
Tuple3(int a, int b, int ind) {
this.a = a;
this.b = b;
this.ind = ind;
}
@Override
public int compareTo(Tuple3 o) {
return this.a + this.b - o.a - o.b;
}
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int q = Integer.parseInt(in.readLine());
for (int t = 0; t < q; t++) {
int n = Integer.parseInt(in.readLine());
int[] p = new int[n + 1];
StringTokenizer s = new StringTokenizer(in.readLine());
for (int i = 1; i <= n; i++) {
p[i] = Integer.parseInt(s.nextToken());
}
int[] ans = new int[n + 1];
boolean[] check = new boolean[n + 1];
int temp, cur;
for (int i = 1; i<= n; i++) {
if (!check[i]) {
ArrayList<Integer> now = new ArrayList<>();
temp = 1;
now.add(i);
cur = p[i];
while (cur != i) {
check[cur] = true;
now.add(cur);
temp++;
cur = p[cur];
}
for (int num : now) {
ans[num] = temp;
}
}
}
for (int i = 1; i <= n; i++) {
System.out.print(ans[i]+" ");
}
System.out.println();
}
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | ded53ebc77c5b73e3d9bd49ccc8941a7 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.Arrays;
public class BookExchange {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int queryCount = Integer.parseInt(reader.readLine());
while (queryCount-- > 0) {
int length = Integer.parseInt(reader.readLine());
int[] books = Arrays.stream(reader.readLine().split("\\s")).mapToInt(Integer::parseInt).toArray();
processBook(books, writer);
writer.write("\n");
}
writer.flush();
}
private static void processBook(int[] books, BufferedWriter writer) throws IOException {
int[] arr = new int[books.length];
int c = 0;
for (int book : books) {
arr[c++] = book - 1;
}
for (int b = 0; b < books.length; b++) {
int index = arr[b];
int count = 1;
if (index == b) {
writer.write(1 + " ");
} else {
while (b != arr[index]) {
count++;
index = arr[index];
}
writer.write(count + 1 + " ");
}
}
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 13dedfb20682e6f20623f23cdb1b917c | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class booksExchangeE {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder ans = new StringBuilder();
int q = sc.nextInt();
while (--q >= 0) {
int n = sc.nextInt(), a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt() - 1;
}
int b[] = new int[n];
boolean visited[] = new boolean[n];
for (int j = 0; j < visited.length; j++) {
if (visited[j])
continue;
int i = j;
int dayCount = 1, sourceVis = 1;
while (sourceVis != 3) {
if (!visited[i]) {
visited[i] = true;
b[i] = dayCount;
} else {
b[i] = dayCount - b[i];
}
i = a[i];
dayCount++;
if (i == j)
sourceVis++;
}
}
for (int i = 0; i < n; i++) {
ans.append(b[i] + " ");
}
ans.append("\n");
}
System.out.print(ans);
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 58b738be2786cb7c87627a3f7e03c569 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.Scanner;
public class Ex2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nQueries = sc.nextInt();
for(int i=0; i<nQueries; i++) {
int nKids = sc.nextInt();
int kids[] = new int[nKids];
for(int j=0; j<nKids; j++) {
kids[j] = sc.nextInt();
}
for(int j=0; j<nKids; j++) {
if(kids[j] == j+1) {
System.out.print("1 ");
}else {
System.out.print(daysToReturnTo(kids, kids[j], j+1, 1) + " ");
}
}
System.out.println();
}
}
public static int daysToReturnTo(int kids[], int newOwner, int actualOwner, int nSteps) {
//System.out.println("Book from " + actualOwner + " now with " + newOwner + " in step number " + nSteps);
if(kids[newOwner-1] == actualOwner) {
//System.out.println("Book from " + actualOwner + " returns from " + newOwner + " to its owner");
return nSteps+1;
}
//System.out.println("Book from " + actualOwner + " goes from " + newOwner + " to " + kids[newOwner-1]);
return daysToReturnTo(kids, kids[newOwner-1], actualOwner, nSteps+1);
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | b486931598fe9b312ba327e05291686f | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* 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();
int[] p = new int[n];
for (int i = 0; i < n; i++) p[i] = in.nextInt();
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
int temp = p[i] - 1;
int cnt = 1;
while (temp != i) {
temp = p[temp] - 1;
cnt++;
}
ans[i] = cnt;
}
for (int i = 0; i < n; i++) out.print(ans[i] + " ");
out.println();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | da33f8cea286977f3738fcfcd02f36f6 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
public class BooksExchange {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int q=sc.nextInt();
while(q--!=0) {
int n=sc.nextInt();int a[]=new int[n+1];for(int i=1;i<=n;i++)a[i]=sc.nextInt();
List<Integer> l=new ArrayList<>();
for(int i=1;i<=n;i++) {
int cnt=1,x=a[i];
while(x!=i) {
x=a[x];
cnt++;
}
l.add(cnt);
}
for(int e:l)System.out.print(e+" ");
System.out.println();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | fc84e9bb6fac8bed0a4cdeb8d3925b7a | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
// ▄▀▀▀▀▄ ▄▀▀▄ ▄▄ ▄▀▀▀▀▄ ▄▀▀▄ █ ▄▀▀█▄▄▄▄ ▄▀▀▄▀▀▀▄ ▄▀▀▀▀▄ ▄▀▀▀▀▄ ▄▀▀▀▀▄
// █ █ ▐ █ █ ▄▀ █ █ █ █ ▄▀ ▐ ▄▀ ▐ █ █ █ █ █ ▐ █ █ ▐ █ █ ▐
// ▀▄ ▐ █▄▄▄█ █ █ ▐ █▀▄ █▄▄▄▄▄ ▐ █▀▀█▀ ▀▄ ▀▄ ▀▄
// ▀▄ █ █ █ ▀▄ ▄▀ █ █ █ ▌ ▄▀ █ ▀▄ █ ▀▄ █ ▀▄ █
// █▀▀▀ ▄▀ ▄▀ ▀▀▀▀ ▄▀ █ ▄▀▄▄▄▄ █ █ █▀▀▀ █▀▀▀ █▀▀▀
// ▐ █ █ █ ▐ █ ▐ ▐ ▐ ▐ ▐ ▐
// ▐ ▐ ▐ ▐
public class Main {
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out);
// br = new BufferedReader(new FileReader("cutting.in"));
// pw = new PrintWriter("cutting.out");
// Scanner sc = new Scanner(new FileReader("dsu.in"));
// PrintWriter pw = new PrintWriter("dsu.out");
int t = nextInt();
for (int q = 0; q < t; q++) {
int n = nextInt();
parent = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
make_set(i);
}
for (int i = 0; i < n; i++) {
union_sets(i, nextInt() - 1);
}
for (int i = 0; i < n; i++) {
pw.print(size[find_set(i)] + " ");
}
pw.println();
}
pw.close();
}
static int[] parent;
static int[] size;
static void make_set(int v) {
parent[v] = v;
size[v] = 1;
}
static int find_set(int v) {
if (v == parent[v]) {
return v;
}
return parent[v] = find_set(parent[v]);
}
static void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (size[a] < size[b]) {
int h = a;
a = b;
b = h;
}
parent[b] = a;
size[a] += size[b];
}
}
static PrintWriter pw;
static StringTokenizer st = new StringTokenizer("");
static BufferedReader br;
static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.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 Cutting {
String s;
int u;
int v;
public Cutting(String s, int u, int v) {
this.s = s;
this.u = u;
this.v = v;
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 5fe2c6534d81cd2547bf1da3eb787191 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf1249b1 {
static BufferedReader __in;
static PrintWriter __out;
static StringTokenizer input;
public static void main(String[] args) throws IOException {
__in = new BufferedReader(new InputStreamReader(System.in));
__out = new PrintWriter(new OutputStreamWriter(System.out));
int q = ri();
while(q --> 0) {
int n = ri(), a[] = ria(n);
DSU dsu = new DSU(n);
for(int i = 0; i < n; ++i) {
dsu.union(i, a[i] - 1);
}
for(int i = 0; i < n; ++i) {
pr(dsu.sz[dsu.find(i)]);
pr(' ');
}
prln();
}
close();
}
static class DSU {
int[] par, sz;
DSU(int n) {
par = new int[n];
sz = new int[n];
for(int i = 0; i < n; ++i) {
make(i);
}
}
void make(int v) {
par[v] = v;
sz[v] = 1;
}
int find(int v) {
if(v == par[v]) {
return v;
} else {
par[v] = find(par[v]);
return par[v];
}
}
void union(int u, int v) {
int a = find(u), b = find(v);
if(a != b) {
if(sz[a] < sz[b]) {
int swap = a;
a = b;
b = swap;
}
par[b] = a;
sz[a] += sz[b];
}
}
}
// references
// IBIG = 1e9 + 7
// IRAND ~= 3e8
// IMAX ~= 2e10
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IRAND = 327859546;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// util
static int minof(int a, int b, int c) {
return min(a, min(b, c));
}
static int minof(int... x) {
return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));
}
static int minstarting(int offset, int... x) {
assert x.length > 2;
return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));
}
static long minof(long a, long b, long c) {
return min(a, min(b, c));
}
static long minof(long... x) {
return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));
}
static long minstarting(int offset, long... x) {
assert x.length > 2;
return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));
}
static int maxof(int a, int b, int c) {
return max(a, max(b, c));
}
static int maxof(int... x) {
return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));
}
static int maxstarting(int offset, int... x) {
assert x.length > 2;
return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));
}
static long maxof(long a, long b, long c) {
return max(a, max(b, c));
}
static long maxof(long... x) {
return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));
}
static long maxstarting(int offset, long... x) {
assert x.length > 2;
return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));
}
static int powi(int a, int b) {
if(a == 0) return 0;
int ans = 1;
while(b > 0) {
if((b & 1) > 0) ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
static long powl(long a, int b) {
if(a == 0) return 0;
long ans = 1;
while(b > 0) {
if((b & 1) > 0) ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
static int floori(double d) {
return (int) d;
}
static int ceili(double d) {
return (int) ceil(d);
}
static long floorl(double d) {
return (long) d;
}
static long ceill(double d) {
return (long) ceil(d);
}
// input
static void r() throws IOException {
input = new StringTokenizer(__in.readLine());
}
static int ri() throws IOException {
return Integer.parseInt(__in.readLine());
}
static long rl() throws IOException {
return Long.parseLong(__in.readLine());
}
static int[] ria(int n) throws IOException {
int[] a = new int[n];
input = new StringTokenizer(__in.readLine());
for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken());
return a;
}
static long[] rla(int n) throws IOException {
long[] a = new long[n];
input = new StringTokenizer(__in.readLine());
for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken());
return a;
}
static char[] rcha() throws IOException {
return __in.readLine().toCharArray();
}
static String rline() throws IOException {
return __in.readLine();
}
static int rni() throws IOException {
input = new StringTokenizer(__in.readLine());
return Integer.parseInt(input.nextToken());
}
static int ni() {
return Integer.parseInt(input.nextToken());
}
static long rnl() throws IOException {
input = new StringTokenizer(__in.readLine());
return Long.parseLong(input.nextToken());
}
static long nl() {
return Long.parseLong(input.nextToken());
}
// output
static void pr(int i) {
__out.print(i);
}
static void prln(int i) {
__out.println(i);
}
static void pr(long l) {
__out.print(l);
}
static void prln(long l) {
__out.println(l);
}
static void pr(double d) {
__out.print(d);
}
static void prln(double d) {
__out.println(d);
}
static void pr(char c) {
__out.print(c);
}
static void prln(char c) {
__out.println(c);
}
static void pr(char[] s) {
__out.print(new String(s));
}
static void prln(char[] s) {
__out.println(new String(s));
}
static void pr(String s) {
__out.print(s);
}
static void prln(String s) {
__out.println(s);
}
static void pr(Object o) {
__out.print(o);
}
static void prln(Object o) {
__out.println(o);
}
static void prln() {
__out.println();
}
static void pryes() {
__out.println("yes");
}
static void pry() {
__out.println("Yes");
}
static void prY() {
__out.println("YES");
}
static void prno() {
__out.println("no");
}
static void prn() {
__out.println("No");
}
static void prN() {
__out.println("NO");
}
static void pryesno(boolean b) {
__out.println(b ? "yes" : "no");
}
;
static void pryn(boolean b) {
__out.println(b ? "Yes" : "No");
}
static void prYN(boolean b) {
__out.println(b ? "YES" : "NO");
}
static void prln(int... a) {
for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i) ;
__out.println(a[a.length - 1]);
}
static void prln(long... a) {
for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i) ;
__out.println(a[a.length - 1]);
}
static <T> void prln(Collection<T> c) {
int n = c.size() - 1;
Iterator<T> iter = c.iterator();
for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i) ;
__out.println(iter.next());
}
static void flush() {
__out.flush();
}
static void close() {
__out.close();
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | ab385e20ff4171499f9e5f6c5999127b | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | /* package whatever; // 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 Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// System.out.println("hello");
int t = Integer.parseInt(br.readLine());
while(t-- > 0) {
int n = Integer.parseInt(br.readLine());
int ans = 1;
ArrayList<Integer> a = new ArrayList<Integer>();
ArrayList<Integer> temp = new ArrayList<Integer>();
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++) {
a.add(Integer.parseInt(st.nextToken()));
}
int day;
for(int i=0;i<n;i++) {
day = 1;
// if(a.get(i) == i) {
// temp.add(1);
// continue;
// }
int j = a.get(i) - 1;
while(j != i){
j = a.get(j) - 1;
day += 1;
}
temp.add(day);
}
for(int i=0;i<n;i++) {
System.out.print(temp.get(i)+ " ");
}
System.out.println();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | d6dc464c2844314bdca942d351f37d3b | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
import java.math.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int tt = 0; tt < t; tt++) {
int n = in.nextInt();
int[] ar = new int[n + 1];
for(int i = 1; i <= n; i++) ar[i] = in.nextInt();
for(int i = 1; i <= n; i++) {
int pos = ar[i];
int d = 1;
while(pos != i) {
pos = ar[pos];
d += 1;
}
if(i > 1) {
System.out.print(" ");
}
System.out.print(d);
}
System.out.print("\n");
}
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | b5c67f909ca4c31fdaf9500e74e29c62 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author gaidash
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
B1ObmenKnigamiProstayaVersiya solver = new B1ObmenKnigamiProstayaVersiya();
solver.solve(1, in, out);
out.close();
}
static class B1ObmenKnigamiProstayaVersiya {
private boolean[] visited;
private int[] a;
private void dfs(int v, ArrayList<Integer> comp) {
visited[v] = true;
comp.add(v);
if (!visited[a[v]]) {
dfs(a[v], comp);
}
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int q = in.nextInt();
while (q-- > 0) {
int n = in.nextInt();
a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt() - 1;
}
visited = new boolean[n];
int[] ret = new int[n];
for (int v : a) {
if (!visited[v]) {
ArrayList<Integer> comp = new ArrayList<>();
dfs(v, comp);
for (int u : comp) {
ret[u] = comp.size();
}
}
}
out.println(ret);
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void println(int[] array) {
print(array);
writer.println();
}
public void close() {
writer.close();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 5615d56296080a30a5116c139c0485a4 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
while(q--!=0)
{
int n = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++)
a[i] = sc.nextInt()-1;
int visited[] = new int[n];
int ans[] = new int[n];
for(int i=0;i<n;i++)
{
if(visited[i]==0)
{
int temp = a[i];
HashSet<Integer> s = new HashSet<Integer>();
s.add(i);
int k = 1;
while(temp!=i)
{
k++;
temp = a[temp];
s.add(temp);
}
for(int p:s)
{
visited[i] = 1;
ans[i] = k;
}
}
}
for(int i=0;i<n;i++)
System.out.print(ans[i]+" ");
System.out.println();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | f72b0ece7b3a2e83b348b3f036af22e6 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
while(q--!=0)
{
int n = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++)
a[i] = sc.nextInt()-1;
int visited[] = new int[n];
int ans[] = new int[n];
for(int i=0;i<n;i++)
{
if(visited[i]==0)
{
int temp = a[i];
HashSet<Integer> s = new HashSet<Integer>();
s.add(i);
int k = 1;
while(temp!=i)
{
k++;
temp = a[temp];
s.add(temp);
}
for(int p:s)
{
visited[p] = 1;
ans[p] = k;
}
}
}
for(int i=0;i<n;i++)
System.out.print(ans[i]+" ");
System.out.println();
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | c05ff24a579c159846a6cc4109311f0e | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
while(q--!=0)
{
int n = sc.nextInt();
Graph g = new Graph(n);
for(int i=0;i<n;i++)
g.addEdge(i,sc.nextInt()-1);
for(int i=0;i<n;i++)
{
g.dfs(i);
System.out.print(g.ans+" ");
}
System.out.println();
}
}
}
class Graph
{
int v ;
int ans;
LinkedList<Integer> addlist[];
Graph(int v)
{
this.v = v;
ans = 0;
addlist = new LinkedList[v];
for(int i=0; i<v; ++i)
addlist[i] = new LinkedList<Integer>();
}
public void addEdge(int s,int d)
{
addlist[s].add(d);
// addlist[d].add(s);
}
public void dfsHelper(int[] visited,int s)
{
visited[s] = 1;
ans++;
for(int i:addlist[s])
{
if(visited[i]!=1)
{
visited[i] = 1;
dfsHelper(visited,i);
}
}
}
public void dfs(int s)
{
int visited[] = new int[v];
ans = 0;
dfsHelper(visited,s);
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 23f5a1ba622655c0722ca98cd620980f | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | /* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */
//codeforces1249B
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Math.*;
import java.math.*;
public class Main{
static PrintWriter go = new PrintWriter(System.out);
public static void main(String args[]) throws IOException,FileNotFoundException {
BufferedReader gi = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-1"));
int t = Integer.parseInt(gi.readLine());
while (t-- > 0) {
int n = Integer.parseInt(gi.readLine());
int[] a = parseArray(gi);
UnionFind dsu = new UnionFind(n + 1);
for ( int k = 0; k < n; k++ )
dsu.unify(k + 1, a[k]);
for ( int k = 0; k < n; k++ )
go.print(dsu.componentSize(a[k]) + " ");
go.println();
}
go.close();
}
static int[] parseArray(BufferedReader gi) throws IOException{
String[] line = gi.readLine().split(" ");
int[] rez = new int[line.length];
for ( int k = 0; k < line.length; k++){
rez[k] = Integer.parseInt(line[k]);
}
return rez;
}
}
class UnionFind {
private int size;
private int[] sz;
private int[] id;
private int numComponents;
public UnionFind(int size) {
this.size = numComponents = size;
sz = new int[size];
id = new int[size];
for(int i = 0; i < size; i++) {
id[i] = i;
sz[i] = 1;
}
}
public int find(int p) {
int root = p;
while( root != id[root] )
root = id[root];
while(p != root) {
int next = id[p];
id[p] = root;
p = next;
}
return root;
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
public int componentSize(int p) {
return sz[find(p)];
}
public int size() {
return size;
}
public int components() {
return numComponents;
}
public void unify(int p, int q) {
int root1 = find(p);
int root2 = find(q);
if (root1 == root2) return;
if (sz[root1] < sz[root2]) {
sz[root2] += sz[root1];
id[root1] = root2;
} else {
sz[root1] += sz[root2];
id[root2] = root1;
}
numComponents--;
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 7e62a5ff58e4b6c5fa185ae35f3e4c50 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.*;
public class B1
{
static int dfs(int u, int[] comp)
{
comp[u] = idx;
int cnt = 1;
for (int v : adj[u])
if (comp[v] == 0)
cnt += dfs(v, comp);
return cnt;
}
static ArrayList<Integer>[] adj;
static int idx;
static void solve(FastIO io)
{
int n = io.nextInt();
adj = new ArrayList[n];
for (int i = 0; i < n; i++)
adj[i] = new ArrayList<>();
for (int i = 0; i < n; i++)
{
int k = io.nextInt() - 1;
adj[i].add(k);
adj[k].add(i);
}
int[] comp = new int[n];
ArrayList<Integer> sizes = new ArrayList<>();
idx = 1;
for (int i = 0; i < n; i++)
{
if (comp[i] == 0)
{
int s = dfs(i, comp);
idx++;
sizes.add(s);
}
}
for (int i = 0; i < n; i++)
{
io.print((sizes.get(comp[i] - 1) + " "));
}
io.println();
}
public static void main(String[] args) {
FastIO io = new FastIO();
int t = io.nextInt();
for (int i = 0; i < t; i++)
solve(io);
io.close();
}
}
class FastIO extends PrintWriter
{
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
FastIO()
{
super(System.out);
}
public String next()
{
while (!st.hasMoreTokens())
{
try {
st = new StringTokenizer(r.readLine());
} catch (Exception e) {
//TODO: handle exception
}
}
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 | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 676fd7e05afff65cef6f898116869da4 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class P_1249B1 {
static final FS sc = new FS();
static final PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) {
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int[] p = sc.nextArray(n);
for(int i=1; i<=n; i++){
int pos = p[i];
int count = 1;
while(pos!=i){
pos = p[pos];
count++;
}
System.out.print(count+" ");
}
System.out.println();
}
}
static class FS {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception ignored) {
}
}
return st.nextToken();
}
int[] nextArray(int n) {
int[] a = new int[n+1];
for (int i = 1; i <= n; i++) {
a[i] = nextInt();
}
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | f4d300d312b70a0480dd14596d7c3933 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
import java.io.*;
public class B {
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
for (int i = 1; i <= t; ++i) {
int n = in.nextInt();
int[] arr = new int[n];
for (int j=0; j<n; j++)
arr[j] = in.nextInt();
int[] ans = new int[n];
for (int j=0; j<n; j++){
if (ans[j] == 0){
ArrayList<Integer> list = new ArrayList<>();
list.add(j);
int nextInd = arr[j]-1;
while (nextInd != j) {
list.add(nextInd);
nextInd = arr[nextInd]-1;
}
int size = list.size();
for (int ind: list)
ans[ind] = size;
}
}
for (int j=0; j<n; j++)
System.out.print(ans[j]+" ");
System.out.print("\n");
}
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 6cb109cd07017f091cfe051a9d462734 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class forces{
public static void main(String args[])throws IOException{
DataInputStream ins=new DataInputStream(System.in);
int t=Integer.parseInt(ins.readLine());
for(int i=0;i<t;i++){
int n=Integer.parseInt(ins.readLine());
String s[]=ins.readLine().split(" ");
int arr[]=new int[n];
for(int f=0;f<n;f++){
arr[f]=Integer.parseInt(s[f]);//s[f]
}
StringBuffer br=new StringBuffer();
for(int j=0;j<n;j++){
int show=1;
int ind=j;
while(arr[ind]-1!=j){
show++;
ind=arr[ind]-1;
}
br.append(String.valueOf(show)+" ");
}
System.out.println(br);
}
}
}
| Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 57624195143fe0593c0d84a1fc053052 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
public class solution{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
ArrayList<Integer> a = new ArrayList<>();
while((q--)!=0){
int n = sc.nextInt();
int lol[] = new int[n+1];
for(int i = 1;i<n+1;i++){
lol[i] = sc.nextInt();
}
for(int i= 1;i<n+1;i++){
int ans = 0;
int temp = i;
temp = lol[i];
ans += 1;
while(true){
if(temp != i){
temp = lol[temp];
ans += 1;
}
else{
a.add(ans);
break;
}
}
}
for (int i=0; i<a.size(); i++)
System.out.print(a.get(i)+" ");
System.out.print("\n");
a.clear();
}
}
} | Java | ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"] | 1 second | ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"] | null | Java 11 | standard input | [
"dsu",
"math"
] | 345e76bf67ae4342e850ab248211eb0b | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. | 1,000 | For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | standard output | |
PASSED | 999ef0bc44f96d666e25d1e47e4c62f4 | train_002.jsonl | 1473584400 | You have recently fallen through a hole and, after several hours of unconsciousness, have realized you are in an underground city. On one of your regular, daily walks through the unknown, you have encountered two unusually looking skeletons called Sanz and P’pairus, who decided to accompany you and give you some puzzles for seemingly unknown reasons.One day, Sanz has created a crossword for you. Not any kind of crossword, but a 1D crossword! You are given m words and a string of length n. You are also given an array p, which designates how much each word is worth — the i-th word is worth pi points. Whenever you find one of the m words in the string, you are given the corresponding number of points. Each position in the crossword can be used at most x times. A certain word can be counted at different places, but you cannot count the same appearance of a word multiple times. If a word is a substring of another word, you can count them both (presuming you haven’t used the positions more than x times).In order to solve the puzzle, you need to tell Sanz what’s the maximum achievable number of points in the crossword. There is no need to cover all postions, just get the maximal score! Crossword and words contain only lowercase English letters. | 256 megabytes | //package bubblecup9f;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
public class G {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni();
char[] s = ns(n);
List<Edge> es = new ArrayList<>();
int m = ni();
int[] ps = new int[m];
char[][] qs = new char[m][];
for(int i = 0;i < m;i++){
qs[i] = ns().toCharArray();
ps[i] = ni();
}
int x = ni();
for(int i = 0;i < n;i++){
es.add(new Edge(i, i+1, x, 100));
}
for(int i = 0;i < m;i++){
inner:
for(int j = 0;j+qs[i].length-1 < n;j++){
for(int k = 0;k < qs[i].length;k++){
if(s[j+k] != qs[i][k])continue inner;
}
es.add(new Edge(j, j+qs[i].length, 1, 100*qs[i].length-ps[i]));
}
}
long F = solveMinCostFlow(compileWD(n+1, es), 0, n, x);
out.println(100L*n*x-F);
}
public static class Edge
{
public int from, to;
public int capacity;
public int cost;
public int flow;
public Edge complement;
// public int iniflow;
public Edge(int from, int to, int capacity, int cost) {
this.from = from;
this.to = to;
this.capacity = capacity;
this.cost = cost;
}
}
public static Edge[][] compileWD(int n, List<Edge> edges)
{
Edge[][] g = new Edge[n][];
// cloning
for(int i = edges.size()-1;i >= 0;i--){
Edge origin = edges.get(i);
Edge clone = new Edge(origin.to, origin.from, origin.capacity, -origin.cost);
clone.flow = origin.capacity;
clone.complement = origin;
origin.complement = clone;
edges.add(clone);
}
int[] p = new int[n];
for(Edge e : edges)p[e.from]++;
for(int i = 0;i < n;i++)g[i] = new Edge[p[i]];
for(Edge e : edges)g[e.from][--p[e.from]] = e;
return g;
}
// NOT VERIFIED
public static Edge[][] compileWU(int n, List<Edge> edges)
{
Edge[][] g = new Edge[n][];
// cloning
for(int i = edges.size()-1;i >= 0;i--){
Edge origin = edges.get(i);
Edge back = new Edge(origin.to, origin.from, origin.capacity, origin.cost);
edges.add(back);
}
for(int i = edges.size()-1;i >= 0;i--){
Edge origin = edges.get(i);
Edge clone = new Edge(origin.to, origin.from, origin.capacity, -origin.cost);
clone.flow = origin.capacity;
clone.complement = origin;
origin.complement = clone;
edges.add(clone);
}
int[] p = new int[n];
for(Edge e : edges)p[e.from]++;
for(int i = 0;i < n;i++)g[i] = new Edge[p[i]];
for(Edge e : edges)g[e.from][--p[e.from]] = e;
return g;
}
public static class MinHeap {
public int[] a;
public int[] map;
public int[] imap;
public int n;
public int pos;
public static int INF = Integer.MAX_VALUE;
public MinHeap(int m)
{
n = m+2;
a = new int[n];
map = new int[n];
imap = new int[n];
Arrays.fill(a, INF);
Arrays.fill(map, -1);
Arrays.fill(imap, -1);
pos = 1;
}
public int add(int ind, int x)
{
int ret = imap[ind];
if(imap[ind] < 0){
a[pos] = x; map[pos] = ind; imap[ind] = pos;
pos++;
up(pos-1);
}
return ret != -1 ? a[ret] : x;
}
public int update(int ind, int x)
{
int ret = imap[ind];
if(imap[ind] < 0){
a[pos] = x; map[pos] = ind; imap[ind] = pos;
pos++;
up(pos-1);
}else{
int o = a[ret];
a[ret] = x;
up(ret);
down(ret);
// if(a[ret] > o){
// up(ret);
// }else{
// down(ret);
// }
}
return x;
}
public int remove(int ind)
{
if(pos == 1)return INF;
if(imap[ind] == -1)return INF;
pos--;
int rem = imap[ind];
int ret = a[rem];
map[rem] = map[pos];
imap[map[pos]] = rem;
imap[ind] = -1;
a[rem] = a[pos];
a[pos] = INF;
map[pos] = -1;
up(rem);
down(rem);
return ret;
}
public int min() { return a[1]; }
public int argmin() { return map[1]; }
public int size() { return pos-1; }
private void up(int cur)
{
for(int c = cur, p = c>>>1;p >= 1 && a[p] > a[c];c>>>=1, p>>>=1){
int d = a[p]; a[p] = a[c]; a[c] = d;
int e = imap[map[p]]; imap[map[p]] = imap[map[c]]; imap[map[c]] = e;
e = map[p]; map[p] = map[c]; map[c] = e;
}
}
private void down(int cur)
{
for(int c = cur;2*c < pos;){
int b = a[2*c] < a[2*c+1] ? 2*c : 2*c+1;
if(a[b] < a[c]){
int d = a[c]; a[c] = a[b]; a[b] = d;
int e = imap[map[c]]; imap[map[c]] = imap[map[b]]; imap[map[b]] = e;
e = map[c]; map[c] = map[b]; map[b] = e;
c = b;
}else{
break;
}
}
}
}
public static long solveMinCostFlow(Edge[][] g, int source, int sink, long all)
{
int n = g.length;
long mincost = 0;
int[] potential = new int[n];
final int[] d = new int[n];
MinHeap q = new MinHeap(n);
while(all > 0){
// shortest path src->sink
Edge[] inedge = new Edge[n];
Arrays.fill(d, Integer.MAX_VALUE / 2);
d[source] = 0;
q.add(source, 0);
while(q.size() > 0){
int cur = q.argmin();
q.remove(cur);
for(Edge ne : g[cur]){
if(ne.capacity - ne.flow > 0){
int nd = d[cur] + ne.cost + potential[cur] - potential[ne.to];
if(d[ne.to] > nd){
inedge[ne.to] = ne;
d[ne.to] = nd;
q.update(ne.to, nd);
}
}
}
}
if(inedge[sink] == null)break;
long minflow = all;
long sumcost = 0;
for(Edge e = inedge[sink];e != null;e = inedge[e.from]){
if(e.capacity - e.flow < minflow)minflow = e.capacity - e.flow;
sumcost += e.cost;
}
mincost += minflow * sumcost;
for(Edge e = inedge[sink];e != null;e = inedge[e.from]){
e.flow += minflow;
e.complement.flow -= minflow;
}
all -= minflow;
for(int i = 0;i < n;i++){
potential[i] += d[i];
}
}
return mincost;
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new G().run(); }
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| Java | ["6\nabacba\n2\naba 6\nba 3\n3"] | 1 second | ["12"] | NoteFor example, with the string "abacba", words "aba" (6 points) and "ba" (3 points), and x = 3, you can get at most 12 points - the word "aba" appears once ("abacba"), while "ba" appears two times ("abacba"). Note that for x = 1, you could get at most 9 points, since you wouldn’t be able to count both "aba" and the first appearance of "ba". | Java 8 | standard input | [
"flows"
] | dde8f30871a79db3581b7a44042d3aa0 | The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the length of the crossword. The second line contains the crossword string. The third line contains a single integer m (1 ≤ m ≤ 100) — the number of given words, and next m lines contain description of words: each line will have a string representing a non-empty word (its length doesn't exceed the length of the crossword) and integer pi (0 ≤ pi ≤ 100). Last line of the input will contain x (1 ≤ x ≤ 100) — maximum number of times a position in crossword can be used. | 2,400 | Output single integer — maximum number of points you can get. | standard output | |
PASSED | 7b3dec7f75711de8faef28a5f38fd64f | train_002.jsonl | 1473584400 | You have recently fallen through a hole and, after several hours of unconsciousness, have realized you are in an underground city. On one of your regular, daily walks through the unknown, you have encountered two unusually looking skeletons called Sanz and P’pairus, who decided to accompany you and give you some puzzles for seemingly unknown reasons.One day, Sanz has created a crossword for you. Not any kind of crossword, but a 1D crossword! You are given m words and a string of length n. You are also given an array p, which designates how much each word is worth — the i-th word is worth pi points. Whenever you find one of the m words in the string, you are given the corresponding number of points. Each position in the crossword can be used at most x times. A certain word can be counted at different places, but you cannot count the same appearance of a word multiple times. If a word is a substring of another word, you can count them both (presuming you haven’t used the positions more than x times).In order to solve the puzzle, you need to tell Sanz what’s the maximum achievable number of points in the crossword. There is no need to cover all postions, just get the maximal score! Crossword and words contain only lowercase English letters. | 256 megabytes | //package bubblecup9f;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
public class G {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni();
char[] s = ns(n);
List<Edge> es = new ArrayList<>();
int m = ni();
int[] ps = new int[m];
char[][] qs = new char[m][];
for(int i = 0;i < m;i++){
qs[i] = ns().toCharArray();
ps[i] = ni();
}
int x = ni();
for(int i = 0;i < n;i++){
es.add(new Edge(i, i+1, x, 20000));
}
for(int i = 0;i < m;i++){
inner:
for(int j = 0;j+qs[i].length-1 < n;j++){
for(int k = 0;k < qs[i].length;k++){
if(s[j+k] != qs[i][k])continue inner;
}
es.add(new Edge(j, j+qs[i].length, 1, 20000*qs[i].length-ps[i]));
}
}
long F = solveMinCostFlow(compileWD(n+1, es), 0, n, x);
out.println(20000L*n*x-F);
}
public static class Edge
{
public int from, to;
public int capacity;
public int cost;
public int flow;
public Edge complement;
// public int iniflow;
public Edge(int from, int to, int capacity, int cost) {
this.from = from;
this.to = to;
this.capacity = capacity;
this.cost = cost;
}
}
public static Edge[][] compileWD(int n, List<Edge> edges)
{
Edge[][] g = new Edge[n][];
// cloning
for(int i = edges.size()-1;i >= 0;i--){
Edge origin = edges.get(i);
Edge clone = new Edge(origin.to, origin.from, origin.capacity, -origin.cost);
clone.flow = origin.capacity;
clone.complement = origin;
origin.complement = clone;
edges.add(clone);
}
int[] p = new int[n];
for(Edge e : edges)p[e.from]++;
for(int i = 0;i < n;i++)g[i] = new Edge[p[i]];
for(Edge e : edges)g[e.from][--p[e.from]] = e;
return g;
}
// NOT VERIFIED
public static Edge[][] compileWU(int n, List<Edge> edges)
{
Edge[][] g = new Edge[n][];
// cloning
for(int i = edges.size()-1;i >= 0;i--){
Edge origin = edges.get(i);
Edge back = new Edge(origin.to, origin.from, origin.capacity, origin.cost);
edges.add(back);
}
for(int i = edges.size()-1;i >= 0;i--){
Edge origin = edges.get(i);
Edge clone = new Edge(origin.to, origin.from, origin.capacity, -origin.cost);
clone.flow = origin.capacity;
clone.complement = origin;
origin.complement = clone;
edges.add(clone);
}
int[] p = new int[n];
for(Edge e : edges)p[e.from]++;
for(int i = 0;i < n;i++)g[i] = new Edge[p[i]];
for(Edge e : edges)g[e.from][--p[e.from]] = e;
return g;
}
public static class MinHeap {
public int[] a;
public int[] map;
public int[] imap;
public int n;
public int pos;
public static int INF = Integer.MAX_VALUE;
public MinHeap(int m)
{
n = m+2;
a = new int[n];
map = new int[n];
imap = new int[n];
Arrays.fill(a, INF);
Arrays.fill(map, -1);
Arrays.fill(imap, -1);
pos = 1;
}
public int add(int ind, int x)
{
int ret = imap[ind];
if(imap[ind] < 0){
a[pos] = x; map[pos] = ind; imap[ind] = pos;
pos++;
up(pos-1);
}
return ret != -1 ? a[ret] : x;
}
public int update(int ind, int x)
{
int ret = imap[ind];
if(imap[ind] < 0){
a[pos] = x; map[pos] = ind; imap[ind] = pos;
pos++;
up(pos-1);
}else{
int o = a[ret];
a[ret] = x;
up(ret);
down(ret);
// if(a[ret] > o){
// up(ret);
// }else{
// down(ret);
// }
}
return x;
}
public int remove(int ind)
{
if(pos == 1)return INF;
if(imap[ind] == -1)return INF;
pos--;
int rem = imap[ind];
int ret = a[rem];
map[rem] = map[pos];
imap[map[pos]] = rem;
imap[ind] = -1;
a[rem] = a[pos];
a[pos] = INF;
map[pos] = -1;
up(rem);
down(rem);
return ret;
}
public int min() { return a[1]; }
public int argmin() { return map[1]; }
public int size() { return pos-1; }
private void up(int cur)
{
for(int c = cur, p = c>>>1;p >= 1 && a[p] > a[c];c>>>=1, p>>>=1){
int d = a[p]; a[p] = a[c]; a[c] = d;
int e = imap[map[p]]; imap[map[p]] = imap[map[c]]; imap[map[c]] = e;
e = map[p]; map[p] = map[c]; map[c] = e;
}
}
private void down(int cur)
{
for(int c = cur;2*c < pos;){
int b = a[2*c] < a[2*c+1] ? 2*c : 2*c+1;
if(a[b] < a[c]){
int d = a[c]; a[c] = a[b]; a[b] = d;
int e = imap[map[c]]; imap[map[c]] = imap[map[b]]; imap[map[b]] = e;
e = map[c]; map[c] = map[b]; map[b] = e;
c = b;
}else{
break;
}
}
}
}
public static long solveMinCostFlow(Edge[][] g, int source, int sink, long all)
{
int n = g.length;
long mincost = 0;
int[] potential = new int[n];
final int[] d = new int[n];
MinHeap q = new MinHeap(n);
while(all > 0){
// shortest path src->sink
Edge[] inedge = new Edge[n];
Arrays.fill(d, Integer.MAX_VALUE / 2);
d[source] = 0;
q.add(source, 0);
while(q.size() > 0){
int cur = q.argmin();
q.remove(cur);
for(Edge ne : g[cur]){
if(ne.capacity - ne.flow > 0){
int nd = d[cur] + ne.cost + potential[cur] - potential[ne.to];
if(d[ne.to] > nd){
inedge[ne.to] = ne;
d[ne.to] = nd;
q.update(ne.to, nd);
}
}
}
}
if(inedge[sink] == null)break;
long minflow = all;
long sumcost = 0;
for(Edge e = inedge[sink];e != null;e = inedge[e.from]){
if(e.capacity - e.flow < minflow)minflow = e.capacity - e.flow;
sumcost += e.cost;
}
mincost += minflow * sumcost;
for(Edge e = inedge[sink];e != null;e = inedge[e.from]){
e.flow += minflow;
e.complement.flow -= minflow;
}
all -= minflow;
for(int i = 0;i < n;i++){
potential[i] += d[i];
}
}
return mincost;
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new G().run(); }
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| Java | ["6\nabacba\n2\naba 6\nba 3\n3"] | 1 second | ["12"] | NoteFor example, with the string "abacba", words "aba" (6 points) and "ba" (3 points), and x = 3, you can get at most 12 points - the word "aba" appears once ("abacba"), while "ba" appears two times ("abacba"). Note that for x = 1, you could get at most 9 points, since you wouldn’t be able to count both "aba" and the first appearance of "ba". | Java 8 | standard input | [
"flows"
] | dde8f30871a79db3581b7a44042d3aa0 | The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the length of the crossword. The second line contains the crossword string. The third line contains a single integer m (1 ≤ m ≤ 100) — the number of given words, and next m lines contain description of words: each line will have a string representing a non-empty word (its length doesn't exceed the length of the crossword) and integer pi (0 ≤ pi ≤ 100). Last line of the input will contain x (1 ≤ x ≤ 100) — maximum number of times a position in crossword can be used. | 2,400 | Output single integer — maximum number of points you can get. | standard output | |
PASSED | e3874e6921f63248dea6e3f134980511 | train_002.jsonl | 1428165300 | In this problem you will meet the simplified model of game Pudding Monsters.An important process in developing any game is creating levels. A game field in Pudding Monsters is an n × n rectangular grid, n of its cells contain monsters and some other cells contain game objects. The gameplay is about moving the monsters around the field. When two monsters are touching each other, they glue together into a single big one (as they are from pudding, remember?). Statistics showed that the most interesting maps appear if initially each row and each column contains exactly one monster and the rest of map specifics is set up by the correct positioning of the other game objects. A technique that's widely used to make the development process more efficient is reusing the available resources. For example, if there is a large n × n map, you can choose in it a smaller k × k square part, containing exactly k monsters and suggest it as a simplified version of the original map.You wonder how many ways there are to choose in the initial map a k × k (1 ≤ k ≤ n) square fragment, containing exactly k pudding monsters. Calculate this number. | 256 megabytes | //package zepto2015;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class F {
InputStream is;
PrintWriter out;
String INPUT = "";
long ret = 0;
void solve()
{
int n = ni();
int[] a = new int[n];
for(int i = 0;i < n;i++){
int x = ni()-1;
int y = ni()-1;
a[x] = y;
}
// tr(a);
ret = n;
dfs(0, n, a);
out.println(ret);
}
int[] z0 = new int[600001];
int[] z1max = new int[1200001];
int[] z1min = new int[1200001];
void dfs(int L, int R, int[] a)
{
if(R-L <= 1)return;
int H = (L+R)/2;
final int o1 = 600000;
// [L,H) [H,R)
int nl = H-L, nr = R-H;
int[] maxl = new int[nl];
int[] minl = new int[nl];
maxl[0] = minl[0] = a[H-1];
for(int i = 1;i < nl;i++){
maxl[i] = Math.max(maxl[i-1], a[H-1-i]);
minl[i] = Math.min(minl[i-1], a[H-1-i]);
}
int[] maxr = new int[nr];
int[] minr = new int[nr];
maxr[0] = minr[0] = a[H];
for(int i = 1;i < nr;i++){
maxr[i] = Math.max(maxr[i-1], a[H+i]);
minr[i] = Math.min(minr[i-1], a[H+i]);
}
// tr(minl, maxl);
// tr(minr, maxr);
// 0 3 1 2 4
// [1,3]
// # [2,3]
// [1,4]
// [0,4]
// [0,3]
int pmax = nr, pmin = nr;
for(int i = nl-1;i >= 0;i--){
int op0 = Math.max(pmax, pmin);
while(pmax > 0 && maxr[pmax-1] > maxl[i]){
if(pmax > pmin){
z1min[minr[pmax-1]+(H+pmax-1)+o1]--; // -pmin
}else{
z1max[maxr[pmax-1]-(H+pmax-1)+o1]++; // +pmax
}
pmax--;
}
while(pmin > 0 && minr[pmin-1] < minl[i]){
if(pmin > pmax){
z1max[maxr[pmin-1]-(H+pmin-1)+o1]--; // -pmax
}else{
z1min[minr[pmin-1]+(H+pmin-1)+o1]++; // +pmin
}
pmin--;
}
int p0 = Math.max(pmin, pmax);
while(op0 > p0){
op0--;
z0[maxr[op0]-minr[op0]-(H+op0)+300000]++;
}
// tr(pmin, pmax, p0, op0);
// trnz(z0);
// trnz(z1max);
// trnz(z1min);
int l = H-1-i;
// zone 0
ret += z0[-l+300000];
// tr("z0", z0[-l+300000]);
//
// zone 2
int q0 = H+Math.min(pmin, pmax);
// maxl[i]-minl[i]=k-i
int col = maxl[i]-minl[i]+l;
// tr("z2", H, col, q0);
if(H <= col && col < q0)ret++;
// zone 1
if(pmin > pmax){
// k+r-minl=r-l
// k=minl-l
ret += z1max[minl[i]-l+o1];
// tr("z1max", z1max[minl[i]-l+o1]);
}else{
// maxl-(k-r)=r-l
// k=maxl+l
ret += z1min[maxl[i]+l+o1];
// tr("z1min", z1min[maxl[i]+l+o1]);
}
// tr("ret",ret);
}
//
int p0 = Math.max(pmin, pmax);
while(p0 < nr){
z0[maxr[p0]-minr[p0]-(H+p0)+300000]--;
p0++;
}
while(pmax > pmin){
z1min[minr[pmax-1]+(H+pmax-1)+o1]--; // -pmin
pmax--;
}
while(pmin > pmax){
z1max[maxr[pmin-1]-(H+pmin-1)+o1]--; // -pmax
pmin--;
}
// for(int i = 0;i < z0.length;i++)assert z0[i] == 0;
// for(int i = 0;i < z1min.length;i++)assert z1min[i] == 0;
// for(int i = 0;i < z1max.length;i++)assert z1max[i] == 0;
// tr(L, H, R, ret);
dfs(L, H, a);
dfs(H, R, a);
}
public static void trnz(int[] o)
{
for(int i = 0;i < o.length;i++)if(o[i] != 0)System.out.print(i+":"+o[i]+" ");
System.out.println();
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new F().run(); }
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| Java | ["5\n1 1\n4 3\n3 2\n2 4\n5 5"] | 2 seconds | ["10"] | null | Java 7 | standard input | [
"data structures",
"divide and conquer"
] | dfaeae0bd55670f562d34e3bf2a65522 | The first line contains a single integer n (1 ≤ n ≤ 3 × 105) — the size of the initial field. Next n lines contain the coordinates of the cells initially containing monsters. The i-th of the next lines contains two numbers ri, ci (1 ≤ ri, ci ≤ n) — the row number and the column number of the cell that initially contains the i-th monster. It is guaranteed that all ri are distinct numbers and all ci are distinct numbers. | 3,000 | Print the number of distinct square fragments of the original field that can form a new map. | standard output | |
PASSED | 0fdae867f95b5d4ee79a6877e6471548 | train_002.jsonl | 1473784500 | Sonya was unable to think of a story for this problem, so here comes the formal description.You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0. | 256 megabytes | import java.io.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
public class test1 {
public static void main(String[] args) throws IOException {
MScanner s = new MScanner(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// creating max Heap
PriorityQueue<Integer> queue = new PriorityQueue<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (o1.equals(o2))
return 0;
return o1.compareTo(o2) > 0 ? -1 : 1;
}
});
// number of input
int n = s.nextInt();
long ans = 0;
for (int i = 1; i <= n; i++) {
int x = s.nextInt() - i;
queue.add(x);
if (x < queue.peek()) {
ans += queue.poll() - x;
queue.add(x);
}
}
out.println(ans);
out.flush();
out.close();
}
static class MScanner {
StringTokenizer st;
BufferedReader br;
public MScanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public MScanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int[] intArr(int n) throws IOException {
int[] in = new int[n];
for (int i = 0; i < n; i++)
in[i] = nextInt();
return in;
}
public long[] longArr(int n) throws IOException {
long[] in = new long[n];
for (int i = 0; i < n; i++)
in[i] = nextLong();
return in;
}
public int[] intSortedArr(int n) throws IOException {
int[] in = new int[n];
for (int i = 0; i < n; i++)
in[i] = nextInt();
shuffle(in);
Arrays.sort(in);
return in;
}
public long[] longSortedArr(int n) throws IOException {
long[] in = new long[n];
for (int i = 0; i < n; i++)
in[i] = nextLong();
shuffle(in);
Arrays.sort(in);
return in;
}
public Integer[] IntegerArr(int n) throws IOException {
Integer[] in = new Integer[n];
for (int i = 0; i < n; i++)
in[i] = nextInt();
return in;
}
public Long[] LongArr(int n) throws IOException {
Long[] in = new Long[n];
for (int i = 0; i < n; i++)
in[i] = nextLong();
return in;
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
static void shuffle(int[] in) {
for (int i = 0; i < in.length; i++) {
int idx = (int) (Math.random() * in.length);
int tmp = in[i];
in[i] = in[idx];
in[idx] = tmp;
}
}
static void shuffle(long[] in) {
for (int i = 0; i < in.length; i++) {
int idx = (int) (Math.random() * in.length);
long tmp = in[i];
in[i] = in[idx];
in[idx] = tmp;
}
}
} | Java | ["7\n2 1 5 11 5 9 11", "5\n5 4 3 2 1"] | 5 seconds | ["9", "12"] | NoteIn the first sample, the array is going to look as follows:2 3 5 6 7 9 11|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9And for the second sample:1 2 3 4 5|5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12 | Java 11 | standard input | [
"dp",
"sortings"
] | 516ed4dbe4da9883c88888b134d6621f | The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array. Next line contains n integer ai (1 ≤ ai ≤ 109). | 2,300 | Print the minimum number of operation required to make the array strictly increasing. | standard output | |
PASSED | bd916867f218879aa646917eaa0d7b88 | train_002.jsonl | 1473784500 | Sonya was unable to think of a story for this problem, so here comes the formal description.You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf713c_2 {
public static void main(String[] args) throws IOException {
int n = ri(), a[] = ria(n);
long ans = 0;
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i = 0; i < n; ++i) {
pq.offer(-(a[i] -= i));
if (pq.peek() < -a[i]) {
ans += -a[i] - pq.poll();
pq.offer(-a[i]);
}
}
prln(ans);
close();
}
static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random __rand = new Random();
// references
// IBIG = 1e9 + 7
// IMAX ~= 2e9
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// math util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}
static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int fli(double d) {return (int)d;}
static int cei(double d) {return (int)ceil(d);}
static long fll(double d) {return (long)d;}
static long cel(double d) {return (long)ceil(d);}
static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
static int lcm(int a, int b) {return a * b / gcf(a, b);}
static long lcm(long a, long b) {return a * b / gcf(a, b);}
static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
static long hash(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
// array util
static void reverse(int[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(long[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(double[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(char[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(double[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static void rsort(double[] a) {shuffle(a); sort(a);}
static int[] copy(int[] a) {int[] ans = new int[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static long[] copy(long[] a) {long[] ans = new long[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static double[] copy(double[] a) {double[] ans = new double[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static char[] copy(char[] a) {char[] ans = new char[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
// graph util
static List<List<Integer>> g(int n) {List<List<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new ArrayList<>()); return g;}
static List<Set<Integer>> sg(int n) {List<Set<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new HashSet<>()); return g;}
static void c(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v); g.get(v).add(u);}
static void cto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v);}
static void dc(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v); g.get(v).remove(u);}
static void dcto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v);}
// input
static void r() throws IOException {input = new StringTokenizer(rline());}
static int ri() throws IOException {return Integer.parseInt(rline());}
static long rl() throws IOException {return Long.parseLong(rline());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for(int i = 0; i < n; ++i) a[i] = ni(); return a;}
static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for(int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for(int i = 0; i < n; ++i) a[i] = nl(); return a;}
static char[] rcha() throws IOException {return rline().toCharArray();}
static String rline() throws IOException {return __in.readLine();}
static String n() {return input.nextToken();}
static int rni() throws IOException {r(); return ni();}
static int ni() {return Integer.parseInt(n());}
static long rnl() throws IOException {r(); return nl();}
static long nl() {return Long.parseLong(n());}
static double rnd() throws IOException {r(); return nd();}
static double nd() {return Double.parseDouble(n());}
static List<List<Integer>> rg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}
static void rg(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}
static List<List<Integer>> rdg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}
static void rdg(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}
static List<Set<Integer>> rsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}
static void rsg(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}
static List<Set<Integer>> rdsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}
static void rdsg(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}
// output
static void pr(int i) {__out.print(i);}
static void prln(int i) {__out.println(i);}
static void pr(long l) {__out.print(l);}
static void prln(long l) {__out.println(l);}
static void pr(double d) {__out.print(d);}
static void prln(double d) {__out.println(d);}
static void pr(char c) {__out.print(c);}
static void prln(char c) {__out.println(c);}
static void pr(char[] s) {__out.print(new String(s));}
static void prln(char[] s) {__out.println(new String(s));}
static void pr(String s) {__out.print(s);}
static void prln(String s) {__out.println(s);}
static void pr(Object o) {__out.print(o);}
static void prln(Object o) {__out.println(o);}
static void prln() {__out.println();}
static void pryes() {__out.println("yes");}
static void pry() {__out.println("Yes");}
static void prY() {__out.println("YES");}
static void prno() {__out.println("no");}
static void prn() {__out.println("No");}
static void prN() {__out.println("NO");}
static void pryesno(boolean b) {__out.println(b ? "yes" : "no");};
static void pryn(boolean b) {__out.println(b ? "Yes" : "No");}
static void prYN(boolean b) {__out.println(b ? "YES" : "NO");}
static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static void prln(double... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next()); else __out.println();}
static void h() {__out.println("hlfd"); flush();}
static void flush() {__out.flush();}
static void close() {__out.close();}
} | Java | ["7\n2 1 5 11 5 9 11", "5\n5 4 3 2 1"] | 5 seconds | ["9", "12"] | NoteIn the first sample, the array is going to look as follows:2 3 5 6 7 9 11|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9And for the second sample:1 2 3 4 5|5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12 | Java 11 | standard input | [
"dp",
"sortings"
] | 516ed4dbe4da9883c88888b134d6621f | The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array. Next line contains n integer ai (1 ≤ ai ≤ 109). | 2,300 | Print the minimum number of operation required to make the array strictly increasing. | standard output | |
PASSED | aaf56b0480bab03d689d244531f0c209 | train_002.jsonl | 1473784500 | Sonya was unable to think of a story for this problem, so here comes the formal description.You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf713c_3 {
public static void main(String[] args) throws IOException {
long n = ri(), a[] = rla((int) n), ans = 0;
PriorityQueue<Long> pq = new PriorityQueue<>();
for (int i = 0; i < n; ++i) {
pq.offer(-(a[i] -= i));
pq.offer(-a[i]);
ans += -a[i] - pq.poll();
}
prln(ans);
close();
}
static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random __rand = new Random();
// references
// IBIG = 1e9 + 7
// IMAX ~= 2e9
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// math util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}
static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int fli(double d) {return (int)d;}
static int cei(double d) {return (int)ceil(d);}
static long fll(double d) {return (long)d;}
static long cel(double d) {return (long)ceil(d);}
static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
static int lcm(int a, int b) {return a * b / gcf(a, b);}
static long lcm(long a, long b) {return a * b / gcf(a, b);}
static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
static long hash(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
// array util
static void reverse(int[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(long[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(double[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(char[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(double[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static void rsort(double[] a) {shuffle(a); sort(a);}
static int[] copy(int[] a) {int[] ans = new int[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static long[] copy(long[] a) {long[] ans = new long[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static double[] copy(double[] a) {double[] ans = new double[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static char[] copy(char[] a) {char[] ans = new char[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
// graph util
static List<List<Integer>> g(int n) {List<List<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new ArrayList<>()); return g;}
static List<Set<Integer>> sg(int n) {List<Set<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new HashSet<>()); return g;}
static void c(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v); g.get(v).add(u);}
static void cto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v);}
static void dc(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v); g.get(v).remove(u);}
static void dcto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v);}
// input
static void r() throws IOException {input = new StringTokenizer(rline());}
static int ri() throws IOException {return Integer.parseInt(rline());}
static long rl() throws IOException {return Long.parseLong(rline());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for(int i = 0; i < n; ++i) a[i] = ni(); return a;}
static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for(int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for(int i = 0; i < n; ++i) a[i] = nl(); return a;}
static char[] rcha() throws IOException {return rline().toCharArray();}
static String rline() throws IOException {return __in.readLine();}
static String n() {return input.nextToken();}
static int rni() throws IOException {r(); return ni();}
static int ni() {return Integer.parseInt(n());}
static long rnl() throws IOException {r(); return nl();}
static long nl() {return Long.parseLong(n());}
static double rnd() throws IOException {r(); return nd();}
static double nd() {return Double.parseDouble(n());}
static List<List<Integer>> rg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}
static void rg(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}
static List<List<Integer>> rdg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}
static void rdg(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}
static List<Set<Integer>> rsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}
static void rsg(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}
static List<Set<Integer>> rdsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}
static void rdsg(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}
// output
static void pr(int i) {__out.print(i);}
static void prln(int i) {__out.println(i);}
static void pr(long l) {__out.print(l);}
static void prln(long l) {__out.println(l);}
static void pr(double d) {__out.print(d);}
static void prln(double d) {__out.println(d);}
static void pr(char c) {__out.print(c);}
static void prln(char c) {__out.println(c);}
static void pr(char[] s) {__out.print(new String(s));}
static void prln(char[] s) {__out.println(new String(s));}
static void pr(String s) {__out.print(s);}
static void prln(String s) {__out.println(s);}
static void pr(Object o) {__out.print(o);}
static void prln(Object o) {__out.println(o);}
static void prln() {__out.println();}
static void pryes() {__out.println("yes");}
static void pry() {__out.println("Yes");}
static void prY() {__out.println("YES");}
static void prno() {__out.println("no");}
static void prn() {__out.println("No");}
static void prN() {__out.println("NO");}
static void pryesno(boolean b) {__out.println(b ? "yes" : "no");};
static void pryn(boolean b) {__out.println(b ? "Yes" : "No");}
static void prYN(boolean b) {__out.println(b ? "YES" : "NO");}
static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static void prln(double... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next()); else __out.println();}
static void h() {__out.println("hlfd"); flush();}
static void flush() {__out.flush();}
static void close() {__out.close();}
} | Java | ["7\n2 1 5 11 5 9 11", "5\n5 4 3 2 1"] | 5 seconds | ["9", "12"] | NoteIn the first sample, the array is going to look as follows:2 3 5 6 7 9 11|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9And for the second sample:1 2 3 4 5|5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12 | Java 11 | standard input | [
"dp",
"sortings"
] | 516ed4dbe4da9883c88888b134d6621f | The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array. Next line contains n integer ai (1 ≤ ai ≤ 109). | 2,300 | Print the minimum number of operation required to make the array strictly increasing. | standard output | |
PASSED | ef2a791763ccfa0474eb6e7db798ba5c | train_002.jsonl | 1473784500 | Sonya was unable to think of a story for this problem, so here comes the formal description.You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0. | 256 megabytes | import java.util.*;
public class SlopeTrick {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
// int e=sc.nextInt();
// while(e-->0) {
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
long ans=0;
// Arrays.sort(arr);
int t=arr[0];
PriorityQueue<Integer> p=new PriorityQueue<Integer>(Collections.reverseOrder());
p.add(t);
for(int i=1;i<n;i++) {
t=arr[i];
t=t-i;
p.add(t);
if(p.peek()>=t) {
ans=ans+p.peek()-t;
p.poll();
p.add(t);
}
}
System.out.println(ans);
// }
}
}
| Java | ["7\n2 1 5 11 5 9 11", "5\n5 4 3 2 1"] | 5 seconds | ["9", "12"] | NoteIn the first sample, the array is going to look as follows:2 3 5 6 7 9 11|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9And for the second sample:1 2 3 4 5|5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12 | Java 11 | standard input | [
"dp",
"sortings"
] | 516ed4dbe4da9883c88888b134d6621f | The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array. Next line contains n integer ai (1 ≤ ai ≤ 109). | 2,300 | Print the minimum number of operation required to make the array strictly increasing. | standard output | |
PASSED | 7c18cbacf4daccbc1fa54144fd99afa0 | train_002.jsonl | 1473784500 | Sonya was unable to think of a story for this problem, so here comes the formal description.You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.PriorityQueue;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.AbstractQueue;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Comparator;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Rustam Musin (t.me/musin_acm)
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
CSonyaIZadachaBezLegendi solver = new CSonyaIZadachaBezLegendi();
solver.solve(1, in, out);
out.close();
}
static class CSonyaIZadachaBezLegendi {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int[] a = in.readIntArray(n);
for (int i = 0; i < n; i++) a[i] -= i;
CSonyaIZadachaBezLegendi.Slope slope = new CSonyaIZadachaBezLegendi.Slope();
for (int x : a) {
slope.add(CSonyaIZadachaBezLegendi.Slope.modFunction(x));
slope.makeConst();
}
long answer = slope.lastConst;
out.print(answer);
}
static class Slope {
long lastK;
long lastConst;
PriorityQueue<Long> changingPoints = new PriorityQueue<>(Comparator.reverseOrder());
Slope(long lastK, long lastConst) {
this.lastK = lastK;
this.lastConst = lastConst;
}
Slope() {
this(0, 0);
}
static CSonyaIZadachaBezLegendi.Slope modFunction(long middle) {
CSonyaIZadachaBezLegendi.Slope res = new CSonyaIZadachaBezLegendi.Slope(1, -middle);
res.changingPoints.add(middle);
res.changingPoints.add(middle);
return res;
}
void add(CSonyaIZadachaBezLegendi.Slope other) {
changingPoints.addAll(other.changingPoints);
lastK += other.lastK;
lastConst += other.lastConst;
}
void makeConst() {
while (lastK != 0) {
dropLast();
}
}
void dropLast() {
long p = changingPoints.poll();
long newF = p * lastK + lastConst;
long newK = lastK - 1;
long newConst = newF - p * newK;
lastK = newK;
lastConst = newConst;
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = readInt();
}
return array;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
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) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void print(long i) {
writer.print(i);
}
}
}
| Java | ["7\n2 1 5 11 5 9 11", "5\n5 4 3 2 1"] | 5 seconds | ["9", "12"] | NoteIn the first sample, the array is going to look as follows:2 3 5 6 7 9 11|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9And for the second sample:1 2 3 4 5|5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12 | Java 11 | standard input | [
"dp",
"sortings"
] | 516ed4dbe4da9883c88888b134d6621f | The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array. Next line contains n integer ai (1 ≤ ai ≤ 109). | 2,300 | Print the minimum number of operation required to make the array strictly increasing. | standard output | |
PASSED | c74524be17c7b038e2510720e50c16d8 | train_002.jsonl | 1326380700 | Autumn came late to the kingdom of Far Far Away. The harvest was exuberant and it is now time to get ready for the winter. As most people celebrate the Harvest festival, Simon the Caretaker tries to solve a very non-trivial task of how to find place for the agricultural equipment in the warehouse.He's got problems with some particularly large piece of equipment, which is, of course, turboplows. The problem is that when a turboplow is stored, it takes up not some simply rectangular space. It takes up a T-shaped space like on one of the four pictures below (here character "#" stands for the space occupied by the turboplow and character "." stands for the free space): ### ..# .#. #...#. ### .#. ###.#. ..# ### #.. Simon faced a quite natural challenge: placing in the given n × m cells warehouse the maximum number of turboplows. As one stores the turboplows, he can rotate them in any manner (so that they take up the space like on one of the four pictures above). However, two turboplows cannot "overlap", that is, they cannot share the same cell in the warehouse.Simon feels that he alone cannot find the optimal way of positioning the plugs in the warehouse that would maximize their quantity. Can you help him? | 256 megabytes | import java.util.*;
public class cf142c {
static int n,m;
static byte[][][] memo;
static byte[][][] move;
static char[][] board;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
memo = new byte[n][m][(1<<(2*m+1))];
move = new byte[n][m][(1<<(2*m+1))];
board = new char[n][m];
for(byte[][] x : memo)
for(byte[] y : x)
Arrays.fill(y,(byte)-1);
for(char[] x : board)
Arrays.fill(x,'.');
short ans = go(0,0,0);
build(0,0,0,0);
System.out.println(ans);
for(int i=0; i<n; i++)
System.out.println(new String(board[i]));
}
static void build(int r, int c, int mask, int level) {
if(c==m) {
build(r+1,0,mask,level);
return;
}
if(r==n) {
return;
}
if(move[r][c][mask] == 0) {
build(r,c+1,mask>>1,level);
return;
}
// #
// ###
// #
if(move[r][c][mask] == 1) {
int nmask = set(set(set(set(set(mask,0),m),m+1),m+2),2*m);
board[r][c] = board[r+1][c] = board[r+1][c+1] = board[r+1][c+2] = board[r+2][c] = (char)('A'+level);
build(r,c+2,nmask>>2,level+1);
return;
}
// ###
// #
// #
if(move[r][c][mask] == 2) {
int nmask = set(set(set(set(set(mask,0),1),2),m+1),2*m+1);
board[r][c] = board[r][c+1] = board[r][c+2] = board[r+1][c+1] = board[r+2][c+1] = (char)('A'+level);
build(r,c+2,nmask>>2,level+1);
return;
}
// #
// ###
// #
if(move[r][c][mask] == 3) {
int nmask = set(set(set(set(set(mask,2),m),m+1),m+2),2*m+2);
board[r][c+2] = board[r+1][c] = board[r+1][c+1] = board[r+1][c+2] = board[r+2][c+2] = (char)('A'+level);
build(r,c+2,nmask>>2,level+1);
return;
}
// #
// #
// ###
if(move[r][c][mask] == 4) {
int nmask = set(set(set(set(set(mask,1),m+1),2*m),2*m+1),2*m+2);
board[r][c+1] = board[r+1][c+1] = board[r+2][c] = board[r+2][c+1] = board[r+2][c+2] = (char)('A'+level);
build(r,c+2,nmask>>2,level+1);
return;
}
}
static byte go(int r, int c, int mask) {
if(c==m)
return go(r+1,0,mask);
if(r==n)
return 0;
if(memo[r][c][mask] != -1) return memo[r][c][mask];
byte best = -1;
int bestMove = -1;
if(r < n-2 && c < m-2) {
// #
// ###
// #
if(!isOn(mask,0) && !isOn(mask,m) && !isOn(mask,m+1) && !isOn(mask,m+2) && !isOn(mask,2*m)) {
int nmask = set(set(set(set(set(mask,0),m),m+1),m+2),2*m);
byte score = (byte)(1+go(r,c+2,nmask>>2));
if(score > best) {
best = score;
bestMove = 1;
}
}
// ###
// #
// #
if(!isOn(mask,0) && !isOn(mask,1) && !isOn(mask,2) && !isOn(mask,m+1) && !isOn(mask,2*m+1)) {
int nmask = set(set(set(set(set(mask,0),1),2),m+1),2*m+1);
byte score = (byte) (1+go(r,c+2,nmask>>2));
if(score > best) {
best = score;
bestMove = 2;
}
}
// #
// ###
// #
if(!isOn(mask,2) && !isOn(mask,m) && !isOn(mask,m+1) && !isOn(mask,m+2) && !isOn(mask,2*m+2)) {
int nmask = set(set(set(set(set(mask,2),m),m+1),m+2),2*m+2);
byte score = (byte) (1+go(r,c+2,nmask>>2));
if(score > best) {
best = score;
bestMove = 3;
}
}
// #
// #
// ###
if(!isOn(mask,1) && !isOn(mask,m+1) && !isOn(mask,2*m) && !isOn(mask,2*m+1) && !isOn(mask,2*m+2)) {
int nmask = set(set(set(set(set(mask,1),m+1),2*m),2*m+1),2*m+2);
byte score = (byte) (1+go(r,c+2,nmask>>2));
if(score > best) {
best = score;
bestMove = 4;
}
}
}
byte score = go(r,c+1,mask>>1);
if(score > best) {
best = score;
bestMove = 0;
}
move[r][c][mask] = (byte) bestMove;
return memo[r][c][mask] = best;
}
static boolean isOn(int x, int p) {
return (x&(1<<p)) != 0;
}
static int set(int x, int p) {
return x|(1<<p);
}
}
| Java | ["3 3", "5 6", "2 2"] | 3 seconds | ["1\nAAA\n.A.\n.A.", "4\nA..C..\nAAAC..\nABCCCD\n.B.DDD\nBBB..D", "0\n..\n.."] | null | Java 6 | standard input | [] | f29eba31c20246686b8427b7aeadd184 | The only line contains two space-separated integers n and m — the sizes of the warehouse (1 ≤ n, m ≤ 9). | 2,300 | In the first line print the maximum number of turboplows that can be positioned in the warehouse. In each of the next n lines print m characters. Use "." (dot) to mark empty space and use successive capital Latin letters ("A" for the first turboplow, "B" for the second one and so on until you reach the number of turboplows in your scheme) to mark place for the corresponding turboplows considering that they are positioned in the optimal manner in the warehouse. The order in which you number places for the turboplows does not matter. If there are several optimal solutions for a warehouse of the given size, print any of them. | standard output | |
PASSED | 360738bf175074394f860d89ab42ce7a | train_002.jsonl | 1326380700 | Autumn came late to the kingdom of Far Far Away. The harvest was exuberant and it is now time to get ready for the winter. As most people celebrate the Harvest festival, Simon the Caretaker tries to solve a very non-trivial task of how to find place for the agricultural equipment in the warehouse.He's got problems with some particularly large piece of equipment, which is, of course, turboplows. The problem is that when a turboplow is stored, it takes up not some simply rectangular space. It takes up a T-shaped space like on one of the four pictures below (here character "#" stands for the space occupied by the turboplow and character "." stands for the free space): ### ..# .#. #...#. ### .#. ###.#. ..# ### #.. Simon faced a quite natural challenge: placing in the given n × m cells warehouse the maximum number of turboplows. As one stores the turboplows, he can rotate them in any manner (so that they take up the space like on one of the four pictures above). However, two turboplows cannot "overlap", that is, they cannot share the same cell in the warehouse.Simon feels that he alone cannot find the optimal way of positioning the plugs in the warehouse that would maximize their quantity. Can you help him? | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
int m, n, kq;
char[][] ans;
char[][] out;
char[][][] p = new char[4][3][3];
final public void solve() throws IOException {
m = nextInt();
n = nextInt();
ans = new char[m][n];
out = new char[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
ans[i][j] = '.';
p[0][0][0] = '#';
p[0][0][1] = '#';
p[0][0][2] = '#';
p[0][1][0] = '.';
p[0][1][1] = '#';
p[0][1][2] = '.';
p[0][2][0] = '.';
p[0][2][1] = '#';
p[0][2][2] = '.';
for (int k = 1; k < 4; k++)
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
p[k][i][j] = p[k - 1][2 - j][i];
kq = -1;
go(0, 0, 0);
writer.println(kq);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
writer.print(out[i][j]);
writer.println();
}
}
private void go(int i, int j, int cnt) {
if (i >= m - 2) {
if (cnt > kq) {
kq = cnt;
for (int x = 0; x < m; x++)
for (int y = 0; y < n; y++)
out[x][y] = ans[x][y];
}
return;
}
if (j >= n - 2) {
go(i + 1, 0, cnt);
return;
}
boolean has = false;
for (int k = 0; k < 4; k++) {
boolean ok = true;
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
int curx = i + x;
int cury = j + y;
if (curx >= m || cury >= n || (p[k][x][y] != '.' && ans[curx][cury] != '.')) {
ok = false;
break;
}
}
if (!ok) break;
}
if (ok) {
has = true;
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
if (p[k][x][y] == '#') {
int curx = i + x;
int cury = j + y;
ans[curx][cury] = (char)('A' + cnt);
}
go(i, j + 1, cnt + 1);
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
if (p[k][x][y] == '#') {
int curx = i + x;
int cury = j + y;
ans[curx][cury] = '.';
}
}
}
if (!has) go(i, j + 1, cnt);
else
if (Math.random() < 0.6) go(i, j + 1, cnt);
}
public static void main(String[] args) throws FileNotFoundException {
new C().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
final public void run() {
try {
long tbegin = System.currentTimeMillis();
reader = new BufferedReader(new InputStreamReader(System.in));
//reader = new BufferedReader(new InputStreamReader(new FileInputStream("fselect.inp")));
tokenizer = null;
writer = new PrintWriter(new OutputStreamWriter(System.out));
//writer = new PrintWriter(new FileOutputStream("fselect.out"));
solve();
//reader.close();
//System.out.println(System.currentTimeMillis() - tbegin + "ms...");
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
final int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
final long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
final double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
final String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
| Java | ["3 3", "5 6", "2 2"] | 3 seconds | ["1\nAAA\n.A.\n.A.", "4\nA..C..\nAAAC..\nABCCCD\n.B.DDD\nBBB..D", "0\n..\n.."] | null | Java 6 | standard input | [] | f29eba31c20246686b8427b7aeadd184 | The only line contains two space-separated integers n and m — the sizes of the warehouse (1 ≤ n, m ≤ 9). | 2,300 | In the first line print the maximum number of turboplows that can be positioned in the warehouse. In each of the next n lines print m characters. Use "." (dot) to mark empty space and use successive capital Latin letters ("A" for the first turboplow, "B" for the second one and so on until you reach the number of turboplows in your scheme) to mark place for the corresponding turboplows considering that they are positioned in the optimal manner in the warehouse. The order in which you number places for the turboplows does not matter. If there are several optimal solutions for a warehouse of the given size, print any of them. | standard output | |
PASSED | 20ca24eab07b63ce587391b86287cf1c | train_002.jsonl | 1326380700 | Autumn came late to the kingdom of Far Far Away. The harvest was exuberant and it is now time to get ready for the winter. As most people celebrate the Harvest festival, Simon the Caretaker tries to solve a very non-trivial task of how to find place for the agricultural equipment in the warehouse.He's got problems with some particularly large piece of equipment, which is, of course, turboplows. The problem is that when a turboplow is stored, it takes up not some simply rectangular space. It takes up a T-shaped space like on one of the four pictures below (here character "#" stands for the space occupied by the turboplow and character "." stands for the free space): ### ..# .#. #...#. ### .#. ###.#. ..# ### #.. Simon faced a quite natural challenge: placing in the given n × m cells warehouse the maximum number of turboplows. As one stores the turboplows, he can rotate them in any manner (so that they take up the space like on one of the four pictures above). However, two turboplows cannot "overlap", that is, they cannot share the same cell in the warehouse.Simon feels that he alone cannot find the optimal way of positioning the plugs in the warehouse that would maximize their quantity. Can you help him? | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class Main implements Runnable{
FastScanner sc;
BufferedReader reader;
PrintWriter out;
int n;
int m;
int ans = 0;
int col = 0;
int iter = 0;
int[][] b;
int[][] a;
int[][] dx = {{-1,-1,-1, 1}, {-1, 0, 0, 1}, {-1, 1, 1, 1}, {-1, 0, 0, 1}};
int[][] dy = {{-1, 0, 1, 0}, { 1,-1, 1, 1}, { 0,-1, 0, 1}, {-1,-1, 1,-1}};
boolean check(int x, int y, int z) {
for (int i = 0;i < 4; ++ i) {
if (a[x + dx[z][i]][y + dy[z][i]] != -1) {
return false;
}
}
return true;
}
void fill(int x, int y, int z) {
a[x][y] = z;
col ++;
for (int i = 0;i < 4; ++ i) {
a[x + dx[z][i]][y + dy[z][i]] = -2;
}
}
void clear(int x, int y, int z) {
a[x][y] = -1;
col --;
for (int i = 0;i < 4; ++ i) {
a[x + dx[z][i]][y + dy[z][i]] = -1;
}
}
void dfs() {
++ iter;
if (iter > 50000000)
return;
boolean flag = false;
for (int i = 1;i < n - 1; ++ i) {
for (int j = 1;j < m - 1; ++ j) {
if (a[i][j] == -1) {
for (int z = 0;z < 4; ++ z) {
if (check(i,j,z)) {
flag = true;
fill(i,j,z);
dfs();
clear(i,j,z);
}
}
}
}
}
if (!flag) {
if (col > ans) {
ans = col;
for (int i = 0;i < n; ++ i) {
for (int j = 0;j < m; ++ j) {
b[i][j] = a[i][j];
}
}
}
}
}
void solve() throws IOException {
preCalc(sc.nextInt(), sc.nextInt());
/*for (n = 1; n < 10; ++ n) {
for (m = 1; m < 10; ++ m) {
a = new int[n][m];
b = new int[n][m];
iter = 0;
ans = 0;
col = 0;
for (int i = 0; i < n; ++ i) {
for (int j = 0;j < m; ++ j) {
a[i][j] = -1;
b[i][j] = -1;
}
}
dfs();
int ch = 'A';
for (int i = 0; i < n; ++ i) {
for (int j = 0;j < m; ++ j) {
if (b[i][j] >= 0 && b[i][j] < 4 && i > 0 && i < n-1 && j > 0 && j < m - 1) {
for (int ii = 0;ii < 4; ++ ii) {
b[i + dx[b[i][j]][ii]][j + dy[b[i][j]][ii]] = ch;
}
b[i][j] = ch;
ch ++;
}
if (b[i][j] == -1) {
b[i][j] = '.';
}
}
}
out.println("if (n == " + n + " && m == " + m + ") {");
out.println("out.println(\"" + ans + "\");");
for (int i = 0; i < n; ++ i) {
out.print("out.println(\"");
for (int j = 0;j < m; ++ j) {
out.print((char)b[i][j]);
}
out.println("\");");
}
out.println("}");
out.flush();
}
}*/
}
void preCalc(int n, int m) {
if (n == 1 && m == 1) {
out.println("0");
out.println(".");
}
if (n == 1 && m == 2) {
out.println("0");
out.println("..");
}
if (n == 1 && m == 3) {
out.println("0");
out.println("...");
}
if (n == 1 && m == 4) {
out.println("0");
out.println("....");
}
if (n == 1 && m == 5) {
out.println("0");
out.println(".....");
}
if (n == 1 && m == 6) {
out.println("0");
out.println("......");
}
if (n == 1 && m == 7) {
out.println("0");
out.println(".......");
}
if (n == 1 && m == 8) {
out.println("0");
out.println("........");
}
if (n == 1 && m == 9) {
out.println("0");
out.println(".........");
}
if (n == 2 && m == 1) {
out.println("0");
out.println(".");
out.println(".");
}
if (n == 2 && m == 2) {
out.println("0");
out.println("..");
out.println("..");
}
if (n == 2 && m == 3) {
out.println("0");
out.println("...");
out.println("...");
}
if (n == 2 && m == 4) {
out.println("0");
out.println("....");
out.println("....");
}
if (n == 2 && m == 5) {
out.println("0");
out.println(".....");
out.println(".....");
}
if (n == 2 && m == 6) {
out.println("0");
out.println("......");
out.println("......");
}
if (n == 2 && m == 7) {
out.println("0");
out.println(".......");
out.println(".......");
}
if (n == 2 && m == 8) {
out.println("0");
out.println("........");
out.println("........");
}
if (n == 2 && m == 9) {
out.println("0");
out.println(".........");
out.println(".........");
}
if (n == 3 && m == 1) {
out.println("0");
out.println(".");
out.println(".");
out.println(".");
}
if (n == 3 && m == 2) {
out.println("0");
out.println("..");
out.println("..");
out.println("..");
}
if (n == 3 && m == 3) {
out.println("1");
out.println("AAA");
out.println(".A.");
out.println(".A.");
}
if (n == 3 && m == 4) {
out.println("1");
out.println("AAA.");
out.println(".A..");
out.println(".A..");
}
if (n == 3 && m == 5) {
out.println("2");
out.println("AAA.B");
out.println(".ABBB");
out.println(".A..B");
}
if (n == 3 && m == 6) {
out.println("2");
out.println("AAA.B.");
out.println(".ABBB.");
out.println(".A..B.");
}
if (n == 3 && m == 7) {
out.println("3");
out.println("AAABCCC");
out.println(".A.B.C.");
out.println(".ABBBC.");
}
if (n == 3 && m == 8) {
out.println("3");
out.println("AAA.BCCC");
out.println(".ABBB.C.");
out.println(".A..B.C.");
}
if (n == 3 && m == 9) {
out.println("4");
out.println("AAABCCC.D");
out.println(".A.B.CDDD");
out.println(".ABBBC..D");
}
if (n == 4 && m == 1) {
out.println("0");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
}
if (n == 4 && m == 2) {
out.println("0");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
}
if (n == 4 && m == 3) {
out.println("1");
out.println("AAA");
out.println(".A.");
out.println(".A.");
out.println("...");
}
if (n == 4 && m == 4) {
out.println("2");
out.println("AAA.");
out.println(".AB.");
out.println(".AB.");
out.println(".BBB");
}
if (n == 4 && m == 5) {
out.println("2");
out.println("AAA.B");
out.println(".ABBB");
out.println(".A..B");
out.println(".....");
}
if (n == 4 && m == 6) {
out.println("3");
out.println("AAABBB");
out.println(".AC.B.");
out.println(".AC.B.");
out.println(".CCC..");
}
if (n == 4 && m == 7) {
out.println("4");
out.println("AAABBB.");
out.println(".AC.BD.");
out.println(".AC.BD.");
out.println(".CCCDDD");
}
if (n == 4 && m == 8) {
out.println("4");
out.println("AAA.BCCC");
out.println(".ABBBDC.");
out.println(".A..BDC.");
out.println("....DDD.");
}
if (n == 4 && m == 9) {
out.println("5");
out.println("AAABBBCCC");
out.println(".AD.BE.C.");
out.println(".AD.BE.C.");
out.println(".DDDEEE..");
}
if (n == 5 && m == 1) {
out.println("0");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
}
if (n == 5 && m == 2) {
out.println("0");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
}
if (n == 5 && m == 3) {
out.println("2");
out.println("AAA");
out.println(".A.");
out.println(".AB");
out.println("BBB");
out.println("..B");
}
if (n == 5 && m == 4) {
out.println("2");
out.println("AAA.");
out.println(".AB.");
out.println(".AB.");
out.println(".BBB");
out.println("....");
}
if (n == 5 && m == 5) {
out.println("4");
out.println("AAA.B");
out.println(".ABBB");
out.println("CA.DB");
out.println("CCCD.");
out.println("C.DDD");
}
if (n == 5 && m == 6) {
out.println("4");
out.println("AAA.B.");
out.println(".ABBB.");
out.println(".AC.BD");
out.println("CCCDDD");
out.println("..C..D");
}
if (n == 5 && m == 7) {
out.println("5");
out.println("AAA.B..");
out.println(".ABBBC.");
out.println("DA.EBC.");
out.println("DDDECCC");
out.println("D.EEE..");
}
if (n == 5 && m == 8) {
out.println("6");
out.println("AAA.BCCC");
out.println(".ABBBDC.");
out.println("EA.FBDC.");
out.println("EEEFDDD.");
out.println("E.FFF...");
}
if (n == 5 && m == 9) {
out.println("7");
out.println("AAA.BCCC.");
out.println(".ABBBDC..");
out.println("EA.FBDCG.");
out.println("EEEFDDDG.");
out.println("E.FFF.GGG");
}
if (n == 6 && m == 1) {
out.println("0");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
}
if (n == 6 && m == 2) {
out.println("0");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
}
if (n == 6 && m == 3) {
out.println("2");
out.println("AAA");
out.println(".A.");
out.println(".AB");
out.println("BBB");
out.println("..B");
out.println("...");
}
if (n == 6 && m == 4) {
out.println("3");
out.println("AAA.");
out.println(".AB.");
out.println(".AB.");
out.println("CBBB");
out.println("CCC.");
out.println("C...");
}
if (n == 6 && m == 5) {
out.println("4");
out.println("AAA.B");
out.println(".ABBB");
out.println(".AC.B");
out.println("CCCD.");
out.println("..CD.");
out.println("..DDD");
}
if (n == 6 && m == 6) {
out.println("5");
out.println("AAA.B.");
out.println(".ABBB.");
out.println(".A.CB.");
out.println("DCCCE.");
out.println("DDDCE.");
out.println("D..EEE");
}
if (n == 6 && m == 7) {
out.println("6");
out.println("AAABCCC");
out.println(".A.B.C.");
out.println("DABBBCE");
out.println("DDDFEEE");
out.println("DFFF..E");
out.println("...F...");
}
if (n == 6 && m == 8) {
out.println("7");
out.println("AAA.BCCC");
out.println(".ABBBDC.");
out.println(".AE.BDC.");
out.println("EEEFDDDG");
out.println("..EF.GGG");
out.println("..FFF..G");
}
if (n == 6 && m == 9) {
out.println("8");
out.println("AAA.BCCC.");
out.println(".ABBB.C..");
out.println(".A.DBEC..");
out.println("FDDDGEEEH");
out.println("FFFDGEHHH");
out.println("F..GGG..H");
}
if (n == 7 && m == 1) {
out.println("0");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
}
if (n == 7 && m == 2) {
out.println("0");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
}
if (n == 7 && m == 3) {
out.println("3");
out.println("AAA");
out.println(".A.");
out.println(".AB");
out.println("BBB");
out.println(".CB");
out.println(".C.");
out.println("CCC");
}
if (n == 7 && m == 4) {
out.println("4");
out.println("AAA.");
out.println(".AB.");
out.println(".AB.");
out.println("CBBB");
out.println("CCCD");
out.println("CDDD");
out.println("...D");
}
if (n == 7 && m == 5) {
out.println("5");
out.println("AAA.B");
out.println(".ABBB");
out.println(".AC.B");
out.println("CCCD.");
out.println(".ECD.");
out.println(".EDDD");
out.println("EEE..");
}
if (n == 7 && m == 6) {
out.println("6");
out.println("AAA.B.");
out.println(".ABBB.");
out.println(".AC.BD");
out.println("CCCDDD");
out.println(".EC.FD");
out.println(".EFFF.");
out.println("EEE.F.");
}
if (n == 7 && m == 7) {
out.println("8");
out.println("AAABCCC");
out.println(".A.B.C.");
out.println("DABBBCE");
out.println("DDDFEEE");
out.println("DG.F.HE");
out.println(".GFFFH.");
out.println("GGG.HHH");
}
if (n == 7 && m == 8) {
out.println("9");
out.println("AAA.BBB.");
out.println(".ACCCBD.");
out.println(".AEC.BD.");
out.println("EEECFDDD");
out.println("G.EHFFFI");
out.println("GGGHFIII");
out.println("G.HHH..I");
}
if (n == 7 && m == 9) {
out.println("10");
out.println("AAA.BC...");
out.println(".ABBBCCCD");
out.println("EA.FBCDDD");
out.println("EEEF.GGGD");
out.println("EHFFFIGJ.");
out.println(".H.IIIGJ.");
out.println("HHH..IJJJ");
}
if (n == 8 && m == 1) {
out.println("0");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
}
if (n == 8 && m == 2) {
out.println("0");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
}
if (n == 8 && m == 3) {
out.println("3");
out.println("AAA");
out.println(".A.");
out.println(".AB");
out.println("BBB");
out.println(".CB");
out.println(".C.");
out.println("CCC");
out.println("...");
}
if (n == 8 && m == 4) {
out.println("4");
out.println("AAA.");
out.println(".AB.");
out.println(".AB.");
out.println("CBBB");
out.println("CCCD");
out.println("CDDD");
out.println("...D");
out.println("....");
}
if (n == 8 && m == 5) {
out.println("6");
out.println("AAA.B");
out.println(".ABBB");
out.println("CA.DB");
out.println("CCCD.");
out.println("CEDDD");
out.println(".EFFF");
out.println("EEEF.");
out.println("...F.");
}
if (n == 8 && m == 6) {
out.println("7");
out.println("AAA.B.");
out.println(".ABBB.");
out.println(".AC.BD");
out.println("CCCDDD");
out.println("..CE.D");
out.println("FEEEG.");
out.println("FFFEG.");
out.println("F..GGG");
}
if (n == 8 && m == 7) {
out.println("9");
out.println("AAAB..C");
out.println(".A.BCCC");
out.println("DABBBEC");
out.println("DDDEEE.");
out.println("DFFF.EG");
out.println(".HFIGGG");
out.println(".HFIIIG");
out.println("HHHI...");
}
if (n == 8 && m == 8) {
out.println("10");
out.println("AAA.BCCC");
out.println(".ABBBDC.");
out.println("EA.FBDC.");
out.println("EEEFDDDG");
out.println("EHFFFGGG");
out.println(".HIII.JG");
out.println("HHHIJJJ.");
out.println("...I..J.");
}
if (n == 8 && m == 9) {
out.println("12");
out.println("A.EEE.JJJ");
out.println("AAAEHHHJ.");
out.println("AB.EFHKJ.");
out.println(".BFFFHKKK");
out.println("BBBDFIK..");
out.println("CDDDGIIIL");
out.println("CCCDGILLL");
out.println("C..GGG..L");
}
if (n == 9 && m == 1) {
out.println("0");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
out.println(".");
}
if (n == 9 && m == 2) {
out.println("0");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
out.println("..");
}
if (n == 9 && m == 3) {
out.println("4");
out.println("AAA");
out.println(".A.");
out.println(".AB");
out.println("BBB");
out.println("C.B");
out.println("CCC");
out.println("C.D");
out.println("DDD");
out.println("..D");
}
if (n == 9 && m == 4) {
out.println("5");
out.println("AAA.");
out.println(".AB.");
out.println(".AB.");
out.println("CBBB");
out.println("CCCD");
out.println("CDDD");
out.println("EEED");
out.println(".E..");
out.println(".E..");
}
if (n == 9 && m == 5) {
out.println("7");
out.println("AAA.B");
out.println(".ABBB");
out.println("CA.DB");
out.println("CCCD.");
out.println("CEDDD");
out.println(".EEEF");
out.println("GEFFF");
out.println("GGG.F");
out.println("G....");
}
if (n == 9 && m == 6) {
out.println("8");
out.println("AAA.B.");
out.println(".ABBB.");
out.println(".AC.BD");
out.println("CCCDDD");
out.println("E.CF.D");
out.println("EEEF..");
out.println("EGFFFH");
out.println(".G.HHH");
out.println("GGG..H");
}
if (n == 9 && m == 7) {
out.println("10");
out.println("AAA.B..");
out.println(".ABBBC.");
out.println(".AD.BC.");
out.println("DDDECCC");
out.println("F.DEEEG");
out.println("FFFEGGG");
out.println("FHIIIJG");
out.println(".H.I.J.");
out.println("HHHIJJJ");
}
if (n == 9 && m == 8) {
out.println("12");
out.println("AAA.BCCC");
out.println(".ABBBDC.");
out.println("EA.FBDC.");
out.println("EEEFDDDG");
out.println("EHFFFGGG");
out.println(".HHHIIIG");
out.println("JHKKKIL.");
out.println("JJJK.IL.");
out.println("J..K.LLL");
}
if (n == 9 && m == 9) {
out.println("13");
out.println("AAA.BCCC.");
out.println(".ABBB.CD.");
out.println(".AE.BFCD.");
out.println("EEEFFFDDD");
out.println("G.E.HFIII");
out.println("GGGJHHHI.");
out.println("GK.JHL.IM");
out.println(".KJJJLMMM");
out.println("KKK.LLL.M");
}
}
public static void main(String[] argv) {
new Thread(null, new Main(), "", 16 * 1024 * 1024).start();
}
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
sc = new FastScanner(reader);
//System.setOut(new PrintStream("out.txt"));
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
out.close();
}
}
class FastScanner {
BufferedReader reader;
StringTokenizer strtok;
public FastScanner(BufferedReader reader) {
this.reader = reader;
}
String nextToken() throws IOException {
if (strtok == null || !strtok.hasMoreElements()) {
strtok = new StringTokenizer(reader.readLine());
}
return strtok.nextToken();
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
}
} | Java | ["3 3", "5 6", "2 2"] | 3 seconds | ["1\nAAA\n.A.\n.A.", "4\nA..C..\nAAAC..\nABCCCD\n.B.DDD\nBBB..D", "0\n..\n.."] | null | Java 6 | standard input | [] | f29eba31c20246686b8427b7aeadd184 | The only line contains two space-separated integers n and m — the sizes of the warehouse (1 ≤ n, m ≤ 9). | 2,300 | In the first line print the maximum number of turboplows that can be positioned in the warehouse. In each of the next n lines print m characters. Use "." (dot) to mark empty space and use successive capital Latin letters ("A" for the first turboplow, "B" for the second one and so on until you reach the number of turboplows in your scheme) to mark place for the corresponding turboplows considering that they are positioned in the optimal manner in the warehouse. The order in which you number places for the turboplows does not matter. If there are several optimal solutions for a warehouse of the given size, print any of them. | standard output | |
PASSED | 03d32ac202039cf42381a0b3e08d43ea | train_002.jsonl | 1326380700 | Autumn came late to the kingdom of Far Far Away. The harvest was exuberant and it is now time to get ready for the winter. As most people celebrate the Harvest festival, Simon the Caretaker tries to solve a very non-trivial task of how to find place for the agricultural equipment in the warehouse.He's got problems with some particularly large piece of equipment, which is, of course, turboplows. The problem is that when a turboplow is stored, it takes up not some simply rectangular space. It takes up a T-shaped space like on one of the four pictures below (here character "#" stands for the space occupied by the turboplow and character "." stands for the free space): ### ..# .#. #...#. ### .#. ###.#. ..# ### #.. Simon faced a quite natural challenge: placing in the given n × m cells warehouse the maximum number of turboplows. As one stores the turboplows, he can rotate them in any manner (so that they take up the space like on one of the four pictures above). However, two turboplows cannot "overlap", that is, they cannot share the same cell in the warehouse.Simon feels that he alone cannot find the optimal way of positioning the plugs in the warehouse that would maximize their quantity. Can you help him? | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class Solution implements Runnable {
private BufferedReader in;
private PrintWriter out;
private StringTokenizer st;
private Random rnd;
/*String[][] vars = {{"###", ".#.", ".#."}, {"..#", "###", "..#"}, {".#.", ".#.", "###"}, {"#..", "###", "#.."}};
int n, m;
final long p = 43;
Field ans = null;
HashSet<Long> set = new HashSet<Long>();
long its = 0;
long border = 10000000L;
private void rec(Field cur, char letter) {
++its;
if(its >= border) return;
if(ans == null || cur.already > ans.already) {
ans = cur;
}
long hash = cur.getHash();
if(set.contains(hash)) return;
set.add(hash);
char next = (char) (letter + 1);
for(int i = 0; i <= n - 3; i++) {
for(int j = 0; j <= m - 3; j++) {
for(int k = 0; k < 4; k++) {
if(cur.check(i, j, k)) {
Field newField = new Field();
newField.copy(cur);
newField.place(i, j, k, letter);
rec(newField, next);
}
}
}
}
}
class Field {
char[][] f;
int already;
Field() {
f = new char[n][m];
already = 0;
}
public void copy(Field another) {
this.already = another.already;
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) this.f[i][j] = another.f[i][j];
}
public boolean check(int x, int y, int k) {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(f[x + i][y + j] >= 'A' && vars[k][i].charAt(j) != '.') return false;
}
}
return true;
}
public void place(int x, int y, int k, char letter) {
++already;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(vars[k][i].charAt(j) == '#') {
f[x + i][y + j] = letter;
}
}
}
}
public long getHash() {
long res = 0, mod = 1, cur = 1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(f[i][j] >= 'A') {
res += cur * mod;
}
++cur;
mod *= p;
}
}
return res;
}
}
private void solve(int n, int m) {
ans = null;
set.clear();
its = 0;
this.n = n;
this.m = m;
Field start = new Field();
rec(start, 'A');
out.print("res[" + n + "][" + m + "] = ");
out.print("\"" + ans.already + "\\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(ans.f[i][j] >= 'A') out.print(ans.f[i][j]);
else out.print('.');
}
out.print("\\n");
}
out.println("\";");
}*/
String[][] res = new String[10][10];
public void solve() throws IOException {
/*for(int n = 1; n <= 9; n++) {
for(int m = 1; m <= 9; m++) {
solve(n, m);
}
}*/
res[1][1] = "0\n.\n";
res[1][2] = "0\n..\n";
res[1][3] = "0\n...\n";
res[1][4] = "0\n....\n";
res[1][5] = "0\n.....\n";
res[1][6] = "0\n......\n";
res[1][7] = "0\n.......\n";
res[1][8] = "0\n........\n";
res[1][9] = "0\n.........\n";
res[2][1] = "0\n.\n.\n";
res[2][2] = "0\n..\n..\n";
res[2][3] = "0\n...\n...\n";
res[2][4] = "0\n....\n....\n";
res[2][5] = "0\n.....\n.....\n";
res[2][6] = "0\n......\n......\n";
res[2][7] = "0\n.......\n.......\n";
res[2][8] = "0\n........\n........\n";
res[2][9] = "0\n.........\n.........\n";
res[3][1] = "0\n.\n.\n.\n";
res[3][2] = "0\n..\n..\n..\n";
res[3][3] = "1\nAAA\n.A.\n.A.\n";
res[3][4] = "1\nAAA.\n.A..\n.A..\n";
res[3][5] = "2\nAAA.B\n.ABBB\n.A..B\n";
res[3][6] = "2\nAAA.B.\n.ABBB.\n.A..B.\n";
res[3][7] = "3\nAAABCCC\n.A.B.C.\n.ABBBC.\n";
res[3][8] = "3\nAAA.BCCC\n.ABBB.C.\n.A..B.C.\n";
res[3][9] = "4\nAAABCCC.D\n.A.B.CDDD\n.ABBBC..D\n";
res[4][1] = "0\n.\n.\n.\n.\n";
res[4][2] = "0\n..\n..\n..\n..\n";
res[4][3] = "1\nAAA\n.A.\n.A.\n...\n";
res[4][4] = "2\nAAA.\n.AB.\n.AB.\n.BBB\n";
res[4][5] = "2\nAAA.B\n.ABBB\n.A..B\n.....\n";
res[4][6] = "3\nAAABBB\n.AC.B.\n.AC.B.\n.CCC..\n";
res[4][7] = "4\nAAABBB.\n.AC.BD.\n.AC.BD.\n.CCCDDD\n";
res[4][8] = "4\nAAA.BCCC\n.ABBBDC.\n.A..BDC.\n....DDD.\n";
res[4][9] = "5\nAAABBBCCC\n.AD.BE.C.\n.AD.BE.C.\n.DDDEEE..\n";
res[5][1] = "0\n.\n.\n.\n.\n.\n";
res[5][2] = "0\n..\n..\n..\n..\n..\n";
res[5][3] = "2\nAAA\n.A.\n.AB\nBBB\n..B\n";
res[5][4] = "2\nAAA.\n.AB.\n.AB.\n.BBB\n....\n";
res[5][5] = "4\nAAA.B\n.ABBB\nCA.DB\nCCCD.\nC.DDD\n";
res[5][6] = "4\nAAA.B.\n.ABBB.\n.AC.BD\nCCCDDD\n..C..D\n";
res[5][7] = "5\nAAA.B..\n.ABBBC.\nDA.EBC.\nDDDECCC\nD.EEE..\n";
res[5][8] = "6\nAAA.BCCC\n.ABBBDC.\nEA.FBDC.\nEEEFDDD.\nE.FFF...\n";
res[5][9] = "7\nAAA.BCCC.\n.ABBBDC..\nEA.FBDCG.\nEEEFDDDG.\nE.FFF.GGG\n";
res[6][1] = "0\n.\n.\n.\n.\n.\n.\n";
res[6][2] = "0\n..\n..\n..\n..\n..\n..\n";
res[6][3] = "2\nAAA\n.A.\n.AB\nBBB\n..B\n...\n";
res[6][4] = "3\nAAA.\n.AB.\n.AB.\nCBBB\nCCC.\nC...\n";
res[6][5] = "4\nAAA.B\n.ABBB\n.AC.B\nCCCD.\n..CD.\n..DDD\n";
res[6][6] = "5\nAAA.B.\n.ABBB.\n.A.CB.\nDCCCE.\nDDDCE.\nD..EEE\n";
res[6][7] = "6\nAAABCCC\n.A.B.C.\nDABBBCE\nDDDFEEE\nDFFF..E\n...F...\n";
res[6][8] = "7\nAAA.BCCC\n.ABBBDC.\n.AE.BDC.\nEEEFDDDG\n..EF.GGG\n..FFF..G\n";
res[6][9] = "8\nAAA.BCCC.\n.ABBB.C..\n.A.DBEC..\nFDDDGEEEH\nFFFDGEHHH\nF..GGG..H\n";
res[7][1] = "0\n.\n.\n.\n.\n.\n.\n.\n";
res[7][2] = "0\n..\n..\n..\n..\n..\n..\n..\n";
res[7][3] = "3\nAAA\n.A.\n.AB\nBBB\n.CB\n.C.\nCCC\n";
res[7][4] = "4\nAAA.\n.AB.\n.AB.\nCBBB\nCCCD\nCDDD\n...D\n";
res[7][5] = "5\nAAA.B\n.ABBB\n.AC.B\nCCCD.\n.ECD.\n.EDDD\nEEE..\n";
res[7][6] = "6\nAAA.B.\n.ABBB.\n.AC.BD\nCCCDDD\n.EC.FD\n.EFFF.\nEEE.F.\n";
res[7][7] = "8\nAAABCCC\n.A.B.C.\nDABBBCE\nDDDFEEE\nDG.F.HE\n.GFFFH.\nGGG.HHH\n";
res[7][8] = "9\nAAA.BBB.\n.ACCCBD.\n.AEC.BD.\nEEECFDDD\nG.EHFFFI\nGGGHFIII\nG.HHH..I\n";
res[7][9] = "10\nAAA.BC...\n.ABBBCCCD\nEA.FBCDDD\nEEEF.GGGD\nEHFFFIGJ.\n.H.IIIGJ.\nHHH..IJJJ\n";
res[8][1] = "0\n.\n.\n.\n.\n.\n.\n.\n.\n";
res[8][2] = "0\n..\n..\n..\n..\n..\n..\n..\n..\n";
res[8][3] = "3\nAAA\n.A.\n.AB\nBBB\n.CB\n.C.\nCCC\n...\n";
res[8][4] = "4\nAAA.\n.AB.\n.AB.\nCBBB\nCCCD\nCDDD\n...D\n....\n";
res[8][5] = "6\nAAA.B\n.ABBB\nCA.DB\nCCCD.\nCEDDD\n.EFFF\nEEEF.\n...F.\n";
res[8][6] = "7\nAAA.B.\n.ABBB.\n.AC.BD\nCCCDDD\n..CE.D\nFEEEG.\nFFFEG.\nF..GGG\n";
res[8][7] = "9\nAAAB..C\n.A.BCCC\nDABBBEC\nDDDEEE.\nDFFF.EG\n.HFIGGG\n.HFIIIG\nHHHI...\n";
res[8][8] = "10\nAAA.BCCC\n.ABBBDC.\nEA.FBDC.\nEEEFDDDG\nEHFFFGGG\n.HIII.JG\nHHHIJJJ.\n...I..J.\n";
res[8][9] = "12\nA.EEE.JJJ\nAAAEHHHJ.\nAB.EFHKJ.\n.BFFFHKKK\nBBBDFIK..\nCDDDGIIIL\nCCCDGILLL\nC..GGG..L\n";
res[9][1] = "0\n.\n.\n.\n.\n.\n.\n.\n.\n.\n";
res[9][2] = "0\n..\n..\n..\n..\n..\n..\n..\n..\n..\n";
res[9][3] = "4\nAAA\n.A.\n.AB\nBBB\nC.B\nCCC\nC.D\nDDD\n..D\n";
res[9][4] = "5\nAAA.\n.AB.\n.AB.\nCBBB\nCCCD\nCDDD\nEEED\n.E..\n.E..\n";
res[9][5] = "7\nAAA.B\n.ABBB\nCA.DB\nCCCD.\nCEDDD\n.EEEF\nGEFFF\nGGG.F\nG....\n";
res[9][6] = "8\nAAA.B.\n.ABBB.\n.AC.BD\nCCCDDD\nE.CF.D\nEEEF..\nEGFFFH\n.G.HHH\nGGG..H\n";
res[9][7] = "10\nAAA.B..\n.ABBBC.\n.AD.BC.\nDDDECCC\nF.DEEEG\nFFFEGGG\nFHIIIJG\n.H.I.J.\nHHHIJJJ\n";
res[9][8] = "12\nAAA.BCCC\n.ABBBDC.\nEA.FBDC.\nEEEFDDDG\nEHFFFGGG\n.HHHIIIG\nJHKKKIL.\nJJJK.IL.\nJ..K.LLL\n";
res[9][9] = "13\nAAA.BCCC.\n.ABBB.CD.\n.AE.BFCD.\nEEEFFFDDD\nG.E.HFIII\nGGGJHHHI.\nGK.JHL.IM\n.KJJJLMMM\nKKK.LLL.M\n";
int n = nextInt(), m = nextInt();
out.println(res[n][m]);
}
public static void main(String[] args) {
new Solution().run();
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader((System.in)));
out = new PrintWriter(System.out);
st = null;
rnd = new Random();
double start = System.currentTimeMillis();
solve();
double total = (System.currentTimeMillis() - start) / 1000.0;
total *= 10; // hello intel core i7-2700 4.5 Ghz :D
//out.println("Total time: " + total + " sec.");
out.close();
} catch(IOException e) {
e.printStackTrace();
}
}
private String nextToken() throws IOException, NullPointerException {
while(st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | Java | ["3 3", "5 6", "2 2"] | 3 seconds | ["1\nAAA\n.A.\n.A.", "4\nA..C..\nAAAC..\nABCCCD\n.B.DDD\nBBB..D", "0\n..\n.."] | null | Java 6 | standard input | [] | f29eba31c20246686b8427b7aeadd184 | The only line contains two space-separated integers n and m — the sizes of the warehouse (1 ≤ n, m ≤ 9). | 2,300 | In the first line print the maximum number of turboplows that can be positioned in the warehouse. In each of the next n lines print m characters. Use "." (dot) to mark empty space and use successive capital Latin letters ("A" for the first turboplow, "B" for the second one and so on until you reach the number of turboplows in your scheme) to mark place for the corresponding turboplows considering that they are positioned in the optimal manner in the warehouse. The order in which you number places for the turboplows does not matter. If there are several optimal solutions for a warehouse of the given size, print any of them. | standard output | |
PASSED | c8fe9e1376036df4522b9806eff50cb1 | train_002.jsonl | 1326380700 | Autumn came late to the kingdom of Far Far Away. The harvest was exuberant and it is now time to get ready for the winter. As most people celebrate the Harvest festival, Simon the Caretaker tries to solve a very non-trivial task of how to find place for the agricultural equipment in the warehouse.He's got problems with some particularly large piece of equipment, which is, of course, turboplows. The problem is that when a turboplow is stored, it takes up not some simply rectangular space. It takes up a T-shaped space like on one of the four pictures below (here character "#" stands for the space occupied by the turboplow and character "." stands for the free space): ### ..# .#. #...#. ### .#. ###.#. ..# ### #.. Simon faced a quite natural challenge: placing in the given n × m cells warehouse the maximum number of turboplows. As one stores the turboplows, he can rotate them in any manner (so that they take up the space like on one of the four pictures above). However, two turboplows cannot "overlap", that is, they cannot share the same cell in the warehouse.Simon feels that he alone cannot find the optimal way of positioning the plugs in the warehouse that would maximize their quantity. Can you help him? | 256 megabytes |
import java.util.*;
public class taskE {
public static void main(String[] args) {
new taskE().main();
}
Scanner s = new Scanner(System.in);
int bestfield[][] = null;
int bestres = 0;
int area = 0;
int[][] delta0 = {{0, 0}, {-1, 0}, {1, 0}, {0, -1}, {0, -2}};
int[][] delta1 = {{0, 0}, {0, -1}, {0, -2}, {-1, -1}, {-2, -1}};
int[][] delta2 = {{0, 0}, {0, -1}, {0, -2}, {-1, -2}, {1, -2}};
int[][] delta3 = {{0, 0}, {0, -1}, {0, -2}, {1, -1}, {2, -1}};
int[][][] deltas = {delta0, delta1, delta2, delta3};
boolean doesntMakeSense(int row, int col, int num) {
int rest = area - row*(bestfield[0].length) - col;
int maxPossible = rest/4;
return (maxPossible + num - 1 < bestres);
}
void rec(int[][] f, int row, int col, int num) {
if (doesntMakeSense(row, col, num)) {
return;
}
if (row == f.length) {
checkResults(f, num - 1);
return;
}
for (int type = 0; type <= deltas.length; type++) {
if (possible(type, f, row, col)) {
set(type, f, row, col, num);
int newcol = col + 1, newrow = row;
if (newcol == f[row].length) {
newcol = 0;
newrow++;
}
rec(f, newrow, newcol, nextNum(num, type));
unset(type, f, row, col);
}
}
}
void main() {
int n = s.nextInt(), m = s.nextInt();
int field[][] = new int[n][m];
bestfield = new int[n][m];
area = n*m;
rec(field, 0, 0, 1);
output();
}
void output() {
System.out.println(bestres);
for (int i = 0; i < bestfield.length; i++) {
for (int j = 0; j < bestfield[i].length; j++) {
if (bestfield[i][j]==0) {
System.out.print('.');
} else {
System.out.print((char)('A'+bestfield[i][j]-1));
}
}
System.out.println();
}
}
private void checkResults(int[][] f, int n) {
if (n > bestres) {
bestres = n;
for (int i = 0; i < f.length; i++) {
System.arraycopy(f[i], 0, bestfield[i], 0, f[i].length);
}
}
}
boolean badCell(int[][] f, int col, int row) {
if (row < 0 || row >= f.length || col < 0 || col >= f[row].length || f[row][col]!=0) {
return true;
}
return false;
}
private boolean possible(int type, int[][] f, int row, int col) {
if (type == deltas.length) {
return true;
}
for (int i = 0; i < deltas[type].length; i++) {
int ncol = col + deltas[type][i][0];
int nrow = row + deltas[type][i][1];
if (badCell(f, ncol, nrow)) {
return false;
}
}
return true;
}
private void set(int type, int[][] f, int row, int col, int num) {
if (type == deltas.length) {
return;
}
for (int i = 0; i < deltas[type].length; i++) {
int ncol = col + deltas[type][i][0];
int nrow = row + deltas[type][i][1];
f[nrow][ncol] = num;
}
}
private void unset(int type, int[][] f, int row, int col) {
set(type, f, row, col, 0);
}
private int nextNum(int num, int type) {
if (type == deltas.length) {
return num;
}
return num + 1;
}
}
| Java | ["3 3", "5 6", "2 2"] | 3 seconds | ["1\nAAA\n.A.\n.A.", "4\nA..C..\nAAAC..\nABCCCD\n.B.DDD\nBBB..D", "0\n..\n.."] | null | Java 6 | standard input | [] | f29eba31c20246686b8427b7aeadd184 | The only line contains two space-separated integers n and m — the sizes of the warehouse (1 ≤ n, m ≤ 9). | 2,300 | In the first line print the maximum number of turboplows that can be positioned in the warehouse. In each of the next n lines print m characters. Use "." (dot) to mark empty space and use successive capital Latin letters ("A" for the first turboplow, "B" for the second one and so on until you reach the number of turboplows in your scheme) to mark place for the corresponding turboplows considering that they are positioned in the optimal manner in the warehouse. The order in which you number places for the turboplows does not matter. If there are several optimal solutions for a warehouse of the given size, print any of them. | standard output | |
PASSED | 010093e64eac358bcafdd01790dfc809 | train_002.jsonl | 1326380700 | Autumn came late to the kingdom of Far Far Away. The harvest was exuberant and it is now time to get ready for the winter. As most people celebrate the Harvest festival, Simon the Caretaker tries to solve a very non-trivial task of how to find place for the agricultural equipment in the warehouse.He's got problems with some particularly large piece of equipment, which is, of course, turboplows. The problem is that when a turboplow is stored, it takes up not some simply rectangular space. It takes up a T-shaped space like on one of the four pictures below (here character "#" stands for the space occupied by the turboplow and character "." stands for the free space): ### ..# .#. #...#. ### .#. ###.#. ..# ### #.. Simon faced a quite natural challenge: placing in the given n × m cells warehouse the maximum number of turboplows. As one stores the turboplows, he can rotate them in any manner (so that they take up the space like on one of the four pictures above). However, two turboplows cannot "overlap", that is, they cannot share the same cell in the warehouse.Simon feels that he alone cannot find the optimal way of positioning the plugs in the warehouse that would maximize their quantity. Can you help him? | 256 megabytes | //package Round102;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class HelpCaretakerGenerated {
public static int[][] dp;
public static String[][][] paths;
/**
* @param args
*/
public static void main(String[] args) throws Exception {
dp = new int[10][10];
paths = new String[10][10][];
dp[3][3] = 1;
paths[3][3] = new String[] {"..A",
"AAA",
"..A"};
dp[3][4] = 1;
paths[3][4] = new String[] {"..A.",
"AAA.",
"..A."};
dp[3][5] = 2;
paths[3][5] = new String[] {".A..B",
".ABBB",
"AAA.B"};
dp[3][6] = 2;
paths[3][6] = new String[] {"..A..B",
"AAABBB",
"..A..B"};
dp[3][7] = 3;
paths[3][7] = new String[] {".ABBB.C",
".A.BCCC",
"AAAB..C"};
dp[3][8] = 3;
paths[3][8] = new String[] {"..A.B..C",
"AAA.BCCC",
"..ABBB.C"};
dp[3][9] = 4;
paths[3][9] = new String[] {".ABBBC..D",
".A.B.CDDD",
"AAABCCC.D"};
dp[4][4] = 2;
paths[4][4] = new String[] {"A...",
"AAAB",
"ABBB",
"...B"};
dp[4][5] = 2;
paths[4][5] = new String[] {"..A..",
"AAAB.",
"..AB.",
"..BBB"};
dp[4][6] = 3;
paths[4][6] = new String[] {"..ACCC",
"AAABC.",
"..ABC.",
"..BBB."};
dp[4][7] = 4;
paths[4][7] = new String[] {"A..CCC.",
"AAABCD.",
"ABBBCD.",
"...BDDD"};
dp[4][8] = 4;
paths[4][8] = new String[] {"..A.C...",
"AAABCCCD",
"..ABCDDD",
"..BBB..D"};
dp[4][9] = 5;
paths[4][9] = new String[] {"..ACCCEEE",
"AAABC.DE.",
"..ABC.DE.",
"..BBBDDD."};
dp[5][5] = 4;
paths[5][5] = new String[] {"A.CCC",
"AAAC.",
"AB.CD",
".BDDD",
"BBB.D"};
dp[5][6] = 4;
paths[5][6] = new String[] {"A..C..",
"AAACCC",
"A.BC.D",
"BBBDDD",
"..B..D"};
dp[5][7] = 5;
paths[5][7] = new String[] {"A.CCC.E",
"AAACEEE",
"A.BC.DE",
"BBBDDD.",
"..B..D."};
dp[5][8] = 6;
paths[5][8] = new String[] {"A..C.EEE",
"AAACCCE.",
"A.BCD.EF",
"BBB.DFFF",
"..BDDD.F"};
dp[5][9] = 7;
paths[5][9] = new String[] {"A.CCC.FFF",
"AAACEEEF.",
"A.BCDE.FG",
"BBB.DEGGG",
"..BDDD..G"};
dp[6][6] = 5;
paths[6][6] = new String[] {"..ADDD",
"AAACD.",
"..ACD.",
".BCCCE",
".B.EEE",
"BBB..E"};
dp[6][7] = 6;
paths[6][7] = new String[] {"..AB...",
"AAABBBF",
"..ABFFF",
".CDDDEF",
".C.D.E.",
"CCCDEEE"};
dp[6][8] = 7;
paths[6][8] = new String[] {"..AB..F.",
"AAABBBF.",
"..ABEFFF",
"C..DEEEG",
"CCCDEGGG",
"C.DDD..G"};
dp[6][9] = 8;
paths[6][9] = new String[] {"..AB..GGG",
"AAABBBFG.",
"..ABFFFG.",
".CDDDEF.H",
".C.D.EHHH",
"CCCDEEE.H"};
dp[7][7] = 8;
paths[7][7] = new String[] {"A.DDD.G",
"AAADGGG",
"A.CDF.G",
"CCCEFFF",
".BCEFH.",
".BEEEH.",
"BBB.HHH"};
dp[7][8] = 9;
paths[7][8] = new String[] {"A..DDD.H",
"AAACDHHH",
"ACCCDG.H",
"BBBCFGGG",
".BE.FGI.",
".BEFFFI.",
".EEE.III"};
dp[7][9] = 10;
paths[7][9] = new String[] {".ABBB.III",
".A.BFFFI.",
"AAABEFHI.",
"C.EEEFHHH",
"CCCDEGH.J",
"CDDD.GJJJ",
"...DGGG.J"};
dp[8][8] = 10;
paths[8][8] = new String[] {"..A.BCCC",
"AAA.B.C.",
"..ABBBCI",
"D.FFFIII",
"DDDFHHHI",
"DE.FGHJ.",
".EGGGHJ.",
"EEE.GJJJ"};
dp[8][9] = 12;
paths[8][9] = new String[] {"A.CCC.HHH",
"AAACGGGH.",
"AB.CFG.HK",
".BFFFGKKK",
"BBBEFJ..K",
"DEEEIJJJL",
"DDDEIJLLL",
"D..III..L"};
dp[9][9] = 13;
paths[9][9] = new String[] {"..ADDDFFF",
"AAACD.EF.",
"..ACD.EF.",
"B.CCCEEEL",
"BBBIIILLL",
"BHHHIKKKL",
".GH.IJKM.",
".GHJJJKM.",
"GGG..JMMM"};
// System.setIn(new FileInputStream("src/Round102/helpcaretaker.in"));
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] parts = bf.readLine().trim().split("[ ]+");
int rows = Integer.parseInt(parts[0]);
int cols = Integer.parseInt(parts[1]);
if(rows < 3 || cols < 3) {
System.out.println("0");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++)
System.out.print(".");
System.out.println();
}
return;
}
if(rows <= cols) {
System.out.println(dp[rows][cols]);
printNormal(rows, cols);
}
else {
System.out.println(dp[cols][rows]);
printTurned(cols, rows);
}
}
public static void printNormal(int i, int j) {
for(int k = 0; k < paths[i][j].length; k++)
System.out.println(paths[i][j][k]);
}
public static void printTurned(int i, int j) {
for(int l = 0; l < paths[i][j][0].length(); l++) {
for(int k = 0; k < paths[i][j].length; k++) {
System.out.print(paths[i][j][k].charAt(l));
}
System.out.println();
}
// for(int k = 0; k < paths[i][j].length; k++) {
// for(int l = 0; l < paths[i][j][k].length(); l++) {
// System.out.print(paths[i][j][k].charAt(l));
// }
// System.out.println();
// }
}
}
| Java | ["3 3", "5 6", "2 2"] | 3 seconds | ["1\nAAA\n.A.\n.A.", "4\nA..C..\nAAAC..\nABCCCD\n.B.DDD\nBBB..D", "0\n..\n.."] | null | Java 6 | standard input | [] | f29eba31c20246686b8427b7aeadd184 | The only line contains two space-separated integers n and m — the sizes of the warehouse (1 ≤ n, m ≤ 9). | 2,300 | In the first line print the maximum number of turboplows that can be positioned in the warehouse. In each of the next n lines print m characters. Use "." (dot) to mark empty space and use successive capital Latin letters ("A" for the first turboplow, "B" for the second one and so on until you reach the number of turboplows in your scheme) to mark place for the corresponding turboplows considering that they are positioned in the optimal manner in the warehouse. The order in which you number places for the turboplows does not matter. If there are several optimal solutions for a warehouse of the given size, print any of them. | standard output | |
PASSED | eb4c8fd6c1952bc98eede119ad447fe4 | train_002.jsonl | 1363534200 | Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: Add the integer xi to the first ai elements of the sequence. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class A {
public static int i(String s) { return Integer.parseInt(s); }
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder out = new StringBuilder();
int n = i(in.readLine());
long[] ADD = new long[n+1];
long[] VAL = new long[n+1];
VAL[0] = 0;
ADD[0] = 0;
double sum = 0;
int len = 1;
for(int t=0; t<n; t++) {
String[] arr = in.readLine().split(" ");
int cmd = i(arr[0]);
if(cmd == 1) {
int how_many = i(arr[1]);
int how_much = i(arr[2]);
ADD[how_many-1] += how_much;
sum += how_many*how_much;
} else if(cmd == 2) {
int val = i(arr[1]);
VAL[len] = val;
ADD[len] = 0;
len++;
sum += val;
} else {
len--;
sum -= VAL[len]+ADD[len];
ADD[len-1]+=ADD[len];
}
out.append(String.format("%.8f\n", sum/len));
}
System.out.print(out);
}
}
| Java | ["5\n2 1\n3\n2 3\n2 1\n3", "6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3"] | 1.5 seconds | ["0.500000\n0.000000\n1.500000\n1.333333\n1.500000", "0.500000\n20.500000\n14.333333\n12.333333\n17.500000\n17.000000"] | NoteIn the second sample, the sequence becomes | Java 6 | standard input | [
"data structures",
"constructive algorithms",
"implementation"
] | d43d4fd6c1e2722da185f34d902ace97 | The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. | 1,600 | Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. | standard output | |
PASSED | 7e8ba32708fa35c25fa256a64fad5bbb | train_002.jsonl | 1363534200 | Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: Add the integer xi to the first ai elements of the sequence. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
/**
*
* @author sukhdeep
*/
public class Case {
static class InputReader {
final private int B_SIZE = 1 << 16;
private DataInputStream d;
private byte[] buffer;
private int pointer, bytes;
public InputReader() {
d = new DataInputStream(System.in);
buffer = new byte[B_SIZE];
pointer = bytes = 0;
}
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;
}
private void fillBuffer() throws IOException {
bytes = d.read(buffer, pointer = 0, B_SIZE);
if (bytes == -1)
buffer[0] = -1;
}
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;
}
public String readLine() throws IOException {
byte[] buf = new byte[1024];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
private byte read() throws IOException {
if (pointer == bytes)
fillBuffer();
return buffer[pointer++];
}
public void close() throws IOException {
if (d == null)
return;
d.close();
}
}
static int top=0;static double a[]=new double[200001],b[]=new double[200000];static double sum=0;static int temp=0;
public static void main(String arg[]) throws IOException
{
InputReader r=new InputReader();
DecimalFormat f=new DecimalFormat("0.000000");
int t=r.nextInt();
BigDecimal avg=new BigDecimal("0");
while(t-->0)
{
int opr=r.nextInt();
if(opr==1)
{
int x=r.nextInt();
double y=r.nextDouble();
b[x-1]=b[x-1]+y;
y=y*x;
sum=sum+y;
}
else if(opr==2)
{
int x=r.nextInt();
push(x);
}
else if(opr==3)
{
pop();
}
avg=BigDecimal.valueOf(sum).divide(BigDecimal.valueOf(top+1),6,RoundingMode.FLOOR);
System.out.println(avg);
}
}
public static void push(int n)
{
top=top+1;
a[top]=n;
sum=sum+n;
}
public static void pop()
{
if(top>=1)
{
sum=sum-a[top]-b[top];
b[top-1]+=b[top];
a[top]=b[top]=0;
top--;
}
}
} | Java | ["5\n2 1\n3\n2 3\n2 1\n3", "6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3"] | 1.5 seconds | ["0.500000\n0.000000\n1.500000\n1.333333\n1.500000", "0.500000\n20.500000\n14.333333\n12.333333\n17.500000\n17.000000"] | NoteIn the second sample, the sequence becomes | Java 6 | standard input | [
"data structures",
"constructive algorithms",
"implementation"
] | d43d4fd6c1e2722da185f34d902ace97 | The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. | 1,600 | Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. | standard output | |
PASSED | 83e5861df4bfcf7a93bd8ec15557b013 | train_002.jsonl | 1363534200 | Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: Add the integer xi to the first ai elements of the sequence. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
(new Main()).solve();
}
public Main() {
}
MyReader in = new MyReader();
PrintWriter out = new PrintWriter(System.out);
void solve() throws IOException {
// BufferedReader in = new BufferedReader(new
// InputStreamReader(System.in));
// Scanner in = new Scanner(System.in);
//Scanner in = new Scanner(new FileReader("forbidden-triples.in"));
//PrintWriter out = new PrintWriter("forbidden-triples.out");
int n = in.nextInt();
ArrayList<Long> a = new ArrayList<Long>();
ArrayList<Long> d = new ArrayList<Long>();
a.add(0l);
d.add(0l);
long sum = 0;
for (int i = 0; i < n; i++) {
int t = in.nextInt();
if (t == 1) {
int k = in.nextInt();
int x = in.nextInt();
d.set(k - 1, d.get(k - 1) + x);
sum += k * x;
} else if (t == 2) {
long x = in.nextInt();
a.add(x);
d.add(0l);
sum += x;
} else {
long el = d.get(a.size() - 1);
d.set(d.size() - 2, d.get(d.size() - 2) + el);
sum -= (el + a.get(a.size() - 1));
a.remove(a.size() - 1);
d.remove(d.size() - 1);
}
out.println(((double)sum) / a.size());
}
out.close();
}
};
class MyReader {
private BufferedReader in;
String[] parsed;
int index = 0;
public MyReader() {
in = new BufferedReader(new InputStreamReader(System.in));
}
public int nextInt() throws NumberFormatException, IOException {
if (parsed == null || parsed.length == index) {
read();
}
return Integer.parseInt(parsed[index++]);
}
public long nextLong() throws NumberFormatException, IOException {
if (parsed == null || parsed.length == index) {
read();
}
return Long.parseLong(parsed[index++]);
}
public String nextString() throws IOException {
if (parsed == null || parsed.length == index) {
read();
}
return parsed[index++];
}
private void read() throws IOException {
parsed = in.readLine().split(" ");
index = 0;
}
public String readLine() throws IOException {
return in.readLine();
}
}; | Java | ["5\n2 1\n3\n2 3\n2 1\n3", "6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3"] | 1.5 seconds | ["0.500000\n0.000000\n1.500000\n1.333333\n1.500000", "0.500000\n20.500000\n14.333333\n12.333333\n17.500000\n17.000000"] | NoteIn the second sample, the sequence becomes | Java 6 | standard input | [
"data structures",
"constructive algorithms",
"implementation"
] | d43d4fd6c1e2722da185f34d902ace97 | The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. | 1,600 | Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. | standard output | |
PASSED | 26b12f79a0cfd0362fb4598144a71a90 | train_002.jsonl | 1363534200 | Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: Add the integer xi to the first ai elements of the sequence. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! | 256 megabytes | import java.util.*;
import java.io.*;
/*
This is an implementation of a recursive splaying algorithm.
It depends on "deep splay" to keep the tree balanced
preventing stack overflow on the splays, and guaranteeing O(log n)
worst case time. It's also totally functional. i.e. the trees are
persistent. It uses the snip/glue paradigm to easily incorporate
complicated augmentations.
Removed the "functional" part of this to see if it's more efficient.
*/
public class AA {
static class Node {
Node l, r;
int size, depth;
int delta;
int key;
Node(Node lx, int k, int d, Node rx, int ss, int dd) {
key = k;
delta = d;
l = lx;
r = rx;
size = ss;
depth = dd;
}
}
static Node copy(Node n) {
return new Node (n.l, n.key, n.delta, n.r, n.size, n.depth);
}
private static Node nullNode;
static {
nullNode = new Node(null, 0, 0, null, 0, 0);
}
static Node connect(Node ll, Node k, Node rr) {
// return new Node(ll, k.key, k.delta, rr, 1, 1);
k.l = ll;
k.r = rr;
return k;
}
static Node glue(Node ll, Node k, Node rr) {
if (ll != nullNode) ll.delta -= k.delta;
if (rr != nullNode) rr.delta -= k.delta;
k.size = ll.size + 1 + rr.size;
k.depth = 1 + Math.max(ll.depth, rr.depth);
k.l = ll;
k.r = rr;
return k;
}
static Node snip(Node n) {
Node l=n.l, r=n.r;
if (l != nullNode) l.delta += n.delta;
if (r != nullNode) r.delta += n.delta;
n.key += n.delta;
n.delta = 0;
n.size = n.depth = 1;
return n;
}
// static Node nodeOfRank(Node t, int rank) {
// int lsize = t.l.size;
// if (rank < lsize) return nodeOfRank(t.l, rank);
// else if (rank > lsize) return nodeOfRank(t.r, rank-lsize-1);
// else return t;
// }
static Node splayRank(Node t, int rank) {
/* t is already snipped. returns a splayed tree, which is in the snipped state */
if (rank == t.l.size) return t;
if (rank < t.l.size) {
Node u = snip(t.l);
if (rank == u.l.size) return connect(u.l, u, glue(u.r, t, t.r));
if (rank < u.l.size) {
Node v = snip(u.l);
v = splayRank(v, rank);
return connect(v.l, v, glue(v.r, u, glue(u.r, t, t.r)));
} else {
u = splayRank(u, rank);
return connect(u.l, u, glue(u.r, t, t.r));
}
} else {
rank -= t.l.size+1;
Node u = snip(t.r);
if (rank == u.l.size) return connect(glue(t.l, t, u.l), u, u.r);
if (rank > u.l.size) {
Node v = snip(u.r);
v = splayRank(v, rank - (u.l.size+1));
return connect(glue(glue(t.l, t, u.l), u, v.l), v, v.r);
} else {
u = splayRank(u, rank);
return connect(glue(t.l, t, u.l), u, u.r);
}
}
}
static Node splay(Node t, int rank) {
if (t == nullNode) return t;
t = splayRank(snip(t), rank);
return glue (t.l, t, t.r);
}
static int rankOfDeepest(Node t, int ac) {
if (t.depth == 1) return ac;
if (t.l.depth >= t.r.depth) return rankOfDeepest(t.l, ac);
return rankOfDeepest(t.r, ac+t.l.size+1);
}
static Node deepSplay(Node t) {
if (t == nullNode) return t;
return splay(t, rankOfDeepest(t,0));
}
static Node append(Node t, int key) {
Node n = new Node(nullNode, key, 0, nullNode, 1, 1);
n = glue(t, n, nullNode);
return deepSplay(n);
}
static void printTree(Node t, int d) {
if (t == nullNode) return;
printTree(t.r, d+1);
for (int i=0; i<d; i++) System.out.printf(" ");
System.out.printf("(%d, %d, %d) \n", t.key, t.size, t.depth);
printTree(t.l, d+1);
}
public void read_input_and_solve() throws IOException, MyException {
StringBuilder out = new StringBuilder();
long total = 0;
Node t = append(nullNode, 0);
int n = nextInt();
for (int i=0; i<n; i++) {
int c = nextInt();
if (c==1) {
int a = nextInt();
int x = nextInt();
t = splay(t, a-1);
t.delta += x;
if (t.r != nullNode) t.r.delta -= x;
total += a*x;
} else if (c==2) {
int k = nextInt();
total += k;
t = append(t, k);
} else {
t = splay(t, t.size-1);
t = snip(t);
total -= t.key;
t = t.l;
}
out.append(String.format("%.6f\n", (total + 0.0) / t.size));
if (i%5 == 0) t = deepSplay(t);
}
System.out.print(out);
}
public static void main(String[] args) {
(new AA()).run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
writer = new PrintWriter(System.out);
read_input_and_solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
class MyException extends Exception {
public String s;
public MyException(String prob) {
super(prob);
s = prob;
}
}
| Java | ["5\n2 1\n3\n2 3\n2 1\n3", "6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3"] | 1.5 seconds | ["0.500000\n0.000000\n1.500000\n1.333333\n1.500000", "0.500000\n20.500000\n14.333333\n12.333333\n17.500000\n17.000000"] | NoteIn the second sample, the sequence becomes | Java 6 | standard input | [
"data structures",
"constructive algorithms",
"implementation"
] | d43d4fd6c1e2722da185f34d902ace97 | The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. | 1,600 | Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. | standard output | |
PASSED | 6fa1f2a0039b42c483f867e376aaeff4 | train_002.jsonl | 1363534200 | Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: Add the integer xi to the first ai elements of the sequence. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Arrays;
public class c{
public static void main(String args[]) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
int[] l=new int[n+1];
int[] adds=new int[n+1];
int size=1;
long sum=0;
for(int i=0;i<n;i++){
st=new StringTokenizer(br.readLine());
int type=Integer.parseInt(st.nextToken());
if(type==1){
int ai=Integer.parseInt(st.nextToken());
int xi=Integer.parseInt(st.nextToken());
adds[ai-1]+=xi;
sum+=ai*xi;
} else if(type==2){
size++;
int tmp=Integer.parseInt(st.nextToken());
l[size-1]=tmp;
sum+=tmp;
} else{
sum-=l[size-1];
l[size-1]=0;
sum-=(adds[size-1]);
adds[size-2]+=adds[size-1];
adds[size-1]=0;
size--;
}
System.out.println(((double)sum)/size);
}
}
} | Java | ["5\n2 1\n3\n2 3\n2 1\n3", "6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3"] | 1.5 seconds | ["0.500000\n0.000000\n1.500000\n1.333333\n1.500000", "0.500000\n20.500000\n14.333333\n12.333333\n17.500000\n17.000000"] | NoteIn the second sample, the sequence becomes | Java 6 | standard input | [
"data structures",
"constructive algorithms",
"implementation"
] | d43d4fd6c1e2722da185f34d902ace97 | The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. | 1,600 | Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. | standard output | |
PASSED | 444ac43d39f8fab4f9e81c3bc296ac8d | train_002.jsonl | 1594565100 | There are $$$n$$$ programmers that you want to split into several non-empty teams. The skill of the $$$i$$$-th programmer is $$$a_i$$$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $$$x$$$.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt(), x = sc.nextInt();
PriorityQueue<Long> pq = new PriorityQueue<>(Collections.reverseOrder());
for(int i = 0; i < n; i++) {
pq.add(sc.nextLong());
}
long cnt = 0, ans = 0;
while(!pq.isEmpty()) {
cnt++;
long cur = pq.poll();
if(cur * cnt >= x) {
ans++;
cnt = 0;
}
}
out.println(ans);
}
out.close();
}
}
class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
} | Java | ["3\n5 10\n7 11 2 9 5\n4 8\n2 4 2 3\n4 11\n1 3 3 7"] | 2 seconds | ["2\n1\n0"] | null | Java 11 | standard input | [
"dp",
"greedy",
"implementation",
"sortings",
"brute force"
] | 8a6953a226abef41a44963c9b4998a25 | The first line contains the integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5; 1 \le x \le 10^9$$$) — the number of programmers and the restriction of team skill respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th programmer. The sum of $$$n$$$ over all inputs does not exceed $$$10^5$$$. | 1,400 | For each test case print one integer — the maximum number of teams that you can assemble. | standard output | |
PASSED | 7119b121fa8d08ad6bd444e91ba07820 | train_002.jsonl | 1594565100 | There are $$$n$$$ programmers that you want to split into several non-empty teams. The skill of the $$$i$$$-th programmer is $$$a_i$$$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $$$x$$$.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
public class CrateTheTeams {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n, int size) {
int[] a = new int[size];
for (int i = 0; i < n; i++) {
a[i] = this.nextInt();
}
return a;
}
long[] readLongArray(int n, int size) {
long[] a = new long[size];
for (int i = 0; i < n; i++) {
a[i] = this.nextLong();
}
return a;
}
}
static void ruffleSort(long[] a) {
int n = a.length;//shuffle, then sort
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n);
long temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static 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;
}
Arrays.sort(a);
}
public static void main(String[] args) {
FastReader fs = new FastReader();
int t = fs.nextInt();
for (int z = 0; z < t; z++) {
int n = fs.nextInt();
int x = fs.nextInt();
int[] skills = new int[n];
for(int i = 0; i < n; i++) skills[i] = fs.nextInt();
Arrays.sort(skills);
int teams = 0;
int count = 0;
for(int i = n - 1; i >= 0; i--) {
count += 1;
if(count * skills[i] >= x) {
teams++;
count = 0;
}
}
System.out.println(teams);
}
}
} | Java | ["3\n5 10\n7 11 2 9 5\n4 8\n2 4 2 3\n4 11\n1 3 3 7"] | 2 seconds | ["2\n1\n0"] | null | Java 11 | standard input | [
"dp",
"greedy",
"implementation",
"sortings",
"brute force"
] | 8a6953a226abef41a44963c9b4998a25 | The first line contains the integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5; 1 \le x \le 10^9$$$) — the number of programmers and the restriction of team skill respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th programmer. The sum of $$$n$$$ over all inputs does not exceed $$$10^5$$$. | 1,400 | For each test case print one integer — the maximum number of teams that you can assemble. | standard output | |
PASSED | 62282c01fb6a88adf2e8bd08d932b561 | train_002.jsonl | 1594565100 | There are $$$n$$$ programmers that you want to split into several non-empty teams. The skill of the $$$i$$$-th programmer is $$$a_i$$$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $$$x$$$.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble. | 256 megabytes | // package JPackage;
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 x=in.nextInt();
int[] arr=new int[n];
List<Integer> al=new ArrayList<Integer>();
Stack<Integer> st=new Stack<Integer>();
for(int i=0;i<arr.length;i++){
arr[i]=in.nextInt();
st.push(arr[i]);
}
Arrays.sort(arr);
int count=0;
for(int i=n-1;i>=0;i--) {
al.add(arr[i]);
if(al.get(al.size()-1)*al.size()>=x) {
al.clear();
count++;
}
}
System.out.println(count);
}
}
}
| Java | ["3\n5 10\n7 11 2 9 5\n4 8\n2 4 2 3\n4 11\n1 3 3 7"] | 2 seconds | ["2\n1\n0"] | null | Java 11 | standard input | [
"dp",
"greedy",
"implementation",
"sortings",
"brute force"
] | 8a6953a226abef41a44963c9b4998a25 | The first line contains the integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5; 1 \le x \le 10^9$$$) — the number of programmers and the restriction of team skill respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th programmer. The sum of $$$n$$$ over all inputs does not exceed $$$10^5$$$. | 1,400 | For each test case print one integer — the maximum number of teams that you can assemble. | standard output | |
PASSED | c313d0cee69547edd19b325a13649d2b | train_002.jsonl | 1594565100 | There are $$$n$$$ programmers that you want to split into several non-empty teams. The skill of the $$$i$$$-th programmer is $$$a_i$$$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $$$x$$$.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble. | 256 megabytes | import java.util.*;
import java.io.*;
public class teams {
public static void main(String[] args) throws java.io.IOException {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
long loops = input.nextLong();
for (long l = 0; l < loops; l++) {
long size = input.nextLong();
long min = input.nextLong();
ArrayList<Long> people = new ArrayList<Long>();
for (long i = 0; i < size; i++) {
people.add(input.nextLong());
}
Collections.sort(people);
long teams = 0;
int position = people.size() - 1;
int nextposition = people.size() - 1;
loop:
while (position >= 0) {
if (people.get(position) >= min) {
nextposition--;
position--;
teams++;
} else {
while (nextposition >= 0 && people.get(nextposition) * (position - nextposition + 1) < min) {
nextposition--;
}
if (nextposition >= 0) {
teams++;
}
nextposition--;
}
position = nextposition;
}
System.out.println(teams);
}
}
} | Java | ["3\n5 10\n7 11 2 9 5\n4 8\n2 4 2 3\n4 11\n1 3 3 7"] | 2 seconds | ["2\n1\n0"] | null | Java 11 | standard input | [
"dp",
"greedy",
"implementation",
"sortings",
"brute force"
] | 8a6953a226abef41a44963c9b4998a25 | The first line contains the integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5; 1 \le x \le 10^9$$$) — the number of programmers and the restriction of team skill respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th programmer. The sum of $$$n$$$ over all inputs does not exceed $$$10^5$$$. | 1,400 | For each test case print one integer — the maximum number of teams that you can assemble. | standard output | |
PASSED | e2bb03690a77f0da17291f6883f372ea | train_002.jsonl | 1594565100 | There are $$$n$$$ programmers that you want to split into several non-empty teams. The skill of the $$$i$$$-th programmer is $$$a_i$$$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $$$x$$$.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble. | 256 megabytes | import java.util.*;
public class Question3{
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
int t = sc.nextInt();
while(t-->0){
solve();
}
}
public static void solve(){
int n = sc.nextInt();
int x = sc.nextInt();
int[] arr = new int[n];
for(int i = 0;i < n;i++)
arr[i] = sc.nextInt();
Arrays.sort(arr);
int count = 0, group = 1;
for(int i = n - 1;i >= 0;i--){
if(arr[i] * group >= x){
count++;
group = 1;
}
else {
group++;
}
}
System.out.println(count);
}
}
| Java | ["3\n5 10\n7 11 2 9 5\n4 8\n2 4 2 3\n4 11\n1 3 3 7"] | 2 seconds | ["2\n1\n0"] | null | Java 11 | standard input | [
"dp",
"greedy",
"implementation",
"sortings",
"brute force"
] | 8a6953a226abef41a44963c9b4998a25 | The first line contains the integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5; 1 \le x \le 10^9$$$) — the number of programmers and the restriction of team skill respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th programmer. The sum of $$$n$$$ over all inputs does not exceed $$$10^5$$$. | 1,400 | For each test case print one integer — the maximum number of teams that you can assemble. | standard output | |
PASSED | f03688face607b7787c8595eade40747 | train_002.jsonl | 1594565100 | There are $$$n$$$ programmers that you want to split into several non-empty teams. The skill of the $$$i$$$-th programmer is $$$a_i$$$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $$$x$$$.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble. | 256 megabytes | import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int x=sc.nextInt();
Integer[] arr=new Integer[n];
for(int i=0;i<n;i++){
arr[i]=new Integer(sc.nextInt());
}
Arrays.sort(arr,Collections.reverseOrder());
int cnt=0;
long res=0;
for(int i=0;i<n;i++){
++cnt;
if(arr[i]*cnt>=x){
res++;
cnt=0;
}
}
System.out.println(res);
}
}
}
| Java | ["3\n5 10\n7 11 2 9 5\n4 8\n2 4 2 3\n4 11\n1 3 3 7"] | 2 seconds | ["2\n1\n0"] | null | Java 11 | standard input | [
"dp",
"greedy",
"implementation",
"sortings",
"brute force"
] | 8a6953a226abef41a44963c9b4998a25 | The first line contains the integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5; 1 \le x \le 10^9$$$) — the number of programmers and the restriction of team skill respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th programmer. The sum of $$$n$$$ over all inputs does not exceed $$$10^5$$$. | 1,400 | For each test case print one integer — the maximum number of teams that you can assemble. | standard output | |
PASSED | dd11b3b0bd2a2fa939fa77ef40288f42 | train_002.jsonl | 1594565100 | There are $$$n$$$ programmers that you want to split into several non-empty teams. The skill of the $$$i$$$-th programmer is $$$a_i$$$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $$$x$$$.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(reader.readLine());
for(int il = 0;il<t;il++){
String[] lo = reader.readLine().split(" ");
long x = Long.parseLong(lo[1]);
long[] list = Arrays.stream(reader.readLine().split(" ")).mapToLong(Long::parseLong).toArray();
Arrays.sort(list);
//HaahMap<Integer,Integer> hm = new HashMap<>();
int i = list.length-1;
int res = 0;
while(i>=0 && list[i]>=x){
res++;
i--;
}
long l = 0;
while(i>=0){
if(list[i]==0) break;
long p =(x+list[i]-1)/list[i];
p-=1;
if(l>=p){
res++;
l-=p;
i--;
}
else{
i--;
l++;
}
}
System.out.println(res);
}
}
}
| Java | ["3\n5 10\n7 11 2 9 5\n4 8\n2 4 2 3\n4 11\n1 3 3 7"] | 2 seconds | ["2\n1\n0"] | null | Java 11 | standard input | [
"dp",
"greedy",
"implementation",
"sortings",
"brute force"
] | 8a6953a226abef41a44963c9b4998a25 | The first line contains the integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5; 1 \le x \le 10^9$$$) — the number of programmers and the restriction of team skill respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th programmer. The sum of $$$n$$$ over all inputs does not exceed $$$10^5$$$. | 1,400 | For each test case print one integer — the maximum number of teams that you can assemble. | standard output | |
PASSED | 61669c31fc8f86201ccf22c1021e1a79 | train_002.jsonl | 1594565100 | There are $$$n$$$ programmers that you want to split into several non-empty teams. The skill of the $$$i$$$-th programmer is $$$a_i$$$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $$$x$$$.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main2 {
///////----------------input----------------//////
static BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st;
static byte[] buffer =new byte[10*1024];
static int index;
static InputStream input_stream= System.in;
static int total;
static String ns() throws IOException
{
StringBuilder sb = new StringBuilder();
int n = read();
while (isWhiteSpace(n))
n = read();
while (!isWhiteSpace(n)) {
sb.append((char)n);
n = read();
}
return sb.toString();
}
static int read() throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total) {
index=0;
total= input_stream.read(buffer);
if(total<=0)
return -1;
}
return buffer[index++];
}
static int ni() throws IOException
{
int integer=0;
int n= read();
while(isWhiteSpace(n))
n= read();
int neg=1;
if(n=='-') {
neg=-1;
n= read();
}
while(!isWhiteSpace(n)) {
if(n>='0'&&n<='9') {
integer*=10;
integer+=n-'0';
n= read();
}
else throw new InputMismatchException();
}
return neg*integer;
}
static long nl() throws IOException
{
long number=0;
int n= read();
while(isWhiteSpace(n))
n= read();
long neg=1l;
if(n=='-') {
neg=-1l;
n= read();
}
while(!isWhiteSpace(n)) {
if(n>='0'&&n<='9') {
number*=10l;
number+=(long)(n-'0');
n= read();
}
else throw new InputMismatchException();
}
return neg*number;
}
static double nd() throws IOException
{
double doub=0;
int n= read();
while(isWhiteSpace(n))
n= read();
int neg=1;
if(n=='-') {
neg=-1;
n= read();
}
while(!isWhiteSpace(n)&&n!='.') {
if(n>='0'&&n<='9') {
doub*=10;
doub+=n-'0';
n= read();
}
else throw new InputMismatchException();
}
if(n=='.') {
n= read();
double temp=1;
while(!isWhiteSpace(n)) {
if(n>='0'&&n<='9') {
temp/=10;
doub+=(n-'0')*temp;
n= read();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
static String nsl() throws IOException
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
static boolean isWhiteSpace(int n) {
if(n==' '|| n=='\n'|| n=='\r'|| n=='\t'|| n==-1)
return true;
return false;
}
///////-------------output-----------------//////
static public void p(String str) throws IOException {
bw.append(str);
}
static public void pn(String str) throws IOException {
p(str);
bw.append("\n");
}
static public void close()throws IOException {
bw.close();
}
static public void flush()throws IOException {
bw.flush();
}
///////----------THE FAST I/O METHODS ENDS HERE -----------/////////////
static class Node{
int h;
int w;
Node(int h,int w)
{
this.h=h;
this.w=w;
}
}
public static void main(String[] args)throws IOException {
int t=ni();
while(t-->0)
{
int n=ni();
int x=ni();
ArrayList<Integer> al=new ArrayList<Integer>();
for(int i=0;i<n;i++)
{
al.add(ni());
}
Collections.sort(al);
Collections.reverse(al);
int result=0,idx=1;
for(int c:al)
{
if(idx*c>=x)
{
result++;
idx=0;
}
idx++;
}
pn(result+"");
}
flush();
}
} | Java | ["3\n5 10\n7 11 2 9 5\n4 8\n2 4 2 3\n4 11\n1 3 3 7"] | 2 seconds | ["2\n1\n0"] | null | Java 11 | standard input | [
"dp",
"greedy",
"implementation",
"sortings",
"brute force"
] | 8a6953a226abef41a44963c9b4998a25 | The first line contains the integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5; 1 \le x \le 10^9$$$) — the number of programmers and the restriction of team skill respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th programmer. The sum of $$$n$$$ over all inputs does not exceed $$$10^5$$$. | 1,400 | For each test case print one integer — the maximum number of teams that you can assemble. | standard output | |
PASSED | 16c67d6c4a9f7c0069fcf3e4dda4b1c2 | train_002.jsonl | 1594565100 | There are $$$n$$$ programmers that you want to split into several non-empty teams. The skill of the $$$i$$$-th programmer is $$$a_i$$$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $$$x$$$.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble. | 256 megabytes | // package com.company.codeforces
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t=input.nextInt();
while (t-->0){
int n=input.nextInt();
int s=input.nextInt();
int a[]=new int[n];
for (int i = 0; i <n; i++) {
a[i]=input.nextInt();
}
Arrays.sort(a);
int res=0;
int prevsum=1;
for (int i = n-1; i >=0 ; i--) {
if (prevsum*a[i]>=s){
prevsum=1;
res++;
}else {
prevsum++;
}
}
System.out.println(res);
}
}
} | Java | ["3\n5 10\n7 11 2 9 5\n4 8\n2 4 2 3\n4 11\n1 3 3 7"] | 2 seconds | ["2\n1\n0"] | null | Java 11 | standard input | [
"dp",
"greedy",
"implementation",
"sortings",
"brute force"
] | 8a6953a226abef41a44963c9b4998a25 | The first line contains the integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5; 1 \le x \le 10^9$$$) — the number of programmers and the restriction of team skill respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th programmer. The sum of $$$n$$$ over all inputs does not exceed $$$10^5$$$. | 1,400 | For each test case print one integer — the maximum number of teams that you can assemble. | standard output | |
PASSED | f1589cf6c37a8d6d7c384cf736ec4b1f | train_002.jsonl | 1594565100 | There are $$$n$$$ programmers that you want to split into several non-empty teams. The skill of the $$$i$$$-th programmer is $$$a_i$$$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $$$x$$$.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble. | 256 megabytes | /*
-------------------------------------------------------------------
* @Name: 1380C Create The Teams
* @Author: Yanan
* @Create Time: 2020/7/14 21:26:18 (UTC+08:00)
* @Url: https://codeforces.com/contest/1380/problem/C
* @Description:
-------------------------------------------------------------------
*/
import java.io.*;
import java.util.*;
public class CF_1380C_CreateTheTeams
{
public static void main(String[] args) throws IOException
{
if (System.getProperty("ONLINE_JUDGE") == null)
{
//redirect stdin/stdout to local file
System.setIn(new FileInputStream(new File("CF_1380C_CreateTheTeams.in")));
System.setOut(new PrintStream(new File("CF_1380C_CreateTheTeams.out")));
}
Scanner in = new Scanner(System.in);
PrintStream out = System.out;
new CF_1380C_CreateTheTeams_Solution().Solve(in, out);
in.close();
out.close();
}
}
class CF_1380C_CreateTheTeams_Solution
{
public void Solve(Scanner in, PrintStream out)
{
/*
int n = in.nextInt(); // read input as integer
long k = in.nextLong(); // read input as long
double d = in.nextDouble(); // read input as double
String str = in.next(); // read input as String
String s = in.nextLine(); // read whole line as String
*/
//out.println("OK");
int t=in.nextInt();
for(int j=0;j<t;++j)
{
int n=in.nextInt(),x=in.nextInt();
//int []vec=new int[n];
ArrayList <Integer> vec=new ArrayList();
for(int i=0;i<n;++i)
{
int temp=in.nextInt();
vec.add(temp);
}
Collections.sort(vec);
Collections.reverse(vec);
int num=0,ans=0;
for(int i=0;i<n;++i)
{
if(vec.get(i).intValue()>=x)
{
++ans;
}
else
{
++num;
if(num*vec.get(i)>=x)
{
++ans;
num=0;
}
}
}
out.println(ans);
}
}
}
| Java | ["3\n5 10\n7 11 2 9 5\n4 8\n2 4 2 3\n4 11\n1 3 3 7"] | 2 seconds | ["2\n1\n0"] | null | Java 11 | standard input | [
"dp",
"greedy",
"implementation",
"sortings",
"brute force"
] | 8a6953a226abef41a44963c9b4998a25 | The first line contains the integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5; 1 \le x \le 10^9$$$) — the number of programmers and the restriction of team skill respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th programmer. The sum of $$$n$$$ over all inputs does not exceed $$$10^5$$$. | 1,400 | For each test case print one integer — the maximum number of teams that you can assemble. | standard output | |
PASSED | 4e9e451174486fcbec9dfbb940c6b26d | train_002.jsonl | 1594565100 | There are $$$n$$$ programmers that you want to split into several non-empty teams. The skill of the $$$i$$$-th programmer is $$$a_i$$$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $$$x$$$.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CCreateTheTeams solver = new CCreateTheTeams();
solver.solve(1, in, out);
out.close();
}
static class CCreateTheTeams {
public void solve(int testNumber, FastReader s, PrintWriter out) {
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt();
int x = s.nextInt();
int[] arr = s.nextIntArray(n);
int[] dp = new int[n];
CCreateTheTeams.arrays.sort(arr);
Arrays.fill(dp, -1);
out.println(func(0, arr, x, dp));
}
}
private int func(int pos, int[] arr, int x, int[] dp) {
if (pos >= arr.length) {
return 0;
}
int reqd = x / arr[pos];
if ((x % arr[pos]) != 0) {
reqd++;
}
if (dp[pos] != -1) {
return dp[pos];
}
if (reqd + pos > arr.length) {
return dp[pos] = func(pos + 1, arr, x, dp);
}
return dp[pos] = Math.max(1 + func(pos + reqd, arr, x, dp), func(pos + 1, arr, x, dp));
}
private static class arrays {
static void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int[n1];
int R[] = new int[n2];
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
static void sort(int arr[], int l, int r) {
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
static void sort(int[] arr) {
sort(arr, 0, arr.length - 1);
}
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["3\n5 10\n7 11 2 9 5\n4 8\n2 4 2 3\n4 11\n1 3 3 7"] | 2 seconds | ["2\n1\n0"] | null | Java 11 | standard input | [
"dp",
"greedy",
"implementation",
"sortings",
"brute force"
] | 8a6953a226abef41a44963c9b4998a25 | The first line contains the integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5; 1 \le x \le 10^9$$$) — the number of programmers and the restriction of team skill respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th programmer. The sum of $$$n$$$ over all inputs does not exceed $$$10^5$$$. | 1,400 | For each test case print one integer — the maximum number of teams that you can assemble. | standard output | |
PASSED | 6c39ded5c6ad82d2bddf592817718e30 | train_002.jsonl | 1594565100 | There are $$$n$$$ programmers that you want to split into several non-empty teams. The skill of the $$$i$$$-th programmer is $$$a_i$$$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $$$x$$$.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.lang.reflect.Array;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Main {
public void exec() {
int t = stdin.nextInt();
for (int i = 0; i < t; i++) {
int n = stdin.nextInt();
long x = stdin.nextInt();
long[] a = stdin.nextLongArray(n);
solve(n, x, a);
}
}
public void solve(int n, long x, long[] a) {
long[] p = new long[n];
for (int i = 0; i < n; i++) {
if (x % a[i] == 0) {
p[i] = x / a[i];
} else {
p[i] = x / a[i] + 1;
}
}
Arrays.sort(p);
int c = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
c++;
if (p[i] <= c) {
c -= p[i];
ans++;
}
}
stdout.println(ans);
}
private static final Stdin stdin = new Stdin();
private static final Stdout stdout = new Stdout();
public static void main(String[] args) {
new Main().exec();
stdout.flush();
}
public static class Stdin {
private BufferedReader stdin;
private Deque<String> tokens;
private Pattern delim;
public Stdin() {
stdin = new BufferedReader(new InputStreamReader(System.in));
tokens = new ArrayDeque<>();
delim = Pattern.compile(" ");
}
public String nextString() {
try {
if (tokens.isEmpty()) {
String line = stdin.readLine();
delim.splitAsStream(line).forEach(tokens::addLast);
}
return tokens.pollFirst();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public int nextInt() {
return Integer.parseInt(nextString());
}
public double nextDouble() {
return Double.parseDouble(nextString());
}
public long nextLong() {
return Long.parseLong(nextString());
}
public String[] nextStringArray(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) a[i] = nextString();
return a;
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
public double[] nextDoubleArray(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextDouble();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
}
public static class Stdout {
private PrintWriter stdout;
public Stdout() {
stdout = new PrintWriter(System.out, false);
}
public void printf(String format, Object ... args) {
stdout.printf(format, args);
}
public void println(Object ... objs) {
String line = Arrays.stream(objs).map(this::deepToString).collect(Collectors.joining(" "));
stdout.println(line);
}
private String deepToString(Object o) {
if (o == null || !o.getClass().isArray()) {
return Objects.toString(o);
}
int len = Array.getLength(o);
String[] tokens = new String[len];
for (int i = 0; i < len; i++) {
tokens[i] = deepToString(Array.get(o, i));
}
return "{" + String.join(",", tokens) + "}";
}
public void flush() {
stdout.flush();
}
}
} | Java | ["3\n5 10\n7 11 2 9 5\n4 8\n2 4 2 3\n4 11\n1 3 3 7"] | 2 seconds | ["2\n1\n0"] | null | Java 11 | standard input | [
"dp",
"greedy",
"implementation",
"sortings",
"brute force"
] | 8a6953a226abef41a44963c9b4998a25 | The first line contains the integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5; 1 \le x \le 10^9$$$) — the number of programmers and the restriction of team skill respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th programmer. The sum of $$$n$$$ over all inputs does not exceed $$$10^5$$$. | 1,400 | For each test case print one integer — the maximum number of teams that you can assemble. | standard output | |
PASSED | a42855a51ef735d910a0d27903f9dcbd | train_002.jsonl | 1582448700 | Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(int arr[],int l1,int r1,int l2,int r2) {
int tmp[]=new int[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(long arr[],int l1,int r1,int l2,int r2) {
long tmp[]=new long[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void main(String args[]) throws IOException {
Scan input=new Scan();
StringBuilder ans=new StringBuilder("");
int test=input.scanInt();
for(int tt=1;tt<=test;tt++) {
int n=input.scanInt();
int x=input.scanInt();
int y=input.scanInt();
if(x>y) {
int tmp=x;
x=y;
y=tmp;
}
int min=0,max=0;
if(n-y>x-1) {
max=n-(n-y-(x-1));
}
else {
max=n;
}
if(n+1>x+y) {
min=1;
}
else {
int tmp=x+y+1-n;
min=Math.min(n, tmp);
}
ans.append(min+" "+max+"\n");
}
System.out.println(ans);
}
}
| Java | ["1\n5 1 3", "1\n6 3 4"] | 1 second | ["1 3", "2 6"] | NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second — third place. | Java 11 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | 58c887568002d947706c448e6faa0f77 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \leq n \leq 10^9$$$, $$$1 \le x, y \le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round. | 1,700 | Print two integers — the minimum and maximum possible overall place Nikolay could take. | standard output | |
PASSED | 3e9a19014ec27bdc9efb0c44cf1aa70d | train_002.jsonl | 1582448700 | Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author EigenFunk
*/
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);
BDifferentRules solver = new BDifferentRules();
solver.solve(1, in, out);
out.close();
}
static class BDifferentRules {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
int min = Math.max(1, Math.min(n, x + y - n + 1));
int max = Math.min(n, ((x + y) - 1));
out.println(min + " " + max);
}
}
}
}
| Java | ["1\n5 1 3", "1\n6 3 4"] | 1 second | ["1 3", "2 6"] | NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second — third place. | Java 11 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | 58c887568002d947706c448e6faa0f77 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \leq n \leq 10^9$$$, $$$1 \le x, y \le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round. | 1,700 | Print two integers — the minimum and maximum possible overall place Nikolay could take. | standard output | |
PASSED | 0f9dede44a45af4b9e822b85227402ea | train_002.jsonl | 1582448700 | Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question. | 256 megabytes |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static long sx = 0, sy = 0, m = (long) (1e9 + 7);
static ArrayList<Integer>[] a;
static int[][] dp;
static long[] farr;
static boolean b = true;
// static HashMap<Long, Integer> hm = new HashMap<>();
static TreeMap<Integer, Integer> hm = new TreeMap<>();
public static PrintWriter out;
static ArrayList<pair> ans = new ArrayList<>();
static long[] fact = new long[(int) 1e6];
static StringBuilder sb = new StringBuilder();
static boolean cycle = false;
static long mod = 998244353;
public static void main(String[] args) throws IOException {
Reader scn = new Reader();
int t = scn.nextInt();
while (t-- != 0) {
long n = scn.nextLong(), x = scn.nextLong(), y = scn.nextLong();
long min = Math.max(1, Math.min(n, x + y - n + 1));
long max = Math.min(n, x + y - 1);
System.out.println(min + " " + max);
}
}
// _________________________TEMPLATE_____________________________________________________________
// private static int gcd(int a, int b) {
// if (a == 0)
// return b;
//
// return gcd(b % a, a);
// }
// static class comp implements Comparator<Integer> {
//
// @Override
// public int compare(Integer o1, Integer o2) {
//
// return (int) (o2 - o1);
// }
//
// }
// 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 ans;
StringBuilder sb = new StringBuilder();
@Override
public int compareTo(pair o) {
return 1;
}
}
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;
}
// kickstart - Solution
// atcoder - Main
}
} | Java | ["1\n5 1 3", "1\n6 3 4"] | 1 second | ["1 3", "2 6"] | NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second — third place. | Java 11 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | 58c887568002d947706c448e6faa0f77 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \leq n \leq 10^9$$$, $$$1 \le x, y \le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round. | 1,700 | Print two integers — the minimum and maximum possible overall place Nikolay could take. | standard output | |
PASSED | 08ccdf20daf5cabb157940a6fcc91130 | train_002.jsonl | 1582448700 | Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Kraken7
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int t = in.nextInt();
while (t-- > 0) {
long n = in.nextLong(), x = in.nextLong(), y = in.nextLong();
out.printf("%d %d\n", Math.max(1, Math.min(n, x + y - n + 1)), Math.min(n, x + y - 1));
}
}
}
}
| Java | ["1\n5 1 3", "1\n6 3 4"] | 1 second | ["1 3", "2 6"] | NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second — third place. | Java 11 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | 58c887568002d947706c448e6faa0f77 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \leq n \leq 10^9$$$, $$$1 \le x, y \le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round. | 1,700 | Print two integers — the minimum and maximum possible overall place Nikolay could take. | standard output | |
PASSED | cbddc0eb038dc56f2d8a30714f3d63ef | train_002.jsonl | 1582448700 | Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question. | 256 megabytes | //package codeforces;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
import java.util.*;
import java.util.ArrayList;
public final class Codeforces {
static int p=1000000007;
public static void main(String[] args) throws Exception{
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);
FastReader sc = new FastReader();
int tc,n,x,y,c,ans,l1,g1,l2,g2;
tc=sc.nextInt();
while(tc-->0)
{
ans=0;
n=sc.nextInt();
x=sc.nextInt();
y=sc.nextInt();
l1=x-1;
g1=n-x;
l2=y-1;
g2=n-y;
out.write(Math.max(1,Math.min(n,x+y-n+1))+" ");
out.write(Math.min(n,x+y-1)+""+'\n');
}
out.flush();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
}
| Java | ["1\n5 1 3", "1\n6 3 4"] | 1 second | ["1 3", "2 6"] | NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second — third place. | Java 11 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | 58c887568002d947706c448e6faa0f77 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \leq n \leq 10^9$$$, $$$1 \le x, y \le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round. | 1,700 | Print two integers — the minimum and maximum possible overall place Nikolay could take. | standard output | |
PASSED | 08e957f29c2da3e3ca636bc64fb1baaa | train_002.jsonl | 1582448700 | Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question. | 256 megabytes | import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.stream.Collectors;
/**
* <p></p>
*
* @author: xty
* @create: 2020-02-23 13:32
**/
public class Main {
public static void main(String[] args) {
int n;
Scanner scanner = new Scanner(System.in);
int a,b,c,t = 1;
n = scanner.nextInt();
while(n-->0)
{
a = scanner.nextInt();
b = scanner.nextInt();
c = scanner.nextInt();
t = 1;
int min = b>c?b:c;
int total = b+c;
if(total<=a)
{
t=1;
}else {
t = total-a+1;
t = t>a?a:t;
}
if(b+c<a+1)
{
int m = b+c-1;
System.out.println(t+" "+m);
}else {
System.out.println(t+" "+a);
}
}
}
}
| Java | ["1\n5 1 3", "1\n6 3 4"] | 1 second | ["1 3", "2 6"] | NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second — third place. | Java 11 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | 58c887568002d947706c448e6faa0f77 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \leq n \leq 10^9$$$, $$$1 \le x, y \le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round. | 1,700 | Print two integers — the minimum and maximum possible overall place Nikolay could take. | standard output | |
PASSED | 30833c48189e423b24a662f7352a8073 | train_002.jsonl | 1582448700 | Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class devesh08
{
public static void main(String args[])throws Exception
{
FastReader in=new FastReader(System.in);
int t=in.nextInt(),i,j;
StringBuilder sb=new StringBuilder();
start:while(t-->0)
{
long n=in.nextLong(),a=0;
long x=in.nextLong();
long y=in.nextLong();
long b=Math.min((x+y-1),n);
if(x+y<=n)
a=1;
else
a=x+y-n+1;
if(a>n)
a=n;
sb.append(a+" "+b+" \n");
}
System.out.print(sb);
}
}
class FastReader {
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
String nextLine() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c != 10 && c != 13; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
char nextChar() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan()) ;
return (char) c;
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}
/*
*/
| Java | ["1\n5 1 3", "1\n6 3 4"] | 1 second | ["1 3", "2 6"] | NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second — third place. | Java 11 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | 58c887568002d947706c448e6faa0f77 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \leq n \leq 10^9$$$, $$$1 \le x, y \le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round. | 1,700 | Print two integers — the minimum and maximum possible overall place Nikolay could take. | standard output | |
PASSED | 8c5b14bb6d5b25afadbb45b502f1101a | train_002.jsonl | 1582448700 | Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question. | 256 megabytes | import javax.print.DocFlavor;
import java.io.*;
import java.util.*;
public class Main implements Runnable {
static boolean use_n_tests = true;
static int stack_size = 1 << 27;
void solve(FastScanner in, PrintWriter out, int testNumber) {
int n = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
long s = x;
s += y;
out.printf("%d %d\n", Math.max(1, Math.min(n, s - n + 1)), Math.min(n, s - 1));
}
// ****************************** template code ***********
class Coeff {
long mod;
long[][] C;
long[] fact;
boolean cycleWay = false;
Coeff(int n, long mod) {
this.mod = mod;
fact = new long[n + 1];
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = i;
fact[i] %= mod;
fact[i] *= fact[i - 1];
fact[i] %= mod;
}
}
Coeff(int n, int m, long mod) {
// n > m
cycleWay = true;
this.mod = mod;
C = new long[n + 1][m + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= Math.min(i, m); j++) {
if (j == 0 || j == i) {
C[i][j] = 1;
} else {
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
C[i][j] %= mod;
}
}
}
}
public long C(int n, int m) {
if (cycleWay) {
return C[n][m];
}
return fC(n, m);
}
private long fC(int n, int m) {
return (fact[n] * inv(fact[n - m] * fact[m] % mod)) % mod;
}
private long inv(long r) {
if (r == 1)
return 1;
return ((mod - mod / r) * inv(mod % r)) % mod;
}
}
class Pair {
int first;
int second;
public int getFirst() {
return first;
}
public int getSecond() {
return second;
}
}
class MultisetTree<T> {
int size = 0;
TreeMap<T, Integer> mp = new TreeMap<>();
void add(T x) {
mp.merge(x, 1, Integer::sum);
size++;
}
void remove(T x) {
if (mp.containsKey(x)) {
mp.merge(x, -1, Integer::sum);
if (mp.get(x) == 0) {
mp.remove(x);
}
size--;
}
}
T greatest() {
return mp.lastKey();
}
int size() {
return size;
}
int diffSize() {
return mp.size();
}
}
class Multiset<T> {
int size = 0;
Map<T, Integer> mp = new HashMap<>();
void add(T x) {
mp.merge(x, 1, Integer::sum);
size++;
}
void remove(T x) {
if (mp.containsKey(x)) {
mp.merge(x, -1, Integer::sum);
if (mp.get(x) == 0) {
mp.remove(x);
}
size--;
}
}
int size() {
return size;
}
int diffSize() {
return mp.size();
}
}
static class Range {
int l, r;
int id;
public int getL() {
return l;
}
public int getR() {
return r;
}
public Range(int l, int r, int id) {
this.l = l;
this.r = r;
this.id = id;
}
}
static class Array {
static Range[] readRanges(int n, FastScanner in) {
Range[] result = new Range[n];
for (int i = 0; i < n; i++) {
result[i] = new Range(in.nextInt(), in.nextInt(), i);
}
return result;
}
static public Integer[] read(int n, FastScanner in) {
Integer[] out = new Integer[n];
for (int i = 0; i < out.length; i++) {
out[i] = in.nextInt();
}
return out;
}
static public int[] readint(int n, FastScanner in) {
int[] out = new int[n];
for (int i = 0; i < out.length; i++) {
out[i] = in.nextInt();
}
return out;
}
}
class Graph {
List<List<Integer>> create(int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
}
return graph;
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream io) {
br = new BufferedReader(new InputStreamReader(io));
}
public String line() {
String result = "";
try {
result = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
void run_t_tests() {
int t = in.nextInt();
int i = 0;
while (t-- > 0) {
solve(in, out, i++);
}
}
void run_one() {
solve(in, out, -1);
}
@Override
public void run() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
if (use_n_tests) {
run_t_tests();
} else {
run_one();
}
out.close();
}
static FastScanner in;
static PrintWriter out;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(null, new Main(), "", stack_size);
thread.start();
thread.join();
}
} | Java | ["1\n5 1 3", "1\n6 3 4"] | 1 second | ["1 3", "2 6"] | NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second — third place. | Java 11 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | 58c887568002d947706c448e6faa0f77 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \leq n \leq 10^9$$$, $$$1 \le x, y \le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round. | 1,700 | Print two integers — the minimum and maximum possible overall place Nikolay could take. | standard output | |
PASSED | 179e7a6594c48e7885b39ac244271b8d | train_002.jsonl | 1582448700 | Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
int max, min;
for (int i = 0; i < t; i++) {
int n = input.nextInt();
int x = input.nextInt();
int y = input.nextInt();
if (x + y <= n)
max = x + y - 1;
else
max = n;
if (x + y <= n)
min = 1;
else
min = Math.min(x + y - n + 1, n);
System.out.println(min + " " + max);
}
}
}
| Java | ["1\n5 1 3", "1\n6 3 4"] | 1 second | ["1 3", "2 6"] | NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second — third place. | Java 11 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | 58c887568002d947706c448e6faa0f77 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \leq n \leq 10^9$$$, $$$1 \le x, y \le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round. | 1,700 | Print two integers — the minimum and maximum possible overall place Nikolay could take. | standard output | |
PASSED | bc9da110f44dc22db6924e5148b5eebe | train_002.jsonl | 1582448700 | Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question. | 256 megabytes |
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class b
{
public static void print(String str,int val){
System.out.println(str+" "+val);
}
public long gcd(long a, long b) {
if (b==0L) return a;
return gcd(b,a%b);
}
public static void debug(long[][] arr){
int len = arr.length;
for(int i=0;i<len;i++){
System.out.println(Arrays.toString(arr[i]));
}
}
public static void debug(int[][] arr){
int len = arr.length;
for(int i=0;i<len;i++){
System.out.println(Arrays.toString(arr[i]));
}
}
public static void debug(String[] arr){
int len = arr.length;
for(int i=0;i<len;i++){
System.out.println(arr[i]);
}
}
public static void print(int[] arr){
int len = arr.length;
for(int i=0;i<len;i++){
System.out.print(arr[i]+" ");
}
System.out.print('\n');
}
public static void print(String[] arr){
int len = arr.length;
for(int i=0;i<len;i++){
System.out.print(arr[i]+" ");
}
System.out.print('\n');
}
public static void print(long[] arr){
int len = arr.length;
for(int i=0;i<len;i++){
System.out.print(arr[i]+" ");
}
System.out.print('\n');
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
public FastReader(String path) throws FileNotFoundException {
br = new BufferedReader(new FileReader(path));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args)
{
FastReader s=new FastReader();
int t = s.nextInt();
for(int tt=0;tt<t;tt++){
int n = s.nextInt();
int x = s.nextInt();
int y = s.nextInt();
int min = Math.max(1,Math.min(n,x+y-n+1));
int max = Math.min(n,x+y-1);
System.out.println(min+" "+max);
}
}
// OutputStream out = new BufferedOutputStream( System.out );
// for(int i=1;i<n;i++){
// out.write((arr[i]+" ").getBytes());
// }
// out.flush();
// long start_time = System.currentTimeMillis();
// long end_time = System.currentTimeMillis();
// System.out.println((end_time - start_time) + "ms");
}
| Java | ["1\n5 1 3", "1\n6 3 4"] | 1 second | ["1 3", "2 6"] | NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second — third place. | Java 11 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | 58c887568002d947706c448e6faa0f77 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \leq n \leq 10^9$$$, $$$1 \le x, y \le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round. | 1,700 | Print two integers — the minimum and maximum possible overall place Nikolay could take. | standard output | |
PASSED | 07240bf613010f5f33fb3d541b1e2955 | train_002.jsonl | 1582448700 | Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question. | 256 megabytes | import java.util.Scanner;
public class IntTree {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0){
int n=s.nextInt();
int x=s.nextInt();
int y=s.nextInt();
int min=Math.max(1,Math.min(n,x+y-n+1));
int max=Math.min(n,x+y-1);
System.out.println(min+" "+max);
}
}
} | Java | ["1\n5 1 3", "1\n6 3 4"] | 1 second | ["1 3", "2 6"] | NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second — third place. | Java 11 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | 58c887568002d947706c448e6faa0f77 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \leq n \leq 10^9$$$, $$$1 \le x, y \le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round. | 1,700 | Print two integers — the minimum and maximum possible overall place Nikolay could take. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.