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
4ebc7a93b3c326b7800540ed002325ae
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa > pb, or pa = pb and ta < tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Main p = new Main(); p.solve(); } public void solve(){ int n, k; Scanner in = new Scanner(System.in); n = in.nextInt(); k = in.nextInt(); Team[] m = new Team[n]; for(int i = 0; i < n; i++){ m[i] = new Team(in.nextInt(), in.nextInt()); } Arrays.sort(m, new Comparator<Team>(){ public int compare(Team a, Team b){ if(a.pn != b.pn) return b.pn - a.pn; else return a.tn - b.tn; } }); int pn = m[k - 1].pn, tn = m[k - 1].tn; //System.out.println(higherBound(m, pn, tn)); //System.out.println(lowerBound(m, pn, tn)); //for(int i = 0; i < m.length; i++){ // System.out.print(m[i].pn + " " + m[i].tn + ", "); // } //System.out.println(); System.out.println(higherBound(m, pn, tn) - lowerBound(m, pn, tn) + 1); } public int lowerBound(Team[] arr, int pn, int tn){ int low = 0, high = arr.length - 1, mid, best = -1; while(low <= high){ mid = low + ((high - low) >> 1); //System.out.println("pn, tn: " + arr[mid].pn + ", " + arr[mid].tn + " mid: " + mid + ", pn, tn: " + pn + ", " + tn); if(arr[mid].pn <= pn){ if(arr[mid].pn == pn && arr[mid].tn < tn){ low = mid + 1; continue; } best = mid; high = mid - 1; } else{ low = mid + 1; } } return best; } public int higherBound(Team[] arr, int pn, int tn){ int low = 0, high = arr.length - 1, mid, best = -1; while(low <= high){ mid = low + ((high - low) >> 1); if(arr[mid].pn >= pn){ if(arr[mid].pn == pn && arr[mid].tn > tn){ high = mid - 1; continue; } best = mid; low = mid + 1; } else{ high = mid - 1; } } return best; } } class Team{ int pn = 0, tn = 0; public Team(int pp, int tt){ pn = pp; tn = tt; } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
4890a5c18721b6bc82b169cb7e073166
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.util.ArrayList; import java.util.Collections; public class RankList { public static void main(String[] args) throws java.io.IOException{ java.util.Scanner sc = new java.util.Scanner(new java.io.BufferedInputStream(System.in)); int members = sc.nextInt(); int place = sc.nextInt(); int ranking [] = new int[members]; ArrayList<ass> list = new ArrayList<ass>(); for(int i = 0 ; i < members ; i++){ list.add(new ass(sc.nextInt(), sc.nextInt())); } Collections.sort(list); for(int k = 0 ; k < members ; k++){ ranking [k] +=1; ass o = list.get(k); for ( int i = 0 ; i < members ; i++) { if(i == k) continue; ass l = list.get(i); if ( l.penalty == o.penalty && o.problems == l.problems) { ranking [k] +=1; } } } System.out.println(ranking[place-1]); } } class ass implements Comparable<ass>{ int problems; int penalty; public ass(int problems, int penalty) { this.problems = problems; this.penalty = penalty; } @Override public int compareTo(ass o) { if(this.problems > o.problems || (this.problems == o.problems && this.penalty < o.penalty)) return -1; else if ( this.problems ==o.problems && this.penalty == o.penalty ) return 0; else return 1; } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
29db3496c1fd9a2e755ad6217c467d96
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) throws IOException { Task task = new Task(); task.getData(); task.solve(); task.printAnswer(); } } class Task { ArrayList<Team> teams = new ArrayList<Team>(); int k; StringBuilder answer = new StringBuilder(); public void getData() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] data = br.readLine().split(" "); int numberOfTeams = Integer.parseInt(data[0]); k = Integer.parseInt(data[1]); for (int i = 0; i < numberOfTeams; ++i) { data = br.readLine().split(" "); teams.add(new Team(Integer.parseInt(data[0]), Integer .parseInt(data[1]))); } } public void solve() { for (int i = 0; i + 1 < teams.size(); ++i) { for (int j = 0; j + 1 < teams.size(); ++j) { if (teams.get(j).compareTo(teams.get(j + 1)) == 1) { Team tmp = teams.get(j); teams.set(j, teams.get(j + 1)); teams.set(j + 1, tmp); } } } int kPlaceCounter = 1; for (int i = k - 2; i >= 0; --i) { if (teams.get(i).compareTo(teams.get(k - 1)) == 0) { ++kPlaceCounter; } else { break; } } for (int i = k; i < teams.size(); ++i) { if (teams.get(i).compareTo(teams.get(k - 1)) == 0) { ++kPlaceCounter; } else { break; } } answer.append(kPlaceCounter); } public void printAnswer() { System.out.print(answer); } } class Team { int solved; int totalTime; Team(int solved, int totalTime) { this.solved = solved; this.totalTime = totalTime; } int compareTo(Team two) { if (solved == two.solved && totalTime == two.totalTime) { return 0; } if (solved > two.solved || solved == two.solved && totalTime < two.totalTime) { return -1; } return 1; } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
c174333b0e05b0a275be18c2a28a6dca
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) throws IOException { Task task = new Task(); task.getData(); task.solve(); task.printAnswer(); } } class Task { ArrayList<Team> teams = new ArrayList<Team>(); int k; StringBuilder answer = new StringBuilder(); public void getData() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] data = br.readLine().split(" "); int numberOfTeams = Integer.parseInt(data[0]); k = Integer.parseInt(data[1]); for (int i = 0; i < numberOfTeams; ++i) { data = br.readLine().split(" "); teams.add(new Team(Integer.parseInt(data[0]), Integer .parseInt(data[1]))); } } public void solve() { for (int i = 0; i + 1 < teams.size(); ++i) { for (int j = 0; j + 1 < teams.size(); ++j) { if (teams.get(j).compareTo(teams.get(j + 1)) == 1) { Team tmp = teams.get(j); teams.set(j, teams.get(j + 1)); teams.set(j + 1, tmp); } } } int kPlaceCounter = teams.lastIndexOf(teams.get(k - 1)) - teams.indexOf(teams.get(k - 1)) + 1; answer.append(kPlaceCounter); } public void printAnswer() { System.out.print(answer); } } class Team { int solved; int totalTime; Team(int solved, int totalTime) { this.solved = solved; this.totalTime = totalTime; } int compareTo(Team two) { if (solved == two.solved && totalTime == two.totalTime) { return 0; } if (solved > two.solved || solved == two.solved && totalTime < two.totalTime) { return -1; } return 1; } @Override public boolean equals(Object o) { return (o instanceof Team) && compareTo((Team) o) == 0; } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
6a00bdc7691d9b80d4f7301defddd6a1
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.util.Scanner; /* * @author zezo * date : Feb 10, 2014 */ public class RankList { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); rank[] myrank = new rank[n]; for (int i = 0; i < n; i++) { myrank[i] = new rank(scanner.nextInt(), scanner.nextInt()); } java.util.Arrays.sort(myrank); // List<rank> integersList = Arrays.asList(myrank); // Collections.sort(integersList, Collections.reverseOrder()); // int count = 0; for (int i = 0; i < n; i++) { if (myrank[k - 1].prob == myrank[i].prob) { if (myrank[k - 1].pena == myrank[i].pena) { count++; } } } System.out.println(count); } } class rank implements Comparable<rank> { //implements Comparable<rank> int prob; int pena; public rank(int problem, int penality) { this.prob = problem; this.pena = penality; } @Override public int compareTo(rank o) { if (this.prob < o.prob ) { return +1; } else if (this.prob == o.prob && this.pena > o.pena) { return +1; } else if (this.prob == o.prob && this.pena == o.pena) { return 0; }else { return -1; } } // // }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
6ced2cbb0611421283cb46ca3059b954
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.util.Scanner; /* * @author zezo * date : Feb 10, 2014 */ public class RankList { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); rank[] myrank = new rank[n]; for (int i = 0; i < n; i++) { myrank[i] = new rank(scanner.nextInt(), scanner.nextInt()); } java.util.Arrays.sort(myrank); // List<rank> integersList = Arrays.asList(myrank); // Collections.sort(integersList, Collections.reverseOrder()); // int count = 0; for (int i = 0; i < n; i++) { if (myrank[k - 1].prob == myrank[i].prob) { if (myrank[k - 1].pena == myrank[i].pena) { count++; } } } System.out.println(count); } } class rank implements Comparable<rank> { //implements Comparable<rank> int prob; int pena; public rank(int problem, int penality) { this.prob = problem; this.pena = penality; } @Override public int compareTo(rank o) { if (this.prob < o.prob ) { return +1; } else if (this.prob == o.prob && this.pena > o.pena) { return +1; } else if (this.prob == o.prob && this.pena == o.pena) { return 0; }else { return -1; } } // }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
15043dcbd51dd05fe96bb7ddc46f8240
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int[] a = new int[n]; int[] b = new int[n]; for(int i = 0; i < n; i++) { a[i] = sc.nextInt(); b[i] = sc.nextInt(); } int m = 0; while(true) { int amax = 0, bmin = 55, count = 0; for(int i = 0; i < n; i ++) { if(a[i] > amax) { amax = a[i]; bmin = b[i]; } else if(a[i] == amax) { if(b[i] <= bmin) { amax = a[i]; bmin = b[i]; } } } for(int i = 0; i < n; i ++) { if(a[i] == amax && b[i] == bmin) { a[i] = -1; m++; count++; } } if(m >= k) { System.out.println(count); break; } } } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
85566e7c96a1f10a975e66c3ec72f288
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Ranklist { public static void main(String[]args) throws IOException{ BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); StringTokenizer str=new StringTokenizer(br.readLine()); int n=Integer.parseInt(str.nextToken()); int rank=Integer.parseInt(str.nextToken()); int p[]=new int [n]; int t[]=new int [n]; for (int i = 0; i < t.length; i++) { str=new StringTokenizer(br.readLine()); p[i]=Integer.parseInt(str.nextToken()); t[i]=Integer.parseInt(str.nextToken()); } for (int i = 0; i < p.length; i++) { int max=p[i]; int index=i; for (int j = i+1; j < p.length; j++) { if (p[j]>max) { max=p[j]; index=j; }else if (p[j]==max) { if (t[j]<t[index]) { max=p[j]; index=j; } } } int temp1=p[i]; p[i]=max; p[index]=temp1; int temp2=t[i]; t[i]=t[index]; t[index]=temp2; } int x=p[rank-1]; int y=t[rank-1]; int count=0; for (int i = 0; i < t.length; i++) { if (p[i]==x) { if (t[i]==y) { count++; } } } System.out.println(count); } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
b11d91946e30ffe8136ea0dc91ae7a83
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.io.*; import java.security.SecureRandom; import java.util.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Solution implements Runnable { class Pair implements Comparable<Pair> { int col; int time; int place; public Pair() { } public Pair(int col, int time) { this.col = col; this.time = time; } @Override public int compareTo(Pair arg0) { if (col == arg0.col) { return time - arg0.time; } return arg0.col - col; } } public void solve() throws Exception { int n = sc.nextInt(); int k = sc.nextInt(); ArrayList<Pair> a = new ArrayList<Pair>(); for (int i = 0;i < n; ++ i) { a.add(new Pair(sc.nextInt(), sc.nextInt())); } Collections.sort(a); int place = 1; int placex = 0; int ans2 = 0; int ans1 = 0; for (int i = 0;i < n; ++ i) { if (i > 0 && a.get(i).compareTo(a.get(i - 1)) != 0) { ++ place; ++ placex; } else { ++ placex; } a.get(i).place = place; if (placex == k) { ans1 = place; } } for (int i = 0;i < n; ++ i) { if (a.get(i).place == ans1) { ++ ans2; } } out.println(ans2); } /*--------------------------------------------------------------*/ static String filename = ""; static boolean fromFile = false; BufferedReader in; PrintWriter out; FastScanner sc; public static void main(String[] args) { new Thread(null, new Solution(), "", 1 << 25).start(); } public void run() { try { init(); solve(); } catch (Exception e) { throw new RuntimeException(e); } finally { out.close(); } } void init() throws Exception { if (fromFile) { in = new BufferedReader(new FileReader(filename+".in")); out = new PrintWriter(new FileWriter(filename+".out")); } else { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } sc = new FastScanner(in); } } class FastScanner { BufferedReader reader; StringTokenizer strTok; public FastScanner(BufferedReader reader) { this.reader = reader; } public String nextToken() throws IOException { while (strTok == null || !strTok.hasMoreTokens()) { strTok = new StringTokenizer(reader.readLine()); } return strTok.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public BigInteger nextBigInteger() throws IOException { return new BigInteger(nextToken()); } public BigDecimal nextBigDecimal() throws IOException { return new BigDecimal(nextToken()); } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
4fd79c434a2ca9eaa140e95ec99e335f
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main8 { public static class Node implements Comparable<Node> { int p, t; public Node(int p, int t) { this.p = p; this.t = t; } @Override public int compareTo(Node o) { // TODO Auto-generated method stub int r = new Integer(p).compareTo(o.p); if ( r == 0 ) { return new Integer(t).compareTo(o.t); } else { return -r; } } @Override public String toString() { return p+" "+t; } } public static void main(String[] args) throws IOException { File file = new File("in"); BufferedReader in; if (file.exists()) { in = new BufferedReader(new FileReader(file)); } else { in = new BufferedReader(new InputStreamReader(System.in)); } String line, lines[]; int n, k; lines = in.readLine().split("\\s+"); n = Integer.parseInt(lines[0]); k = Integer.parseInt(lines[1]); Node arrN[] = new Node[n]; for( int i = 0; i < n; i++ ) { lines = in.readLine().split("\\s+"); arrN[i] = new Node(Integer.parseInt(lines[0]), Integer.parseInt(lines[1])); } Arrays.sort(arrN); int arr[] = new int[n]; arr[0] = 1; int pb = arrN[0].p, tb = arrN[0].t; for( int i = 1; i < n; i++ ) { if ( arrN[i].t == tb && arrN[i].p == pb) { arr[i] = arr[i-1]; } else { arr[i] = i+1; tb = arrN[i].t; pb = arrN[i].p; } } int result = 0; for( int i = 0; i < n; i++ ) { if ( arr[i] == arr[k-1] ) { result++; } } //System.out.println(Arrays.toString(arrN)); //System.out.println(Arrays.toString(arr)); System.out.println(result); } public static int[] readInts(String line) { String lines[] = line.split("\\s+"); int[] result = new int[line.length()]; for (int i = 0; i < lines.length; i++) result[i] = Integer.parseInt(lines[i]); return result; } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
f74e546ce426f86d503e515cc201126f
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Stack; import java.util.TreeMap; public class yo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(), m = scanner.nextInt()-1, captureAlias = 0, captureValues = 0; int [] alias = new int[n]; int [] values = new int[n]; for(int i = 0; i < n; i ++){ alias[i] = scanner.nextInt(); values[i] = scanner.nextInt(); } for(int i=0;i<=alias.length-1;i++){ int min, temp, flag; for (int j = i+1;j<alias.length;j++){ if(alias[j]>alias[i]){ temp=alias[i]; alias[i]=alias[j]; alias[j]=temp; flag=values[i]; values[i]=values[j]; values[j]=flag; } } } int index = 0; for(int k = 0; k < alias.length-1; k ++){ if(alias[k]!=alias[k+1]){ for(int i=index;i<=k;i++){ int min, temp, flag; for (int j = i+1;j<k+1;j++){ if(values[j]<values[i]){ temp=values[i]; values[i]=values[j]; values[j]=temp; } } } index = k+1; } } for(int i=index;i<=alias.length;i++){ int min, temp, flag; for (int j = i;j<alias.length;j++){ if(values[j]<values[i]){ temp=values[i]; values[i]=values[j]; values[j]=temp; } } } int count = 0; //System.out.println(alias[m] + " " + captureAlias + " " + values[m] + " " + captureValues + " " + m); captureAlias = alias[m]; captureValues = values[m]; for(int i = 0; i < alias.length; i ++){ //System.out.println(alias[i] + " " + values[i]); if(alias[i]==captureAlias&&values[i]==captureValues){ count++; } } // System.out.println("M " + m + " " + captureAlias + " " + captureValues); System.out.println(count ); } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
93e7939616833bdeae16acb7a597a9bb
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class RankList { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); int k = cin.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = cin.nextInt() * 100 + 50 - cin.nextInt(); } cin.close(); Arrays.sort(a); int sol = 0; for (int i = n - 1; i >= 0; i--) { if (a[i] == a[n - k]) { sol++; } if (a[i] < a[n - k]) { break; } } System.out.println(sol); } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
0673bfa78552567ff78dc9f9bb5a47de
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.io.Writer; import java.math.BigInteger; 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; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { class Team implements Comparable<Team>{ int problems; int time; public Team(int problems, int time) { this.problems = problems; this.time = time; } public int compareTo(Team o){ if (problems > o.problems){ return -1; } if (problems < o.problems){ return 1; } if (time < o.time){ return -1; } if (time > o.time){ return 1; } return 0; } } public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int k = in.readInt(); Team[] mem = new Team[n]; for (int i = 0; i < mem.length; i++) { mem[i] = new Team(in.readInt(), in.readInt()); } Arrays.sort(mem); int count = 0; Team needed = mem[k - 1]; for (int i = 0; i < mem.length; i++) { if (needed.problems == mem[i].problems && needed.time == mem[i].time) { count++; } } out.print(count); } } 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 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); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public void close() { writer.close(); } public void print(int i) { writer.print(i); } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
98332bc6bb6f5526d52a66381024ccf1
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.util.Scanner; public class main { public static void main ( String args [] ){ Scanner input = new Scanner (System.in); int n ; int k ; int [][] array = new int [100][2]; n = input.nextInt(); k = input.nextInt(); for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < 2 ; j ++){ array[i][j] = input.nextInt(); } } for ( int s = 1 ; s < n ; s ++){ int key = array [s][0]; int s1 = s - 1 ; int counter = 0 ; while (s1 >= 0 && array [s1][0] <= array [s][0]){ if(array[s1][0] == array[s][0]){ if(array[s1][1] > array [s][1]){ int temp = array[s1][1] ; array[s1][1]=array[s][1]; array[s][1]=temp; int temp1 = array[s1][0] ; array[s1][0]=array[s][0]; array[s][0]=temp1; s -- ; counter++; } } else if (array[s1][0] < array[s][0]){ int temp2 = array[s1][1] ; array[s1][1]=array[s][1]; array[s][1]=temp2; int temp3 = array[s1][0] ; array[s1][0]=array[s][0]; array[s][0]=temp3; s -- ; counter ++ ; } s1 -- ; } } int counter1 = 0 ; int search1 = array [k-1][0]; int search2 = array [k-1][1]; for(int d = 0 ; d < n ; d ++ ){ if( search1 == array[d][0] && search2 == array[d][1]){ counter1++; } } System.out.println(counter1); } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
63623b7217c2bec9a5cee3149a2612b0
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.util.*; import java.awt.Point; public class Rank { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); int numberteam=input.nextInt(); int place =input.nextInt(); int x; int y; Point[] array = new Point[numberteam]; for(int i=0;i< array.length;i++){ x=input.nextInt(); y=input.nextInt(); array[i]=new Point(x,y); } Point Temp; boolean swapflag=true; for(int i=0;i<array.length && swapflag ;i++){ swapflag=false; for(int j=0;j<(array.length-1-i);j++){ if(array[j].x < array[j+1].x){ Temp = array[j]; array[j] = array[j + 1]; array[j + 1] = Temp; swapflag=true; }else if(array[j].x == array[j+1].x){ if(array[j].y > array[j+1].y){ Temp = array[j]; array[j] = array[j + 1]; array[j + 1] = Temp; swapflag=true; } } } } int count=0; for(int i=0; i< array.length;i++){ if(array[i].x==array[place-1].x && array[i].y==array[place-1].y){ count++; } } System.out.println(count); } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
bd32169cfa10ab1a2806627a7d373416
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.io.*; import java.util.*; /** * * @coder Altynbek Nurgaziyev */ public class C166A { String fileName = ""; class Pair implements Comparable<Pair> { int p, t; public Pair(int p, int t) { this.p = p; this.t = t; } @Override public int compareTo(Pair o) { if (this.p != o.p) { return o.p - this.p; } return this.t - o.t; } } void solution() throws Exception { int n = in.nextInt(); int k = in.nextInt() - 1; Pair[] arr = new Pair[n]; for (int i = 0; i < n; i++) { arr[i] = new Pair(in.nextInt(), in.nextInt()); } Arrays.sort(arr); int counter = 0; for (int i = 0; i < n; i++) { if (arr[i].p == arr[k].p && arr[i].t == arr[k].t) { counter++; } } out.println(counter); } public static void main(String[] args) throws Exception { new C166A().run(); } Scanner in; PrintWriter out; void run() throws Exception { if (fileName.equals("")) { in = new Scanner(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); } else { in = new Scanner(new File(fileName + ".in")); out = new PrintWriter(new File(fileName + ".out")); } solution(); out.flush(); } class Scanner { final BufferedReader br; StringTokenizer st; Scanner(InputStreamReader stream) { br = new BufferedReader(stream); } Scanner(File file) throws Exception { br = new BufferedReader(new FileReader(file)); } String next() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } Integer nextInt() throws Exception { return Integer.parseInt(next()); } Long nextLong() throws Exception { return Long.parseLong(next()); } Double nextDouble() throws Exception { return Double.parseDouble(next()); } } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
15316e5114845a38451265d6fd911dbc
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class Main { static final int INF = 1000000000; public static void main(String[] args) throws Exception { Scanner input = new Scanner(System.in); int n, k; n = input.nextInt(); k = input.nextInt(); int[][] score = new int[n][2]; for (int i = 0; i < n; ++i) { score[i][0] = input.nextInt(); score[i][1] = input.nextInt(); } Arrays.sort(score, new Comparator<int[]>() { public int compare(int[] s1, int[] s2) { if (s1[0] != s2[0]) return s2[0] - s1[0]; return s1[1] - s2[1]; } }); int res = 0; --k; for (int i = k; i >= 0; --i) { if (score[i][0] == score[k][0] && score[i][1] == score[k][1]) ++res; else break; } for (int i = k + 1; i < n; ++i) { if (score[i][0] == score[k][0] && score[i][1] == score[k][1]) ++res; else break; } System.out.println(res); } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
45abb1085444ef1014501d5eff420855
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Rank { public static void main (String args[]) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] data = br.readLine().split(" "); int n = Integer.parseInt(data[0]), place = Integer.parseInt(data[1]); int[][] p = new int[n][2]; for(int i = 0; i < n; i++){ data = br.readLine().split(" "); p[i][0] = Integer.parseInt(data[0]); p[i][1] = Integer.parseInt(data[1]); } int[] puntaje = new int[n]; for (int i = 0; i < n; i++){ puntaje[i] = 1000*p[i][0] - p[i][1]; } Arrays.sort(puntaje); int puntajeBuscado = puntaje[n-place]; int count = 0; for(int i = 0; i < n; i++){ if (puntaje[i] == puntajeBuscado) count++; else if (puntaje[i] > puntajeBuscado) break; } System.out.println(count); } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
80eebd6d98e73482fc48d9ded0381a15
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class ordenRangoLista { //http://codeforces.com/problemset/problem/166/A public static void main(String[] args) { Scanner scaner = new Scanner(System.in); int n =scaner.nextInt(); int k = scaner.nextInt(); int p =0; int t =0; int[] m = new int[n]; for(int i=0; i<n; i++) { p = scaner.nextInt(); t = scaner.nextInt(); m[i] = ((p)*1000)-t; } Arrays.sort(m); int c = 0; //como esta ordenado ascd recorro desde atras hacia adelante for(int i=m.length - 1; i >= 0; i--) { if(m[i] == m[n-k]) { //n-k patron que da el orden c++; } } System.out.println(c); } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
23fc2dfe531840f3ea77b7e481d74e67
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
//package TestOnly.Div2A_113.Code1; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class Main { FastScanner in; PrintWriter out; public void solve() throws IOException { int n = in.nextInt(); int position = in.nextInt() - 1; Team a[] = new Team[n]; for (int i = 0; i < n; i++) { int problems = in.nextInt(); int time = in.nextInt(); a[i] = new Team(problems, time); } int count = 0; Arrays.sort(a); Team temp = a[position]; for (int i = position; i < n; i++) { if (temp.problems == a[i].problems && temp.time == a[i].time) { count++; } } for (int i = position - 1; i >= 0; i--) { if (temp.problems == a[i].problems && temp.time == a[i].time) { count++; } } out.println(count); } public void run() { try { in = new FastScanner(); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } } public static void main(String[] arg) { new Main().run(); } } class Team implements Comparable<Team> { int problems; int time; public Team(int problems, int time) { this.problems = problems; this.time = time; } public int compareTo(Team that) { if (this.problems != that.problems) { return that.problems - this.problems; } return this.time - that.time; } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
ae23f50f1d7ab82526e8f736c7326aa7
train_003.jsonl
1332516600
Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place.You know the rules of comparing the results of two given teams very well. Let's say that team a solved pa problems with total penalty time ta and team b solved pb problems with total penalty time tb. Team a gets a higher place than team b in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team a gets a higher place than team b in the final results' table if either pa &gt; pb, or pa = pb and ta &lt; tb. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of x teams that solved the same number of problems with the same penalty time. Let's also say that y teams performed better than the teams from this group. In this case all teams from the group share places y + 1, y + 2, ..., y + x. The teams that performed worse than the teams from this group, get their places in the results table starting from the y + x + 1-th place.Your task is to count what number of teams from the given list shared the k-th place.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner input = new Scanner(System.in); int n = input.nextInt(); int k = input.nextInt(); //how many teams sharing this place int a[] = new int[n]; for(int i = 0;i < n;i++){ a[i] = input.nextInt() * 100 - input.nextInt(); } Arrays.sort(a); int counter = 0; for(int i = 0;i < n;i++) if(a[i] == a[a.length-k]) ++counter; System.out.println(counter); } }
Java
["7 2\n4 10\n4 10\n4 10\n3 20\n2 1\n2 1\n1 10", "5 4\n3 1\n3 1\n5 3\n3 1\n3 1"]
2 seconds
["3", "4"]
NoteThe final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams.The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams.
Java 7
standard input
[ "sortings", "binary search", "implementation" ]
63e03361531999db408dc0d02de93579
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 50). Then n lines contain the description of the teams: the i-th line contains two integers pi and ti (1 ≤ pi, ti ≤ 50) — the number of solved problems and the total penalty time of the i-th team, correspondingly. All numbers in the lines are separated by spaces.
1,100
In the only line print the sought number of teams that got the k-th place in the final results' table.
standard output
PASSED
d4d340fdeb267855a7cf118d16b11e43
train_003.jsonl
1533047700
You are given two strings $$$s$$$ and $$$t$$$. Both strings have length $$$n$$$ and consist of lowercase Latin letters. The characters in the strings are numbered from $$$1$$$ to $$$n$$$.You can successively perform the following move any number of times (possibly, zero): swap any two adjacent (neighboring) characters of $$$s$$$ (i.e. for any $$$i = \{1, 2, \dots, n - 1\}$$$ you can swap $$$s_i$$$ and $$$s_{i + 1})$$$. You can't apply a move to the string $$$t$$$. The moves are applied to the string $$$s$$$ one after another.Your task is to obtain the string $$$t$$$ from the string $$$s$$$. Find any way to do it with at most $$$10^4$$$ such moves.You do not have to minimize the number of moves, just find any sequence of moves of length $$$10^4$$$ or less to transform $$$s$$$ into $$$t$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author masterbios */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); char a[] = in.next().toCharArray(); char b[] = in.next().toCharArray(); int occura[] = new int[26]; int occurb[] = new int[26]; for (int i = 0; i < n; i++) { occura[a[i] - 'a']++; occurb[b[i] - 'a']++; } boolean signal = false; for (int i = 0; i < 26; i++) { if (occura[i] != occurb[i]) signal = true; } if (signal) { out.println(-1); } else { ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { if (a[i] == b[i]) { continue; } else { int idx = -1; for (int j = i; j < n; j++) { if (a[j] == b[i]) { idx = j; for (int l = idx - 1; l >= i; l--) { list.add(l + 1); } char h = a[idx]; for (int k = idx; k >= i + 1; k--) { a[k] = a[k - 1]; } a[i] = h; break; } } } } out.println(list.size()); for (Integer x : list) { out.print(x + " "); } } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreElements()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["6\nabcdef\nabdfec", "4\nabcd\naccd"]
1 second
["4\n3 5 4 5", "-1"]
NoteIn the first example the string $$$s$$$ changes as follows: "abcdef" $$$\rightarrow$$$ "abdcef" $$$\rightarrow$$$ "abdcfe" $$$\rightarrow$$$ "abdfce" $$$\rightarrow$$$ "abdfec".In the second example there is no way to transform the string $$$s$$$ into the string $$$t$$$ through any allowed moves.
Java 8
standard input
[ "implementation" ]
48e323edc41086cae52cc0e6bdd84e35
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of strings $$$s$$$ and $$$t$$$. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. The third line of the input contains the string $$$t$$$ consisting of $$$n$$$ lowercase Latin letters.
1,200
If it is impossible to obtain the string $$$t$$$ using moves, print "-1". Otherwise in the first line print one integer $$$k$$$ — the number of moves to transform $$$s$$$ to $$$t$$$. Note that $$$k$$$ must be an integer number between $$$0$$$ and $$$10^4$$$ inclusive. In the second line print $$$k$$$ integers $$$c_j$$$ ($$$1 \le c_j &lt; n$$$), where $$$c_j$$$ means that on the $$$j$$$-th move you swap characters $$$s_{c_j}$$$ and $$$s_{c_j + 1}$$$. If you do not need to apply any moves, print a single integer $$$0$$$ in the first line and either leave the second line empty or do not print it at all.
standard output
PASSED
759dc18303e9bc885d61cf107dfe2c19
train_003.jsonl
1533047700
You are given two strings $$$s$$$ and $$$t$$$. Both strings have length $$$n$$$ and consist of lowercase Latin letters. The characters in the strings are numbered from $$$1$$$ to $$$n$$$.You can successively perform the following move any number of times (possibly, zero): swap any two adjacent (neighboring) characters of $$$s$$$ (i.e. for any $$$i = \{1, 2, \dots, n - 1\}$$$ you can swap $$$s_i$$$ and $$$s_{i + 1})$$$. You can't apply a move to the string $$$t$$$. The moves are applied to the string $$$s$$$ one after another.Your task is to obtain the string $$$t$$$ from the string $$$s$$$. Find any way to do it with at most $$$10^4$$$ such moves.You do not have to minimize the number of moves, just find any sequence of moves of length $$$10^4$$$ or less to transform $$$s$$$ into $$$t$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { private static List<Integer> list = new ArrayList<Integer>(); private static void swap(int a, int b) { if (a > b) { int tmp = a; a = b; b = tmp; } for (int i = a; i < b; ++i) { list.add(i); } for (int i = b - 2; i >= a; --i) { list.add(i); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out, true); int n = in.nextInt(); String s = in.next(); String t = in.next(); boolean[] vis = new boolean[55]; int[] num = new int[26]; int[] mp = new int[55]; int ch; for (int i = 0; i < n; ++i) { ch = s.charAt(i) - 'a'; ++num[ch]; ch = t.charAt(i) - 'a'; --num[ch]; mp[i] = s.charAt(i); } for (int i : num) { if (i != 0) { out.println(-1); out.close(); return; } } while (true) { boolean flag = true; for (int i = 0; i < n; ++i) { if (vis[i]) continue; for (int j = 0; j < n; ++j) { if (!vis[j] && mp[i] == t.charAt(j)) { swap(i + 1, j + 1); vis[j] = true; mp[i] = mp[j]; flag = false; break; } } } if (flag) break; } out.println(list.size()); for (int i = 0; i < list.size(); ++i) { if (i != 0) out.print(' '); out.print(list.get(i)); } in.close(); out.close(); } }
Java
["6\nabcdef\nabdfec", "4\nabcd\naccd"]
1 second
["4\n3 5 4 5", "-1"]
NoteIn the first example the string $$$s$$$ changes as follows: "abcdef" $$$\rightarrow$$$ "abdcef" $$$\rightarrow$$$ "abdcfe" $$$\rightarrow$$$ "abdfce" $$$\rightarrow$$$ "abdfec".In the second example there is no way to transform the string $$$s$$$ into the string $$$t$$$ through any allowed moves.
Java 8
standard input
[ "implementation" ]
48e323edc41086cae52cc0e6bdd84e35
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of strings $$$s$$$ and $$$t$$$. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. The third line of the input contains the string $$$t$$$ consisting of $$$n$$$ lowercase Latin letters.
1,200
If it is impossible to obtain the string $$$t$$$ using moves, print "-1". Otherwise in the first line print one integer $$$k$$$ — the number of moves to transform $$$s$$$ to $$$t$$$. Note that $$$k$$$ must be an integer number between $$$0$$$ and $$$10^4$$$ inclusive. In the second line print $$$k$$$ integers $$$c_j$$$ ($$$1 \le c_j &lt; n$$$), where $$$c_j$$$ means that on the $$$j$$$-th move you swap characters $$$s_{c_j}$$$ and $$$s_{c_j + 1}$$$. If you do not need to apply any moves, print a single integer $$$0$$$ in the first line and either leave the second line empty or do not print it at all.
standard output
PASSED
b665dadb58dca918a9b467682c4ba5d4
train_003.jsonl
1533047700
You are given two strings $$$s$$$ and $$$t$$$. Both strings have length $$$n$$$ and consist of lowercase Latin letters. The characters in the strings are numbered from $$$1$$$ to $$$n$$$.You can successively perform the following move any number of times (possibly, zero): swap any two adjacent (neighboring) characters of $$$s$$$ (i.e. for any $$$i = \{1, 2, \dots, n - 1\}$$$ you can swap $$$s_i$$$ and $$$s_{i + 1})$$$. You can't apply a move to the string $$$t$$$. The moves are applied to the string $$$s$$$ one after another.Your task is to obtain the string $$$t$$$ from the string $$$s$$$. Find any way to do it with at most $$$10^4$$$ such moves.You do not have to minimize the number of moves, just find any sequence of moves of length $$$10^4$$$ or less to transform $$$s$$$ into $$$t$$$.
256 megabytes
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; public class B1015 { static class Solver { int N; char[] p, q; void solve(FastScanner s, PrintWriter out) { N = s.nextInt(); p = s.next().toCharArray(); q = s.next().toCharArray(); ArrayList<Integer> moves = new ArrayList<Integer>(); for (int i = 0; i < N; i++) { if (p[i] == q[i]) continue; int j = i + 1; while (j < N && p[j] != q[i]) j++; if (j == N) { out.println(-1); return; } while (j > i) { p[j] = swap(p[j - 1], p[j - 1] = p[j]); moves.add(j--); } } out.println(moves.size()); if (moves.size() > 0) { out.print(moves.remove(0)); for (int x : moves) out.print(" " + x); out.println(); } } char swap(char p, char q) { return p; } } public static void main(String[] args) { FastScanner s = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); Solver solver = new Solver(); solver.solve(s, out); out.close(); } static int swap(int a, int b) { return a; } static Object swap(Object a, Object b) { return a; } static String ts(Object... o) { return Arrays.deepToString(o); } static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } public FastScanner(File f) throws FileNotFoundException { this(new FileInputStream(f)); } public FastScanner(String s) { this.stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)); } 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++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } // Jacob Garbage public int[] nextIntArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = this.nextInt(); return ret; } public int[][] next2DIntArray(int N, int M) { int[][] ret = new int[N][]; for (int i = 0; i < N; i++) ret[i] = this.nextIntArray(M); return ret; } public long[] nextLongArray(int N) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = this.nextLong(); return ret; } public long[][] next2DLongArray(int N, int M) { long[][] ret = new long[N][]; for (int i = 0; i < N; i++) ret[i] = nextLongArray(M); return ret; } public double[] nextDoubleArray(int N) { double[] ret = new double[N]; for (int i = 0; i < N; i++) ret[i] = this.nextDouble(); return ret; } public double[][] next2DDoubleArray(int N, int M) { double[][] ret = new double[N][]; for (int i = 0; i < N; i++) ret[i] = this.nextDoubleArray(M); return ret; } } }
Java
["6\nabcdef\nabdfec", "4\nabcd\naccd"]
1 second
["4\n3 5 4 5", "-1"]
NoteIn the first example the string $$$s$$$ changes as follows: "abcdef" $$$\rightarrow$$$ "abdcef" $$$\rightarrow$$$ "abdcfe" $$$\rightarrow$$$ "abdfce" $$$\rightarrow$$$ "abdfec".In the second example there is no way to transform the string $$$s$$$ into the string $$$t$$$ through any allowed moves.
Java 8
standard input
[ "implementation" ]
48e323edc41086cae52cc0e6bdd84e35
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of strings $$$s$$$ and $$$t$$$. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. The third line of the input contains the string $$$t$$$ consisting of $$$n$$$ lowercase Latin letters.
1,200
If it is impossible to obtain the string $$$t$$$ using moves, print "-1". Otherwise in the first line print one integer $$$k$$$ — the number of moves to transform $$$s$$$ to $$$t$$$. Note that $$$k$$$ must be an integer number between $$$0$$$ and $$$10^4$$$ inclusive. In the second line print $$$k$$$ integers $$$c_j$$$ ($$$1 \le c_j &lt; n$$$), where $$$c_j$$$ means that on the $$$j$$$-th move you swap characters $$$s_{c_j}$$$ and $$$s_{c_j + 1}$$$. If you do not need to apply any moves, print a single integer $$$0$$$ in the first line and either leave the second line empty or do not print it at all.
standard output
PASSED
33ea053a142304afdad41a88fb91850d
train_003.jsonl
1533047700
You are given two strings $$$s$$$ and $$$t$$$. Both strings have length $$$n$$$ and consist of lowercase Latin letters. The characters in the strings are numbered from $$$1$$$ to $$$n$$$.You can successively perform the following move any number of times (possibly, zero): swap any two adjacent (neighboring) characters of $$$s$$$ (i.e. for any $$$i = \{1, 2, \dots, n - 1\}$$$ you can swap $$$s_i$$$ and $$$s_{i + 1})$$$. You can't apply a move to the string $$$t$$$. The moves are applied to the string $$$s$$$ one after another.Your task is to obtain the string $$$t$$$ from the string $$$s$$$. Find any way to do it with at most $$$10^4$$$ such moves.You do not have to minimize the number of moves, just find any sequence of moves of length $$$10^4$$$ or less to transform $$$s$$$ into $$$t$$$.
256 megabytes
import java.util.*; import java.util.ArrayList; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); char s[] = sc.next().toCharArray(); char t[] = sc.next().toCharArray(); List<Integer> l = new ArrayList<>(); for(int i=0;i<n;i++){ int temp=-1; for(int j=i;j<n;j++){ if(t[i] == s[j]){ temp = j; break; } } if(temp == -1){ System.out.println(-1); return; } for(int j=temp-1;j>=i;j--){ l.add(j+1); char temp1 = s[j]; s[j] = s[j+1]; s[j+1] = temp1; } } System.out.println(l.size()); for(int i=0;i<l.size();i++){ System.out.print(l.get(i) + " "); } } }
Java
["6\nabcdef\nabdfec", "4\nabcd\naccd"]
1 second
["4\n3 5 4 5", "-1"]
NoteIn the first example the string $$$s$$$ changes as follows: "abcdef" $$$\rightarrow$$$ "abdcef" $$$\rightarrow$$$ "abdcfe" $$$\rightarrow$$$ "abdfce" $$$\rightarrow$$$ "abdfec".In the second example there is no way to transform the string $$$s$$$ into the string $$$t$$$ through any allowed moves.
Java 8
standard input
[ "implementation" ]
48e323edc41086cae52cc0e6bdd84e35
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of strings $$$s$$$ and $$$t$$$. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. The third line of the input contains the string $$$t$$$ consisting of $$$n$$$ lowercase Latin letters.
1,200
If it is impossible to obtain the string $$$t$$$ using moves, print "-1". Otherwise in the first line print one integer $$$k$$$ — the number of moves to transform $$$s$$$ to $$$t$$$. Note that $$$k$$$ must be an integer number between $$$0$$$ and $$$10^4$$$ inclusive. In the second line print $$$k$$$ integers $$$c_j$$$ ($$$1 \le c_j &lt; n$$$), where $$$c_j$$$ means that on the $$$j$$$-th move you swap characters $$$s_{c_j}$$$ and $$$s_{c_j + 1}$$$. If you do not need to apply any moves, print a single integer $$$0$$$ in the first line and either leave the second line empty or do not print it at all.
standard output
PASSED
0d345c4a7a2b3137e53fa40556ea5b3b
train_003.jsonl
1533047700
You are given two strings $$$s$$$ and $$$t$$$. Both strings have length $$$n$$$ and consist of lowercase Latin letters. The characters in the strings are numbered from $$$1$$$ to $$$n$$$.You can successively perform the following move any number of times (possibly, zero): swap any two adjacent (neighboring) characters of $$$s$$$ (i.e. for any $$$i = \{1, 2, \dots, n - 1\}$$$ you can swap $$$s_i$$$ and $$$s_{i + 1})$$$. You can't apply a move to the string $$$t$$$. The moves are applied to the string $$$s$$$ one after another.Your task is to obtain the string $$$t$$$ from the string $$$s$$$. Find any way to do it with at most $$$10^4$$$ such moves.You do not have to minimize the number of moves, just find any sequence of moves of length $$$10^4$$$ or less to transform $$$s$$$ into $$$t$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { static int min; public static void main(String[] args) throws Exception, IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int n=Integer.parseInt(in.readLine()); String s=in.readLine(); String t=in.readLine(); // Map<String,Integer>map=new HashMap(); // for(int i=0;i<n;i++) // { // String team=String.valueOf(t.charAt(i)); // map.put(team, i); // // } char a[]=s.toCharArray(); char b[]=t.toCharArray(); // for(int i=0;i<n;i++) // { // if(map.containsKey(String.valueOf(s.charAt(i)))) // a[i]=map.get(String.valueOf(s.charAt(i))); // } List list=new ArrayList(); for(int i=0;i<n;i++) { if(a[i]!=b[i]) { min=-1; for(int j=i+1;j<n;j++) { if(a[j]==b[i]) { min=i; for(int q=j-1;q>=i;q--) { char team=a[q]; a[q]=a[q+1];a[q+1]=team; list.add(q+1); } break; } } if(min==-1) {break;} } } if(min==-1) {out.println(-1);} else { out.println(list.size()); for(int i=0;i<list.size();i++) { if(i==list.size()-1)out.println(list.get(i)); else out.print(list.get(i)+" "); }} out.flush(); } }
Java
["6\nabcdef\nabdfec", "4\nabcd\naccd"]
1 second
["4\n3 5 4 5", "-1"]
NoteIn the first example the string $$$s$$$ changes as follows: "abcdef" $$$\rightarrow$$$ "abdcef" $$$\rightarrow$$$ "abdcfe" $$$\rightarrow$$$ "abdfce" $$$\rightarrow$$$ "abdfec".In the second example there is no way to transform the string $$$s$$$ into the string $$$t$$$ through any allowed moves.
Java 8
standard input
[ "implementation" ]
48e323edc41086cae52cc0e6bdd84e35
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of strings $$$s$$$ and $$$t$$$. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. The third line of the input contains the string $$$t$$$ consisting of $$$n$$$ lowercase Latin letters.
1,200
If it is impossible to obtain the string $$$t$$$ using moves, print "-1". Otherwise in the first line print one integer $$$k$$$ — the number of moves to transform $$$s$$$ to $$$t$$$. Note that $$$k$$$ must be an integer number between $$$0$$$ and $$$10^4$$$ inclusive. In the second line print $$$k$$$ integers $$$c_j$$$ ($$$1 \le c_j &lt; n$$$), where $$$c_j$$$ means that on the $$$j$$$-th move you swap characters $$$s_{c_j}$$$ and $$$s_{c_j + 1}$$$. If you do not need to apply any moves, print a single integer $$$0$$$ in the first line and either leave the second line empty or do not print it at all.
standard output
PASSED
c7888f6f55b43fcdb6edead22a0175a9
train_003.jsonl
1440261000
'In Boolean logic, a formula is in conjunctive normal form (CNF) or clausal normal form if it is a conjunction of clauses, where a clause is a disjunction of literals' (cited from https://en.wikipedia.org/wiki/Conjunctive_normal_form)In the other words, CNF is a formula of type , where &amp; represents a logical "AND" (conjunction), represents a logical "OR" (disjunction), and vij are some boolean variables or their negations. Each statement in brackets is called a clause, and vij are called literals.You are given a CNF containing variables x1, ..., xm and their negations. We know that each variable occurs in at most two clauses (with negation and without negation in total). Your task is to determine whether this CNF is satisfiable, that is, whether there are such values of variables where the CNF value is true. If CNF is satisfiable, then you also need to determine the values of the variables at which the CNF is true. It is guaranteed that each variable occurs at most once in each clause.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; /** * @author Pavel Mavrin */ public class C { private void solve() throws IOException { int n = nextInt(); int m = nextInt(); init(n, m); int[] t = new int[m]; boolean[] res = new boolean[m]; Arrays.fill(res, true); for (int i = 0; i < n; i++) { int k = nextInt(); for (int j = 0; j < k; j++) { int x = nextInt(); int xx = Math.abs(x) - 1; int s = Integer.signum(x); if (t[xx] == 0) { t[xx] = s * (i + 1); } else { int ii = Math.abs(t[xx]) - 1; int ss = Integer.signum(t[xx]); // i, s -- ii, ss if (s == ss) { res[xx] = (s == 1); mk[i] = true; mk[ii] = true; continue; } if (s == -1) { addEdge(i, ii, xx); } else { addEdge(ii, i, xx); } t[xx] = n + 1; } } } for (int i = 0; i < m; i++) { if (t[i] != 0 && t[i] != n + 1) { int x = Math.abs(t[i]) - 1; res[i] = (t[i] > 0); mk[x] = true; } } for (int i = 0; i < n; i++) { if (!z[i]) { dfs(i, -1); if (!mk[i]) { out.println("NO"); return; } } } for (int i = 0; i < this.m; i++) { if (mkE[i]) { res[ids[i]] = (i % 2 == 0); } } out.println("YES"); for (int i = 0; i < m; i++) { out.print(res[i] ? "1" : "0"); } out.println(); } void init(int n, int m) { m *= 2; this.n = n; this.m = m; last = 0; head = new int[n]; nx = new int[m]; dst = new int[m]; src = new int[m]; Arrays.fill(head, -1); z = new boolean[n]; mk = new boolean[n]; mkE = new boolean[m]; ids = new int[m]; } void addEdge(int x, int y, int id) { nx[last] = head[x]; src[last] = x; dst[last] = y; ids[last] = id; head[x] = last; last++; nx[last] = head[y]; src[last] = y; dst[last] = x; ids[last] = id; head[y] = last; last++; } private void dfs(int x, int lE) { z[x] = true; int j = head[x]; while (j >= 0) { if ((j ^ 1) != lE) { int y = dst[j]; if (z[y]) { if (!mkE[j ^ 1]) { mk[y] = true; mkE[j] = true; } } else { dfs(y, j); if (mk[y]) { mkE[j ^ 1] = true; mk[x] = true; } else { mkE[j] = true; } } } j = nx[j]; } } int n, m; int[] head; int[] nx; int[] src; int[] dst; boolean[] z; boolean[] mk; boolean[] mkE; int[] ids; int last; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; PrintWriter out = new PrintWriter(System.out); String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } public static void main(String[] args) throws IOException { new C().run(); } private void run() throws IOException { solve(); out.close(); } }
Java
["2 2\n2 1 -2\n2 2 -1", "4 3\n1 1\n1 2\n3 -1 -2 3\n1 -3", "5 6\n2 1 2\n3 1 -2 3\n4 -3 5 4 6\n2 -6 -4\n1 5"]
2 seconds
["YES\n11", "NO", "YES\n100010"]
NoteIn the first sample test formula is . One of possible answer is x1 = TRUE, x2 = TRUE.
Java 7
standard input
[ "constructive algorithms", "dfs and similar", "greedy", "graphs" ]
a63af6feb5d35c5a1f8af5c85c51bfc4
The first line contains integers n and m (1 ≤ n, m ≤ 2·105) — the number of clauses and the number variables, correspondingly. Next n lines contain the descriptions of each clause. The i-th line first contains first number ki (ki ≥ 1) — the number of literals in the i-th clauses. Then follow space-separated literals vij (1 ≤ |vij| ≤ m). A literal that corresponds to vij is x|vij| either with negation, if vij is negative, or without negation otherwise.
2,500
If CNF is not satisfiable, print a single line "NO" (without the quotes), otherwise print two strings: string "YES" (without the quotes), and then a string of m numbers zero or one — the values of variables in satisfying assignment in the order from x1 to xm.
standard output
PASSED
4db2ddd45120419afaa477f0c89424b8
train_003.jsonl
1440261000
'In Boolean logic, a formula is in conjunctive normal form (CNF) or clausal normal form if it is a conjunction of clauses, where a clause is a disjunction of literals' (cited from https://en.wikipedia.org/wiki/Conjunctive_normal_form)In the other words, CNF is a formula of type , where &amp; represents a logical "AND" (conjunction), represents a logical "OR" (disjunction), and vij are some boolean variables or their negations. Each statement in brackets is called a clause, and vij are called literals.You are given a CNF containing variables x1, ..., xm and their negations. We know that each variable occurs in at most two clauses (with negation and without negation in total). Your task is to determine whether this CNF is satisfiable, that is, whether there are such values of variables where the CNF value is true. If CNF is satisfiable, then you also need to determine the values of the variables at which the CNF is true. It is guaranteed that each variable occurs at most once in each clause.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import java.io.BufferedReader; 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; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { int cycleBegin; int cycleEnd; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[] e1 = new int[m]; int[] e2 = new int[m]; boolean[] inv1 = new boolean[m]; boolean[] inv2 = new boolean[m]; Arrays.fill(e1, -1); Arrays.fill(e2, -1); for (int i = 0; i < n; ++i) { int k = in.nextInt(); for (int j = 0; j < k; ++j) { int v = in.nextInt(); int vv = Math.abs(v) - 1; if (e1[vv] < 0) { e1[vv] = i; inv1[vv] = v < 0; } else { e2[vv] = i; inv2[vv] = v < 0; } } } boolean[] done = new boolean[n]; int[] vals = new int[m]; Arrays.fill(vals, -1); Node[] nodes = new Node[n]; for (int i = 0; i < n; ++i) nodes[i] = new Node(i); for (int i = 0; i < m; ++i) { if (e1[i] < 0) continue; if (e2[i] < 0) { vals[i] = inv1[i] ? 0 : 1; done[e1[i]] = true; continue; } if (inv1[i] == inv2[i]) { vals[i] = inv1[i] ? 0 : 1; done[e1[i]] = true; done[e2[i]] = true; continue; } nodes[e1[i]].adj.add(new Edge(nodes[e2[i]], i, inv1[i] ? 0 : 1)); nodes[e2[i]].adj.add(new Edge(nodes[e1[i]], i, inv2[i] ? 0 : 1)); } for (int i = 0; i < n; ++i) if (done[i]) { dfs(vals, done, i, nodes); } Edge[] stack = new Edge[n + 1]; int[] inStack = new int[n + 1]; Arrays.fill(inStack, -1); for (int i = 0; i < n; ++i) if (!done[i]) { if (!dfs2(i, nodes, stack, inStack, 0, null)) { out.println("NO"); return; } for (int j = cycleBegin + 1; j <= cycleEnd; ++j) { Edge e = stack[j]; done[e.dest.index] = true; vals[e.varIndex] = 1 - e.val; } for (int j = cycleBegin + 1; j <= cycleEnd; ++j) { Edge e = stack[j]; dfs(vals, done, e.dest.index, nodes); } } out.println("YES"); for (int i = 0; i < m; ++i) out.print(Math.max(vals[i], 0)); out.println(); } private boolean dfs2(int at, Node[] nodes, Edge[] stack, int[] inStack, int sp, Edge via) { inStack[at] = sp; stack[sp++] = via; for (Edge e : nodes[at].adj) { if (via != null && e.varIndex == via.varIndex) continue; if (inStack[e.dest.index] >= 0) { cycleBegin = inStack[e.dest.index]; cycleEnd = sp; stack[sp] = e; return true; } if (inStack[e.dest.index] == -1) { if (dfs2(e.dest.index, nodes, stack, inStack, sp, e)) return true; } } inStack[at] = -2; return false; } private void dfs(int[] vals, boolean[] done, int at, Node[] nodes) { for (Edge e : nodes[at].adj) { if (!done[e.dest.index]) { done[e.dest.index] = true; vals[e.varIndex] = 1 - e.val; dfs(vals, done, e.dest.index, nodes); } } } static class Edge { Node dest; int varIndex; int val; public Edge(Node dest, int varIndex, int val) { this.dest = dest; this.varIndex = varIndex; this.val = val; } } static class Node { int index; List<Edge> adj = new ArrayList<>(1); public Node(int index) { this.index = index; } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["2 2\n2 1 -2\n2 2 -1", "4 3\n1 1\n1 2\n3 -1 -2 3\n1 -3", "5 6\n2 1 2\n3 1 -2 3\n4 -3 5 4 6\n2 -6 -4\n1 5"]
2 seconds
["YES\n11", "NO", "YES\n100010"]
NoteIn the first sample test formula is . One of possible answer is x1 = TRUE, x2 = TRUE.
Java 7
standard input
[ "constructive algorithms", "dfs and similar", "greedy", "graphs" ]
a63af6feb5d35c5a1f8af5c85c51bfc4
The first line contains integers n and m (1 ≤ n, m ≤ 2·105) — the number of clauses and the number variables, correspondingly. Next n lines contain the descriptions of each clause. The i-th line first contains first number ki (ki ≥ 1) — the number of literals in the i-th clauses. Then follow space-separated literals vij (1 ≤ |vij| ≤ m). A literal that corresponds to vij is x|vij| either with negation, if vij is negative, or without negation otherwise.
2,500
If CNF is not satisfiable, print a single line "NO" (without the quotes), otherwise print two strings: string "YES" (without the quotes), and then a string of m numbers zero or one — the values of variables in satisfying assignment in the order from x1 to xm.
standard output
PASSED
a1eca04ac30ea39e456222f7ca029179
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.util.*; import java.io.*; public class RPGProtaganist { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for(int tt = 0; tt < t; tt++) { long space1 = in.nextLong(); long space2 = in.nextLong(); long s = in.nextLong(); long w = in.nextLong(); long sCost = in.nextLong(); long wCost = in.nextLong(); long max = 0; long count1 = 0; long count2 = 0; long sCount = 0; long wCount = 0; if(sCost < wCost) { for(long i = 0; i <= s; i++) { count1 += Math.min(i * sCost, space1 - (space1 % sCost)); sCount += count1 / sCost; count2 += Math.min((s - i) * sCost, space2 - (space2 % sCost)); sCount += count2 / sCost; wCount += Math.min(w, (space1 - count1) / wCost); wCount += Math.min((space2 - count2) / wCost, w - wCount); max = Math.max(max, sCount + wCount); count1 = 0; count2 = 0; sCount = 0; wCount = 0; } } else { for(long i = 0; i <= w; i++) { count1 += Math.min(i * wCost, space1 - space1 % wCost); wCount += count1 / wCost; count2 += Math.min((w - i) * wCost, space2 - space2 % wCost); wCount += count2 / wCost; sCount += Math.min(s, (space1 - count1) / sCost); sCount += Math.min((space2 - count2) / sCost, s - sCount); max = Math.max(max, sCount + wCount); count1 = 0; count2 = 0; sCount = 0; wCount = 0; } } out.println(max); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
8f6e0af7402a10632dde54d4b83dda5f
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; public class B { public static void main(String[] srgs) throws NumberFormatException, IOException { Scanner nik = new Scanner(System.in); int t = nik.nextInt(); StringBuilder st = new StringBuilder(); while (t-- > 0) { long p = nik.nextLong(); long f = nik.nextLong(); long n1 = nik.nextLong(); long n2 = nik.nextLong(); long w1 = nik.nextLong(); long w2 = nik.nextLong(); long res = 0; for (int i = 0; i <= n1; i++) { long np = p - w1 * i; if (np >= 0) { long temp1 = Math.min(n2, np / w2); long nn2 = n2 - temp1; long temp2 = find(n1 - i, nn2, w1, w2, f); res = Math.max(res, i + temp1 + temp2); } else { break; } } st.append(res + "\n"); } System.out.println(st); } private static long find(long nn1, long nn2, long w1, long w2, long f) { if (w1 > w2) { return find(nn2, nn1, w2, w1, f); } if (w1 * nn1 >= f) { return f / w1; } long temp1 = f - w1 * nn1; return nn1 + Math.min(nn2, temp1 / w2); } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
427566d8603267854da2fd22a2702330
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.util.Scanner; public class Main4 { public static void main(String[] args) { // TODO Auto-generated method stub try (Scanner reader = new Scanner(System.in)) { int cases = reader.nextInt(); for (int i = 0; i < cases; i++) { long max1 = reader.nextLong(); long max2 = reader.nextLong(); long count1 = reader.nextLong(); long count2 = reader.nextLong(); long weight1 = reader.nextLong(); long weight2 = reader.nextLong(); long largerweight = weight1; long largerCount = count1; long smallerWeight = weight2; long smallerCount = count2; if( weight1 < weight2 ) { largerweight = weight2; largerCount = count2; smallerWeight = weight1; smallerCount = count1; } long fewestSmallItems = max1 / smallerWeight + max2 / smallerWeight; fewestSmallItems = fewestSmallItems < smallerCount ? fewestSmallItems : smallerCount; long maxItems = 0; for (int pack1Small = (int)fewestSmallItems, pack2Small = 0 ; pack1Small >= 0; pack1Small--,pack2Small++) { long totalItems = tryItempacks(pack1Small, pack2Small, largerCount, largerweight, smallerCount, smallerWeight, max1, max2); maxItems = maxItems < totalItems ? totalItems : maxItems; } System.out.printf("%d%n", maxItems); } } } public static long tryItempacks(long pack1SmallTry, long pack2smallTry,long largerCount, long largerweight, long smallerCount, long smallerWeight, long max1, long max2) { long pack2 = 0; long smallItem1 = pack1SmallTry; long pack1 = smallItem1 * smallerWeight; long left1 = max1 - pack1; long smallItem2 = pack2smallTry; pack2 = smallItem2 * smallerWeight; long left2 = max2 - pack2; if(left2 < 0 || left1 < 0) { return 0; } long largerItem1 = left1 / largerweight; largerItem1 = largerItem1 < largerCount ? largerItem1 : largerCount; pack1 += largerItem1 * largerweight; left1 = max1 - pack1; long largerItem2 = left2 / largerweight; largerItem2 = largerItem2 < (largerCount - largerItem1) ? largerItem2 : (largerCount - largerItem1); pack2 += largerItem2 * largerweight; left2 = max2 - pack2; return smallItem1 + smallItem2 + largerItem1 + largerItem2; } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
11332b1e7f09e9199382132ea2bb71e4
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { int p=s.nextInt(); int f=s.nextInt(); int res=0; int c1=s.nextInt(); int c2=s.nextInt(); int w1=s.nextInt(); int w2=s.nextInt(); if(p<f) { int temp=p; p=f; f=temp; } if(w1>w2) { int temp=w1; w1=w2; w2=temp; temp=c1; c1=c2; c2=temp; } for(int i=0;i<=Math.min(p/w1, c1);i++) { int x1=i; int y1=(p-w1*i)/w2; int x2=Math.min(c1-x1,f/w1); int y2=Math.min(c2-y1,(f-x2*w1)/w2); res=Math.max(res, x1+x2+y1+y2); } System.out.println(res); } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
84a466933c6995eeb3e3f6249182f367
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
//JHope import java.util.*; import java.io.*; import java.math.*; public class B2 { public static void main(String dynamite[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); long T = Long.parseLong(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); long P = Long.parseLong(st.nextToken()); long F = Long.parseLong(st.nextToken()); st = new StringTokenizer(infile.readLine()); long cnt1 = Long.parseLong(st.nextToken()); long cnt2 = Long.parseLong(st.nextToken()); st = new StringTokenizer(infile.readLine()); long weight1 = Long.parseLong(st.nextToken()); long weight2 = Long.parseLong(st.nextToken()); long res = 0; for(long i=0L; i <= (P/weight1); i++) { long temp = i; long a = cnt1-i; if(a < 0) break; long b = cnt2; long left1 = P-i*weight1; b -= left1/weight2; b = Math.max(b, 0); temp += (int)(cnt2-b); //follower long max = Math.max(calc(a, weight1, b, weight2, F), calc(b, weight2, a, weight1, F)); res = Math.max(res, temp+max); } //switch long temp = P; P = F; F = temp; for(long i=0L; i <= (P/weight1); i++) { temp = i; long a = cnt1-i; if(a < 0) break; long b = cnt2; long left1 = P-i*weight1; b -= left1/weight2; b = Math.max(b, 0); temp += (int)(cnt2-b); //follower long max = Math.max(calc(a, weight1, b, weight2, F), calc(b, weight2, a, weight1, F)); res = Math.max(res, temp+max); } sb.append(res+"\n"); } System.out.print(sb); } public static long calc(long a, long weight1, long b, long weight2, long capacity) { if(capacity/weight1 < a) return (int)(capacity/weight1); long take = a; capacity -= weight1*a; take += (int)Math.min(b, capacity/weight2); return take; } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
92c3d3e40f0e22460acfa43a3d092606
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.util.*; import java.io.*; public final class RPG{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int numTests=sc.nextInt(); for(int j=1;j<=numTests;j++){ long p=sc.nextLong(),f=sc.nextLong(),cnts=sc.nextLong(),cntw=sc.nextLong(),weights=sc.nextLong(),weightw=sc.nextLong(); long mval=0,mval1=0; long ans=0; if(weights>weightw){ long tmp=cnts; cnts=cntw; cntw=tmp; tmp=weights; weights=weightw; weightw=tmp; } for(long i=cnts;i>=0;i--){ long remw=cntw,tmpp=p,tmpf=f; long tmp=0; if(tmpp/weights>=i){ tmp+=i; tmpp-=(i*weights); long mw=Math.min(tmpp/weightw,remw); tmp+=mw; remw-=mw; long ms=Math.min(tmpf/weights,cnts-i); tmp+=ms; tmpf-=(ms*weights); mw=Math.min(tmpf/weightw,remw); tmp+=mw; ans=Math.max(ans,tmp); } } // System.out.println("--"); System.out.println(ans); } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
274e61eddcf145b9e6d30868e79ed0dd
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while (t-- > 0) { int p = scan.nextInt(), f = scan.nextInt(); int cnts = scan.nextInt(), cntw = scan.nextInt();//件数 int s = scan.nextInt(), w = scan.nextInt();//重量 if (w < s) { int temp = s; s = w; w = temp; temp = cnts; cnts = cntw; cntw = temp; } int pcs = Math.min(p/s, cnts);//1 long max = 0; for (int i = 0; i <= pcs; i ++) { long tt = 0; long pcw = Math.min((p - i*s)/w, cntw); tt += i; tt += pcw; long i2 = (long)(cnts - i)*s < f ? cnts-i : f/s;//剩余的第一种件数 tt += i2; long fcw = Math.min(cntw-pcw, (f-i2*s)/w); tt += fcw; max = Math.max(max, tt); } System.out.println(max); } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
b1997468006de4156168f50772b9dc02
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.io.ByteArrayInputStream; import java.io.PrintWriter; import java.util.Scanner; public class B2 { public static void main(String[] args) throws Exception { new B2().go(); } static boolean LOCAL = System.getProperty("ONLINE_JUDGE") == null; Scanner in; String INPUT = "3\r\n" + "33 27\r\n" + "6 10\r\n" + "5 6\r\n" + "100 200\r\n" + "10 10\r\n" + "5 5\r\n" + "1 19\r\n" + "1 3\r\n" + "19 5"; void go() { if (LOCAL) { System.setIn(new ByteArrayInputStream(INPUT.getBytes())); } in = new Scanner(System.in); long startTime = System.currentTimeMillis(); solve(); if (LOCAL) { System.out.printf("[%dms]", System.currentTimeMillis()-startTime); } in.close(); } PrintWriter out = new PrintWriter(System.out); StringBuffer sb = new StringBuffer(); void solve() { int t = in.nextInt(); for (int i=1; i<=t; i++) { solve2(i); } out.flush(); } // end of solve void solve2(int caseNr) { int p = in.nextInt(); int f = in.nextInt(); int cnt_s = in.nextInt(); int cnt_w = in.nextInt(); int s = in.nextInt(); int w = in.nextInt(); // if (caseNr != 3) return; // 1 19 // 1 3 // 19 5 long answer = 0; for (int i = 0; i <= cnt_s; i++) { long compare = givePlayer1Swords(p, f, cnt_s, cnt_w, s, w, i); if (compare == -1) break; // System.out.println(i); // System.out.println(compare); answer = Math.max(answer, compare); } System.out.println(answer); } // end of solve2 long givePlayer1Swords(long p, long f, long cnt_s, long cnt_w, long s, long w, int count) { if (count > cnt_s) { return -1; } if (count * s > p) { return -1; } long answer = 0; if (count > 0) { long player1_sword_count = count; answer += player1_sword_count; p -= count * s; cnt_s -= count; } // if (count == 1) { // System.out.println("answer = "+answer); // } // System.out.println(count); long can_carry = p / w; if (can_carry >= cnt_w) { answer += cnt_w; p -= cnt_w * w; cnt_w = 0; } else { answer += can_carry; cnt_w -= can_carry; p -= can_carry * w; } // if (count == 1) { // System.out.println(can_carry); // System.out.println(answer); // } if (s < w) { if (cnt_s > 0) { can_carry = f / (s); if (can_carry >= cnt_s) { answer += cnt_s; f -= cnt_s * s; cnt_s = 0; } else { answer += can_carry; cnt_s -= can_carry; f -= can_carry * s; } } if (cnt_w > 0) { can_carry = f / (w); if (can_carry >= cnt_w) { answer += cnt_w; f -= cnt_w * w; cnt_w = 0; } else { answer += can_carry; cnt_w -= can_carry; f -= can_carry * w; } } } else { if (cnt_w > 0) { can_carry = f / (w); if (can_carry >= cnt_w) { answer += cnt_w; f -= cnt_w * w; cnt_w = 0; } else { answer += can_carry; cnt_w -= can_carry; f -= can_carry * w; } } if (cnt_s > 0) { can_carry = f / (s); if (can_carry >= cnt_s) { answer += cnt_s; f -= cnt_s * s; cnt_s = 0; } else { answer += can_carry; cnt_s -= can_carry; f -= can_carry * s; } } } return answer; } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
46ab424ace224808cda5787f7e8acaeb
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
// package EducationalRound94; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class ProblemB { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringBuilder print=new StringBuilder(); StringTokenizer st; int test=Integer.parseInt(br.readLine()); while(test--!=0){ st=new StringTokenizer(br.readLine()); int p=Integer.parseInt(st.nextToken()); int f=Integer.parseInt(st.nextToken()); if(p>f){ int temp=f; f=p; p=temp; } st=new StringTokenizer(br.readLine()); int cnta=Integer.parseInt(st.nextToken()); int cntb=Integer.parseInt(st.nextToken()); st=new StringTokenizer(br.readLine()); int s=Integer.parseInt(st.nextToken()); int w=Integer.parseInt(st.nextToken()); if(s>w){ int temp=w; w=s; s=temp; temp=cntb; cntb=cnta; cnta=temp; } int ans=0; for(int i=1;i<=cnta;i++){ long is=1l*i*s; int total=0; int arem=cnta,brem=cntb; if(is<=p){ total+=i; int rem=(int)(p-is); int j=Math.min(rem/w,cntb); total+=j; arem-=i; brem-=j; } else{ int j=Math.min(p/w,cntb); total+=j; brem-=j; } int sf=Math.min(f/s,arem); total+=sf; int rem=f-sf*s; total+=Math.min(rem/w,brem); ans=Math.max(ans,total); } for(int i=1;i<=cntb;i++){ long is=1l*i*w; int total=0; int arem=cnta,brem=cntb; if(is<=p){ total+=i; int rem=(int)(p-is); int j=Math.min(rem/s,cnta); total+=j; arem-=j; brem-=i; } else{ int j=Math.min(p/s,cnta); total+=j; arem-=j; } int sf=Math.min(f/s,arem); total+=sf; int rem=f-sf*s; total+=Math.min(rem/w,brem); ans=Math.max(ans,total); } print.append(ans).append("\n"); } System.out.print(print.toString()); } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
c3ffe5ab544e5119097bdb6c24287067
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class solution{ public static void main(String[] args){ FastReader sc= new FastReader(); int t = sc.nextInt(); while(t-- > 0){ int cap1=sc.nextInt(); int cap2=sc.nextInt(); int swords=sc.nextInt(); int axe=sc.nextInt(); int wts=sc.nextInt(); int wta=sc.nextInt(); if(wts > wta){ int temp = wts; wts=wta; wta=temp; temp=swords; swords=axe; axe=temp; } int ans = Integer.MIN_VALUE; for(int i=0;i <= swords;i++){ int m_sword = Math.min(i , cap1 / wts); int m_axe = Math.min(((cap1 - (m_sword * wts )) / wta) , axe); int f_sword = Math.min( (swords-m_sword) , cap2 /wts ); int f_axe = Math.min(axe - m_axe, (cap2 - (f_sword * wts)) / wta); int max= m_sword + m_axe + f_sword + f_axe; ans =Math.max(ans , max); } System.out.println(ans); } } } 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
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
734685cd0adc1613cb8f04f15adec2bc
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class ProblemB { public static void main(String[] args) throws IOException { InputStream in = System.in; InputReader scan = new InputReader(in); int test = scan.nextInt(); StringBuilder sb = new StringBuilder(); for(int t=0;t<test;t++) { int p = scan.nextInt(); int f = scan.nextInt(); int cnts = scan.nextInt(); int cntw = scan.nextInt(); int s = scan.nextInt(); int w = scan.nextInt(); long total = 0L; for(int i=0;i<=cnts && (i*s)<=p;i++) { int j = Math.min(cntw, Math.max(0,p-i*s)/w); int availableSword = cnts-i; int availableWeapon = cntw-j; int sword = 0; int weapon = 0; if(s<=w) { sword = Math.min(availableSword, f/s); weapon = Math.min(availableWeapon, (f-sword*s)/w); } else { weapon = Math.min(availableWeapon, f/w); sword = Math.min(availableSword, (f-weapon*w)/s); } total = Math.max(total, i+j+sword+weapon); } sb.append(total).append("\n"); } System.out.print(sb); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
2cd7593aca019d3f00fba30d98dbb40b
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class B { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader file = new BufferedReader(new InputStreamReader(System.in)); int inputs = Integer.parseInt(file.readLine()); while(inputs-->0) { StringTokenizer st = new StringTokenizer(file.readLine()); long p = Long.parseLong(st.nextToken()); long f = Long.parseLong(st.nextToken()); st = new StringTokenizer(file.readLine()); long numSwords = Long.parseLong(st.nextToken()); long numAxes = Long.parseLong(st.nextToken()); st = new StringTokenizer(file.readLine()); long swordCost = Long.parseLong(st.nextToken()); long axeCost = Long.parseLong(st.nextToken()); long best = 0; for(int i = 0; i <= numSwords; i++) { long tempP = p, tempF = f, tempSwords = numSwords, tempAxes = numAxes; long taken = 0; if(i*swordCost<=p) { taken += i; tempSwords -= i; tempP -= i*swordCost; } long take = Math.min(tempAxes, tempP/axeCost); taken += take; tempP -= take*axeCost; tempAxes -= take; if(swordCost < axeCost) { take = Math.min(tempSwords, tempF/swordCost); taken += take; tempF -= take*swordCost; tempSwords -= take; take = Math.min(tempAxes, tempF/axeCost); taken += take; } else { take = Math.min(tempAxes, tempF/axeCost); taken += take; tempF -= take*axeCost; tempAxes -= take; take = Math.min(tempSwords, tempF/swordCost); taken += take; } best = Math.max(best, taken); } System.out.println(best); } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
af7a2b517f5a790551f7e55bda671d32
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.io.*; import java.util.*; public class Main extends PrintWriter { private void solve() { int t = sc.nextInt(); for(int tc = 1; tc <= t; tc++) { int p = sc.nextInt(); int f = sc.nextInt(); int cnt_s = sc.nextInt(); int cnt_w = sc.nextInt(); int s = sc.nextInt(); int w = sc.nextInt(); if(s > w) { int temp = s; s = w; w = temp; temp = cnt_s; cnt_s = cnt_w; cnt_w = temp; } int ans = 0; for(int i = 0; i <= cnt_s && i*s <= p; i++) { int rem_s = cnt_s - i; int ii = Math.min(rem_s, f/s); int rem_p = p - i*s; int rem_f = f - ii*s; int j = Math.min(rem_p/w, cnt_w); int rem_w = cnt_w - j; int jj = Math.min(rem_w, rem_f/w); ans = Math.max(ans, i+ii+j+jj); } println(ans); } } // Main() throws FileNotFoundException { super(new File("output.txt")); } // InputReader sc = new InputReader(new FileInputStream("test_input.txt")); Main() { super(System.out); } InputReader sc = new InputReader(System.in); static class InputReader { InputReader(InputStream in) { this.in = in; } InputStream in; private byte[] buf = new byte[16384]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static void main(String[] $) { new Thread(null, new Runnable() { public void run() { long start = System.nanoTime(); try {Main solution = new Main(); solution.solve(); solution.close();} catch (Exception e) {e.printStackTrace(); System.exit(1);} System.err.println((System.nanoTime()-start)/1E9); } }, "1", 1 << 27).start(); } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
8841ebef29b4d23e309c2674aed0e8f7
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) { FastReader fs=new FastReader(); PrintWriter out=new PrintWriter(System.out); int t=1; t=fs.nextInt(); for(int i=1;i<=t;i++) { Main m=new Main(); m.solve(i,out,fs); } out.close(); } ArrayList<ArrayList<Integer>> adj; int tin[],tout[]; int timer; void dfs(int u,int p) { tin[u]=++timer; for(int v:adj.get(u)) { if(v==p)continue; dfs(v,u); } tout[u]=timer; } boolean isAncestor(int u,int v) { return tin[u]<=tin[v]&&tout[u]>=tout[v]; } public void solve(int test,PrintWriter out,FastReader fs){ int a=fs.nextInt(),b=fs.nextInt(); int cs=fs.nextInt(),ca=fs.nextInt(); int ws=fs.nextInt(),wa=fs.nextInt(); if(ws>wa){ int temp=cs; cs=ca; ca=temp; temp=ws; ws=wa; wa=temp; } int ans=0; for(int i=0;i<=cs;i++){ int masterStore=Integer.max(a,b),padawanStore=Integer.min(a,b); int masterSwardKeep=Integer.min(i,masterStore/ws); int masterAxeKeep=Integer.min(ca,(masterStore-masterSwardKeep*ws)/wa); int padawnSwardKeep=Integer.min( (cs-masterSwardKeep) , padawanStore/ws); int padawanAxeKeep=Integer.min( (ca-masterAxeKeep),(padawanStore-padawnSwardKeep*ws)/wa ); ans=Integer.max( masterSwardKeep+masterAxeKeep+padawnSwardKeep+padawanAxeKeep,ans ); } out.println(ans); } } class SegmentTree{ long t[]; SegmentTree(int n){ t=new long[4*n]; } void update(int v,int tl,int tr,int pos,long delta) { if(tl==tr) { t[v]+=delta; }else { int tm=(tl+tr)/2; if(pos<=tm) update(2*v,tl,tm,pos,delta); else update(2*v+1,tm+1,tr,pos,delta); t[v]=t[2*v]+t[2*v+1]; } } long sum(int v,int l,int r,int tl,int tr) { if(l>r)return 0; if(l==tl&&r==tr) { return t[v]; }else { int tm=(tl+tr)/2; return sum(2*v,l,Integer.min(r,tm),tl,tm)+sum(2*v+1,Integer.max(l, tm+1),r,tm+1,tr); } } } class FastReader{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next(){ while(!st.hasMoreTokens()){ try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } int[] nextIntArray(int n){ int a[]=new int[n]; for(int i=0;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]=nextInt(); return a; } char[] nextCharArray(){ char a[]=next().toCharArray(); return a; } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
fcdb611f5cedfc98a7b421bd7630e186
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
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 q2 { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc = new Scanner(System.in); int t= Integer.parseInt(sc.next()); while(t-->0) { long p= Long.parseLong(sc.next()); long f= Long.parseLong(sc.next()); long cs= Long.parseLong(sc.next()); long cw= Long.parseLong(sc.next()); long s = Long.parseLong(sc.next()); long w = Long.parseLong(sc.next()); long tw= p+f; long tw2=cs+cw; long curr= cs*s+cw*w; // if(curr<=tw) // { // System.out.println(tw2); // continue; // } if(s>w) { long temp=w; w=s; s=temp; temp=cw; cw=cs; cs=temp; } long ans=0; for(int i=0;i<=cs;i++) { if(i*s>p) { break; } long tempans=i; long lefts= cs-i; long leftcap= p-(i*s); long fd=Math.min(f/s,lefts); tempans+=fd; long z= Math.min(leftcap/w,cw); tempans+=z; tempans+=Math.min((f-fd*s)/w,cw-z); ans= Math.max(ans, tempans); } System.out.println(ans); } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
b51f4711352f03f4d37b1747dba54e26
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
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 q2 { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc = new Scanner(System.in); int t= Integer.parseInt(sc.next()); while(t-->0) { long p= Long.parseLong(sc.next()); long f= Long.parseLong(sc.next()); long cs= Long.parseLong(sc.next()); long cw= Long.parseLong(sc.next()); long s = Long.parseLong(sc.next()); long w = Long.parseLong(sc.next()); long tw= p+f; long tw2=cs+cw; long curr= cs*s+cw*w; // if(curr<=tw) // { // System.out.println(tw2); // continue; // } if(s>w) { long temp=w; w=s; s=temp; temp=cw; cw=cs; cs=temp; } long ans=0; for(int i=0;i<=cs;i++) { if(i*s>p) { continue; } long tempans=i; long lefts= cs-i; long leftcap= p-(i*s); long fd=Math.min(f/s,lefts); tempans+=fd; long z= Math.min(leftcap/w,cw); tempans+=z; tempans+=Math.min((f-fd*s)/w,cw-z); ans= Math.max(ans, tempans); } System.out.println(ans); } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
8fe93af587023e972f4f9e956e6ad892
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.util.*; import java.io.*; public class B { public static void main(String[] args) throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(f.readLine()); while(t-->0){ StringTokenizer st = new StringTokenizer(f.readLine()); long pp1 = Long.parseLong(st.nextToken()); long pp2 = Long.parseLong(st.nextToken()); long p1 = pp1; long p2 = pp2; st = new StringTokenizer(f.readLine()); int amt1 = Integer.parseInt(st.nextToken()); int amt2 = Integer.parseInt(st.nextToken()); st = new StringTokenizer(f.readLine()); long cost1 = Long.parseLong(st.nextToken()); long cost2 = Long.parseLong(st.nextToken()); long maxcnt = 0; if(cost2 < cost1){ long temp = cost1; cost1 = cost2; cost2 = temp; int temp2 = amt1; amt1 = amt2; amt2 = temp2; } for(int val1 = 0; val1 <= amt1; val1++){ long cnt = 0; p1 = pp1; p2 = pp2; if(cost1*val1 > p1) continue; p1-=(cost1*val1); cnt+=val1; long swordsleft = amt1-val1; long axes1 = Math.min(p1/cost2, amt2); cnt+=axes1; long axesleft = amt2-axes1; if(swordsleft*cost1 <= p2){ p2-=(swordsleft*cost1); cnt+=swordsleft; }else{ long val = (p2/cost1); p2-=(val*cost1); cnt+=val; } if(axesleft*cost2 <= p2){ p2-=(axesleft*cost2); cnt+=axesleft; }else{ long val = (p2/cost2); p2-=(val*cost2); cnt+=val; } maxcnt = Math.max(cnt, maxcnt); } out.println(maxcnt); } out.close(); } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
3a476d91c52a4b15ecf5ce48654f34cf
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class B { public static void main(String[] args) { FastScanner fs=new FastScanner(); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int cap1=fs.nextInt(), cap2=fs.nextInt(); int nSs=fs.nextInt(), nAs=fs.nextInt(); int sWeight=fs.nextInt(), aWeight=fs.nextInt(); if (sWeight>aWeight) { int temp=nSs; nSs=nAs; nAs=temp; temp=sWeight; sWeight=aWeight; aWeight=temp; } long ans=0; for (int toKeep=0; toKeep<=nSs; toKeep++) { if (toKeep*(long)sWeight>cap1) continue; int oKeep=Math.min(nSs-toKeep, cap2/sWeight); int myLeft=cap1-toKeep*sWeight; int hisLeft=cap2-oKeep*sWeight; int myAs=Math.min(myLeft/aWeight, nAs); int hisAs=Math.min(hisLeft/aWeight, nAs-myAs); ans=Math.max(ans, toKeep+oKeep+myAs+hisAs); } System.out.println(ans); } } 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); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
7bb41601fb20d58c9d8c0ebbd6694bda
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
//JDope import java.util.*; import java.io.*; import java.math.*; public class B{ public static void main(String[] omkar) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(in.readLine()); StringBuilder sb = new StringBuilder(); int cases = Integer.parseInt(st.nextToken()); int p, f, cnts, cntw, s, w, a, b, c, d, max, temp; for(int i = 0; i < cases; i++) { st = new StringTokenizer(in.readLine()); p = Integer.parseInt(st.nextToken()); f = Integer.parseInt(st.nextToken()); st = new StringTokenizer(in.readLine()); cnts = Integer.parseInt(st.nextToken()); cntw = Integer.parseInt(st.nextToken()); st = new StringTokenizer(in.readLine()); s = Integer.parseInt(st.nextToken()); w = Integer.parseInt(st.nextToken()); if(s < w) { temp = s; s = w; w = temp; temp = cnts; cnts = cntw; cntw = temp; } a = p/w; b = f/w; if(a+b <= cntw) { sb.append((a+b)); sb.append("\n"); } else { max = 0; for(int j = 0; j <= cntw; j++) { c = p - w*j; d = f - w*(cntw-j); if(j <= a && (cntw-j) <= b) { max = Math.max(c/s+d/s, max); } } sb.append(Math.min(cntw+max, cntw+cnts)); sb.append("\n"); } } System.out.println(sb); } public static int[] readArr(int N, BufferedReader in, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(in.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
121a73a18df90bd6f11629e732a97445
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.io.*; import java.util.*; /* Shortcut Commands makeali : Make ArrayList of Integer makesti : Make Stack of Integer makeqi : Make Queue of Integer makepqi : Make PriorityQueue of Integer makestc : Make Stack of Character mapii : Make Map<Integer,Integer> mappi : Make Map<Pair,Integer> print println printf */ public class B{ public static int helper(int b1,int b2,int n1,int n2,int w1,int w2){ // w1<=w2 int res=0; for(int i=0;i<=n1 && i*w1<=b1;i++){ int j; // if((n1-i)*w1 > b2) // j=b2/w1; // else // j=n1-i; j = Math.min(n1-i,b2/w1); // i*w1 filled in b1 and j*w1 in b2 int r1=Math.min(n2,(b1-(i*w1))/w2); int r2=Math.min(n2-r1,(b2-(j*w1))/w2); // System.out.printf("Check %d %d %d %d\n",i,j,r1,r2); res = Math.max(res,i+j+r1+r2); } return res; } public static void solve()throws IOException{ int b1=sc.nextInt(); int b2=sc.nextInt(); int n1=sc.nextInt(); int n2=sc.nextInt(); int w1=sc.nextInt(); int w2=sc.nextInt(); // fill p bag then f bag // swords,axes,bag1,bag2 // swords in bag1 = s1, axes in bag1 = a1 // swords in bag2 = s2, axes in bag2 = a2 // s1*ws + a1*wa <= p // s2*ws + a2*wa <= f // Max(s1*ws + a1*wa + s2*ws + a2*wa) // Max(ws(s1+s2)+wa(a1+a2)) if(w1<=w2){ // if(b1<b2) // System.out.print(helper(b1,b2,n1,n2,w1,w2)); // else System.out.print(helper(b2,b1,n1,n2,w1,w2)); }else{ // if(b1<b2) // System.out.print(helper(b1,b2,n2,n1,w2,w1)); // else System.out.print(helper(b2,b1,n2,n1,w2,w1)); } } public static void main(String args[])throws IOException{ int t = sc.nextInt(); for(int i=1;i<=t;i++){ solve(); System.out.println(); } } public static void sort(int[] arr,boolean reverse){ ArrayList<Integer> list = new ArrayList<Integer>(); int n =arr.length; for(int i=0;i<n;i++){ list.add(arr[i]); } if(reverse) Collections.sort(list,Collections.reverseOrder()); else Collections.sort(list); for(int i=0;i<n;i++){ arr[i] = list.get(i); } } static class Graph{ int n; ArrayList<Integer>[] g; Graph(int n){ this.n = n; this.g = new ArrayList[n]; for(int i=0;i<n;i++){ this.g[i] = new ArrayList<Integer>(); } } ArrayList<Integer> get(int i){ return this.g[i]; } void add(int a,int b){ this.g[a].add(b); } } static class Pair{ // Implementing equals() and hashCode() // Map<Pair, V> map = //... private final int x; private final int y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Pair)) return false; Pair pair = (Pair) o; return x == pair.x && y == pair.y; } @Override public int hashCode() { int result = x; result = 31 * result + y; return result; } } static FastScanner sc = new FastScanner(); static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } public long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
22dc861f8cc5fc9e53ba0e667b283230
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.io.*; import java.util.*; /* Shortcut Commands makeali : Make ArrayList of Integer makesti : Make Stack of Integer makeqi : Make Queue of Integer makepqi : Make PriorityQueue of Integer makestc : Make Stack of Character mapii : Make Map<Integer,Integer> mappi : Make Map<Pair,Integer> print println printf */ public class B{ public static int helper(int b1,int b2,int n1,int n2,int w1,int w2){ // w1<=w2 int res=0; for(int i=0;i<=n1 && i*w1<=b1;i++){ int j; // if((n1-i)*w1 > b2) // j=b2/w1; // else // j=n1-i; j = Math.min(n1-i,b2/w1); // i*w1 filled in b1 and j*w1 in b2 int r1=Math.min(n2,(b1-(i*w1))/w2); int r2=Math.min(n2-r1,(b2-(j*w1))/w2); // System.out.printf("Check %d %d %d %d\n",i,j,r1,r2); res = Math.max(res,i+j+r1+r2); } return res; } public static void solve()throws IOException{ int b1=sc.nextInt(); int b2=sc.nextInt(); int n1=sc.nextInt(); int n2=sc.nextInt(); int w1=sc.nextInt(); int w2=sc.nextInt(); // fill p bag then f bag // swords,axes,bag1,bag2 // swords in bag1 = s1, axes in bag1 = a1 // swords in bag2 = s2, axes in bag2 = a2 // s1*ws + a1*wa <= p // s2*ws + a2*wa <= f // Max(s1*ws + a1*wa + s2*ws + a2*wa) // Max(ws(s1+s2)+wa(a1+a2)) if(w1<=w2){ if(b1<b2) System.out.print(helper(b1,b2,n1,n2,w1,w2)); else System.out.print(helper(b2,b1,n1,n2,w1,w2)); }else{ if(b1<b2) System.out.print(helper(b1,b2,n2,n1,w2,w1)); else System.out.print(helper(b2,b1,n2,n1,w2,w1)); } } public static void main(String args[])throws IOException{ int t = sc.nextInt(); for(int i=1;i<=t;i++){ solve(); System.out.println(); } } public static void sort(int[] arr,boolean reverse){ ArrayList<Integer> list = new ArrayList<Integer>(); int n =arr.length; for(int i=0;i<n;i++){ list.add(arr[i]); } if(reverse) Collections.sort(list,Collections.reverseOrder()); else Collections.sort(list); for(int i=0;i<n;i++){ arr[i] = list.get(i); } } static class Graph{ int n; ArrayList<Integer>[] g; Graph(int n){ this.n = n; this.g = new ArrayList[n]; for(int i=0;i<n;i++){ this.g[i] = new ArrayList<Integer>(); } } ArrayList<Integer> get(int i){ return this.g[i]; } void add(int a,int b){ this.g[a].add(b); } } static class Pair{ // Implementing equals() and hashCode() // Map<Pair, V> map = //... private final int x; private final int y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Pair)) return false; Pair pair = (Pair) o; return x == pair.x && y == pair.y; } @Override public int hashCode() { int result = x; result = 31 * result + y; return result; } } static FastScanner sc = new FastScanner(); static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } public long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
27bdacf6491572cfc8b1026f6481f43c
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.io.*; import java.util.*; public class Solution { private static boolean useInFile = false; private static boolean useOutFile = false; public static void main(String args[]) throws IOException { InOut inout = new InOut(); Resolver resolver = new Resolver(inout); resolver.solve(); inout.flush(); } private static class Resolver { final long LONG_INF = (long) 1e18; final int INF = (int) (1e9 + 7); final int MOD = 998244353; long f[], inv[]; InOut inout; Resolver(InOut inout) { this.inout = inout; } void initF(int n, int mod) { f = new long[n + 1]; f[1] = 1; for (int i = 2; i <= n; i++) { f[i] = (f[i - 1] * i) % mod; } } void initInv(int n, int mod) { inv = new long[n + 1]; inv[n] = pow(f[n], INF - 2, INF); for (int i = inv.length - 2; i >= 0; i--) { inv[i] = inv[i + 1] * (i + 1) % mod; } } long cmn(int n, int m, int mod) { return f[n] * inv[m] % mod * inv[n - m] % mod; } int d[] = {0, -1, 0, 1, 0}; boolean legal(int r, int c, int n, int m) { return r >= 0 && r < n && c >= 0 && c < m; } void solve() throws IOException { int tt = 1; boolean hvt = true; if (hvt) { tt = nextInt(); // tt = Integer.parseInt(nextLine()); } for (int cs = 1; cs <= tt; cs++) { long rs = 0; long n = nextInt(); long m = nextInt(); long cnts = nextInt(); long cntw = nextInt(); long s = nextInt(); long w = nextInt(); long sum = n + m; for (int i = 0; i <= cnts; i++) { if (i * s > sum) { break; } rs = getRs(rs, n, m, cnts, cntw, s, w, i); rs = getRs(rs, m, n, cnts, cntw, s, w, i); } format("%d", rs); // format("Case #%d: %d", cs, rs); if (cs < tt) { format("\n"); } // flush(); } } private long getRs(long rs, long n, long m, long cnts, long cntw, long s, long w, int i) { if (i * s <= n) { long usew = Math.min(cntw, (n - i * s) / w); long cur = i + usew; long lefts = cnts - i; long leftw = cntw - usew; long left = m; if (s < w) { long tmp = Math.min(lefts, left / s); cur += tmp; cur += Math.min((left - tmp * s) / w, leftw); } else { long tmp = Math.min(leftw, left / w); cur += tmp; cur += Math.min((left - tmp * w) / s, lefts); } rs = Math.max(rs, cur); } return rs; } private void updateSegTree(int n, long l, SegmentTree lft) { long lazy; lazy = 1; for (int j = 1; j <= l; j++) { lazy = (lazy + cmn((int) l, j, INF)) % INF; lft.modify(1, j, j, lazy); } lft.modify(1, (int) (l + 1), n, lazy); } String next() throws IOException { return inout.next(); } String next(int n) throws IOException { return inout.next(n); } String nextLine() throws IOException { return inout.nextLine(); } int nextInt() throws IOException { return inout.nextInt(); } long nextLong(int n) throws IOException { return inout.nextLong(n); } int[] anInt(int i, int j) throws IOException { int a[] = new int[j + 1]; for (int k = i; k <= j; k++) { a[k] = nextInt(); } return a; } long[] anLong(int i, int j, int len) throws IOException { long a[] = new long[j + 1]; for (int k = i; k <= j; k++) { a[k] = nextLong(len); } return a; } void print(String s, boolean nextLine) { inout.print(s, nextLine); } void format(String format, Object... obj) { inout.format(format, obj); } void flush() { inout.flush(); } void swap(long a[], int i, int j) { a[i] ^= a[j]; a[j] ^= a[i]; a[i] ^= a[j]; } int getP(int x, int p[]) { if (p[x] == 0 || p[x] == x) { return x; } return p[x] = getP(p[x], p); } void union(int x, int y, int p[]) { if (x < y) { p[y] = x; } else { p[x] = y; } } boolean topSort() { int n = adj2.length - 1; int d[] = new int[n + 1]; for (int i = 1; i <= n; i++) { for (int j = 0; j < adj2[i].size(); j++) { d[adj2[i].get(j)[0]]++; } } List<Integer> list = new ArrayList<>(); for (int i = 1; i <= n; i++) { if (d[i] == 0) { list.add(i); } } for (int i = 0; i < list.size(); i++) { for (int j = 0; j < adj2[list.get(i)].size(); j++) { int t = adj2[list.get(i)].get(j)[0]; d[t]--; if (d[t] == 0) { list.add(t); } } } return list.size() == n; } class SegmentTreeNode { long defaultVal = 0; int l, r; long val = defaultVal, lazy = defaultVal; SegmentTreeNode(int l, int r) { this.l = l; this.r = r; } } class SegmentTree { SegmentTreeNode tree[]; long inf = Long.MIN_VALUE; SegmentTree(int n) { assert n > 0; tree = new SegmentTreeNode[n << 2]; } SegmentTree build(int k, int l, int r) { if (l > r) { return this; } if (null == tree[k]) { tree[k] = new SegmentTreeNode(l, r); } if (l == r) { return this; } int mid = (l + r) >> 1; build(k << 1, l, mid); build(k << 1 | 1, mid + 1, r); return this; } void pushDown(int k) { if (tree[k].l == tree[k].r) { return; } long lazy = tree[k].lazy; tree[k << 1].val += lazy; tree[k << 1].lazy += lazy; tree[k << 1 | 1].val += lazy; tree[k << 1 | 1].lazy += lazy; tree[k].lazy = 0; } void modify(int k, int l, int r, long val) { if (tree[k].l >= l && tree[k].r <= r) { tree[k].val += val; tree[k].lazy += val; return; } int mid = (tree[k].l + tree[k].r) >> 1; if (mid >= l) { modify(k << 1, l, r, val); } if (mid + 1 <= r) { modify(k << 1 | 1, l, r, val); } tree[k].val = Math.max(tree[k << 1].val, tree[k << 1 | 1].val); } long query(int k, int l, int r) { if (tree[k].l > r || tree[k].r < l) { return inf; } if (tree[k].lazy != 0) { pushDown(k); } if (tree[k].l >= l && tree[k].r <= r) { return tree[k].val; } long ans = Math.max(query(k << 1, l, r), query(k << 1 | 1, l, r)); if (tree[k].l < tree[k].r) { tree[k].val = Math.max(tree[k << 1].val, tree[k << 1 | 1].val); } return ans; } } class BinaryIndexedTree { int n = 1; long C[]; BinaryIndexedTree(int sz) { while (n <= sz) { n <<= 1; } C = new long[n]; } int lowbit(int x) { return x & -x; } void add(int x, long val) { while (x < n) { C[x] += val; x += lowbit(x); } } long getSum(int x) { long res = 0; while (x > 0) { res += C[x]; x -= lowbit(x); } return res; } int binSearch(long sum) { if (sum == 0) { return 0; } int n = C.length; int mx = 1; while (mx < n) { mx <<= 1; } int res = 0; for (int i = mx / 2; i >= 1; i >>= 1) { if (C[res + i] < sum) { sum -= C[res + i]; res += i; } } return res + 1; } } int mnS; int mxS; Map<Integer, TrieNode> SS = new HashMap<>(); class TrieNode { int cnt = 0; boolean isMin = false; boolean isMax = false; int mnIdx = 26; int mxIdx = -1; TrieNode next[]; TrieNode() { next = new TrieNode[26]; } private void insert(TrieNode trie, char ch[], int i, int j) { while (i < ch.length) { int idx = ch[i] - 'a'; if (null == trie.next[idx]) { trie.next[idx] = new TrieNode(); } trie.cnt++; int lastMnIdx = trie.mnIdx; int lastMxIdx = trie.mxIdx; trie.mnIdx = Math.min(trie.mnIdx, idx); trie.mxIdx = Math.max(trie.mxIdx, idx); if (trie.isMin && lastMnIdx != trie.mnIdx) { if (lastMnIdx != 26) { notMin(trie.next[lastMnIdx]); } trie.next[trie.mnIdx].isMin = true; } if (trie.isMax && lastMxIdx != trie.mxIdx) { if (lastMxIdx >= 0) { notMax(trie.next[lastMxIdx]); } trie.next[trie.mxIdx].isMax = true; } trie = trie.next[idx]; i++; } SS.put(j + 1, trie); if (trie.cnt == 0) { if (trie.isMin) { mnS = j + 1; } if (trie.isMax) { mxS = j + 1; } } } private void notMin(TrieNode trie) { while (null != trie) { trie.isMin = false; int val = trie.mnIdx; if (val >= 26 || val < 0 || null == trie.next[val]) { break; } trie = trie.next[val]; } } private void notMax(TrieNode trie) { while (null != trie) { trie.isMax = false; int val = trie.mxIdx; if (val >= 26 || val < 0 || null == trie.next[val]) { break; } trie = trie.next[val]; } } } //Binary tree class TreeNode { int val; int tier = -1; TreeNode parent; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } } //binary tree dfs void tierTree(TreeNode root) { if (null == root) { return; } if (null != root.parent) { root.tier = root.parent.tier + 1; } else { root.tier = 0; } tierTree(root.left); tierTree(root.right); } //LCA start TreeNode[][] lca; TreeNode[] tree; void lcaDfsTree(TreeNode root) { if (null == root) { return; } tree[root.val] = root; TreeNode nxt = root.parent; int idx = 0; while (null != nxt) { lca[root.val][idx] = nxt; nxt = lca[nxt.val][idx]; idx++; } lcaDfsTree(root.left); lcaDfsTree(root.right); } TreeNode lcaTree(TreeNode root, int n, TreeNode x, TreeNode y) throws IOException { if (null == root) { return null; } if (-1 == root.tier) { tree = new TreeNode[n + 1]; tierTree(root); } if (null == lca) { lca = new TreeNode[n + 1][31]; lcaDfsTree(root); } int z = Math.abs(x.tier - y.tier); int xx = x.tier > y.tier ? x.val : y.val; while (z > 0) { final int zz = z; int l = (int) BinSearch.bs(0, 31 , k -> zz < (1 << k)); xx = lca[xx][l].val; z -= 1 << l; } int yy = y.val; if (x.tier <= y.tier) { yy = x.val; } while (xx != yy) { final int xxx = xx; final int yyy = yy; int l = (int) BinSearch.bs(0, 31 , k -> (1 << k) <= tree[xxx].tier && lca[xxx][(int) k] != lca[yyy][(int) k]); xx = lca[xx][l].val; yy = lca[yy][l].val; } return tree[xx]; } //LCA end //graph List<Integer> adj[]; List<int[]> adj2[]; void initGraph(int n, int m, boolean hasW, boolean directed, int type) throws IOException { if (type == 1) { adj = new List[n + 1]; } else { adj2 = new List[n + 1]; } for (int i = 1; i <= n; i++) { if (type == 1) { adj[i] = new ArrayList<>(); } else { adj2[i] = new ArrayList<>(); } } for (int i = 0; i < m; i++) { int f = nextInt(); int t = nextInt(); if (type == 1) { adj[f].add(t); if (!directed) { adj[t].add(f); } } else { int w = hasW ? nextInt() : 0; adj2[f].add(new int[]{t, w}); if (!directed) { adj2[t].add(new int[]{f, w}); } } } } void getDiv(Map<Integer, Integer> map, int n) { int sqrt = (int) Math.sqrt(n); for (int i = 2; i <= sqrt; i++) { int cnt = 0; while (n % i == 0) { cnt++; n /= i; } if (cnt > 0) { map.put(i, cnt); } } if (n > 1) { map.put(n, 1); } } boolean[] generatePrime(int n) { boolean p[] = new boolean[n + 1]; p[2] = true; for (int i = 3; i <= n; i += 2) { p[i] = true; } for (int i = 3; i <= Math.sqrt(n); i += 2) { if (!p[i]) { continue; } for (int j = i * i; j <= n; j += i << 1) { p[j] = false; } } return p; } boolean isPrime(long n) { //determines if n is a prime number int p[] = {2, 3, 5, 233, 331}; int pn = p.length; long s = 0, t = n - 1;//n - 1 = 2^s * t while ((t & 1) == 0) { t >>= 1; ++s; } for (int i = 0; i < pn; ++i) { if (n == p[i]) { return true; } long pt = pow(p[i], t, n); for (int j = 0; j < s; ++j) { long cur = llMod(pt, pt, n); if (cur == 1 && pt != 1 && pt != n - 1) { return false; } pt = cur; } if (pt != 1) { return false; } } return true; } long llMod(long a, long b, long mod) { return (a * b - (long) ((double) a / mod * b + 0.5) * mod + mod) % mod; // long r = 0; // a %= mod; // b %= mod; // while (b > 0) { // if ((b & 1) == 1) { // r = (r + a) % mod; // } // b >>= 1; // a = (a << 1) % mod; // } // return r; } long pow(long a, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans = ans * a; } a = a * a; n >>= 1; } return ans; } long pow(long a, long n, long mod) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans = llMod(ans, a, mod); } a = llMod(a, a, mod); n >>= 1; } return ans; } private long[][] initC(int n) { long c[][] = new long[n][n]; for (int i = 0; i < n; i++) { c[i][0] = 1; } for (int i = 1; i < n; i++) { for (int j = 1; j <= i; j++) { c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; } } return c; } /** * ps: n >= m, choose m from n; */ // private int cmn(long n, long m) { // if (m > n) { // n ^= m; // m ^= n; // n ^= m; // } // m = Math.min(m, n - m); // // long top = 1; // long bot = 1; // for (long i = n - m + 1; i <= n; i++) { // top = (top * i) % MOD; // } // for (int i = 1; i <= m; i++) { // bot = (bot * i) % MOD; // } // // return (int) ((top * pow(bot, MOD - 2, MOD)) % MOD); // } long gcd(long a, long b) { if (a < b) { return gcd(b, a); } while (b != 0) { long tmp = a % b; a = b; b = tmp; } return a; } int[] unique(int a[], Map<Integer, Integer> idx) { int tmp[] = a.clone(); Arrays.sort(tmp); int j = 0; for (int i = 0; i < tmp.length; i++) { if (i == 0 || tmp[i] > tmp[i - 1]) { idx.put(tmp[i], j++); } } int rs[] = new int[j]; j = 0; for (int key : idx.keySet()) { rs[j++] = key; } Arrays.sort(rs); return rs; } boolean isEven(long n) { return (n & 1) == 0; } static class BinSearch { static long bs(long l, long r, IBinSearch sort) throws IOException { while (l < r) { long m = l + (r - l) / 2; if (sort.binSearchCmp(m)) { l = m + 1; } else { r = m; } } return l; } interface IBinSearch { boolean binSearchCmp(long k) throws IOException; } } } private static class InOut { private BufferedReader br; private StreamTokenizer st; private PrintWriter pw; InOut() throws FileNotFoundException { if (useInFile) { System.setIn(new FileInputStream("resources/inout/in.text")); } if (useOutFile) { System.setOut(new PrintStream("resources/inout/out.text")); } br = new BufferedReader(new InputStreamReader(System.in)); st = new StreamTokenizer(br); pw = new PrintWriter(new OutputStreamWriter(System.out)); st.ordinaryChar('\''); st.ordinaryChar('\"'); st.ordinaryChar('/'); } private long[] anLong(int n) throws IOException { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } private String next() throws IOException { st.nextToken(); return st.sval; } private String next(int len) throws IOException { char ch[] = new char[len]; int cur = 0; char c; while ((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t') ; do { ch[cur++] = c; } while (!((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t')); return String.valueOf(ch, 0, cur); } private int nextInt() throws IOException { st.nextToken(); return (int) st.nval; } private long nextLong(int n) throws IOException { return Long.parseLong(next(n)); } private double nextDouble() throws IOException { st.nextToken(); return st.nval; } private String[] nextSS(String reg) throws IOException { return br.readLine().split(reg); } private String nextLine() throws IOException { return br.readLine(); } private void print(String s, boolean newLine) { if (null != s) { pw.print(s); } if (newLine) { pw.println(); } } private void format(String format, Object... obj) { pw.format(format, obj); } private void flush() { pw.flush(); } } private static class FFT { double[] roots; int maxN; public FFT(int maxN) { this.maxN = maxN; initRoots(); } public long[] multiply(int[] a, int[] b) { int minSize = a.length + b.length - 1; int bits = 1; while (1 << bits < minSize) bits++; int N = 1 << bits; double[] aa = toComplex(a, N); double[] bb = toComplex(b, N); fftIterative(aa, false); fftIterative(bb, false); double[] c = new double[aa.length]; for (int i = 0; i < N; i++) { c[2 * i] = aa[2 * i] * bb[2 * i] - aa[2 * i + 1] * bb[2 * i + 1]; c[2 * i + 1] = aa[2 * i] * bb[2 * i + 1] + aa[2 * i + 1] * bb[2 * i]; } fftIterative(c, true); long[] ret = new long[minSize]; for (int i = 0; i < ret.length; i++) { ret[i] = Math.round(c[2 * i]); } return ret; } static double[] toComplex(int[] arr, int size) { double[] ret = new double[size * 2]; for (int i = 0; i < arr.length; i++) { ret[2 * i] = arr[i]; } return ret; } void initRoots() { roots = new double[2 * (maxN + 1)]; double ang = 2 * Math.PI / maxN; for (int i = 0; i <= maxN; i++) { roots[2 * i] = Math.cos(i * ang); roots[2 * i + 1] = Math.sin(i * ang); } } int bits(int N) { int ret = 0; while (1 << ret < N) ret++; if (1 << ret != N) throw new RuntimeException(); return ret; } void fftIterative(double[] array, boolean inv) { int bits = bits(array.length / 2); int N = 1 << bits; for (int from = 0; from < N; from++) { int to = Integer.reverse(from) >>> (32 - bits); if (from < to) { double tmpR = array[2 * from]; double tmpI = array[2 * from + 1]; array[2 * from] = array[2 * to]; array[2 * from + 1] = array[2 * to + 1]; array[2 * to] = tmpR; array[2 * to + 1] = tmpI; } } for (int n = 2; n <= N; n *= 2) { int delta = 2 * maxN / n; for (int from = 0; from < N; from += n) { int rootIdx = inv ? 2 * maxN : 0; double tmpR, tmpI; for (int arrIdx = 2 * from; arrIdx < 2 * from + n; arrIdx += 2) { tmpR = array[arrIdx + n] * roots[rootIdx] - array[arrIdx + n + 1] * roots[rootIdx + 1]; tmpI = array[arrIdx + n] * roots[rootIdx + 1] + array[arrIdx + n + 1] * roots[rootIdx]; array[arrIdx + n] = array[arrIdx] - tmpR; array[arrIdx + n + 1] = array[arrIdx + 1] - tmpI; array[arrIdx] += tmpR; array[arrIdx + 1] += tmpI; rootIdx += (inv ? -delta : delta); } } } if (inv) { for (int i = 0; i < array.length; i++) { array[i] /= N; } } } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
2c576195793ba0aa6e6b79f3d8e73d66
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
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.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author revanth */ 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); BRPGProtagonist solver = new BRPGProtagonist(); solver.solve(1, in, out); out.close(); } static class BRPGProtagonist { public void solve(int testNumber, InputReader in, OutputWriter out) { int t = in.nextInt(); while (t-- > 0) { int p = in.nextInt(), f = in.nextInt(); int cnts = in.nextInt(), cntw = in.nextInt(); int s = in.nextInt(), w = in.nextInt(); if (s > w) { int temp = s; s = w; w = temp; temp = cnts; cnts = cntw; cntw = temp; } int max = 0; // out.println(s+" "+w+" "+cnts+" "+cntw+" "); for (int i = 0; i <= Math.min(cnts, p / s); i++) { int x = Math.min(cntw, (p - s * i) / w); int y = Math.min(cnts - i, f / s); int z = Math.min(cntw - x, (f - s * y) / w); max = Math.max(max, i + x + y + z); } out.println(max); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar; private int snumChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
c884318d13192b0381bacad31c22e4e3
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(new BufferedOutputStream(System.out)); int T = in.nextInt(); for(int test=0;test<T;test++){ long p = in.nextInt(); long f = in.nextInt(); long cs = in.nextInt(); long cw = in.nextInt(); long s = in.nextInt(); long w = in.nextInt(); if(w<s){ long t = s; s=w; w=t; t=cs; cs=cw; cw=t; } long max=0; for(int i=0;i<=cs;i++){ if(s*i>p){ break; } long s1=i; long r1=p-s*i; long w1=Math.min(r1/w,cw); long s2=Math.min(f/s,cs-s1); long r2=f-s*s2; long w2=Math.min(r2/w,cw-w1); long c=s1+w1+s2+w2; if(c>max){ max=c; } } out.println(max); } out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
964946ec26b6bce40f88338dce8697da
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.util.*; import java.util.Map.Entry; import java.io.*; public class A { // static ArrayList<Integer>[] g; static pair[] g; static ArrayList<Integer>[] adj; public static void main(String[] args) throws NumberFormatException, IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); while (t-- > 0) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int p = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int s = Integer.parseInt(st.nextToken()); int a = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); if (x > y) { int te = s; s = a; a = te; te = x; x = y; y = te; } int max = 0; int id = -1; for (int i = 0; i < s; i++) { if (i * 1l * x > n) break; int ans = i; int r = n - i * x; ans += Math.min(a, r / y); int rs = s - i; int ra = a - Math.min(a, r / y); ans += Math.min(rs, p / x); int rp = p - Math.min(rs, p / x) * x; ans += Math.min(ra, rp / y); if (max < ans) { max = ans; id = i; } } int max2 = 0; int te = n; n = p; p = te; for (int i = 0; i < s; i++) { if (i * 1l * x > n) break; int ans = i; int r = n - i * x; ans += Math.min(a, r / y); int rs = s - i; int ra = a - Math.min(a, r / y); ans += Math.min(rs, p / x); int rp = p - Math.min(rs, p / x) * x; ans += Math.min(ra, rp / y); if (max2 < ans) { max2 = ans; id = i; } } max = Math.max(max, max2); int max3 = 0; te = s; s = a; a = te; te = x; x = y; y = te; for (int i = 0; i < s; i++) { if (i * 1l * x > n) break; int ans = i; int r = n - i * x; ans += Math.min(a, r / y); int rs = s - i; int ra = a - Math.min(a, r / y); ans += Math.min(rs, p / x); int rp = p - Math.min(rs, p / x) * x; ans += Math.min(ra, rp / y); if (max2 < ans) { max2 = ans; id = i; } } max = Math.max(max, max3); max3 = 0; te = n; n = p; p = te; for (int i = 0; i < s; i++) { if (i * 1l * x > n) break; int ans = i; int r = n - i * x; ans += Math.min(a, r / y); int rs = s - i; int ra = a - Math.min(a, r / y); ans += Math.min(rs, p / x); int rp = p - Math.min(rs, p / x) * x; ans += Math.min(ra, rp / y); if (max2 < ans) { max2 = ans; id = i; } } max = Math.max(max, max3); pw.println(max); } pw.flush(); } static int m; static int[] state; static boolean[] vis; static HashMap<Integer, Long> hm; static void topologicalSortUtil(int v, boolean visited[], Stack<Integer> stack) { // Mark the current node as visited. visited[v] = true; // Recur for all the vertices adjacent // to thisvertex for (int x : adj[v]) { if (!visited[x]) topologicalSortUtil(x, visited, stack); } // Push current vertex to stack // which stores result stack.push(v); } // The function to do Topological Sort. // It uses recursive topologicalSortUtil() static void topologicalSort() { Stack<Integer> stack = new Stack<Integer>(); // Mark all the vertices as not visited boolean visited[] = new boolean[m]; for (int i = 0; i < m; i++) visited[i] = false; // Call the recursive helper // function to store // Topological Sort starting // from all vertices one by one for (int i = 0; i < m; i++) if (visited[i] == false) topologicalSortUtil(i, visited, stack); // Print contents of stack while (stack.empty() == false) { int u = stack.pop(); if (!hm.containsKey(u)) { continue; } long n = hm.get(u); if (state[u] == 1) { hm.put(g[u].x, n / 2 + hm.getOrDefault(g[u].x, 0l)); hm.put(g[u].y, (n + 1) / 2 + hm.getOrDefault(g[u].y, 0l)); } else { hm.put(g[u].y, n / 2 + hm.getOrDefault(g[u].y, 0l)); hm.put(g[u].x, (n + 1) / 2 + hm.getOrDefault(g[u].x, 0l)); } if (n % 2 == 1) { state[u] = 1 - state[u]; } } } /* * 5 3 L 2 3 R 0 3 L 0 0 */ static int count(String x) { int sum = 0; for (int i = 0; i < x.length(); i++) { sum += x.charAt(i) - '0'; } return sum; } static long x, y, d; static void extendedEuclid(int a, int b) { if (b == 0) { x = 1; y = 0; d = a; return; } extendedEuclid(b, a % b); long x1 = y; long y1 = x - a / b * y; x = x1; y = y1; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int countD(int x) { int c = 0; while (x != 0) { c += x % 10; x /= 10; } return c; } static ArrayList<Integer> intersect(ArrayList<Integer> a, ArrayList<Integer> b) { ArrayList<Integer> res = new ArrayList<>(); if (b.size() != 0) { HashSet<Integer> hm = new HashSet<>(); for (int x : a) hm.add(x); for (int x : b) if (hm.contains(x)) res.add(x); } return res; } static class pair implements Comparable<pair> { int x; int y; public pair(int d, int u) { x = d; y = u; } @Override public int compareTo(pair o) { return x - o.x; } } static class pair2 implements Comparable<pair2> { int x; int y; public pair2(int d, int u) { x = d; y = u; } @Override public int compareTo(pair2 o) { // TODO Auto-generated method stub int x1 = y - x; int x2 = o.y - o.x; return x1 - x2; } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
a254739da8177fbce6bb102e63399181
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class B { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); // code goes here int t = nextInt(br); while (t-- > 0){ int[] in1 = nextIntArray(br, 2); int p = in1[0]; int f = in1[1]; int[] in2 = nextIntArray(br, 2); int cnts = in2[0]; int cntw = in2[1]; int[] in3 = nextIntArray(br, 2); int s = in3[0]; int w = in3[1]; int minw = -1; int c = -1; int maxw = -1; int d = -1; if(s < w){ minw = s; c = cnts; d = cntw; maxw = w; }else { minw = w; c = cntw; d = cnts; maxw = s; } int maxX = p/minw; int max = Integer.MIN_VALUE; for(int i = 0; i <= Math.min(maxX, c); i++){ int r = c - i; r = Math.min(r, f/minw); int xr = p - (i * minw); int yr = f - (r * minw); int ax = Math.min(xr/maxw, d); int ay = Math.min(yr/maxw, d - ax); max = Math.max(max, i + r + ax + ay); } sb.append(max).append("\n"); } System.out.print(sb.toString()); } private static int nextInt(BufferedReader br) throws IOException{ return Integer.parseInt(br.readLine()); } private static int[] nextIntArray(BufferedReader br, int n) throws IOException{ StringTokenizer st = new StringTokenizer(br.readLine()); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = Integer.parseInt(st.nextToken()); } return arr; } static class Pair<A, B>{ A first; B second; public Pair(A first, B second){ this.first = first; this.second = second; } } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
c3cae0021292ad6ab8b2b062df953863
train_003.jsonl
1598366100
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?
256 megabytes
import java.util.*; import java.io.*; public class Solution{ static PrintWriter out=new PrintWriter(System.out); public static void main (String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String[] input=br.readLine().trim().split(" "); int numTestCases=Integer.parseInt(input[0]); while(numTestCases-->0){ input=br.readLine().trim().split(" "); int p=Integer.parseInt(input[0]); int f=Integer.parseInt(input[1]); input=br.readLine().trim().split(" "); int numSwords=Integer.parseInt(input[0]); int numWarAxes=Integer.parseInt(input[1]); input=br.readLine().trim().split(" "); int swordWeight=Integer.parseInt(input[0]); int warAxeWeight=Integer.parseInt(input[1]); out.println(maxWeapons(p,f,numSwords,numWarAxes,swordWeight,warAxeWeight)); } out.flush(); out.close(); } public static int maxWeapons(int p,int f,int numSwords,int numWarAxes,int swordWeight,int warAxeWeight) { int ans=0; for(int i=0;i<=numSwords;i++) { long totalWeight=1L*i*swordWeight; int s=numSwords-i,w=numWarAxes; int count=i; if(totalWeight<=p) { p-=totalWeight; count+=Math.min(w, p/warAxeWeight); w-=Math.min(w, p/warAxeWeight); int minWeight=Math.min(swordWeight, warAxeWeight); int maxWeight=Math.max(swordWeight, warAxeWeight); int temp; if(swordWeight<=warAxeWeight) { temp=Math.min(s, f/minWeight); } else { temp=Math.min(w,f/minWeight); } count+=temp; f-=temp*minWeight; if(swordWeight<=warAxeWeight) { count+=Math.min(w, f/maxWeight); } else { count+=Math.min(s, f/maxWeight); } f+=temp*minWeight; p+=totalWeight; ans=Math.max(count, ans); } } return ans; } }
Java
["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"]
2 seconds
["11\n20\n3"]
NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.
Java 8
standard input
[ "greedy", "math", "brute force" ]
ee32db8e7cdd9561d9215651ff8a262e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.
1,700
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
standard output
PASSED
0c6b02b490cd5e500a5790c72b689ff0
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.*; import java.util.*; public class Taske{ public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.flush();out.close(); } static class TaskE { int n; long a[]; final long max=Long.MAX_VALUE; final long r=(long)(1E13); public void solve(int testNumber, InputReader in, PrintWriter out) { n=in.nextInt();a=new long[n+1]; long ps=0; for(int i=2;i<=n;i+=2){ a[i]=in.nextLong(); long min=max; for(int j=1;j*j<=a[i];j++){ if(a[i]%j==0){ long x=a[i]/j,y=j; if((x+y)%2==0&&x!=y){ long f=(x+y)/2,s=(x-y)/2; if(f>=max/f)continue; if(f*f-a[i]==s*s&&s*s-ps>0){ long val=s*s-ps; min=Math.min(min,val); } } } } if(min>r){ out.print("No");return; } a[i-1]=min; ps+=a[i]+a[i-1]; } out.println("Yes"); for(int i=1;i<=n;i++)out.print(a[i]+" "); } // pair ja[][];long w[];int from[],to[],c[]; // void make(int n,int m,InputReader in){ // ja=new pair[n+1][];w=new long[m];from=new int[m];to=new int[m];c=new int[n+1]; // for(int i=0;i<m;i++){ // int u=in.nextInt(),v=in.nextInt();long wt=in.nextLong(); // c[u]++;c[v]++;from[i]=u;to[i]=v;w[i]=wt; // } // for(int i=1;i<=n;i++){ // ja[i]=new pair[c[i]];c[i]=0; // } // for(int i=0;i<m;i++){ // ja[from[i]][c[from[i]]++]=new pair(to[i],w[i]); // ja[to[i]][c[to[i]]++]=new pair(from[i],w[i]); // } // } // int[] radixSort(int[] f){ return radixSort(f, f.length); } // int[] radixSort(int[] f, int n) // { // int[] to = new int[n]; // { // int[] b = new int[65537]; // for(int i = 0;i < n;i++)b[1+(f[i]&0xffff)]++; // for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; // for(int i = 0;i < n;i++)to[b[f[i]&0xffff]++] = f[i]; // int[] d = f; f = to;to = d; // } // { // int[] b = new int[65537]; // for(int i = 0;i < n;i++)b[1+(f[i]>>>16)]++; // for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; // for(int i = 0;i < n;i++)to[b[f[i]>>>16]++] = f[i]; // int[] d = f; f = to;to = d; // } // return f; // } } static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); st = null; } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
e430d44affbe368ec3fb32ffc7f2bcf1
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.*; public class MissingNumbers { public static void main(String[] args) { FastScanner scanner = new FastScanner(); int N = scanner.nextInt(); long[] arr = scanner.readLongArray(N/2); ArrayList<Long> out = new ArrayList<>(); int p =1; long next = arr[0]; long cur = 0; long prev = 0; while(true) { long temp = cur* cur - next-prev; if (temp <= 0) { cur++; continue; } if (temp > 10000000000000L) break; if (isSquare(temp+prev)) { out.add(temp); out.add(next); prev+=temp + next; if (p == N/2) break; next = arr[p]; p++; } cur++; } if (out.size() < N) { System.out.println("No"); } else { System.out.println("Yes"); StringBuilder stringBuilder = new StringBuilder(); for(long l : out) { stringBuilder.append(l + " "); } System.out.println(stringBuilder.toString()); } } public static boolean isSquare(long a) { long temp = (long)Math.sqrt(a); return temp*temp==a; } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner() { this(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 readNextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readIntArray(int n) { int[] a = new int[n]; for (int idx = 0; idx < n; idx++) { a[idx] = nextInt(); } return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int idx = 0; idx < n; idx++) { a[idx] = nextLong(); } return a; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
db95dd96d9fda1b81f15f6fec17bdafa
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeMap; public class Solution{ static final int max = (int)1e5; public static void main(String[] args) throws IOException { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int tt = 1; while(tt-->0) { int n = fs.nextInt(); int[] x = fs.readArray(n/2); HashSet<Long> set = new HashSet<Long>(); for(int i=1;(long)i*i<=(long)1e11;i++) set.add((long)i*i); ArrayList<Long> list = new ArrayList<Long>(); list.add(0L); int cur = 0; for(int i=1;i<=max && cur<n/2;i++) { long num = (long)i*i; if(set.contains(num+x[cur])) { list.add(num); list.add(num+x[cur]); while((long)i*i!=num+x[cur]) i++; cur++; } } if(list.size()<n+1) { out.println("No"); out.flush(); return; } out.println("Yes"); for(int i=1;i<=n;i++) { out.print(list.get(i)-list.get(i-1)+" "); } out.println(); } out.close(); } 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); int temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } public long nextLong() { return Long.parseLong(next()); } public char nextChar() { return next().toCharArray()[0]; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
a1a79afa409d25688ca7cfd994753938
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ijxjdjd */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { int MAX = (int) (1e9); public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); ArrayList<Integer> nums = new ArrayList<>(); for (int i = 0; i < N / 2; i++) { nums.add(in.nextInt()); } int[][] intervals = new int[N][2]; for (int i = 0; i < N; i++) { intervals[i][0] = MAX; intervals[i][1] = MAX; } intervals[0][0] = 0; intervals[0][1] = 1; for (int i = 1; i < N; i += 2) { for (int div = 1; div < Math.sqrt(nums.get(i / 2)); div++) { if (nums.get(i / 2) % div == 0) { int n = doesWork(nums.get(i / 2), div); if (n >= intervals[i - 1][1]) { if (n + div < intervals[i][1]) { intervals[i][0] = n; intervals[i][1] = n + div; } } n = doesWork(nums.get(i / 2), nums.get(i / 2) / div); if (n >= intervals[i - 1][1]) { if (n + div < intervals[i][1]) { intervals[i][0] = n; intervals[i][1] = n + div; } } } } if (i + 1 < N) { intervals[i + 1][0] = intervals[i][1]; intervals[i + 1][1] = intervals[i][1] + 1; } if (intervals[i][1] == MAX || intervals[i][0] == MAX) { out.println("No"); return; } } out.println("Yes"); for (int i = 0; i < N; i++) { if (i % 2 == 1) { out.print(nums.get(i / 2) + " "); } else { out.print(pow2(intervals[i + 1][0]) - pow2(intervals[i][0]) + " "); } } } long pow2(long N) { return N * N; } int doesWork(int num, int a) { num /= a; if (num < a) { return MAX; } num -= a; if (num % 2 == 1) { return MAX; } return num / 2; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
832bc825f620decae8a8e28c89a067a5
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; public class E { public static PrintWriter out; public static void quit() { out.println("No"); out.close(); System.exit(0); } public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); // Scanner scan = new Scanner(System.in); out = new PrintWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(bf.readLine()); StringTokenizer st = new StringTokenizer(bf.readLine()); int[] a = new int[n/2]; for(int i=0; i<n/2; i++) a[i] = Integer.parseInt(st.nextToken()); boolean[] isPrime = new boolean[100001]; Arrays.fill(isPrime, true); isPrime[0] = false; isPrime[1] = false; for(int i=2; i<100001; i++) { for(int j=2*i; j<100001; j+=i) isPrime[j] = false; } long[] ans = new long[n]; Arrays.fill(ans, -1); long cur_sum = 0; for(int j=0; j<n/2; j++) { if(a[j] % 4 == 2) quit(); int val = a[j]; ArrayList<Integer> divisors = new ArrayList<Integer>(); for(int i=1; 1L*i*i<=1L*val; i++) if(val%i == 0) divisors.add(i); hi: for(int i=divisors.size()-1; i>=0; i--) { if((val/divisors.get(i) - divisors.get(i)) % 2 == 0) { int val1 = (val/divisors.get(i) - divisors.get(i))/2; int val2 = (val/divisors.get(i) + divisors.get(i))/2; if(1L*val1*val1 - cur_sum<1) continue hi; ans[2*j] = 1L*val1*val1 - cur_sum; ans[2*j+1] = a[j]; cur_sum += ans[2*j]; cur_sum += ans[2*j+1]; //out.println(ans[2*j]); break; } } if(ans[2*j] == -1) quit(); } out.println("Yes"); StringBuilder sb = new StringBuilder(); for(int i=0; i<n; i++) sb.append(ans[i] + " "); out.println(sb.toString()); // int n = Integer.parseInt(st.nextToken()); // int n = scan.nextInt(); out.close(); System.exit(0); } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
6b1cf0f9e956b011e206c4ad05c6150f
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class E { public static void main(String[] args) throws IOException { /**/ Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); /*/ Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(new FileInputStream("src/e.in")))); /**/ ArrayList<ArrayList<Integer>> ref = new ArrayList<ArrayList<Integer>>(); ArrayList<ArrayList<Integer>> ref2 = new ArrayList<ArrayList<Integer>>(); for (int i = 0; i <= 200000; i++) { ref.add(new ArrayList<>()); ref2.add(new ArrayList<>()); } for (int i = 0; i <= 100000; i++) { long sq = ((long)i)*i; for (int j = i+1; true; j++) { long sq2 = ((long)j)*j; int diff = (int)(sq2-sq); if (diff>200000) break; ref.get(diff).add(i); ref2.get(diff).add(j); } } int n = sc.nextInt(); long[] ans = new long[n]; long curr = 0; for (int i = 0; i < n/2; i++) { int diff = sc.nextInt(); boolean ret = true; for (int j = 0; j < ref.get(diff).size(); ++j) { long refgj = ref.get(diff).get(j); if (refgj > curr) { ret = false; ans[i*2] = refgj*refgj-curr*curr; ans[i*2+1] = diff; curr = ref2.get(diff).get(j); break; } } if (ret) { System.out.println("No"); return; } } StringBuilder asb = new StringBuilder(); String join = ""; for (int i = 0; i < n; i++) { asb.append(join+ans[i]); join = " "; } System.out.println("Yes"); System.out.println(asb); } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
633169eb2a48cd7ad0c728c300c46427
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import org.omg.CORBA.INTERNAL; import javax.swing.plaf.basic.BasicTreeUI; import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class Main { private static Scanner sc; private static Printer pr; static boolean []visited; static int []color; static int []pow=new int[(int)3e5+1]; static int []count; static boolean check=true; static boolean checkBip=true; static TreeSet<Integer>[]list; private static long aLong=(long)(Math.pow(10,9)+7); static final long div=998244353; static int answer=0; static int[]countSubTree; static int[]colorNode; static boolean con=true; static ArrayList<Integer>dfsPath=new ArrayList<>(); static ArrayList<Integer>ans=new ArrayList<>(); static boolean[]rec; static int min=Integer.MAX_VALUE; static ArrayList<Task>tasks=new ArrayList<>(); static boolean []checkpath; private static void solve() throws IOException { int n=sc.nextInt(); long []arr=new long[n]; int []in=new int[n/2]; ArrayList<Long>[]p=new ArrayList[n/2]; ArrayList<Long>d=new ArrayList<>(); for (int i=0;i<n/2;++i)p[i]=new ArrayList<>(); in[0]=sc.nextInt(); long min=Long.MAX_VALUE; for (int i=1;i*i<=in[0];++i){ if (in[0]%i==0) { if ((in[0]/i-i)%2==0){ int q1=(in[0]/i-i)/2; p[0].add((long)Math.pow((q1+i),2)); if ((long)Math.pow((q1+i),2)<=(long)1e13&&q1+i<min&&(long)Math.pow((q1+i),2)>in[0]){ min=(q1+i); } } } } if (min!=Long.MAX_VALUE) { d.add((long) Math.pow(min, 2) - in[0]); d.add((long) Math.pow(min, 2)); }else { System.out.println("No"); System.exit(0); } for (int i=1;i<n/2;++i){ int num=sc.nextInt(); in[i]=num; long mins=Long.MAX_VALUE; boolean check=false; for (int j=1;j*j<=num;++j){ if (num%j==0){ if ((num/j-j)%2==0){ int q=(num/j-j)/2; if ((long)Math.pow((q+j),2)<=(long)1e13&&q+j<mins&&q+j- Math.sqrt(d.get(d.size()-1))>=2&&(long)Math.pow((q+j),2)-num>d.get(d.size()-1)){ p[i].add((long)Math.pow((q+j),2)); mins=q+j; check=true; } } } } if (!check){ System.out.println("No"); System.exit(0); } //System.out.println("mins:"+mins); //System.out.println("***********"); d.add((long)Math.pow(mins,2)-num); d.add((long)Math.pow(mins,2)); } //for (int i=0;i<n/2;++i) System.out.println("list:"+i+" "+p[i]); //System.out.println("d:"+d); pr.println("Yes"); pr.print(d.get(0)+" "); for (int i=1;i<d.size();++i)pr.print((d.get(i)-d.get(i-1))+" "); } public static class dsu{ int []rank; int[]parent; long []sum; int n; public dsu(int n) { this.n = n; parent=new int[n]; rank=new int[n]; sum=new long[n]; for (int i=0;i<n;++i){ rank[i]=1; parent[i]=i; sum[i]=0; } } public int dad(int child){ if (parent[child]==child)return child; else return dad(parent[child]); } public void merge(int u,int v){ int dadU=dad(u); int dadV=dad(v); if (dadU==dadV)return ; if (u!=v){ if (rank[u]<rank[v]){ parent[u]=v; sum[v]+=sum[u]; } else if (rank[u]==rank[v]){ parent[u]=v; sum[v]+=sum[u]; } else{ parent[v]=u; sum[u]+=sum[v]; } } } } public static long power(long x,long y){ if (y==0) return 1%aLong; long u=power(x,y/2); u=((u)*(u))%aLong; if (y%2==1) u=(u*(x%aLong))%aLong; return u; } public static class Task{ int locX; int locY; int turn; public Task(int w,int z,int v) { this.locX = w; this.locY=z; this.turn=v; } } public static void printSolve(StringBuilder str, int[] colors, int n,int color){ for(int i = 1;i<=n;++i) if(colors[i] == color) str.append(i + " "); str.append('\n'); } public static class pairTask{ int val; int size; public pairTask(int x, int y) { this.val = x; this.size = y; } } public static void subTree(int src,int parent,int x){ countSubTree[src]=1; if (src==x) { checkpath[src]=true; //System.out.println("src:"+src); } else checkpath[src]=false; for (int v:list[src]){ if (v==parent) continue; subTree(v,src,x); countSubTree[src]+=countSubTree[v]; checkpath[src]|=checkpath[v]; } } public static boolean prime(long src){ for (int i=2;i*i<=src;i++){ if (src%i==0) return false; } return true; } public static void bfsColor(int src){ Queue<Integer>queue = new ArrayDeque<>(); queue.add(src); while (!queue.isEmpty()){ boolean b=false; int vertex=-1,p=-1; int poll=queue.remove(); for (int v:list[poll]){ if (color [v]==0){ vertex=v; break; } } for (int v:list[poll]){ if (color [v]!=0&&v!=vertex){ p=v; break; } } for (int v:list[p]){ if (color [v]!=0){ color[vertex]=color[v]; b=true; break; } } if (!b){ color[vertex]=color[poll]+1; } } } static int add(int a,int b ){ if (a+b>=div) return (int)(a+b-div); return (int)a+b; } public static int isBipartite(ArrayList<Integer>[]list,int src){ color[src]=0; Queue<Integer>queue=new LinkedList<>(); int []ans={0,0}; queue.add(src); while (!queue.isEmpty()){ ans[color[src=queue.poll()]]++; for (int v:list[src]){ if (color[v]==-1){ queue.add(v); color[v]=color[src]^1; }else if (color[v]==color[src]) check=false; } } return add(pow[ans[0]],pow[ans[1]]); } public static int powerMod(long b, long e){ long ans=1; while (e-->0){ ans=ans*b%div; } return (int)ans; } public static int dfs(int s){ int ans=1; visited[s]=true; for (int k:list[s]){ if (!visited[k]){ ans+=dfs(k); } } return ans; } public static int[] radixSort(int[] f) { int[] to = new int[f.length]; { int[] b = new int[65537]; for (int i = 0; i < f.length; i++) b[1 + (f[i] & 0xffff)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < f.length; i++) to[b[f[i] & 0xffff]++] = f[i]; int[] d = f; f = to; to = d; } { int[] b = new int[65537]; for (int i = 0; i < f.length; i++) b[1 + (f[i] >>> 16)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < f.length; i++) to[b[f[i] >>> 16]++] = f[i]; int[] d = f; f = to; to = d; } return f; } public static long []primeFactor(int n){ long []prime=new long[n+1]; prime[1]=1; for (int i=2;i<=n;i++) prime[i]=((i&1)==0)?2:i; for (int i=3;i*i<=n;i++){ if (prime[i]==i){ for (int j=i*i;j<=n;j+=i){ if (prime[j]==j) prime[j]=i; } } } return prime; } public static StringBuilder binaryradix(long number){ StringBuilder builder=new StringBuilder(); long remainder; while (number!=0) { remainder = number % 2; number >>= 1; builder.append(remainder); } builder.reverse(); return builder; } public static int binarySearch(long[] a, int index,long target) { int l = index; int h = a.length - 1; while (l<=h) { int med = l + (h-l)/2; if(a[med] - target <= target) { l = med + 1; } else h = med - 1; } return h; } public static int val(char c){ return c-'0'; } public static long gcd(long a,long b) { if (a == 0) return b; return gcd(b % a, a); } private static class Pair implements Comparable<Pair> { long x; long y; Pair() { this.x = 0; this.y = 0; } Pair(long x, long y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) return false; Pair other = (Pair) obj; if (this.x == other.x && this.y == other.y) { return true; } return false; } @Override public int compareTo(Pair other) { if (this.x != other.x) return Long.compare(this.x, other.x); return Long.compare(this.y*other.x, this.x*other.y); } } public static void main(String[] args) throws IOException { sc = new Scanner(System.in); pr = new Printer(System.out); solve(); pr.close(); // sc.close(); } private static class Scanner { BufferedReader br; Scanner (InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } private boolean isPrintable(int ch) { return ch >= '!' && ch <= '~'; } private boolean isCRLF(int ch) { return ch == '\n' || ch == '\r' || ch == -1; } private int nextPrintable() { try { int ch; while (!isPrintable(ch = br.read())) { if (ch == -1) { throw new NoSuchElementException(); } } return ch; } catch (IOException e) { throw new NoSuchElementException(); } } String next() { try { int ch = nextPrintable(); StringBuilder sb = new StringBuilder(); do { sb.appendCodePoint(ch); } while (isPrintable(ch = br.read())); return sb.toString(); } catch (IOException e) { throw new NoSuchElementException(); } } int nextInt() { try { // parseInt from Integer.parseInt() boolean negative = false; int res = 0; int limit = -Integer.MAX_VALUE; int radix = 10; int fc = nextPrintable(); if (fc < '0') { if (fc == '-') { negative = true; limit = Integer.MIN_VALUE; } else if (fc != '+') { throw new NumberFormatException(); } fc = br.read(); } int multmin = limit / radix; int ch = fc; do { int digit = ch - '0'; if (digit < 0 || digit >= radix) { throw new NumberFormatException(); } if (res < multmin) { throw new NumberFormatException(); } res *= radix; if (res < limit + digit) { throw new NumberFormatException(); } res -= digit; } while (isPrintable(ch = br.read())); return negative ? res : -res; } catch (IOException e) { throw new NoSuchElementException(); } } long nextLong() { try { // parseLong from Long.parseLong() boolean negative = false; long res = 0; long limit = -Long.MAX_VALUE; int radix = 10; int fc = nextPrintable(); if (fc < '0') { if (fc == '-') { negative = true; limit = Long.MIN_VALUE; } else if (fc != '+') { throw new NumberFormatException(); } fc = br.read(); } long multmin = limit / radix; int ch = fc; do { int digit = ch - '0'; if (digit < 0 || digit >= radix) { throw new NumberFormatException(); } if (res < multmin) { throw new NumberFormatException(); } res *= radix; if (res < limit + digit) { throw new NumberFormatException(); } res -= digit; } while (isPrintable(ch = br.read())); return negative ? res : -res; } catch (IOException e) { throw new NoSuchElementException(); } } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { try { int ch; while (isCRLF(ch = br.read())) { if (ch == -1) { throw new NoSuchElementException(); } } StringBuilder sb = new StringBuilder(); do { sb.appendCodePoint(ch); } while (!isCRLF(ch = br.read())); return sb.toString(); } catch (IOException e) { throw new NoSuchElementException(); } } void close() { try { br.close(); } catch (IOException e) { // throw new NoSuchElementException(); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } static class List { String Word; int length; List(String Word, int length) { this.Word = Word; this.length = length; } } private static class Printer extends PrintWriter { Printer(PrintStream out) { super(out); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
c57728b829921a36572de673d4f5dcaf
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Vadim */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); E solver = new E(); solver.solve(1, in, out); out.close(); } static class E { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.ni() / 2; long[] x = in.nal(n); boolean possible = true; long[] ans = new long[n]; long[] a = new long[n]; long[] b = new long[n]; for (int i = 0; i < n; i++) { long after = Integer.MAX_VALUE; long before = -1; for (long div = 1; div * div < x[i]; div++) { if (x[i] % div == 0) { if (div % 2 == (x[i] / div) % 2) { long curBefore = (x[i] / div - div) / 2; long curAfter = (x[i] / div + div) / 2; if (i == 0 || curBefore > b[i - 1]) { if (curAfter < after) { after = curAfter; before = curBefore; } } } } } if (before <= 0) { possible = false; break; } a[i] = before; b[i] = after; if (i == 0) { ans[i] = a[i] * a[i]; } else { if (a[i] <= b[i - 1]) { possible = false; break; } ans[i] = (a[i] - b[i - 1]) * (a[i] + b[i - 1]); } } if (!possible) { out.println("No"); } else { out.println("Yes"); for (int i = 0; i < n; i++) { if (ans[i] == 0) { throw new RuntimeException(); } out.print(ans[i] + " " + x[i] + " "); } out.println(); } } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); } public String ns() { while (st == null || !st.hasMoreTokens()) { try { String rl = in.readLine(); if (rl == null) { return null; } st = new StringTokenizer(rl); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int ni() { return Integer.parseInt(ns()); } public long nl() { return Long.parseLong(ns()); } public long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
a6e8df1c8f4b4fa295efd8e7d3ddb7e3
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class E { static MyScanner sc; static PrintWriter pw; public static void main(String[] args) throws Throwable { sc = new MyScanner(); pw = new PrintWriter(System.out); int n=sc.nextInt(); long [] a=new long [n]; boolean ok=true; for(int i=0;i<n/2;i++) a[2*i+1]=sc.nextInt(); long lst=0; for(int i=1;i<n;i+=2){ long minX=-1; for(int d=1;d*d<=a[i];d++){ long x=a[i]-d*d; if(x%(2*d)!=0) continue; x/=(2*d); if(x*x>lst) minX=x; } if(minX==-1){ ok=false; break; } a[i-1]=minX*minX-lst; lst=minX*minX+a[i]; } if(ok){ pw.println("Yes"); for(int i=0;i<n;i++) pw.print(a[i]+" "); }else pw.println("No"); pw.flush(); pw.close(); } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
948fd124ce258af0c3a6160e52f87c1c
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.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; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); long[] x = new long[n]; boolean res = true; long sum = 0; for (int i = 0; i < n / 2; i++) { int X = in.nextInt(); x[2 * i + 1] = X; res = false; for (int k = (int) Math.sqrt(X); k > 0; k--) { if ((X - k * k) % (2 * k) == 0) { long a = (X - k * k) / (2 * k); if (a * a > sum) { x[2 * i] = a * a - sum; res = true; break; } } } if (!res) break; sum += x[2 * i]; sum += x[2 * i + 1]; } if (res) { out.println("Yes"); for (int i = 0; i < n; i++) { if (i > 0) out.print(' '); out.print(x[i]); } out.println(); } else { out.println("No"); } } } static class InputReader { final InputStream is; final byte[] buf = new byte[1024]; int pos; int size; public InputReader(InputStream is) { this.is = is; } public int nextInt() { int c = read(); while (isWhitespace(c)) c = read(); int sign = 1; if (c == '-') { sign = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = read(); } while (!isWhitespace(c)); return res * sign; } int read() { if (size == -1) throw new InputMismatchException(); if (pos >= size) { pos = 0; try { size = is.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (size <= 0) return -1; } return buf[pos++] & 255; } static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
1cea900f5d183a4ce6e679547c350164
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class E { static int maxn = 200020; static long a[] = new long[maxn]; static long sum[] = new long[maxn]; static long ans[] = new long[maxn]; static long valid = 10000000000000L; public static void main(String[] aaaa) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int i = 1; i <= n / 2; ++i) { a[i] = scanner.nextLong(); sum[i] = sum[i - 1] + a[i]; } long c = 1, s = 0; for (int i = 1; i <= n / 2; ++i) { while (c * c < s + sum[i - 1]) { c += 1; } // printf("fuck %lld %lld\n", sum[i - 1], c); while (true) { ans[i] = c * c - (s + sum[i - 1]); if (ans[i] > valid) { System.out.println("No"); return; } long tmp = s + ans[i] + sum[i]; double ttmp = (long)(Math.sqrt(tmp)); if (ttmp * ttmp == tmp || (ttmp - 1) * (ttmp - 1) == tmp || (ttmp + 1) * (ttmp + 1) == tmp) { break; } c += 1; } s += ans[i]; } for (int i = 1; i <= n / 2; ++i) { if (ans[i] > valid) { System.out.println("No"); return; } } System.out.println("Yes"); for (int i = 1; i <= n / 2; ++i) { System.out.print(ans[i] + " " + a[i] + " "); } System.out.println(); } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
43b80de4da7ee3a3836b7faacd00cec2
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
public class Main { private static void solve() { int n = ni() / 2; long[] x = new long[n]; for (int i = 0; i < n; i ++) { x[i] = nl(); } long[] ret = new long[n * 2]; long y = 1; long z = 0; long lasty = 0; for (int i = 0; i < n; i ++) { for (;; z ++, y += z * 2 + 1) { if (z > 1000000) { System.out.println("No"); return; } if (sqrtL(y + x[i]) >= 0) { ret[i * 2] = y - lasty; ret[i * 2 + 1] = x[i]; break; } } y += x[i]; z = sqrtL(y) - 1; lasty = y; } System.out.println("Yes"); StringBuilder sb = new StringBuilder(); for (long v : ret) { sb.append(v + " "); } System.out.println(sb.substring(0, sb.length() - 1)); } public static long sqrtL(long N) { long left = (long)(Math.sqrt(N) * 0.99); long right = (long)(Math.sqrt(N) * 1.01); while (left <= right) { long mid = (left + right) / 2; long cmp = N - mid * mid; if (cmp == 0) { return mid; } else if (cmp > 0) { left = mid + 1; } else { right = mid - 1; } } return -1; } public static void main(String[] args) { new Thread(null, new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); String debug = args.length > 0 ? args[0] : null; if (debug != null) { try { is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug)); } catch (Exception e) { throw new RuntimeException(e); } } reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768); solve(); out.flush(); tr((System.currentTimeMillis() - start) + "ms"); } }, "", 64000000).start(); } private static java.io.InputStream is = System.in; private static java.io.PrintWriter out = new java.io.PrintWriter(System.out); private static java.util.StringTokenizer tokenizer = null; private static java.io.BufferedReader reader; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private static double nd() { return Double.parseDouble(next()); } private static long nl() { return Long.parseLong(next()); } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static char[] ns() { return next().toCharArray(); } private static long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private static int[][] ntable(int n, int m) { int[][] table = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[i][j] = ni(); } } return table; } private static int[][] nlist(int n, int m) { int[][] table = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[j][i] = ni(); } } return table; } private static int ni() { return Integer.parseInt(next()); } private static void tr(Object... o) { if (is != System.in) System.out.println(java.util.Arrays.deepToString(o)); } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
fd84023950fd0eed1a72949962d5543a
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; 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); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { final int MAXN = 200000; public void solve(int testNumber, FastReader in, PrintWriter out) { List<TaskE.Pair>[] pos = new List[MAXN + 10]; for (int i = 0; i < pos.length; ++i) pos[i] = new ArrayList<>(); for (long i = 1; i <= MAXN; ++i) { if (i * 2 + 1 > MAXN) break; for (long j = i + 1; j * j - i * i <= MAXN; ++j) { pos[(int) (j * j - i * i)].add(new TaskE.Pair(i, j)); } } int n = in.nextInt(); long[] ans = new long[n + 1]; for (int i = 2; i <= n; i += 2) { int cur = in.nextInt(); for (TaskE.Pair pair : pos[cur]) { if (ans[i - 2] < pair.first) { ans[i - 1] = pair.first; ans[i] = pair.second; break; } } if (ans[i] == 0) { out.println("No"); return; } } out.println("Yes"); boolean first = true; for (int i = 1; i <= n; ++i) { if (!first) out.print(" "); first = false; out.print(ans[i] * ans[i] - ans[i - 1] * ans[i - 1]); } } static class Pair { long first; long second; public Pair(long first, long second) { this.first = first; this.second = second; } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
f390c4c3465820510beb750d46ec804f
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; 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); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { final int MAXN = 200000; public void solve(int testNumber, FastReader in, PrintWriter out) { List<TaskE.Pair>[] pos = new List[MAXN + 100]; for (int i = 0; i < pos.length; ++i) pos[i] = new ArrayList<>(); for (long i = 1; i <= MAXN; ++i) { if (i * 2 + 1 > MAXN) break; for (long j = i + 1; j * j - i * i <= MAXN; ++j) { pos[(int) (j * j - i * i)].add(new TaskE.Pair(i, j)); } } int n = in.nextInt(); long[] ans = new long[n + 1]; for (int i = 2; i <= n; i += 2) { int cur = in.nextInt(); for (TaskE.Pair pair : pos[cur]) { if (ans[i - 2] < pair.first) { ans[i - 1] = pair.first; ans[i] = pair.second; break; } } if (ans[i] == 0) { out.println("No"); return; } } out.println("Yes"); boolean first = true; for (int i = 1; i <= n; ++i) { if (!first) out.print(" "); first = false; out.print(ans[i] * ans[i] - ans[i - 1] * ans[i - 1]); } } static class Pair { long first; long second; public Pair(long first, long second) { this.first = first; this.second = second; } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
7eff1608372ea75bfdc2ca11f31a38be
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.*; import java.util.*; public class E implements Runnable { FastReader scn; PrintWriter out; String INPUT = ""; void solve() { int n = scn.nextInt(); long[] arr = new long[n]; long prev = 0; for (int i = 1; i < n; i += 2) { int x = scn.nextInt(); long a = Long.MAX_VALUE, b = -1; for (int j = 1; j * j < x; j++) { if (x % j == 0) { int p = j, q = x / j; if ((p & 1) == (q & 1)) { long A = (q - p) / 2; long B = (q + p) / 2; if (A * A > prev && A * A < a) { a = A * A; b = B * B; } } } } if (b == -1) { out.println("No"); return; } arr[i - 1] = a - prev; arr[i] = x; prev += arr[i] + arr[i - 1]; } out.println("Yes"); for (long x : arr) { out.print(x + " "); } } public void run() { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; out = new PrintWriter(System.out); scn = new FastReader(oj); solve(); out.flush(); if (!oj) { System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } } public static void main(String[] args) { new Thread(null, new E(), "Main", 1 << 26).start(); } class FastReader { InputStream is; public FastReader(boolean onlineJudge) { is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes()); } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; 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++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char[] next(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); } int nextInt() { 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(); } } long nextLong() { 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(); } } char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; 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[][] next2DInt(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } long[][] next2DLong(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; } return arr; } int[] uniq(int[] arr) { Arrays.sort(arr); int[] rv = new int[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
a68a5a7a0a970faf8e3ab157212b4fe5
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.*; import java.util.*; public class E implements Runnable { FastReader scn; PrintWriter out; String INPUT = ""; void solve() { int n = scn.nextInt(); long[] arr = new long[n]; long prev = 0; for (int i = 1; i < n; i += 2) { int x = scn.nextInt(); long a = Long.MAX_VALUE, b = -1; for (int j = 1; j * j < x; j++) { if (x % j == 0) { int p = j, q = x / j; if (p % 2 == q % 2) { long A = (q - p) / 2; long B = (q + p) / 2; if (A * A > prev && A * A < a) { a = A * A; b = B * B; } } } } if (b == -1) { out.println("No"); return; } arr[i - 1] = a - prev; arr[i] = x; prev += arr[i] + arr[i - 1]; } out.println("Yes"); for (long x : arr) { out.print(x + " "); } } public void run() { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; out = new PrintWriter(System.out); scn = new FastReader(oj); solve(); out.flush(); if (!oj) { System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } } public static void main(String[] args) { new Thread(null, new E(), "Main", 1 << 26).start(); } class FastReader { InputStream is; public FastReader(boolean onlineJudge) { is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes()); } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; 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++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char[] next(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); } int nextInt() { 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(); } } long nextLong() { 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(); } } char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; 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[][] next2DInt(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } long[][] next2DLong(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; } return arr; } int[] uniq(int[] arr) { Arrays.sort(arr); int[] rv = new int[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
062ec0fe0f1b9627ee282211abe5add1
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.*; import java.util.*; public class E implements Runnable { FastReader scn; PrintWriter out; String INPUT = ""; void solve() { int n = scn.nextInt(); long[] arr = new long[n]; long prev = 0; for(int i = 1; i < n; i += 2) { int x = scn.nextInt(); long a = Long.MAX_VALUE, b = -1; for(int j = 1; j * j < x; j++) { if(x % j == 0) { int p = j, q = x / j; if((q + p) % 2 == 0 && (q - p) % 2 == 0) { long A = (q - p) / 2; long B = (q + p) / 2; if(A * A > prev && A * A < a) { a = A * A; b = B * B; } } } } if(b == -1) { out.println("No"); return; } arr[i - 1] = a - prev; arr[i] = x; prev += arr[i] + arr[i - 1]; } out.println("Yes"); for(long x : arr) { out.print(x + " "); } } public void run() { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; out = new PrintWriter(System.out); scn = new FastReader(oj); solve(); out.flush(); if (!oj) { System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } } public static void main(String[] args) { new Thread(null, new E(), "Main", 1 << 26).start(); } class FastReader { InputStream is; public FastReader(boolean onlineJudge) { is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes()); } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; 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++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char[] next(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); } int nextInt() { 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(); } } long nextLong() { 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(); } } char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; 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[][] next2DInt(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } long[][] next2DLong(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; } return arr; } int[] uniq(int[] arr) { Arrays.sort(arr); int[] rv = new int[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
63d020db32644b70010809e4e2e556e7
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.*; import java.util.*; public class Main { static Scanner sc = new Scanner(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int n = sc.nextInt(); long[] res = new long[n]; long curIdx = 2; long curSum = 0; long LIMIT = 10000000000000L; for (int i = 1; i < n; i += 2){ res[i] = sc.nextLong(); while (true){ // if(curIdx * curIdx > LIMIT){ // out.println("No"); // out.flush(); // return; // } long trial = curIdx * curIdx; if(trial - (curSum + res[i]) > LIMIT){ out.println("No"); out.flush(); return; } if(trial >= res[i] + curSum && squaresSet((trial - (res[i])))){ res[i - 1] = trial - (curSum + res[i]); curSum = trial; curIdx++; break; } curIdx++; } } out.println("Yes"); for(int i = 0; i < n; i++){ if(i > 0)out.print(" "); out.print(res[i]); } out.println(); out.flush(); out.close(); } private static boolean squaresSet(long l) { long lo = 1, hi = (long)1e7; while (lo <= hi){ long mid = lo + hi >> 1; if(mid * mid == l)return true; if(mid * mid < l){ lo = mid + 1; }else { hi = mid - 1; } } return false; } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public Scanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
13a2f2b979a6e995b3694c3450582b95
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.*; import java.util.*; public class D{ static long INF=(long)1e18; public static void main(String[] args) throws IOException { Scanner sc=new Scanner(); PrintWriter out=new PrintWriter(System.out); int n=sc.nextInt(); long []ans=new long [n]; long sum=0; for(int i=0;i<n;i+=2) { int x=sc.nextInt(); // (b-a)(b+a) = x long min=INF; for(int d=1;d*d<=x;d++) { if(x%d!=0) continue;; int d2=x/d; if((d+d2)%2!=0) continue; int b=(d+d2)/2; int a=d2-b; long y=1l*a*a-sum; if(y<=0) continue; min=Math.min(min, y); } if(min>1e13) break; ans[i]=min; ans[i+1]=x; sum+=min+x; } if(ans[n-1]==0) out.println("No"); else { out.println("Yes"); for(long x:ans) out.println(x); } out.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner(){ br=new BufferedReader(new InputStreamReader(System.in)); } Scanner(String fileName) throws FileNotFoundException{ br=new BufferedReader(new FileReader(fileName)); } String next() throws IOException { while(st==null || !st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws IOException{ return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
65ef3c15dae50cfa8ff843bcd90a9e3f
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main { static final long MOD = 1_000_000_007, INF = 1_000_000_000_000_000_000L; static final int INf = 1_000_000_000; static FastReader reader; static PrintWriter writer; public static void main(String[] args) { Thread t = new Thread(null, new O(), "Integer.MAX_VALUE", 100000000); t.start(); } static class O implements Runnable { public void run() { try { magic(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } } static class FastReader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; 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(); } } //variables here static HashMap<Long, Long> options[]; static HashMap<Long, Boolean> dp[]; static int n; static long arr[]; static void magic() throws IOException{ reader = new FastReader(); writer = new PrintWriter(System.out, true); n = reader.nextInt(); options = new HashMap[n+1]; dp = new HashMap[n+1]; arr = new long[n+1]; boolean invalid = false; for(int i=2;i<=n;i+=2) { arr[i] = reader.nextInt(); options[i] = new HashMap<>(); add(i, (int)arr[i]); if(options[i].size()==0) { invalid = true; } } for(int i=0;i<=n;++i) { dp[i] = new HashMap<>(); } if(invalid) { writer.println("No"); } else { boolean found = false; for(long e : options[2].keySet()) { if(f(2, e)) { arr[1] = 1L * e * e; found = true; break; } } if(!found) { writer.println("No"); } else { StringBuilder fast = new StringBuilder(); for(int i=1;i<=n;++i) { fast.append(arr[i]); fast.append(" "); } writer.println("Yes"); writer.println(fast); } } } static boolean f(int index, long first_ele) { if(index==n+1) { return true; } if(dp[index].containsKey(first_ele)) { return dp[index].get(first_ele); } if(index%2==0) { if(!options[index].containsKey(first_ele)) { dp[index].put(first_ele, false); return false; } boolean ff = f(index+1, options[index].get(first_ele)); dp[index].put(first_ele, ff); return ff; } else { for(long e : options[index+1].keySet()) { if(e>first_ele) { boolean ok = f(index+1, e); if(ok) { arr[index] = 1L * (e * e) - 1L * (first_ele*first_ele); dp[index].put(first_ele, true); return true; } } } dp[index].put(first_ele, false); return false; } } static void add(int index, int n) { //ArrayList<Pair> list = new ArrayList<>(); for(int i=(int)sqrt(n);i>0;--i) { if(n%i==0) { int a = i; int b = n/i; if((a+b)%2==0 && b>a) { options[index].put((long)((b-a)/2), (long)((a+b)/2)); } } } } static class Pair implements Comparable<Pair> { int x,y; Pair(int x,int y) { this.x=x; this.y=y; } public int compareTo(Pair other) { if(this.x!=other.x) return this.x-other.x; return this.y-other.y; } public String toString() { return "("+x+","+y+")"; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
cc8ea69ca1f447724558cdb7450d0eee
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.exit; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class E { static void solve() throws Exception { int n = scanInt() / 2; // int n = 100000; long ans[] = new long[2 * n]; long cur = 0; for (int i = 0; i < n; i++) { long x = scanInt(); // long x = 200000; long next = -1; for (long d = 1; d * d < x; d++) { if (x % d != 0 || (d + x / d) % 2 != 0) { continue; } if ((x / d - d) / 2 > cur) { next = (d + x / d) / 2; } } if (next < 0) { out.print("No"); return; } ans[2 * i] = next * next - cur * cur - x; ans[2 * i + 1] = x; cur = next; } out.println("Yes"); for (int i = 0; i < 2 * n; i++) { out.print(ans[i] + " "); } } static int scanInt() throws IOException { return parseInt(scanString()); } static long scanLong() throws IOException { return parseLong(scanString()); } static String scanString() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; public static void main(String[] args) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
779967a90880b8dcbe0ecc3a15056896
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.util.*; import java.io.*; //import javafx.util.Pair; public class Main implements Runnable { static class Pair implements Comparable <Pair> { int x,y,wt; Pair(int x,int y,int wt) { this.x=x; this.y=y; this.wt=wt; } /*public boolean equals(Object obj) { if(obj==null) return false; if(obj==this) return true; if(!(obj instanceof Pair)) return false; Pair o=(Pair) obj; return o.x==this.x && o.y==this.y; } public int hashCode() { return this.x+this.y; }*/ public int compareTo(Pair p) { return Integer.compare(wt,p.wt); } } public static void main(String[] args) { new Thread(null, new Main(), "rev", 1 << 29).start(); } public void run() { InputStream inputStream=System.in; OutputStream outputStream=System.out; InputReader in=new InputReader(inputStream); PrintWriter out=new PrintWriter(outputStream); Task solver=new Task(); solver.solve(1,in,out); out.close(); } static class Task { void solve(int testNumber, InputReader in, PrintWriter out) { int n=in.nextInt(); long []a=new long[n+1]; long []dp=new long[n+1]; for(int i=2;i<=n;i+=2) a[i]=in.nextInt(); ArrayList <Integer>adj[]=new ArrayList[200001]; for(int i=1;i<=200000;i++) adj[i]=new ArrayList<>(); for(int i=1;i<=200000;i++) for(int j=i;j<=200000;j+=i) adj[j].add(i); int k; long s; for(int i=2;i<=n;i+=2) { for(int j:adj[(int)a[i]]) { k=(int)a[i]/j; if((j+k)%2==1 || k<=j) continue; s=(k-j)/2; s*=s; if(dp[i-2]<s) a[i-1]=s-dp[i-2]; } if(a[i-1]==0) { out.println("No"); return; } dp[i-1]=dp[i-2]+a[i-1]; dp[i]=dp[i-1]+a[i]; } out.println("Yes"); for(int i=1;i<=n;i++) out.print(a[i]+" "); } } static class InputReader { private InputStream stream; private byte[] buf=new byte[8192]; private int curChar,snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream=stream; } public int snext() { if(snumChars==-1) throw new InputMismatchException(); if(curChar>=snumChars) { curChar=0; try { snumChars=stream.read(buf); } catch(IOException e) { throw new InputMismatchException(); } if(snumChars<=0) return -1; } return buf[curChar++]; } public int nextInt() { int c=snext(); while(isSpaceChar(c)) c=snext(); int sgn=1; if(c=='-') { sgn=-1; c=snext(); } int res=0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res*=10; res+=c-'0'; c=snext(); }while(!isSpaceChar(c)); return res*sgn; } public long nextLong() { int c=snext(); while(isSpaceChar(c)) c=snext(); int sgn=1; if(c=='-') { sgn=-1; c=snext(); } long res=0; do{ if (c<'0' || c>'9') throw new InputMismatchException(); res*=10; res+=c-'0'; c=snext(); }while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=nextInt(); return a; } public long[] nextLongArray(int n) { long a[]=new long[n]; for(int i=1;i<n;i++) a[i]=nextLong(); return a; } public String nextString() { int c=snext(); while(isSpaceChar(c)) c=snext(); StringBuilder res=new StringBuilder(); do { res.appendCodePoint(c); c=snext(); }while(!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if(filter != null) return filter.isSpaceChar(c); return c==' ' || c=='\n' || c=='\r' || c=='\t' || c==-1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
eaa652cf2f2c1291c78bd6aa44c06170
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.PrintStream; import java.util.HashMap; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import java.util.Map; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Wolfgang Beyer */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); long[] even = in.readLongArray(n / 2); Map<Long, List<Pair>> memo = new HashMap<>(); for (long a = 1; a <= 200000; a++) { long b = a + 1; while (b * b - a * a <= 200000) { Pair p = new Pair(a, b); long diff = b * b - a * a; List<Pair> list = memo.getOrDefault(diff, new ArrayList<Pair>()); list.add(p); memo.put(diff, list); b++; } } List<Pair> cand2 = memo.get(even[0]); long prev = 0; long[] odd = new long[n / 2]; for (int i = 0; i < n / 2; i++) { List<Pair> cand = memo.get(even[i]); if (cand == null) { out.println("No"); return; } long currentA = Integer.MAX_VALUE; long currentB = Integer.MAX_VALUE; for (Pair p : cand) { if (p.a > prev && p.a < currentA) { currentA = p.a; currentB = p.b; } } if (currentA == Integer.MAX_VALUE) { out.println("No"); return; } else { odd[i] = currentA * currentA - prev * prev; // out.println(odd[i]); prev = currentB; } } out.println("Yes"); for (int i = 0; i < n / 2; i++) { out.print(odd[i] + " " + even[i] + " "); } } class Pair { long a; long b; public Pair(long a, long b) { this.a = a; this.b = b; } } } static class InputReader { private static BufferedReader in; private static StringTokenizer tok; public InputReader(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public long[] readLongArray(int n) { long[] ar = new long[n]; for (int i = 0; i < n; i++) { ar[i] = nextLong(); } return ar; } public String next() { try { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); //tok = new StringTokenizer(in.readLine(), ", \t\n\r\f"); //adds commas as delimeter } } catch (IOException ex) { System.err.println("An IOException was caught :" + ex.getMessage()); } return tok.nextToken(); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
2d855f8b18b68745988477330683380f
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
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.stream.LongStream; import java.io.IOException; import java.util.stream.Collectors; import java.io.InputStreamReader; import java.util.TreeSet; import java.util.Objects; import java.util.stream.Stream; import java.util.StringTokenizer; import java.io.BufferedReader; 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; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); EMissingNumbers solver = new EMissingNumbers(); solver.solve(1, in, out); out.close(); } static class EMissingNumbers { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); TreeSet<Long> powers = new TreeSet<>(); long MAX = (long) 1e12 + 1; for (long p = 1; p * p <= MAX; p++) { powers.add(p * p); } long[] X = new long[N + 1]; Arrays.fill(X, -1); for (int i = 2; i <= N; i += 2) { X[i] = in.nextInt(); } long[] P = new long[N + 1]; for (int i = 2; i <= N; i += 2) { while (!powers.isEmpty()) { long p1 = powers.pollFirst(); long p2 = p1 + X[i]; if (powers.contains(p2)) { while (!powers.isEmpty() && powers.first() <= p2) { powers.pollFirst(); } P[i] = p2; P[i - 1] = p1; X[i - 1] = p1 - P[i - 2]; break; } } } for (int i = 1; i <= N; i++) { if (X[i] == -1) { out.println("No"); return; } } out.println("Yes"); out.println(Arrays.stream(X).skip(1).mapToObj(Objects::toString).collect(Collectors.joining(" "))); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
8f36f54292b6ddfbe04ba1a7d0906f7a
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; public class MissingNumbers { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int n = Integer.parseInt(br.readLine()); long[] inputArray = Arrays.stream(br.readLine().split(" ")) .mapToLong(x -> Long.parseLong(x)).toArray(); // answer array long[] a = new long[n]; for (int i = 0; i < n / 2; i++) { a[2 * i + 1] = inputArray[i]; } // value such that a[0] + ... + a[i] = k^2 long k = 0; // value such that a[0] + ... + a[i+1] = l^2 long l = 0; //a[0] + ... + a[i - 1] long s = 0; for (int i = 0; i < n; i += 2) { boolean found = false; while (!found) { k++; //we are given that a[i] < 10^13 if (k * k - s > 10_000_000_000_000L) { System.out.println("No"); return; } // suppose a[i] is such that a[0] + ... + a[i] = k^2 // check if a[0] + ... + a[i+1] = l^2 long sumToiPlus1 = k * k + a[i + 1]; while (sumToiPlus1 > l * l) { l++; } if (sumToiPlus1 == l * l) { found = true; a[i] = k * k - s; s = sumToiPlus1; // the next k will be greater than the current l k = l; } } } pw.println("Yes"); for (int i = 0; i < a.length - 1; i++) { pw.print(a[i] + " "); } pw.println(a[a.length - 1]); pw.flush(); br.close(); pw.close(); } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
2610cf9fb15d68e2d3fe9eb6be79b732
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; public class MissingNumbers { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int size = Integer.parseInt(br.readLine()); long[] myarray = Arrays.stream(br.readLine().split(" ")) .mapToLong(x -> Long.parseLong(x)).toArray(); // answer array long[] arr1 = new long[size]; for (int i = 0; i < size / 2; i++) { arr1[2 * i + 1] = myarray[i]; } // value such that arr1[0] + ... + arr1[i] = k^2 long k = 0; // value such that arr1[0] + ... + arr1[i+1] = l^2 long l = 0; //arr1[0] + ... + arr1[i - 1] long s = 0; for (int i = 0; i < size; i += 2) { boolean found = false; while (!found) { k++; //we are given that arr1[i] < 10^13 if (k * k - s > 10_000_000_000_000L) { System.out.println("No"); return; } // suppose arr1[i] is such that arr1[0] + ... + arr1[i] = k^2 // check if arr1[0] + ... + arr1[i+1] = l^2 long sumToiPlus1 = k * k + arr1[i + 1]; while (sumToiPlus1 > l * l) { l++; } if (sumToiPlus1 == l * l) { found = true; arr1[i] = k * k - s; s = sumToiPlus1; // the next k will be greater than the current l k = l; } } } pw.println("Yes"); for (int i = 0; i < arr1.length - 1; i++) { pw.print(arr1[i] + " "); } pw.println(arr1[arr1.length - 1]); pw.flush(); br.close(); pw.close(); } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
8a653505b0ddf0c0fb0852ae4e6390a8
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
//package ; import java.io.*; import java.util.*; public class E { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(); PrintWriter pw = new PrintWriter(System.out); int n=sc.nextInt(); long a[]=new long[n]; for(int i=1;i<n;i+=2) a[i]=sc.nextLong(); long s=0;//cumlative sum for(int i=0;i<n;i+=2) { //s + a[i] =a^2 //s + a[i] + a[i+1] =b^2 //a[i+1]= b^2 - a^2 long b=-1,c=-1; long y=Long.MAX_VALUE; for(int j=1;j*j<=a[i+1];j++) { if(a[i+1]%j==0) { if((j+a[i+1]/j)%2==0) { b=(j+a[i+1]/j)/2; c=j-b; long tmp=c*c-s; if(tmp>0) y=Math.min(y, tmp); } } } if(b==-1) { System.out.println("No"); return; } if(y==Long.MAX_VALUE) { System.out.println("No"); return; } a[i]=y; s+=a[i]+a[i+1]; } pw.println("Yes"); for(long x:a) pw.print(x+" "); pw.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } boolean hasnext() throws IOException { return br.ready(); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
658520311f930d1d7c4de65d07713cbf
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
//package ; import java.io.*; import java.util.*; public class E { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(); PrintWriter pw = new PrintWriter(System.out); int n=sc.nextInt(); long a[]=new long[n]; for(int i=1;i<n;i+=2) a[i]=sc.nextLong(); long s=0;//cumlative sum for(int i=0;i<n;i+=2) { //s + a[i] =a^2 //s + a[i] + a[i+1] =b^2 //a[i+1]= b^2 - a^2 long b=-1,c=-1; long y=Long.MAX_VALUE; for(int j=1;j*j<=a[i+1];j++) { if(a[i+1]%j==0) { if((j+a[i+1]/j)%2==0) { // System.out.println(j); b=(j+a[i+1]/j)/2; c=j-b; long tmp=c*c-s; if(tmp>0) y=Math.min(y, tmp); // break; } } } if(b==-1) { System.out.println("No"); return; } if(y<1 || y==Long.MAX_VALUE) { System.out.println("No"); return; } a[i]=y; s+=a[i]+a[i+1]; } pw.println("Yes"); for(long x:a) pw.print(x+" "); pw.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } boolean hasnext() throws IOException { return br.ready(); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
60351b1e7eb9db7816d2d8ef6de252f8
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.FilterInputStream; import java.io.BufferedInputStream; 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; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); EMissingNumbers solver = new EMissingNumbers(); solver.solve(1, in, out); out.close(); } static class EMissingNumbers { public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); long ans[] = new long[n]; Arrays.fill(ans, -1); for (int i = 1; i < n; i += 2) { ans[i] = in.scanInt(); } for (long j = 1; j <= 4000000; j++) { if (Math.sqrt(j * j + ans[1]) == (long) Math.sqrt(j * j + ans[1])) { ans[0] = j * j; break; } } if (ans[0] == -1) { out.println("No"); return; } long sum = 0; boolean f = true; for (int i = 0; i < n; i++) { if (ans[i] != -1) { sum += ans[i]; } else { for (long j = (long) Math.sqrt(sum); j <= 4000000; j++) { if (Math.sqrt(j * j + ans[i + 1]) == (long) Math.sqrt(j * j + ans[i + 1]) && j * j != sum) { ans[i] = j * j - sum; sum += ans[i]; if (ans[i] == 0) { out.println("No"); return; } break; } } if (ans[i] == -1) { f = false; break; } } } if (f) { out.println("Yes"); for (long a : ans) { out.print(a + " "); } } else { out.println("No"); } } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int INDEX; private BufferedInputStream in; private int TOTAL; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (INDEX >= TOTAL) { INDEX = 0; try { TOTAL = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (TOTAL <= 0) return -1; } return buf[INDEX++]; } public int scanInt() { int I = 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') { I *= 10; I += n - '0'; n = scan(); } } return neg * I; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
142d7ab6d1ea6d382465082b0f8fe7de
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
//package codeforces.contests.avito2018; import java.io.*; import java.util.InputMismatchException; /** * @author tainic on Dec 16, 2018 */ public class E { private static boolean LOCAL; static { try { LOCAL = "aurel".equalsIgnoreCase(System.getenv().get("USER")); } catch (Exception e){} } private static final String TEST = "6\n" + "5 11 44"; void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); long[] x = new long[n + 1]; long z = 0; boolean pos = true; for (int i = 2; i <= n; i += 2) { x[i] = in.nextLong(); if (!pos) continue; if (x[i] % 2 == 0 && x[i] % 4 != 0) { pos = false; continue; } long y = 0; int rs = (int) Math.sqrt(x[i]); if (x[i] % 2 != rs % 2) { rs--; } for (int r = rs; r >= 2 - (int)(x[i] % 2); r -= 2) { if ((x[i] - r * r) % (2 * r) != 0) continue; y = (x[i] - r * r) / (2 * r); if (y * y <= z) continue; else break; } if (y * y <= z) { pos = false; continue; } x[i - 1] = y * y - z; z += x[i - 1] + x[i]; } if (pos) { out.println("Yes"); String sep = ""; for (int i = 1; i <= n; i++) { out.print(sep); out.print(x[i]); sep = " "; } out.println(); } else { out.println("No"); } } //region main public static void main(String[] args) throws Exception { long t = System.currentTimeMillis(); try ( InputReader in = new StreamInputReader(!LOCAL ? System.in : new ByteArrayInputStream(TEST.getBytes())); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out, 2048), false) ) { new E().solve(in, out); } System.err.println("time: " + (System.currentTimeMillis() - t) + "ms"); } //endregion //region fast io abstract static class InputReader implements AutoCloseable { public abstract int read(); 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 String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } static class StreamInputReader extends InputReader { private InputStream stream; private byte[] buf; private int curChar, numChars; public StreamInputReader(InputStream stream) { this(stream, 2048); } public StreamInputReader(InputStream stream, int bufSize) { this.stream = stream; this.buf = new byte[bufSize]; } @Override 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++]; } @Override public void close() throws Exception { stream.close(); } } //endregion //region imports //endregion }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
896ca264c4b5cf2a7b42ff650e4bace2
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
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.Set; import java.util.InputMismatchException; import java.io.IOException; import java.util.TreeSet; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author prakharjain */ 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); EMissingNumbers solver = new EMissingNumbers(); solver.solve(1, in, out); out.close(); } static class EMissingNumbers { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); long[] x = new long[n]; for (int i = 1; i < n; i += 2) { x[i] = in.nextInt(); } // long[] numa = new long[10000]; // // numa[0] = 0; // numa[1] = 3; // // long csum = 0; // for (int i = 2; i < 10000; i++) // { // numa[i] = numa[i] + 2; // // csum // } long sum = 0; for (int i = 0; i < n; i += 2) { long diff = x[i + 1]; // if (diff % 2 == 1) { // long lo = diff / 2; // x[i] = lo * lo - sum; // // if (x[i] <= 0) { // out.println("No"); // return; // } // sum += x[i]; // sum += x[i + 1]; // } else { // diff /= 2; // diff--; // long lo = diff / 2; // x[i] = lo * lo - sum; // // if (x[i] <= 0) { // out.println("No"); // return; // } // sum += x[i]; // sum += x[i + 1]; // } if (diff % 2 == 1) { //long mreq = sum + 1; //long sqrt = squareRoot(mreq); //ts Set<Long> facs = factors(x[i + 1]); long lo = -1; for (Long num : facs) { long parts = x[i + 1] / num; if (parts % 2 == 1) { long hp = parts / 2; long fnum = num - 2 * hp; long slo = fnum / 2; if (slo > 0 && slo * slo - sum > 0) { lo = slo; break; } } } if (lo == -1) { out.println("No"); return; } x[i] = lo * lo - sum; sum += x[i]; sum += x[i + 1]; } else { long mreq = sum + 1; long sqrt = squareRoot(mreq); //ts Set<Long> facs = factors(x[i + 1]); long lo = -1; for (Long num : facs) { //if (num >= sqrt) { long parts = x[i + 1] / num; if (parts % 2 == 0) { long hp = parts / 2; long fnum = num - 2 * hp + 1; long slo = fnum / 2; if (fnum % 2 == 1 && slo > 0 && slo * slo - sum > 0) { lo = slo; break; } } //} } if (lo == -1) { out.println("No"); return; } x[i] = lo * lo - sum; sum += x[i]; sum += x[i + 1]; } } out.println("Yes"); for (int i = 0; i < n; i++) { out.print(x[i] + " "); } } TreeSet<Long> factors(long n) { TreeSet<Long> factors = new TreeSet<>(); for (long i = 1; i * i <= n; i++) { if (n % i == 0) { factors.add(i); factors.add(n / i); } } return factors; } long squareRoot(long num) { long squareRoot = (long) Math.sqrt(num); while (squareRoot * squareRoot <= num) { if (squareRoot * squareRoot == num) { return squareRoot; } else { squareRoot++; } } return squareRoot; } } 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 static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } 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 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(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
a8bddedaa6e9f22c052b0c0420bb4786
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.util.*; import java.io.*; import java.text.*; public class Main { //SOLUTION BEGIN //Into the Hardware Mode void pre() throws Exception{} void solve(int TC) throws Exception { int N = ni(); long[] X = new long[N]; for(int i = 1; i< N; i+= 2)X[i] = nl(); long S = 0;int ptr = 0; for(long SQRT = 1; SQRT*SQRT <= 1e13 && ptr < N; SQRT++){ long t = SQRT*SQRT-S; if(t <= 0)continue; if(f(S+t) && f(S+t+X[ptr+1])) { X[ptr] = t; S += X[ptr]+X[ptr+1]; ptr += 2; } } if(ptr < N){ pn("No"); return; } pn("Yes"); for(long x:X)p(x+" ");pn(""); } boolean f(long x){ if(x <= 0)return false; long y = (long)Math.sqrt(x); while(y*y > x)y--; while(y*y < x)y++; return y*y == x; } //SOLUTION END void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");} void exit(boolean b){if(!b)System.exit(0);} static void debug(Object... o){System.out.println(Arrays.deepToString(o));} final long IINF = (long)2e18; final int INF = (int)1e9+2; DecimalFormat df = new DecimalFormat("0.00000000000"); double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-8; static boolean multipleTC = false, memory = true, fileIO = false; FastReader in;PrintWriter out; void run() throws Exception{ long ct = System.currentTimeMillis(); if (fileIO) { in = new FastReader(""); out = new PrintWriter(""); } else { in = new FastReader(); out = new PrintWriter(System.out); } //Solution Credits: Taranpreet Singh int T = multipleTC? ni():1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); System.err.println(System.currentTimeMillis() - ct); } public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start(); else new Main().run(); } int[][] make(int n, int[] from, int[] to, int e, boolean f){ int[][] g = new int[n][];int[]cnt = new int[n]; for(int i = 0; i< e; i++){ cnt[from[i]]++; if(f)cnt[to[i]]++; } for(int i = 0; i< n; i++)g[i] = new int[cnt[i]]; for(int i = 0; i< e; i++){ g[from[i]][--cnt[from[i]]] = to[i]; if(f)g[to[i]][--cnt[to[i]]] = from[i]; } return g; } int[][][] makeS(int n, int[] from, int[] to, int e, boolean f){ int[][][] g = new int[n][][];int[]cnt = new int[n]; for(int i = 0; i< e; i++){ cnt[from[i]]++; if(f)cnt[to[i]]++; } for(int i = 0; i< n; i++)g[i] = new int[cnt[i]][]; for(int i = 0; i< e; i++){ g[from[i]][--cnt[from[i]]] = new int[]{to[i], i}; if(f)g[to[i]][--cnt[to[i]]] = new int[]{from[i], i}; } return g; } int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));} int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;} long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));} void p(Object o){out.print(o);} void pn(Object o){out.println(o);} void pni(Object o){out.println(o);out.flush();} String n()throws Exception{return in.next();} String nln()throws Exception{return in.nextLine();} int ni()throws Exception{return Integer.parseInt(in.next());} long nl()throws Exception{return Long.parseLong(in.next());} double nd()throws Exception{return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next() throws Exception{ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception{ String str; try{ str = br.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } return str; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
676b70e99a8eceaab2ff8f5fb68b1310
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.FilterInputStream; import java.io.BufferedInputStream; 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; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); EMissingNumbers solver = new EMissingNumbers(); solver.solve(1, in, out); out.close(); } static class EMissingNumbers { public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); long ans[] = new long[n]; Arrays.fill(ans, -1); for (int i = 1; i < n; i += 2) { ans[i] = in.scanInt(); } for (long j = 1; j <= 4000000; j++) { if (Math.sqrt(j * j + ans[1]) == (long) Math.sqrt(j * j + ans[1])) { ans[0] = j * j; break; } } if (ans[0] == -1) { out.println("No"); return; } long sum = 0; boolean f = true; for (int i = 0; i < n; i++) { if (ans[i] != -1) { sum += ans[i]; } else { for (long j = (long) Math.sqrt(sum); j <= 4000000; j++) { if (Math.sqrt(j * j + ans[i + 1]) == (long) Math.sqrt(j * j + ans[i + 1])) { ans[i] = j * j - sum; sum += ans[i]; if (ans[i] == 0) { out.println("No"); return; } break; } } if (ans[i] == -1) { f = false; break; } } } if (f) { out.println("Yes"); for (long a : ans) { out.print(a + " "); } } else { out.println("No"); } } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int INDEX; private BufferedInputStream in; private int TOTAL; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (INDEX >= TOTAL) { INDEX = 0; try { TOTAL = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (TOTAL <= 0) return -1; } return buf[INDEX++]; } public int scanInt() { int I = 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') { I *= 10; I += n - '0'; n = scan(); } } return neg * I; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
389e5afdb3c9b9e265a09eb0ab482849
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
//package avito2018b; 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 E { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(); int[] lpf = enumLowestPrimeFactors(200005); int[][] dss = new int[200004][]; for(int i = 1;i <= 200003;i++){ int[] ds = enumDivisorsFast(i, lpf); Arrays.sort(ds); int p = 0; for(int d : ds){ if(d < i/d && (d+i/d)%2 == 0){ ds[p++] = d; } } dss[i] = Arrays.copyOf(ds, p); } int[] a = na(n/2); // x=(t-s)(t+s) long[] ret = new long[n]; for(int i = 0;i < n/2;i++)ret[2*i+1] = a[i]; long cur = 0; outer: for(int i = 0;i < n/2;i++){ int v = a[i]; long s = 0, t = 0; for(int j = dss[v].length-1;j >= 0;j--){ t = (v/dss[v][j]+dss[v][j])/2; s = (v/dss[v][j]-dss[v][j])/2; if(cur < s*s){ ret[i*2] = s*s-cur; if(ret[i*2] > 10000000000000L){ out.println("No"); return; } cur = t*t; continue outer; } } out.println("No"); return; } out.println("Yes"); for(long v : ret){ out.print(v + " "); } out.println(); } public static int[] enumDivisorsFast(int n, int[] lpf) { int[] divs = {1}; while(lpf[n] > 0){ int p = lpf[n]; int e = 0; for(;p == lpf[n];e++, n /= p); int olen = divs.length; divs = Arrays.copyOf(divs, olen*(e+1)); for(int i = olen;i < divs.length;i++)divs[i] = divs[i-olen] * p; } return divs; } public static int[] enumLowestPrimeFactors(int n) { int tot = 0; int[] lpf = new int[n + 1]; int u = n + 32; double lu = Math.log(u); int[] primes = new int[(int) (u / lu + u / lu / lu * 1.5)]; for (int i = 2; i <= n; i++) lpf[i] = i; for (int p = 2; p <= n; p++) { if (lpf[p] == p) primes[tot++] = p; int tmp; for (int i = 0; i < tot && primes[i] <= lpf[p] && (tmp = primes[i] * p) <= n; i++) { lpf[tmp] = primes[i]; } } return lpf; } 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 E().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private 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\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
e9e55e7e1acedbf0287971a117c1aeb8
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import sun.reflect.generics.tree.Tree; import java.io.*; import java.util.*; public class Task { public static void main(String[] args) throws IOException { new Task().go(); } PrintWriter out; Reader in; BufferedReader br; Task() throws IOException { try { //br = new BufferedReader( new FileReader("input.txt") ); in = new Reader("input.txt"); out = new PrintWriter( new BufferedWriter(new FileWriter("output.txt")) ); } catch (Exception e) { //br = new BufferedReader( new InputStreamReader( System.in ) ); in = new Reader(); out = new PrintWriter( new BufferedWriter(new OutputStreamWriter(System.out)) ); } } void go() throws IOException { int t = 1; while (t > 0) { solve(); //out.println(); t--; } out.flush(); out.close(); } int inf = 2000000000; int mod = 1000000007; double eps = 0.000000001; int n; int m; ArrayList<Pair>[] g; void solve() throws IOException { n = in.nextInt(); int cur = 1; long sum = 1; long s = 1; ArrayList<Long> ans = new ArrayList<>(); for (int i = 0; i < n / 2; i++) { int x = in.nextInt(); while (s == 0 || cur <= x && !check(sum + x)) { cur += 2; sum += cur; s += cur; } if (cur > x) { out.print("No"); return; } long need = sum + x; while (sum < need) { cur += 2; sum += cur; } ans.add(s); ans.add((long)x); s = 0; } out.println("Yes"); for (long k : ans) out.print(k + " "); } boolean check(long x) { long sqr = (long) Math.sqrt(x); return x == sqr * sqr; } class Pair implements Comparable<Pair> { int a; int b; Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair p) { if (a != p.a) return Integer.compare(a, p.a); else return Integer.compare(b, p.b); } } class Item { int a; int b; int c; Item(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } } class Reader { BufferedReader br; StringTokenizer tok; Reader(String file) throws IOException { br = new BufferedReader( new FileReader(file) ); } Reader() throws IOException { br = new BufferedReader( new InputStreamReader(System.in) ); } String next() throws IOException { while (tok == null || !tok.hasMoreElements()) tok = new StringTokenizer(br.readLine()); return tok.nextToken(); } int nextInt() throws NumberFormatException, IOException { return Integer.valueOf(next()); } long nextLong() throws NumberFormatException, IOException { return Long.valueOf(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.valueOf(next()); } String nextLine() throws IOException { return br.readLine(); } int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } ArrayList<Integer>[] nextGraph(int n, int m) throws IOException { ArrayList<Integer>[] g = new ArrayList[n]; for (int i = 0; i < n; i++) g[i] = new ArrayList<>(); for (int i = 0; i < m; i++) { int x = nextInt() - 1; int y = nextInt() - 1; g[x].add(y); g[y].add(x); } return g; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
4fa91068a4dc60bff85470783ebf549a
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
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.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ISTU: Ne nado dumat' */ 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); TaskWE solver = new TaskWE(); solver.solve(1, in, out); out.close(); } static class TaskWE { int n; int[] a; int maxDepth; List<Long> answer; public void solve(int testNumber, InputReader in, OutputWriter out) { // if (true) { // long sum = 0; // for (int i = 1; i <= 100; i++) { // long add = i * (long) i - sum; // out.print(add + " "); // sum += add; // } // } n = in.readInt(); a = in.readIntArray(n / 2); boolean ok = dfs(0, 1, 0); if (!ok) { out.print("No"); return; } out.printLine("Yes"); Collections.reverse(answer); out.print(answer.toArray()); } boolean isSquare(long x) { long l = 1; long r = (long) Math.sqrt(x) + 100; while (r - l > 1) { long m = l + r >> 1; if (m * m <= x) { l = m; } else { r = m; } } return l * l == x; } boolean dfs(int at, int minSq, long sum) { maxDepth = Math.max(maxDepth, at); if (at == a.length) { answer = new ArrayList<>(); return true; } for (int i = minSq; ; i++) { long sq = i * (long) i; if (sq > 1e13) { break; } long left = sq - sum - a[at]; if (left <= 0 || !isSquare(sum + left)) { continue; } if (dfs(at + 1, i, sum + left + a[at])) { answer.add((long) a[at]); answer.add(left); return true; } if (maxDepth > at + 1) { return false; } } return false; } } 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 print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
1982a4fe700b034f5507a21be79440be
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); EMissingNumbers solver = new EMissingNumbers(); solver.solve(1, in, out); out.close(); } static class EMissingNumbers { public boolean checksq(long num) { long tt = (long) Math.sqrt(num); for (long i = tt - 10; i <= tt + 10; i++) if (i * i == num) return true; return false; } public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); long[] b = new long[n]; long[] a = new long[n]; for (int i = 1; i < n; i += 2) b[i] = in.scanInt(); long sum = 0; long num = 1; for (int i = 0; i < n; i++) { if ((i & 1) == 1) { a[i] = b[i]; while (b[i] > 0) { sum += num; b[i] -= num; num += 2; } } else { a[i] = num; num += 2; while (!checksq(sum + b[i + 1] + a[i]) && num <= b[i + 1]) { a[i] += num; num += 2; } if (!checksq(sum + b[i + 1] + a[i])) { out.println("No"); return; } sum += a[i]; } } out.println("Yes"); for (long l : a) out.print(l + " "); } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int index; private BufferedInputStream in; private int total; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (index >= total) { index = 0; try { total = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (total <= 0) return -1; } return buf[index++]; } public int scanInt() { 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(); } } return neg * integer; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
10fa60b583ca0262c4c345eb057dc961
train_003.jsonl
1544970900
Chouti is working on a strange math problem.There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
256 megabytes
/* Keep solving problems. */ import java.util.*; import java.io.*; public class CFA { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; private static long MOD = 1000L * 1000L * 1000L + 7; private static final int[] dx = {0, -1, 0, 1}; private static final int[] dy = {1, 0, -1, 0}; private static final String yes = "Yes"; private static final String no = "No"; int n; int LIMIT = 500000 + 100; // int LIMIT = 200; List<List<Integer>> factors = new ArrayList<>(); long[] x; class Pair implements Comparable<Pair> { long x; long y; public Pair(long x, long y) { this.x = x; this.y = y; } @Override public String toString() { return x + " " + y; } @Override public int compareTo(Pair o) { return Long.compare(x, o.x); } } void solve() throws IOException { n = nextInt(); for (int i = 0; i < LIMIT; i++) { factors.add(new ArrayList<>()); } for (int i = 1; i < LIMIT; i++) { for (int j = i; j < LIMIT; j += i) { if (i * i < j) { factors.get(j).add(i); } } } x = nextLongArr(n / 2); List<List<Pair>> ls = new ArrayList<>(); for (int i = 0; i < n / 2; i++) { ls.add(new ArrayList<>()); } for (int i = 0; i < n / 2; i++) { int val = (int) x[i]; List<Integer> facts = factors.get(val); for (int v : facts) { long f1 = v; long f2 = val / v; if ((f1 + f2) % 2 != 0) { continue; } long a1 = (f2 - f1) / 2; long a2 = (f2 + f1) / 2; ls.get(i).add(new Pair(a1 * a1, a2 * a2)); } } for (List<Pair> l : ls) { Collections.sort(l); } List<Pair> select = new ArrayList<>(); if (ls.get(0).isEmpty()) { outln(no); return; } long prev = -1; for (int i = 0; i < n / 2; i++) { int idx = -1; for (int j = 0; j < ls.get(i).size(); j++) { Pair cur = ls.get(i).get(j); if (cur.x > prev) { idx = j; prev = cur.y; break; } } if (idx == -1) { outln(no); return; } select.add(ls.get(i).get(idx)); } List<Long> res = new ArrayList<>(); prev = 0; for (Pair p : select) { res.add(p.x - prev); prev = p.x; res.add(p.y - prev); prev = p.y; } for (long v : res) { if (v < 1 || v > 1000L * 1000 * 1000L * 1000L * 10) { outln(no); return; } } outln(yes); for (long v : res) { out(v + " "); } } void shuffle(int[] a) { int n = a.length; for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } long gcd(long a, long b) { while(a != 0 && b != 0) { long c = b; b = a % b; a = c; } return a + b; } private void outln(Object o) { out.println(o); } private void out(Object o) { out.print(o); } private void formatPrint(double val) { outln(String.format("%.9f%n", val)); } public CFA() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new CFA(); } public long[] nextLongArr(int n) throws IOException{ long[] res = new long[n]; for(int i = 0; i < n; i++) res[i] = nextLong(); return res; } public int[] nextIntArr(int n) throws IOException { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } public String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
Java
["6\n5 11 44", "2\n9900", "6\n314 1592 6535"]
2 seconds
["Yes\n4 5 16 11 64 44", "Yes\n100 9900", "No"]
NoteIn the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares.In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.In the third example, it is possible to show, that no such sequence exists.
Java 8
standard input
[ "greedy", "constructive algorithms", "number theory", "math", "binary search" ]
d07730b7bbbfa5339ea24162df7a5cab
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
1,900
If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
standard output
PASSED
1780724c8672b00ec88a6dab0de54367
train_003.jsonl
1597415700
You are given an array $$$a_1, a_2, \dots , a_n$$$, which is sorted in non-decreasing order ($$$a_i \le a_{i + 1})$$$. Find three indices $$$i$$$, $$$j$$$, $$$k$$$ such that $$$1 \le i &lt; j &lt; k \le n$$$ and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$ (for example it is possible to construct a non-degenerate triangle with sides $$$3$$$, $$$4$$$ and $$$5$$$ but impossible with sides $$$3$$$, $$$4$$$ and $$$7$$$). If it is impossible to find such triple, report it.
256 megabytes
import java.util.*; import java.io.*; public class A{ public static boolean bs(int n,int arr[],int l,int r) { while(l<=r) { int mid=l+(r-l>>1); if(arr[mid]>n)r=mid-1; else if(arr[mid]<n)l=mid+1; else return true; } return false; } public static void main(String[] args) throws IOException { PrintWriter out=new PrintWriter(System.out); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(br.readLine()); int t=Integer.parseInt(st.nextToken()); while(t-->0) { st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()); st=new StringTokenizer(br.readLine()); int arr[]=new int[n]; for(int i=0;i<n;i++)arr[i]=Integer.parseInt(st.nextToken()); if(arr[0]+arr[1]<=arr[n-1])out.println(1+" "+2+" "+n); else out.println(-1); } out.close(); } static class Pair{ int x; int y; public Pair(int x,int y) { this.x=x; this.y=y; } public String toString() { return this.x+" "+this.y; } } }
Java
["3\n7\n4 6 11 11 15 18 20\n4\n10 10 10 11\n3\n1 1 1000000000"]
1 second
["2 3 6\n-1\n1 2 3"]
NoteIn the first test case it is impossible with sides $$$6$$$, $$$11$$$ and $$$18$$$. Note, that this is not the only correct answer.In the second test case you always can construct a non-degenerate triangle.
Java 8
standard input
[ "geometry", "math" ]
341555349b0c1387334a0541730159ac
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \le n \le 5 \cdot 10^4$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$; $$$a_{i - 1} \le a_i$$$) — the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case print the answer to it in one line. If there is a triple of indices $$$i$$$, $$$j$$$, $$$k$$$ ($$$i &lt; j &lt; k$$$) such that it is impossible to construct a non-degenerate triangle having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$, print that three indices in ascending order. If there are multiple answers, print any of them. Otherwise, print -1.
standard output
PASSED
a00a7628e64a2a20ed0d102e4833fe18
train_003.jsonl
1597415700
You are given an array $$$a_1, a_2, \dots , a_n$$$, which is sorted in non-decreasing order ($$$a_i \le a_{i + 1})$$$. Find three indices $$$i$$$, $$$j$$$, $$$k$$$ such that $$$1 \le i &lt; j &lt; k \le n$$$ and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$ (for example it is possible to construct a non-degenerate triangle with sides $$$3$$$, $$$4$$$ and $$$5$$$ but impossible with sides $$$3$$$, $$$4$$$ and $$$7$$$). If it is impossible to find such triple, report it.
256 megabytes
import java.lang.*; import java.util.*; public class Solution{ public static void main(String args[]) { int t, n, arr[]; Scanner sc = new Scanner(System.in); t = sc.nextInt(); while(t != 0) { n = sc.nextInt(); arr = new int[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextInt(); if(arr[0] + arr[1] > arr[n-1]) System.out.println(-1); else System.out.println(1 + " " + 2 + " " + n); t--; } } }
Java
["3\n7\n4 6 11 11 15 18 20\n4\n10 10 10 11\n3\n1 1 1000000000"]
1 second
["2 3 6\n-1\n1 2 3"]
NoteIn the first test case it is impossible with sides $$$6$$$, $$$11$$$ and $$$18$$$. Note, that this is not the only correct answer.In the second test case you always can construct a non-degenerate triangle.
Java 8
standard input
[ "geometry", "math" ]
341555349b0c1387334a0541730159ac
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \le n \le 5 \cdot 10^4$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$; $$$a_{i - 1} \le a_i$$$) — the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case print the answer to it in one line. If there is a triple of indices $$$i$$$, $$$j$$$, $$$k$$$ ($$$i &lt; j &lt; k$$$) such that it is impossible to construct a non-degenerate triangle having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$, print that three indices in ascending order. If there are multiple answers, print any of them. Otherwise, print -1.
standard output
PASSED
107487f136f09ae0364cf2b262b7d0a1
train_003.jsonl
1597415700
You are given an array $$$a_1, a_2, \dots , a_n$$$, which is sorted in non-decreasing order ($$$a_i \le a_{i + 1})$$$. Find three indices $$$i$$$, $$$j$$$, $$$k$$$ such that $$$1 \le i &lt; j &lt; k \le n$$$ and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$ (for example it is possible to construct a non-degenerate triangle with sides $$$3$$$, $$$4$$$ and $$$5$$$ but impossible with sides $$$3$$$, $$$4$$$ and $$$7$$$). If it is impossible to find such triple, report it.
256 megabytes
/*input 3 7 4 6 11 11 15 18 20 4 10 10 10 11 3 1 1 1000000000 */ import java.util.*; import java.math.BigInteger; public class Easy{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int i,t = sc.nextInt(),n; long arr[]; boolean ans = false; while(t>0) { t--; n=sc.nextInt(); arr = new long[n]; ans = false; for(i=0;i<n;i++){ arr[i] = sc.nextLong(); } for(i=0;i<n-2;i++) { if((arr[i]+arr[i+1])<=arr[n-1]){ System.out.println((++i)+" "+(i+1)+" "+n); ans = true; break; } } if(!ans) System.out.println("-1"); } sc.close(); } }
Java
["3\n7\n4 6 11 11 15 18 20\n4\n10 10 10 11\n3\n1 1 1000000000"]
1 second
["2 3 6\n-1\n1 2 3"]
NoteIn the first test case it is impossible with sides $$$6$$$, $$$11$$$ and $$$18$$$. Note, that this is not the only correct answer.In the second test case you always can construct a non-degenerate triangle.
Java 8
standard input
[ "geometry", "math" ]
341555349b0c1387334a0541730159ac
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \le n \le 5 \cdot 10^4$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$; $$$a_{i - 1} \le a_i$$$) — the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case print the answer to it in one line. If there is a triple of indices $$$i$$$, $$$j$$$, $$$k$$$ ($$$i &lt; j &lt; k$$$) such that it is impossible to construct a non-degenerate triangle having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$, print that three indices in ascending order. If there are multiple answers, print any of them. Otherwise, print -1.
standard output
PASSED
160585b7e5916feedba25882fe9f9dc2
train_003.jsonl
1597415700
You are given an array $$$a_1, a_2, \dots , a_n$$$, which is sorted in non-decreasing order ($$$a_i \le a_{i + 1})$$$. Find three indices $$$i$$$, $$$j$$$, $$$k$$$ such that $$$1 \le i &lt; j &lt; k \le n$$$ and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$ (for example it is possible to construct a non-degenerate triangle with sides $$$3$$$, $$$4$$$ and $$$5$$$ but impossible with sides $$$3$$$, $$$4$$$ and $$$7$$$). If it is impossible to find such triple, report it.
256 megabytes
/*input 3 7 4 6 11 11 15 18 20 4 10 10 10 11 3 1 1 1000000000 */ import java.util.*; import java.math.BigInteger; public class Easy{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int i,arr[],t = sc.nextInt(),n; boolean ans = false; while(t>0) { t--; n=sc.nextInt(); arr = new int[n]; ans = false; for(i=0;i<n;i++){ arr[i] = sc.nextInt(); } for(i=0;i<n-2;i++) { if((arr[i]+arr[i+1])<=arr[n-1]){ System.out.println((++i)+" "+(i+1)+" "+n); ans = true; break; } } if(!ans) System.out.println("-1"); } sc.close(); } }
Java
["3\n7\n4 6 11 11 15 18 20\n4\n10 10 10 11\n3\n1 1 1000000000"]
1 second
["2 3 6\n-1\n1 2 3"]
NoteIn the first test case it is impossible with sides $$$6$$$, $$$11$$$ and $$$18$$$. Note, that this is not the only correct answer.In the second test case you always can construct a non-degenerate triangle.
Java 8
standard input
[ "geometry", "math" ]
341555349b0c1387334a0541730159ac
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \le n \le 5 \cdot 10^4$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$; $$$a_{i - 1} \le a_i$$$) — the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case print the answer to it in one line. If there is a triple of indices $$$i$$$, $$$j$$$, $$$k$$$ ($$$i &lt; j &lt; k$$$) such that it is impossible to construct a non-degenerate triangle having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$, print that three indices in ascending order. If there are multiple answers, print any of them. Otherwise, print -1.
standard output
PASSED
4702b2f4826002b2f46508d36bee96ae
train_003.jsonl
1597415700
You are given an array $$$a_1, a_2, \dots , a_n$$$, which is sorted in non-decreasing order ($$$a_i \le a_{i + 1})$$$. Find three indices $$$i$$$, $$$j$$$, $$$k$$$ such that $$$1 \le i &lt; j &lt; k \le n$$$ and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$ (for example it is possible to construct a non-degenerate triangle with sides $$$3$$$, $$$4$$$ and $$$5$$$ but impossible with sides $$$3$$$, $$$4$$$ and $$$7$$$). If it is impossible to find such triple, report it.
256 megabytes
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { PrintWriter out=new PrintWriter(System.out); InputReader in=new InputReader(System.in); //Write your logic here int t=in.nextInt(); while(t-->0) { int n=in.nextInt(); int[] a=in.readArray(n); if(a[n-1]>=a[0]+a[1]) { out.println(1+" "+2+" "+n); } else out.println(-1); } out.close(); } static class InputReader { BufferedReader br; StringTokenizer to; InputReader(InputStream stream) { br=new BufferedReader(new InputStreamReader(stream)); to=new StringTokenizer(""); } String next() { while(!to.hasMoreTokens()) { try { to=new StringTokenizer(br.readLine()); } catch(IOException e) {} } return to.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int array[]=new int[n]; for(int i=0; i<array.length; i++) array[i]=nextInt(); return array; } } public static void print_array (int[] a, int n) { for(int i=0;i<n;i++) { System.out.print(a[i]+" "); } } public static String str_insert_ele (String s, int i, char c) { String str=""; for(int j=0;j<i;j++) { str=str+s.charAt(j); } str=str+c; for(int j=i;j<s.length();j++) { str=str+s.charAt(j); } return str; } public static String str_remove_ele (String s, int i) { String str=""; for(int j=0;j<s.length();j++) { if(j!=i) str=str+s.charAt(j); } return str; } static int index(int[]a,int number) { int left =0,right=a.length-1,mid=(left+right)/2,ind=0; while (left<=right) { if (a[mid]<=number) { ind=mid+1; left=mid+1; } else right=mid-1; mid=(left+right)/2; } return ind; } }
Java
["3\n7\n4 6 11 11 15 18 20\n4\n10 10 10 11\n3\n1 1 1000000000"]
1 second
["2 3 6\n-1\n1 2 3"]
NoteIn the first test case it is impossible with sides $$$6$$$, $$$11$$$ and $$$18$$$. Note, that this is not the only correct answer.In the second test case you always can construct a non-degenerate triangle.
Java 8
standard input
[ "geometry", "math" ]
341555349b0c1387334a0541730159ac
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \le n \le 5 \cdot 10^4$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$; $$$a_{i - 1} \le a_i$$$) — the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case print the answer to it in one line. If there is a triple of indices $$$i$$$, $$$j$$$, $$$k$$$ ($$$i &lt; j &lt; k$$$) such that it is impossible to construct a non-degenerate triangle having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$, print that three indices in ascending order. If there are multiple answers, print any of them. Otherwise, print -1.
standard output
PASSED
aecfb4f27a044a797eaf3d3ae39b1135
train_003.jsonl
1597415700
You are given an array $$$a_1, a_2, \dots , a_n$$$, which is sorted in non-decreasing order ($$$a_i \le a_{i + 1})$$$. Find three indices $$$i$$$, $$$j$$$, $$$k$$$ such that $$$1 \le i &lt; j &lt; k \le n$$$ and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$ (for example it is possible to construct a non-degenerate triangle with sides $$$3$$$, $$$4$$$ and $$$5$$$ but impossible with sides $$$3$$$, $$$4$$$ and $$$7$$$). If it is impossible to find such triple, report it.
256 megabytes
import java.io.*; import java.util.*; public class abc { public static void main(String agr[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); StringBuilder sb=new StringBuilder(); while(t-->0) { int n=Integer.parseInt(br.readLine()); String s[]=br.readLine().split(" "); long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=Long.parseLong(s[i]); Arrays.sort(a); int i=0; boolean t2=false; Upper: for( i=2;i<n;i++) { if(a[0]+a[1]<=a[i]) { sb.append("1 2 "+String.valueOf(i+1)+"\n"); t2=true; break Upper; } } if(!t2&&i==n) sb.append("-1\n"); } System.out.println(String.valueOf(sb)); } }
Java
["3\n7\n4 6 11 11 15 18 20\n4\n10 10 10 11\n3\n1 1 1000000000"]
1 second
["2 3 6\n-1\n1 2 3"]
NoteIn the first test case it is impossible with sides $$$6$$$, $$$11$$$ and $$$18$$$. Note, that this is not the only correct answer.In the second test case you always can construct a non-degenerate triangle.
Java 8
standard input
[ "geometry", "math" ]
341555349b0c1387334a0541730159ac
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \le n \le 5 \cdot 10^4$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$; $$$a_{i - 1} \le a_i$$$) — the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case print the answer to it in one line. If there is a triple of indices $$$i$$$, $$$j$$$, $$$k$$$ ($$$i &lt; j &lt; k$$$) such that it is impossible to construct a non-degenerate triangle having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$, print that three indices in ascending order. If there are multiple answers, print any of them. Otherwise, print -1.
standard output
PASSED
638672b1677168b8079ac7a68fd31229
train_003.jsonl
1597415700
You are given an array $$$a_1, a_2, \dots , a_n$$$, which is sorted in non-decreasing order ($$$a_i \le a_{i + 1})$$$. Find three indices $$$i$$$, $$$j$$$, $$$k$$$ such that $$$1 \le i &lt; j &lt; k \le n$$$ and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$ (for example it is possible to construct a non-degenerate triangle with sides $$$3$$$, $$$4$$$ and $$$5$$$ but impossible with sides $$$3$$$, $$$4$$$ and $$$7$$$). If it is impossible to find such triple, report it.
256 megabytes
import java.util.*; import java.io.*; public class vivek{ 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(); } char nextChar() { return next().charAt(0); } 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[] nextArray(int rows) { int arr[] = new int[rows]; for (int i = 0; i < rows; i++) arr[i] = nextInt(); return arr; } long[] nextLongArray(int rows) { long arr[] = new long[rows]; for (int i = 0; i < rows; i++) arr[i] = nextLong(); return arr; } char[] nextCharArray(int rows) { char arr[] = new char[rows]; for (int i = 0; i < rows; i++) arr[i] = next().charAt(0); return arr; } int[][] nextMatrix(int rows, int columns) { int mat[][] = new int[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { mat[i][j] = nextInt(); } } return mat; } } public static void main(String args[]){ FastReader sc=new FastReader(); PrintWriter p=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=sc.nextLong(); if(a[0]+a[1]<=a[n-1]) p.println("1 2 "+n); else p.println("-1"); } p.flush(); } }
Java
["3\n7\n4 6 11 11 15 18 20\n4\n10 10 10 11\n3\n1 1 1000000000"]
1 second
["2 3 6\n-1\n1 2 3"]
NoteIn the first test case it is impossible with sides $$$6$$$, $$$11$$$ and $$$18$$$. Note, that this is not the only correct answer.In the second test case you always can construct a non-degenerate triangle.
Java 8
standard input
[ "geometry", "math" ]
341555349b0c1387334a0541730159ac
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \le n \le 5 \cdot 10^4$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$; $$$a_{i - 1} \le a_i$$$) — the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
800
For each test case print the answer to it in one line. If there is a triple of indices $$$i$$$, $$$j$$$, $$$k$$$ ($$$i &lt; j &lt; k$$$) such that it is impossible to construct a non-degenerate triangle having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$, print that three indices in ascending order. If there are multiple answers, print any of them. Otherwise, print -1.
standard output