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
bc9ad4c22c3ca47d644e52de7fce8f10
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.util.*; import java.io.*; public class File { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); DSU dsu = new DSU(); // True is character among the passwords. // We treat characters as numbers 0-25 instead. boolean[] charExists = new boolean[26]; for (int i = 0; i < n; i++) { String s = sc.next(); int root = (int) (s.charAt(0) - 'a'); charExists[root] = true; for (int j = 1; j < s.length(); j++) { int c = (int) (s.charAt(j) - 'a'); charExists[c] = true; dsu.union(root, c); } } Set<Integer> set = new HashSet<>(); for (int i = 0; i < 26; i++) { if (charExists[i]) { set.add(dsu.getParent(i)); } } System.out.println(set.size()); } static class DSU { int[] parents; public DSU() { parents = new int[26]; for (int i = 1; i < 26; i++) { parents[i] = i; } } public int getParent(int x) { if (x != parents[x]) parents[x] = getParent(parents[x]); return parents[x]; } public void union(int x, int y) { int xParent = getParent(x); int yParent = getParent(y); if (xParent < yParent) { parents[yParent] = xParent; } else if (yParent < xParent) { parents[xParent] = yParent; } } } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
07d0f76b64e611cb3cedae46cb7d47c7
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.io.*; import java.util.*; public class Graph { private int V; private LinkedList<Integer> adj[]; Graph(int v) { V = v; adj = new LinkedList[v]; for (int i=0; i<v; ++i) adj[i] = new LinkedList(); } void addEdge(int v, int w) { adj[v].add(w); adj[w].add(v); } void DFSUtil(int v,boolean visited[]) { visited[v] = true; Iterator<Integer> i = adj[v].listIterator(); while (i.hasNext()) { int n = i.next(); if (!visited[n]) DFSUtil(n,visited); } } int DFS() { int ans=0; boolean visited[] = new boolean[V]; for (int i=0; i<V; ++i) if (visited[i] == false) { if(adj[i].size()!=0) ans++; DFSUtil(i, visited); } return ans; } public static void main(String args[]) { Scanner sc=new Scanner(System.in); Graph g = new Graph(26); int N=sc.nextInt(); sc.nextLine(); int[][]A=new int[26][26]; for(int i=0;i<N;i++) { String s=sc.nextLine(); for(int j=0;j<s.length();j++) for(int k=0;k<s.length();k++) A[s.charAt(j)-97][s.charAt(k)-97]++; } for(int k=0;k<26;k++) for(int i=0;i<26;i++) if(A[k][i]!=0) g.addEdge(i,k); System.out.println(g.DFS()); } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
0ceef08b2005a5720a12550ad5c81e6b
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.io.*; import java.util.*; public class Main { private static FastReader f = new FastReader(); public static void main(String[] args) { int n = f.nextInt(); Dsu dsu = new Dsu(26); boolean[] vis = new boolean[26]; while (n-- > 0) { String s = f.next(); for(int i=0;i<s.length();i++) { vis[s.charAt(i)-'a'] = true; dsu.union(s.charAt(0)-'a',s.charAt(i)-'a'); } } HashSet<Integer> set = new HashSet<>(); for(int i=0;i<26;i++) { if(vis[i]) { set.add(dsu.getParent(i)); } } System.out.println(set.size()); } //dsu static class Dsu { int[] parent; int[] size; Dsu(int n) { parent = new int[n]; size = new int[n]; init(n); } private void init(int n) { for(int i=0;i<n;i++) { parent[i] = i; size[i] = i; } } int getParent(int i) { if(i == parent[i]) { return i; } return parent[i] = getParent(parent[i]); } void union(int a, int b) { a = getParent(a); b = getParent(b); if(a != b) { if(size[a] < size[b]) { int loc = b; b = a; a = loc; } parent[b] = a; size[a] += size[b]; } } } //fast input reader static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
75e93f6482922713f862ed2cca276260
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
//package com.example.myPackage; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class SecretPasswords { private static Scanner fs = new Scanner(System.in); private static List<Integer> graph[]; private static boolean isUsed[]; static void addEdge(int a,int b) { graph[a].add(b); graph[b].add(a); } private static long res =0; private static void dfs(int start) { isUsed[start] = true; for(int i : graph[start]) { if(!isUsed[i]) { dfs(i); } } } public static void main(String[] args) { int n = fs.nextInt(); graph = new ArrayList[n+27]; for(int i=0;i<n+27;i++) graph[i] = new ArrayList<>(); isUsed = new boolean[26+n+1]; for(int i=0;i<n;i++) { String in = fs.next(); for(int k =0;k<in.length();k++) { addEdge(i,n+in.charAt(k) -'a'); } } for(int i=n;i<n+26;i++) { if(!graph[i].isEmpty() && !isUsed[i]) { dfs(i); res++; } } System.out.println(res); } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
900f728b53b17bf39ac94e68943808ac
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; /************** * AUTHOR: AMAN KUMAR SINGH * * INSTITUITION: KALYANI GOVERNMENT ENGINEERING COLLEGE * **************/ public class lets_do { InputReader in; PrintWriter out; final long mod=1000000007; final int N=200005; int MAX_Ai=100005; public static void main(String[] args) throws java.lang.Exception{ new lets_do().run(); } void run() throws Exception{ in=new InputReader(System.in); out = new PrintWriter(System.out); int t = 1; while(t-->0){ solve(); } out.flush(); out.close(); } int n; int[] segtree = new int[2 * N]; void build_segtree(){ int i = 0; for(i = n - 1; i > 0; i--) segtree[i] = segtree[i << 1] + segtree[i << 1 | 1]; } void segtree_update(int ind, int val){ for(segtree[ind += n] = val; ind > 1; ind >>= 1) segtree[ind >> 1] = segtree[ind] + segtree[ind ^ 1]; } long segtree_query(int l, int r){ long res = 0; for(l += n, r += n; l < r; l >>= 1, r >>= 1){ if((l & 1) == 1) res += segtree[l++]; if((r & 1) == 1) res += segtree[--r]; } return res; } int search(int p){ int lo = 0, hi = n - 1; while(lo <= hi){ int mid = (lo + hi) >> 1; if(segtree_query(0, mid + 1) < p) lo = mid + 1; else hi = mid - 1; } return lo; } void solve(){ n = ni(); boolean[] p = new boolean[26]; DSU hola = new DSU(26); int i = 0; for(i = 0; i < n; i++){ String st = n(); for(int j = 0; j < st.length() - 1; j++) { hola.unite(st.charAt(j) - 'a', st.charAt(j + 1) - 'a'); } for(int j = 0; j < st.length(); j++) p[st.charAt(j) - 'a'] = true; } HashSet<Integer> set = new HashSet<>(); for(i = 0; i < 26; i++){ if(p[i]) set.add(hola.find(i)); } pn(set.size()); } //https://github.com/amanCoder110599/Competitive-Programming/blob/master/DSU.java class DSU{ int[] par, sz; long ans = 0; DSU(int n){ par = new int[n]; sz = new int[n]; int i = 0; for(i = 0; i < n; i++){ par[i] = i; sz[i] = 1; } } int find(int x){ if(par[x] != x) return par[x] = find(par[x]); return par[x]; } void unite(int x, int y){ int x_root = find(x); int y_root = find(y); if(x_root == y_root) return; if(sz[x_root] <= sz[y_root]){ par[x_root] = y_root; sz[y_root] += sz[x_root]; } else{ par[y_root] = x_root; sz[x_root] += sz[y_root]; } } } final Comparator<Tuple> com = new Comparator<Tuple>() { public int compare(Tuple t1, Tuple t2) { if(t2.x != t1.x) return Integer.compare(t2.x, t1.x); else return Integer.compare(t1.y, t2.y); } }; class Tuple{ int x, y, z; Tuple(int a, int b, int c){ x = a; y = b; z = c; } } long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} long gcd1(long a, long b){return (b==0)?a:gcd(b,a%b);} int bitcount(long n){return (n==0)?0:(1+bitcount(n&(n-1)));} void p(Object o){out.print(o);} void pn(Object o){out.println(o);} void pni(Object o){out.println(o);out.flush();} String n(){return in.next();} String nln(){return in.nextLine();} int ni(){return in.nextInt();} long nl(){return in.nextLong();} double nd(){return in.nextDouble();} long mul(long a,long b){ if(a>=mod)a%=mod; if(b>=mod)b%=mod; a*=b; if(a>=mod)a%=mod; return a; } long modPow(long a, long p){ long o = 1; while(p>0){ if((p&1)==1)o = mul(o,a); a = mul(a,a); p>>=1; } return o; } long add(long a, long b){ if(a>=mod)a%=mod; if(b>=mod)b%=mod; if(b<0)b+=mod; a+=b; if(a>=mod)a-=mod; return a; } class InputReader{ private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new UnknownError(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int peek() { if (numChars == -1) return -1; if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) return -1; } return buf[curChar]; } public void skip(int x) { while (x-- > 0) read(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextString() { return next(); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { StringBuffer buf = new StringBuffer(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } 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 boolean hasNext() { int value; while (isSpaceChar(value = peek()) && value != -1) read(); return value != -1; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
23683f1f1d9ec996471759d0f8231dfe
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; @SuppressWarnings("Duplicates") // author @mdazmat9 public class Main{ static Scanner sc = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int test = 1; // test = sc.nextInt(); for (int ind = 0; ind < test; ind++) { solve(); } out.flush(); } static void solve(){ int n=sc.nextInt(); rank=new int[26]; parent=new int[26]; for(int i=0;i<26;i++){ rank[i]=1; parent[i]=i; } int[] visited=new int[26]; for(int i=0;i<n;i++){ String s=sc.next(); int u=s.charAt(0)-'a'; for(char v:s.toCharArray()){ union(u,v-'a'); visited[v-'a']=1; } } HashSet<Integer>set=new HashSet<>(); int ans=0; for(int i=0;i<26;i++){ if(visited[i]==1) { int x = find(i); set.add(x); } } out.println(set.size()); } static int[] rank; static int[] parent; static int find(int x){ if(x!=parent[x]){ parent[x]=find(parent[x]); } return parent[x]; } static void union(int x,int y){ int xroot = find(x); int yroot = find(y); if (rank[xroot] < rank[yroot]) parent[xroot] = yroot; else if (rank[xroot] > rank[yroot]) parent[yroot] = xroot; else { parent[xroot] = yroot; rank[yroot]++; } } static int[] intarray(int n){ int [] a=new int[n];for(int i=0;i<n;i++)a[i]=sc.nextInt();return a; } static void sort(int[]a){ shuffle(a);Arrays.sort(a);} static void sort(long[]a){ shuffle(a);Arrays.sort(a);} static long[] longarray(int n){ long [] a=new long[n];for(int i=0;i<n;i++)a[i]=sc.nextLong();return a; } static ArrayList<Integer> intlist(int n){ArrayList<Integer> list=new ArrayList<>();for(int i=0;i<n;i++)list.add(sc.nextInt());return list; } static ArrayList<Long> longlist(int n){ArrayList<Long> list=new ArrayList<>();for(int i=0;i<n;i++)list.add(sc.nextLong());return list; } static int[][] int2darray(int n,int m){ int [][] a=new int[n][m];for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ a[i][j]=sc.nextInt(); } }return a; } static long[][] long2darray(int n,int m){ long [][] a=new long[n][m];for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ a[i][j]=sc.nextLong(); } }return a; } static char[][] char2darray(int n,int m){ char [][] a=new char[n][m];for(int i=0;i<n;i++){ String s=sc.next(); a[i]=s.toCharArray(); }return a; } static double pi=3.14159265358979323846264; public static double logb( double a, double b ) {return Math.log(a) / Math.log(b); } static long fast_pow(long a, long b,long abs) { if(b == 0) return 1L; long val = fast_pow(a, b / 2,abs); if(b % 2 == 0) return val * val % abs; else return val * val % abs * a % abs; } static long abs = (long)1e9 + 7; static void shuffle(int[] a) { int n = a.length;for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i));int tmp = a[i];a[i] = a[r];a[r] = tmp; } } static void shuffle(long[] a) { int n = a.length;for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i));long tmp = a[i];a[i] = a[r];a[r] = tmp; } } static long gcd(long a , long b) { if(b == 0) return a; return gcd(b , a % b); } } class Scanner { public BufferedReader reader; public StringTokenizer st; public Scanner(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); st = null; } public String next() { while (st == null || !st.hasMoreTokens()) { try { String line = reader.readLine(); if (line == null) return null; st = new StringTokenizer(line); } catch (Exception e) { throw (new RuntimeException()); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
fc85808ad9fc7f708edac0008fe30c7a
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.io.*; import java.util.*; /* */ public class A { static FastReader sc=null; static boolean leaf[]; public static void main(String[] args) { sc=new FastReader(); int n=sc.nextInt(); Set<Integer> set=new HashSet<>(); DU du=new DU(26); for(int i=0;i<n;i++) { char line[]=sc.nextLine().toCharArray(); int p=line[0]-'a'; set.add(p); for(int j=1;j<line.length;j++) { int q=line[j]-'a'; set.add(q); du.unify(p, q); } } int ans=du.comp; for(int i=0;i<26;i++) { if(!set.contains(i))ans--; } System.out.println(ans); } static class DU{ int id[]; int size; int comp; int sz[]; Map<Integer,ArrayList<Integer>> map=new HashMap<>(); DU(int size){ this.size=size; id=new int[size]; sz=new int[size]; for(int i=0;i<size;i++) { id[i]=i; sz[i]=1; } comp=size; } public int find(int p) { int root=p; while(id[root]!=root) root=id[root]; while(p!=root) { int next=id[p]; id[p]=root; p=next; } return root; } public boolean connected(int p,int q) { return id[p]==id[q]; } public int compsize(int p) { return sz[find(p)]; } public void unify(int p,int q) { int r1=find(p),r2=find(q); if(r1==r2)return; if(sz[r1]<sz[r2]) { sz[r2]+=sz[r1]; id[r1]=id[r2]; } else { sz[r1]+=sz[r2]; id[r2]=id[r1]; } comp--; } } static int[] reverse(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al,Collections.reverseOrder()); for(int i=0;i<a.length;i++)a[i]=al.get(i); return a; } static void print(long a[]) { for(long e:a) { System.out.print(e+" "); } System.out.println(); } static void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { 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]=sc.nextInt(); } return a; } } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
1b5011f28f624f6aa0d95dd70567d3f2
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.util.*; public class SecretPasswords2 { int n; String[] strs; int[] fa; boolean[][] contains; Map<Character, Set<Character>> map = new HashMap<Character, Set<Character>>(); public SecretPasswords2(int n, String[] strs) { this.n = n; this.strs = strs; this.fa = new int[n]; this.contains = new boolean[n][26]; } public void init() { for (int i = 0; i < n; i++) { fa[i] = i; } for (int i = 0; i < n; i++) { for (int j = 0; j < strs[i].length(); j++) { int k = strs[i].charAt(j) - 'a'; contains[i][k] = true; } } } public int find(int x) { if (fa[x] == x) { return x; } fa[x] = find(fa[x]); return fa[x]; } public int merge(int i, int j) { int x = find(i); int y = find(j); fa[y] = x; return x; } public int getNum() { init(); int tmpRoot = -1; for (int i = 0; i < 26; i++) { for (int j = 0; j < strs.length; j++) { if (contains[j][i]) { if (tmpRoot == -1) tmpRoot = fa[j]; else merge(tmpRoot, j); } } tmpRoot = -1; } Set<Integer> set = new HashSet<>(); for (int i = 0; i < n; i++) { set.add(find(i)); } return set.size(); } public static void main(String[] args) { int n; Scanner sc = new Scanner(System.in); n = sc.nextInt(); String[] strs = new String[n]; for (int i = 0; i < n; i++) { strs[i] = sc.next(); sc.nextLine(); } SecretPasswords2 sp = new SecretPasswords2(n, strs); System.out.println(sp.getNum()); } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
db8a1fee65e753bbcfe315bb14fbb109
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; import java.util.TreeSet; import com.sun.source.tree.Tree; import java.io.*; public class Main { static int fa[]; public static void main(String args[]) { Scanner sc= new Scanner (System.in); PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); int n=sc.nextInt(); fa=new int[n+26]; for(int i=0;i<n+26;i++) fa[i]=i; for(int i=0;i<n;i++) { String s=sc.next(); int len=s.length(); HashSet<Character> set=new HashSet<Character>(); for(int j=0;j<len;j++) { set.add(s.charAt(j)); } for(int a:set) { union(a-'a',i+26); } } int sum=0; for(int i=26;i<26+n;i++) {//System.out.println(fa[i]+" "); if(fa[i]==i) sum++; } System.out.println(sum); } public static int find(int x) { if(x==fa[x]) return x; return fa[x]=find(fa[x]); } public static void union(int a,int b) { int x=find(a); int y=find(b); fa[x]=y; } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
8b495099b5aef88e51db066e2e540e2b
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.util.Scanner; //import java.util.Arrays; public class D { public static int[] l = new int[] {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; public static boolean[] unique = new boolean[] {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.nextLine(); for (int i = 0; i < n; ++i) { char[] password = in.nextLine().toCharArray(); int cur = password[0] - 97; union(cur, cur); for (int j = 1; j < password.length; ++j) { int num = password[j] - 97; union(cur, num); cur = num; } } int count = 0; for (int p = 0; p < 26; ++p) { if (l[p] != -1 && !unique[find(p)]) { unique[find(p)] = true; ++count; } } //System.out.println(Arrays.toString(l)); //System.out.println(Arrays.toString(unique)); System.out.println(count); in.close(); } public static void union(int a, int b) { if (l[a] == -1) { l[a] = a; } l[b] = l[a]; } public static int find(int a) { if (l[a] == a) { return a; } l[a] = find(l[a]); return l[a]; } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
195db00575f9836c5ae0f8adfb94fb01
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.util.Scanner; import java.util.ArrayList; import java.util.Vector; public class Prog1263D { static Scanner sc = new Scanner(System.in); private static void solve(){ int n = sc.nextInt(); ArrayList<Integer> graphs = new ArrayList<Integer>(); graphs.ensureCapacity(n); for (int i = 0 ; i < n ; i++){ String st = sc.next(); int cur = 0; for (int j = 0 ; j < st.length() ; j++){ char ch = st.charAt(j); cur = cur | (1<<(ch-97)); } graphs.add(cur); } Vector<Integer> vec = new Vector<Integer>(); for (int i = 0 ; i < n ; i++){ int last = graphs.remove(graphs.size() - 1); boolean br = false; for (int j = 0 ; j < graphs.size() ; j++){ if ((last & graphs.get(j)) != 0){ graphs.set(j,(last | graphs.get(j))); br = true; break; } } if (!br){ vec.add(last); } } System.out.println(vec.size()); } public static void main(String[] args) { solve(); sc.close(); } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
3cade5d9faacc3e92d1af985bbcd9a7e
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.util.*;import java.io.*; public class Main { public static void process()throws IOException { int n=ni(); Graph g=new Graph(26); while(n-- > 0){ String s=nln(); char u=s.charAt(0); g.flag[u-'a']=true; for(int i=1;i<s.length();i++){ char c=s.charAt(i); g.addEdge(u-'a',c-'a'); } } pn(g.dfs()); } static class Graph{ LinkedList<Integer> l[]; boolean visited[]; int V; HashSet<Integer> arr[]; boolean flag[]; public Graph(int v){ V=v; l=new LinkedList[V]; visited=new boolean[V]; arr=new HashSet[V]; flag=new boolean[V]; for(int i=0;i<V;i++){ l[i]=new LinkedList<Integer>(); visited[i]=false; arr[i]=new HashSet<Integer>(); flag[i]=false; } } void addEdge(int u,int v){ if(!arr[u].contains(v) || !arr[v].contains(u)){ arr[u].add(v); arr[v].add(u); l[u].add(v); l[v].add(u); flag[u]=true; flag[v]=true; } } int dfs(){ int res=0; for(int i=0;i<V;i++){ if(!visited[i] && flag[i]){ dfs_helper(i); res++; } } return res; } void dfs_helper(int node){ visited[node]=true; LinkedList<Integer> x=l[node]; for(Integer n : x){ if(!visited[n]) dfs_helper(n); } } } static FastReader sc=new FastReader(); //static AnotherReader sc; public static void main(String[]args)throws IOException { //sc=new AnotherReader(); int t=1; while(t-->0) process(); System.out.flush(); System.out.close(); } static boolean multipleTC=false; static void pn(Object o){System.out.println(o);} static void p(Object o){System.out.print(o);} static void pni(Object o){System.out.println(o);System.out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// static class FastReader{BufferedReader br;StringTokenizer st;public FastReader() {br=new BufferedReader(new InputStreamReader(System.in));}String next() {while(st==null||!st.hasMoreElements()) {try{st=new StringTokenizer(br.readLine());} catch(IOException e){e.printStackTrace();}} return st.nextToken();}int nextInt() {return Integer.parseInt(next());}long nextLong() {return Long.parseLong(next());}double nextDouble() {return Double.parseDouble(next());} String nextLine(){String str="";try {str=br.readLine();}catch(IOException e) {e.printStackTrace();}return str;}} static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
4c9ff69cd73c2126623ff85781860d09
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Indrajit Sinha */ 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); DSecretPasswords solver = new DSecretPasswords(); solver.solve(1, in, out); out.close(); } static class DSecretPasswords { int n; PrintWriter out; InputReader in; public void solve(int testNumber, InputReader in, PrintWriter out) { int t, i, j, tt, k; this.out = out; this.in = in; n = ni(); DisjointUnionSets dsu = new DisjointUnionSets(n); int ind[] = new int[26]; Arrays.fill(ind, -1); ; for (i = 0; i < n; i++) { String ss = in.next(); for (j = 0; j < ss.length(); j++) { int ch = ss.charAt(j) - 'a'; if (ind[ch] == -1) { ind[ch] = i; } else dsu.union(i, ind[ch]); } } int ans = 0; for (i = 0; i < n; i++) { if (dsu.find(i) == i) ans++; } pn(ans); } int ni() { return in.nextInt(); } void pn(int zx) { out.println(zx); } class DisjointUnionSets { int[] size; int[] parent; int n; public DisjointUnionSets(int n) { size = new int[n]; parent = new int[n]; this.n = n; makeSet(); } void makeSet() { for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) { return; } if (size[xRoot] < size[yRoot]) { parent[xRoot] = yRoot; size[yRoot] += size[xRoot]; } else if (size[yRoot] < size[xRoot]) { parent[yRoot] = xRoot; size[xRoot] += size[yRoot]; } else { parent[yRoot] = xRoot; size[xRoot] += size[yRoot]; } } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new UnknownError(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(next()); } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
63fa786f9cfa54a6cd9342a0681e70c1
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; import static java.lang.Math.*; public class Main { int[] parent; int[] size; int ans; int get(int a) { if (a == parent[a]) return a; return parent[a] = get(parent[a]); } void unite(int a, int b) { a = get(a); b = get(b); ans--; if (size[a] > size[b]) { size[a] += size[b]; parent[b] = a; } else { size[b] += size[a]; parent[a] = b; } } void run() throws IOException { int n = nextInt(); TreeMap<Character, Integer> tm = new TreeMap<>(); char[][] a = new char[n][]; for (int i = 0; i < a.length; i++) { a[i] = next().toCharArray(); } parent = new int[n]; size = new int[n]; ans = n; Arrays.fill(size, 1); for (int i = 0; i < parent.length; i++) { parent[i] = i; } for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (!tm.containsKey(a[i][j])) tm.put(a[i][j], i); else if (get(i) != get(tm.get(a[i][j]))) unite(i, tm.get(a[i][j])); } } pw.println(ans); pw.close(); } class point implements Comparable<point> { int x; int col; public point(int a, int b) { x = a; col = b; } @Override public int compareTo(point o) { if (o.col == this.col) return Integer.compare(o.x, this.x); return -Integer.compare(o.col, this.col); } } long mod = (long) 1e9 + 7; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //BufferedReader br = new BufferedReader(new FileReader("qual.in")); StringTokenizer st = new StringTokenizer(""); PrintWriter pw = new PrintWriter(System.out); //PrintWriter pw = new PrintWriter("qual.out"); int nextInt() throws IOException { return Integer.parseInt(next()); } String next() throws IOException { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } public Main() throws FileNotFoundException { } public static void main(String[] args) throws IOException { new Main().run(); } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
f387e1003421abf0520c5cc6d7ea8ca7
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class GFG { static class edge{ int s,d,w; public edge(int s,int d,int w) { this.s=s; this.d=d; this.w=w; } } static class graph{ int v; LinkedList<edge> li[]; boolean visit[]; boolean visit1[]; int arr[]; int s[]=new int[210000]; int cnt=1,c=0; public graph(int v) { this.v=v; visit=new boolean[v]; visit1=new boolean[v]; arr=new int[v]; li=new LinkedList[v]; for(int i=0;i<v;i++) li[i]=new LinkedList<>(); } public void addedge(int x,int y,int w) { edge e=new edge(x,y,w); li[x].add(e); edge e1=new edge(y,x,w); li[y].add(e1); } public void removeedge(int x,int y,int w) { Iterator<edge> it =li[x].iterator(); while (it.hasNext()) { if (it.next().d == y) { it.remove(); break; } } Iterator<edge> it1 =li[y].iterator(); while (it1.hasNext()) { if (it1.next().d == x) { it1.remove(); return; } } } public boolean hasEdge(int i, int j) { return li[i].contains(j); } public boolean isbipartite(int s) { LinkedList<Integer> q=new LinkedList<Integer>(); int arr[]=new int[v]; Arrays.fill(arr,-1); Arrays.fill(visit1,false); visit1[s]=true; q.add(s); arr[s]=1; int f=1; while(q.size()!=0) { s=q.poll(); Iterator<edge> i=li[s].listIterator(); while(i.hasNext()){int n=i.next().d; if(arr[n]==arr[s]) {f=0;break;} if(!visit1[n]){visit1[n]=true; q.add(n); if(arr[n]==-1) arr[n]=(arr[s]==1)?0:1;} } } if(f==0) return false; int c=0,d=0; for(int i=0;i<v;i++) { if(arr[i]==1) c++; if(arr[i]==0) d++; } System.out.println(c); for(int i=0;i<v;i++) { if(arr[i]==1) System.out.print((i+1)+" "); } System.out.println(); System.out.println(d); for(int i=0;i<v;i++) { if(arr[i]==0) System.out.print((i+1)+" "); } System.out.println(); return true; } public void dfs(int s) { visit[s]=true; cnt=cnt+li[s].size()-c; Iterator<edge> i=li[s].listIterator(); while(i.hasNext()){int n=i.next().d; if(!visit[n]) dfs(n); } } public void print() { System.out.println(cnt); } public int bfs(int s) { if(visit[s]==true) return 0; LinkedList<Integer> q=new LinkedList<Integer>(); visit[s]=true; q.add(s); int c=1; while(q.size()!=0){ s=q.poll(); Iterator<edge> i=li[s].listIterator(); while(i.hasNext()){int n=i.next().d; if(!visit[n]){visit[n]=true; q.add(n); } } } //System.out.println(tmp); return 1; } } public static void main (String[] args)throws IOException { //Reader sc=new Reader(); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); graph g=new graph(n); String arr[]=new String[n]; for(int i=0;i<n;i++) arr[i]=sc.next(); for(char p='a';p<='z';p++){ ArrayList<Integer> h=new ArrayList<Integer>(); for(int i=0;i<n;i++) { for(int j=0;j<arr[i].length();j++) { if(arr[i].charAt(j)==p){ h.add(i);break;} } } for(int j=0;j<h.size()-1;j++) { if(!g.hasEdge(h.get(j),h.get(j+1))) g.addedge(h.get(j),h.get(j+1),0); } } int c=0; for(int i=0;i<n;i++) c=c+g.bfs(i); //g.print(); System.out.println(c); } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
c19c7bf39fdfeabab12462941b3c09d0
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.io.*; import java.util.ArrayList; public class competetive { static ArrayList<ArrayList<Integer>>l=new ArrayList<>(); static boolean[]vis=new boolean[100001]; static int[]dis=new int[100001]; static int[]fdis=new int[100001]; static int ans=0; static void dfs(int node){ vis[node]=true; for(int child:l.get(node)){ if(!vis[child]){ dfs(child); } } } public static void main(String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512); int n=Integer.parseInt(br.readLine()); l.clear(); for(int i=0;i<=26;i++){ l.add(i,new ArrayList<>()); vis[i]=false; } int mx[][]=new int[27][27]; for(int i=0;i<n;i++){ String pass=br.readLine(); for(int j=0;j<pass.length();j++){ for(int k=j;k<pass.length();k++){ int a=(int)pass.charAt(j)-96; int b=(int)pass.charAt(k)-96; mx[a][a]=1; mx[b][b]=1; if(a!=b&&mx[a][b]==0){ l.get(a).add(b); l.get(b).add(a); mx[a][b]=1; mx[b][a]=1; } } } } for(int i=1;i<27;i++){ if(!vis[i]&&mx[i][i]==1){ dfs(i); ans++; } } out.write(String.valueOf(ans)); out.flush(); } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
e0046be00cada91d8cf6bfeac683ce7b
train_003.jsonl
1575038100
One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters.Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashSet; import java.util.Iterator; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DSecretPasswords solver = new DSecretPasswords(); solver.solve(1, in, out); out.close(); } static class DSecretPasswords { public void solve(int testNumber, FastReader s, PrintWriter out) { DSecretPasswords.UnionFindDisjointSet uds = new DSecretPasswords.UnionFindDisjointSet(26); int n = s.nextInt(); HashSet<Integer> set = new HashSet<>(); HashSet<Integer> allChars = new HashSet<>(); for (int i = 0; i < n; i++) { String str = s.nextString(); if (str.length() == 1) { allChars.add(str.charAt(0) - 'a'); } for (int j = 0; j < str.length() - 1; j++) { uds.union(str.charAt(j) - 'a', str.charAt(j + 1) - 'a'); allChars.add(str.charAt(j) - 'a'); allChars.add(str.charAt(j + 1) - 'a'); } } Iterator<Integer> iter = allChars.iterator(); while (iter.hasNext()) { int num = iter.next(); set.add(uds.root(num)); } out.println(set.size()); } private static class UnionFindDisjointSet { int[] parent; int[] size; int n; int size1; public UnionFindDisjointSet(int n) { this.n = n; this.parent = new int[n]; this.size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; } for (int i = 0; i < n; i++) { size[i] = 1; } this.size1 = n; } private int root(int b) { if (parent[b] != b) { return parent[b] = root(parent[b]); } return b; } private void union(int a, int b) { int rootA = root(a); int rootB = root(b); if (rootA == rootB) { return; } if (size[rootA] < size[rootB]) { parent[rootA] = parent[rootB]; size[rootB] += size[rootA]; } else { parent[rootB] = parent[rootA]; size[rootA] += size[rootB]; } size1--; // System.out.println(Arrays.toString(parent)); } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["4\na\nb\nab\nd", "3\nab\nbc\nabc", "1\ncodeforces"]
1 second
["2", "1", "1"]
NoteIn the second example hacker need to use any of the passwords to access the system.
Java 11
standard input
[ "dsu", "dfs and similar", "graphs" ]
00db0111fd80ce1820da2af07a6cb8f1
The first line contain integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters.
1,500
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
standard output
PASSED
7246fd5f3cc7ee74b1164a16ba2671eb
train_003.jsonl
1349105400
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
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 code{ public static void main (String[] args) throws java.lang.Exception { int[] arr = new int[1000100]; int[] fail = new int[1000100]; // System.out.println(fail[8]); for(int i=2;i<1000100;i++){ if(fail[i]==0){ arr[i]=1; { for(int j=2;i*j<1000100;j++) fail[i*j]=1; } } } Scanner obj = new Scanner(System.in); int n = obj.nextInt(); for(int i=0;i<n;i++){ long num = obj.nextLong(); //System.out.println(arr[k]); double sr = Math.sqrt(num); int k = (int) sr; //System.out.println(arr[k]); if(sr-Math.floor(sr)==0 && arr[k]==1) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["3\n4 5 6"]
2 seconds
["YES\nNO\nNO"]
NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".
Java 11
standard input
[ "binary search", "number theory", "implementation", "math" ]
6cebf9af5cfbb949f22e8b336bf07044
The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier.
1,300
Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't.
standard output
PASSED
ac0a6b1eef997bb68131dc4e5fa98590
train_003.jsonl
1349105400
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
256 megabytes
//package codes; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Tprimes { public static void main(String[] args) { // TODO Auto-generated method stub long t; BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); try { t = l(b.readLine()); String s[] = b.readLine().split(" "); //one method to convert this strings array into integer array is- long seive[] = new long[1000001]; long a[] = Arrays.stream(s).mapToLong(Long::parseLong).toArray(); for(int i=2;i<Math.sqrt(1000001);i++) { if(seive[i]==0) { for(int j=i*i;j<1000001;j+=i) { seive[j]=1; } } } seive[1]=1; for(int i=0;i<a.length;i++) { int div=0; double check= Math.pow(a[i], 0.5); if(check== (int)check) { if(seive[(int) check]==0) System.out.println("YES"); else System.out.println("NO"); } else System.out.println("NO"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static long l(String a) { return Long.parseLong(a); } }
Java
["3\n4 5 6"]
2 seconds
["YES\nNO\nNO"]
NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".
Java 11
standard input
[ "binary search", "number theory", "implementation", "math" ]
6cebf9af5cfbb949f22e8b336bf07044
The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier.
1,300
Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't.
standard output
PASSED
0cd47f95497147f5ee14b31e5ee971fb
train_003.jsonl
1349105400
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
256 megabytes
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); // Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int arr[]=new int[1000001]; arr[0]=0; arr[1]=0; int i,j; int c=0; for(i=2;i<1000001;i++) arr[i]=i; HashSet s=new HashSet(); for(i=2;i<=Math.sqrt(1000000);i++) { if(arr[i]!=0) { for(j=i*i;j<1000001;j+=i) { arr[j]=0; c++; } } } c=0; for(i=0;i<1000001;i++) { if(arr[i]!=0) { s.add((long)i*i); } } int n=Integer.parseInt(br.readLine()); String str=br.readLine(); String sp[]=str.split(" "); long v; for(i=0;i<sp.length;i++) { v=Long.parseLong(sp[i]); if(s.contains(v)) { out.println("YES"); } else out.println("NO"); } out.flush(); out.close(); // sc.close(); br.close(); } }
Java
["3\n4 5 6"]
2 seconds
["YES\nNO\nNO"]
NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".
Java 11
standard input
[ "binary search", "number theory", "implementation", "math" ]
6cebf9af5cfbb949f22e8b336bf07044
The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier.
1,300
Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't.
standard output
PASSED
b99e5783ecba0fd3ddaf7bdd47551f0c
train_003.jsonl
1349105400
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
256 megabytes
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); // Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int arr[]=new int[1000001]; arr[0]=0; arr[1]=0; int i,j; int c=0; for(i=2;i<1000001;i++) arr[i]=i; HashSet s=new HashSet(); for(i=2;i<=Math.sqrt(1000000);i++) { if(arr[i]!=0) { for(j=i*i;j<1000001;j+=i) { arr[j]=0; c++; } } } c=0; for(i=0;i<1000001;i++) { if(arr[i]!=0) { s.add((long)i*i); } } int n=Integer.parseInt(br.readLine()); String str=br.readLine(); String sp[]=str.split(" "); long v; for(i=0;i<sp.length;i++) { v=Long.parseLong(sp[i]); if(s.contains(v)) { out.println("YES"); } else out.println("NO"); } out.flush(); out.close(); // sc.close(); br.close(); } }
Java
["3\n4 5 6"]
2 seconds
["YES\nNO\nNO"]
NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".
Java 11
standard input
[ "binary search", "number theory", "implementation", "math" ]
6cebf9af5cfbb949f22e8b336bf07044
The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier.
1,300
Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't.
standard output
PASSED
ee4072beddcc9b07435b94b289850a75
train_003.jsonl
1349105400
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
256 megabytes
//Written by Shagoto import java.util.*; public class Main { static boolean [] sieve() { boolean [] prime = new boolean[1000000+1]; for(int i=0; i<1000000; i++) { prime[i] = true; } for(int p = 2; p*p<=1000000; p++) { if(prime[p] == true) { for(int i = p*p; i<=1000000; i += p) { prime[i] = false; } } } return prime; } public static void main(String[]args) { Scanner read = new Scanner(System.in); boolean [] arr = sieve(); int n = read.nextInt(); for(int x = 1; x<=n; x++) { long a = read.nextLong(); long temp = (long)Math.sqrt(a); if(temp * temp == a && a != 1) { if(arr[(int)temp] == true) { System.out.println("YES"); } else { System.out.println("NO"); } } else { System.out.println("NO"); } } } }
Java
["3\n4 5 6"]
2 seconds
["YES\nNO\nNO"]
NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".
Java 11
standard input
[ "binary search", "number theory", "implementation", "math" ]
6cebf9af5cfbb949f22e8b336bf07044
The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier.
1,300
Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't.
standard output
PASSED
342342e81b3909bdfe06f64696233a1a
train_003.jsonl
1349105400
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
256 megabytes
import java.io.*; import java.util.*; public class Main{ static int mod=(int)1e9+7; public static void main(String[] args) throws IOException { Writer out=new Writer(System.out); Reader in=new Reader(System.in); int ts=1; outer: while(ts-->0) { int n=in.nextInt(); long a[]=in.readLongArray(n); int max=(int)1e6; boolean isPrime[]=new boolean[max+1]; for(int i=0; i<=max; i++) isPrime[i]=true; for(int i=2; i*i<=max; i++) { if(isPrime[i]) { for(int j=i*i; j<=max; j+=i) { isPrime[j]=false; } } } Set<Long> st=new HashSet<>(); for(int i=2; i<=max; i++) { if(isPrime[i]) st.add((long)i*i); } for(long i: a) { if(st.contains(i)) out.println("YES"); else out.println("NO"); } } out.close(); } static boolean isPrime(long n) { for(int i=2; i*i<=n; i++) if(n%i==0) return false; return true; } /*********************************** UTILITY CODE BELOW **************************************/ static int abs(int a) { return a>0 ? a : -a; } static int max(int a, int b) { return a>b ? a : b; } static long max(long a, long b) { return a+b-min(a,b); } static long min(long a, long b) { return a<b ? a : b; } static int min(int a, int b) { return a<b ? a : b; } static int pow(int n, int m) { if(m==0) return 1; long temp=pow(n,m/2); long res=((temp*temp)%mod); if(m%2==0) return (int)res; return (int)((res*n)%mod); } static long pow(long n, long m) { if(m==0) return 1; long temp=pow(n,m/2); long res=temp*temp; if(m%2==0) return res; return res*n; } static class Pair{ int u, v; Pair(int u, int v){this.u=u; this.v=v;} static void sort(Pair [] coll) { List<Pair> al=new ArrayList<>(Arrays.asList(coll)); Collections.sort(al,new Comparator<Pair>() { public int compare(Pair p1, Pair p2) { return p1.u-p2.u; } }); for(int i=0; i<al.size(); i++) { coll[i]=al.get(i); } } } static void sort(int[] a) { ArrayList<Integer> list=new ArrayList<>(); for (int i:a) list.add(i); Collections.sort(list); for (int i=0; i<a.length; i++) a[i]=list.get(i); } static void sort(long a[]) { ArrayList<Long> list=new ArrayList<>(); for(long i: a) list.add(i); Collections.sort(list); for(int i=0; i<a.length; i++) a[i]=list.get(i); } static int [] array(int n, int value) { int a[]=new int[n]; for(int i=0; i<n; i++) a[i]=value; return a; } static class Reader{ BufferedReader br; StringTokenizer to; Reader(InputStream stream){ br=new BufferedReader(new InputStreamReader(stream)); to=new StringTokenizer(""); } String next() { while(!to.hasMoreTokens()) { try { to=new StringTokenizer(br.readLine()); }catch(IOException e) {} } return to.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } 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; } } static class Writer extends PrintWriter{ Writer(OutputStream stream){ super(stream); } void println(int [] array) { for(int i=0; i<array.length; i++) { print(array[i]+" "); } println(); } void println(long [] array) { for(int i=0; i<array.length; i++) { print(array[i]+" "); } println(); } } }
Java
["3\n4 5 6"]
2 seconds
["YES\nNO\nNO"]
NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".
Java 11
standard input
[ "binary search", "number theory", "implementation", "math" ]
6cebf9af5cfbb949f22e8b336bf07044
The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier.
1,300
Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't.
standard output
PASSED
85708396f452d4c7c1826174e8e3ae9e
train_003.jsonl
1349105400
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class CodeForces { public static void main(String[] args) throws IOException,NumberFormatException{ try { FastScanner sc=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int n=sc.nextInt(); Set<Long> list=new HashSet<>(); for(long i=2;i<=1000000;i++) { if(isPrime(i)) { list.add(i*i); } } for(int i=0;i<n;i++) { long s=sc.nextLong(); out.println(list.contains(s)?"YES":"NO"); } out.close(); } catch(Exception e) { return ; } } public static boolean isPrime(long n) { if(n==1) return false; for(int i=2;i*i<=n;i++) { if(n%i==0) return false; } return true; } public static int[] gcd(int a,int b) { if(b==0) { return new int[] {a,1,0}; } int[] vals=gcd(b,a%b); int d=vals[0]; int x=vals[2]; int y=vals[1]-(a/b)*vals[2]; return new int[] {d,x,y}; } public static long prodDigit(long n) { int sum=1; while(n!=0) { sum*=(n%10); n/=10; } return sum; } public static int GCD(int a,int b) { if(b==0) return a; return GCD(b,a%b); } public 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) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n4 5 6"]
2 seconds
["YES\nNO\nNO"]
NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".
Java 11
standard input
[ "binary search", "number theory", "implementation", "math" ]
6cebf9af5cfbb949f22e8b336bf07044
The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier.
1,300
Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't.
standard output
PASSED
9b934104248675c861324dfbfc68a471
train_003.jsonl
1349105400
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class TPrimes { public static void main(String[] args) { Reader1 s1=new Reader1(); long t=s1.nextLong(); double n= (Math.pow(10,6)+1); double[] array=new double[(int) n]; // HashMap<Double,Double > array=new HashMap<>(); for(double i=3;i<n;i+=2){ array[(int) i]=1; }//marking all the odd numbers as primes(1). for(double i=3;i<n;i+=2){ if(array[(int) i]==1){ for(double j=i*i;j<n;j+=i){ array[(int) j]=0;//unke multiples ko setting as 0. } } } array[2]=1; //System.out.println(array.containsKey((double)2)); // System.out.println(array.containsKey((double)6)); for(double i=0;i<t;i++){ double temp=s1.nextDouble(); double y=Math.sqrt(temp); if(array[(int) Math.sqrt(temp)]==1&&y==(long)Math.sqrt(temp)){ System.out.println("YES"); }else{ System.out.println("NO"); } } } } class Reader1 { BufferedReader br; StringTokenizer st; public Reader1() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int 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\n4 5 6"]
2 seconds
["YES\nNO\nNO"]
NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".
Java 11
standard input
[ "binary search", "number theory", "implementation", "math" ]
6cebf9af5cfbb949f22e8b336bf07044
The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier.
1,300
Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't.
standard output
PASSED
a58b1935ec2bd0a848bef2044a856991
train_003.jsonl
1349105400
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
256 megabytes
import java.io.*; import java.util.*; public class Sample { static int k; static boolean sieve[]; static int MAX = (int)1e7+1; static int kk = 0; static HashMap<Long,Boolean>map = new HashMap<>(); static ArrayList<Integer>list = new ArrayList<>(); public static void main(String[] args) throws Exception{ Scanner input = new Scanner(System.in); int n = input.nextInt(); long a[] = new long[n]; StringBuilder sb = new StringBuilder(); for(int i =0;i<n;i++)a[i] = input.nextLong(); sieve =new boolean[MAX]; build(); for(int i =0;i<n;i++) { if(isTPrime(a[i])) { sb.append("YES"); sb.append("\n"); }else { sb.append("NO"); sb.append("\n"); } } System.out.println(sb); } public static void build() { for(int i =2;i*i<MAX;i++) { if(!sieve[i]) { for(int j = i*i;j<MAX;j+=i) { sieve[j] = true; } } } for(int i = 2;i<MAX;i++) { if(!sieve[i])list.add(i); } for(long it:list) { long ans = it*it; map.put(ans,true); } } public static boolean isTPrime(long val) { return map.containsKey(val); } public static int findMax(int a[]) { int max = -1; int index = -1; for(int i =0;i<a.length;i++) { if(a[i]>max) { max = a[i]; index = i; } } return index; } public static int findMin(int a[]) { int min = Integer.MAX_VALUE; int index = -1; for(int i =0;i<a.length;i++) { if(a[i]<min) { min = a[i]; index =i; } } return index; } }
Java
["3\n4 5 6"]
2 seconds
["YES\nNO\nNO"]
NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".
Java 11
standard input
[ "binary search", "number theory", "implementation", "math" ]
6cebf9af5cfbb949f22e8b336bf07044
The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier.
1,300
Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't.
standard output
PASSED
a2f7f61251c93a7c1e4bfe5b66e7cfae
train_003.jsonl
1349105400
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
256 megabytes
import java.io.*; import java.util.*; public class Sample { static int k; static boolean sieve[]; static int MAX = (int)1e7+1; static int kk = 0; static HashMap<Long,Boolean>map = new HashMap<>(); static ArrayList<Integer>list = new ArrayList<>(); public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String l[] = br.readLine().split(" "); StringBuilder sb = new StringBuilder(); sieve =new boolean[MAX]; build(); for(int i =0;i<n;i++) { long val = Long.parseLong(l[i]); if(isTPrime(val)) { sb.append("YES"); sb.append("\n"); }else { sb.append("NO"); sb.append("\n"); } } System.out.println(sb); } public static void build() { for(int i =2;i*i<MAX;i++) { if(!sieve[i]) { for(int j = i*i;j<MAX;j+=i) { sieve[j] = true; } } } for(int i = 2;i<MAX;i++) { if(!sieve[i])list.add(i); } for(long it:list) { long ans = it*it; map.put(ans,true); } } public static boolean isTPrime(long val) { return map.containsKey(val); } public static int findMax(int a[]) { int max = -1; int index = -1; for(int i =0;i<a.length;i++) { if(a[i]>max) { max = a[i]; index = i; } } return index; } public static int findMin(int a[]) { int min = Integer.MAX_VALUE; int index = -1; for(int i =0;i<a.length;i++) { if(a[i]<min) { min = a[i]; index =i; } } return index; } }
Java
["3\n4 5 6"]
2 seconds
["YES\nNO\nNO"]
NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".
Java 11
standard input
[ "binary search", "number theory", "implementation", "math" ]
6cebf9af5cfbb949f22e8b336bf07044
The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier.
1,300
Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't.
standard output
PASSED
38dba814becebda6d3a8132051b62ada
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class stairs{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); if(m!=0){ int[] dirty = new int[m]; int i = 0; for(i=0;i<m;i++){ dirty[i]=sc.nextInt(); } sc.close(); dirty=mergesort(dirty); i = 0; Boolean result = true; for(i=1;i<(m-1);i++){ if((dirty[i-1]+1)==dirty[i] && (dirty[i]+1)==dirty[i+1]){ result=false; } } if(dirty[0]==1 || dirty[dirty.length-1]==n){ result=false; } if(result){ System.out.println("YES"); } else{ System.out.println("NO"); } } else{ System.out.println("YES"); } } public static int[] mergesort (int[] dirty){ int n = dirty.length; if(n==1){ return (dirty); } else{ if(n%2==0){ return(merge(mergesort(Arrays.copyOfRange(dirty,0,n/2)),mergesort(Arrays.copyOfRange(dirty,n/2,n)))); } else{ return(merge(mergesort(Arrays.copyOfRange(dirty,0,((n+1)/2))),mergesort(Arrays.copyOfRange(dirty,((n+1)/2),n)))); } } } public static int[] merge (int[] array1, int[] array2){ int i = 0; int j = 0; int[] retarray = new int[(array1.length+array2.length)]; int k = 0; while(i<array1.length && j<array2.length){ if(array1[i]<=array2[j]){ retarray[k]=array1[i]; i++; k++; } else{ retarray[k]=array2[j]; j++; k++; } } while(i<array1.length){ retarray[k]=array1[i]; i++; k++; } while(j<array2.length){ retarray[k]=array2[j]; j++; k++; } return (retarray); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
37a1c5269855906da9eebe6848b8e855
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.util.InputMismatchException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Zakhar Voit (zakharvoit@gmail.com) */ 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(); } } class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[] d = in.nextIntArray(m); Arrays.sort(d); if (m > 0 && (d[0] == 1 || d[m - 1] == n)) { out.println("NO"); return; } for (int i = 0; i < m - 2; i++) { int a = d[i + 1] - d[i]; int b = d[i + 2] - d[i + 1]; if (a == 1 && b == 1) { out.println("NO"); return; } } out.println("YES"); } } class Scanner { BufferedReader in; StringTokenizer tok; public Scanner(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); tok = new StringTokenizer(""); } public String nextToken() { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(next()); } return tok.nextToken(); } private String tryReadNextLine() { try { return in.readLine(); } catch (IOException e) { throw new InputMismatchException(); } } public String next() { String newLine = tryReadNextLine(); if (newLine == null) throw new InputMismatchException(); return newLine; } public int nextInt() { return Integer.parseInt(nextToken()); } public int[] nextIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
2fd8da6ed43624cc0edcd6d48621112f
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class contest_B_13_noy { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); if (m==0) { System.out.println("YES"); return; } int [] s=new int [m]; for (int i =0; i <m; i++) { s[i]=sc.nextInt(); } Arrays.sort(s); if(s[0]==1 || s[m-1]==n){ System.out.println("NO"); return; } for (int i=1; i<m-1; i++) { if ((s[i-1]+1==s[i])&&(s[i+1]-1==s[i])) { System.out.println("NO"); return; } } System.out.println("YES"); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
cef11cbd7ee632df84047e160447d60b
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.util.*; public class PetyaAndStaircases { static boolean can(int[] dirty, int n) { if (dirty.length == 0) return true; Arrays.sort(dirty); if (dirty[0] == 1 || dirty[dirty.length - 1] == n) return false; // look for three consecutive dirty stairs for (int i = 1; i < dirty.length - 1; ++i) if (dirty[i] - dirty[i - 1] == 1 && dirty[i + 1] - dirty[i] == 1) return false; return true; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[] dirty = new int[m]; for (int i = 0; i < m; ++i) dirty[i] = in.nextInt(); System.out.println(can(dirty, n) ? "YES" : "NO"); in.close(); System.exit(0); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
d592d49e693314c760736a50659913d2
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.util.*; import java.io.*; public class TaskB { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n=in.nextInt(); int m=in.nextInt(); int[] dirty = new int[m]; for (int i=0;i<m;i++) { dirty[i]=in.nextInt(); } Arrays.sort(dirty); boolean f=false; if (m==0) { out.print("YES"); } else { if (dirty[0]==1||dirty[m-1]==n) { out.print("NO"); } else { for (int i=2;i<m;i++) { if (((dirty[i]-dirty[i-1])==1)&&((dirty[i-1]-dirty[i-2])==1)) { out.print("NO"); f=true; break; } } if (!f) { out.print("YES"); } } } out.close(); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
c28caa82706f0621d6d74432647e8b3d
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Task1 { public static void solve() throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(in.readLine()); int n = Integer.valueOf(tok.nextToken()); int m = Integer.valueOf(tok.nextToken()); int mas[] = new int[m]; //for (int i = 0; i < m; i++) { // mas[Integer.valueOf(tok.nextToken())-1] = 1; //} if(m == 0){ System.out.println("YES"); System.exit(0); } tok = new StringTokenizer(in.readLine()); for (int i = 0; i < m; i++) { mas[i] = Integer.valueOf(tok.nextToken()); } Arrays.sort(mas); for (int i = 2; i < m; i++) { if(mas[0] == 1){ System.out.println("NO"); System.exit(0); } if(mas[m-1] == n){ System.out.println("NO"); System.exit(0); } if(mas[i-2]+2 == mas[i] && mas[i] == mas[i-1]+1){ System.out.println("NO"); System.exit(0); } } if(mas[0] == 1){ System.out.println("NO"); System.exit(0); } if(mas[m-1] == n){ System.out.println("NO"); System.exit(0); } System.out.println("YES"); } public static void main(String[] args) throws IOException { solve(); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
b1c733f2e88cbc188b3152a7fadf08d8
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(in, out); out.close(); } } class TaskB { public void solve(InputReader in, OutputWriter out) { int n = in.nextInt(); int m = in.nextInt(); List<Integer> values = new ArrayList<>(); for (int i = 0; i < m; i++) { values.add(in.nextInt()); } Collections.sort(values); int i = 2; int act,prev1,prev2; while(i < values.size()) { act = values.get(i); prev1 = values.get(i-1); prev2 = values.get(i-2); if(act == prev1 + 1 && act == prev2 + 2) { System.out.println("NO"); return; } i++; } if(values.isEmpty() || (values.get(0) != 1 && values.get(values.size()-1) != n)) { System.out.println("YES"); } else { System.out.println("NO"); } } } 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()); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
b9e9ba5dc83695eade80571c3750d894
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import java.io.StreamCorruptedException; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Solution { public static void main(String[] args) { new Solution().solve(); } PrintWriter out; // long mod=1000000007L ; public void solve() { out=new PrintWriter(System.out); int n=in.nextInt(); int m=in.nextInt(); int[] arr=in.nextIntArray(m); Arrays.sort(arr); if(m==0) { System.out.println("YES"); System.exit(0); } if(arr[0]==1) { System.out.println("NO"); System.exit(0); } else if(arr[arr.length-1]==n) { System.out.println("NO"); System.exit(0); } else { for(int i=0;i<m-2;i++) { if(arr[i+1]==arr[i]+1 && arr[i+2]==arr[i]+2) { System.out.println("NO"); System.exit(0); } } } System.out.println("YES"); out.close(); } public long lcm(long lcm,long lcm2) { return ((long)lcm/gcd(lcm,lcm2))*lcm2; } public static int gcd(long lcm, long lcm2) { BigInteger b1 = BigInteger.valueOf(lcm); BigInteger b2 = BigInteger.valueOf(lcm2); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } FasterScanner in=new FasterScanner(); public class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
49ed720874cf49d718d94a2b196d9ef7
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class PetyaStaricase { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); if(n==0 || m==0){ System.out.println("YES"); return; } st = new StringTokenizer(br.readLine()); int [] dirty = new int[m]; for(int i=0; i<m; i++){ dirty[i] = Integer.parseInt(st.nextToken()); } boolean jumpOnDirty = false; Arrays.sort(dirty); if(dirty[0]==1 || dirty[dirty.length-1]==n) jumpOnDirty = true; for(int i=0; i<m-2 && !jumpOnDirty; i++){ if(dirty[i+1]-dirty[i]==1 && dirty[i+2]-dirty[i+1]==1) jumpOnDirty = true; } if(jumpOnDirty) System.out.println("NO"); else System.out.println("YES"); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
2fba7bc64e781bf34fdf7e75ff7f7dd9
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class PetyaAndStaircases { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(); int[] a = new int[m]; for (int i = 0; i < m; i++) a[i] = in.nextInt(); boolean f = true; Arrays.sort(a); for (int i = 0; i < m; i++) { if (a[i] == 1 || a[i] == n) f = false; if (i + 2 < m && a[i + 2] - a[i] == 2) f = false; } System.out.println(f ? "YES" : "NO"); in.close(); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
c430b4b82d724f0b645a5a5ec1085eff
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); long n; int d,i,flag=0,flag1=0; n=sc.nextLong(); d=sc.nextInt(); int a[]=new int[d]; for(i=0;i<d;i++) { a[i]=sc.nextInt(); if(a[i]==1 || a[i]==n) { System.out.println("NO"); System.exit(0); } } Arrays.sort(a); for(i=0;i<d-2;i++) { if(a[i]+1==a[i+1]) { if(a[i+1]+1==a[i+2]) { flag++; System.out.println("NO"); break; } } } if(flag==0) { System.out.println("YES"); } } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
506d265b81355598f307a91802a35a06
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; import java.util.Scanner; import java.util.TreeSet; public class Main { int a[][]; boolean b[][]; boolean ans = false; void r(int x, int y, int len){ if (a[x][y] == 1) { if (len % 2 == 0 && a[x][y] != 2) ans = true; return; } } public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); Integer a[] = new Integer[m]; for (int i = 0; i<m; i++) a[i] = in.nextInt(); Arrays.sort(a); if (m == 0) { System.out.println("YES"); return; } if (m == 1) { if (a[0] == 1 || a[0] == n) { System.out.println("NO"); return; } else { System.out.println("YES"); return; } } if (a[0] == 1 || a[m - 1] == n) { System.out.println("NO"); return; } // 1 2 3 4 5 6 7 8 // . . . . . . . # for (int i = 0; i < m - 2; i++) { if (a[i] + 1 == a[i + 1] && a[i + 1] + 1 == a[i + 2]){ System.out.println("NO"); return; } } System.out.println("YES"); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
3741f930d51597e50a1cbdb332bae8ab
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.util.*; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(); int a[] = new int[m]; for(int i = 0; i < m; ++i) { a[i] = sc.nextInt(); } Arrays.sort(a); if(m == 0) { System.out.println("YES"); } else if(a[0] == 1 || a[m - 1] == n) { System.out.println("NO"); } else { boolean f = true; for(int i = 0; i < m - 2; ++i) { if(!f) break; if(a[i] == a[i + 1] - 1 && a[i + 1] == a[i + 2] - 1) { System.out.println("NO"); f = false; } } if(f) System.out.println("YES"); } } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
d6cff5f02e2c8494ea206bf1975deef4
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.util.*; public class probB { 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(); boolean answer = true; int arr[] = new int [k]; for (int i = 0; i < k; i++) { int z =in.nextInt()-1; arr[i]= z; if(z==0 || z== n-1) answer =false; } Arrays.sort(arr); for (int i = 0; i < arr.length-2 && answer; i++) { if(arr[i]==(arr[i+1]-1) && arr[i+1]==(arr[i+2]-1)) { answer = false; break; } } if(answer) System.out.println("YES"); else System.out.println("NO"); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
953805866620bf3b7ca949d683c8240a
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 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); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); if(m == 0){ out.println("YES"); return; } int a[] = in.parseInt1D(m); Arrays.sort(a); if(a[0] == 1){ out.println("NO"); return; } if(a[a.length-1] == n){ out.println("NO"); return; } for(int i=0;i<a.length-2;i++){ if(a[i]+1 == a[i+1] && a[i+1] +1 == a[i+2] ){ out.println("NO"); return; } } out.println("YES"); } } class InputReader { private BufferedReader br; private StringTokenizer st; public InputReader(InputStream in) { br=new BufferedReader(new InputStreamReader(in)); try { st=new StringTokenizer(br.readLine()); } catch (IOException ignored) { } } public void readLine() { try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { return; } } public int nextInt(){ return Integer.parseInt(st.nextToken()); } /** * Parse 1D array from current StringTokenizer */ public int[] parseInt1D(int n){ readLine(); int r[]=new int[n]; for(int i=0;i<n;i++){ r[i]=nextInt(); } return r; } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
030717053315eb35d980a6616c7e5241
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.util.*; import java.io.*; public class Main { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static StringBuilder out = new StringBuilder(); public static void main(String[] args){ int[] inp = nextIntArray(); int n = inp[0]; int m = inp[1]; if (m == 0){ System.out.println("YES"); return; } int[] d = nextIntArray(); Arrays.sort(d); if (d[0] == 1 || d[m - 1] == n){ out.append("NO"); } else { for (int i = 0; i + 2 < m; i++){ if (d[i] + 2 == d[i + 2]){ out.append("NO"); break; } } if (out.length() == 0){ out.append("YES"); } } System.out.println(out); } // the followings are methods to take care of inputs. static int nextInt(){ return Integer.parseInt(nextLine()); } static long nextLong(){ return Long.parseLong(nextLine()); } static int[] nextIntArray(){ String[] inp = nextLine().split("\\s+"); int[] ary = new int[inp.length]; for (int i = 0; i < ary.length; i++){ ary[i] = Integer.parseInt(inp[i]); } return ary; } static int[] nextIntArrayFrom1(){ String[] inp = nextLine().split("\\s+"); int[] ary = new int[inp.length + 1]; for (int i = 0; i < inp.length; i++){ ary[i+1] = Integer.parseInt(inp[i]); } return ary; } static long[] nextLongArray(){ String[] inp = nextLine().split("\\s+"); long[] ary = new long[inp.length]; for (int i = 0; i < inp.length; i++){ ary[i] = Long.parseLong(inp[i]); } return ary; } static long[] nextLongArrayFrom1(){ String[] inp = nextLine().split("\\s+"); long[] ary = new long[inp.length + 1]; for (int i = 0; i < inp.length; i++){ ary[i+1] = Long.parseLong(inp[i]); } return ary; } static String nextLine(){ try { return reader.readLine().trim(); } catch (Exception e){} return null; } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
86b3d264e3e90ff09648dcdc5d3594cb
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
/* Date : Problem Name : Location : Algorithm : Status : CodingTime : ReadingTime : */ import java.util.*; import java.io.*; public class Main { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args){ int[] inp = nextIntArray(); int n = inp[0]; int m = inp[1]; if (m == 0){ System.out.println("YES"); return; } int[] dirtySteps = nextIntArray(); Arrays.sort(dirtySteps); if (dirtySteps[0] == 1 || dirtySteps[m-1] == n){ System.out.println("NO"); return; } else { for (int i = 0; i + 2 < m; i++){ if (1 + dirtySteps[i] == dirtySteps[i+1] && 1 + dirtySteps[i + 1] == dirtySteps[i+2]){ System.out.println("NO"); return; } } } System.out.println("YES"); return; } static int nextLong(){ return Integer.parseInt(nextLine()); } static int nextInt(){ return Integer.parseInt(nextLine()); } static long[] nextLongArrayFrom1(){ String[] inp = nextLine().split("\\s+"); long[] ary = new long[inp.length + 1]; for (int i = 0; i < inp.length; i++){ ary[i+1] = Long.parseLong(inp[i]); } return ary; } static long[] nextLongArray(){ String[] inp = nextLine().split("\\s+"); long[] ary = new long[inp.length]; for (int i = 0; i < inp.length; i++){ ary[i] = Long.parseLong(inp[i]); } return ary; } static int[] nextIntArrayFrom1(){ String[] inp = nextLine().split("\\s+"); int[] ary = new int[inp.length + 1]; for (int i = 0; i < inp.length; i++){ ary[i+1] = Integer.parseInt(inp[i]); } return ary; } static int[] nextIntArray(){ String[] inp = nextLine().split("\\s+"); int[] ary = new int[inp.length]; for (int i = 0; i < ary.length; i++){ ary[i] = Integer.parseInt(inp[i]); } return ary; } static String nextLine(){ try { return reader.readLine().trim(); } catch (Exception e){} return null; } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
face2e389da124441a3b723826bc20c2
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main{ public static void main(String args[]) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int m = in.nextInt(); // boolean isDirty[] = new boolean[m + 1]; int dirty[] = new int[m + 1]; boolean flag = false; int i, stair; for (i = 1; i <= m; i++) { stair = in.nextInt(); if (stair == 1 || stair == N) { System.out.println("NO"); return; } // isDirty[stair] = true; dirty[i] = stair; } Arrays.sort(dirty); // int consecutive = 0; for (i = 1; i <= m - 2; i++) { if (dirty[i] + 1 == dirty[i + 1] && dirty[i] + 2 == dirty[i + 2]) { flag = true; System.out.println("NO"); break; } } if (!flag) System.out.println("YES"); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
2e4676f1c65e3dd8896f57528b42a925
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Hashtable; import java.util.InputMismatchException; import java.util.PriorityQueue; import java.util.Stack; import java.util.TreeSet; public class Q1 { static int count=0; static char[] a; static boolean b[]; public static void main(String[] args) { FasterScanner s= new FasterScanner(); PrintWriter out=new PrintWriter(System.out); int n=s.nextInt(); int m=s.nextInt(); int[] a=s.nextIntArray(m); Arrays.sort(a); int j=0; if(m==0) { System.out.println("YES"); return; } if(a[0]==1 || a[m-1]==n) { System.out.println("NO"); return; } for(int i=0;i<m-2;i++) { if(a[i]==a[i+1]-1 && a[i]==a[i+2]-2) { System.out.println("NO"); return; } } System.out.println("YES"); } static class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
e69773b4a5b63a444de232a5edf7473a
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.io.*; import java.util.*; public class cf362b { static FastIO in = new FastIO(), out = in; public static void main(String[] args) { int n = in.nextInt(); int m = in.nextInt(); int[] v = new int[m]; for(int i=0; i<m; i++) v[i] = in.nextInt(); Arrays.sort(v); boolean ok = true; if(m > 0 && v[0] == 1) ok = false; if( m > 0 && v[m-1] == n) ok = false; for(int i=2; i<m; i++) if(v[i] == 1+v[i-1] && v[i-1] == 1+v[i-2]) ok = false; out.println(ok?"YES":"NO"); out.close(); } static class FastIO extends PrintWriter { BufferedReader br; StringTokenizer st; public FastIO() { this(System.in, System.out); } public FastIO(InputStream in, OutputStream out) { super(new BufferedWriter(new OutputStreamWriter(out))); br = new BufferedReader(new InputStreamReader(in)); scanLine(); } public void scanLine() { try { st = new StringTokenizer(br.readLine().trim()); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } public int numTokens() { if (!st.hasMoreTokens()) { scanLine(); return numTokens(); } return st.countTokens(); } public String next() { if (!st.hasMoreTokens()) { scanLine(); return next(); } return st.nextToken(); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
530c12a25f9ded27e14cbbd0dc258939
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class JavaApplication52 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int k = s.nextInt(); if(k == 0){ System.out.println("YES"); return; } int[] dirty = new int[k]; for(int i = 0; i < k; i++){ dirty[i] = s.nextInt(); } Arrays.sort(dirty); if(dirty[0] == 1 || dirty[k - 1] == n){ System.out.println("NO"); return; } for(int i = 1; i < k - 1; i++){ if(dirty[i] - dirty[i - 1] == 1 && dirty[i+1] - dirty[i] == 1){ System.out.println("NO"); return; } } System.out.println("YES"); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
3e1810a326cd58c0f50af750d2a3e427
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
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 { new Main().run(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); final int maxn = 1000 * 1000 + 100; int n, m; int a[] = new int[maxn]; boolean check() { for (int i = 0; i + 2 < m; i++) if (a[i + 1] == a[i] + 1 && a[i + 2] == a[i] + 2) return false; return true; } private void run() throws IOException { // in = new BufferedReader(new FileReader("input.txt")); // out = new PrintWriter("output.txt"); if (new File("input.txt").exists()) in = new BufferedReader(new FileReader("input.txt")); else in = new BufferedReader(new InputStreamReader(System.in)); if (new File("output.txt").exists()) out = new PrintWriter("output.txt"); else out = new PrintWriter(System.out); n = nextInt(); m = nextInt(); for (int i = 0; i < m; i++) a[i] = nextInt(); sort(a, 0, m); if (m == 0) out.println("YES"); else if (a[0] == 1 || a[m - 1] == n || !check()) out.println("NO"); else out.println("YES"); 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
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
2acd3e4135eaf8df7de9c611a70fb09b
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.StringTokenizer; public class Main { static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter writer; static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } static long nextLong() throws IOException { return Long.parseLong(nextToken()); } static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } static boolean eof = false; static String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static void main(String[] args) throws IOException { tokenizer = null; reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(System.out); banana(); reader.close(); writer.close(); } static void banana() throws IOException { int n = nextInt(); int m = nextInt(); int a[] = new int[m]; for (int i = 0; i < m; ++i) a[i] = nextInt(); for (int i = 0; i < m; ++i) if (a[i] == n || a[i] == 1) { System.out.println("NO"); return; } Arrays.sort(a); for (int i = 0 ; i +2 < m; ++i) if (a[i] + 2 == a[i+2] && a[i+1]+1 == a[i+2]) { System.out.println("NO"); return; } System.out.println("YES"); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
6c6ca6641d389d710cc66d5b6720f770
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.io.*; import java.util.*; import java.lang.StringBuilder; public class Solution362B { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); MyScanner sc = new MyScanner(); int n = sc.nextInt(); int m = sc.nextInt(); int[] dirtyStairs = new int[m]; for (int i = 0 ; i < m ; i++) { int dirtyNum = sc.nextInt(); dirtyStairs[i] = dirtyNum; } Arrays.sort(dirtyStairs); boolean isPossible = true; if(m != 0) { if(dirtyStairs[0] == 1 || dirtyStairs[m-1] == n) { isPossible = false; } else { for (int i = 2 ; i < m ; i++) { if(dirtyStairs[i]-1 == dirtyStairs[i-1] && dirtyStairs[i]-2 == dirtyStairs[i-2]) { isPossible = false; break; } } } } if(isPossible) System.out.println("YES"); else System.out.println("NO"); } } 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
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
2980d0b562d3b46f1255d4f5c3434ba7
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner ; import java.util.StringTokenizer; import sun.util.locale.StringTokenIterator; public class JavaApplication96 { /** * @param args the command line arguments */ public static void main(String[] args) throws Exception{ // TODO code application logic here BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)) ; StringTokenizer st = new StringTokenizer(bf.readLine()) ; long n = Long.parseLong(st.nextToken()) ; int a = Integer.parseInt(st.nextToken()) ; long ar [] = new long [a] ; if(a > 0) st = new StringTokenizer(bf.readLine()) ; for(int i = 0 ; i < a ; i++) ar[i] = Integer.parseInt(st.nextToken()) ; Arrays.sort(ar); boolean flag = true ; for(int i = 0 ; i < a - 2 ; i++) if(ar[i] - ar[i+1] == -1 && ar[i+1] - ar[i+2] == -1 ) {System.out.println("NO"); flag = false ; break; } if(a > 0) if((ar[0] == 1 || ar[a-1 ] == n )&& flag) { System.out.println("NO"); flag = false ; } if(flag) System.out.println("YES"); }}
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
02cac33fb32ba7b9a1aad14c9769129e
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class CF362B { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); Set<Integer> dirty = new HashSet<Integer>(); int[] dirtyArr = new int[m]; for (int i = 0; i < m; i++) { dirtyArr[i] = in.nextInt(); dirty.add(dirtyArr[i]); } if (dirty.contains(1) || dirty.contains(n)) { System.out.println("NO"); return; } Arrays.sort(dirtyArr); int combo = 1; for (int i = 1; i < m; i++) { if (dirtyArr[i]-1 == dirtyArr[i-1]) { combo++; if (combo == 3) { System.out.println("NO"); return; } } else { combo = 1; } } System.out.println("YES"); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
af4b07743a6d9dd122466e1bdb116a30
train_003.jsonl
1384443000
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
256 megabytes
import java.io.*; import java.util.*; import java.*; import java.math.BigInteger; import java.util.Random; import javax.naming.BinaryRefAddr; import java.io.*; import java.util.*; import java.lang.*; public class zad { private static BufferedReader in; private static StringTokenizer tok; private static PrintWriter out; final static boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") !=null; public static 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("out.txt"); } } private static String readString() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } private static int readInt() throws IOException { return Integer.parseInt(readString()); } private static double readDouble() throws IOException { return Double.parseDouble(readString()); } private static long readLong() throws IOException { return Long.parseLong(readString()); } static int gcd(int a, int b) { while (b != 0) { int r = a % b; a = b; b = r; } return a; } public static void Solve() throws IOException{ int n = readInt(); int m= readInt(); long[] mas = new long[m]; for(int i=0;i<m;i++){ mas[i]=readLong(); if(mas[i]==n || mas[i]==1){ out.println("NO"); return; } } if(m<3){ out.println("YES"); return; } Arrays.sort(mas); for (int i=1;i<mas.length-1;i++){ if (mas[i]-mas[i-1]==1 && mas[i+1]-mas[i]==1){ out.println("NO"); return; } } out.print("YES"); } public static void main(String[] args) throws IOException { init(); Solve(); in.close(); out.close(); } }
Java
["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"]
1 second
["NO", "YES"]
null
Java 7
standard input
[ "implementation", "sortings" ]
422cbf106fac3216d58bdc8411c0bf9f
The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).
1,100
Print "YES" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print "NO".
standard output
PASSED
62ab2b730f71f9ba6a1706ebd616eeae
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class codeforces { public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] = sc.nextInt(); } Arrays.sort(a); int zero=0; int sum=0,product=1; int count=0; for(int i=0;i<a.length;i++) { if(a[i]==0) { zero++; a[i]++; } sum+=a[i]; } if(sum!=0) { System.out.println(zero); } else { System.out.println(zero+1); } } } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
346ed7d27dafcf73a27f09d26bd677f4
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.InputMismatchException; public class A1300 { static class Solver { int a[], N, zer, sum, dir, res; void solve(int testNumber, FastScanner s, PrintWriter out) { a = s.nextIntArray(N = s.nextInt()); zer = sum = 0; for (int i : a) { if (i == 0) zer++; sum += i; } if (zer == 0) { out.println(sum == 0 ? 1 : 0); } else { out.println(zer == -sum ? zer + 1 : zer); } } } final static boolean cases = true; public static void main(String[] args) { FastScanner s = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); Solver solver = new Solver(); for (int t = 1, T = cases ? s.nextInt() : 1; t <= T; t++) solver.solve(t, s, out); out.close(); } static int min(int a, int b) { return a < b ? a : b; } static int max(int a, int b) { return a > b ? a : b; } static long min(long a, long b) { return a < b ? a : b; } static long max(long a, long b) { return a > b ? a : b; } static int swap(int a, int b) { return a; } static Object swap(Object a, Object b) { return a; } static String ts(Object... o) { return Arrays.deepToString(o); } static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } public FastScanner(File f) throws FileNotFoundException { this(new FileInputStream(f)); } public FastScanner(String s) { this.stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)); } int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } // Jacob Garbage public int[] nextIntArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = this.nextInt(); return ret; } public int[][] next2DIntArray(int N, int M) { int[][] ret = new int[N][]; for (int i = 0; i < N; i++) ret[i] = this.nextIntArray(M); return ret; } public long[] nextLongArray(int N) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = this.nextLong(); return ret; } public long[][] next2DLongArray(int N, int M) { long[][] ret = new long[N][]; for (int i = 0; i < N; i++) ret[i] = nextLongArray(M); return ret; } public double[] nextDoubleArray(int N) { double[] ret = new double[N]; for (int i = 0; i < N; i++) ret[i] = this.nextDouble(); return ret; } public double[][] next2DDoubleArray(int N, int M) { double[][] ret = new double[N][]; for (int i = 0; i < N; i++) ret[i] = this.nextDoubleArray(M); return ret; } } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
1e4c66968ff63805fcf8ba734d216062
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class A { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); //StringBuilder sb=new StringBuilder(); //StringTokenizer st=new StringTokenizer(br.readLine()); //PriorityQueue<Integer> pq=new PriorityQueue<>(); //HashSet<Integer> hs=new HashSet<>(); int q = Integer.parseInt(br.readLine()); int kk =1; while (q-- > 0) { StringTokenizer st=new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int[]a=new int[n]; int s=0; int z=0; st=new StringTokenizer(br.readLine()); for (int i=0;i<n;i++){ a[i]=Integer.parseInt(st.nextToken()); if(a[i]==0)z++; s+=a[i]; } if(s>0)out.println(z); else if(s==0)out.println(z==0?1:z); else out.println(s+z==0?z+1:z); // out.println(Arrays.toString(r)); //out.println(); //out.println(); //out.println(ans1+" "+ans2); //out.println(Arrays.toString(b)); }//out.println(); out.close(); } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
563015379cca9af73d2795219cf24e10
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.io.*; import java.util.*; import java.io.IOException; public class Main { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t--!=0) { int n=Integer.parseInt(br.readLine()); String s=br.readLine(); String st[]=s.split(" "); int arr[]=new int[st.length]; int prod=1,z=0,pval=0; long sum=0,res=0; for(int i=0;i<st.length;i++) { arr[i]=Integer.parseInt(st[i]); sum+=arr[i]; prod*=arr[i]; if(arr[i]==0) z++; else if(arr[i]>0) pval++; } if(sum==0 && prod==0) { int x=0; for(int i=0;i<st.length;i++){ if(arr[i]==0) arr[i]++; x+=arr[i]; } if(x==0) z++; System.out.println(z); } else if(sum==0 && prod!=0) System.out.println(1); else if(sum!=0 && prod==0) { int x=0; for(int i=0;i<st.length;i++){ if(arr[i]==0) arr[i]++; x+=arr[i]; } if(x==0) z++; System.out.println(z); } else System.out.println(0); } } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
3d9795dc8dd64502b793755499ce7d9c
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class GHGG { public static void main(String args[]) { Scanner cin = new Scanner(System.in); int t= cin.nextInt(); ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i <t; i++) { int n=cin.nextInt(); int sum=0; ArrayList<Integer> A= new ArrayList<>(); for (int j = 0; j <n; j++) { A.add(cin.nextInt()); sum=sum+A.get(j); } int min=Integer.MAX_VALUE,h=0; int g=0; for (int j = 0; j <n; j++) { if(A.get(j)==0) { h++; } } int r=h; if(sum+r==0) { r++; } list.add(r); } for (int j = 0; j <t; j++) { System.out.println(list.get(j)); } } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
7a9f60b5869caf6beeb8b1d558669b88
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.util.*; public class temp { public static void main(String[] args) { int t; Scanner in = new Scanner(System.in); t=in.nextInt(); for(int j=0;j<t;j++) { int n; n=in.nextInt(); int[] ar= new int[n]; for(int i=0;i<n;i++) { ar[i]=in.nextInt(); } int count=0; for(int i=0;i<ar.length;i++) { if(ar[i]==0) { ar[i]=1; count++; } } int sum=0; for(int i=0;i<ar.length;i++) { sum+=ar[i]; } // System.out.println("count = "+count+" sum = "+sum); if(sum==0) { System.out.println((sum+1)+count); } else { System.out.println(count); } } } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
1663eb5fa7bc534cb23646f9bbd1b59d
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.util.Scanner; public class NonZero { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); while(n>0){ int t = s.nextInt(), sum = 0, prd = 1, nsum = 0, sumr = 0, psum = 0; int a[] = new int[t]; for(int i = 0; i < t; i++){ a[i] = s.nextInt(); if(a[i]>0) { psum += a[i]; }else if(a[i]==0){ psum++; sumr++; }else { nsum+=a[i]; } sum += a[i]; prd *= a[i]; } if(sum!=0&&prd!=0){ System.out.println(0); }else{ System.out.println(nsum+psum==0?sumr+1:sumr); } n--; } } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
86b258190766eba578492f76a27fa1b7
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
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 t = sc.nextInt(); for(int i =0;i<t;i++){ int n = sc.nextInt(); int[] array = new int[n]; int prd = 1; int sum = 0; int steps = 0; Stack<Integer> st = new Stack<>(); if(n==0){ out.println(0); }else{ for(int j=0;j<n;j++){ int element = sc.nextInt(); array[j] = element; sum+=element; prd*=element; if(element==0){ st.push(j); } } if(sum!=0 && prd!=0){ out.println(0); }else{ if(st.size()!=0){ steps+=st.size(); if(Math.abs(sum)==st.size() && sum < 0){ steps++; } } if(sum+steps==0){ steps++; } out.println(steps); } } } // Start writing your solution here. ------------------------------------- /* 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 = 3*n; out.println(result); // print via PrintWriter */ // Stop writing your solution here. ------------------------------------- out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } 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
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
94b83663895b61912de33ba70eb9da90
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.io.*; import java.util.*; public final class Main{ public static void main(String [] args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int k=0;k<t;k++){ int n=sc.nextInt(); int [] arr=new int[n]; long sum=0,prod=1,count=0; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); sum+=arr[i]; prod=(prod*arr[i])%1000000007; if(arr[i]==0){ count++; } } if(sum!=0 && prod!=0){ System.out.println("0"); } else if(sum==0 && prod==0){ System.out.println(count); } else if(sum==0 && prod!=0){ System.out.println("1"); } else{ if(sum+count==0){ System.out.println(count+1); } else{ System.out.println(count); } } } } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
55312853e1e01ad84975f0ca97007e37
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); StringBuilder result = new StringBuilder(); for(int i = 0; i < t; i++) { int n = scan.nextInt(); int[] a = new int[n]; int res = 0; for(int j = 0; j < n; j++) { a[j] = scan.nextInt(); if(a[j] == 0) { a[j]++; res++; } } if(sum(a) == 0) { res++; } result.append(res + "\n"); } System.out.println(result); } private static int sum(int[] a) { int sum = 0; for(int i=0; i<a.length; i++) { sum += a[i]; } return sum; } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
d6b11d85de98cbed81357da0c54d8e27
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.util.*; public final class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int sum = 0, z = 0; for (int i = 0; i < n; i++) { int num = sc.nextInt(); if (num == 0) { z++; } sum += num; } int steps = 0; if (sum == 0) { steps = z > 0 ? z : 1; } else { steps = z > 0 ? (sum + z == 0 ? z + 1 : z) : 0; } System.out.println(steps); } } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
39f2cce724ae4f7ee0ad7db59f68ebb2
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int tcase = sc.nextInt(); for(int t = 0 ; t < tcase ; t++){ int size = sc.nextInt(); int[] arr = new int[size]; for(int i = 0 ; i < arr.length ; i++){ arr[i] = sc.nextInt(); } int steps = 0 , sum = 0; for(int i = 0 ; i < arr.length ; i++){ if(arr[i] == 0){ arr[i] = 1; steps++; } } for(int i = 0 ; i < arr.length ; i++){ sum = sum + arr[i]; } if(sum == 0){ steps++; } System.out.println(steps); } } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
b317bd78fa57ee9ec04f5b5c786c7fbc
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.util.Scanner; public class NonZero { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t=scan.nextInt(); for(int j=0;j<t;j++) { int n=scan.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) { a[i]=scan.nextInt(); } int sum=0,count=0,t1=0; for(int i=0;i<n;i++) { sum+=a[i]; if(a[i]==0) { count++; } } if(count==0) { if(sum==0) { System.out.println("1"); } else { System.out.println("0"); } } else { t1=count; sum=sum+count; { if(sum==0) { System.out.println(t1+1); } else { System.out.println(t1); } } } } } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
0fcd611df35578af1df666032fd73453
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
/*package whatever //do not write package name here */ import java.io.*; import java.util.*; public class A { public static void main (String[] args) { Scanner sc =new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int a[] = new int[n]; int nz=0; int sum=0; for(int i=0;i<n;i++) { a[i]= sc.nextInt(); if(a[i]==0)nz++; sum+=a[i]; } int cn=nz; // if(nz+sum<0) // { // cn+=Math.abs(nz+sum); // } if(nz+sum==0) cn++; System.out.println(cn); } } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
a6bc235c3d7435615b5dcdfa94a060b1
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class q1 { public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); while(t-- > 0) { int n = s.nextInt(); int[] inp = new int[n]; int sum = 0; int ans = 0; for(int i=0;i<n;++i) { inp[i] = s.nextInt(); sum += inp[i]; if(inp[i]==0) { ans++; sum++; } } if(sum !=0) System.out.println(ans); else { System.out.println(ans+1); } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
6c71c9fc26393bccff6778c2ca547547
train_003.jsonl
1581257100
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.
256 megabytes
import java.util.Scanner; public class Homework2 { public static void main(String[] args) { Scanner in=new Scanner(System.in); int t=in.nextInt();int sum=0;int countZ=0; for(int i=0;i<t;i++){ int n=in.nextInt();sum=0;countZ=0; for(int j=0;j<n;j++){ int s=in.nextInt(); sum+=s; if(s==0)countZ++; } sum+=countZ; if(sum!=0) System.out.println(countZ); else System.out.println(Math.abs(countZ+1)); } } }
Java
["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"]
1 second
["1\n2\n0\n2"]
NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.
Java 8
standard input
[ "implementation", "math" ]
1ffb08fe61cdf90099c82162b8353b1f
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .
800
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
standard output
PASSED
e45200a6140343f76225e29616a955b9
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION; import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class TaskB implements Runnable { boolean prime[] = new boolean[100001]; void sieveOfEratosthenes(int n) { for (int i = 0; i < n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } InputReader c; PrintWriter w; public void run() { c = new InputReader(System.in); w = new PrintWriter(System.out); int n = c.nextInt(); int m = c.nextInt(); int ma[][] = new int[n][m]; int mb[][] = new int[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m;j++) ma[i][j] = c.nextInt(); for(int i=0;i<n;i++) for(int j=0;j<m;j++) mb[i][j] = c.nextInt(); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(ma[i][j]<mb[i][j]){ int te = mb[i][j]; mb[i][j] = ma[i][j]; ma[i][j] = te; } } } if(check(ma)&&check(mb)){ w.println("Possible"); } else w.println("Impossible"); w.close(); } private boolean check(int[][] ma) { for(int i=0;i<ma.length;i++){ for(int j=0;j<ma[i].length;j++){ if(i!=0 &&ma[i-1][j]>=ma[i][j]) return false; if(j!=0 && ma[i][j-1]>=ma[i][j]) return false; } } return true; } class pair{ int x,c; public pair(int a,int b){ x=a;c=b; } } public static void sortbyColumn(int arr[][], int col){ Arrays.sort(arr, new Comparator<int[]>() { public int compare(int[] o1, int[] o2){ return(Integer.valueOf(o1[col]).compareTo(o2[col])); } }); } static long gcd(long a, long b){ if (b == 0) return a; return gcd(b, a % b); } public static class DJSet { public int[] upper; public DJSet(int n) { upper = new int[n]; Arrays.fill(upper, -1); } public int root(int x) { return upper[x] < 0 ? x : (upper[x] = root(upper[x])); } public boolean equiv(int x, int y) { return root(x) == root(y); } public boolean union(int x, int y) { x = root(x); y = root(y); if (x != y) { if (upper[y] < upper[x]) { int d = x; x = y; y = d; } upper[x] += upper[y]; upper[y] = x; } return x == y; } } public static int[] radixSort(int[] f) { int[] to = new int[f.length]; { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]&0xffff]++] = f[i]; int[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]>>>16)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]>>>16]++] = f[i]; int[] d = f; f = to;to = d; } return f; } public void printArray(int[] a){ for(int i=0;i<a.length;i++) w.print(a[i]+" "); w.println(); } public int[] scanArrayI(int n){ int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = c.nextInt(); return a; } public long[] scanArrayL(int n){ long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = c.nextLong(); return a; } public void printArray(long[] a){ for(int i=0;i<a.length;i++) w.print(a[i]+" "); w.println(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new TaskB(),"TaskB",1<<26).start(); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
54515810efa97c456bb226bcd4620674
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.util.Scanner; public class ex7 { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int array1[][] = new int[n][m]; int array2[][] = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { array1[i][j] = sc.nextInt(); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { array2[i][j] = sc.nextInt(); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (array2[i][j] < array1[i][j]) { int temp = array2[i][j]; array2[i][j] = array1[i][j]; array1[i][j] = temp; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if ((i + 1) < n && (array1[i][j] >= array1[i + 1][j] || array2[i][j] >= array2[i + 1][j])) { System.out.println("Impossible"); return; } if ((j + 1) < m && (array1[i][j] >= array1[i][j + 1] || array2[i][j] >= array2[i][j + 1])) { System.out.println("Impossible"); return; } } } System.out.println("Possible"); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
05423a13406ab1930c402b2f5e9addc4
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
// Change Of Plans BABY.... Change Of Plans // import java.io.*; import java.util.*; import static java.lang.Math.*; public class doublematrix { static void MainSolution() { n = ni(); m = ni(); int a[][] = new int[n][m]; int b[][] = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = ni(); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) b[i][j] = ni(); int flag = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (i < n - 1 && (min(a[i][j], b[i][j]) >= min(a[i + 1][j], b[i + 1][j]))) { flag = 1; break; } if (i < n - 1 && (max(a[i][j], b[i][j]) >= max(a[i + 1][j], b[i + 1][j]))) { flag = 1; break; } if (j < m - 1 && (min(a[i][j], b[i][j]) >= min(a[i][j + 1], b[i][j + 1]))) { flag = 1; break; } if (j < m - 1 && (max(a[i][j], b[i][j]) >= max(a[i][j + 1], b[i][j + 1]))) { flag = 1; break; } } if (flag == 1) pl("Impossible"); else pl("Possible"); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //THE DON'T CARE ZONE BEGINS HERE...// static int mod9 = 1000000007; static int n, m, l, k, t, mod = 998244353; static AwesomeInput input = new AwesomeInput(System.in); static PrintWriter pw = new PrintWriter(System.out, true); static class AwesomeInput { private InputStream letsDoIT; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private AwesomeInput(InputStream ayega) { this.letsDoIT = ayega; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = letsDoIT.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int ForInteger() { 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 ForLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String ForString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } // functions to take input// static int ni() { return input.ForInteger(); } static String ns() { return input.ForString(); } static long nl() { return input.ForLong(); } static double nd() throws IOException { return Double.parseDouble(new BufferedReader(new InputStreamReader(System.in)).readLine()); } //functions to give output static void pl() { pw.println(); } static void p(Object o) { pw.print(o + " "); } static void pws(Object o) { pw.print(o + ""); } static void pl(Object o) { pw.println(o); } static void flush() { pw.flush(); pw.close(); } public static int[] fastSort(int[] f) { int n=f.length; int[] to = new int[n]; { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[f[i]&0xffff]++] = f[i]; int[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(f[i]>>>16)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[f[i]>>>16]++] = f[i]; int[] d = f; f = to;to = d; } return f; } public static void main(String[] args) { //threading has been used to increase the stack size. new Thread(null, null, "DHAGA", 1 << 25) //the last parameter is stack size desired. { public void run() { try { double s = System.currentTimeMillis(); MainSolution(); //pl(("\nExecution Time : "+((double) System.currentTimeMillis() - s) / 1000) + " s"); flush(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
288cb204bd3d9e18c582dfc52df286d2
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
/*Author: Satyajeet Singh, Delhi Technological University*/ import java.io.*; import java.util.*; import java.text.*; import java.lang.*; import java.math.*; public class Main{ /*********************************************Constants******************************************/ static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static long mod=(long)1e9+7; static long mod1=998244353; static boolean sieve[]; static ArrayList<Integer> primes; static ArrayList<Long> factorial; static HashSet<Pair> graph[]; /****************************************Solutions Begins***************************************/ static int A[][],B[][],n,m; static Boolean dp[][][]; static boolean dfs(int i,int j,int t){ if(i==n-1&&j==m-1){ return true; } if(dp[i][j][t]!=null){ return dp[i][j][t]; } boolean ans1=false,ans2=false; if(i==n-1){ ans1=true; } if(j==m-1){ ans2=true; } if(t==0){ if(i<n-1&&A[i][j]<A[i+1][j]&&B[i][j]<B[i+1][j]){ boolean bb=dfs(i+1,j,0); ans1=ans1||bb; } if(i<n-1&&A[i][j]<B[i+1][j]&&B[i][j]<A[i+1][j]){ boolean bb=dfs(i+1,j,1); ans1=ans1||bb; } if(j<m-1&&A[i][j]<A[i][j+1]&&B[i][j]<B[i][j+1]){ boolean bb=dfs(i,j+1,0); ans2=ans2||bb; } if(j<m-1&&A[i][j]<B[i][j+1]&&B[i][j]<A[i][j+1]){ boolean bb=dfs(i,j+1,1); ans2=ans2||bb; } } else{ if(i<n-1&&B[i][j]<A[i+1][j]&&A[i][j]<B[i+1][j]){ boolean bb=dfs(i+1,j,0); ans1=ans1||bb; } if(i<n-1&&B[i][j]<B[i+1][j]&&A[i][j]<A[i+1][j]){ boolean bb=dfs(i+1,j,1); ans1=ans1||bb; } if(j<m-1&&B[i][j]<A[i][j+1]&&A[i][j]<B[i][j+1]){ boolean bb=dfs(i,j+1,0); ans2=ans2||bb; } if(j<m-1&&B[i][j]<B[i][j+1]&&A[i][j]<A[i][j+1]){ boolean bb=dfs(i,j+1,1); ans2=ans2||bb; } } dp[i][j][t]=ans1&&ans2; // debug(i,j,t); // debug(dp[i][j][t]); return dp[i][j][t]; } public static void main (String[] args) throws Exception { String st[]=br.readLine().split(" "); n=Integer.parseInt(st[0]); m=Integer.parseInt(st[1]); A=new int[n][m]; B=new int[n][m]; for(int i=0;i<n;i++){ st=br.readLine().split(" "); for(int j=0;j<m;j++){ A[i][j]=Integer.parseInt(st[j]); } } for(int i=0;i<n;i++){ st=br.readLine().split(" "); for(int j=0;j<m;j++){ B[i][j]=Integer.parseInt(st[j]); } } dp=new Boolean[n][m][2]; for(Boolean bb[][]:dp){ for(Boolean b[]:bb){ Arrays.fill(b,null); } } boolean ans=dfs(0,0,0)||dfs(0,0,1); if(ans){ out.println("Possible"); } else{ out.println("Impossible"); } /****************************************Solutions Ends**************************************************/ out.flush(); out.close(); } /****************************************Template Begins************************************************/ /***************************************Precision Printing**********************************************/ static void printPrecision(double d){ DecimalFormat ft = new DecimalFormat("0.00000000000000000"); out.println(ft.format(d)); } /******************************************Graph*********************************************************/ static void Makegraph(int n){ graph=new HashSet[n]; for(int i=0;i<n;i++){ graph[i]=new HashSet<>(); } } static void addEdge(int a,int b,int c){ graph[a].add(new Pair(b,c)); } /*********************************************PAIR********************************************************/ static class PairComp implements Comparator<Pair>{ public int compare(Pair p1,Pair p2){ return p2.v-p1.v; } } static class Pair implements Comparable<Pair> { int u; int v; int index=-1; public Pair(int u, int v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return u == other.u && v == other.v; } public int compareTo(Pair other) { if(index!=other.index) return Long.compare(index, other.index); return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /******************************************Long Pair*************************************************/ static class PairCompL implements Comparator<Pairl>{ public int compare(Pairl p1,Pairl p2){ if(p1.v>p2.v){ return -1; } else if(p1.v<p2.v){ return 1; } else{ return 0; } } } static class Pairl implements Comparable<Pair> { long u; long v; int index=-1; public Pairl(long u, long v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return u == other.u && v == other.v; } public int compareTo(Pair other) { if(index!=other.index) return Long.compare(index, other.index); return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /*****************************************DEBUG***********************************************************/ public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } /************************************MODULAR EXPONENTIATION***********************************************/ static long modulo(long a,long b,long c) { long x=1; long y=a; while(b > 0){ if(b%2 == 1){ x=(x*y)%c; } y = (y*y)%c; // squaring the base b /= 2; } return x%c; } /********************************************GCD**********************************************************/ static long gcd(long x, long y) { if(x==0) return y; if(y==0) return x; long r=0, a, b; a = (x > y) ? x : y; // a is greater number b = (x < y) ? x : y; // b is smaller number r = b; while(a % b != 0) { r = a % b; a = b; b = r; } return r; } /******************************************SIEVE**********************************************************/ static void sieveMake(int n){ sieve=new boolean[n]; Arrays.fill(sieve,true); sieve[0]=false; sieve[1]=false; for(int i=2;i*i<n;i++){ if(sieve[i]){ for(int j=i*i;j<n;j+=i){ sieve[j]=false; } } } primes=new ArrayList<Integer>(); for(int i=0;i<n;i++){ if(sieve[i]){ primes.add(i); } } } /********************************************End***********************************************************/ }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
670ecfc45098e3ccf412650d6d527dad
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; public class Main { static int inf = 1000000000; static int mod = (int) 10e9 + 7; static long gcd(long a, long b) { return (a == 0) ? b : gcd(b % a, a); } static int rightBinSearch(int[] a, int key) { int l = -1; int r = a.length - 1; while (l < r) { int m = (l + r) / 2; if (a[m] > key) r = m - 1; else l = m; } return r; } static int leftBinSearch(int[] a, int key) { int l = 0; int r = a.length; while (l < r) { int m = (l + r) / 2; if (a[m] < key) l = m + 1; else r = m; } return r; } private static BufferedReader br; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); int[] nm = readArr(); int n = nm[0]; int m = nm[1]; int[][] a = new int[n][m], b = new int[n][m]; for (int i = 0; i < n; i++) { a[i] = readArr(); } for (int i = 0; i < n; i++) { b[i] = readArr(); } boolean ok = true; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] > b[i][j]){ int k = a[i][j]; a[i][j] = b[i][j]; b[i][j] = k; } } } for (int i = 0; i < n; i++) { for (int j = 1; j < m; j++) { if (a[i][j-1] >= a[i][j]||b[i][j-1] >= b[i][j]) ok = false; } } for (int i = 1; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i-1][j] >= a[i][j]||b[i-1][j] >= b[i][j]) ok = false; } } System.out.println(ok?"Possible":"Impossible"); } static int[] readArr() throws IOException { String[] s = br.readLine().split(" "); int[] ans = new int[s.length]; for (int i = 0; i < s.length; i++) { ans[i] = Integer.parseInt(s[i]); } return ans; } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
83124675bef7a2e69015602a12d46027
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; public class MainS { static final long MOD = 1_000_000_007, INF = 1_000_000_000_000_000_000L; static final int INf = 1_000_000_000; static FastReader reader; static PrintWriter writer; public static void main(String[] args) { Thread t = new Thread(null, new O(), "Integer.MAX_VALUE", 100000000); t.start(); } static class O implements Runnable { public void run() { try { magic(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void magic() throws IOException { reader = new FastReader(); writer = new PrintWriter(System.out, true); int n = reader.nextInt(), m = reader.nextInt(); int a[][] = new int[n][m]; int b[][] = new int[n][m]; for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { a[i][j] = reader.nextInt(); } } for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { b[i][j] = reader.nextInt(); } } boolean impossible = false; for(int i=0;i<n;++i) { for(int j=0;j+1<m;++j) { if(min(a[i][j], b[i][j])>=max(a[i][j+1], b[i][j+1])) { impossible = true; } } } for(int COL=0;COL<m;++COL) { for(int ROW=0;ROW+1<n;++ROW) { if(min(a[ROW][COL], b[ROW][COL])>=max(a[ROW+1][COL], b[ROW+1][COL])) { impossible = true; } } } for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { int aa = a[i][j]; int bb = b[i][j]; a[i][j] = min(aa,bb); b[i][j] = max(aa,bb); } } if(check(a,n,m) && check(b,n,m) && !impossible) { writer.println("Possible"); } else { writer.println("Impossible"); } } static boolean check(int mat[][], int n,int m) { for(int i=0;i<n;++i) { for(int j=0;j+1<m;++j) { if(mat[i][j]>=mat[i][j+1]) { return false; } } } for(int COL=0;COL<m;++COL) { for(int ROW=0;ROW+1<n;++ROW) { if(mat[ROW][COL]>=mat[ROW+1][COL]) { return false; } } } return true; } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
05cad942067119d3952a6c6ebd4327d0
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class First { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int[][] array1 = new int[n][m]; int[][] array2 = new int[n][m]; for(int i = 0; i < n; ++i){ st = new StringTokenizer(reader.readLine()); for(int j = 0; j < m; ++j){ array1[i][j] = Integer.parseInt(st.nextToken()); } } for(int i = 0; i < n; ++i){ st = new StringTokenizer(reader.readLine()); for(int j = 0; j < m; ++j){ array2[i][j] = Integer.parseInt(st.nextToken()); } } for(int i = 0; i < n; ++i){ for(int j = 0; j < m; ++j){ if(array1[i][j] > array2[i][j]){ int p = array1[i][j]; array1[i][j] = array2[i][j]; array2[i][j] = p; } } } for(int i = 0; i < n; ++i){ for(int j = 1; j < m; ++j){ if(array1[i][j-1] >= array1[i][j] || array2[i][j-1] >= array2[i][j]){ System.out.println("Impossible"); return; } } } for(int i = 0; i < m; ++i){ for(int j = 1; j < n; ++j){ if(array1[j-1][i] >= array1[j][i] || array2[j-1][i] >= array2[j][i]){ System.out.println("Impossible"); return; } } } System.out.println("Possible"); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
adba09b3902b3e963e50d237e7ac1ab4
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; import java.math.BigInteger; public class B { public static void main(String[] args) throws Exception { FastScanner scanner = new FastScanner(); int[] tmp = scanner.nextIntArray(); int n = tmp[0]; int m = tmp[1]; long[][] A = new long[n][]; long[][] B = new long[n][]; for (int i = 0; i < n; i++) { A[i] = scanner.nextLongArray(); } for (int i = 0; i < n; i++) { B[i] = scanner.nextLongArray(); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (i + 1 < n) { if (A[i][j] >= A[i + 1][j]) { if (!(B[i][j] < A[i + 1][j] && A[i][j] < B[i + 1][j])) { System.out.println("Impossible"); System.exit(0); } else { long t = A[i][j]; A[i][j] = B[i][j]; B[i][j] = t; } } } if (j + 1 < m) { if (A[i][j] >= A[i][j + 1]) { if (!(B[i][j] < A[i][j + 1] && A[i][j] < B[i][j + 1])) { System.out.println("Impossible"); System.exit(0); } else { long t = A[i][j]; A[i][j] = B[i][j]; B[i][j] = t; } } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (i + 1 < n) { if (B[i][j] >= B[i + 1][j]) { if (!(A[i][j] < B[i + 1][j] && B[i][j] < A[i + 1][j])) { System.out.println("Impossible"); System.exit(0); } else { long t = A[i][j]; A[i][j] = B[i][j]; B[i][j] = t; } } } if (j + 1 < m) { if (B[i][j] >= B[i][j + 1]) { if (!(A[i][j] < B[i][j + 1] && B[i][j] < A[i][j + 1])) { System.out.println("Impossible"); System.exit(0); } else { long t = A[i][j]; A[i][j] = B[i][j]; B[i][j] = t; } } } } } System.out.println("Possible"); } private static class FastScanner { private BufferedReader br; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public int[] nextIntArray() throws IOException { String line = br.readLine(); String[] strings = line.trim().split("\\s+"); int[] array = new int[strings.length]; for (int i = 0; i < array.length; i++) array[i] = Integer.parseInt(strings[i]); return array; } public long[] nextLongArray() throws IOException { String line = br.readLine(); String[] strings = line.trim().split("\\s+"); long[] array = new long[strings.length]; for (int i = 0; i < array.length; i++) array[i] = Long.parseLong(strings[i]); return array; } } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
219085745f7018877804fe09bec9ed8c
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.util.*; import java.lang.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub Reader.init(System.in); Main mm=new Main(); int n=Reader.nextInt(); int m=Reader.nextInt(); int[][] arr=new int[n][m]; int[][] arr1=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { arr[i][j]=Reader.nextInt(); } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { arr1[i][j]=Reader.nextInt(); } } int flag=1; for(int i=0;i<n;i++) { for(int j=0;j<m-1;j++) { if(arr[i][j]<arr1[i][j+1] && arr1[i][j]< arr[i][j+1] || arr1[i][j]<arr1[i][j+1] && arr[i][j]< arr[i][j+1]) { } else { flag=0; break; } } if(flag==0) { break; } } for(int i=0;i<n-1;i++) { for(int j=0;j<m;j++) { if(arr[i][j]<arr1[i+1][j] && arr1[i][j]< arr[i+1][j] || arr1[i][j]<arr1[i+1][j] && arr[i][j]< arr[i+1][j]) { } else { flag=0; break; } } if(flag==0) { break; } } if(flag==0) { System.out.println("Impossible"); } else { System.out.println("Possible"); } } } class pair{ int f; int s; pair(int f,int s){ this.f=f; this.s=s; } } class a implements Comparator<pair>{ public int compare(pair n1,pair n2) { return n1.s-n2.s; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
88739161a02854c6946956bccededd6f
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class q5 { public static void main(String[] args) throws IOException { Reader.init(System.in); PrintWriter out=new PrintWriter(System.out); StringBuffer output=new StringBuffer(""); String ans="Possible"; int n=Reader.nextInt();int m=Reader.nextInt(); int[][] arr1=new int[n][m]; int[][] arr2=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) arr1[i][j]=Reader.nextInt(); } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) arr2[i][j]=Reader.nextInt(); } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) if(arr2[i][j]<arr1[i][j]) { int temp=arr2[i][j]; arr2[i][j]=arr1[i][j]; arr1[i][j]=temp; } } for(int i=0;i<n;i++) { for(int j=1;j<m;j++) { if(arr1[i][j]<=arr1[i][j-1] || arr2[i][j]<=arr2[i][j-1]) ans="Impossible"; } } for(int j=0;j<m;j++) { for(int i=1;i<n;i++) { if(arr1[i][j]<=arr1[i-1][j] || arr2[i][j]<=arr2[i-1][j]) ans="Impossible"; } } output.append(ans); out.write(output.toString()); out.flush(); } } class NoD{ int a,b; String s; NoD(int aa,int bb){ a=aa;b=bb; s=a+" "+b; } @Override public boolean equals(Object o) { if(o!=null && o.getClass()==getClass()) { NoD c= (NoD) o; return c.a==a && c.b==b; } return false; } @Override public int hashCode() { return s.hashCode(); } } 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 nextLine() throws IOException{ return reader.readLine(); } static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
adee944a2ba11fd9bec35b2f605159f6
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
// Created by Whiplash99 import java.io.*; import java.util.*; public class B { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int i,N,j,tmp,flag=0; String s[]=br.readLine().trim().split(" "); N=Integer.parseInt(s[0]); int M=Integer.parseInt(s[1]); int a[][]=new int[N][M]; int b[][]=new int[N][M]; for(i=0;i<N;i++) { s=br.readLine().trim().split(" "); for(j=0;j<M;j++) a[i][j]=Integer.parseInt(s[j]); } for(i=0;i<N;i++) { s=br.readLine().trim().split(" "); for(j=0;j<M;j++) b[i][j]=Integer.parseInt(s[j]); } for(i=0;i<N;i++) { for(j=0;j<M;j++) { if(a[i][j]>b[i][j]) { tmp=a[i][j]; a[i][j]=b[i][j]; b[i][j]=tmp; } } } for(i=0;i<N;i++) { for(j=0;j<M-1;j++) { if(a[i][j]>=a[i][j+1]||b[i][j]>=b[i][j+1]) { flag=1; break; } } } for(j=0;j<M;j++) { for(i=0;i<N-1;i++) { if(a[i][j]>=a[i+1][j]||b[i][j]>=b[i+1][j]) { flag=1; break; } } } if(flag==0) System.out.println("Possible"); else System.out.println("Impossible"); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
d7262928695e9632af5fe110af3687d1
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.awt.Point; public class Code { //static final long MOD = 998244353L; //static final long INF = -1000000000000000007L; static final long MOD = 1000000007L; //static final int INF = 1000000007; public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int N = sc.ni(); int M = sc.ni(); int[][][] nums = new int[N][M][2]; for (int k = 0; k < 2; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { nums[i][j][k] = sc.ni(); } } } for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (nums[i][j][1] > nums[i][j][0]) { int temp = nums[i][j][1]; nums[i][j][1] = nums[i][j][0]; nums[i][j][0] = temp; } } } for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (i < N-1) { if (nums[i][j][0]>=nums[i+1][j][0]||nums[i][j][1]>=nums[i+1][j][1]) { pw.println("Impossible"); pw.close(); return; } } if (j < M-1) { if (nums[i][j][0]>=nums[i][j+1][0]||nums[i][j][1]>=nums[i][j+1][1]) { pw.println("Impossible"); pw.close(); return; } } } } pw.println("Possible"); pw.close(); } public static int[][] sort(int[][] array) { //Sort an array (immune to quicksort TLE) Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); int[] temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } Arrays.sort(array, new Comparator<int[]>() { @Override public int compare(int[] arr1, int[] arr2) { return arr1[0]-arr2[0]; //ascending order } }); return array; } public static long[][] sort(long[][] array) { //Sort an array (immune to quicksort TLE) Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); long[] temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } Arrays.sort(array, new Comparator<long[]>() { @Override public int compare(long[] arr1, long[] arr2) { return 0; } }); return array; } 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 ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
07d68c185c715e376e8be81431d32c5d
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.util.*; import java.io.*; /** * * @author alanl */ public class Main{ static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String[] args) throws IOException{ int n = readInt(), m = readInt(), arr[][] = new int[n][m], arr1[][] = new int[n][m]; for(int i = 0; i<n; i++){ for(int j = 0; j<m; j++){ arr[i][j] = readInt(); } } for(int i = 0; i<n; i++){ for(int j = 0; j<m; j++){ arr1[i][j] = readInt(); } } for(int i = 0; i<n; i++){ for(int j = 0; j<m; j++){ if(arr[i][j]>arr1[i][j]){ int sub = arr[i][j]; arr[i][j] = arr1[i][j]; arr1[i][j] = sub; } } } boolean a = check(arr), b = check(arr1); if(!a||!b)println("Impossible"); else println("Possible"); } static boolean check(int[][]a){ for(int i = 0; i<a.length; i++){ for(int j = 0; j<a[0].length; j++){ if(i!=0 && a[i][j]<=a[i-1][j])return false; if(j!=0 && a[i][j]<=a[i][j-1])return false; } } return true; } static String next () throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine().trim()); return st.nextToken(); } static long readLong () throws IOException { return Long.parseLong(next()); } static int readInt () throws IOException { return Integer.parseInt(next()); } static double readDouble () throws IOException { return Double.parseDouble(next()); } static char readChar () throws IOException { return next().charAt(0); } static String readLine () throws IOException { return input.readLine().trim(); } static void print(Object b) { System.out.print(b); } static void println(Object b) { System.out.println(b); } static void println() { System.out.println(); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
a4a12bbc72b8fe8e40c6d970b44b48c5
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
/* roses are red memes are neat all my test cases time out lmao yeet golions pls no hack me */ import java.util.*; import java.io.*; public class B { public static void main(String args[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int M = Integer.parseInt(st.nextToken()); int[][] mat1 = new int[N][M]; int[][] mat2 = new int[N][M]; for(int i=0; i < N; i++) { st = new StringTokenizer(infile.readLine()); for(int a=0; a < M; a++) mat1[i][a] = Integer.parseInt(st.nextToken()); } for(int i=0; i < N; i++) { st = new StringTokenizer(infile.readLine()); for(int a=0; a < M; a++) mat2[i][a] = Integer.parseInt(st.nextToken()); } //rows boolean bol = true; for(int a=0; a < N; a++) { ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int b=0; b < M; b++) { ls1.add(Math.min(mat1[a][b], mat2[a][b])); ls2.add(Math.max(mat1[a][b], mat2[a][b])); } bol = bol && (inc(ls1) && inc(ls2)); } //cols for(int b=0; b < M; b++) { ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int a=0; a < N; a++) { ls1.add(Math.min(mat1[a][b], mat2[a][b])); ls2.add(Math.max(mat1[a][b], mat2[a][b])); } bol = bol && (inc(ls1) && inc(ls2)); } String res = bol ? "Possible": "Impossible"; System.out.println(res); } public static boolean inc(ArrayList<Integer> ls) { HashSet<Integer> set = new HashSet<Integer>(); for(int a: ls) set.add(a); if(set.size() != ls.size()) return false; for(int i=0; i < ls.size()-1; i++) if(ls.get(i) >= ls.get(i+1)) return false; return true; } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
1eaffa80348788d9dee4dff37c157f0d
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner s=new Scanner(System.in); int n=s.nextInt(); int m=s.nextInt(); int[][] a=new int[n][m]; int[][] b=new int[n][m]; int min=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++){ a[i][j]=s.nextInt(); } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++){ b[i][j]=s.nextInt(); } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { min=a[i][j]; if(b[i][j]<min){ a[i][j]=b[i][j]; b[i][j]=min; } } } // for(int i=0;i<n;i++) // { // for(int j=0;j<m;j++) // System.out.println(b[i][j]); // } for(int i=0;i<n;i++){ for(int j=0;j<m-1;j++){ if(a[i][j]>=a[i][j+1]||b[i][j]>=b[i][j+1]){ System.out.println("Impossible"); return; } if(i<n-1){ //System.out.println("Im"); if(a[i][j]>=a[i+1][j]||b[i][j]>=b[i+1][j]){ System.out.println("Impossible"); return; } } } if(i<n-1){ // System.out.println("Im"); if(a[i][m-1]>=a[i+1][m-1]||b[i][m-1]>=b[i+1][m-1]){ System.out.println("Impossible"); return; } } } System.out.println("Possible"); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
6983def7e856f23aa316564ca16ceb96
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
// A Computer is Like a mischievous genie. // It will give you exactly what you ask for, // but not always what you want // A code by Rahul Verma import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.StringTokenizer; public class Main { public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); 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[] nextSArray() { String sr[] = null; try { sr = br.readLine().trim().split(" "); } catch (IOException e) { e.printStackTrace(); } return sr; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long powmodulo(long a, long p) { if (p == 0) return 1 % mod; if (p == 1) return a % mod; long ans = 1; while (p > 0) { if ((p & 1) > 0) { ans = (ans * a) % mod; } a = (a * a) % mod; p = p >> 1; } return ans % mod; } static long mod = 1000000007; static long gcd(long a,long b) { if(a==0) return b; return gcd(b%a,a); } static long fast_powerNumbers(long a,long n) { if(n==1) return a; long ans=fast_powerNumbers(a,n/2); if(n%2==0) return (ans*ans); else return ((ans*ans)*(a)); } static HashMap<Integer,ArrayList<Integer>>hmm; static int visited[]; static void dfs_helper(int root,int team) { ArrayList<Integer>al=hmm.get(root); visited[root]=team; for (Integer i :al) { if(visited[i]==0) dfs_helper(i,team); } } static void dfs(int n) { visited=new int[n+1]; int k=1; for (int i = 1; i <=n; i++) { if(visited[i]==0) { dfs_helper(i,k); ++k; } } } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); // int t1=sc.nextInt(); // for (int t = 0; t < t1; t++) { int n = sc.nextInt(); int m = sc.nextInt(); long arr[][]=new long[n][m]; long brr[][]=new long[n][m]; for (int i = 0; i < n ; i++) { for (int j = 0; j <m ; j++) { arr[i][j]=sc.nextLong(); } } for (int i = 0; i < n ; i++) { for (int j = 0; j <m ; j++) { brr[i][j]=sc.nextLong(); } } long a,b,a1,b1,ele,ele1; boolean ans=true; for (int i = 0; i < n ; i++) { for (int j = 0; j <m ; j++) { a=-1; b=-1; b1=-1; a1=-1; if(i>0) { a = arr[i-1][j]; a1 = brr[i-1][j]; } if(j>0) { b = arr[i][j-1]; b1 = brr[i][j-1]; } ele=Math.min(arr[i][j],brr[i][j]); ele1=Math.max(arr[i][j],brr[i][j]); if(ele>a && ele>b) { arr[i][j]=ele; } else { ans=false; break; } if(ele1>a1 && ele1 >b1) { brr[i][j]=ele1; } else { ans=false; break; } } } if (ans) { System.out.println("Possible"); } else { System.out.println("Impossible"); } }} class Pair { int a; int b; Pair(int a,int b) { this.a=a; this.b=b; } } class Graph { HashMap<Integer,ArrayList<Integer>>hm; Graph() { hm=new HashMap<>(); } Graph(int n){ hm=new HashMap<>(); for (int i = 1; i <=n ; i++) { hm.put(i,new ArrayList<Integer>()); } } // function for adding an edge................................................. public void addEdge(int a,int b,boolean isDir) { if(isDir) { if(hm.containsKey(a)) { hm.get(a).add(b); } else { hm.put(a,new ArrayList<>(Arrays.asList(b))); } } else { if(hm.containsKey(a)) { hm.get(a).add(b); } else if(!hm.containsKey(a)) { hm.put(a,new ArrayList<>(Arrays.asList(b))); } if(hm.containsKey(b)) { hm.get(b).add(a); } else if(!hm.containsKey(b)) { hm.put(b,new ArrayList<>(Arrays.asList(a))); } } } } // out.println(al.toString().replaceAll("[\\[|\\]|,]",""));
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
449937a6226a426f580e3da96b0be0c9
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.*; import java.util.*; public class Main { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException { Main m = new Main(); m.solve(); m.close(); } void close() throws IOException { pw.flush(); pw.close(); br.close(); } int[] readIntLine() throws IOException { String[] tokens = br.readLine().split(" "); int[] A = new int[tokens.length]; for (int i = 0; i < A.length; i++) A[i] = Integer.parseInt(tokens[i]); return A; } long[] readLongLine() throws IOException { String[] tokens = br.readLine().split(" "); long[] A = new long[tokens.length]; for (int i = 0; i < A.length; i++) A[i] = Long.parseLong(tokens[i]); return A; } void solve() throws IOException { int[] line = readIntLine(); int n = line[0]; int m = line[1]; int[][] A = new int[n][]; int[][] B = new int[n][]; for (int i = 0; i < n; i++) { A[i] = readIntLine(); } for (int i = 0; i < n; i++) { B[i] = readIntLine(); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (A[i][j] > B[i][j]) swap(A, B, i, j); } } for (int i = 0; i < n; i++) { for (int j = 1; j < m; j++) { if (A[i][j] <= A[i][j - 1] || B[i][j] <= B[i][j - 1]) { pw.println("Impossible"); return; } } } for (int j = 0; j < m; j++) { for (int i = 1; i < n; i++) { if (A[i][j] <= A[i - 1][j] || B[i][j] <= B[i - 1][j]) { pw.println("Impossible"); return; } } } pw.println("Possible"); } void swap(int[][] A, int[][] B, int i, int j) { int tmp = A[i][j]; A[i][j] = B[i][j]; B[i][j] = tmp; } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
18df366aba3c7b36a08b8f528c70a53e
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
//package com.karancp; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.InputStream; public class karan { public static void main(String args[]) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader z = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); task solver =new task(); solver.solve(z,out); out.close(); } static class task { static int gcd(int a,int b) { if(b==0)return a; else return gcd(b,a%b); } static int lcm(int a,int b) { int pp=a*b; int bb=gcd(a,b); return pp/bb; } static int max(int a,int b) { return Math.max(a,b); } static int min(int a,int b) { return Math.min(a,b); } static int cfor(int ar[][],int br[][],int n,int m) { int i,j; int fl=0; for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(i+1<n) { if(ar[i][j]>=ar[i+1][j])fl=1; } if(j+1<m) { if(ar[i][j]>=ar[i][j+1])fl=1; } //System.out.println(ar[i][j]); } } for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(i+1<n) { if(br[i][j]>=br[i+1][j])fl=1; } if(j+1<m) { if(br[i][j]>=br[i][j+1])fl=1; } } } return fl; } public static void solve(InputReader z, PrintWriter out) { int n=z.nextInt(); int m=z.nextInt(); int ar[][]=new int[n][m]; int br[][]=new int[n][m]; int i; int j; for(i=0;i<n;i++) { for(j=0;j<m;j++) { ar[i][j]=z.nextInt(); } } for(i=0;i<n;i++) { for(j=0;j<m;j++) { br[i][j]=z.nextInt(); } } for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(ar[i][j]>br[i][j]) { int temp=ar[i][j]; ar[i][j]=br[i][j]; br[i][j]=temp; } } } int ans=cfor(ar,br,n,m); if(ans==0) { out.print("Possible"); } else { int temp=ar[n-1][m-1]; ar[n-1][m-1]=br[n-1][m-1]; br[n-1][m-1]=temp; ans=cfor(ar,br,n,m); if(ans==0)out.println("Possible"); else out.println("Impossible"); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
ec7a99d4363630d1d92e66f4339e36ba
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); BDoubleMatrix solver = new BDoubleMatrix(); solver.solve(1, in, out); out.close(); } static class BDoubleMatrix { public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); int m = in.scanInt(); int[][] ar1 = new int[n + 1][m + 1]; int[][] ar2 = new int[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) ar1[i][j] = Integer.MIN_VALUE / 3; else ar1[i][j] = in.scanInt(); } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) ar2[i][j] = Integer.MIN_VALUE / 3; else ar2[i][j] = in.scanInt(); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (ar1[i][j] >= ar2[i][j]) { ar1[i][j] = ar1[i][j] + ar2[i][j] - (ar2[i][j] = ar1[i][j]); } if (ar1[i][j] <= ar1[i - 1][j] || ar1[i][j] <= ar1[i][j - 1] || ar2[i][j] <= ar2[i - 1][j] || ar2[i][j] <= ar2[i][j - 1]) { out.println("Impossible"); return; } } } out.println("Possible"); } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int index; private BufferedInputStream in; private int total; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (index >= total) { index = 0; try { total = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (total <= 0) return -1; } return buf[index++]; } public int scanInt() { int integer = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } } return neg * integer; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
1f495223a35d2c7cf1a6e94d14774d33
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Solution { PrintWriter out = new PrintWriter(System.out); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""); String next() throws IOException { if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } long nl() throws IOException { return Long.parseLong(next()); } void solve() throws IOException { int N=ni(); int M=ni(); int[][] A=new int[N][M]; int[][] B=new int[N][M]; for (int n=0;n<N;n++) { for (int m=0;m<M;m++) A[n][m]=ni(); } for (int n=0;n<N;n++) { for (int m=0;m<M;m++) B[n][m]=ni(); } boolean F=true; int a,b,c,d; LP: for (int n=0;n<N;n++) { for (int m=0;m<M;m++) { if (n<N-1) { a=A[n][m]; b=B[n][m]; c=A[n+1][m]; d=B[n+1][m]; if (!(((c>a)||(c>b))&&((d>a)&&(d>b)))) { if (!(((d>a)||(d>b))&&((c>a)&&(c>b)))) { F=false; break LP; } } } if (m<M-1) { a=A[n][m]; b=B[n][m]; c=A[n][m+1]; d=B[n][m+1]; if (!(((c>a)||(c>b))&&((d>a)&&(d>b)))) { if (!(((d>a)||(d>b))&&((c>a)&&(c>b)))) { F=false; break LP; } } } } } if (F) out.println("Possible"); else out.println("Impossible"); out.flush(); } public static void main(String[] args) throws IOException { new Solution().solve(); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
1c06820cc014b178dbc4804eda7ce94a
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class fast implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class pair implements Comparable<pair>{ int x; int y; pair(int xi, int yi){ x=xi; y=yi; } @Override public int compareTo(pair other){ if(this.x>other.x){return 1;} if(this.x<other.x){return -1;} if(this.y>other.y){return 1;} if(this.y<other.y){return -1;} return 0; } } class dist implements Comparable<dist>{ int x; int y; int z; dist(int xi, int yi, int zi){ x=xi; y=yi; z=zi; } @Override public int compareTo(dist other){ if(this.z>other.z){return 1;} if(this.z<other.z){return -1;} return 0; } } public static void main(String args[]) throws Exception { new Thread(null, new fast(),"fast",1<<26).start(); } public void sortbyColumn(int arr[][], final int col){ // Using built-in sort function Arrays.sort Arrays.sort(arr, new Comparator<int[]>() { @Override // Compare values according to columns public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; if(entry1[col] < entry2[col]) return -1; return 0; } }); // End of function call sort(). } public void sortbyColumn(long arr[][], final int col){ // Using built-in sort function Arrays.sort Arrays.sort(arr, new Comparator<long[]>() { @Override // Compare values according to columns public int compare(final long[] entry1, final long[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; if(entry1[col] < entry2[col]) return -1; return 0; } }); // End of function call sort(). } public void sort(int ar[]){ int n=ar.length; Integer arr[]=new Integer[n]; for(int i=0;i<n;i++){ arr[i]=ar[i]; } Arrays.sort(arr); for(int i=0;i<n;i++){ ar[i]=arr[i].intValue(); } } public void sort(long ar[]){ int n=ar.length; Long arr[]=new Long[n]; for(int i=0;i<n;i++){ arr[i]=ar[i]; } Arrays.sort(arr); for(int i=0;i<n;i++){ ar[i]=arr[i].longValue(); } } long power(long x, long y, long p){ long res = 1; x = x % p; while (y > 0) { if((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} public void run(){ InputReader s = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n=s.nextInt(),m=s.nextInt(),a[][]=new int[n][m],b[][]=new int[n][m],flag=0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ a[i][j]=s.nextInt(); } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ b[i][j]=s.nextInt(); } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(a[i][j]>b[i][j]){ int tem=a[i][j]; a[i][j]=b[i][j]; b[i][j]=tem; } } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if((i>0 && a[i-1][j]>=a[i][j])||(i<n-1 && a[i+1][j]<=a[i][j])||(j>0 && a[i][j-1]>=a[i][j])||(j<m-1 && a[i][j+1]<=a[i][j])||(i>0 && b[i-1][j]>=b[i][j])||(i<n-1 && b[i+1][j]<=b[i][j])||(j>0 && b[i][j-1]>=b[i][j])||(j<m-1 && b[i][j+1]<=b[i][j])){ flag=1; } } }w.println(flag==0?"Possible":"Impossible"); w.close(); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
1dcf6529e730f126bcdc1d09d618f338
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import javax.print.attribute.HashAttributeSet; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.util.stream.IntStream; import java.awt.geom.Line2D; public class Solutiona { InputStream is; PrintWriter out; String INPUT = ""; long mod = (long)(1e9 + 7), inf = (long)(3e18); void solve(){ int n=ni(); int m=ni(); int a[][]=new int[n][m]; int b[][]=new int[n][m]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ a[i][j]=ni(); } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ b[i][j]=ni(); } } boolean flag=false; outer: for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(i==0 && j==0) continue; else if(i==0){ if(a[i][j]>a[i][j-1] && b[i][j]>b[i][j-1]){ int w=a[i][j-1]; int x=b[i][j-1]; int y=a[i][j]; int z=b[i][j]; if(w<x){ if(y>z){ a[i][j]=z; b[i][j]=y; } } else if(w>x){ if(y<z){ a[i][j]=z; b[i][j]=y; } } } else{ int w=a[i][j-1]; int x=b[i][j-1]; int y=a[i][j]; int z=b[i][j]; if(w<x){ if(y>z){ a[i][j]=z; b[i][j]=y; } } else if(w>x){ if(y<z){ a[i][j]=z; b[i][j]=y; } } if(a[i][j]<=a[i][j-1] || b[i][j]<=b[i][j-1]){ flag=true; break outer; } } } else if(j==0){ if(a[i][j]>a[i-1][j] && b[i][j]>b[i-1][j]){ int w=a[i-1][j]; int x=b[i-1][j]; int y=a[i][j]; int z=b[i][j]; if(w<x){ if(y>z){ a[i][j]=z; b[i][j]=y; } } else if(w>x){ if(y<z){ a[i][j]=z; b[i][j]=y; } } } else{ int w=a[i-1][j]; int x=b[i-1][j]; int y=a[i][j]; int z=b[i][j]; if(w<x){ if(y>z){ a[i][j]=z; b[i][j]=y; } } else if(w>x){ if(y<z){ a[i][j]=z; b[i][j]=y; } } if(a[i][j]<=a[i-1][j] || b[i][j]<=b[i-1][j]){ flag=true; break outer; } } } else{ if(a[i][j]>a[i][j-1] && b[i][j]>b[i][j-1] && a[i][j]>a[i-1][j] && b[i][j]>b[i-1][j]){ int w=a[i][j-1]; int x=b[i][j-1]; int y=a[i][j]; int z=b[i][j]; int u=a[i-1][j]; int v=b[i-1][j]; if(w<x && u<v){ if(y>z){ a[i][j]=z; b[i][j]=y; } } else if(w>x && u>v){ if(y<z){ a[i][j]=z; b[i][j]=y; } } } else{ int w=a[i][j-1]; int x=b[i][j-1]; int y=a[i][j]; int z=b[i][j]; int u=a[i-1][j]; int v=b[i-1][j]; if(w<x && u<v){ if(y>z){ a[i][j]=z; b[i][j]=y; } } else if(w>x && u>v){ if(y<z){ a[i][j]=z; b[i][j]=y; } } if(a[i][j]<=a[i][j-1] || b[i][j]<=b[i][j-1] || a[i][j]<=a[i-1][j] || b[i][j]<=b[i-1][j]){ flag=true; break outer; } } } } } // for(int i=0;i<n;i++){ // for(int j=0;j<m;j++){ // out.print(a[i][j]+" "); // } // out.println(); // } // for(int i=0;i<n;i++){ // for(int j=0;j<m;j++){ // out.print(b[i][j]+" "); // } // out.println(); // } if(flag){ out.println("Impossible"); } else{ out.println("Possible"); } } void run() throws Exception{ is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } private boolean isPrime(long n) { if(n <= 1) return false; if(n <= 3) return true; if(n%2 == 0 || n%3 == 0) return false; for(long i = 5; i * i <= n; i += 6) { if(n % i == 0 || n % (i + 2) == 0) return false; } return true; } int P[], S[], SZ, NP = 15485900; boolean isP[]; private int log2(int n){ if(n <= 0) throw new IllegalArgumentException(); return 31 - Integer.numberOfLeadingZeros(n); } private int sieve() { int i, j, n = NP; isP = new boolean[n]; S = new int[n]; for(i = 3; i < n; i += 2) { isP[i] = true; S[i-1] = 2; S[i] = i; } isP[2] = true; S[1] = 1; //(UD) for(i = 3; i * i <= n; i += 2) { if( !isP[i] ) continue; for(j = i * i; j < n; j += i) { isP[j] = false; if(S[j] == j) S[j] = i; } } P = new int[n]; P[0] = 2; j = 1; for(i = 3; i < n; i += 2) { if ( isP[i] ) P[j++] = i; } P = Arrays.copyOf(P, j); return j; } private long gcd(long a,long b){ while (a!=0 && b!=0) { if(a>b) a%=b; else b%=a; } return a+b; } private long mp(long b, long e, long mod) { b %= mod; long r = 1; while(e > 0) { if((e & 1) == 1) { r *= b; r %= mod; } b *= b; b %= mod; e >>= 1; } return r; } public static void main(String[] args) throws Exception { new Solutiona().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 String nsl() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!isSpaceChar(b) || 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 long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); 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
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
5906f845d1e76ea7f979466c7fadc997
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author ddebettencourt20 */ public class DoubleMatrix { public static void main(String[] args) { MyScanner sc = new MyScanner(); int rows = sc.nextInt(); int cols = sc.nextInt(); int[][] matrix = new int[rows][cols]; int[][] matrix2 = new int[rows][cols]; for (int i = 0; i<rows; i++){ for (int j = 0; j<cols; j++){ matrix[i][j] = sc.nextInt(); } } for (int i = 0; i<rows; i++){ for (int j = 0; j<cols; j++){ matrix2[i][j] = sc.nextInt(); } } for (int i = 0; i<rows; i++){ for (int j = 0; j<cols; j++){ if (matrix2[i][j] < matrix[i][j]){ int temp = matrix[i][j]; matrix[i][j] = matrix2[i][j]; matrix2[i][j] = temp; } } } for (int i = 0; i<rows; i++){ for (int j = 0; j < cols; j++){ if (i > 0 && matrix[i][j] <= matrix[i-1][j]){ System.out.println("Impossible"); System.exit(0); } if (j > 0 && matrix[i][j] <= matrix[i][j-1]){ System.out.println("Impossible"); System.exit(0); } if (i > 0 && matrix2[i][j] <= matrix2[i-1][j]){ System.out.println("Impossible"); System.exit(0); } if (j > 0 && matrix2[i][j] <= matrix2[i][j-1]){ System.out.println("Impossible"); System.exit(0); } } } System.out.println("Possible"); } 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
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
a4f196be95c8da4658136efda67f1e9b
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
//package competitive; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class B1162 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void fl() { System.out.flush(); } public static void main(String[] args) { FastReader rd=new FastReader(); int n=rd.nextInt(),m=rd.nextInt(); int i,j,temp; int m1[][]=new int[n][m]; int m2[][]=new int[n][m]; for(i=0;i<n;++i) for(j=0;j<m;++j) m1[i][j]=rd.nextInt(); for(i=0;i<n;++i) for(j=0;j<m;++j) {m2[i][j]=rd.nextInt(); if(m1[i][j]<m2[i][j]) { m1[i][j]+=m2[i][j]; m2[i][j]=m1[i][j]-m2[i][j]; m1[i][j]=m1[i][j]-m2[i][j]; } } boolean flag=true; for(i=0;i<n;++i) for(j=0;j<m;++j) { if(j<m-1 && (m1[i][j]>=m1[i][j+1] || m2[i][j]>=m2[i][j+1])) {flag=false;break;} if(i<n-1 && (m1[i][j]>=m1[i+1][j] || m2[i][j]>=m2[i+1][j])) {flag=false;break;} } if(flag) System.out.print("Possible"); else System.out.print("Impossible"); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
696bcda96fb4178a80ebf64fe0b0fcc3
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import org.omg.PortableInterceptor.INACTIVE; import java.io.*; import java.math.*; import java.util.*; @SuppressWarnings("Duplicates") // author @mdazmat9 public class codeforces{ static long abs=1000000007; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int test = 1; for (int ind = 0; ind < test; ind++) { int n=sc.nextInt(); int m=sc.nextInt(); int [][]a=new int[n][m]; int [][]b=new int[n][m]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ a[i][j]=sc.nextInt(); } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ b[i][j]=sc.nextInt(); } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ int x = a[i][j]; int y = b[i][j]; a[i][j] = Math.min(x, y); b[i][j] = Math.max(x, y); } } boolean ok = check(n, m, a) && check(n, m, b); out.print(ok ? "Possible" : "Impossible"); } out.flush(); } static boolean check(int n, int m, int[][] a) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (i > 0 && !(a[i][j] > a[i - 1][j])) { return false; } if (j > 0 && !(a[i][j] > a[i][j - 1])) { return false; } } } return true; } public static double logb( double a, double b ) { return Math.log(a) / Math.log(b); } static long fast_pow(long a, long b) { if(b == 0) return 1L; long val = fast_pow(a, b / 2); if(b % 2 == 0) return val * val % mod; else return val * val % mod * a % mod; } static long mod = (long)1e9 + 7; static void shuffle(int[] a) { int n = a.length; for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } static long gcd(long a , long b) { if(b == 0) return a; return gcd(b , a % b); } } class Scanner { public BufferedReader reader; public StringTokenizer st; public Scanner(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); st = null; } public String next() { while (st == null || !st.hasMoreTokens()) { try { String line = reader.readLine(); if (line == null) return null; st = new StringTokenizer(line); } catch (Exception e) { throw (new RuntimeException()); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } class OutputWriter { BufferedWriter writer; public OutputWriter(OutputStream stream) { writer = new BufferedWriter(new OutputStreamWriter(stream)); } public void print(int i) throws IOException { writer.write(i); } public void print(String s) throws IOException { writer.write(s); } public void print(char[] c) throws IOException { writer.write(c); } public void close() throws IOException { writer.close(); } } class Pair { long x; long y; // Constructor public Pair(long x, long y) { this.x = x; this.y = y; } } class Compare { static void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return (int)((p2.x - p2.y)-(p1.x-p1.y)); } }); } } class Graph { int V; ArrayList<Integer>[] adjListArray; // constructor Graph(int V) { this.V = V; adjListArray = new ArrayList[V]; for (int i = 0; i < V; i++) { adjListArray[i] = new ArrayList<>(); } } // Adds an edge to an undirected graph void addEdge(int src, int dest) { adjListArray[src].add(dest); adjListArray[dest].add(src); } ArrayList<Integer> DFSUtil(int u, boolean visited[], int parent,ArrayList<Integer> list) { // Mark the current node as visited visited[u] = true; for (int i = 0; i < adjListArray[u].size(); i++) { int n = adjListArray[u].get(i); list.add(n); if (!visited[i]) { DFSUtil(n, visited, u,list); } } return list; } // The function to do DFS traversal. It uses recursive DFSUtil() void DFS(int r,int c) { // Mark all the vertices as not visited(set as // false by default in java) boolean visited[] = new boolean[V]; ArrayList<Integer> list=new ArrayList<>(); // Call the recursive helper function to print DFS traversal boolean bool=true; } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
1d396532867450cf3a142814c2ca485b
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CF1162B { public static void main(String[] args) { FastReader input = new FastReader(); int n = input.nextInt(); int m = input.nextInt(); int[][] arr = new int[n][m]; int[][] arr2 = new int[n][m]; for(int i = 0;i < n;i++){ for(int j = 0;j < m;j++){ arr[i][j] = input.nextInt(); } } for(int i = 0;i < n;i++){ for(int j = 0;j < m;j++){ arr2[i][j] = input.nextInt(); } } for(int i = 0;i < n;i++){ for(int j = 0;j < m;j++){ int max = Math.max(arr[i][j],arr2[i][j]); int min = Math.min(arr[i][j],arr2[i][j]); arr[i][j] = max; arr2[i][j] = min; } } for(int i = 0;i < n;i++){ for(int j = 1;j < m;j++){ if(arr[i][j] <= arr[i][j-1] || arr2[i][j] <= arr2[i][j-1]){ System.out.println("Impossible"); return; } } } for(int i = 0;i < m;i++){ for(int j = 1;j < n;j++){ if(arr[j][i] <= arr[j-1][i] || arr2[j][i] <= arr2[j-1][i]){ System.out.println("Impossible"); return; } } } System.out.println("Possible"); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
ccf9e0345da1f2b0b6237657a0fed8c5
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class Doublee { 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 sc = new FastReader(); OutputStream outputstream = System.out; PrintWriter out = new PrintWriter(outputstream); int n = sc.nextInt(); int m = sc.nextInt(); int[][] arr1 = new int[n+1][m+1]; int[][] arr2 = new int[n+1][m+1]; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { arr1[i][j] = sc.nextInt(); } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { arr2[i][j] = sc.nextInt(); } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(arr1[i][j]<=arr1[i-1][j] || arr1[i][j]<=arr1[i][j-1]) { if(arr2[i][j]<=arr1[i][j]) { out.println("Impossible"); out.close(); return; } else { if(arr2[i][j]>arr1[i-1][j] && arr2[i][j]>arr1[i][j-1]) { int tmp = arr1[i][j]; arr1[i][j]=arr2[i][j]; arr2[i][j]=tmp; } else { out.println("Impossible"); out.close(); return; } } } else { if(arr2[i][j]<arr1[i][j]) { if(arr2[i][j]>arr1[i-1][j] && arr2[i][j]>arr1[i][j-1]) { int tmp = arr1[i][j]; arr1[i][j]=arr2[i][j]; arr2[i][j]=tmp; } } } } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(arr1[i][j]<=arr1[i-1][j] || arr1[i][j]<=arr1[i][j-1]) { out.println("Impossible"); out.close(); return; } } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(arr2[i][j]<=arr2[i-1][j] || arr2[i][j]<=arr2[i][j-1]) { out.println("Impossible"); out.close(); return; } } } /*for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { out.print(arr1[i][j] + " "); } out.println(); } out.println(); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { out.print(arr2[i][j] + " "); } out.println(); }*/ out.println("Possible"); out.close(); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
028cb1f4093057395310fecfa3a414bb
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.util.*; public class Main { public static void swap(int i, int j, int[][] a, int[][] b) { int temp = a[i][j]; a[i][j]=b[i][j]; b[i][j]=temp; } public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int m = s.nextInt(); int[][] a = new int[n][m]; int[][] b = new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=s.nextInt(); } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { b[i][j]=s.nextInt(); } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]>b[i][j]) swap(i, j, a, b); } } for(int i=0;i<n;i++) { for(int j=0;j<m-1;j++) { if(a[i][j]>=a[i][j+1]) { System.out.println("Impossible"); return; } if(b[i][j]>=b[i][j+1]) { System.out.println("Impossible"); return; } } } for(int i=0;i<m;i++) { for(int j=0;j<n-1;j++) { if(a[j][i]>=a[j+1][i]) { System.out.println("Impossible"); return; } if(b[j][i]>=b[j+1][i]) { System.out.println("Impossible"); return; } } } System.out.println("Possible"); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
6fb278579bf0d578ae2bc80dd6fe3519
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class B { final int INF = 1_000_000_000; void solve() { int n = readInt(); int m = readInt(); int[][][] a = new int[2][n][m]; for(int i = 0;i<n;i++){ for(int j = 0;j<m;j++){ a[0][i][j] = readInt(); } } for(int i = 0;i<n;i++){ for(int j = 0;j<m;j++){ a[1][i][j] = readInt(); } } for(int i = 0;i<n;i++){ for(int j = 0;j<m;j++){ int x = a[0][i][j]; int y = a[1][i][j]; for(int c = 0;c<=i;c++){ for(int q = 0;q<=j;q++){ int num1 = -2; int num2 = -2; if(c == i && q == j) continue; if(a[0][c][q] < x) num1 = 0; if(a[1][c][q] < x && num1 != -2) num1 = -1; else if(a[1][c][q] < x) num1 = 1; if(a[0][c][q] < y) num2 = 0; if(a[1][c][q] < y && num2 != -2) num2 = -1; else if(a[1][c][q] < y) num2 = 1; if((num1 != -1 && (num1 == num2)) || num1 == -2 || num2 == -2){ out.print("Impossible"); return; } } } } } out.print("Possible"); } public static void main(String[] args) { new B().run(); } private void run() { try { init(); solve(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } private BufferedReader in; private StringTokenizer tok = new StringTokenizer(""); private PrintWriter out; private void init() { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); // try { // in = new BufferedReader(new FileReader("absum.in")); // out = new PrintWriter(new File("absum.out")); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } } private String readLine() { try { return in.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } private String readString() { while (!tok.hasMoreTokens()) { String nextLine = readLine(); if (nextLine == null) return null; tok = new StringTokenizer(nextLine); } return tok.nextToken(); } private int readInt() { return Integer.parseInt(readString()); } private long readLong() { return Long.parseLong(readString()); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
08e2361e2dff26e5d4400d92f47f4c78
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.FileInputStream; import java.util.Arrays; import java.util.Scanner; import static java.lang.Math.min; public class TaskB { int n,m; int[][] m1, m2; void solve() throws Exception{ //Scanner sc = new Scanner(new FileInputStream("inputs/b.in")); Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); m1 = new int[n][m]; m2 = new int[n][m]; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ m1[i][j] = sc.nextInt(); } } for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ m2[i][j] = sc.nextInt(); } } for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ if(m1[i][j] > m2[i][j]){ int t = m1[i][j]; m1[i][j] = m2[i][j]; m2[i][j] = t; } } } boolean f = true; for(int j=0; j<m && f; j++){ for(int i=1; i<n && f; i++){ if(m1[i][j] <= m1[i-1][j] || m2[i][j] <= m2[i-1][j]){ f = false; break; } } } for(int i=0; i<n && f; i++){ for(int j=1; j<m && f; j++){ if(m1[i][j] <= m1[i][j-1] || m2[i][j] <= m2[i][j-1]){ f = false; break; } } } if(f) System.out.println("Possible"); else System.out.println("Impossible"); } public static void main(String[] args) throws Exception { TaskB s = new TaskB(); s.solve(); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output