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
1d4943df93ca1b4473aa3d31f89dc14a
train_003.jsonl
1526308500
After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up with a game called treasure hunt. Of course, she invited her best friends Kuro and Shiro to play with her.The three friends are very smart so they passed all the challenges very quickly and finally reached the destination. But the treasure can only belong to one cat so they started to think of something which can determine who is worthy of the treasure. Instantly, Kuro came up with some ribbons.A random colorful ribbon is given to each of the cats. Each color of the ribbon can be represented as an uppercase or lowercase Latin letter. Let's call a consecutive subsequence of colors that appears in the ribbon a subribbon. The beauty of a ribbon is defined as the maximum number of times one of its subribbon appears in the ribbon. The more the subribbon appears, the more beautiful is the ribbon. For example, the ribbon aaaaaaa has the beauty of $$$7$$$ because its subribbon a appears $$$7$$$ times, and the ribbon abcdabc has the beauty of $$$2$$$ because its subribbon abc appears twice.The rules are simple. The game will have $$$n$$$ turns. Every turn, each of the cats must change strictly one color (at one position) in his/her ribbon to an arbitrary color which is different from the unchanged one. For example, a ribbon aaab can be changed into acab in one turn. The one having the most beautiful ribbon after $$$n$$$ turns wins the treasure.Could you find out who is going to be the winner if they all play optimally?
256 megabytes
import java.util.Scanner; public class TreasureHunt { public static String Solve() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); String kuro = sc.nextLine(), shiro = sc.nextLine(), katie = sc.nextLine(); sc.close(); String[] output = {"Kuro", "Shiro", "Katie", "Draw"}; if(n >= kuro.length()) return output[3]; int[] maxArr = new int[3]; int[][] freq = new int[3][58]; for(int i = 0; i < kuro.length(); i++) { maxArr[0] = ++freq[0][kuro.charAt(i) - 65] > maxArr[0]? freq[0][kuro.charAt(i) - 65] : maxArr[0]; maxArr[1] = ++freq[1][shiro.charAt(i) - 65] > maxArr[1]? freq[1][shiro.charAt(i) - 65] : maxArr[1]; maxArr[2] = ++freq[2][katie.charAt(i) - 65] > maxArr[2]? freq[2][katie.charAt(i) - 65] : maxArr[2]; } int winner = 0, max = 0; for(int i = 0; i < 3; i++) { if(kuro.length() - maxArr[i] >= n) maxArr[i] += n; else maxArr[i] = n == 1? kuro.length() - 1: kuro.length(); if(max < maxArr[i]) { winner = i; max = maxArr[i]; } else if(max == maxArr[i]) winner = 3; } return output[winner]; } public static void main(String[] args) { System.out.println(Solve()); } }
Java
["3\nKuroo\nShiro\nKatie", "7\ntreasurehunt\nthreefriends\nhiCodeforces", "1\nabcabc\ncbabac\nababca", "15\nfoPaErcvJ\nmZaxowpbt\nmkuOlaHRE"]
1 second
["Kuro", "Shiro", "Katie", "Draw"]
NoteIn the first example, after $$$3$$$ turns, Kuro can change his ribbon into ooooo, which has the beauty of $$$5$$$, while reaching such beauty for Shiro and Katie is impossible (both Shiro and Katie can reach the beauty of at most $$$4$$$, for example by changing Shiro's ribbon into SSiSS and changing Katie's ribbon into Kaaaa). Therefore, the winner is Kuro.In the fourth example, since the length of each of the string is $$$9$$$ and the number of turn is $$$15$$$, everyone can change their ribbons in some way to reach the maximal beauty of $$$9$$$ by changing their strings into zzzzzzzzz after 9 turns, and repeatedly change their strings into azzzzzzzz and then into zzzzzzzzz thrice. Therefore, the game ends in a draw.
Java 8
standard input
[ "greedy" ]
9b277feec7952947357b133a152fd599
The first line contains an integer $$$n$$$ ($$$0 \leq n \leq 10^{9}$$$) — the number of turns. Next 3 lines contain 3 ribbons of Kuro, Shiro and Katie one per line, respectively. Each ribbon is a string which contains no more than $$$10^{5}$$$ uppercase and lowercase Latin letters and is not empty. It is guaranteed that the length of all ribbons are equal for the purpose of fairness. Note that uppercase and lowercase letters are considered different colors.
1,800
Print the name of the winner ("Kuro", "Shiro" or "Katie"). If there are at least two cats that share the maximum beauty, print "Draw".
standard output
PASSED
96ea7ac089df022c684a80d2fb47efa2
train_003.jsonl
1526308500
After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up with a game called treasure hunt. Of course, she invited her best friends Kuro and Shiro to play with her.The three friends are very smart so they passed all the challenges very quickly and finally reached the destination. But the treasure can only belong to one cat so they started to think of something which can determine who is worthy of the treasure. Instantly, Kuro came up with some ribbons.A random colorful ribbon is given to each of the cats. Each color of the ribbon can be represented as an uppercase or lowercase Latin letter. Let's call a consecutive subsequence of colors that appears in the ribbon a subribbon. The beauty of a ribbon is defined as the maximum number of times one of its subribbon appears in the ribbon. The more the subribbon appears, the more beautiful is the ribbon. For example, the ribbon aaaaaaa has the beauty of $$$7$$$ because its subribbon a appears $$$7$$$ times, and the ribbon abcdabc has the beauty of $$$2$$$ because its subribbon abc appears twice.The rules are simple. The game will have $$$n$$$ turns. Every turn, each of the cats must change strictly one color (at one position) in his/her ribbon to an arbitrary color which is different from the unchanged one. For example, a ribbon aaab can be changed into acab in one turn. The one having the most beautiful ribbon after $$$n$$$ turns wins the treasure.Could you find out who is going to be the winner if they all play optimally?
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.Scanner; /** * 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; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { int turn = in.nextInt(); String[] set = new String[3]; int index = 0; while (in.hasNext()) { set[index] = in.next(); index++; } int[] ans = new int[3]; int max = Integer.MIN_VALUE; for (int i = 0; i < 3; i++) { String s = set[i]; if (s.length() == 1) { out.print("Draw"); out.close(); } String alpha = "QWERTYUIOPASDFGHJKLZXCVBNM"; int[] count = new int[100]; count[99] = 0; char[] se = s.toCharArray(); Arrays.sort(se); for (char c : se) { int j; if (alpha.indexOf(Character.toString(c)) != -1) { j = alpha.indexOf(Character.toString(c)) + 26; } else { j = c - 'a'; } count[j]++; } int lmax = 0; for (int z : count) { int curr = 0; if (turn <= (s.length() - z)) { curr = z + turn; } else if (z == s.length() && turn == 1) { curr = s.length() - 1; } else { curr = s.length(); } if (curr > lmax) { lmax = curr; } } ans[i] = lmax; } int res = 0; for (int i = 0; i < 3; i++) { //System.out.println(ans[i]); if (ans[i] > max) { max = ans[i]; res = i; } } if ((ans[0] == ans[1] && ans[0] == max) || (ans[1] == ans[2] && ans[1] == max) || (ans[0] == ans[2] && ans[0] == max)) { out.print("Draw"); out.close(); } if (res == 0) { out.print("Kuro"); } else if (res == 1) { out.print("Shiro"); } else { out.print("Katie"); } } } }
Java
["3\nKuroo\nShiro\nKatie", "7\ntreasurehunt\nthreefriends\nhiCodeforces", "1\nabcabc\ncbabac\nababca", "15\nfoPaErcvJ\nmZaxowpbt\nmkuOlaHRE"]
1 second
["Kuro", "Shiro", "Katie", "Draw"]
NoteIn the first example, after $$$3$$$ turns, Kuro can change his ribbon into ooooo, which has the beauty of $$$5$$$, while reaching such beauty for Shiro and Katie is impossible (both Shiro and Katie can reach the beauty of at most $$$4$$$, for example by changing Shiro's ribbon into SSiSS and changing Katie's ribbon into Kaaaa). Therefore, the winner is Kuro.In the fourth example, since the length of each of the string is $$$9$$$ and the number of turn is $$$15$$$, everyone can change their ribbons in some way to reach the maximal beauty of $$$9$$$ by changing their strings into zzzzzzzzz after 9 turns, and repeatedly change their strings into azzzzzzzz and then into zzzzzzzzz thrice. Therefore, the game ends in a draw.
Java 8
standard input
[ "greedy" ]
9b277feec7952947357b133a152fd599
The first line contains an integer $$$n$$$ ($$$0 \leq n \leq 10^{9}$$$) — the number of turns. Next 3 lines contain 3 ribbons of Kuro, Shiro and Katie one per line, respectively. Each ribbon is a string which contains no more than $$$10^{5}$$$ uppercase and lowercase Latin letters and is not empty. It is guaranteed that the length of all ribbons are equal for the purpose of fairness. Note that uppercase and lowercase letters are considered different colors.
1,800
Print the name of the winner ("Kuro", "Shiro" or "Katie"). If there are at least two cats that share the maximum beauty, print "Draw".
standard output
PASSED
08aeda0ba6afea9739fbc402eee1c3a3
train_003.jsonl
1526308500
After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up with a game called treasure hunt. Of course, she invited her best friends Kuro and Shiro to play with her.The three friends are very smart so they passed all the challenges very quickly and finally reached the destination. But the treasure can only belong to one cat so they started to think of something which can determine who is worthy of the treasure. Instantly, Kuro came up with some ribbons.A random colorful ribbon is given to each of the cats. Each color of the ribbon can be represented as an uppercase or lowercase Latin letter. Let's call a consecutive subsequence of colors that appears in the ribbon a subribbon. The beauty of a ribbon is defined as the maximum number of times one of its subribbon appears in the ribbon. The more the subribbon appears, the more beautiful is the ribbon. For example, the ribbon aaaaaaa has the beauty of $$$7$$$ because its subribbon a appears $$$7$$$ times, and the ribbon abcdabc has the beauty of $$$2$$$ because its subribbon abc appears twice.The rules are simple. The game will have $$$n$$$ turns. Every turn, each of the cats must change strictly one color (at one position) in his/her ribbon to an arbitrary color which is different from the unchanged one. For example, a ribbon aaab can be changed into acab in one turn. The one having the most beautiful ribbon after $$$n$$$ turns wins the treasure.Could you find out who is going to be the winner if they all play optimally?
256 megabytes
import java.util.*; public class B { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s1 = sc.next(); String s2 = sc.next(); String s3 = sc.next(); int a1 = beauty(s1, n); int a2 = beauty(s2, n); int a3 = beauty(s3, n); if(a1>a2 && a1>a3) System.out.println("Kuro"); else if(a2>a1 && a2>a3) System.out.println("Shiro"); else if(a3>a1 && a3>a2) System.out.println("Katie"); else System.out.println("Draw"); } static int beauty(String s, int n) { int cnt[] = new int[128]; for(int i=0;i<s.length();i++){ char c = s.charAt(i); cnt[c]++; } for(char c='a';c<='z';c++){ if(cnt[c] == s.length() && n==1) cnt[c]--; else{ cnt[c]+=n; cnt[c] = Math.min(s.length() ,cnt[c]); } } for(char c='A';c<='Z';c++){ if(cnt[c] == s.length() && n==1) cnt[c]--; else{ cnt[c]+=n; cnt[c] = Math.min(s.length() ,cnt[c]); } } int max = 0; for(int i=0;i<128;i++){ max = Math.max(max, cnt[i]); } return max; } }
Java
["3\nKuroo\nShiro\nKatie", "7\ntreasurehunt\nthreefriends\nhiCodeforces", "1\nabcabc\ncbabac\nababca", "15\nfoPaErcvJ\nmZaxowpbt\nmkuOlaHRE"]
1 second
["Kuro", "Shiro", "Katie", "Draw"]
NoteIn the first example, after $$$3$$$ turns, Kuro can change his ribbon into ooooo, which has the beauty of $$$5$$$, while reaching such beauty for Shiro and Katie is impossible (both Shiro and Katie can reach the beauty of at most $$$4$$$, for example by changing Shiro's ribbon into SSiSS and changing Katie's ribbon into Kaaaa). Therefore, the winner is Kuro.In the fourth example, since the length of each of the string is $$$9$$$ and the number of turn is $$$15$$$, everyone can change their ribbons in some way to reach the maximal beauty of $$$9$$$ by changing their strings into zzzzzzzzz after 9 turns, and repeatedly change their strings into azzzzzzzz and then into zzzzzzzzz thrice. Therefore, the game ends in a draw.
Java 8
standard input
[ "greedy" ]
9b277feec7952947357b133a152fd599
The first line contains an integer $$$n$$$ ($$$0 \leq n \leq 10^{9}$$$) — the number of turns. Next 3 lines contain 3 ribbons of Kuro, Shiro and Katie one per line, respectively. Each ribbon is a string which contains no more than $$$10^{5}$$$ uppercase and lowercase Latin letters and is not empty. It is guaranteed that the length of all ribbons are equal for the purpose of fairness. Note that uppercase and lowercase letters are considered different colors.
1,800
Print the name of the winner ("Kuro", "Shiro" or "Katie"). If there are at least two cats that share the maximum beauty, print "Draw".
standard output
PASSED
86f784cdbe57c949b83805ef524e4b2c
train_003.jsonl
1526308500
After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up with a game called treasure hunt. Of course, she invited her best friends Kuro and Shiro to play with her.The three friends are very smart so they passed all the challenges very quickly and finally reached the destination. But the treasure can only belong to one cat so they started to think of something which can determine who is worthy of the treasure. Instantly, Kuro came up with some ribbons.A random colorful ribbon is given to each of the cats. Each color of the ribbon can be represented as an uppercase or lowercase Latin letter. Let's call a consecutive subsequence of colors that appears in the ribbon a subribbon. The beauty of a ribbon is defined as the maximum number of times one of its subribbon appears in the ribbon. The more the subribbon appears, the more beautiful is the ribbon. For example, the ribbon aaaaaaa has the beauty of $$$7$$$ because its subribbon a appears $$$7$$$ times, and the ribbon abcdabc has the beauty of $$$2$$$ because its subribbon abc appears twice.The rules are simple. The game will have $$$n$$$ turns. Every turn, each of the cats must change strictly one color (at one position) in his/her ribbon to an arbitrary color which is different from the unchanged one. For example, a ribbon aaab can be changed into acab in one turn. The one having the most beautiful ribbon after $$$n$$$ turns wins the treasure.Could you find out who is going to be the winner if they all play optimally?
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; public class BB { 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 (Exception e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception e) { e.printStackTrace(); } return str; } } public static void main(String args[]) { FastReader reader = new FastReader(); //long n = reader.nextLong(); int n = reader.nextInt(); String[] ksk = {"Kuro", "Shiro", "Katie"}; int[] lens = new int[3]; int[] mxs = new int[3]; int mx = -10; String draw = "Draw"; String ans = "kk"; for (int i = 0; i < 3; i++) { String str = reader.next(); lens[i] = str.length(); Integer[] szs = new Integer[1600]; for (int j = 0; j < 1600; j++) { szs[j] = new Integer(0); } for (int j = 0; j < lens[i]; j++) { //System.out.println((int) str.charAt(j) + " : " + szs[str.charAt(j)]); szs[str.charAt(j)]++; } Arrays.sort(szs, Comparator.reverseOrder()); mxs[i] = szs[0]; mxs[i] = Math.min(lens[i], szs[0] + n); if(szs[0] + n > lens[i]) { int max = -1; for (int j = 0; j < 2*26; j++) { if(szs[j]==0){ continue; } int thisAns = szs[j] + n; if(thisAns > lens[i]) { if(n == 1) { max = Math.max(max, lens[i] - 1); } else { max = Math.max(max, lens[i]); } } else { max = Math.max(max, thisAns); } } mxs[i] = max; } if(mxs[i] > mx) { mx = mxs[i]; ans = ksk[i]; } } Arrays.sort(mxs); if(mxs[2] == mxs[1]) { System.out.println(draw); return ; } System.out.println(ans); } }
Java
["3\nKuroo\nShiro\nKatie", "7\ntreasurehunt\nthreefriends\nhiCodeforces", "1\nabcabc\ncbabac\nababca", "15\nfoPaErcvJ\nmZaxowpbt\nmkuOlaHRE"]
1 second
["Kuro", "Shiro", "Katie", "Draw"]
NoteIn the first example, after $$$3$$$ turns, Kuro can change his ribbon into ooooo, which has the beauty of $$$5$$$, while reaching such beauty for Shiro and Katie is impossible (both Shiro and Katie can reach the beauty of at most $$$4$$$, for example by changing Shiro's ribbon into SSiSS and changing Katie's ribbon into Kaaaa). Therefore, the winner is Kuro.In the fourth example, since the length of each of the string is $$$9$$$ and the number of turn is $$$15$$$, everyone can change their ribbons in some way to reach the maximal beauty of $$$9$$$ by changing their strings into zzzzzzzzz after 9 turns, and repeatedly change their strings into azzzzzzzz and then into zzzzzzzzz thrice. Therefore, the game ends in a draw.
Java 8
standard input
[ "greedy" ]
9b277feec7952947357b133a152fd599
The first line contains an integer $$$n$$$ ($$$0 \leq n \leq 10^{9}$$$) — the number of turns. Next 3 lines contain 3 ribbons of Kuro, Shiro and Katie one per line, respectively. Each ribbon is a string which contains no more than $$$10^{5}$$$ uppercase and lowercase Latin letters and is not empty. It is guaranteed that the length of all ribbons are equal for the purpose of fairness. Note that uppercase and lowercase letters are considered different colors.
1,800
Print the name of the winner ("Kuro", "Shiro" or "Katie"). If there are at least two cats that share the maximum beauty, print "Draw".
standard output
PASSED
0e018e34feb90b5517b7570dafc629fe
train_003.jsonl
1526308500
After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up with a game called treasure hunt. Of course, she invited her best friends Kuro and Shiro to play with her.The three friends are very smart so they passed all the challenges very quickly and finally reached the destination. But the treasure can only belong to one cat so they started to think of something which can determine who is worthy of the treasure. Instantly, Kuro came up with some ribbons.A random colorful ribbon is given to each of the cats. Each color of the ribbon can be represented as an uppercase or lowercase Latin letter. Let's call a consecutive subsequence of colors that appears in the ribbon a subribbon. The beauty of a ribbon is defined as the maximum number of times one of its subribbon appears in the ribbon. The more the subribbon appears, the more beautiful is the ribbon. For example, the ribbon aaaaaaa has the beauty of $$$7$$$ because its subribbon a appears $$$7$$$ times, and the ribbon abcdabc has the beauty of $$$2$$$ because its subribbon abc appears twice.The rules are simple. The game will have $$$n$$$ turns. Every turn, each of the cats must change strictly one color (at one position) in his/her ribbon to an arbitrary color which is different from the unchanged one. For example, a ribbon aaab can be changed into acab in one turn. The one having the most beautiful ribbon after $$$n$$$ turns wins the treasure.Could you find out who is going to be the winner if they all play optimally?
256 megabytes
import java.util.*; import java.io.*; public class Main { void solve(){ int n=ni(); char s[][]=new char[3][]; int cc[]=new int[3]; int cnt[]=new int[123]; // pw.println((int)('a')+" "+((int)('z'))+" "+((int)('A'))+" "+((int)('Z'))); for(int i=0;i<3;i++) { s[i]=ns().toCharArray(); Arrays.fill(cnt,0); for(int j=0;j<s[i].length;j++) cnt[(int)(s[i][j])]++; int l=s[i].length; for(int j=0;j<123;j++) { if((j>9 && j<65) ||(j>90 && j<97)) continue; if(l-cnt[j]<n){ if(l-cnt[j]==0 && n==1) cc[i]=Math.max(cc[i],l-1); else cc[i]=Math.max(cc[i],l); }else { cc[i]=Math.max(cc[i],cnt[j]+n); } } } int mx=Math.max(cc[0],Math.max(cc[1],cc[2])); if((mx==cc[0] && mx==cc[1]) || (mx==cc[0] && mx==cc[2]) || (mx==cc[2] && mx==cc[1]) ) pw.println("Draw"); else if(cc[0]>cc[1] && cc[0]>cc[2]) pw.println("Kuro"); else if(cc[1]>cc[0] && cc[1]>cc[2]) pw.println("Shiro"); else if(cc[2]>cc[0] && cc[2]>cc[1]) pw.println("Katie"); } long M=(long)1e9+7; InputStream is; PrintWriter pw; String INPUT = ""; void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); pw = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); pw.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Main().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 void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); } }
Java
["3\nKuroo\nShiro\nKatie", "7\ntreasurehunt\nthreefriends\nhiCodeforces", "1\nabcabc\ncbabac\nababca", "15\nfoPaErcvJ\nmZaxowpbt\nmkuOlaHRE"]
1 second
["Kuro", "Shiro", "Katie", "Draw"]
NoteIn the first example, after $$$3$$$ turns, Kuro can change his ribbon into ooooo, which has the beauty of $$$5$$$, while reaching such beauty for Shiro and Katie is impossible (both Shiro and Katie can reach the beauty of at most $$$4$$$, for example by changing Shiro's ribbon into SSiSS and changing Katie's ribbon into Kaaaa). Therefore, the winner is Kuro.In the fourth example, since the length of each of the string is $$$9$$$ and the number of turn is $$$15$$$, everyone can change their ribbons in some way to reach the maximal beauty of $$$9$$$ by changing their strings into zzzzzzzzz after 9 turns, and repeatedly change their strings into azzzzzzzz and then into zzzzzzzzz thrice. Therefore, the game ends in a draw.
Java 8
standard input
[ "greedy" ]
9b277feec7952947357b133a152fd599
The first line contains an integer $$$n$$$ ($$$0 \leq n \leq 10^{9}$$$) — the number of turns. Next 3 lines contain 3 ribbons of Kuro, Shiro and Katie one per line, respectively. Each ribbon is a string which contains no more than $$$10^{5}$$$ uppercase and lowercase Latin letters and is not empty. It is guaranteed that the length of all ribbons are equal for the purpose of fairness. Note that uppercase and lowercase letters are considered different colors.
1,800
Print the name of the winner ("Kuro", "Shiro" or "Katie"). If there are at least two cats that share the maximum beauty, print "Draw".
standard output
PASSED
66e35f358262528cd48444e0c7ddea09
train_003.jsonl
1526308500
After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up with a game called treasure hunt. Of course, she invited her best friends Kuro and Shiro to play with her.The three friends are very smart so they passed all the challenges very quickly and finally reached the destination. But the treasure can only belong to one cat so they started to think of something which can determine who is worthy of the treasure. Instantly, Kuro came up with some ribbons.A random colorful ribbon is given to each of the cats. Each color of the ribbon can be represented as an uppercase or lowercase Latin letter. Let's call a consecutive subsequence of colors that appears in the ribbon a subribbon. The beauty of a ribbon is defined as the maximum number of times one of its subribbon appears in the ribbon. The more the subribbon appears, the more beautiful is the ribbon. For example, the ribbon aaaaaaa has the beauty of $$$7$$$ because its subribbon a appears $$$7$$$ times, and the ribbon abcdabc has the beauty of $$$2$$$ because its subribbon abc appears twice.The rules are simple. The game will have $$$n$$$ turns. Every turn, each of the cats must change strictly one color (at one position) in his/her ribbon to an arbitrary color which is different from the unchanged one. For example, a ribbon aaab can be changed into acab in one turn. The one having the most beautiful ribbon after $$$n$$$ turns wins the treasure.Could you find out who is going to be the winner if they all play optimally?
256 megabytes
import java.util.*; import java.io.*; public class Main { void solve(){ int n=ni(); char s[][]=new char[3][]; int cc[]=new int[3]; int cnt[]=new int[123]; // pw.println((int)('a')+" "+((int)('z'))+" "+((int)('A'))+" "+((int)('Z'))); for(int i=0;i<3;i++) { s[i]=ns().toCharArray(); Arrays.fill(cnt,0); for(int j=0;j<s[i].length;j++) cnt[(int)(s[i][j])]++; int l=s[i].length; for(int j=0;j<123;j++) { if((j>9 && j<65) ||(j>90 && j<97)) continue; if(l-cnt[j]<n){ if(l-cnt[j]==0 && n==1) cc[i]=Math.max(cc[i],l-1); else cc[i]=Math.max(cc[i],l); }else { cc[i]=Math.max(cc[i],cnt[j]+n); } } } int mx=Math.max(cc[0],Math.max(cc[1],cc[2])); if((mx==cc[0] && mx==cc[1]) || (mx==cc[0] && mx==cc[2]) || (mx==cc[2] && mx==cc[1]) ) pw.println("Draw"); else if(cc[0]>cc[1] && cc[0]>cc[2]) pw.println("Kuro"); else if(cc[1]>cc[0] && cc[1]>cc[2]) pw.println("Shiro"); else if(cc[2]>cc[0] && cc[2]>cc[1]) pw.println("Katie"); } long M=(long)1e9+7; InputStream is; PrintWriter pw; String INPUT = ""; void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); pw = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); pw.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Main().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 void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); } }
Java
["3\nKuroo\nShiro\nKatie", "7\ntreasurehunt\nthreefriends\nhiCodeforces", "1\nabcabc\ncbabac\nababca", "15\nfoPaErcvJ\nmZaxowpbt\nmkuOlaHRE"]
1 second
["Kuro", "Shiro", "Katie", "Draw"]
NoteIn the first example, after $$$3$$$ turns, Kuro can change his ribbon into ooooo, which has the beauty of $$$5$$$, while reaching such beauty for Shiro and Katie is impossible (both Shiro and Katie can reach the beauty of at most $$$4$$$, for example by changing Shiro's ribbon into SSiSS and changing Katie's ribbon into Kaaaa). Therefore, the winner is Kuro.In the fourth example, since the length of each of the string is $$$9$$$ and the number of turn is $$$15$$$, everyone can change their ribbons in some way to reach the maximal beauty of $$$9$$$ by changing their strings into zzzzzzzzz after 9 turns, and repeatedly change their strings into azzzzzzzz and then into zzzzzzzzz thrice. Therefore, the game ends in a draw.
Java 8
standard input
[ "greedy" ]
9b277feec7952947357b133a152fd599
The first line contains an integer $$$n$$$ ($$$0 \leq n \leq 10^{9}$$$) — the number of turns. Next 3 lines contain 3 ribbons of Kuro, Shiro and Katie one per line, respectively. Each ribbon is a string which contains no more than $$$10^{5}$$$ uppercase and lowercase Latin letters and is not empty. It is guaranteed that the length of all ribbons are equal for the purpose of fairness. Note that uppercase and lowercase letters are considered different colors.
1,800
Print the name of the winner ("Kuro", "Shiro" or "Katie"). If there are at least two cats that share the maximum beauty, print "Draw".
standard output
PASSED
2bd132f2dfaed2cd56a56eaf05538ff3
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.PrintStream; import java.util.Scanner; public class Main implements Runnable, AutoCloseable { Scanner in = new Scanner(new BufferedInputStream(System.in)); // StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintStream out = new PrintStream(new BufferedOutputStream(System.out)); @Override public void run() { int t = in.nextInt(); for (int i = 0; i < t; i++) { doOnce(); } } private void doOnce() { if (test(in.nextInt(), in.nextInt())) { out.println("YES"); } else { out.println("NO"); } } private boolean test(int n, int m) { switch (n) { case 1: return m == 1; case 2: case 3: return m <= 3; default: return true; } } public static void main(String[] args) { try (Main main = new Main()) { main.run(); } } @Override public void close() { out.close(); } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
0c1d6337ce7265d44644155d37c7ad8b
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import javax.swing.plaf.synth.SynthSeparatorUI; public class temp1 { static long sx = 0, sy = 0, t = 0; public static void main(String[] args) throws IOException { Reader scn = new Reader(); // Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while (t-- != 0) { long x = scn.nextLong(), y = scn.nextLong(); if ((x == 1 && y != 1) || (x == 2 && (y > 3)) || (x == 3 && (y > 3))) System.out.println("NO"); else System.out.println("YES"); } } // _________________________TEMPLATE_____________________________________________________________ // private static int gcd(int a, int b) { // if(a== 0) // return b; // // return gcd(b%a,a); // } // static class comp implements Comparator<pair> { // // @Override // public int compare(pair o1, pair o2) { // // return (int) (o1.y - o1.y); // } // // } private static class pair implements Comparable<pair> { long x, y; pair(long a, long b) { x = a; y = b; } @Override public int compareTo(pair o) { long d1 = Math.abs(sx - this.x) + Math.abs(sy - this.y); long d2 = Math.abs(sx - o.x) + Math.abs(sy - o.y); if (d1 < d2) return -1; else return 1; } } public static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[100000 + 1]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public int[][] nextInt2DArray(int m, int n) throws IOException { int[][] arr = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) arr[i][j] = nextInt(); } return arr; } } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
7ba5b617c780ddd84d4dbd923ff4e8a2
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.util.*; public class HackerEarth { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int x=sc.nextInt(); int y=sc.nextInt(); if(x>=y) { System.out.println("YES"); } else if((x==1&&y!=1)||(x==3&&y!=3)||(x==2&&y>3)) { System.out.println("NO"); } else { System.out.println("YES"); } } } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
a9c6cc42df44730c0db9d5cac2a26cd7
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.HashSet; import java.util.InputMismatchException; import java.util.StringTokenizer; public class B { static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } static 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; } BigInteger nextBigInteger() { try { return new BigInteger(nextLine()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } } public static void main(String[] args) throws IOException { FastReader fr = new FastReader(); FastWriter fw = new FastWriter(); int t = fr.nextInt(); o: while (t-- > 0) { long a = fr.nextLong(); long b = fr.nextLong(); HashSet<Long> set = new HashSet<>(); int count = 0; while (count < 1000) { set.add(a); if (a >= b) { fw.println("YES"); continue o; } if (a % 2 == 0) { a *= 3; a /= 2; } else a -= 1; if (set.contains(a)) { fw.println("NO"); continue o; } count++; } fw.println("YES"); } fw.close(); } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
d1819bf227d0ab014adf5c299afab709
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.HashSet; public class Magic { public static void main(String[] args) { FastIO sc = new FastIO(); int cases = sc.nextInt(); double start; double inter; int end; HashSet<Double> arr; for (int i = 0; i < cases; i++) { start = (double) sc.nextInt(); end = sc.nextInt(); arr = new HashSet<Double>(); while (start < end && start == (int) start && start > 0) { arr.add(start); if (start % 2 == 0) { //System.out.println(start); start = start * 1.5; } else { start--; if (arr.contains(start)) { break; } } } if (start >= end && start == (int) start) { //System.out.println("yes"); sc.println("yes"); } else { //System.out.println("no"); sc.println("no"); } } sc.close(); } } class FastIO extends PrintWriter { BufferedReader br; StringTokenizer st; public FastIO() { super(new BufferedOutputStream(System.out)); 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
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
b0057a23f2b2069a8d2356c7d4928f71
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.util.Scanner; public class prob { public static void main(String[] args) { Scanner input = new Scanner(System.in); long t,i; t=input.nextInt(); while(t-->0){ long n,a,m,sum=1,b; a=input.nextInt(); b=input.nextInt(); if(a<2 && b>1 || a<4 && b>3){ System.out.println("NO"); } else{ System.out.println("YES"); } } } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
5a2bd1e51ea5f68504cb0e2074b767f6
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class MyProgram { public static FastIO file = new FastIO(); private static void solve() { int tt = nextInt(); while (tt-- > 0) { int x = nextInt(), y = nextInt(); if ((x < y && x > 3) || x > y || x == y || (x % 2 == 0 && ((x * 3 / 2 - 1 != x) || x * 3 / 2 == y))) System.out.println("Yes"); else System.out.println("No"); } } private static int[] nextArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public static int[][] nextArray2D(int n, int m) { int[][] result = new int[n][]; for (int i = 0; i < n; i++) { result[i] = nextArray(m); } return result; } public static long pow(long n, long p) { long ret = 1L; while (p > 0) { if (p % 2 != 0L) ret *= n; n *= n; p >>= 1L; } return ret; } public static String next() { return file.next(); } public static int nextInt() { return file.nextInt(); } public static long nextLong() { return file.nextLong(); } public static double nextDouble() { return file.nextDouble(); } public static String nextLine() { return file.nextLine(); } public static class FastIO { BufferedReader br; StringTokenizer st; PrintWriter out; public FastIO() { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { solve(); } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
a6dc222a38d6ea64f4eb5c3c6a2fef1b
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.sqrt; import static java.lang.Math.floor; import static java.lang.Math.abs; public class cf { static final double EPS = 1e-6; final static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); final static Scanner sc=new Scanner(System.in); public static void main(String[] args) throws IOException { // int t=Integer.parseInt(br.readLine()); int t=sc.nextInt(); while (t-->0) { // int[] input=sia(2); long x=sc.nextLong(); long y=sc.nextLong(); if (x==1&&y==1) { System.out.println("yes"); } else if ((x==2||x==3)&&y<4) { System.out.println("yes"); } else if (x>3) { System.out.println("yes"); } else System.out.println("no"); } //System.out.println(primeFactors(sc.nextLong())); } public static int[] siasc(int n) { int[] a=new int[n]; for (int i = 0; i < n; i++) { a[i]=sc.nextInt(); } return a; } static int[] getArr(BigInteger num) { String str = num.toString(); int[] arr = new int[str.length()]; for (int i=0; i<arr.length; i++) arr[i] = str.charAt(i)-'0'; return arr; } static int min(Queue<Integer> q){ int min=10000; for (Integer var : q) { min=Math.min(min, var); } return min; } public static int[] sia(int n) throws Exception{ int a[] = new int[n]; String line = br.readLine(); String[] strs = line.trim().split("\\s+"); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(strs[i]); } return a; } public static String baseConversion(String number,int sBase, int dBase) { // Parse the number with source radix // and return in specified radix(base) return Long.toString(Long.parseLong(number, sBase),dBase); } static long phi(long n) { long result = n; long m=n; for (long p = 2; p * p <= n; ++p) { if (n % p == 0) { while (n % p == 0) n /= p; result -= result / p; } } if (n > 1) result -= result / n; if(result==m-1) result++; return result; } static long primeFactors(long n) { long m=n; if (n%2==0) { m-=n/2; } while (n%2==0) { n /= 2; } for (int i = 3; i <= Math.sqrt(n); i+= 2) { while (n%i == 0) { m-=n/i; n /= i; } } if (n > 2) return m-n/n; return m+1; } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
1adce5217b15bd977370eb64b14a3ac8
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int testNum = Integer.parseInt(st.nextToken()); for (int testInx = 0; testInx < testNum; testInx++) { st = new StringTokenizer(br.readLine()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); if (solve(x, y)) System.out.println("YES"); else System.out.println("NO"); } br.close(); } private static boolean solve(int x, int y) { if(x >= y) { return true; }else { // if(x==2 && y>3) return false; else if(x==3 && y>3) return false; else if(x >= 4) return true; else if(x==1 && y!=1) return false; } return true; } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
9b56701b8a71dda12b9c86fb3e6fdc40
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.util.*; public class magicstick { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i=0;i<n;i++) { int a = sc.nextInt(); int b = sc.nextInt(); if(a==1 && b==1) { System.out.println("YES"); continue; } else if((a>1 && a<=3) && (b>1 && b<=3)) { System.out.println("YES"); continue; } else if(a>=b) { System.out.println("YES"); continue; } else if(a>3) { System.out.println("YES"); continue; } else { System.out.println("NO"); continue; } } } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
77e0131b27cdc78f321b9d7474946cc1
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.LinkedList; public class Main { static BufferedReader input = new BufferedReader( new InputStreamReader(System.in) ); public static void main(String[] args) throws IOException { short tests = Short.parseShort(input.readLine()); while (tests-- > 0) { String[] x = input.readLine().split(" "); int a = Integer.parseInt(x[0]); int b = Integer.parseInt(x[1]); if (solve(a,b)) System.out.println("yes"); else System.out.println("no"); } } public static boolean solve(int a, int b) { HashSet<Integer> found = new HashSet<>(); while (a < b) { if (a == 1) return false; a = a / 2 * 3; if (found.contains(a)) return false; found.add(a); } return true; } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
7ade55295ed71924b79039b4772b28fb
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.util.Scanner; public class spell { static String proc(int x, int y){ if (x==y) return "YES"; else if ((x==2 && y==3)) return "YES"; else if (x==1 && y>x) return "NO"; else if ((x==2 || x==3) && y>3) return "NO"; else return "YES"; } public static void main(String[] args){ Scanner input = new Scanner(System.in); int testNum=input.nextInt(); for (int i=0; i<testNum; i++){ int x=input.nextInt(); int y=input.nextInt(); System.out.println(proc(x,y)); } } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
afffac4b0666543ebff4fc66b984340c
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.io.PrintWriter; import java.util.Scanner; public class Spell { public static void main(String args[]) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int x = sc.nextInt(), y = sc.nextInt(); //if (x >= y || (x != 3 && x != 1 && x != 2) || (x == 2 && y == 3)) if (x < y && (x == 3 || x == 1 || (x == 2 && y != 3))) out.println("NO"); else out.println("YES"); } sc.close(); out.flush(); out.close(); } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
7275aaf808cb3f2e19c5dd7226e3741e
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.io.PrintWriter; import java.util.Scanner; public class Spell { public static void main(String args[]) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int x = sc.nextInt(), y = sc.nextInt(); if (x >= y || (x != 3 && x != 1 && x != 2) || (x == 2 && y == 3)) out.println("YES"); else out.println("NO"); } sc.close(); out.flush(); out.close(); } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
ad60c6a8db775aae6b5e5a009ecd8efb
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import javax.swing.*; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { int T = 0; File f = new File("input.txt"); Scanner sc = new Scanner(System.in); T = sc.nextInt(); for(int t = 0; t < T; t++) {//2-3-2 int a = sc.nextInt(); int b = sc.nextInt(); if(a == 1 && b > 1) System.out.println("NO"); else if(a >= b) System.out.println("YES"); else if (a <= 3) { if (b <= 3) System.out.println("YES"); else System.out.println("NO"); }else { System.out.println("YES"); } } } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
65161b0183255a3aaf955c3de3cd931a
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.util.*; import java.util.Map; import java.util.HashMap; import java.lang.Math; public class s { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int testcases=sc.nextInt(); for(int testcase=0;testcase<testcases;testcase++) { long x=sc.nextLong(); long y=sc.nextLong(); if(x<2) { if(y==0 || y==1) { System.out.println("YES"); } else { System.out.println("NO"); } } else if(x<4) { if(y==0 || y==1 ||y==2 || y==3) { System.out.println("YES"); } else { System.out.println("NO"); } } else { System.out.println("YES"); } } } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
3b8910a97974f220b6fa3b7c4c03db42
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.util.Scanner; public class Solution1257B { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0) { int x = scanner.nextInt(); int y = scanner.nextInt(); if (x >= y) { System.out.println("YES"); } else { if (x == 1) { System.out.println("NO"); } else if (x == 2 && (y == 1 || y == 3)) { System.out.println("YES"); } else if (x == 3 && y == 2) { System.out.println("YES"); } else if (x > 3) { System.out.println("YES"); } else { System.out.println("NO"); } } } } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
e18b6807e5c3608e888dd9d26f909492
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.util.Scanner; public class B { public static void main(String args[]) { Scanner in = new Scanner(System.in); int q = in.nextInt(); while (q-- > 0) { int x = in.nextInt(); int y = in.nextInt(); if (x == 1) System.out.println((y == 1 ? "YES\n" : "NO\n")); else if (x <= 3) System.out.println(y <= 3 ? "YES\n" : "NO\n"); else System.out.println("YES\n"); } in.close(); } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
ddc2643609b5566d26ff683afe12fc15
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
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 MagicStick { public static void main(String[] args) { FastScanner fs=new FastScanner(); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int x = fs.nextInt(), y = fs.nextInt(); if(y<=x) { System.out.println("Yes"); } else { if(x==2 && y==3) System.out.println("Yes"); else if(x<=3) System.out.println("No"); else System.out.println("Yes"); }} } 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
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
2182fda4dfe9607250dfd5247c00aae1
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
// package com.company; import java.text.DecimalFormat; import java.util.*; import java.io.*; import java.lang.*; public class Main{ /* ** || ॐ श्री गणेशाय नमः || ** @𝙖𝙪𝙩𝙝𝙤𝙧 𝙅𝙞𝙜𝙖𝙧_𝙉𝙖𝙞𝙣𝙪𝙟𝙞 ** 𝙎𝙑𝙉𝙄𝙏-𝙎𝙐𝙍𝘼𝙏 */ public static void main(String args[]){ PrintWriter out=new PrintWriter(System.out); InputReader in=new InputReader(System.in); TASK solver = new TASK(); int t=1; t = in.nextInt(); for(int i=0;i<t;i++) { solver.solve(in,out,i); } out.close(); } static class TASK { static int mod = 1000000007; void solve(InputReader in, PrintWriter out, int testNumber) { int x = in.nextInt(); int y= in.nextInt(); if(y<=x) { System.out.println("YES"); return; } if(x==1 && y!=1) { System.out.println("NO"); return; } if(x==2 && y==3) { System.out.println("YES"); return; } if(x>=4) { System.out.println("YES"); } else System.out.println("NO"); } } static class pair{ int x; int y; pair(int x,int y) { this.x=x; this.y=y; } } static class Maths { static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static long factorial(int n) { long fact = 1; for (int i = 1; i <= n; i++) { fact *= i; } return fact; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
a7929e4bb3726aa3285230bc7c4496e9
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); while (N-- > 0) { int a = sc.nextInt(), b = sc.nextInt(); if (a == 1 && b == 1) { System.out.println("YES"); } else if (a < 4 && a > 1 && b <= 3) { System.out.println("YES"); } else if (a >= 4) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
b0948b4a92569bb09b5ec8143e7ad9ce
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
//normal import java.util.*; import java.lang.*; import java.io.*; // String Tokenizer public class Main { public static void main(String[] args) { // code Scanner scn = new Scanner(System.in); // System.out.println("Jai"); int t = scn.nextInt(); while (t > 0) { t--; long a = scn.nextLong(); long b = scn.nextLong(); if (a >= b) { System.out.println("YES"); } else { HashMap<Long, Integer> map = new HashMap<>(); boolean pp = false, op = true; while (a < b) { if (a == 0) { break; } if (a % 2 == 0) { a = (a * 3) / 2; } else { if (map.containsKey(a) == true) { op = false; break; } else { map.put(a, 0); a--; } } } if (a == 0 || op == false) { System.out.println("NO"); } else { System.out.println("YES"); } } } } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
a74483dea3aa9b9cd9f5fb627b4e64d6
train_003.jsonl
1573655700
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.
256 megabytes
import java.util.Scanner;; public class MagicStick { public static void main(String[] args) { Scanner scan = new Scanner( System.in ); int t = scan.nextInt(); int x, y; for( int i = 0; i < t; i++ ) { x = scan.nextInt(); y = scan.nextInt(); if( x >= y ) { System.out.println("YES"); } else if( x <= 3 ) { if(x == 3 ) System.out.println("NO"); else if( x == 2) { if( y == 3 ) System.out.println("YES"); else System.out.println("NO"); } else if( x == 1 ) { System.out.println("NO"); } } else { System.out.println("YES"); } } } }
Java
["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"]
1 second
["YES\nYES\nNO\nYES\nNO\nYES\nYES"]
null
Java 11
standard input
[ "math" ]
b3978805756262e17df738e049830427
The first line contains single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the current number and the number that Petya wants to get.
1,000
For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
standard output
PASSED
e7dcc9b8fc379f7dd54c7f68a5683b0b
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
////b ////import javax.swing.plaf.synth.SynthDesktopIconUI; //import com.sun.source.tree.Tree; // //import java.lang.reflect.Array; import java.util.*; import java .io.*; public class Yahia { public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); int n=sc.nextInt(),m=sc.nextInt(); Integer[] x =new Integer[n]; for (int i=0;i<n;++i) x[i]=sc.nextInt(); Arrays.sort(x); ArrayList<Integer>arr=new ArrayList<>(); arr.add(x[0]); for (int i=1;i<n;++i) arr.add(x[i]-x[i-1]); StringBuilder sb=new StringBuilder(); int yaya=0; for (int i : arr){ if (yaya<m) { if (i != 0) { sb.append(i + "\n"); ++yaya; } }else break; } for (;yaya<m;++yaya) sb.append("0\n"); System.out.println(sb); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
130f4c332840fa17d8beba7cf8f3f5d7
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.util.*; import static java.lang.Math.*; public class m { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), k = in.nextInt(); TreeSet<Integer> gg = new TreeSet<Integer>(); for (int i = 0;i < n;i++) { int q; q = in.nextInt(); gg.add(new Integer(q)); } Iterator<Integer> iter = gg.iterator(); int cur = 0; while (iter.hasNext() && k-- > 0) { int q = iter.next().intValue(); System.out.println(q - cur); cur = q; } while (k-- > 0) { System.out.println("0"); } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
090f0558158da4a7472a1111f8094792
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.util.*; import static java.lang.Math.*; public class m { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), k = in.nextInt(); SortedSet<Integer> gg = new TreeSet<Integer>(); for (int i = 0;i < n;i++) { int q; q = in.nextInt(); gg.add(new Integer(q)); } Iterator<Integer> iter = gg.iterator(); int cur = 0; while (iter.hasNext() && k-- > 0) { int q = iter.next().intValue(); System.out.println(q - cur); cur = q; } while (k-- > 0) { System.out.println("0"); } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
c27413a3d6235ab948652ea4ee0f9452
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.util.*; public class m { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), k = in.nextInt(); int a[] = new int[n + 1]; for (int i = 0;i < n;i++) { a[i] = in.nextInt(); } a[n] = 0; Arrays.sort(a); for (int i = 1;i <= n;i++) { if (k == 0) { break; } if (a[i] != a[i - 1]) { k--; System.out.println(a[i] - a[i - 1]); } } for (int i = 0;i < k;i++) { System.out.println("0"); } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
d9a09cda5e9417e640a8b6f5f6e05a83
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.io.*; import java.util.*; public class Task { public static void main(String[] args) throws Exception { 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"); 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 Exception { //int t = in.nextInt(); int t = 1; while (t > 0) { solve(); t--; } out.flush(); out.close(); } int inf = 2000000000; int mod = 1000000007; double eps = 0.000000001; int n; int m; ArrayList<Integer>[] g; int[] a; void solve() throws IOException { int n = in.nextInt(); int k = in.nextInt(); a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); Arrays.sort(a); long s = 0; int cur = 0; for (int i = 0; i < k; i++) { while (cur < n && a[cur] - s == 0) cur++; if (cur >= n) { out.println(0); continue; } out.println(a[cur] - s); s += a[cur] - s; } } 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 1; if (a < p.a) return -1; if (b > p.b) return 1; if (b < p.b) return -1; return 0; } } 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(); } } static class InputReader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public InputReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public InputReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
97838984795343a1abfc840c80bb3ae2
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.util.*; public class C { static Scanner k = new Scanner(System.in); static long [] y; static int n, m; static int bs(int index, long sum){ int low = index; int high = y.length - 1; //long ans = Long.MAX_VALUE; int ansIndex = -1; while(low <= high){ if(low == high){ ansIndex = low; break; } int mid = (low + high) / 2; if(y[mid]-sum <= 0){ low = mid + 1; } else{ high = mid; ansIndex = mid; //ans = Math.min(ans, y[mid]); } } if(ansIndex == -1){ return y.length; } return ansIndex; } public static void main(String[] args) { while(k.hasNext()){ n = k.nextInt(); m = k.nextInt(); y = new long[n]; for(int i = 0; i<n; i++){ y[i] = k.nextLong(); } Arrays.sort(y); int index = 0; long sum = 0; while(m-->0){ int nonZeroMinValueIndex = bs(index, sum); if(nonZeroMinValueIndex >= y.length){ System.out.println(0); } else{ System.out.println(y[nonZeroMinValueIndex] - sum); sum += (y[nonZeroMinValueIndex] - sum); index = nonZeroMinValueIndex + 1; } } } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
0329fca2cb2d069301faeb7f032941fb
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
/* BY - LAXMIKANT KATRE (laxmikant2000) */ import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(); int k = sc.nextInt(); long arr[] = new long[n]; List<Long> ans = new ArrayList<>(); long min = Long.MAX_VALUE; for(int i = 0; i < n; ++i) { arr[i] = sc.nextLong(); } Arrays.sort(arr); ans.add(arr[0]); for(int i = 1; i < n; ++i) { long x = arr[i] - arr[i - 1]; if(x != 0) ans.add(x); } if(ans.size() < k) { int x = k - ans.size(); for(int i = 0; i < x; ++i) ans.add(0L); } for(int i = 0; i < k; ++i) out.println(ans.get(i)); out.close(); /* int n = sc.nextInt(); // read input as integer long k = sc.nextLong(); // read input as long double d = sc.nextDouble(); // read input as double String str = sc.next(); // read input as String String s = sc.nextLine(); // read whole line as String int result = 40*n; out.println(result); // print via PrintWriter */ } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int 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 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
daee2e8762c64c2e811cdf9429acd5c2
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.util.Arrays; import java.util.HashMap; import java.util.Scanner; public class Akele { public static void main(String args[]) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int k=s.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++)arr[i]=s.nextInt(); Arrays.sort(arr); int sum=0; int index=0; for (; index <n ; index++) { if (arr[index]>0){ break; } } while(k-->0){ if (index<n){ sum+=arr[index]; System.out.println(arr[index]); }else{ System.out.println(0); } for (; index <n ; index++) { if (arr[index]>sum){ arr[index]-=sum; break; } } } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
fcec3de3aa5b10c5be1664a92f1285b1
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
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.HashSet; import java.util.StringTokenizer; /** * @author Andrei Chugunov */ public class A { private BufferedReader br; private PrintWriter out; private StringTokenizer st; void solve() throws IOException { int n = nextInt(); int k = nextInt(); Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } HashSet<Integer> newArr = new HashSet<>(Arrays.asList(a)); Object[] uniqueInt = newArr.toArray(); Arrays.sort(uniqueInt); long min = (Integer) uniqueInt[0]; for (int i = 0; i < k; i++) { out.println(min); if (i + 1 >= uniqueInt.length) { min = 0; } else { min = (Integer) uniqueInt[i + 1] - (Integer) uniqueInt[i]; } } } A() 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 A(); } String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { return null; } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } String nextString() throws IOException { return br.readLine(); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
9fbc6585280ac2e8412e17d87649062a
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; import java.util.Arrays; public class Subs{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } Arrays.sort(arr); int ini=0; int i=0; int numtim=0; for(;i<n&& numtim<k;i++){ while(i<n && arr[i]-ini==0)i++; if(i<n){ System.out.println(arr[i]-ini); ini=arr[i]; numtim++; } else break; } while(numtim<k) { numtim++; System.out.println("0"); } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
c908753c9c462368d5625315e247a87c
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.Scanner; import java.util.Collections; import java.util.ArrayList; /** * 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; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); List<Integer> l = new ArrayList<>(); for (int i = 0; i != n; i++) { l.add(in.nextInt()); } Collections.sort(l); int start = 0; for (int i = 0; i != n; i++) { if (l.get(i) == 0) start++; } int sum = 0; while (k-- != 0) { if (start >= l.size()) { out.println("0"); } else { l.set(start, l.get(start) - sum); if (l.get(start) > 0) { out.println(l.get(start)); sum += l.get(start); } else k++; start++; } } } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
9680bcb50c75db55301a8e65967c20fe
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.util.*; public class a{ public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int k=in.nextInt(); ArrayList<Integer> ar=new ArrayList<Integer>(); for(int i=0;i<n;i++) ar.add(in.nextInt()); Collections.sort(ar); LinkedHashSet<Integer> set=new LinkedHashSet<Integer>(); for(int i=0;i<ar.size();i++){ set.add(ar.get(i)); } int min=0; int steps=0; for(int x: set){ if(steps==k) break; else steps++; if((x-min)!=0){ System.out.println(x-min); } min=min+(x-min); } int length=k-steps; for(int i=0;i<length;i++) System.out.println(0); } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
43c8b7fe569c9b5c9d8242384c67201b
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class tG { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args){ FastReader in=new FastReader(); int n=in.nextInt(); int k=in.nextInt(); ArrayList<Integer> ar=new ArrayList<Integer>(); for(int i=0;i<n;i++) ar.add(in.nextInt()); Collections.sort(ar); LinkedHashSet<Integer> set=new LinkedHashSet<Integer>(); for(int i=0;i<ar.size();i++){ set.add(ar.get(i)); } long min=0; int steps=0; for(int x: set){ if(steps==k) break; else steps++; if((x-min)!=0){ System.out.println(x-min); } min=min+(x-min); } int length=k-steps; for(int i=0;i<length;i++) System.out.println(0); } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
04e56d2e5555a14dec5e7c9c23cb3158
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.util.*; public class a{ public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int k=in.nextInt(); ArrayList<Integer> ar=new ArrayList<Integer>(); for(int i=0;i<n;i++) ar.add(in.nextInt()); Collections.sort(ar); LinkedHashSet<Integer> set=new LinkedHashSet<Integer>(); for(int i=0;i<ar.size();i++){ set.add(ar.get(i)); } long min=0; int steps=0; for(int x: set){ if(steps==k) break; else steps++; if((x-min)!=0){ System.out.println(x-min); } min=min+(x-min); } int length=k-steps; for(int i=0;i<length;i++) System.out.println(0); } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
ffe6c674c9b7a3fecd860e3091269008
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; import java.math.BigInteger; public class Main { private static final FS scanner = new FS(System.in); //private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { int n = scanner.nextInt(); int k = scanner.nextInt(); SortedSet<Integer> set = new TreeSet<>(); set.add(0); for (int i = 0; i < n; i++) { int value = scanner.nextInt(); set.add(value); } Integer[] arr = set.toArray(new Integer[set.size()]); for (int i = 1; i <= k; i++) { if (i == arr.length) { break; } System.out.println(arr[i] - arr[i - 1]); } if (k >= set.size() - 1) { StringBuilder str = new StringBuilder(); for (int i = 0; i < k - (set.size() - 1); i++) { str.append("0\n\r"); } System.out.println(str.toString()); } } static class FS { BufferedReader br; StringTokenizer st; public FS(InputStream i) { br = new BufferedReader(new InputStreamReader(i)); st = new StringTokenizer(""); } public String next() throws IOException { if(st.hasMoreTokens()) return st.nextToken(); else st = new StringTokenizer(br.readLine()); return next(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
66481679d518e518fd5b2fb4b379c601
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
//package CodeForces; import java.io.*; import java.util.*; public class Main { public class Haha { /* _____ ___ /\ /\ | | | | /\ /\ /\ /\ /\ /\ | \ محمد أبوحسن* / \ / \ | | |___| /__\ / \ / \ / \ / \ /__\ | \ / \ / \ | | | | / \ / \ / \ / \ / \ / \ | / / \/ \ |_____| | | / \ / \/ \ / \/ \ / \ |___/ */ } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static class MyPair { int x; int y; public MyPair(int x, int y) { this.x = x; this.y = y; } } public static int Fibonacci(int n) { int[] a = new int[n + 1]; a[0] = 0; a[1] = 1; for (int i = 2; i < a.length; i++) { a[i] = a[i - 1] + a[i - 2]; } return a[n]; } public static boolean isprime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return true; } } return false; } public static int[] addarr(int[] a, int n) { for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } return a; } public static int fact(int n){ int fact = 1; for(int i = 1; i <= n; i++) fact *= i; return fact; } public static long sumDigit(long n){ int a = 0; while(n != 0){ a += n%10; n /= 10; } return a; } public static MyScanner sc = new MyScanner(); public static int MOD = 1000000007; // __builtin_popcountll(n) <-- c++ // binary count(1) ex. 8 --> 0100 --> 1 HashSet<Integer> set = new HashSet<>(); HashMap<Integer, Integer> map = new HashMap<>(); Random r = new Random(); ArrayList<Integer> l = new ArrayList<>(); public static void main(String[] args) { int n = sc.nextInt(), k = sc.nextInt(), a = 1; ArrayList<Integer> l = new ArrayList<>(); for (int i = 0; i < n; i++) l.add(sc.nextInt()); Collections.sort(l); System.out.println(l.get(0)); while(--k>0){ if(a >= n){ System.out.println(0); continue; } boolean ok = true; if(!Objects.equals(l.get(a), l.get(a-1))){ System.out.println(l.get(a) - l.get(a-1)); ok = false; } if(ok) k++; a++; } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
ed2ca870d03820d068b2d3c0d7f56615
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.io.*; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class Today { public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int k = sc.nextInt(); TreeSet<Long> set1 = new TreeSet<>(); int max = 0; long sum = 0; for (int i = 0; i < n; i++) { long x = sc.nextInt(); max = Math.max((int) x, max); set1.add(x); } for (int i = 0; i < k; i++) { long min = -1; if (sum >= max) { pw.println(0); continue; } if (set1.contains(sum)) min = sum; if (set1.higher(sum) != null) min = set1.higher(sum); if (min == -1) { pw.println(0); continue; } pw.println(min - sum); sum += (min - sum); } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(4000); } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
6ffffbe1f40caf14f9986b973707f210
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.util.*; public class pl { public static long result(long num) { long ctr = 0; for(long i=1; i<=(long)Math.sqrt(num); i++) { if(num%i==0 && i*i!=num) { ctr+=2; } else if (i*i==num) { ctr++; } } return ctr; } static long ten(long n,long m) {int t=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { long sum=(long)(Math.pow(i, 2)+Math.pow(j, 2)); if(sum%m==0) t++; } } return t; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int n=in.nextInt(); int k=in.nextInt();int a[]=new int[n]; for(int i=0;i<n;i++) {a[i]=in.nextInt();} Arrays.sort(a); int cur = a[0]; System.out.println(cur); k--; for (int i = 1; i < n && k > 0; i++) { if (a[i] == cur) continue; System.out.println(a[i] - cur); cur = a[i]; k--; } for (int i = 0; i < k; i++) { System.out.println(0); } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
a96e3213691503263402493b19745179
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); ArrayList<Integer> arrayList = new ArrayList<>(); for (int i = 0; i < n; i++) { arrayList.add(scanner.nextInt()); } Collections.sort(arrayList); System.out.println(arrayList.get(0)); int j = 1; int i = 0; while (i < k-1 && j < n){ while (j < n && arrayList.get(j).equals(arrayList.get(j-1))){ j++; } if (j!=n) System.out.println(arrayList.get(j) - arrayList.get(j-1)); else i--; i++; j++; } while (i < k-1){ System.out.println(0); i++; } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
ad266052f4cccc1d5a8477fe6868953e
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.util.*; public class Sub { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i<n; i++) { arr[i] = sc.nextInt(); } Arrays.sort(arr); int sum = 0; int i = 0, j=0; while(j<k) { if(i>=n) { j++; System.out.println(0); continue; } int curr = arr[i]-sum; i++; if(curr==0) continue; System.out.println(curr); sum += curr; j++; } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
8ef5fd8c51b070a592bdde15f633feac
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { static final long mod=(int)1e9+7; public static void main(String[] args) throws Exception { FastReader in=new FastReader(); PrintWriter pw=new PrintWriter(System.out); int n=in.nextInt(); int k=in.nextInt(); Integer[] arr=new Integer[n]; for(int i=0;i<n;i++) arr[i]=in.nextInt(); Arrays.sort(arr); long sum=0; int j=0; for(int i=0;i<n && j<k;i++) { while(i<n && arr[i]-sum<=0) i++; if(i>=n) break; pw.println(arr[i]-sum); sum+=arr[i]-sum; j++; } for(;j<k;j++) pw.println(0); pw.flush(); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { if(st==null || !st.hasMoreElements()) { st=new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
3f65ed5cd0b45d8d308c703545522ec3
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.util.StringTokenizer; public class Main{ public static void main(String[] args) throws IOException{ boolean check = false; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine().trim()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); int[] arr = new int[n]; st = new StringTokenizer(br.readLine().trim()); for(int i=0; i<n; i++) { arr[i] = Integer.parseInt(st.nextToken()); } Arrays.sort(arr); int minIndex = 0; for(int i=0; i<n; i++) { if(arr[i] == 0) continue; else { minIndex = i; break; } } int sum = 0; for(int i=0; i<k; i++) { if(check) { System.out.println(0); continue; } if(arr[minIndex] - sum > 0) { System.out.println(arr[minIndex] - sum); sum = arr[minIndex]; } else if(arr[minIndex] - sum == 0) { while(arr[minIndex] - sum == 0) { minIndex++; if(minIndex==n) { check = true; break; } } if(check) { System.out.println(0); continue; } System.out.println(arr[minIndex] - sum); sum = arr[minIndex]; } } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
911fbbd7506f72cc87dd1e22daae69d9
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int k=s.nextInt(); ArrayList<Integer> p=new ArrayList<Integer>(); for(int i=0;i<n;i++) { p.add(s.nextInt()); } Collections.sort(p); int i=0; int sum=0; while(k-->0) { if(p.get(n-1)-sum==0) System.out.println("0"); else{ while(i<n&&p.get(i)-sum<=0) { i++; } System.out.println(p.get(i)-sum); sum=p.get(i);} i++; } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
842cd8797a4dd51eddacad9aad6bfc06
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.io.*; import java.util.*; public class Ehabandsubtraction { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); ArrayList<Integer> intlist = new ArrayList<Integer>(); ArrayList<Integer> flist = new ArrayList<Integer>(); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken());// set(int index, E element) st = new StringTokenizer(br.readLine()); for(int i=0; i<n; i++) { int num = Integer.parseInt(st.nextToken()); intlist.add(num); } Collections.sort(intlist); int crrnum, prenum = 0, s = 0; if(k > n) { for(int i=0; i<intlist.size(); i++) { crrnum = intlist.get(i); if(crrnum != prenum) { System.out.println(crrnum - prenum); } else s++; prenum = crrnum; } for(int i=0; i<k-(intlist.size()-s); i++) System.out.println("0"); } else { for(int i=0; i<intlist.size() && k>0; i++) { crrnum = intlist.get(i); if(crrnum != prenum) { System.out.println(crrnum - prenum); k--; } prenum = crrnum; } for(int i=0; i<k; i++) System.out.println("0"); } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
a64f8bf781b31f07c55f94865455c6ab
train_003.jsonl
1543934100
You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class Jetch { static Random random; private static void mySort(int[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); int t = s[i]; s[i] = s[j]; s[j] = t; } Arrays.sort(s); } public static void main(String[] args) { random = new Random(543534151132L + System.currentTimeMillis()); InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int nb=in.nextInt(); int k=in.nextInt(); int[] T=new int[nb]; for(int i=0;i<nb;i++)T[i]=in.nextInt(); mySort(T); int x=0; int j=0; for(int i=0;i<k;i++) { while(j<nb && T[j]-x<=0)j++; if(j<nb) { out.println(T[j]-x); x=T[j]; }else out.println(0); } out.close(); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } } }
Java
["3 5\n1 2 3", "4 2\n10 3 5 3"]
1 second
["1\n1\n1\n0\n0", "3\n2"]
NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2.
Java 8
standard input
[ "implementation", "sortings" ]
0f100199a720b0fdead5f03e1882f2f3
The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \le n,k \le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array.
1,000
Print the minimum non-zero element before each operation in a new line.
standard output
PASSED
fa145064f7774a03335d614d00b24005
train_003.jsonl
1405256400
DZY owns 2m islands near his home, numbered from 1 to 2m. He loves building bridges to connect the islands. Every bridge he builds takes one day's time to walk across.DZY has a strange rule of building the bridges. For every pair of islands u, v (u ≠ v), he has built 2k different bridges connecting them, where (a|b means b is divisible by a). These bridges are bidirectional.Also, DZY has built some bridges connecting his home with the islands. Specifically, there are ai different bridges from his home to the i-th island. These are one-way bridges, so after he leaves his home he will never come back.DZY decides to go to the islands for sightseeing. At first he is at home. He chooses and walks across one of the bridges connecting with his home, and arrives at some island. After that, he will spend t day(s) on the islands. Each day, he can choose to stay and rest, or to walk to another island across the bridge. It is allowed to stay at an island for more than one day. It's also allowed to cross one bridge more than once.Suppose that right after the t-th day DZY stands at the i-th island. Let ans[i] be the number of ways for DZY to reach the i-th island after t-th day. Your task is to calculate ans[i] for each i modulo 1051131.
512 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ 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(); } } class TaskE { private static final int MODULO = 1051131; private long days; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = 1 << in.nextInt(); days = in.nextLong(); int s = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < s; ++i) a[i] = in.nextInt() % MODULO; for (int i = s; i < n; ++i) a[i] = (101 * a[i - s] + 10007) % MODULO; a = doIt(a, 1, 0); int res = 0; for (int i = 0; i < n; ++i) { if (a[i] < 0) a[i] += MODULO; res ^= a[i]; } out.println(res); } private int[] doIt(int[] a, int p, int q) { int n = a.length; assert (n & (n - 1)) == 0; if (n == 1) { a[0] = (int) (a[0] * IntegerUtils.powerModulo(p + q, days, MODULO) % MODULO); return a; } n /= 2; int[] b = new int[n]; for (int i = 0; i < n; ++i) { b[i] = a[i] + a[i + n]; if (b[i] >= MODULO) b[i] -= MODULO; } b = Arrays.copyOf(doIt(b, 2 * p % MODULO, (int) (((n - 1L) * p + q) % MODULO)), 2 * n); System.arraycopy(b, 0, b, n, n); long multiplier = IntegerUtils.powerModulo(((1L - n) * p + q) % MODULO, days, MODULO); for (int i = 0; i < n; ++i) { b[i] = (int) ((b[i] + (a[i] - a[i + n]) * multiplier) % MODULO); b[i + n] = (int) ((b[i + n] - (a[i] - a[i + n]) * multiplier) % MODULO); } for (int i = 0; i < 2 * n; ++i) b[i] = (int) (b[i] * (MODULO + 1L) / 2 % MODULO); return b; } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { try { while(tokenizer == null || !tokenizer.hasMoreTokens()) 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()); } } class IntegerUtils { public static long powerModulo(long a, long n, long mod) { long res = 1; for(; n > 0; n >>= 1) { if((n & 1) != 0) res = res * a % mod; a = a * a % mod; } return res; } }
Java
["2 1 4\n1 1 1 2", "3 5 6\n389094 705719 547193 653800 947499 17024"]
5 seconds
["1", "556970"]
NoteIn the first sample, ans = [6, 7, 6, 6].If he wants to be at island 1 after one day, he has 6 different ways: home —&gt; 1 -(stay)-&gt; 1 home —&gt; 2 —&gt; 1 home —&gt; 3 —&gt; 1 home —&gt; 3 —&gt; 1 (note that there are two different bridges between 1 and 3) home —&gt; 4 —&gt; 1 home —&gt; 4 —&gt; 1 (note that there are two different bridges from home to 4) In the second sample, (a1, a2, a3, a4, a5, a6, a7, a8) = (389094, 705719, 547193, 653800, 947499, 17024, 416654, 861849), ans = [235771, 712729, 433182, 745954, 139255, 935785, 620229, 644335].
Java 8
standard input
[ "math", "matrices" ]
2a7bc7a9fd37fdc729770697959bff33
To avoid huge input, we use the following way to generate the array a. You are given the first s elements of array: a1, a2, ..., as. All the other elements should be calculated by formula: ai = (101·ai - s + 10007) mod 1051131 (s &lt; i ≤ 2m). The first line contains three integers m, t, s (1 ≤ m ≤ 25; 1 ≤ t ≤ 1018; 1 ≤ s ≤ min(2m, 105)). The second line contains s integers a1, a2, ..., as (1 ≤ ai ≤ 106).
3,100
To avoid huge output, you only need to output xor-sum of all the answers for all i modulo 1051131 (1 ≤ i ≤ 2m), i.e. (ans[1] mod 1051131) xor (ans[2] mod 1051131) xor... xor (ans[n] mod 1051131).
standard output
PASSED
9a29673423a45840dc0d4a4c4d65b085
train_003.jsonl
1405256400
DZY owns 2m islands near his home, numbered from 1 to 2m. He loves building bridges to connect the islands. Every bridge he builds takes one day's time to walk across.DZY has a strange rule of building the bridges. For every pair of islands u, v (u ≠ v), he has built 2k different bridges connecting them, where (a|b means b is divisible by a). These bridges are bidirectional.Also, DZY has built some bridges connecting his home with the islands. Specifically, there are ai different bridges from his home to the i-th island. These are one-way bridges, so after he leaves his home he will never come back.DZY decides to go to the islands for sightseeing. At first he is at home. He chooses and walks across one of the bridges connecting with his home, and arrives at some island. After that, he will spend t day(s) on the islands. Each day, he can choose to stay and rest, or to walk to another island across the bridge. It is allowed to stay at an island for more than one day. It's also allowed to cross one bridge more than once.Suppose that right after the t-th day DZY stands at the i-th island. Let ans[i] be the number of ways for DZY to reach the i-th island after t-th day. Your task is to calculate ans[i] for each i modulo 1051131.
512 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ 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(); } } class TaskE { private static final int MODULO = 1051131; private long days; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = 1 << in.nextInt(); days = in.nextLong(); int s = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < s; ++i) a[i] = in.nextInt() % MODULO; for (int i = s; i < n; ++i) a[i] = (101 * a[i - s] + 10007) % MODULO; a = doIt(a, 1, 0); int res = 0; for (int i = 0; i < n; ++i) { if (a[i] < 0) a[i] += MODULO; res ^= a[i]; } out.println(res); } private int[] doIt(int[] a, int p, int q) { int n = a.length; assert (n & (n - 1)) == 0; if (n == 1) { a[0] = (int) (a[0] * IntegerUtils.powerModulo(p + q, days, MODULO) % MODULO); return a; } n /= 2; int[] b = new int[n]; for (int i = 0; i < n; ++i) { b[i] = a[i] + a[i + n]; if (b[i] >= MODULO) b[i] -= MODULO; } b = Arrays.copyOf(doIt(b, 2 * p % MODULO, (int) (((n - 1L) * p + q) % MODULO)), 2 * n); System.arraycopy(b, 0, b, n, n); long multiplier = IntegerUtils.powerModulo(((1L - n) * p + q) % MODULO, days, MODULO); for (int i = 0; i < n; ++i) { b[i] = (int) ((b[i] + (a[i] - a[i + n]) * multiplier) % MODULO); b[i + n] = (int) ((b[i + n] - (a[i] - a[i + n]) * multiplier) % MODULO); } for (int i = 0; i < 2 * n; ++i) b[i] = (int) (b[i] * (MODULO + 1L) / 2 % MODULO); return b; } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { try { while(tokenizer == null || !tokenizer.hasMoreTokens()) 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()); } } class IntegerUtils { public static long powerModulo(long a, long n, long mod) { long res = 1; for(; n > 0; n >>= 1) { if((n & 1) != 0) res = res * a % mod; a = a * a % mod; } return res; } }
Java
["2 1 4\n1 1 1 2", "3 5 6\n389094 705719 547193 653800 947499 17024"]
5 seconds
["1", "556970"]
NoteIn the first sample, ans = [6, 7, 6, 6].If he wants to be at island 1 after one day, he has 6 different ways: home —&gt; 1 -(stay)-&gt; 1 home —&gt; 2 —&gt; 1 home —&gt; 3 —&gt; 1 home —&gt; 3 —&gt; 1 (note that there are two different bridges between 1 and 3) home —&gt; 4 —&gt; 1 home —&gt; 4 —&gt; 1 (note that there are two different bridges from home to 4) In the second sample, (a1, a2, a3, a4, a5, a6, a7, a8) = (389094, 705719, 547193, 653800, 947499, 17024, 416654, 861849), ans = [235771, 712729, 433182, 745954, 139255, 935785, 620229, 644335].
Java 6
standard input
[ "math", "matrices" ]
2a7bc7a9fd37fdc729770697959bff33
To avoid huge input, we use the following way to generate the array a. You are given the first s elements of array: a1, a2, ..., as. All the other elements should be calculated by formula: ai = (101·ai - s + 10007) mod 1051131 (s &lt; i ≤ 2m). The first line contains three integers m, t, s (1 ≤ m ≤ 25; 1 ≤ t ≤ 1018; 1 ≤ s ≤ min(2m, 105)). The second line contains s integers a1, a2, ..., as (1 ≤ ai ≤ 106).
3,100
To avoid huge output, you only need to output xor-sum of all the answers for all i modulo 1051131 (1 ≤ i ≤ 2m), i.e. (ans[1] mod 1051131) xor (ans[2] mod 1051131) xor... xor (ans[n] mod 1051131).
standard output
PASSED
3c74980cec461e906a911a2f6624c53f
train_003.jsonl
1448382900
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i &lt; n, it's guaranteed that |ai + 1 - ai| ≤ 1.A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.Find the length of the longest almost constant range.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.awt.Point; // SHIVAM GUPTA : //NSIT //decoder_1671 // STOP NOT TILL IT IS DONE OR U DIE . // U KNOW THAT IF THIS DAY WILL BE URS THEN NO ONE CAN DEFEAT U HERE................ // ASCII = 48 + i ;// 2^28 = 268,435,456 > 2* 10^8 // log 10 base 2 = 3.3219 // odd:: (x^2+1)/2 , (x^2-1)/2 ; x>=3// even:: (x^2/4)+1 ,(x^2/4)-1 x >=4 // FOR ANY ODD NO N : N,N-1,N-2 //ALL ARE PAIRWISE COPRIME //THEIR COMBINED LCM IS PRODUCT OF ALL THESE NOS // two consecutive odds are always coprime to each other // two consecutive even have always gcd = 2 ; public class Main { // static int[] arr = new int[100002] ; // static int[] dp = new int[100002] ; static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out); } String next(){ while(st==null || !st.hasMoreElements()){ try{ st= new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str=br.readLine(); } catch(IOException e){ e.printStackTrace(); } return str; } } //////////////////////////////////////////////////////////////////////////////////// public static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } ///////////////////////////////////////////////////////////////////////////////////////// public static int sumOfDigits(long n) { if( n< 0)return -1 ; int sum = 0; while( n > 0) { sum = sum + (int)( n %10) ; n /= 10 ; } return sum ; } ////////////////////////////////////////////////////////////////////////////////////////////////// public static long arraySum(int[] arr , int start , int end) { long ans = 0 ; for(int i = start ; i <= end ; i++)ans += arr[i] ; return ans ; } ///////////////////////////////////////////////////////////////////////////////// public static int mod(int x) { if(x <0)return -1*x ; else return x ; } public static long mod(long x) { if(x <0)return -1*x ; else return x ; } //////////////////////////////////////////////////////////////////////////////// public static void swapArray(int[] arr , int start , int end) { while(start < end) { int temp = arr[start] ; arr[start] = arr[end]; arr[end] = temp; start++ ;end-- ; } } ////////////////////////////////////////////////////////////////////////////////// public static int[][] rotate(int[][] input){ int n =input.length; int m = input[0].length ; int[][] output = new int [m][n]; for (int i=0; i<n; i++) for (int j=0;j<m; j++) output [j][n-1-i] = input[i][j]; return output; } /////////////////////////////////////////////////////////////////////////////////////////////////////// public static int countBits(long n) { int count = 0; while (n != 0) { count++; n = (n) >> (1L) ; } return count; } /////////////////////////////////////////// //////////////////////////////////////////////// public static boolean isPowerOfTwo(long n) { if(n==0) return false; if(((n ) & (n-1)) == 0 ) return true ; else return false ; } ///////////////////////////////////////////////////////////////////////////////////// public static int min(int a ,int b , int c, int d) { int[] arr = new int[4] ; arr[0] = a;arr[1] = b ;arr[2] = c;arr[3] = d;Arrays.sort(arr) ; return arr[0]; } ///////////////////////////////////////////////////////////////////////////// public static int max(int a ,int b , int c, int d) { int[] arr = new int[4] ; arr[0] = a;arr[1] = b ;arr[2] = c;arr[3] = d;Arrays.sort(arr) ; return arr[3]; } /////////////////////////////////////////////////////////////////////////////////// public static String reverse(String input) { StringBuilder str = new StringBuilder("") ; for(int i =input.length()-1 ; i >= 0 ; i-- ) { str.append(input.charAt(i)); } return str.toString() ; } /////////////////////////////////////////////////////////////////////////////////////////// public static boolean sameParity(long a ,long b ) { long x = a% 2L; long y = b%2L ; if(x==y)return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////////////////////// public static boolean isPossibleTriangle(int a ,int b , int c) { if( a + b > c && c+b > a && a +c > b)return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////////////// static long xnor(long num1, long num2) { if (num1 < num2) { long temp = num1; num1 = num2; num2 = temp; } num1 = togglebit(num1); return num1 ^ num2; } static long togglebit(long n) { if (n == 0) return 1; long i = n; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return i ^ n; } /////////////////////////////////////////////////////////////////////////////////////////////// public static int xorOfFirstN(int n) { if( n % 4 ==0)return n ; else if( n % 4 == 1)return 1 ; else if( n % 4 == 2)return n+1 ; else return 0 ; } ////////////////////////////////////////////////////////////////////////////////////////////// public static int gcd(int a, int b ) { if(b==0)return a ; else return gcd(b,a%b) ; } public static long gcd(long a, long b ) { if(b==0)return a ; else return gcd(b,a%b) ; } //////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c , int d ) { int temp = lcm(a,b , c) ; int ans = lcm(temp ,d ) ; return ans ; } /////////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c ) { int temp = lcm(a,b) ; int ans = lcm(temp ,c) ; return ans ; } //////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a , int b ) { int gc = gcd(a,b); return (a*b)/gc ; } public static long lcm(long a , long b ) { long gc = gcd(a,b); return (a*b)/gc ; } /////////////////////////////////////////////////////////////////////////////////////////// static boolean isPrime(long n) { if(n==1) { return false ; } boolean ans = true ; for(long i = 2L; i*i <= n ;i++) { if(n% i ==0) { ans = false ;break ; } } return ans ; } /////////////////////////////////////////////////////////////////////////// static int sieve = 1000000 ; static boolean[] prime = new boolean[sieve + 1] ; public static void sieveOfEratosthenes() { // FALSE == prime // TRUE == COMPOSITE // FALSE== 1 // time complexity = 0(NlogLogN)== o(N) // gives prime nos bw 1 to N for(int i = 4; i<= sieve ; i++) { prime[i] = true ; i++ ; } for(int p = 3; p*p <= sieve; p++) { if(prime[p] == false) { for(int i = p*p; i <= sieve; i += p) prime[i] = true; } p++ ; } } /////////////////////////////////////////////////////////////////////////////////// public static void sortD(int[] arr , int s , int e) { sort(arr ,s , e) ; int i =s ; int j = e ; while( i < j) { int temp = arr[i] ; arr[i] =arr[j] ; arr[j] = temp ; i++ ; j-- ; } return ; } ///////////////////////////////////////////////////////////////////////////////////////// public static long countSubarraysSumToK(long[] arr ,long sum ) { HashMap<Long,Long> map = new HashMap<>() ; int n = arr.length ; long prefixsum = 0 ; long count = 0L ; for(int i = 0; i < n ; i++) { prefixsum = prefixsum + arr[i] ; if(sum == prefixsum)count = count+1 ; if(map.containsKey(prefixsum -sum)) { count = count + map.get(prefixsum -sum) ; } if(map.containsKey(prefixsum )) { map.put(prefixsum , map.get(prefixsum) +1 ); } else{ map.put(prefixsum , 1L ); } } return count ; } /////////////////////////////////////////////////////////////////////////////////////////////// // KMP ALGORITHM : TIME COMPL:O(N+M) // FINDS THE OCCURENCES OF PATTERN AS A SUBSTRING IN STRING //RETURN THE ARRAYLIST OF INDEXES // IF SIZE OF LIST IS ZERO MEANS PATTERN IS NOT PRESENT IN STRING public static ArrayList<Integer> kmpAlgorithm(String str , String pat) { ArrayList<Integer> list =new ArrayList<>(); int n = str.length() ; int m = pat.length() ; String q = pat + "#" + str ; int[] lps =new int[n+m+1] ; longestPefixSuffix(lps, q,(n+m+1)) ; for(int i =m+1 ; i < (n+m+1) ; i++ ) { if(lps[i] == m) { list.add(i-2*m) ; } } return list ; } public static void longestPefixSuffix(int[] lps ,String str , int n) { lps[0] = 0 ; for(int i = 1 ; i<= n-1; i++) { int l = lps[i-1] ; while( l > 0 && str.charAt(i) != str.charAt(l)) { l = lps[l-1] ; } if(str.charAt(i) == str.charAt(l)) { l++ ; } lps[i] = l ; } } /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// // CALCULATE TOTIENT Fn FOR ALL VALUES FROM 1 TO n // TOTIENT(N) = count of nos less than n and grater than 1 whose gcd with n is 1 // or n and the no will be coprime in nature //time : O(n*(log(logn))) public static void eulerTotientFunction(int[] arr ,int n ) { for(int i = 1; i <= n ;i++)arr[i] =i ; for(int i= 2 ; i<= n ;i++) { if(arr[i] == i) { arr[i] =i-1 ; for(int j =2*i ; j<= n ; j+= i ) { arr[j] = (arr[j]*(i-1))/i ; } } } return ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long nCr(int n,int k) { long ans=1L; k=k>n-k?n-k:k; int j=1; for(;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } /////////////////////////////////////////////////////////////////////////////////////////// public static ArrayList<Integer> allFactors(int n) { ArrayList<Integer> list = new ArrayList<>() ; for(int i = 1; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } public static ArrayList<Long> allFactors(long n) { ArrayList<Long> list = new ArrayList<>() ; for(long i = 1L; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } //////////////////////////////////////////////////////////////////////////////////////////////////// static final int MAXN = 10000001; static int spf[] = new int[MAXN]; static void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) spf[i] = i; for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { if (spf[i] == i) { for (int j=i*i; j<MAXN; j+=i) if (spf[j]==j) spf[j] = i; } } } // The above code works well for n upto the order of 10^7. // Beyond this we will face memory issues. // Time Complexity: The precomputation for smallest prime factor is done in O(n log log n) // using sieve. // Where as in the calculation step we are dividing the number every time by // the smallest prime number till it becomes 1. // So, let’s consider a worst case in which every time the SPF is 2 . // Therefore will have log n division steps. // Hence, We can say that our Time Complexity will be O(log n) in worst case. static Vector<Integer> getFactorization(int x) { Vector<Integer> ret = new Vector<>(); while (x != 1) { ret.add(spf[x]); x = x / spf[x]; } return ret; } ////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() public static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } ///////////////////////////////////////////////////////////////////////////////////////// public static long knapsack(int[] weight,long value[],int maxWeight){ int n= value.length ; //dp[i] stores the profit with KnapSack capacity "i" long []dp = new long[maxWeight+1]; //initially profit with 0 to W KnapSack capacity is 0 Arrays.fill(dp, 0); // iterate through all items for(int i=0; i < n; i++) //traverse dp array from right to left for(int j = maxWeight; j >= weight[i]; j--) dp[j] = Math.max(dp[j] , value[i] + dp[j - weight[i]]); /*above line finds out maximum of dp[j](excluding ith element value) and val[i] + dp[j-wt[i]] (including ith element value and the profit with "KnapSack capacity - ith element weight") */ return dp[maxWeight]; } /////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// // to return max sum of any subarray in given array public static long kadanesAlgorithm(long[] arr) { long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long kadanesAlgorithm(int[] arr) { long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } /////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// public static long binarySerachGreater(int[] arr , int start , int end , int val) { // fing total no of elements strictly grater than val in sorted array arr if(start > end)return 0 ; //Base case int mid = (start + end)/2 ; if(arr[mid] <=val) { return binarySerachGreater(arr,mid+1, end ,val) ; } else{ return binarySerachGreater(arr,start , mid -1,val) + end-mid+1 ; } } ////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// //TO GENERATE ALL(DUPLICATE ALSO EXIST) PERMUTATIONS OF A STRING // JUST CALL generatePermutation( str, start, end) start :inclusive ,end : exclusive //Function for swapping the characters at position I with character at position j public static String swapString(String a, int i, int j) { char[] b =a.toCharArray(); char ch; ch = b[i]; b[i] = b[j]; b[j] = ch; return String.valueOf(b); } //Function for generating different permutations of the string public static void generatePermutation(String str, int start, int end) { //Prints the permutations if (start == end-1) System.out.println(str); else { for (int i = start; i < end; i++) { //Swapping the string by fixing a character str = swapString(str,start,i); //Recursively calling function generatePermutation() for rest of the characters generatePermutation(str,start+1,end); //Backtracking and swapping the characters again. str = swapString(str,start,i); } } } //////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// public static long factMod(long n, long mod) { if (n <= 1) return 1; long ans = 1; for (int i = 1; i <= n; i++) { ans = (ans * i) % mod; } return ans; } ///////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static long power(long x ,long n) { //time comp : o(logn) if(n==0)return 1L ; if(n==1)return x; long ans =1L ; while(n>0) { if(n % 2 ==1) { ans = ans *x ; } n /= 2 ; x = x*x ; } return ans ; } //////////////////////////////////////////////////////////////////////////////////////////////////// public static long powerMod(long x, long n, long mod) { //time comp : o(logn) if(n==0)return 1L ; if(n==1)return x; long ans = 1; while (n > 0) { if (n % 2 == 1) ans = (ans * x) % mod; x = (x * x) % mod; n /= 2; } return ans; } ////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// /* lowerBound - finds largest element equal or less than value paased upperBound - finds smallest element equal or more than value passed if not present return -1; */ public static long lowerBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static int lowerBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static long upperBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } public static int upperBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } ////////////////////////////////////////////////////////////////////////////////////////// public static void printArray(int[] arr , int si ,int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } } public static void printArrayln(int[] arr , int si ,int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } out.println() ; } public static void printLArray(long[] arr , int si , int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } } public static void printLArrayln(long[] arr , int si , int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } out.println() ; } public static void printtwodArray(int[][] ans) { for(int i = 0; i< ans.length ; i++) { for(int j = 0 ; j < ans[0].length ; j++)out.print(ans[i][j] +" "); out.println() ; } out.println() ; } static long modPow(long a, long x, long p) { //calculates a^x mod p in logarithmic time. long res = 1; while(x > 0) { if( x % 2 != 0) { res = (res * a) % p; } a = (a * a) % p; x /= 2; } return res; } static long modInverse(long a, long p) { //calculates the modular multiplicative of a mod m. //(assuming p is prime). return modPow(a, p-2, p); } static long modBinomial(long n, long k, long p) { // calculates C(n,k) mod p (assuming p is prime). long numerator = 1; // n * (n-1) * ... * (n-k+1) for (int i=0; i<k; i++) { numerator = (numerator * (n-i) ) % p; } long denominator = 1; // k! for (int i=1; i<=k; i++) { denominator = (denominator * i) % p; } // numerator / denominator mod p. return ( numerator* modInverse(denominator,p) ) % p; } ///////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// static ArrayList<Integer>[] tree ; // static long[] child; // static int mod= 1000000007 ; // static int[][] pre = new int[3001][3001]; // static int[][] suf = new int[3001][3001] ; //program to calculate noof nodes in subtree for every vertex including itself public static void countNoOfNodesInsubtree(int child ,int par , int[] dp) { int count = 1 ; for(int x : tree[child]) { if(x== par)continue ; countNoOfNodesInsubtree(x,child,dp) ; count= count + dp[x] ; } dp[child] = count ; } public static void depth(int child ,int par , int[] dp , int d ) { dp[child] =d ; for(int x : tree[child]) { if(x== par)continue ; depth(x,child,dp,d+1) ; } } ////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// public static void solve() { FastReader scn = new FastReader() ; //Scanner scn = new Scanner(System.in); //int[] store = {2 ,3, 5 , 7 ,11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 } ; // product of first 11 prime nos is greater than 10 ^ 12; //ArrayList<Integer> arr[] = new ArrayList[n] ; ArrayList<Integer> list = new ArrayList<>() ; ArrayList<Long> listl = new ArrayList<>() ; ArrayList<Integer> lista = new ArrayList<>() ; ArrayList<Integer> listb = new ArrayList<>() ; //ArrayList<String> lists = new ArrayList<>() ; //HashMap<Integer,Integer> map = new HashMap<>() ; HashMap<Long,Long> map = new HashMap<>() ; HashMap<Integer,Integer> map1 = new HashMap<>() ; HashMap<Integer,Integer> map2 = new HashMap<>() ; //HashMap<String,Integer> maps = new HashMap<>() ; //HashMap<Integer,Boolean> mapb = new HashMap<>() ; //HashMap<Point,Integer> point = new HashMap<>() ; Set<Integer> set = new HashSet<>() ; Set<Integer> setx = new HashSet<>() ; Set<Integer> sety = new HashSet<>() ; StringBuilder sb =new StringBuilder("") ; //Collections.sort(list); //if(map.containsKey(arr[i]))map.put(arr[i] , map.get(arr[i]) +1 ) ; //else map.put(arr[i],1) ; // if(map.containsKey(temp))map.put(temp , map.get(temp) +1 ) ; // else map.put(temp,1) ; //int bit =Integer.bitCount(n); // gives total no of set bits in n; // Arrays.sort(arr, new Comparator<Pair>() { // @Override // public int compare(Pair a, Pair b) { // if (a.first != b.first) { // return a.first - b.first; // for increasing order of first // } // return a.second - b.second ; //if first is same then sort on second basis // } // }); int testcase = 1; //testcase = scn.nextInt() ; for(int testcases =1 ; testcases <= testcase ;testcases++) { //if(map.containsKey(arr[i]))map.put(arr[i],map.get(arr[i])+1) ;else map.put(arr[i],1) ; //if(map.containsKey(temp))map.put(temp,map.get(temp)+1) ;else map.put(temp,1) ; // int n = scn.nextInt() ; // tree = new ArrayList[n] ; // for(int i = 0; i< n; i++) // { // tree[i] = new ArrayList<Integer>(); // } // for(int i = 0 ; i <= n-2 ; i++) // { // int fv = scn.nextInt()-1 ; int sv = scn.nextInt()-1 ; // tree[fv].add(sv) ; // tree[sv].add(fv) ; // } int n = scn.nextInt() ; int[] arr = new int[n] ; for(int i = 0 ; i < n ; i++)arr[i] = scn.nextInt() ; int[][] dp = new int[3][n] ; dp[0][0] = 1 ; dp[1][0] =1 ; dp[2][0] = 1; int ans = 1 ; for(int i = 1; i< n ;i++) { if(arr[i] == arr[i-1]) { dp[0][i] = dp[0][i-1]+1; dp[1][i] = dp[1][i-1]+1 ; dp[2][i] = dp[2][i-1]+1 ; } else if( arr[i] - arr[i-1] > 1 || arr[i] - arr[i-1] < -1 ) { dp[0][i] = 1; dp[1][i] = 1 ; dp[2][i] = 1 ; } else{ dp[0][i] = 1; if(arr[i] - arr[i-1] ==1) { dp[1][i] =dp[2][i-1] +1 ; dp[2][i] = 1; } else{ dp[1][i] = 1 ; dp[2][i] =dp[1][i-1] +1 ; } } int small = dp[0][i] ; if(small < dp[1][i])small = dp[1][i] ; if(small < dp[2][i])small = dp[2][i] ; ans = Math.max(ans , small) ; } //printtwodArray(dp) ; out.println(ans) ; sb.delete(0 , sb.length()) ; list.clear() ;listb.clear() ; map.clear() ; map1.clear() ; map2.clear() ; set.clear() ;sety.clear() ; } // test case end loop out.flush() ; } // solve fn ends public static void main (String[] args) throws java.lang.Exception { solve() ; } } class Pair { int first ; int second ; @Override public String toString() { String ans = "" ; ans += this.first ; ans += " "; ans += this.second ; return ans ; } }
Java
["5\n1 2 3 3 2", "11\n5 4 5 5 6 7 8 8 8 7 6"]
2 seconds
["4", "5"]
NoteIn the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
Java 11
standard input
[ "dp", "two pointers", "implementation" ]
b784cebc7e50cc831fde480171b9eb84
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
1,400
Print a single number — the maximum length of an almost constant range of the given sequence.
standard output
PASSED
9a679200ac2789d7af06930e67870268
train_003.jsonl
1448382900
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i &lt; n, it's guaranteed that |ai + 1 - ai| ≤ 1.A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.Find the length of the longest almost constant range.
256 megabytes
import java.util.Scanner; public class ApproximatingaConstantRange { static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int n = sc.nextInt(); //initial values int prev = -1; int current, next; int prevCount = 0, currentCount = 1; //longest constant range length int longest = 1; //first number current = sc.nextInt(); for (int i = 1; i < n; i++) { next = sc.nextInt(); if (next == current) { currentCount++; } else if (next == prev) { prevCount += currentCount; prev = current; current = next; currentCount = 1; } else { longest = Math.max(longest, currentCount + prevCount); prev = current; prevCount = currentCount; current = next; currentCount = 1; } } System.out.println(Math.max(longest, currentCount + prevCount)); } }
Java
["5\n1 2 3 3 2", "11\n5 4 5 5 6 7 8 8 8 7 6"]
2 seconds
["4", "5"]
NoteIn the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
Java 11
standard input
[ "dp", "two pointers", "implementation" ]
b784cebc7e50cc831fde480171b9eb84
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
1,400
Print a single number — the maximum length of an almost constant range of the given sequence.
standard output
PASSED
c62a0f8a260c7d3a63ea2fc0509a11f1
train_003.jsonl
1448382900
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i &lt; n, it's guaranteed that |ai + 1 - ai| ≤ 1.A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.Find the length of the longest almost constant range.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Solution { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int arr[] = new int[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextInt(); int max = arr[0], min = arr[0], ans = 0; HashMap<Integer, Integer> map = new HashMap(); map.put(arr[0], 1); for (int i = 1, j = 0; i < n; i++) { if (arr[i] > max) { max = arr[i]; if (max - min > 1) { while (j < i && map.containsKey(min)) { int x = map.get(arr[j]); if (x == 1) map.remove(arr[j]); else map.put(arr[j], x - 1); j++; } min = max - 1; } } else if (arr[i] < min) { min = arr[i]; if (max - min > 1) { while (j < i && map.containsKey(max)) { int x = map.get(arr[j]); if (x == 1) map.remove(arr[j]); else map.put(arr[j], x - 1); j++; } max = min + 1; } } map.put(arr[i], map.getOrDefault(arr[i], 0) + 1); ans = Math.max(ans, i - j + 1); // System.out.println(j + " " + i); } System.out.println(ans); out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["5\n1 2 3 3 2", "11\n5 4 5 5 6 7 8 8 8 7 6"]
2 seconds
["4", "5"]
NoteIn the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
Java 11
standard input
[ "dp", "two pointers", "implementation" ]
b784cebc7e50cc831fde480171b9eb84
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
1,400
Print a single number — the maximum length of an almost constant range of the given sequence.
standard output
PASSED
c5da394508abbdc157e65314b127d37a
train_003.jsonl
1448382900
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i &lt; n, it's guaranteed that |ai + 1 - ai| ≤ 1.A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.Find the length of the longest almost constant range.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class cf602b { public static void main(String[] args) throws IOException { int n = ri(), a[] = ria(n), i = 0, j = 0, last = 0, ans = 0; while(i < n) { while(j < n && a[j] == a[i]) { ++j; } if(j == n) { ans = max(ans, j - i); break; } else if (a[j] == a[i] + 1) { while(j < n && (a[j] == a[i] || a[j] == a[i] + 1)) { if(a[j] != a[j - 1]) { last = j; } ++j; } ans = max(ans, j - i); i = last; } else if (a[j] == a[i] - 1) { while(j < n && (a[j] == a[i] || a[j] == a[i] - 1)) { if(a[j] != a[j - 1]) { last = j; } ++j; } ans = max(ans, j - i); i = last; } else { ans = max(ans, j - i); i = j; } } prln(ans); close(); } static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random __rand = new Random(); // references // IBIG = 1e9 + 7 // IMAX ~= 2e10 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final int IMIN = -2147483648; static final long LMAX = 9223372036854775807L; static final long LMIN = -9223372036854775808L; // math util static int minof(int a, int b, int c) {return min(a, min(b, c));} static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static long minof(long a, long b, long c) {return min(a, min(b, c));} static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static int maxof(int a, int b, int c) {return max(a, max(b, c));} static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static long maxof(long a, long b, long c) {return max(a, max(b, c));} static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static int fli(double d) {return (int)d;} static int cei(double d) {return (int)ceil(d);} static long fll(double d) {return (long)d;} static long cel(double d) {return (long)ceil(d);} static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);} static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);} static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;} static long hash(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);} // array util static void reverse(int[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(long[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(char[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void rsort(int[] a) {shuffle(a); sort(a);} static void rsort(long[] a) {shuffle(a); sort(a);} static int[] copy(int[] a) {int[] ans = new int[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static long[] copy(long[] a) {long[] ans = new long[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static char[] copy(char[] a) {char[] ans = new char[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static int[] sorted(int[] a) {int[] ans = copy(a); sort(ans); return ans;} static long[] sorted(long[] a) {long[] ans = copy(a); sort(ans); return ans;} static int[] rsorted(int[] a) {int[] ans = copy(a); rsort(ans); return ans;} static long[] rsorted(long[] a) {long[] ans = copy(a); rsort(ans); return ans;} // graph util static List<List<Integer>> graph(int n) {List<List<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new ArrayList<>()); return g;} static List<List<Integer>> graph(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connect(g, rni() - 1, ni() - 1); return g;} static List<List<Integer>> graph(int n, int m) throws IOException {return graph(graph(n), m);} static List<List<Integer>> dgraph(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connecto(g, rni() - 1, ni() - 1); return g;} static List<List<Integer>> dgraph(List<List<Integer>> g, int n, int m) throws IOException {return dgraph(graph(n), m);} static List<Set<Integer>> sgraph(int n) {List<Set<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new HashSet<>()); return g;} static List<Set<Integer>> sgraph(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connect(g, rni() - 1, ni() - 1); return g;} static List<Set<Integer>> sgraph(int n, int m) throws IOException {return sgraph(sgraph(n), m);} static List<Set<Integer>> dsgraph(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connecto(g, rni() - 1, ni() - 1); return g;} static List<Set<Integer>> dsgraph(List<Set<Integer>> g, int n, int m) throws IOException {return dsgraph(sgraph(n), m);} static void connect(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v); g.get(v).add(u);} static void connecto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v);} static void dconnect(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v); g.get(v).remove(u);} static void dconnecto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v);} // input static void r() throws IOException {input = new StringTokenizer(__in.readLine());} static int ri() throws IOException {return Integer.parseInt(__in.readLine());} static long rl() throws IOException {return Long.parseLong(__in.readLine());} static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;} static int[] riam1(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()) - 1; return a;} static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;} static char[] rcha() throws IOException {return __in.readLine().toCharArray();} static String rline() throws IOException {return __in.readLine();} static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());} static int ni() {return Integer.parseInt(input.nextToken());} static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());} static long nl() {return Long.parseLong(input.nextToken());} // output static void pr(int i) {__out.print(i);} static void prln(int i) {__out.println(i);} static void pr(long l) {__out.print(l);} static void prln(long l) {__out.println(l);} static void pr(double d) {__out.print(d);} static void prln(double d) {__out.println(d);} static void pr(char c) {__out.print(c);} static void prln(char c) {__out.println(c);} static void pr(char[] s) {__out.print(new String(s));} static void prln(char[] s) {__out.println(new String(s));} static void pr(String s) {__out.print(s);} static void prln(String s) {__out.println(s);} static void pr(Object o) {__out.print(o);} static void prln(Object o) {__out.println(o);} static void prln() {__out.println();} static void pryes() {__out.println("yes");} static void pry() {__out.println("Yes");} static void prY() {__out.println("YES");} static void prno() {__out.println("no");} static void prn() {__out.println("No");} static void prN() {__out.println("NO");} static void pryesno(boolean b) {__out.println(b ? "yes" : "no");}; static void pryn(boolean b) {__out.println(b ? "Yes" : "No");} static void prYN(boolean b) {__out.println(b ? "YES" : "NO");} static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next()); else __out.println();} static void h() {__out.println("hlfd");} static void flush() {__out.flush();} static void close() {__out.close();} }
Java
["5\n1 2 3 3 2", "11\n5 4 5 5 6 7 8 8 8 7 6"]
2 seconds
["4", "5"]
NoteIn the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
Java 11
standard input
[ "dp", "two pointers", "implementation" ]
b784cebc7e50cc831fde480171b9eb84
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
1,400
Print a single number — the maximum length of an almost constant range of the given sequence.
standard output
PASSED
04a886c26ecbf1a1f8ab485f7c09a691
train_003.jsonl
1448382900
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i &lt; n, it's guaranteed that |ai + 1 - ai| ≤ 1.A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.Find the length of the longest almost constant range.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static int longestSubarray(int[] input) { int prev = -1; int current, next; int prevCount = 0, currentCount = 1; // longest constant range length int longest = 1; // first number current = input[0]; for (int i = 1; i < input.length; i++) { next = input[i]; // If we see same number if (next == current) { currentCount++; } // If we see different number, but // same as previous. else if (next == prev) { prevCount += currentCount; prev = current; current = next; currentCount = 1; } // If number is neither same as previous // nor as current. else { longest = Math.max(longest, currentCount + prevCount); prev = current; prevCount = currentCount; current = next; currentCount = 1; } } return Math.max(longest, currentCount + prevCount); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); System.out.println(longestSubarray(a)); } }
Java
["5\n1 2 3 3 2", "11\n5 4 5 5 6 7 8 8 8 7 6"]
2 seconds
["4", "5"]
NoteIn the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
Java 11
standard input
[ "dp", "two pointers", "implementation" ]
b784cebc7e50cc831fde480171b9eb84
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
1,400
Print a single number — the maximum length of an almost constant range of the given sequence.
standard output
PASSED
253d1e28cd26956e3f5490e6e9904765
train_003.jsonl
1448382900
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i &lt; n, it's guaranteed that |ai + 1 - ai| ≤ 1.A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.Find the length of the longest almost constant range.
256 megabytes
/* package codechef; // don't place package name! */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; public class HelloWorld{ public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); while(input.hasNext()){ int n=input.nextInt(); int[] l=new int[n]; int[][] dp=new int[n][2]; for(int i=0;i<n;i++){ l[i]=input.nextInt(); } dp[n-1][0]=1; dp[n-1][1]=1; int max=1; for(int i=dp.length-2;i>=0;i--){ if(l[i]==l[i+1]){ dp[i][0]=dp[i+1][0]+1; dp[i][1]=dp[i+1][1]+1; max=Math.max(max, Math.max(dp[i][0], dp[i][1])); continue; } dp[i][0]=1; if(i+1+dp[i+1][0]>n-1){ dp[i][1]=1+dp[i+1][1]; max=Math.max(max, Math.max(dp[i][0], dp[i][1])); continue; } if(l[i+1+dp[i+1][0]]==l[i]){ dp[i][1]=1+dp[i+1][1]; max=Math.max(max, Math.max(dp[i][0], dp[i][1])); continue; } dp[i][1]=1+dp[i+1][0]; max=Math.max(max, Math.max(dp[i][0], dp[i][1])); } System.out.println(max); } } }
Java
["5\n1 2 3 3 2", "11\n5 4 5 5 6 7 8 8 8 7 6"]
2 seconds
["4", "5"]
NoteIn the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
Java 11
standard input
[ "dp", "two pointers", "implementation" ]
b784cebc7e50cc831fde480171b9eb84
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
1,400
Print a single number — the maximum length of an almost constant range of the given sequence.
standard output
PASSED
a8b31e728c5f9b163300d840f834466c
train_003.jsonl
1448382900
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i &lt; n, it's guaranteed that |ai + 1 - ai| ≤ 1.A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.Find the length of the longest almost constant range.
256 megabytes
/** * @author : Kshitij */ import java.io.*; import java.util.InputMismatchException; public class Main { public static void main(String[] xps){ InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); StringBuilder stringBuilder = new StringBuilder(); int n = in.Int(); int[] a = new int[n]; for(int i = 0; i < n ; i++){ a[i] = in.Int(); } int min=0, max=0, l=0, r=0, ans=0; while (r<n){ if (a[r]>=a[max]) max=r; if (a[r]<=a[min]) min=r; if (a[max]-a[min]>1){ if (max == r){ l = min+1; min = r-1; }else{ l = max+1; max = r-1; } } r++; ans=Math.max(ans, r-l); } out.println(ans); } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int Int() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { 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 double Double() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, Int()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, Int()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public long Long() { 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 boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } private 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]); } writer.flush(); } public void println(Object... objects) { print(objects); writer.println(); writer.flush(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } }
Java
["5\n1 2 3 3 2", "11\n5 4 5 5 6 7 8 8 8 7 6"]
2 seconds
["4", "5"]
NoteIn the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
Java 11
standard input
[ "dp", "two pointers", "implementation" ]
b784cebc7e50cc831fde480171b9eb84
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
1,400
Print a single number — the maximum length of an almost constant range of the given sequence.
standard output
PASSED
d587e4656807946c235af2164a0d3c0d
train_003.jsonl
1448382900
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i &lt; n, it's guaranteed that |ai + 1 - ai| ≤ 1.A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.Find the length of the longest almost constant range.
256 megabytes
import java.util.StringTokenizer; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main{ public static void main(String[]args) { FastReader in = new FastReader(); PrintWriter b=new PrintWriter(new BufferedOutputStream(System.out)); int n=in.nextInt(); int prev=-1,current,next; int prevCount=0,currentCount=1; int longest=1; current=in.nextInt(); for(int i=1;i<n;i++) { next=in.nextInt(); if(current==next) currentCount++; else if(next==prev) { prevCount += currentCount; prev = current; current = next; currentCount = 1; } else { longest = Math.max(longest, currentCount + prevCount); prev = current; prevCount = currentCount; current = next; currentCount = 1; } } b.println(Math.max(longest, currentCount + prevCount)); b.close(); }} 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()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["5\n1 2 3 3 2", "11\n5 4 5 5 6 7 8 8 8 7 6"]
2 seconds
["4", "5"]
NoteIn the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
Java 11
standard input
[ "dp", "two pointers", "implementation" ]
b784cebc7e50cc831fde480171b9eb84
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
1,400
Print a single number — the maximum length of an almost constant range of the given sequence.
standard output
PASSED
b982ded0343c4c829bdaecdc524dcdec
train_003.jsonl
1448382900
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i &lt; n, it's guaranteed that |ai + 1 - ai| ≤ 1.A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.Find the length of the longest almost constant range.
256 megabytes
import java.util.*; import java.math.*; import java.io.*; public class A{ final int N = 10000; static int cnt[]=new int[27]; static int n; public static void main(String[] args) throws IOException { InputReader scan=new InputReader(System.in); PrintWriter out=new PrintWriter(System.out); int n=scan.nextInt(); int a[]=new int[n]; int cur=scan.nextInt(),next; int prev=-1; int prevcount=0; int curcount=1; int longest=1; for(int i=1;i<n;i++){ next=scan.nextInt(); if(next==cur) curcount++; else if(next==prev){ prev=cur; prevcount+=curcount; cur=next; curcount=1; } else{ longest=Math.max(longest,curcount+prevcount); prevcount=curcount; prev=cur; cur=next; curcount=1; } } out.println(Math.max(longest,curcount+prevcount)); out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public Double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public int[] nextIntArray(int arraySize) { int array[] = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextInt(); } return array; } } static class pair{ int i; int j; pair(int i,int j){ this.i=i; this.j=j; } } }
Java
["5\n1 2 3 3 2", "11\n5 4 5 5 6 7 8 8 8 7 6"]
2 seconds
["4", "5"]
NoteIn the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
Java 11
standard input
[ "dp", "two pointers", "implementation" ]
b784cebc7e50cc831fde480171b9eb84
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
1,400
Print a single number — the maximum length of an almost constant range of the given sequence.
standard output
PASSED
313bd7e00844384914ecd647a11e02fb
train_003.jsonl
1448382900
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i &lt; n, it's guaranteed that |ai + 1 - ai| ≤ 1.A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.Find the length of the longest almost constant range.
256 megabytes
// package endsem; import java.util.*; import java.lang.Math; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Cf { public static void main(String[] args) throws IOException{ //Scanner sc=new Scanner(System.in); // int n=sc.nextInt(); Reader.init(System.in); int n=Reader.nextInt(); int[] a=new int[n]; for (int i = 0; i < n; i++) { a[i]=Reader.nextInt(); } int maxx=a[0]; int maxx_ind=0; int minn=a[0]; int minn_ind=0; int lenn=0; int ans=0; // System.out.println("dfsdkj"); for (int i = 0; i < a.length; i++) { if(a[i]>=maxx) { maxx=a[i]; maxx_ind=i; } if(a[i]<=minn) { minn=a[i]; minn_ind=i; } if(maxx-minn>1) { if(lenn>ans)ans=lenn; // System.out.println(ans+"skdhkjs "+a[i]+"ffdfg "+lenn); if(a[i]==maxx) { i=minn_ind+1; // System.out.println(a[i]+"ho"); maxx=a[i]; minn=a[i]; maxx_ind=i; minn_ind=i; //lenn=lenn+1; } else { i=maxx_ind+1; maxx=a[i]; minn=a[i]; maxx_ind=i; minn_ind=i; //lenn=lenn+1; } lenn=1; } else { lenn=lenn+1; } if(maxx-minn<=1 && i==n-1) { if(lenn>ans)ans=lenn; } } System.out.println(ans); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } }
Java
["5\n1 2 3 3 2", "11\n5 4 5 5 6 7 8 8 8 7 6"]
2 seconds
["4", "5"]
NoteIn the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
Java 11
standard input
[ "dp", "two pointers", "implementation" ]
b784cebc7e50cc831fde480171b9eb84
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
1,400
Print a single number — the maximum length of an almost constant range of the given sequence.
standard output
PASSED
a06d95e2a1b63dc8ad6f884336e876c9
train_003.jsonl
1448382900
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i &lt; n, it's guaranteed that |ai + 1 - ai| ≤ 1.A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.Find the length of the longest almost constant range.
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 Ideone { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner in=new Scanner(System.in); int n=in.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++){ a[i]=in.nextInt(); } HashMap<Integer,Integer> map=new HashMap<>(); map.put(a[0],0); int len=1; int l=0; int r=1; int max=1; while(l<n && r<n){ if(map.containsKey(a[r]-2) ){ int u=map.get(a[r]-2); l=u+1; map.remove(a[r]-2); } if(map.containsKey(a[r]+2)){ int u=map.get(a[r]+2); l=u+1; map.remove(a[r]+2); } if(map.containsKey(a[r])|| map.containsKey(a[r]+1)||map.containsKey(a[r]-1)){ } else{ l=r; map=new HashMap<>(); } map.put(a[r],r); len=(r-l)+1; if(len>max){ max=len; } r++; } System.out.println(max); } }
Java
["5\n1 2 3 3 2", "11\n5 4 5 5 6 7 8 8 8 7 6"]
2 seconds
["4", "5"]
NoteIn the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
Java 11
standard input
[ "dp", "two pointers", "implementation" ]
b784cebc7e50cc831fde480171b9eb84
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
1,400
Print a single number — the maximum length of an almost constant range of the given sequence.
standard output
PASSED
9f6994850a658dfba099a9a65ba503b2
train_003.jsonl
1533737100
After the war, the supersonic rocket became the most common public transportation.Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has $$$n$$$ power sources, and the second one has $$$m$$$ power sources. A power source can be described as a point $$$(x_i, y_i)$$$ on a 2-D plane. All points in each engine are different.You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i+a, y_i+b)$$$, $$$a$$$ and $$$b$$$ can be any real numbers. In other words, all power sources will be shifted. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i \cos \theta - y_i \sin \theta, x_i \sin \theta + y_i \cos \theta)$$$, $$$\theta$$$ can be any real number. In other words, all power sources will be rotated.The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources $$$A(x_a, y_a)$$$ and $$$B(x_b, y_b)$$$ exist, then for all real number $$$k$$$ that $$$0 \lt k \lt 1$$$, a new power source will be created $$$C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b)$$$. Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred).A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well.Given a supersonic rocket, check whether it is safe or not.
256 megabytes
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; public class E { static int A,B; static ArrayList<Vector> hull1; static ArrayList<Vector> hull2; static long dist1[]; static long dist2[]; static int N; public static void main(String[] args) { JS in = new JS(); A = in.nextInt(); B = in.nextInt(); ArrayList<Vector> p1s = new ArrayList<Vector>(); ArrayList<Vector> p2s = new ArrayList<Vector>(); for(int i = 0; i < A; i++) p1s.add(new Vector(in.nextLong(), in.nextLong())); for(int i = 0; i < B; i++) p2s.add(new Vector(in.nextLong(), in.nextLong())); hull1 = getHull(p1s); hull2 = getHull(p2s); if(hull1.size() != hull2.size()) { System.out.println("NO"); return; } N = hull1.size(); dist1 = new long[N]; dist2 = new long[N]; for(int i = 0; i < N; i++) { Vector p1 = hull1.get(i); Vector p2 = hull1.get((i+1)%N); dist1[i] = (p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y); } for(int i = 0; i < N; i++) { Vector p1 = hull2.get(i); Vector p2 = hull2.get((i+1)%N); dist2[i] = (p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y); } long dist2Double[] = new long[2*N]; for(int i = 0; i < N; i++) { dist2Double[i] = dist2[i]; dist2Double[N+i] = dist2[i]; } //Run KMP on the distances ArrayList<Integer> distLocs = search(dist2Double, dist1, KMP_Construct(dist1)); //Now run KMP on the angles and if there are matches on both, then the answer is yes long ang1[] = new long[N]; long ang2[] = new long[N]; for(int i = 0; i < N; i++) { Vector p2 = hull1.get(i); Vector p1 = hull1.get((i+N-1)%N); Vector p3 = hull1.get((i+1)%N); Vector v1 = new Vector(p3.x-p2.x, p3.y-p2.y); Vector v2 = new Vector(p1.x-p2.x, p1.y-p2.y); ang1[i] = v1.crs(v2); } for(int i = 0; i < N; i++) { Vector p2 = hull2.get(i); Vector p1 = hull2.get((i+N-1)%N); Vector p3 = hull2.get((i+1)%N); Vector v1 = new Vector(p3.x-p2.x, p3.y-p2.y); Vector v2 = new Vector(p1.x-p2.x, p1.y-p2.y); ang2[i] = v1.crs(v2); } long ang2Double[] = new long[2*N]; for(int i = 0; i < N; i++) { ang2Double[i] = ang2[i]; ang2Double[N+i] = ang2[i]; } ArrayList<Integer> angLocs = search(ang2Double, ang1, KMP_Construct(ang1)); if(distLocs.size() > 0 && angLocs.size() > 0) { System.out.println("YES"); } else { System.out.println("NO"); } } static boolean eq(double a, double b) { return Math.abs(a-b) < 1e-6; } // Given a pattern string w, return the prefix array. //% // p[i] indicates the length of the longest prefix //% // that is also a substring ending at i. O(|w|) //% static int[] KMP_Construct(long[] w) { int n = w.length + 1, k = 0; int[] p = new int[n]; p[1] = 0; for(int i = 2; i < n; ++i) { while(k > 0 && w[k] != w[i-1]) k = p[k]; if(w[k] == w[i-1]) k++; p[i] = k; } return p; } // Given the prefix array of a pattern, search the //% // text for all occurrences of the pattern. O(|s|) //% static ArrayList<Integer> search(long[] s, long[] w, int[] p) { int k = 0; ArrayList<Integer> locs = new ArrayList<>(); for(int i = 0; i < s.length; ++i) { while(k > 0 && w[k] != s[i]) k = p[k]; if(w[k] == s[i]) k++; if(k == w.length) { locs.add(i-w.length+1); k = p[k]; } } return locs; } // Returns the convex hull in COUNTER-CLOCKWISE order. Vector: Compare X then Y. public static ArrayList<Vector> getHull(ArrayList<Vector> ps) { ArrayList<Vector> s = (ArrayList<Vector>) ps.clone(); Collections.sort(s); int n = s.size(), j = 2, k = 2; Vector[] lo = new Vector[n], up = new Vector[n]; lo[0] = s.get(0); lo[1] = s.get(1); for (int i = 2; i < n; i++) { Vector p = s.get(i); while (j > 1 && !RT(lo[j - 2], lo[j - 1], p)) j--; lo[j++] = p; } up[0] = s.get(n - 1); up[1] = s.get(n - 2); for (int i = n - 3; i >= 0; i--) { // note difference Vector p = s.get(i); while (k > 1 && !RT(up[k - 2], up[k - 1], p)) k--; up[k++] = p; } ArrayList<Vector> res = new ArrayList<Vector>(); for (int i = 0; i < k; i++) res.add(up[i]); for (int i = 1; i < j - 1; i++) res.add(lo[i]); // Only needed if want the two halves of the polygon Vector[] hullUp = new Vector[k]; for(int i = 0; i < k; i++) hullUp[i] = up[k - i - 1]; Vector[] hullLo = new Vector[j]; for(int i = 0; i < j; i++) hullLo[i] = lo[i]; return res; } static boolean RT(Vector a, Vector b, Vector c) { return b.sub(a).cross(c.sub(a)).z > 0; } static class Vector implements Comparable<Vector> { public long x, y, z; //% public long x0 = 0, y0 = 0, z0 = 0; //% public static double epsilon = 1e-6; //% public static Vector ORIGIN = new Vector(0, 0, 0); //% public static int curId = 0; //% public int id = Vector.curId++; //% //# // Constructor for normal vector public Vector(long xx, long yy, long zz) { x = xx; y = yy; z = zz; } // Constructor for 2d normal vector public Vector(long xx, long yy) { x = xx; y = yy; z = 0; } // Constructor for ray/line vector public Vector(Vector a, Vector b) { x = b.x - (x0 = a.x); y = b.y - (y0 = a.y); z = b.z - (z0 = a.z); } //$ //# public static boolean equals(double a, double b) { return Math.abs(a - b) < epsilon; } public Vector add(Vector v) { return new Vector(x + v.x, y + v.y, z + v.z); } public Vector sub(Vector v) { return new Vector(x - v.x, y - v.y, z - v.z); } public double mag() { return Math.sqrt((x*x) + (y*y) + (z*z)); } public double mag2() { return dot(this); } public double dot(Vector v) { return (x * v.x) + (y * v.y) + (z * v.z); } public long crs(Vector v) { return this.cross(v).z; } public Vector cross(Vector v) { return new Vector(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x); } // Returns this vector rotated 90 degrees clockwise //% public Vector orthoCW() { return new Vector(y, -x, z); } // Returns this vector rotated 90 degrees counter-clockwise //% public Vector orthoCCW() { return new Vector(-y, x, z); } //# public boolean pointOnLine(Vector p) { if(equals(x, 0.0)) return equals(p.x, x0); return equals(p.y - y0, (y/x) * (p.x - x0)); } //# public int compareTo(Vector p) { if (y == p.y) return Double.compare(x, p.x); return Double.compare(y, p.y); } //$ //# // Compares a and b with whichever comes first in a positive circular sweep // Works best for longs public static int compare(Vector a, Vector b) { int res = 0; if (a.x < 0) res += a.y >= 0 ? 1 : 2; else if (a.y < 0) res += 3; if (b.x < 0) res -= b.y >= 0 ? 1 : 2; else if (b.y < 0) res -= 3; if(res == 0) { Vector cross = a.cross(b); if(cross.z == 0) return 0; if(cross.z > 0) return -1; else return 1; } return res; } //$ //# public String toString() { if(equals(x0, 0.0) && equals(y0, 0.0) && equals(z0, 0.0)) return String.format("<%.2f, %.2f, %.2f>", x, y, z); return String.format("<%.2f + %.2ft, %.2f + %.2ft, %.2f + %.2ft>", x0, x, y0, y, z0, z); } //$ } static class JS{ public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { while(c!='.'&&c!='-'&&(c <'0' || c>'9')) c = nextChar(); boolean neg = c=='-'; if(neg)c=nextChar(); boolean fl = c=='.'; double cur = nextLong(); if(fl) return neg ? -cur/num : cur/num; if(c == '.') { double next = nextLong(); return neg ? -cur-next/num : cur+next/num; } else return neg ? -cur : cur; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c!='\n') { res.append(c); c=nextChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=nextChar(); if(c==NC)return false; else if(c>32)return true; } } } }
Java
["3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n1 1", "3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n0 0"]
1 second
["YES", "NO"]
NoteThe first sample: Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with $$$\theta = \pi$$$ (to rotate all power sources $$$180$$$ degrees).The power sources in the first engine become $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-2, 0)$$$. Second, manipulate the second engine: use the first operation with $$$a = b = -2$$$.The power sources in the second engine become $$$(-2, 0)$$$, $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-1, -1)$$$. You can examine that destroying any point, the power field formed by the two engines are always the solid triangle $$$(0, 0)$$$, $$$(-2, 0)$$$, $$$(0, -2)$$$.In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it.
Java 8
standard input
[ "hashing", "geometry", "strings" ]
aeda11f32911d256fb6263635b738e99
The first line contains two integers $$$n$$$, $$$m$$$ ($$$3 \le n, m \le 10^5$$$) — the number of power sources in each engine. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the first engine. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine.
2,400
Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower).
standard output
PASSED
4b0b1f0d608d43163e756c7ba77aa5fc
train_003.jsonl
1533737100
After the war, the supersonic rocket became the most common public transportation.Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has $$$n$$$ power sources, and the second one has $$$m$$$ power sources. A power source can be described as a point $$$(x_i, y_i)$$$ on a 2-D plane. All points in each engine are different.You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i+a, y_i+b)$$$, $$$a$$$ and $$$b$$$ can be any real numbers. In other words, all power sources will be shifted. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i \cos \theta - y_i \sin \theta, x_i \sin \theta + y_i \cos \theta)$$$, $$$\theta$$$ can be any real number. In other words, all power sources will be rotated.The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources $$$A(x_a, y_a)$$$ and $$$B(x_b, y_b)$$$ exist, then for all real number $$$k$$$ that $$$0 \lt k \lt 1$$$, a new power source will be created $$$C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b)$$$. Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred).A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well.Given a supersonic rocket, check whether it is safe or not.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class CF1017_E { public static void main(String[] args)throws Throwable { MyScanner sc=new MyScanner(); PrintWriter pw=new PrintWriter(System.out); int n=sc.nextInt(); int m=sc.nextInt(); Point [] a=new Point [n]; for(int i=0;i<n;i++) a[i]=new Point(sc.nextLong(),sc.nextLong()); Polygon p1=convexHull(a); int len=p1.g.length-1; Point [] b=new Point [m]; for(int i=0;i<m;i++) b[i]=new Point(sc.nextLong(),sc.nextLong()); Polygon p2=convexHull(b); if(p1.g.length!=p2.g.length) { pw.println("NO"); } else{ Point [] p22=new Point [len*2+1]; for(int i=0;i<p2.g.length;i++){ if(i<p2.g.length-1) p22[i]=p2.g[i]; p22[len+i]=p2.g[i]; } Point [] p11=p1.g; int [] f=new int [len]; f[0]=0; int j=0; for(int i=1;i<len;i++){ f[i]=j; while(j>0 && (j==len || !same(p11[j-1],p11[j],p11[j+1],p11[i-1],p11[i],p11[i+1]))) j=f[j-1]; if(same(p11[(j>0)? j-1 : len-1],p11[j],p11[j+1],p11[i-1],p11[i],p11[i+1])) j++; f[i]=j; } // for(Point p : p11) // System.err.println(p.x+" "+p.y); // System.err.println(); // // for(Point p : p22) // System.err.println(p.x+" "+p.y); int mx=0; j=0; for(int i=0;i<p22.length-1;i++){ while(j>0 && (j==len || !same(p11[j-1],p11[j],p11[j+1],p22[(i>0)? i-1 : p22.length-2],p22[i],p22[i+1]))) j=f[j-1]; if(same(p11[(j>0)? j-1 : len-1],p11[j],p11[j+1],p22[(i>0)? i-1 : p22.length-2],p22[i],p22[i+1])) j++; mx=Math.max(mx,j); } if(mx==len) pw.println("YES"); else pw.println("NO"); } pw.flush(); pw.close(); } static boolean same(Point x1,Point x2,Point x3,Point y1,Point y2,Point y3){ if(x2.dist(x3)!=y2.dist(y3)) return false; if(Math.abs(angle(x1,x2,x3)-angle(y1,y2,y3))>EPS) return false; return true; } static final double EPS = 1e-9; static double angle(Point a, Point o, Point b) // angle AOB { Vector oa = new Vector(o, a), ob = new Vector(o, b); return Math.acos(1.0*oa.dot(ob) / Math.sqrt(1.0*oa.norm2() * ob.norm2())); } static class Point implements Comparable<Point>{ long x, y; Point(long a, long b) { x = a; y = b; } public long dist(Point p) { return sq(x - p.x) + sq(y - p.y); } static long sq(long x) { return x * x; } static boolean ccw(Point p, Point q, Point r) { return new Vector(p, q).cross(new Vector(p, r)) > 0; } public int compareTo(Point p) { if(x !=p.x) return x > p.x ? 1 : -1; if(y!=p.y) return y > p.y ? 1 : -1; return 0; } } static class Polygon { // Cases to handle: collinear points, polygons with n < 3 static final double EPS = 1e-9; Point[] g; //first point = last point, counter-clockwise representation Polygon(Point[] o) { g = o; } } static class Vector { long x, y; Vector(long a, long b) { x = a; y = b; } Vector(Point a, Point b) { this(b.x - a.x, b.y - a.y); } long dot(Vector v) { return (x * v.x + y * v.y); } long cross(Vector v) { return x * v.y - y * v.x; } double norm2() { return x * x + y * y; } } static Polygon convexHull(Point[] points) //all points are unique, remove duplicates, edit ccw to accept collinear points { int n = points.length; Arrays.sort(points); Point[] ans = new Point[n<<1]; int size = 0, start = 0; for(int i = 0; i < n; i++) { Point p = points[i]; while(size - start >= 2 && !Point.ccw(ans[size-2], ans[size-1], p)) --size; ans[size++] = p; } start = --size; for(int i = n-1 ; i >= 0 ; i--) { Point p = points[i]; while(size - start >= 2 && !Point.ccw(ans[size-2], ans[size-1], p)) --size; ans[size++] = p; } // if(size < 0) size = 0 for empty set of points return new Polygon(Arrays.copyOf(ans, size)); } 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
["3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n1 1", "3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n0 0"]
1 second
["YES", "NO"]
NoteThe first sample: Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with $$$\theta = \pi$$$ (to rotate all power sources $$$180$$$ degrees).The power sources in the first engine become $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-2, 0)$$$. Second, manipulate the second engine: use the first operation with $$$a = b = -2$$$.The power sources in the second engine become $$$(-2, 0)$$$, $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-1, -1)$$$. You can examine that destroying any point, the power field formed by the two engines are always the solid triangle $$$(0, 0)$$$, $$$(-2, 0)$$$, $$$(0, -2)$$$.In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it.
Java 8
standard input
[ "hashing", "geometry", "strings" ]
aeda11f32911d256fb6263635b738e99
The first line contains two integers $$$n$$$, $$$m$$$ ($$$3 \le n, m \le 10^5$$$) — the number of power sources in each engine. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the first engine. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine.
2,400
Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower).
standard output
PASSED
9287670a674423ba485e33aa342ad6f0
train_003.jsonl
1533737100
After the war, the supersonic rocket became the most common public transportation.Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has $$$n$$$ power sources, and the second one has $$$m$$$ power sources. A power source can be described as a point $$$(x_i, y_i)$$$ on a 2-D plane. All points in each engine are different.You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i+a, y_i+b)$$$, $$$a$$$ and $$$b$$$ can be any real numbers. In other words, all power sources will be shifted. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i \cos \theta - y_i \sin \theta, x_i \sin \theta + y_i \cos \theta)$$$, $$$\theta$$$ can be any real number. In other words, all power sources will be rotated.The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources $$$A(x_a, y_a)$$$ and $$$B(x_b, y_b)$$$ exist, then for all real number $$$k$$$ that $$$0 \lt k \lt 1$$$, a new power source will be created $$$C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b)$$$. Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred).A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well.Given a supersonic rocket, check whether it is safe or not.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class Main { public static boolean kmp(double[] s, double[] w){ int[] t = buildTable(w); return search(s, w, t); } public static boolean doubleEquals(double a, double b){ return Math.abs(a - b) < 1e-9; } public static int[] buildTable(double[] w){ int[] t = new int[w.length]; t[0] = -1; t[1] = 0; int pos = 2; int end = 0; while(pos < t.length){ if(doubleEquals(w[pos-1], w[end])){ t[pos] = end + 1; pos++; end++; } else if(end > 0){ end = t[end]; } else{ t[pos] = 0; pos++; } } return t; } static int N; public static boolean search(double[] s,double[] w,int[] t){ int i = 0; int j = 0; int maxJ = 0; while(i < s.length){ if(j == w.length){ return true; } if(doubleEquals(s[i], w[j])){ i++; j++; maxJ = Math.max(maxJ, j); if(i == s.length && j == w.length){ return true; } } else{ if(j == 0){ i++; } else{ j = t[j]; } } } return false; } public static class Point implements Comparable<Point>{ double x; double y; double t; double t2; public Point(double x, double y){ this.x = x; this.y = y; } @Override public int compareTo(Point p){ return this.t == p.t ? Double.compare(this.t2, p.t2) : Double.compare(this.t, p.t); } } public static double calcAngle(Point p1, Point p2){ if(p1.x == p2.x){ return 90; } double tan = (p1.y - p2.y) / (p1.x - p2.x); double angle = Math.toDegrees((Math.atan(Math.abs(tan)))); return tan < 0 ? 180 - angle : angle; } public static boolean convex(Point p1, Point p2, Point p3){ double det = ((p3.x - p2.x) * (p1.y - p2.y)) - ((p1.x - p2.x) * (p3.y - p2.y)); return det > 0; } public static ArrayList<Point> convexHull(Point[] points, int n){ int min = 0; for(int i = 0; i < n; i++){ if(points[i].y < points[min].y){ min = i; } else if(points[i].y == points[min].y && points[i].x < points[min].x){ min = i; } } for(int i = 0; i < n; i++){ points[i].t = calcAngle(points[i], points[min]); points[i].t2 = getDist(points[i], points[min]); } points[min].t = -1; ArrayList<Point> sorted = new ArrayList<Point>(); for(int i = 0; i < n; i++){ sorted.add(points[i]); } Collections.sort(sorted); ArrayList<Point> convexHull = new ArrayList<Point>(); sorted.add(sorted.get(0)); convexHull.add(sorted.get(0)); convexHull.add(sorted.get(1)); int i = 2; while(i < n + 1){ int last = convexHull.size() - 1; if(convex(convexHull.get(last - 1), convexHull.get(last), sorted.get(i))){ convexHull.add(sorted.get(i)); i++; } else{ convexHull.remove(last); if(convexHull.size() == 1){ convexHull.add(sorted.get(i)); i++; } } } convexHull.remove(convexHull.size() - 1); return convexHull; } public static double getAngle(Point p1, Point p2, Point p3){ double x21 = p1.x - p2.x; double y21 = p1.y - p2.y; double x23 = p3.x - p2.x; double y23 = p3.y - p2.y; double dot = (x21 * x23) + (y21 * y23); double mag1 = Math.sqrt((x21 * x21) + (y21 * y21)); double mag2 = Math.sqrt((x23 * x23) + (y23 * y23)); return Math.acos(dot / (mag1 * mag2)) * 180.0 / Math.PI; } public static double getDist(Point p1, Point p2){ return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); } public static void main(String[] args){ /* System.out.println(getAngle(new Point(5999, 35988001), new Point(6000, 36000000), new Point(0, 36000000))); System.out.println(getAngle(new Point(35988001, 1), new Point(36000000, 0), new Point(36000000, 6000))); System.out.println(getAngle(new Point(6000, 36000000), new Point(0, 36000000), new Point(0, 0))); System.out.println(getAngle(new Point(36000000, 0), new Point(36000000, 6000), new Point(0, 6000))); */ InputReader sc = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int m = sc.nextInt(); Point[] pointsA = new Point[n]; for(int i = 0; i < n; i++){ pointsA[i] = new Point(sc.nextDouble(),sc.nextDouble()); } Point[] pointsB = new Point[m]; for(int i = 0 ;i < m; i++){ pointsB[i] = new Point(sc.nextDouble(),sc.nextDouble()); } ArrayList<Point> convexHullA = convexHull(pointsA, n); ArrayList<Point> convexHullB = convexHull(pointsB, m); if(convexHullB.size() == convexHullA.size()){ int len = convexHullA.size(); if(len == 1){ double distA = 0.0; double distB = 0.0; Point min = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE); Point max = new Point(0, 0); if(pointsA[0].x == pointsA[1].x){ for(Point p : pointsA){ if(p.y < min.y){ min = p; } if(p.y > max.y){ max = p; } } } else{ for(Point p : pointsA){ if(p.x < min.x){ min = p; } if(p.x > max.x){ max = p; } } } distA = getDist(min, max); min = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE); max = new Point(0, 0); if(pointsB[0].x == pointsB[1].x){ for(Point p : pointsB){ if(p.y < min.y){ min = p; } if(p.y > max.y){ max = p; } } } else{ for(Point p : pointsB){ if(p.x < min.x){ min = p; } if(p.x > max.x){ max = p; } } } distB = getDist(min, max); System.out.println(distA == distB ? "YES" : "NO"); } else{ double[] stringA = new double[len * 4]; double[] stringB = new double[len * 2]; stringA[0] = getAngle(convexHullA.get(len - 1), convexHullA.get(0), convexHullA.get(1)); stringA[1] = getDist(convexHullA.get(0), convexHullA.get(1));; stringA[2 * len] = stringA[0]; stringA[(2 * len) + 1] = stringA[1]; for(int i = 1; i < len; i++){ stringA[2 * i] = getAngle(convexHullA.get(i - 1), convexHullA.get(i), convexHullA.get((i + 1) % len)); stringA[(2 * i) + 1] = getDist(convexHullA.get(i), convexHullA.get((i + 1) % len)); stringA[(2 * i) + (2 * len)] = stringA[2 * i]; stringA[(2 * i) + (2 * len) + 1] = stringA[(2 * i) + 1]; } stringB[0] = getAngle(convexHullB.get(len - 1), convexHullB.get(0), convexHullB.get(1)); stringB[1] = getDist(convexHullB.get(0), convexHullB.get(1));; for(int i = 1; i < len; i++){ stringB[2 * i] = getAngle(convexHullB.get(i - 1), convexHullB.get(i), convexHullB.get((i + 1) % len)); stringB[(2 * i) + 1] = getDist(convexHullB.get(i), convexHullB.get((i + 1) % len)); } System.out.println(kmp(stringA, stringB) ? "YES" : "NO"); } } else{ System.out.println("NO"); } out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n1 1", "3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n0 0"]
1 second
["YES", "NO"]
NoteThe first sample: Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with $$$\theta = \pi$$$ (to rotate all power sources $$$180$$$ degrees).The power sources in the first engine become $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-2, 0)$$$. Second, manipulate the second engine: use the first operation with $$$a = b = -2$$$.The power sources in the second engine become $$$(-2, 0)$$$, $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-1, -1)$$$. You can examine that destroying any point, the power field formed by the two engines are always the solid triangle $$$(0, 0)$$$, $$$(-2, 0)$$$, $$$(0, -2)$$$.In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it.
Java 8
standard input
[ "hashing", "geometry", "strings" ]
aeda11f32911d256fb6263635b738e99
The first line contains two integers $$$n$$$, $$$m$$$ ($$$3 \le n, m \le 10^5$$$) — the number of power sources in each engine. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the first engine. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine.
2,400
Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower).
standard output
PASSED
21279c09efb9de077e5f0a55f3460381
train_003.jsonl
1533737100
After the war, the supersonic rocket became the most common public transportation.Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has $$$n$$$ power sources, and the second one has $$$m$$$ power sources. A power source can be described as a point $$$(x_i, y_i)$$$ on a 2-D plane. All points in each engine are different.You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i+a, y_i+b)$$$, $$$a$$$ and $$$b$$$ can be any real numbers. In other words, all power sources will be shifted. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i \cos \theta - y_i \sin \theta, x_i \sin \theta + y_i \cos \theta)$$$, $$$\theta$$$ can be any real number. In other words, all power sources will be rotated.The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources $$$A(x_a, y_a)$$$ and $$$B(x_b, y_b)$$$ exist, then for all real number $$$k$$$ that $$$0 \lt k \lt 1$$$, a new power source will be created $$$C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b)$$$. Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred).A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well.Given a supersonic rocket, check whether it is safe or not.
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.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.util.Comparator; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Vadim Semenov */ 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 final class TaskE { public void solve(int __, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[][] fst = in.nextIntTable(n, 2); int[][] snd = in.nextIntTable(m, 2); int fstHullLength = convexHull(fst); int sndHullLength = convexHull(snd); if (fstHullLength != sndHullLength) { out.println("NO"); return; } long[] sequence = new long[fstHullLength * 6 - 1]; int ptr = 0; for (int i = 0; i < fstHullLength; ++i) { sequence[ptr++] = squaredDistance(fst[i], fst[(i + 1) % fstHullLength]); sequence[ptr++] = squaredDistance(fst[i], fst[(i + 2) % fstHullLength]); } sequence[ptr++] = -1; for (int j = 0; j < 2; ++j) { for (int i = 0; i < sndHullLength - j; ++i) { sequence[ptr++] = squaredDistance(snd[i], snd[(i + 1) % sndHullLength]); sequence[ptr++] = squaredDistance(snd[i], snd[(i + 2) % sndHullLength]); } } int[] prefixFunction = prefixFunction(sequence); for (int i = fstHullLength * 2; i < prefixFunction.length; ++i) { if (prefixFunction[i] == fstHullLength * 2) { out.println("YES"); return; } } out.println("NO"); } private int convexHull(int[][] points) { Arrays.sort(points, Comparator .comparingInt((int[] point) -> point[0]) .thenComparingInt(point -> point[1])); int ptr = 1; while (ptr < points.length && points[0][0] == points[ptr][0] && points[0][1] == points[ptr][1]) { ++ptr; } Arrays.sort(points, ptr, points.length, (int[] fst, int[] snd) -> { int cmp = ccw(points[0], fst, snd); if (cmp == 0) cmp = Long.compare(squaredDistance(points[0], fst), squaredDistance(points[0], snd)); return cmp; }); ptr = 1; for (int i = 1; i < points.length; ++i) { while (ptr > 1 && ccw(points[ptr - 2], points[ptr - 1], points[i]) >= 0) { --ptr; } points[ptr++] = points[i]; } return ptr; } private long squaredDistance(int[] a, int[] b) { long dx = a[0] - b[0]; long dy = a[1] - b[1]; return dx * dx + dy * dy; } private int ccw(int[] a, int[] b, int[] c) { long x1 = b[0] - a[0]; long y1 = b[1] - a[1]; long x2 = c[0] - a[0]; long y2 = c[1] - a[1]; long vp = x1 * y2 - x2 * y1; return vp > 0 ? 1 : vp < 0 ? -1 : 0; } private int[] prefixFunction(long[] sequence) { int[] pf = new int[sequence.length]; for (int i = 1, k = 0; i < sequence.length; ++i) { while (k > 0 && sequence[i] != sequence[k]) { k = pf[k - 1]; } if (sequence[i] == sequence[k]) { ++k; } pf[i] = k; } return pf; } } static class InputReader { private final BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public int[][] nextIntTable(int rows, int columns) { int[][] table = new int[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { table[i][j] = nextInt(); } } return table; } public int nextInt() { return Integer.parseInt(next()); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(readLine()); } return tokenizer.nextToken(); } public String readLine() { String line; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } } }
Java
["3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n1 1", "3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n0 0"]
1 second
["YES", "NO"]
NoteThe first sample: Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with $$$\theta = \pi$$$ (to rotate all power sources $$$180$$$ degrees).The power sources in the first engine become $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-2, 0)$$$. Second, manipulate the second engine: use the first operation with $$$a = b = -2$$$.The power sources in the second engine become $$$(-2, 0)$$$, $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-1, -1)$$$. You can examine that destroying any point, the power field formed by the two engines are always the solid triangle $$$(0, 0)$$$, $$$(-2, 0)$$$, $$$(0, -2)$$$.In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it.
Java 8
standard input
[ "hashing", "geometry", "strings" ]
aeda11f32911d256fb6263635b738e99
The first line contains two integers $$$n$$$, $$$m$$$ ($$$3 \le n, m \le 10^5$$$) — the number of power sources in each engine. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the first engine. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine.
2,400
Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower).
standard output
PASSED
f11f81515f3e47d41ec23b6737dba2e5
train_003.jsonl
1533737100
After the war, the supersonic rocket became the most common public transportation.Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has $$$n$$$ power sources, and the second one has $$$m$$$ power sources. A power source can be described as a point $$$(x_i, y_i)$$$ on a 2-D plane. All points in each engine are different.You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i+a, y_i+b)$$$, $$$a$$$ and $$$b$$$ can be any real numbers. In other words, all power sources will be shifted. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i \cos \theta - y_i \sin \theta, x_i \sin \theta + y_i \cos \theta)$$$, $$$\theta$$$ can be any real number. In other words, all power sources will be rotated.The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources $$$A(x_a, y_a)$$$ and $$$B(x_b, y_b)$$$ exist, then for all real number $$$k$$$ that $$$0 \lt k \lt 1$$$, a new power source will be created $$$C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b)$$$. Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred).A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well.Given a supersonic rocket, check whether it is safe or not.
256 megabytes
import java.util.*; import java.io.*; public class A { final static boolean debug=false; public static void main(String[] args) { FastScanner fs=new FastScanner(); int n1=fs.nextInt(), n2=fs.nextInt(); Vec[] vecs1=new Vec[n1], vecs2=new Vec[n2]; for (int i=0; i<n1; i++) vecs1[i]=new Vec(fs.nextInt(), fs.nextInt()); for (int i=0; i<n2; i++) vecs2[i]=new Vec(fs.nextInt(), fs.nextInt()); ArrayList<Vec> h1=getHull(vecs1); ArrayList<Vec> h2=getHull(vecs2); if (debug) System.out.println(h1); if (debug) System.out.println(h2); if (h1.size()!=h2.size()) { System.out.println("NO"); return; } long[][] h1Parts=getParts(h1), h2Parts=getParts(h2); if (debug)System.out.println("h1 parts:"); for (int i=0; i<h1Parts.length; i++) { if (debug) System.out.println(h1Parts[i][0]+" "+h1Parts[i][1]+" "+h1Parts[i][2]); } if (debug)System.out.println("h2 parts:"); for (int i=0; i<h2Parts.length; i++) { if (debug) System.out.println(h2Parts[i][0]+" "+h2Parts[i][1]+" "+h1Parts[i][2]); } if (works(h1Parts, h2Parts)) { System.out.println("YES"); } else { System.out.println("NO"); } } public static long[][] getParts(ArrayList<Vec> hull) { int n=hull.size(); long[][] toReturn=new long[n][3]; for (int i=0; i<n; i++) { Vec v1=hull.get((i+1)%n).sub(hull.get(i)); Vec v2=hull.get((i+2)%n).sub(hull.get((i+1)%n)); toReturn[i][0]=v1.mag2(); toReturn[i][1]=v1.cross(v2); toReturn[i][2]=v1.dot(v2); } return toReturn; } private static boolean works(long[][] h1, long[][] h2) { int n=h1.length; long[][] full=new long[n+1+n+n][2]; for (int i=0; i<n; i++) { full[i]=h1[i]; full[n+1+i]=full[n+1+n+i]=h2[i]; } full[n]=new long[] {-999999999999999l, -999999999999999l, -999999999999999l}; int[] zVals=zValues(full); if (debug) System.out.println("z: "+Arrays.toString(zVals)); for (int i=n; i<zVals.length; i++) if (zVals[i]>=n) return true; return false; } public static int[] zValues(long[][] s) { int n = s.length; int[] z = new int[n]; z[0] = n; int L = 0, R = 0; int[] left = new int[n], right = new int[n]; for(int i = 1; i < n; ++i) { if(i > R) { L = R = i; while(R < n && s[R-L][0] == s[R][0] && s[R-L][1] == s[R][1] && s[R-L][2] == s[R][2]) R++; z[i] = R - L; R--; } else { int k = i-L; if(z[k] < R-i+1) z[i] = z[k]; else { L = i; while(R < n && s[R-L][0] == s[R][0] && s[R-L][1]== s[R-L][1] && s[R-L][2]== s[R-L][2]) R++; z[i] = R - L; R--; } } left[i] = L; right[i] = R; } return z; } // Returns the convex hull in COUNTER-CLOCKWISE order. Vector: Compare X then Y. public static ArrayList<Vec> getHull(Vec[] ps) { ArrayList<Vec> s = new ArrayList<>(); for (Vec v:ps) s.add(v); Collections.sort(s); int n = s.size(), j = 2, k = 2; Vec[] lo = new Vec[n], up = new Vec[n]; lo[0] = s.get(0); lo[1] = s.get(1); for (int i = 2; i < n; i++) { Vec p = s.get(i); while (j > 1 && !RT(lo[j - 2], lo[j - 1], p)) j--; lo[j++] = p; } up[0] = s.get(n - 1); up[1] = s.get(n - 2); for (int i = n - 3; i >= 0; i--) { // note difference Vec p = s.get(i); while (k > 1 && !RT(up[k - 2], up[k - 1], p)) k--; up[k++] = p; } ArrayList<Vec> res = new ArrayList<Vec>(); for (int i = 0; i < k; i++) res.add(up[i]); for (int i = 1; i < j - 1; i++) res.add(lo[i]); return res; } static boolean RT(Vec a, Vec b, Vec c) { return b.sub(a).cross(c.sub(a)) > 0; } private static class Vec implements Comparable<Vec> { long x, y; public Vec(long x, long y) { this.x=x; this.y=y; } public Vec add(Vec o) { return new Vec(x+o.x, y+o.y); } public Vec scale(long s) { return new Vec(x*s, y*s); } public Vec sub(Vec o) { return add(o.scale(-1)); } public long mag2() { return x*x+y*y; } public long dot(Vec o) { return x*o.x+y*o.y; } public long cross(Vec o) { return x*o.y-y*o.x; } public double angle() { return Math.atan2(y, x); } public int compareTo(Vec o) { if (x!=o.x) return Long.compare(x, o.x); return Long.compare(y, o.y); } public String toString() { return "("+x+", "+y+")"; } } private static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } }
Java
["3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n1 1", "3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n0 0"]
1 second
["YES", "NO"]
NoteThe first sample: Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with $$$\theta = \pi$$$ (to rotate all power sources $$$180$$$ degrees).The power sources in the first engine become $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-2, 0)$$$. Second, manipulate the second engine: use the first operation with $$$a = b = -2$$$.The power sources in the second engine become $$$(-2, 0)$$$, $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-1, -1)$$$. You can examine that destroying any point, the power field formed by the two engines are always the solid triangle $$$(0, 0)$$$, $$$(-2, 0)$$$, $$$(0, -2)$$$.In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it.
Java 8
standard input
[ "hashing", "geometry", "strings" ]
aeda11f32911d256fb6263635b738e99
The first line contains two integers $$$n$$$, $$$m$$$ ($$$3 \le n, m \le 10^5$$$) — the number of power sources in each engine. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the first engine. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine.
2,400
Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower).
standard output
PASSED
529b03cf5b5c87fd1785f5f6e09a1362
train_003.jsonl
1533737100
After the war, the supersonic rocket became the most common public transportation.Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has $$$n$$$ power sources, and the second one has $$$m$$$ power sources. A power source can be described as a point $$$(x_i, y_i)$$$ on a 2-D plane. All points in each engine are different.You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i+a, y_i+b)$$$, $$$a$$$ and $$$b$$$ can be any real numbers. In other words, all power sources will be shifted. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i \cos \theta - y_i \sin \theta, x_i \sin \theta + y_i \cos \theta)$$$, $$$\theta$$$ can be any real number. In other words, all power sources will be rotated.The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources $$$A(x_a, y_a)$$$ and $$$B(x_b, y_b)$$$ exist, then for all real number $$$k$$$ that $$$0 \lt k \lt 1$$$, a new power source will be created $$$C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b)$$$. Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred).A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well.Given a supersonic rocket, check whether it is safe or not.
256 megabytes
//package round502; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.InputMismatchException; public class E { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(), m = ni(); long[][] coa = new long[n][]; long[][] cob = new long[m][]; for(int i = 0;i < n;i++){ coa[i] = new long[]{ni(), ni()}; } for(int i = 0;i < m;i++){ cob[i] = new long[]{ni(), ni()}; } long[][] ha = convexHull(coa); long[][] hb = convexHull(cob); out.println(equiv(ha, hb) ? "YES" : "NO"); } boolean equiv(long[][] a, long[][] b) { if(a.length != b.length)return false; int n = a.length; long[] ads = new long[n]; long[] bds = new long[n]; for(int i = 0, j = 1;i < n;i++,j++){ if(j == n)j = 0; ads[i] = d2(a[i], a[j]); bds[i] = d2(b[i], b[j]); } long[] c = new long[3*n]; for(int i = 0;i < n;i++)c[i] = ads[i]; for(int i = 0;i < n;i++)c[i+n] = bds[i]; for(int i = 0;i < n;i++)c[i+2*n] = bds[i]; int[] z = Z(c); outer: for(int i = n;i < 2*n;i++){ if(z[i] >= n){ for(int j = 0, k = i-n;j < n;j++,k++){ if(k == n)k = 0; long va = d2(a[j], a[0]); long vb = d2(b[k], b[i-n]); if(va != vb)continue outer; } return true; } } return false; } public static long d2(long ax, long ay, long bx, long by) { return (bx-ax)*(bx-ax)+(by-ay)*(by-ay); } public static long d2(long[] a, long[] b) { return (b[0]-a[0])*(b[0]-a[0])+(b[1]-a[1])*(b[1]-a[1]); } public static int[] Z(long[] str) { int n = str.length; int[] z = new int[n]; if(n == 0)return z; z[0] = n; int l = 0, r = 0; for(int i = 1;i < n;i++){ if(i > r){ l = r = i; while(r < n && str[r-l] == str[r])r++; z[i] = r-l; r--; }else{ if(z[i-l] < r-i+1){ z[i] = z[i-l]; }else{ l = i; while(r < n && str[r-l] == str[r])r++; z[i] = r-l; r--; } } } return z; } public static int ccw(long ax, long ay, long bx, long by, long tx, long ty){ return Long.signum((tx-ax)*(by-ay)-(bx-ax)*(ty-ay)); } public static int ccw(long[] a, long[] b, long[] t){ return Long.signum((t[0]-a[0])*(b[1]-a[1])-(b[0]-a[0])*(t[1]-a[1])); } public static int ccw(int[] a, int[] b, int[] t){ return Long.signum((long)(t[0]-a[0])*(b[1]-a[1])-(long)(b[0]-a[0])*(t[1]-a[1])); } public static long[][] convexHull(long[][] co) { int n = co.length; if(n <= 1)return co; Arrays.sort(co, new Comparator<long[]>(){ public int compare(long[] a, long[] b){ if(a[0] != b[0])return Long.compare(a[0], b[0]); return Long.compare(a[1], b[1]); } }); int[] inds = new int[n + 1]; int p = 0; for(int i = 0;i < n;i++){ if(p >= 1 && co[inds[p-1]][0] == co[i][0] && co[inds[p-1]][1] == co[i][1])continue; while(p >= 2 && ccw(co[inds[p-2]], co[inds[p-1]], co[i]) >= 0)p--; // if you need point on line inds[p++] = i; } int inf = p + 1; for(int i = n - 2;i >= 0;i--){ if(co[inds[p-1]][0] == co[i][0] && co[inds[p-1]][1] == co[i][1])continue; while(p >= inf && ccw(co[inds[p-2]], co[inds[p-1]], co[i]) >= 0)p--; // if you need point on line inds[p++] = i; } int len = Math.max(p-1, 1); long[][] ret = new long[len][]; for(int i = 0;i < len;i++)ret[i] = co[inds[i]]; return ret; } 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
["3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n1 1", "3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n0 0"]
1 second
["YES", "NO"]
NoteThe first sample: Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with $$$\theta = \pi$$$ (to rotate all power sources $$$180$$$ degrees).The power sources in the first engine become $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-2, 0)$$$. Second, manipulate the second engine: use the first operation with $$$a = b = -2$$$.The power sources in the second engine become $$$(-2, 0)$$$, $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-1, -1)$$$. You can examine that destroying any point, the power field formed by the two engines are always the solid triangle $$$(0, 0)$$$, $$$(-2, 0)$$$, $$$(0, -2)$$$.In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it.
Java 8
standard input
[ "hashing", "geometry", "strings" ]
aeda11f32911d256fb6263635b738e99
The first line contains two integers $$$n$$$, $$$m$$$ ($$$3 \le n, m \le 10^5$$$) — the number of power sources in each engine. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the first engine. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine.
2,400
Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower).
standard output
PASSED
afac6521b264cf702e86a153ff50699c
train_003.jsonl
1533737100
After the war, the supersonic rocket became the most common public transportation.Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has $$$n$$$ power sources, and the second one has $$$m$$$ power sources. A power source can be described as a point $$$(x_i, y_i)$$$ on a 2-D plane. All points in each engine are different.You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i+a, y_i+b)$$$, $$$a$$$ and $$$b$$$ can be any real numbers. In other words, all power sources will be shifted. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i \cos \theta - y_i \sin \theta, x_i \sin \theta + y_i \cos \theta)$$$, $$$\theta$$$ can be any real number. In other words, all power sources will be rotated.The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources $$$A(x_a, y_a)$$$ and $$$B(x_b, y_b)$$$ exist, then for all real number $$$k$$$ that $$$0 \lt k \lt 1$$$, a new power source will be created $$$C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b)$$$. Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred).A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well.Given a supersonic rocket, check whether it is safe or not.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class DivCmb_502E { public static void main(String[] args) throws IOException { new DivCmb_502E().execute(); } static long BASE = 101323; static long MOD = 1_000_000_123; static long[] POW = new long[200_001]; void execute() throws IOException { POW[0] = 1; for (int i = 1; i <= 200_000; i++) { POW[i] = POW[i - 1] * BASE % MOD; } BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); StringTokenizer inputData = new StringTokenizer(reader.readLine()); int N = Integer.parseInt(inputData.nextToken()); int M = Integer.parseInt(inputData.nextToken()); Point[] set1 = new Point[N]; for (int i = 0; i < N; i++) { inputData = new StringTokenizer(reader.readLine()); set1[i] = new Point(Integer.parseInt(inputData.nextToken()), Integer.parseInt(inputData.nextToken())); } Point[] hull1 = solve(set1); Point[] set2 = new Point[M]; for (int i = 0; i < M; i++) { inputData = new StringTokenizer(reader.readLine()); set2[i] = new Point(Integer.parseInt(inputData.nextToken()), Integer.parseInt(inputData.nextToken())); } Point[] hull2 = solve(set2); if (hull1.length != hull2.length) { printer.println("NO"); printer.close(); return; } int hSize = hull1.length; long[] vals1 = new long[hSize * 2]; for (int i = 0; i < hSize; i++) { Point cP = hull1[i]; Point pP = hull1[(i - 1 + hSize) % hSize]; Point nP = hull1[(i + 1) % hSize]; vals1[2 * i] = (pP.x - cP.x) * (nP.x - cP.x) + (pP.y - cP.y) * (nP.y - cP.y); vals1[2 * i + 1] = (nP.x - cP.x) * (nP.x - cP.x) + (nP.y - cP.y) * (nP.y - cP.y); } long[] vals2 = new long[hSize * 2]; for (int i = 0; i < hSize; i++) { Point cP = hull2[i]; Point pP = hull2[(i - 1 + hSize) % hSize]; Point nP = hull2[(i + 1) % hSize]; vals2[2 * i] = (pP.x - cP.x) * (nP.x - cP.x) + (pP.y - cP.y) * (nP.y - cP.y); vals2[2 * i + 1] = (nP.x - cP.x) * (nP.x - cP.x) + (nP.y - cP.y) * (nP.y - cP.y); } long h1 = 0; long h2 = 0; for (int i = 0; i < 2 * hSize; i++) { h1 = (h1 * BASE + vals1[i] % MOD + MOD) % MOD; h2 = (h2 * BASE + vals2[i] % MOD + MOD) % MOD; } if (h1 == h2 && Arrays.equals(vals1, vals2)) { printer.println("YES"); printer.close(); return; } for (int i = 0; i < hSize * 2; i += 2) { h1 = (h1 - (POW[hSize * 2 - 1] * ((vals1[i] % MOD) + MOD)) % MOD + MOD) % MOD; h1 = h1 * BASE % MOD; h1 = (h1 + vals1[i] % MOD + MOD) % MOD; h1 = (h1 - (POW[hSize * 2 - 1] * ((vals1[i + 1] % MOD) + MOD)) % MOD + MOD) % MOD; h1 = h1 * BASE % MOD; h1 = (h1 + vals1[i + 1] % MOD + MOD) % MOD; if (h1 == h2 && equals(vals1, vals2, i + 2)) { printer.println("YES"); printer.close(); return; } } printer.println("NO"); printer.close(); } static boolean equals(long[] a, long[] b, int off) { for (int i = 0; i < a.length; i++) { if (a[(i + off) % a.length] != b[i]) { return false; } } return true; } Point[] solve(Point[] points) { Arrays.sort(points); Point[] cHull = new Point[2 * points.length]; int hSize = 0; for (int i = 0; i < points.length; i++) { while (hSize >= 2 && orient(cHull[hSize - 2], cHull[hSize - 1], points[i]) <= 0) { hSize--; } cHull[hSize++] = points[i]; } int lHullSize = hSize; for (int i = points.length - 2; i >= 0; i--) { while (hSize > lHullSize && orient(cHull[hSize - 2], cHull[hSize - 1], points[i]) <= 0) { hSize--; } cHull[hSize++] = points[i]; } Point[] aCHull = new Point[hSize - 1]; for (int i = 0; i < hSize - 1; i++) { aCHull[i] = cHull[i]; } return aCHull; } public static long orient(Point a, Point b, Point c) { return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); } class Point implements Comparable<Point> { long x; long y; Point(long x, long y) { this.x = x; this.y = y; } public int compareTo(Point o) { return x < o.x ? -1 : x == o.x ? (y < o.y ? -1 : y == o.y ? 0 : 1) : 1; } } }
Java
["3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n1 1", "3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n0 0"]
1 second
["YES", "NO"]
NoteThe first sample: Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with $$$\theta = \pi$$$ (to rotate all power sources $$$180$$$ degrees).The power sources in the first engine become $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-2, 0)$$$. Second, manipulate the second engine: use the first operation with $$$a = b = -2$$$.The power sources in the second engine become $$$(-2, 0)$$$, $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-1, -1)$$$. You can examine that destroying any point, the power field formed by the two engines are always the solid triangle $$$(0, 0)$$$, $$$(-2, 0)$$$, $$$(0, -2)$$$.In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it.
Java 8
standard input
[ "hashing", "geometry", "strings" ]
aeda11f32911d256fb6263635b738e99
The first line contains two integers $$$n$$$, $$$m$$$ ($$$3 \le n, m \le 10^5$$$) — the number of power sources in each engine. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the first engine. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine.
2,400
Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower).
standard output
PASSED
a47c4ce5ddb21dc093d92104e8b60fb3
train_003.jsonl
1533737100
After the war, the supersonic rocket became the most common public transportation.Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has $$$n$$$ power sources, and the second one has $$$m$$$ power sources. A power source can be described as a point $$$(x_i, y_i)$$$ on a 2-D plane. All points in each engine are different.You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i+a, y_i+b)$$$, $$$a$$$ and $$$b$$$ can be any real numbers. In other words, all power sources will be shifted. For every power source as a whole in that engine: $$$(x_i, y_i)$$$ becomes $$$(x_i \cos \theta - y_i \sin \theta, x_i \sin \theta + y_i \cos \theta)$$$, $$$\theta$$$ can be any real number. In other words, all power sources will be rotated.The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources $$$A(x_a, y_a)$$$ and $$$B(x_b, y_b)$$$ exist, then for all real number $$$k$$$ that $$$0 \lt k \lt 1$$$, a new power source will be created $$$C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b)$$$. Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred).A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well.Given a supersonic rocket, check whether it is safe or not.
256 megabytes
import java.io.*; import java.util.*; public class E502 { public static void main(String[] args) { new E502(); } FS in = new FS(); PrintWriter out = new PrintWriter(System.out); int n, m; vec[] A, hullA, B, hullB; E502() { n = in.nextInt(); m = in.nextInt(); A = new vec[n]; for (int i = 0; i < n; i++) A[i] = new vec(in.nextLong(), in.nextLong()); B = new vec[m]; for (int i = 0; i < m; i++) B[i] = new vec(in.nextLong(), in.nextLong()); convexHull CH = new convexHull(); hullA = CH.getHull(CH.removeDupes(A)); hullB = CH.getHull(CH.removeDupes(B)); int szA = hullA.length, szB = hullB.length; if (szA != szB) { out.println("NO"); out.close(); return; } long[] vrtxA = new long[3 * szA]; for (int i = 0; i < szA; i++) { int l = (i - 1 + szA) % szA; vec L = hullA[l].sub(hullA[i]); int r = (i + 1) % szA; vec R = hullA[r].sub(hullA[i]); vrtxA[3 * i] = L.mag2(); vrtxA[3 * i + 1] = R.mag2(); vrtxA[3 * i + 2] = L.dot(R); } long[] vrtxB = new long[3 * szB]; for (int i = 0; i < szB; i++) { int l = (i - 1 + szB) % szB; vec L = hullB[l].sub(hullB[i]); int r = (i + 1) % szB; vec R = hullB[r].sub(hullB[i]); vrtxB[3 * i] = L.mag2(); vrtxB[3 * i + 1] = R.mag2(); vrtxB[3 * i + 2] = L.dot(R); } int vA = vrtxA.length, vB = vrtxB.length; long[] vrtx = new long[vA + 1 + 2 * vB]; vrtx[vA] = -1; for (int i = 0; i < vA; i++) vrtx[i] = vrtxA[i]; for (int i = 0; i < vB; i++) { int j = vA + 1 + i; vrtx[j] = vrtx[j + vB] = vrtxB[i]; } int[] zvals = zValues(vrtx); int LCS = -1; for (int i = vA + 1; i < vrtx.length; i++) LCS = max(LCS, zvals[i]); if (LCS == vA) out.println("YES"); else out.println("NO"); out.close(); } int[] zValues(long[] arr) { int n = arr.length; int[] z = new int[n]; z[0] = n; int l = 0, r = 0; int[] left = new int[n], right = new int[n]; for (int i = 1; i < n; ++i) { if (i > r) { l = r = i; while (r < n && arr[r - l] == arr[r]) r++; z[i] = r - l; r--; } else { int k = i - l; if (z[k] < r - i + 1) z[i] = z[k]; else { l = i; while (r < n && arr[r - l] == arr[r]) r++; z[i] = r - l; r--; } } left[i] = l; right[i] = r; } return z; } class convexHull { vec[] removeDupes(vec[] pts) { HashSet<vec> set = new HashSet<>(); for (vec v : pts) set.add(v); int ptr = 0; pts = new vec[set.size()]; for (vec v : set) pts[ptr++] = v; return pts; } vec[] getHull(vec[] pts) { pts = pts.clone(); Arrays.sort(pts); if (pts.length < 3) return pts; int n = pts.length, j = 2, k = 2; vec[] lo = new vec[n]; lo[0] = pts[0]; lo[1] = pts[1]; for (int i = 2; i < n; i++) { vec p = pts[i]; while (j > 1 && !right(lo[j - 2], lo[j - 1], p)) j--; lo[j++] = p; } vec[] up = new vec[n]; up[0] = pts[n - 1]; up[1] = pts[n - 2]; for (int i = n - 3; i >= 0; i--) { vec p = pts[i]; while (k > 1 && !right(up[k - 2], up[k - 1], p)) k--; up[k++] = p; } vec[] hull = new vec[j + k - 2]; for (int i = 0; i < k; i++) hull[i] = up[i]; for (int i = 1; i < j - 1; i++) hull[k + i - 1] = lo[i]; return hull; } boolean right(vec a, vec b, vec c) { return b.sub(a).cross(c.sub(a)) > 0; } } class vec implements Comparable<vec> { long x, y; vec (long xx, long yy) { x = xx; y = yy; } vec sub(vec o) { return new vec(x - o.x, y - o.y); } long mag2() { return dot(this); } long dot(vec o) { return x * o.x + y * o.y; } long cross(vec o) { return x * o.y - y * o.x; } public int compareTo(vec o) { if (x != o.x) return Long.compare(x, o.x); return Long.compare(y, o.y); } } int abs(int x) { if (x < 0) return -x; return x; } long abs(long x) { if (x < 0) return -x; return x; } int min(int a, int b) { if (a < b) return a; return b; } int max(int a, int b) { if (a > b) return a; return b; } long min(long a, long b) { if (a < b) return a; return b; } long max(long a, long b) { if (a > b) return a; return b; } int gcd(int a, int b) { while (b > 0) { a = b^(a^(b = a)); b %= a; } return a; } long gcd(long a, long b) { while (b > 0) { a = b^(a^(b = a)); b %= a; } return a; } long lcm(int a, int b) { return (((long) a) * b) / gcd(a, b); } long lcm(long a, long b) { return (a * b) / gcd(a, b); } void sort(int[] arr) { int sz = arr.length, j; Random r = new Random(); for (int i = 0; i < sz; i++) { j = r.nextInt(i + 1); arr[i] = arr[j]^(arr[i]^(arr[j] = arr[i])); } Arrays.sort(arr); } class FS { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) {} } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n1 1", "3 4\n0 0\n0 2\n2 0\n0 2\n2 2\n2 0\n0 0"]
1 second
["YES", "NO"]
NoteThe first sample: Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with $$$\theta = \pi$$$ (to rotate all power sources $$$180$$$ degrees).The power sources in the first engine become $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-2, 0)$$$. Second, manipulate the second engine: use the first operation with $$$a = b = -2$$$.The power sources in the second engine become $$$(-2, 0)$$$, $$$(0, 0)$$$, $$$(0, -2)$$$, and $$$(-1, -1)$$$. You can examine that destroying any point, the power field formed by the two engines are always the solid triangle $$$(0, 0)$$$, $$$(-2, 0)$$$, $$$(0, -2)$$$.In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it.
Java 8
standard input
[ "hashing", "geometry", "strings" ]
aeda11f32911d256fb6263635b738e99
The first line contains two integers $$$n$$$, $$$m$$$ ($$$3 \le n, m \le 10^5$$$) — the number of power sources in each engine. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the first engine. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0\leq x_i, y_i\leq 10^8$$$) — the coordinates of the $$$i$$$-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine.
2,400
Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower).
standard output
PASSED
0d3d94bd9bfa2fd653c6297b6aa5bd23
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import static java.lang.Math.*; import static java.lang.System.currentTimeMillis; import static java.lang.System.exit; import static java.lang.System.arraycopy; import static java.util.Arrays.sort; import static java.util.Arrays.binarySearch; import static java.util.Arrays.fill; import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { try { if (new File("input.txt").exists()) System.setIn(new FileInputStream("input.txt")); } catch (SecurityException e) { } new Main().run(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); final int INF = Integer.MAX_VALUE; int head[]; int next[]; int dest[]; int we[]; int en = 0; int dia[]; int ok[]; void add(int a, int b, int w){ next[++en] = head[a]; head[a] = en; dest[en] = b; we[en] = w; } void dfs(int v, int col){ ok[v] = col; int j = head[v]; while(j != -1){ int d = dest[j]; if(ok[d] == 0){ //out.println(v + " " + d + " " + we[j]); dia[d] = min(dia[v], we[j]); dfs(d, col); } j = next[j]; } } private void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int v = nextInt(); int e = nextInt(); head = new int[v * 2]; next = new int[e * 2]; dest = new int[e * 2]; we = new int[e * 2]; ok = new int[v * 2]; dia = new int[v * 2]; fill(dia, INF); fill(next, -1); fill(head, -1); boolean isinp[] = new boolean[v + 1]; boolean isout[] = new boolean[v + 1]; for(int i = 0; i < e; i++){ int a = nextInt(); int b = nextInt(); int w = nextInt(); add(a - 1, b - 1, w); isinp[b - 1] = true; isout[a - 1] = true; } int c = 0; for(int i = 0; i < v; i++){ if(ok[i] == 0 && !isinp[i]) dfs(i, ++c); } int ans = 0; for(int i = 0; i < v; i++) if(!isinp[i] && isout[i]) ans++; if(e == 0) ans = 0; out.println(ans); if(ans > 0){ int mx = 0; int a, b = 0; for(int i = 0; i < v && ans > 0; i++){ if(isinp[i]) continue; mx = 0; for(int j = 0; j < v; j++){ if(!isout[j] && (ok[i] == ok[j]) && i != j){ mx = max(mx, dia[j]); b = j + 1; } } isout[b - 1] = true; out.println((i + 1) + " " + b + " " + mx); ans--; } } in.close(); out.close(); } void chk(boolean b) { if (b) return; System.out.println(new Error().getStackTrace()[1]); exit(999); } void deb(String fmt, Object... args) { System.out.printf(Locale.US, fmt + "%n", args); } String nextToken() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!st.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; st = new StringTokenizer(s); } return false; } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
86cc904cfb656c4a8604a5f6aaad2b25
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String [] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); args = br.readLine().split(" "); int n = Integer.parseInt(args[0]), p = Integer.parseInt(args[1]); int [] link = new int [n], mag = new int [n]; boolean isSource [] = new boolean [n]; Arrays.fill(isSource, true); for (int i = 0; i < p; i++) { args = br.readLine().split(" "); int a = Integer.parseInt(args[0]), b = Integer.parseInt(args[1]), d = Integer.parseInt(args[2]); link [a - 1] = b - 1; mag [a - 1] = d; isSource[b - 1] = false; } LinkedList<Integer> src = new LinkedList<Integer>(), dst = new LinkedList<Integer>(), caps = new LinkedList<Integer>(); for (int i = 0; i < n; i++) { if (isSource[i]) { int curr = i; int cap = mag[curr]; boolean first = true; while(mag[curr] != 0 && (first || curr != i)) { cap = Math.min(cap,mag[curr]); curr = link[curr]; first = false; } if (cap != 0) { src.add(i); dst.add(curr); caps.add(cap); } } } int res = src.size(); StringBuffer sb = new StringBuffer(); sb.append(res + "\n"); for (int i = 0; i < res; i++) { sb.append((src.remove() + 1) + " " + (dst.remove()+ 1) + " " + caps.remove() + "\n"); } System.out.print(sb.toString()); } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
9c48a6d4051e11d013cfd44a5a370a72
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.io.PrintWriter; import java.util.*; public class C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(); int res=0; PrintWriter out=new PrintWriter(System.out); int[][] grid = new int[n][n]; for (int i = 0; i < m; i++) { int f = sc.nextInt(), s = sc.nextInt(); grid[f - 1][s - 1] = sc.nextInt(); } boolean[] vis = new boolean[n]; for (int i = 0; i < n; i++) { if (vis[i]) continue; boolean start = true; for (int j = 0; j < n; j++) { if (grid[j][i] > 0) { start = false; break; } } if (start) { vis[i]=true; int st = i; int end = i; int min = 1000000000; boolean cont = true; while (cont) { cont = false; for (int j = 0; j < n; j++) { if (grid[end][j] > 0) { if(vis[j]) { end=st; break; } vis[j]=true; cont = true; min = Math.min(min, grid[end][j]); end = j; break; } } } if (st == end); else{ res++; out.println((st+1) + " " + (end+1) + " " + min); } } } System.out.println(res); out.flush(); } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
beb3765891acb7376c2c5839602b6092
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.util.*; import java.io.*; public class C{ static int[][] a= new int[1001][1001]; static int[] d1 = new int[1001], d2 = new int[1001],xx = new int[1001],yy = new int[1001],zz = new int[1001]; static boolean[] use = new boolean[1001]; static int v, u, s,n,m,len=0; public static void solve(int i){ u=i;use[i]=true; for (int j=1;j<=n;j++) if (a[i][j]>0 && !use[j]) { if (a[i][j]<s) s = a[i][j]; solve(j); } } public static void main(String args[]) { Scanner in = new Scanner(System.in); n=in.nextInt();m=in.nextInt(); for (int i=1;i<=m;i++) { int x=in.nextInt(),y=in.nextInt(),z=in.nextInt(); d1[x]++;a[x][y]=z;d2[y]++; } for (int i=1;i<=n;i++) if (d2[i]==0 && !use[i]) { s=Integer.MAX_VALUE;u=0; solve(i); if (u!=i && u>0 && d1[u]==0 && s<Integer.MAX_VALUE) {len++;xx[len]=i;yy[len]=u;zz[len]=s;} } System.out.println(len); for (int i=1;i<=len;i++) System.out.println(xx[i]+" "+yy[i]+" "+zz[i]); } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
8d48451f5e9e0903f1a87bddba80d7b4
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.Scanner; /** * * @author JAVA */ public class c { static boolean t[]; static boolean a[][]; static int n; static int diam[][]; static int way[][]; static int count = 0; static int min[]; static int root; static void dfs(int r){ t[r] = true; way[count][1] = r+1; for (int i = 0; i<n; i++) if (a[r][i] && !t[i]){ if (min[count]>diam[r][i]) min[count] = diam[r][i]; dfs(i); } t[r] =false; } public static void main(String[] args) { Scanner in = new Scanner (System.in); n = in.nextInt(); a = new boolean[n][n]; t = new boolean [n]; diam = new int[n][n]; way = new int[n][n]; min = new int[n]; int m = in.nextInt(); for (int i = 0; i<m; i++) { int x = in.nextInt()-1; int y = in.nextInt()-1; int tank = in.nextInt(); a[x][y] = true; diam[x][y] = tank; } int ans = 0; for (int i = 0; i<n; i++){ boolean bool = true; for(int j =0; j<n; j++) if (a[i][j]) bool = false; if (bool) ans++; } int ans1 = 0; for (int i = 0; i<n; i++){ for(int j =0; j<n; j++) if (a[i][j]) ans1++; } if (ans>0 && ans1>0){ for (int i = 0; i<n; i++) if (!t[i]){ min[count] = Integer.MAX_VALUE; way[count][0] = i+1; dfs(i); if (check(way[count][1]-1) && check1(way[count][0]) && way[count][0]!=way[count][1]) count++; } Print(way); }else System.out.println(0); } private static void Print(int[][] way) { System.out.println(count); for (int i = 0; i<count; i++){ for (int j = 0; j<2; j++) System.out.print(way[i][j] + " "); System.out.println(min[i]); } } private static boolean check(int i) { boolean bool = true; for (int j = 0; j<n; j++) if (a[i][j]) bool = false; return bool; } private static boolean check1(int x){ boolean bool = true; for (int i = 0; i<n; i++) if (a[i][x-1]) bool = false; return bool; } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
9ee22b15080e6aa80df9b218c5479e15
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.Queue; import javax.management.Query; public class C { static class TankTap implements Comparable<TankTap>{ int tank; int tap; int diam; public TankTap(int tank, int tap, int diam) { this.tank = tank; this.tap = tap; this.diam = diam; } @Override public int compareTo(TankTap o) { return tank - o.tank; } public String toString() { return (tank + 1) + " " + (tap + 1) + " " + diam; } } public static int[] readNNumbers(String line, int n) { int[] nums = new int[n]; int idx = 0; for(int i = 0; i < line.length(); i++) { char c = line.charAt(i); if(c == ' ') idx++; else nums[idx] = 10 * nums[idx] + (c - '0'); } return nums; } /** * @param args */ public static void main(String[] args) throws Exception { //System.setIn(new FileInputStream("c.in")); BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int[] input = readNNumbers(bf.readLine(), 2); int V = input[0], E = input[1]; int[] graphTo = new int[V]; int[] graphFrom = new int[V]; int[] graphWeight = new int[V]; for(int i = 0; i < V; i++) { graphTo[i] = -1; graphFrom[i] = -1; graphWeight[i] = 0; } for(int i = 0; i < E; i++) { input = readNNumbers(bf.readLine(), 3); int from = input[0] - 1, to = input[1] - 1, diam = input[2]; graphTo[from] = to; graphFrom[to] = from; graphWeight[from] = diam; } ArrayList<TankTap> sols = new ArrayList<TankTap>(); Queue<Integer> parents = new LinkedList<Integer>(); for(int i = 0; i < V; i++) { if(graphFrom[i] == -1) { parents.add(i); } } while(!parents.isEmpty()) { int tank = parents.poll(); int tap = tank; int minDiam = Integer.MAX_VALUE / 2; while(graphTo[tap] != -1) { minDiam = Math.min(minDiam, graphWeight[tap]); tap = graphTo[tap]; } if(tank == tap) continue; sols.add(new TankTap(tank, tap, minDiam)); } Collections.sort(sols); System.out.println(sols.size()); for(TankTap t : sols) { System.out.println(t); } } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
d9120ae3eb71ff4fb6457fd884951c07
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int[] d=new int[1001]; static int[] w=new int[1001]; static int n,p,k=0; static boolean[] bak=new boolean[1001]; static void dfs(int v,int u,int min) { if (d[u]>0) { if (w[u]<min) dfs(v,d[u],w[u]); else dfs(v,d[u],min); } else System.out.println(v+" "+u+" "+min); } public static void main(String[] args) { Scanner in=new Scanner(System.in); n=in.nextInt(); p=in.nextInt(); for (int i=1;i<=p;i++) { int a=in.nextInt(); int b=in.nextInt(); int c=in.nextInt(); d[a]=b; w[a]=c; bak[a]=true; k++; } for (int i=1;i<=n;i++) if (bak[d[i]]) {bak[d[i]]=false;k--;} System.out.println(k); for (int i=1;i<=n;i++) if (bak[i]) dfs(i,i,1000001); } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
d096f762ca8efa9ce058da208182e5b5
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class ProblemC { static class Pipe { int to; int dia; Pipe(int t, int d) { to = t; dia = d; } } static Pipe[] gopipe; static int last = 0; private static int dfs(int now) { if (gopipe[now] == null) { last = now; return Integer.MAX_VALUE; } return Math.min(dfs(gopipe[now].to), gopipe[now].dia); } public static void main(String[] args) throws IOException { BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); String[] line = s.readLine().split(" "); int h = Integer.valueOf(line[0]); int p = Integer.valueOf(line[1]); int[] indeg = new int[h]; int[] outdeg = new int[h]; gopipe = new Pipe[h]; for (int i = 0 ; i < p ; i++) { String[] pipe = s.readLine().split(" "); int from = Integer.valueOf(pipe[0])-1; int to = Integer.valueOf(pipe[1])-1; int diameter = Integer.valueOf(pipe[2]); gopipe[from] = new Pipe(to, diameter); outdeg[from]++; indeg[to]++; } int cnt = 0; StringBuffer ans = new StringBuffer(); for (int i = 0 ; i < h ; i++) { if (indeg[i] == 0 ) { int maxdia = dfs(i); if (maxdia == Integer.MAX_VALUE || last == i) { continue; } cnt++; ans.append((i+1)).append(" ").append(last+1).append(" ").append(maxdia).append("\n"); } } System.out.println(cnt); System.out.println(ans.toString()); } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
8a5ab014a2b902de8e75946c6c609254
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class _C { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE")!=null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException{ if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException{ while(!tok.hasMoreTokens()){ tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException{ return Integer.parseInt(readString()); } long readLong() throws IOException{ return Long.parseLong(readString()); } double readDouble() throws IOException{ return Double.parseDouble(readString()); } public static void main(String[] args){ new _C().run(); } public void run(){ try{ long t1 = System.currentTimeMillis(); init(); solve(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = "+(t2-t1)); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } static class Utils { private Utils() {} public static void mergeSort(int[] a) { mergeSort(a, 0, a.length - 1); } private static void mergeSort(int[] a, int leftIndex, int rightIndex) { final int MAGIC_VALUE = 50; if (leftIndex < rightIndex) { if (rightIndex - leftIndex <= MAGIC_VALUE) { insertionSort(a, leftIndex, rightIndex); } else { int middleIndex = (leftIndex + rightIndex) / 2; mergeSort(a, leftIndex, middleIndex); mergeSort(a, middleIndex + 1, rightIndex); merge(a, leftIndex, middleIndex, rightIndex); } } } private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - leftIndex + 1; int length2 = rightIndex - middleIndex; int[] leftArray = new int[length1]; int[] rightArray = new int[length2]; System.arraycopy(a, leftIndex, leftArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = leftArray[i++]; } else { a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++]; } } } private static void insertionSort(int[] a, int leftIndex, int rightIndex) { for (int i = leftIndex + 1; i <= rightIndex; i++) { int current = a[i]; int j = i - 1; while (j >= leftIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } } class trubs implements Comparable<trubs>{ public int tank, tap, diam; @Override public int compareTo(trubs o) { return 0; } public trubs(int tank, int tap, int diam) { this.tank = tank; this.tap = tap; this.diam = diam; } } void solve() throws IOException{ int n = readInt(); int p = readInt(); int[][] diam = new int[n][n]; for(int i = 0; i < p; i++){ int a = readInt()-1; int b = readInt()-1; diam[a][b] = readInt(); } ArrayList<Integer> tanks = new ArrayList<Integer>(); ArrayList<Integer> taps = new ArrayList<Integer>(); int[] coof = new int[n]; for(int i = 0; i < n; i++){ boolean a = false; boolean b = false; for(int j = 0; j< n; j++){ if(diam[i][j] > 0){ a = true; coof[i] = j; } if(diam[j][i] > 0){ b = true; } } if(a && !b){ tanks.add(i); } if(!a && b){ taps.add(i); coof[i] = -1; } } if(tanks.size() == 0){ out.println("0"); return; } String[] t = new String[tanks.size()]; for(int i = 0; i < tanks.size(); i++){ int min = 1000000; int k = tanks.get(i); while(coof[k]!=-1){ if(diam[k][coof[k]] < min) min = diam[k][coof[k]]; k = coof[k]; } t[i] = Integer.toString(tanks.get(i)+1) + " " + Integer.toString(k+1) + " " + Integer.toString(min); } out.println(t.length); for(int i = 0; i < t.length; i++){ out.println(t[i]); } } static double distance(long x1, long y1, long x2, long y2){ return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } static long gcd(long a, long b){ while(a != b){ if(a < b) a -=b; else b -= a; } return a; } static long lcm(long a, long b){ return a * b /gcd(a, b); } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
f126428ecaf5f60da53d6e03fe215e50
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; /** * Created by IntelliJ IDEA. * User: piyushd * Date: 3/26/11 * Time: 10:53 PM * To change this template use File | Settings | File Templates. */ public class TaskA { int[] par; int[] limit; int getParent(int v) { int parent = par[v]; if(parent == v) return v; return par[v] = getParent(parent); } void join(int a, int b, int lim) { int pa = getParent(a); int pb = getParent(b); if(pa == pb) return; par[pa] = pb; int minLimit = Math.min(lim, Math.min(limit[pa], limit[pb])); limit[pa] = limit[pb] = minLimit; } class Pair implements Comparable<Pair>{ int tank; int tap; int diameter; Pair(int tank, int tap, int diameter) { this.tank = tank; this.tap = tap; this.diameter = diameter; } public int compareTo(Pair o) { return tank - o.tank; } } void run(){ int N = nextInt(), P = nextInt(); par = new int[N]; limit = new int[N]; for(int i = 0; i < N; i++) { par[i] = i; limit[i] = Integer.MAX_VALUE; } int[] degree = new int[N]; for(int i = 0; i < P; i++) { int a = nextInt() - 1; int b = nextInt() - 1; int d = nextInt(); degree[a]++; degree[b]--; join(a, b, d); } ArrayList<Integer> tank = new ArrayList<Integer>(); ArrayList<Integer> tap = new ArrayList<Integer>(); for(int i = 0; i < N; i++) { if(degree[i] > 0) tank.add(i); if(degree[i] < 0) tap.add(i); } ArrayList<Pair> solutions = new ArrayList<Pair>(); for(int i = 0; i < tank.size(); i++) { for(int j = 0; j < tap.size(); j++) { if(getParent(tank.get(i)) == getParent(tap.get(j))) { solutions.add(new Pair(tank.get(i), tap.get(j), limit[getParent(tank.get(i))])); } } } Collections.sort(solutions); System.out.println(solutions.size()); for(Pair x : solutions) { System.out.println((x.tank + 1) + " " + (x.tap + 1) + " " + x.diameter); } } int nextInt(){ try{ int c = System.in.read(); if(c == -1) return c; while(c != '-' && (c < '0' || '9' < c)){ c = System.in.read(); if(c == -1) return c; } if(c == '-') return -nextInt(); int res = 0; do{ res *= 10; res += c - '0'; c = System.in.read(); }while('0' <= c && c <= '9'); return res; }catch(Exception e){ return -1; } } long nextLong(){ try{ int c = System.in.read(); if(c == -1) return -1; while(c != '-' && (c < '0' || '9' < c)){ c = System.in.read(); if(c == -1) return -1; } if(c == '-') return -nextLong(); long res = 0; do{ res *= 10; res += c-'0'; c = System.in.read(); }while('0' <= c && c <= '9'); return res; }catch(Exception e){ return -1; } } double nextDouble(){ return Double.parseDouble(next()); } String next(){ try{ StringBuilder res = new StringBuilder(""); int c = System.in.read(); while(Character.isWhitespace(c)) c = System.in.read(); do{ res.append((char)c); }while(!Character.isWhitespace(c=System.in.read())); return res.toString(); }catch(Exception e){ return null; } } String nextLine(){ try{ StringBuilder res = new StringBuilder(""); int c = System.in.read(); while(c == '\r' || c == '\n') c = System.in.read(); do{ res.append((char)c); c = System.in.read(); }while(c != '\r' && c != '\n'); return res.toString(); }catch(Exception e){ return null; } } public static void main(String[] args){ new TaskA().run(); } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
03769f667ff3de5f0814ea6a4f1d4e8e
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * @author Ivan Pryvalov (ivan.pryvalov@gmail.com) */ public class Codeforces_Round83_Div2_C implements Runnable{ /* * new ByteScanner(scanner.nextLineAsArray()).nextInt() */ private void solve() throws IOException { int n = scanner.nextInt(); // [1,10^3] int p = scanner.nextInt(); // [0,n] int[] d = new int[p]; int[] a = new int[p]; int[] b = new int[p]; for (int i = 0; i < p; i++) { a[i] = scanner.nextInt(); b[i] = scanner.nextInt(); d[i] = scanner.nextInt(); //[1,10^6] } int[][] e = new int[n+1][2]; for (int i = 0; i <= n; i++) { Arrays.fill(e[i], -1); } G1 = new int[n+1][n+1]; // 10^6 for (int i = 0; i < p; i++) { int x = a[i]; int y = b[i]; e[x][1] = y; e[y][0] = x; G1[x][y] = d[i]; } List<Data> list = new ArrayList<Data>(); boolean[] used = new boolean[n+1]; for (int i = 1; i <= n; i++) { if (!used[i]){ int u = i; if (e[u][1]!=-1 && e[u][0]==-1){ Data dat = new Data(); dat.tank = u; dat.diam = G1[u][e[u][1]]; used[u] = true; u = e[u][1]; while (e[u][1]!=-1){ dat.diam = Math.min(dat.diam, G1[u][e[u][1]]); u = e[u][1]; used[u] = true; } dat.tap = u; list.add(dat); } } } Collections.sort(list, new Comparator<Data>() { @Override public int compare(Data o1, Data o2) { return o1.tank-o2.tank; } }); out.println(list.size()); for (int i = 0; i < list.size(); i++) { out.println(list.get(i).print()); } } static class Data{ int tank, tap, diam; public String print() { return tank+" "+tap+" "+diam; } } int n; int[][] G1; ///////////////////////////////////////////////// final int BUF_SIZE = 1024 * 1024 * 8;//important to read long-string tokens properly final int INPUT_BUFFER_SIZE = 1024 * 1024 * 8 ; final int BUF_SIZE_INPUT = 1024; final int BUF_SIZE_OUT = 1024; boolean inputFromFile = false; String filenamePrefix = "A-small-attempt0"; String inSuffix = ".in"; String outSuffix = ".out"; //InputStream bis; //OutputStream bos; PrintStream out; ByteScanner scanner; ByteWriter writer; @Override public void run() { try{ InputStream bis = null; OutputStream bos = null; //PrintStream out = null; if (inputFromFile){ File baseFile = new File(getClass().getResource("/").getFile()); bis = new BufferedInputStream( new FileInputStream(new File( baseFile, filenamePrefix+inSuffix)), INPUT_BUFFER_SIZE); bos = new BufferedOutputStream( new FileOutputStream( new File(baseFile, filenamePrefix+outSuffix))); out = new PrintStream(bos); }else{ bis = new BufferedInputStream(System.in, INPUT_BUFFER_SIZE); bos = new BufferedOutputStream(System.out); out = new PrintStream(bos); } scanner = new ByteScanner(bis, BUF_SIZE_INPUT, BUF_SIZE); writer = new ByteWriter(bos, BUF_SIZE_OUT); solve(); out.flush(); }catch (Exception e) { e.printStackTrace(); System.exit(1); } } public interface Constants{ final static byte ZERO = '0';//48 or 0x30 final static byte NINE = '9'; final static byte SPACEBAR = ' '; //32 or 0x20 final static byte MINUS = '-'; //45 or 0x2d final static char FLOAT_POINT = '.'; } public static class EofException extends IOException{ } public static class ByteWriter implements Constants { int bufSize = 1024; byte[] byteBuf = new byte[bufSize]; OutputStream os; public ByteWriter(OutputStream os, int bufSize){ this.os = os; this.bufSize = bufSize; } public void writeInt(int num) throws IOException{ int byteWriteOffset = byteBuf.length; if (num==0){ byteBuf[--byteWriteOffset] = ZERO; }else{ int numAbs = Math.abs(num); while (numAbs>0){ byteBuf[--byteWriteOffset] = (byte)((numAbs % 10) + ZERO); numAbs /= 10; } if (num<0) byteBuf[--byteWriteOffset] = MINUS; } os.write(byteBuf, byteWriteOffset, byteBuf.length - byteWriteOffset); } /** * Please ensure ar.length <= byteBuf.length! * * @param ar * @throws IOException */ public void writeByteAr(byte[] ar) throws IOException{ for (int i = 0; i < ar.length; i++) { byteBuf[i] = ar[i]; } os.write(byteBuf,0,ar.length); } public void writeSpaceBar() throws IOException{ byteBuf[0] = SPACEBAR; os.write(byteBuf,0,1); } } public static class ByteScanner implements Constants{ InputStream is; public ByteScanner(InputStream is, int bufSizeInput, int bufSize){ this.is = is; this.bufSizeInput = bufSizeInput; this.bufSize = bufSize; byteBufInput = new byte[this.bufSizeInput]; byteBuf = new byte[this.bufSize]; } public ByteScanner(byte[] data){ byteBufInput = data; bufSizeInput = data.length; bufSize = data.length; byteBuf = new byte[bufSize]; byteRead = data.length; bytePos = 0; } private int bufSizeInput; private int bufSize; byte[] byteBufInput; byte by=-1; int byteRead=-1; int bytePos=-1; static byte[] byteBuf; int totalBytes; boolean eofMet = false; private byte nextByte() throws IOException{ if (bytePos<0 || bytePos>=byteRead){ byteRead = is==null? -1: is.read(byteBufInput); bytePos=0; if (byteRead<0){ byteBufInput[bytePos]=-1;//!!! if (eofMet) throw new EofException(); eofMet = true; } } return byteBufInput[bytePos++]; } /** * Returns next meaningful character as a byte.<br> * * @return * @throws IOException */ public byte nextChar() throws IOException{ while ((by=nextByte())<=0x20); return by; } /** * Returns next meaningful character OR space as a byte.<br> * * @return * @throws IOException */ public byte nextCharOrSpacebar() throws IOException{ while ((by=nextByte())<0x20); return by; } /** * Reads line. * * @return * @throws IOException */ public String nextLine() throws IOException { readToken((byte)0x20); return new String(byteBuf,0,totalBytes); } public byte[] nextLineAsArray() throws IOException { readToken((byte)0x20); byte[] out = new byte[totalBytes]; System.arraycopy(byteBuf, 0, out, 0, totalBytes); return out; } /** * Reads token. Spacebar is separator char. * * @return * @throws IOException */ public String nextToken() throws IOException { readToken((byte)0x21); return new String(byteBuf,0,totalBytes); } /** * Spacebar is included as separator char * * @throws IOException */ private void readToken() throws IOException { readToken((byte)0x21); } private void readToken(byte acceptFrom) throws IOException { totalBytes = 0; while ((by=nextByte())<acceptFrom); byteBuf[totalBytes++] = by; while ((by=nextByte())>=acceptFrom){ byteBuf[totalBytes++] = by; } } public int nextInt() throws IOException{ readToken(); int num=0, i=0; boolean sign=false; if (byteBuf[i]==MINUS){ sign = true; i++; } for (; i<totalBytes; i++){ num*=10; num+=byteBuf[i]-ZERO; } return sign?-num:num; } public long nextLong() throws IOException{ readToken(); long num=0; int i=0; boolean sign=false; if (byteBuf[i]==MINUS){ sign = true; i++; } for (; i<totalBytes; i++){ num*=10; num+=byteBuf[i]-ZERO; } return sign?-num:num; } /* //TODO test Unix/Windows formats public void toNextLine() throws IOException{ while ((ch=nextChar())!='\n'); } */ public double nextDouble() throws IOException{ readToken(); char[] token = new char[totalBytes]; for (int i = 0; i < totalBytes; i++) { token[i] = (char)byteBuf[i]; } return Double.parseDouble(new String(token)); } } public static void main(String[] args) { new Codeforces_Round83_Div2_C().run(); } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
5f660ef45e44422be7d60be5fb5a7c25
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.awt.Point; import java.io.*; import java.util.Arrays; //BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public class p108c { public static void main(String[] args) throws Exception { p108cc in = new p108cc (System.in); int h = in.nextLong(); int p = in.nextLong(); boolean[] first = new boolean[h]; boolean[] second = new boolean[h]; Point[] all = new Point[h]; for (int i = 0; i < p; i++) { int a = in.nextLong()-1; int b = in.nextLong()-1; int c = in.nextLong(); all[a] = new Point(b,c); first[a] = true; second[b] = true; } boolean [] tank = new boolean [h]; for (int i = 0; i < tank.length; i++) { tank[i] = first[i] && (!second[i]); } boolean [] tap = new boolean [h]; for (int i = 0; i < tank.length; i++) { tap[i] = (!first[i]) && (second[i]); } int countTanks = 0; StringBuffer ress = new StringBuffer(); for (int i = 0; i < tank.length; i++) { if(tank[i]) { countTanks++; int now = i; int minD = 9999999; while(true) { int next = all[now].x; minD = Math.min(minD, all[now].y); now = next; if(all[now] == null) { ress = ress.append((i+1)+" "+(now+1)+" "+minD); ress = ress.append("\n"); break; } } } } if(countTanks == 0) { System.out.println(0); return; } else { System.out.println(countTanks); System.out.println(ress); } } } class p108cc { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public p108cc(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public int nextLong() throws Exception { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; return ret; } public char nextChar() throws Exception { char ret = ' '; byte c = read(); while (c <= ' ') c = read(); ret = (char)c; return ret; } public String nextString() throws Exception { StringBuffer ret = new StringBuffer(); byte c = read(); while (c <= ' ') c = read(); do { ret = ret.append((char)c); c = read(); } while (c > ' '); return ret.toString(); } private void fillBuffer() throws Exception { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws Exception { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
94b1af874e672dad81de7e64310515ab
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; /** * Author - * User: kansal * Date: 8/23/11 * Time: 8:40 PM */ public class DormWaterSupply { public static void main(String[] args) { reader = new BufferedReader(new InputStreamReader(System.in)); int houses = nextInt(), pipes = nextInt(); boolean[] in = new boolean[houses]; boolean[] out = new boolean[houses]; int[] D = new int[pipes]; int[] next = new int[houses]; int[] id = new int[houses]; Arrays.fill(id, -1); Arrays.fill(next, -1); for(int i = 0; i < pipes; ++i) { int a = nextInt() - 1; int b = nextInt() - 1; D[i] = nextInt(); out[a] = true; in[b] = true; next[a] = b; id[a] = i; } int tanks = 0; for(int i = 0; i < houses; ++i) { if (!in[i] && out[i]) ++tanks; } System.out.println(tanks); for(int i = 0; i < houses; ++i) { if (in[i] || !out[i]) continue; int maxDiameter = Integer.MAX_VALUE; int j = i; while (id[j] != -1) { maxDiameter = Math.min(maxDiameter, D[id[j]]); j = next[j]; } System.out.println(String.format("%d %d %d", i+1, j+1, maxDiameter)); } } public static BufferedReader reader; public static StringTokenizer tokenizer = null; static String nextToken() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } static public int nextInt() { return Integer.parseInt(nextToken()); } static public long nextLong() { return Long.parseLong(nextToken()); } static public String next() { return nextToken(); } static public String nextLine() { try { return reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return null; } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
105e3f1acfee6ee367170bc668dc9d97
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; public class C { private static StreamTokenizer in; private static PrintWriter out; private static BufferedReader inB; private static int nextInt() throws Exception{ in.nextToken(); return (int)in.nval; } private static String nextString() throws Exception{ in.nextToken(); return in.sval; } static{ inB = new BufferedReader(new InputStreamReader(System.in)); in = new StreamTokenizer(inB); out = new PrintWriter(System.out); } private static int n,p; private static int[][] tr; private static int[][] mas; private static boolean used[]; private static int curb; private static ArrayList<int[]> ans = new ArrayList<int[]>(); public static void main(String[] args)throws Exception { n = nextInt(); p = nextInt(); tr = new int[n][2]; mas = new int[p][]; used = new boolean[n]; for(int i = 0;i<n; i++)tr[i][0] = tr[i][1] = -1; for(int i = 0; i<p; i++) { int a = nextInt()-1, b = nextInt()-1; int d = nextInt(); tr[a][1] = i; tr[b][0] = i; mas[i] = new int[] {a, b, d}; } for(int i = 0;i<n; i++) { if(used[i])continue; if(tr[i][0] != -1)continue; curb = i; dfs(i, Integer.MAX_VALUE); } int t = ans.size(); out.println(t); int[][] an = new int[t][]; int i = 0; for(int[] cur : ans) { an[i++] = cur; } Arrays.sort(an, new Comparator<int[]>() { public int compare(int[] mas1, int[] mas2) { return mas1[0] - mas2[0]; } }); for(int j = 0; j<t; j++) { out.println(an[j][0] + 1 + " " + (an[j][1] + 1) + " " + (an[j][2])); } out.flush(); } private static void dfs(int cur, int min) { if(used[cur])return; used[cur] = true; int ind = tr[cur][1]; if(ind == -1) { if(min == Integer.MAX_VALUE)return; ans.add(new int[] {curb, cur, min}); return; } int[] a = mas[ind]; int curm = Math.min(min, a[2]); dfs(a[1], curm); } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
95fbedad276a27636fea33255692a8f1
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.Arrays; import java.util.Scanner; /** * * @author joshuahm */ public class CodeForce83C { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int p=in.nextInt(); int[] outhouse=new int[n+1]; int[] diam=new int[n+1]; boolean[] inhouse=new boolean[n+1]; Arrays.fill(inhouse, false); Arrays.fill(outhouse, -1); Arrays.fill(diam, 20000000); for(int i=0;i<p;i++){ int temp=in.nextInt(); int temp2=in.nextInt(); int temp3=in.nextInt(); outhouse[temp]=temp2; inhouse[temp2]=true; diam[temp]=temp3; } String OUT=""; int count=0; for(int i=1;i<n+1;i++){ int min=20000000; int temp=i; if(!inhouse[i] && outhouse[i]!=-1){ while(true){ if(min>diam[temp]) min=diam[temp]; if(outhouse[temp]==i) break; if(outhouse[temp]==-1){ String S=""+i+" "+temp+" "+min+"\n"; OUT+=S; count++; break; } temp=outhouse[temp]; } } } System.out.println(count); System.out.println(OUT); } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
16557fbfa1d950f43ad894932d9f2536
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Scanner; public class C { private void processInput() throws IOException { Scanner in = new Scanner(System.in); int n = in.nextInt(); int p = in.nextInt(); next = new int[n]; back = new int[n]; Arrays.fill(next, -1); Arrays.fill(back, -1); int[][] adj = new int[n][n]; for (int i = 0; i < p; i++) { int a = in.nextInt(); int b = in.nextInt(); int d = in.nextInt(); adj[a-1][b-1] = d; adj[b-1][a-1] = d; next[a-1] = b-1; back[b-1] = a-1; } go(n, adj); in.close(); } int[] next; int[] back; void go(int n, int[][] adj) { boolean[] vis = new boolean[n]; int t = 0; List<Integer> src = new ArrayList<Integer>(); boolean[] vv = new boolean[n]; OUT: for (int i = 0; i < n; i++) { Arrays.fill(vv, false); int pos = i; vv[pos] = true; while (back[pos] != -1) { pos = back[pos]; if (vv[pos]) { continue OUT; } vv[pos] = true; } if (!vis[pos] && i != pos) { vis[pos] = true; t++; src.add(pos); } } Collections.sort(src); int [] cap = new int[t]; int [] dest = new int[t]; Arrays.fill(cap, Integer.MAX_VALUE); for (int i = 0; i < t; i++) { int pos = src.get(i); while (next[pos] != -1) { cap[i] = Math.min(cap[i], adj[pos][next[pos]]); dest[i] = next[pos]; pos = next[pos]; } } System.out.printf(Locale.ENGLISH, "%d\n", t); for (int i = 0 ; i < cap.length; i++) { System.out.printf(Locale.ENGLISH, "%d %d %d\n", src.get(i) + 1, dest[i] + 1, cap[i]); } } public static void main(String[] args) throws Exception { C a = new C(); a.processInput(); } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
ea2ade101590100709f8470db79d9595
train_003.jsonl
1314111600
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
256 megabytes
import java.io.*; import java.util.Arrays; public class round83C { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = 0, p = 0; String[] use = br.readLine().split(" "); n = Integer.parseInt(use[0]); p = Integer.parseInt(use[1]); int[] diameter = new int[n + 100]; int[] in = new int[n + 100]; int[] out = new int[n + 100]; Arrays.fill(in, -1); Arrays.fill(out, -1); for (int i = 0; i < p; ++i) { use = br.readLine().split(" "); int a = Integer.parseInt(use[0]); int b = Integer.parseInt(use[1]); int d = Integer.parseInt(use[2]); diameter[a] = d; out[a] = b; in[b] = a; } int c = 0; for(int i = 1 ; i < out.length ; ++i){ if(out[i] != -1 && in[i] == -1)c++; } System.out.println(c); for (int i = 1; i < out.length; ++i) { if (out[i] != -1 && in[i] == -1) { int cur = out[i]; int min = diameter[i]; while (out[cur] != -1) { min = Math.min(min, diameter[cur]); cur = out[cur]; } System.out.print(i + " " + cur + " " + min); System.out.println(); } } } }
Java
["3 2\n1 2 10\n2 3 20", "3 3\n1 2 20\n2 3 10\n3 1 5", "4 2\n1 2 60\n3 4 50"]
1 second
["1\n1 3 10", "0", "2\n1 2 60\n3 4 50"]
null
Java 6
standard input
[ "implementation", "dfs and similar", "graphs" ]
e83a8bfabd7ea096fae66dcc8c243be7
The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly. Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
1,400
Print integer t in the first line — the number of tank-tap pairs of houses. For the next t lines, print 3 integers per line, separated by spaces: tanki, tapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tanki and tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All the t lines should be ordered (increasingly) by tanki.
standard output
PASSED
3e086a2e0f3c8033ffef765885d48958
train_003.jsonl
1598798100
Ziota found a video game called "Monster Invaders".Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.For the sake of simplicity, we only consider two different types of monsters and three different types of guns.Namely, the two types of monsters are: a normal monster with $$$1$$$ hp. a boss with $$$2$$$ hp. And the three types of guns are: Pistol, deals $$$1$$$ hp in damage to one monster, $$$r_1$$$ reloading time Laser gun, deals $$$1$$$ hp in damage to all the monsters in the current level (including the boss), $$$r_2$$$ reloading time AWP, instantly kills any monster, $$$r_3$$$ reloading time The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.The levels of the game can be considered as an array $$$a_1, a_2, \ldots, a_n$$$, in which the $$$i$$$-th stage has $$$a_i$$$ normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the $$$a_i$$$ normal monsters.If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level $$$i$$$ $$$(1 &lt; i &lt; n)$$$ are levels $$$i - 1$$$ and $$$i + 1$$$, the only adjacent level of level $$$1$$$ is level $$$2$$$, the only adjacent level of level $$$n$$$ is level $$$n - 1$$$). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with $$$d$$$ teleportation time.In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation. Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
512 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Jaynil */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); EMonsterInvaders solver = new EMonsterInvaders(); solver.solve(1, in, out); out.close(); } static class EMonsterInvaders { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); long r1 = in.nextInt(); long r2 = in.nextInt(); long r3 = in.nextInt(); long d = in.nextInt(); long a[] = new long[n]; long dp[][] = new long[n][3]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); dp[0][0] = a[0] * r1 + r3; dp[0][1] = Math.min((a[0] + 1) * r1, (r2)); // dp[0][1] = Math.min(dp[0][0],dp[0][1]); // out.println(dp[0][0] +" " + dp[0][1]); long half = 0; for (int i = 1; i < n; i++) { half = Math.min((a[i] + 1) * r1, (r2)); dp[i][0] = a[i] * r1 + r3 + dp[i - 1][0] + d; dp[i][0] = Math.min(dp[i][0], half + 3 * d + dp[i - 1][1] + 2 * r1); if (i != n - 1) dp[i][0] = Math.min(dp[i][0], a[i] * r1 + r3 + dp[i - 1][1] + 3 * d + r1); else dp[i][0] = Math.min(dp[i][0], a[i] * r1 + r3 + dp[i - 1][1] + 2 * d + r1); // out.println(dp[i][0]); dp[i][1] = dp[i - 1][0] + half + d; dp[i][1] = Math.min(dp[i][1], dp[i - 1][1] + d + half + d + d + r1); // out.println(dp[i][0] +" " + dp[i][1]); } out.println(dp[n - 1][0]); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["4 1 3 4 3\n3 2 5 1", "4 2 4 4 1\n4 5 1 2"]
2 seconds
["34", "31"]
NoteIn the first test case, the optimal strategy is: Use the pistol to kill three normal monsters and AWP to kill the boss (Total time $$$1\cdot3+4=7$$$) Move to stage two (Total time $$$7+3=10$$$) Use the pistol twice and AWP to kill the boss (Total time $$$10+1\cdot2+4=16$$$) Move to stage three (Total time $$$16+3=19$$$) Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time $$$19+3+3=25$$$) Use the pistol once, use AWP to kill the boss (Total time $$$25+1\cdot1+4=30$$$) Move back to stage three (Total time $$$30+3=33$$$) Kill the boss at stage three with the pistol (Total time $$$33+1=34$$$) Note that here, we do not finish at level $$$n$$$, but when all the bosses are killed.
Java 11
standard input
[ "dp", "greedy" ]
b532deda90a4edc6e97b207cb05d3843
The first line of the input contains five integers separated by single spaces: $$$n$$$ $$$(2 \le n \le 10^6)$$$ — the number of stages, $$$r_1, r_2, r_3$$$ $$$(1 \le r_1 \le r_2 \le r_3 \le 10^9)$$$ — the reload time of the three guns respectively, $$$d$$$ $$$(1 \le d \le 10^9)$$$ — the time of moving between adjacent levels. The second line of the input contains $$$n$$$ integers separated by single spaces $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6, 1 \le i \le n)$$$.
2,300
Print one integer, the minimum time to finish the game.
standard output
PASSED
b811fc9f5eceb0bdcd57ea9b5865acef
train_003.jsonl
1402241400
Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1, y1), (x2, y2), ..., (xr, yr), that: r ≥ 2; for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds; each table cell, which belongs to the tube, must occur exactly once in the sequence. Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled: no pair of tubes has common cells; each cell of the table belongs to some tube. Help Valera to arrange k tubes on his rectangle table in a fancy manner.
256 megabytes
import java.util.Scanner; public class ValeraandTubes { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); solve(n, m, k); } private static void solve(int n, int m, int k) { int count = 0; int step = 1; int row = 1; int col = 1; for (; count < k - 1; ++count) { System.out.print("2 "); System.out.print(Integer.toString(row) + " " + Integer.toString(col)); col += step; if (col > m) { col = m; row++; step = -step; } else if (col < 1) { col = 1; row++; step = -step; } System.out.print(" " + Integer.toString(row) + " " + Integer.toString(col)); col += step; if (col > m) { col = m; row++; step = -step; } else if (col < 1) { col = 1; row++; step = -step; } System.out.println(); } int leftCells = n * m - (k - 1) * 2; System.out.print(leftCells); while (row <= n) { System.out.print(" " + Integer.toString(row) + " " + Integer.toString(col)); col += step; if (col > m) { col = m; row++; step = -step; } else if (col < 1) { col = 1; row++; step = -step; } } System.out.println(); } }
Java
["3 3 3", "2 3 1"]
1 second
["3 1 1 1 2 1 3\n3 2 1 2 2 2 3\n3 3 1 3 2 3 3", "6 1 1 1 2 1 3 2 3 2 2 2 1"]
NotePicture for the first sample: Picture for the second sample:
Java 7
standard input
[ "constructive algorithms", "implementation", "dfs and similar" ]
779e73c2f5eba950a20e6af9b53a643a
The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 300; 2 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly.
1,500
Print k lines. In the i-th line print the description of the i-th tube: first print integer ri (the number of tube cells), then print 2ri integers xi1, yi1, xi2, yi2, ..., xiri, yiri (the sequence of table cells). If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists.
standard output
PASSED
7f158a5c1618792ceae79697721f2bdd
train_003.jsonl
1402241400
Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1, y1), (x2, y2), ..., (xr, yr), that: r ≥ 2; for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds; each table cell, which belongs to the tube, must occur exactly once in the sequence. Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled: no pair of tubes has common cells; each cell of the table belongs to some tube. Help Valera to arrange k tubes on his rectangle table in a fancy manner.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class C implements Runnable { private void solve() throws IOException { int n = nextInt(); int m = nextInt(); int k = nextInt(); int state = 1; int i = n - 1; int j = m - 1; int lastI = n - 1, lastJ = m - 1; HashSet<String> reserved = new HashSet<>(); for (int K = 1; K < k; K++) { ArrayList<String> tube = new ArrayList<>(); while (true) { tube.add((i + 1) + " " + (j + 1)); reserved.add((i + 1) + " " + (j + 1)); if (state == 1) { --j; if (j < 0) { --i; state = 2; j = 0; } } else { ++j; if (j >= m) { --i; state = 1; j = m - 1; } } tube.add((i + 1) + " " + (j + 1)); reserved.add((i + 1) + " " + (j + 1)); if (state == 1) { --j; if (j < 0) { --i; state = 2; j = 0; } } else { ++j; if (j >= m) { --i; state = 1; j = m - 1; } } lastI = i; lastJ = j; break; } System.out.print(tube.size() + " "); for (String ans : tube) { System.out.print(ans + " "); } System.out.println(); } ArrayList<String> lastTube = new ArrayList<>(); int r = lastI, c = lastJ, cnt = 0; while (cnt++ < n * m) { if (reserved.contains((r + 1) + " " + (c + 1))) continue; if (r < 0 || c < 0) continue; lastTube.add((r + 1) + " " + (c + 1)); if (state == 1) { --c; if (c < 0) { --r; state = 2; c = 0; } } else { ++c; if (c >= m) { --r; state = 1; c = m - 1; } } } System.out.print(lastTube.size() + " "); for (String ans : lastTube) { System.out.print(ans + " "); } System.out.println(); } public static void main(String[] args) { new C().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new BufferedReader( new InputStreamReader(System.in))); writer = new PrintWriter(System.out); tokenizer = null; solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } BigInteger nextBigInteger() throws IOException { return new BigInteger(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } void p(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } void pl(Object... objects) { p(objects); writer.println(); } int cc; void pf() { writer.printf("Case #%d: ", ++cc); } }
Java
["3 3 3", "2 3 1"]
1 second
["3 1 1 1 2 1 3\n3 2 1 2 2 2 3\n3 3 1 3 2 3 3", "6 1 1 1 2 1 3 2 3 2 2 2 1"]
NotePicture for the first sample: Picture for the second sample:
Java 7
standard input
[ "constructive algorithms", "implementation", "dfs and similar" ]
779e73c2f5eba950a20e6af9b53a643a
The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 300; 2 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly.
1,500
Print k lines. In the i-th line print the description of the i-th tube: first print integer ri (the number of tube cells), then print 2ri integers xi1, yi1, xi2, yi2, ..., xiri, yiri (the sequence of table cells). If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists.
standard output
PASSED
faa197dc2a9eec7cd506513b8ccb3ef4
train_003.jsonl
1402241400
Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1, y1), (x2, y2), ..., (xr, yr), that: r ≥ 2; for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds; each table cell, which belongs to the tube, must occur exactly once in the sequence. Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled: no pair of tubes has common cells; each cell of the table belongs to some tube. Help Valera to arrange k tubes on his rectangle table in a fancy manner.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } } class Task { public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); int x = n*m/k, last; if(x*k!=n*m) { last = n*m-(k-1)*x; if(last < 2) last+=x; } else last = x; int points = n*m, count = 0; boolean end = false; out.print(x); for(int i = 1; i <= n; ++i) { if(i%2 != 0) { for(int j = 1; j <= m; ++j) { out.print(" " + i + " " + j); points--; count++; if(!end && count == x && points >= x) { if(points == last) { end = true; out.print("\n" + last); } else out.print("\n" + x); count = 0; } } } else { for(int j = m; j >= 1; --j) { out.print(" " + i + " " + j); points--; count++; if(!end && count == x && points >= x) { if(points == last) { end = true; out.print("\n" + last); } else out.print("\n" + x); count = 0; } } } } } } 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
["3 3 3", "2 3 1"]
1 second
["3 1 1 1 2 1 3\n3 2 1 2 2 2 3\n3 3 1 3 2 3 3", "6 1 1 1 2 1 3 2 3 2 2 2 1"]
NotePicture for the first sample: Picture for the second sample:
Java 7
standard input
[ "constructive algorithms", "implementation", "dfs and similar" ]
779e73c2f5eba950a20e6af9b53a643a
The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 300; 2 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly.
1,500
Print k lines. In the i-th line print the description of the i-th tube: first print integer ri (the number of tube cells), then print 2ri integers xi1, yi1, xi2, yi2, ..., xiri, yiri (the sequence of table cells). If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists.
standard output
PASSED
959ff096d4bd6006482a09853a3ec95c
train_003.jsonl
1402241400
Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1, y1), (x2, y2), ..., (xr, yr), that: r ≥ 2; for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds; each table cell, which belongs to the tube, must occur exactly once in the sequence. Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled: no pair of tubes has common cells; each cell of the table belongs to some tube. Help Valera to arrange k tubes on his rectangle table in a fancy manner.
256 megabytes
import java.io.*; import java.util.*; public class Test { public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); int curI = 1; int curJ = 1; int direction = 1; PrintWriter pw = new PrintWriter(System.out); for (int i = 0; i < k - 1; i++) { pw.print("2 "); if((curI == m && direction > 0) || (curI == 1 && direction < 0)){ pw.print(curJ); pw.print(' '); pw.print(curI); pw.print(' '); curJ ++; pw.print(curJ); pw.print(' '); pw.println(curI); direction *= -1; } else { pw.print(curJ); pw.print(' '); pw.print(curI); pw.print(' '); curI += direction; pw.print(curJ); pw.print(' '); pw.println(curI); } if((curI == m && direction > 0) || (curI == 1 && direction < 0)){ curJ++; direction *= -1; } else curI += direction; } int t = n * m - 2 * (k - 1); pw.print(t); pw.print(' '); for(int i = 0; i < t; i ++ ){ pw.print(curJ); pw.print(' '); pw.print(curI); pw.print(' '); if((curI == m && direction > 0) || (curI == 1 && direction < 0)){ curJ++; direction *= -1; } else{ curI += direction; } } pw.flush(); } }
Java
["3 3 3", "2 3 1"]
1 second
["3 1 1 1 2 1 3\n3 2 1 2 2 2 3\n3 3 1 3 2 3 3", "6 1 1 1 2 1 3 2 3 2 2 2 1"]
NotePicture for the first sample: Picture for the second sample:
Java 7
standard input
[ "constructive algorithms", "implementation", "dfs and similar" ]
779e73c2f5eba950a20e6af9b53a643a
The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 300; 2 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly.
1,500
Print k lines. In the i-th line print the description of the i-th tube: first print integer ri (the number of tube cells), then print 2ri integers xi1, yi1, xi2, yi2, ..., xiri, yiri (the sequence of table cells). If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists.
standard output
PASSED
f491455dd36f1d64bc23ca3e7a77de42
train_003.jsonl
1402241400
Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1, y1), (x2, y2), ..., (xr, yr), that: r ≥ 2; for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds; each table cell, which belongs to the tube, must occur exactly once in the sequence. Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled: no pair of tubes has common cells; each cell of the table belongs to some tube. Help Valera to arrange k tubes on his rectangle table in a fancy manner.
256 megabytes
import java.util.*; public class Tubes { public static int nextX(int x, int y, int n, int m) { if (y == m && x % 2 == 1) { return x + 1; } else if (y == 1 && x % 2 == 0) { return x + 1; } return x; } public static int nextY(int x, int y, int n, int m) { if (x % 2 == 1) { if (y == m) { return y; } else { return y + 1; } } else { if (y == 1) { return 1; } else { return y - 1; } } } public static void main(String[] args) { Scanner rdr = new Scanner(System.in); int n = rdr.nextInt(); int m = rdr.nextInt(); int k = rdr.nextInt(); int area = n * m; int x = 1; int y = 1; StringBuilder bldr = new StringBuilder(); for (int i = 0; i < k - 1; i++) { int r = 2; bldr.append(r); for (int j = 0; j < r; j++) { bldr.append(' '); bldr.append(x); bldr.append(' '); bldr.append(y); int nx = nextX(x, y, n, m); int ny = nextY(x, y, n, m); x = nx; y = ny; } bldr.append('\n'); } int r = area - 2 * (k - 1); bldr.append(r); for (int j = 0; j < r; j++) { bldr.append(' '); bldr.append(x); bldr.append(' '); bldr.append(y); int nx = nextX(x, y, n, m); int ny = nextY(x, y, n, m); x = nx; y = ny; } bldr.append('\n'); System.out.print(bldr.toString()); } }
Java
["3 3 3", "2 3 1"]
1 second
["3 1 1 1 2 1 3\n3 2 1 2 2 2 3\n3 3 1 3 2 3 3", "6 1 1 1 2 1 3 2 3 2 2 2 1"]
NotePicture for the first sample: Picture for the second sample:
Java 7
standard input
[ "constructive algorithms", "implementation", "dfs and similar" ]
779e73c2f5eba950a20e6af9b53a643a
The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 300; 2 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly.
1,500
Print k lines. In the i-th line print the description of the i-th tube: first print integer ri (the number of tube cells), then print 2ri integers xi1, yi1, xi2, yi2, ..., xiri, yiri (the sequence of table cells). If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists.
standard output
PASSED
aa46d7615b653330621471cd72808855
train_003.jsonl
1402241400
Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1, y1), (x2, y2), ..., (xr, yr), that: r ≥ 2; for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds; each table cell, which belongs to the tube, must occur exactly once in the sequence. Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled: no pair of tubes has common cells; each cell of the table belongs to some tube. Help Valera to arrange k tubes on his rectangle table in a fancy manner.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.LinkedList; import java.util.PriorityQueue; public class hals { public static void main(String[] args)throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] in = br.readLine().split(" "); int n = Integer.parseInt(in[0]); int m= Integer.parseInt(in[1]); int k = Integer.parseInt(in[2]); int cnt = 1,temp=0; if(k>1) System.out.print("2 "); else System.out.print(n*m+" "); for(int i=1;i<=n;i++){ if(i%2==1){ for(int j=1;j<=m;j++){ System.out.print(i+" "+j+" "); temp++; if(temp==2 && cnt<k){ temp=0; cnt++; System.out.println(); if(cnt<k) System.out.print("2 "); else System.out.print(n*m-2*(k-1)+" "); } } } else{ for(int j=m;j>=1;j--){ System.out.print(i+" "+j+" "); temp++; if(temp==2 && cnt<k){ temp=0; cnt++; System.out.println(); if(cnt<k) System.out.print("2 "); else System.out.print(n*m-2*(k-1)+" "); } } } } } }
Java
["3 3 3", "2 3 1"]
1 second
["3 1 1 1 2 1 3\n3 2 1 2 2 2 3\n3 3 1 3 2 3 3", "6 1 1 1 2 1 3 2 3 2 2 2 1"]
NotePicture for the first sample: Picture for the second sample:
Java 7
standard input
[ "constructive algorithms", "implementation", "dfs and similar" ]
779e73c2f5eba950a20e6af9b53a643a
The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 300; 2 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly.
1,500
Print k lines. In the i-th line print the description of the i-th tube: first print integer ri (the number of tube cells), then print 2ri integers xi1, yi1, xi2, yi2, ..., xiri, yiri (the sequence of table cells). If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists.
standard output
PASSED
365aa08684837a2813d901c1afa888b4
train_003.jsonl
1402241400
Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1, y1), (x2, y2), ..., (xr, yr), that: r ≥ 2; for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds; each table cell, which belongs to the tube, must occur exactly once in the sequence. Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled: no pair of tubes has common cells; each cell of the table belongs to some tube. Help Valera to arrange k tubes on his rectangle table in a fancy manner.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class ValeraandTubes { public static void main(String[] args) throws IOException { ValeraandTubes main = new ValeraandTubes(); main.solve(); } void solve() throws IOException { Reader3 reader = new Reader3(); reader.Init(System.in); int n = reader.NextInt(); int m = reader.NextInt(); int numbOfTubes = reader.NextInt(); int size = (n * m) / numbOfTubes; List<Cell> datas = new ArrayList<Cell>(); Cell cell; for (int i = 0; i < n; i++) { if (i % 2 == 0) { for (int j = 0; j < m; j++) { cell = new Cell(i + 1, j + 1); datas.add(cell); } } else { for (int j = m - 1; j >= 0; j--) { cell = new Cell(i + 1, j + 1); datas.add(cell); } } } int j = 0; int nn = 0; for (int i = 0; i < numbOfTubes; i++) { if ((n * m) % numbOfTubes != 0 && i == numbOfTubes - 1) { size += (n * m) % numbOfTubes; } System.out.print(size); nn += size; for (; j < nn; j++) { System.out.print(" " + datas.get(j).x + " " + datas.get(j).y); } System.out.print("\n"); } } } class Cell { int x; int y; public Cell(int x1, int y1) { x = x1; y = y1; } } class Reader3 { static BufferedReader reader; static StringTokenizer tokenizer; static void Init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String Next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int NextInt() throws IOException { return Integer.parseInt(Next()); } static long NextLong() throws IOException { return Long.parseLong(Next()); } static Double NextDouble() throws IOException { return Double.parseDouble(Next()); } }
Java
["3 3 3", "2 3 1"]
1 second
["3 1 1 1 2 1 3\n3 2 1 2 2 2 3\n3 3 1 3 2 3 3", "6 1 1 1 2 1 3 2 3 2 2 2 1"]
NotePicture for the first sample: Picture for the second sample:
Java 7
standard input
[ "constructive algorithms", "implementation", "dfs and similar" ]
779e73c2f5eba950a20e6af9b53a643a
The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 300; 2 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly.
1,500
Print k lines. In the i-th line print the description of the i-th tube: first print integer ri (the number of tube cells), then print 2ri integers xi1, yi1, xi2, yi2, ..., xiri, yiri (the sequence of table cells). If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists.
standard output