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
761650dd9805a3cbdf6931b05aab35e1
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k > 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; public class q2 { static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); while(t-- > 0) { int n = s.nextInt(); int m = s.nextInt(); int[][] inp = new int[n][m]; int flag = 0; for(int i=0;i<n;++i) for(int j=0;j<m;++j) { inp[i][j] = s.nextInt(); if(((i == 0 && j==0) || (i==0 && j == m-1) || (i == n-1 && j == 0) || (i == n-1 && j == m-1)) && inp[i][j] > 2) flag = 1; else if((i == 0 || i == n-1 || j == 0 || j == m-1) && inp[i][j] >3) flag = 1; else if(inp[i][j] >4) flag = 1; } if(flag == 1) out.println("NO"); else { out.println("YES"); for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { if((i == 0 && j==0) || (i==0 && j == m-1) || (i == n-1 && j == 0) || (i == n-1 && j == m-1)) inp[i][j] = 2; else if(i == 0 || i == n-1 || j == 0 || j == m-1) inp[i][j] = 3; else inp[i][j] =4; } } for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { out.print(inp[i][j]+" "); } out.println(); } } } out.flush(); out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
a6756e5af92a859cf8e466dc48b6b8c2
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.util.Scanner; public class B_Neighbor_Grid { static Scanner sc = new Scanner(System.in); static int n,m; static int[][] a; public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) { n = sc.nextInt(); m = sc.nextInt(); a = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = sc.nextInt(); mine(); } } public static void mine() { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int c = 0; if (i > 0) c++; if (j > 0) c++; if (i < n - 1) c++; if (j < m - 1) c++; if (a[i][j] > c) { System.out.println("NO"); return; } else a[i][j] = c; } } System.out.println("YES"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) System.out.print(a[i][j] + " "); System.out.println(); } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
b8a9ca97cda716a3553f87c3f73a5af4
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; public class LongInteger { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(reader.readLine()); while (t-- > 0) { //int n = Integer.parseInt(reader.readLine()); String input1 = reader.readLine(); String[] nk = input1.split(" "); int n = Integer.parseInt(nk[0]); int m = Integer.parseInt(nk[1]); /*String[] nk = input1.split(" "); int k = Integer.parseInt(nk[1]); String input2 = reader.readLine(); String[] splits = input2.split(" "); int[] in = new int[splits.length]; for( int i = 0; i < splits.length;i++){ in[i] = Integer.parseInt(splits[i]); }*/ /*String in = reader.readLine(); String bin = reader.readLine();*/ //solve(Integer.parseInt(splits[0]),Integer.parseInt(splits[1]),Integer.parseInt(splits[2])); //String[] splits = input1.split(" "); int[][] in = new int[n][m]; for( int i = 0; i < n;i++){ String[] splits = reader.readLine().split(" "); for( int j = 0; j < m;j++){ in[i][j] = Integer.parseInt(splits[j]); } } solve(in); //solve(in.split(""), bin.split("")); } } private static void solve(int[][] st) { int n = st.length; int m = st[0].length; for(int i = 0 ;i < n; i++) { for(int j = 0 ;j < m; j++) { int cnt = 0; if(st[i][j] == 0) { if(i+1<n && st[i+1][j]>0) cnt++; if(i-1>=0 && st[i-1][j]>0) cnt++; if(j+1<m && st[i][j+1]>0) cnt++; if(j-1>=0 && st[i][j-1]>0) cnt++; st[i][j] = cnt; } } } for(int i = 0;i < n;i++) { for(int j = 0; j < m; j++) { int cnt = 0; if(st[i][j] > 0) { if(i+1<n && st[i+1][j]>0) cnt++; if(i-1>=0 && st[i-1][j]>0) cnt++; if(j+1<m && st[i][j+1]>0) cnt++; if(j-1>=0 && st[i][j-1]>0) cnt++; if(cnt < st[i][j]) { System.out.println("NO"); return; } else { st[i][j] = cnt; } } } } System.out.println("YES"); for(int[] row : st) { for(int col : row) { System.out.print(col+" "); } System.out.println(); } //System.out.println(); } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
7949eb08d8a4c481cf0c8f90c209a141
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.*; import java.util.*; public class Solution { public static void main(String args[]) throws IOException { InOut inout = new InOut(); Resolver resolver = new Resolver(inout); resolver.solve(); inout.flush(); } private static class Resolver { final long LONG_INF = (long) 1e18; final int INF = (int) (1e9 + 7); final int MOD = 998244353; long f[]; InOut inout; Resolver(InOut inout) { this.inout = inout; } void initF(int n) { f = new long[n + 1]; f[1] = 1; for (int i = 2; i <= n; i++) { f[i] = (f[i - 1] * i) % MOD; } } int d[] = {0, -1, 0, 1, 0}; boolean legal(int r, int c, int n, int m) { return r >= 0 && r < n && c >= 0 && c < m; } void solve() throws IOException { int tt = 1; boolean hvt = true; if (hvt) { tt = nextInt(); } for (int cs = 1; cs <= tt; cs++) { long rs = 0; int n = nextInt(); int m = nextInt(); int g[][] = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { g[i][j] = nextInt(); } } boolean ok = true; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int cnt = 0; if (g[i][j] == 0) { continue; } for (int k = 0; k < 4; k++) { int r = i + d[k]; int c = j + d[k + 1]; if (legal(r, c, n, m) && g[r][c] > 0) { cnt++; } } if (cnt > g[i][j]) { g[i][j] = cnt; } else if (cnt < g[i][j]) { for (int k = 0; k < 4 && cnt < g[i][j]; k++) { int r = i + d[k]; int c = j + d[k + 1]; if (legal(r, c, n, m) && g[r][c] == 0) { int tmp = 0; for (int l = 0; l < 4; l++) { int rr = r + d[l]; int cc = c + d[l + 1]; if (legal(rr, cc, n, m) && g[rr][cc] > 0) { tmp++; } } g[r][c] = tmp; cnt++; } } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int cnt = 0; if (g[i][j] == 0) continue; for (int k = 0; k < 4; k++) { int r = i + d[k]; int c = j + d[k + 1]; if (legal(r, c, n, m) && g[r][c] > 0) { cnt++; } } if (cnt < g[i][j]) { ok = false; } g[i][j] = cnt; } } if (ok) { format("YES\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { format("%d%s", g[i][j] , j == m - 1 ? "\n" : " "); } } } else { format("NO\n"); } // format("%d", rs); if (cs < tt) { // format("\n"); } // flush(); } } String next(int n) throws IOException { return inout.next(n); } String nextLine() throws IOException { return inout.nextLine(); } int nextInt() throws IOException { return inout.nextInt(); } long nextLong(int n) throws IOException { return inout.nextLong(n); } long[] anLong(int i, int j, int len) throws IOException { long a[] = new long[j + 1]; for (int k = i; k <= j; k++) { a[k] = nextLong(len); } return a; } void print(String s, boolean nextLine) { inout.print(s, nextLine); } void format(String format, Object... obj) { inout.format(format, obj); } void flush() { inout.flush(); } void swap(int a[], int i, int j) { a[i] ^= a[j]; a[j] ^= a[i]; a[i] ^= a[j]; } int getP(int x, int p[]) { if (p[x] == 0 || p[x] == x) { return x; } return p[x] = getP(p[x], p); } void union(int x, int y, int p[]) { if (x < y) { p[y] = x; } else { p[x] = y; } } boolean topSort() { int n = adj.length - 1; int d[] = new int[n + 1]; for (int i = 1; i <= n; i++) { for (int j = 0; j < adj[i].size(); j++) { d[adj[i].get(j)[0]]++; } } List<Integer> list = new ArrayList<>(); for (int i = 1; i <= n; i++) { if (d[i] == 0) { list.add(i); } } for (int i = 0; i < list.size(); i++) { for (int j = 0; j < adj[list.get(i)].size(); j++) { int t = adj[list.get(i)].get(j)[0]; d[t]--; if (d[t] == 0) { list.add(t); } } } return list.size() == n; } class BinaryIndexedTree { int n = 1; long C[]; BinaryIndexedTree(int sz) { while (n <= sz) { n <<= 1; } C = new long[n]; } int lowbit(int x) { return x & -x; } void add(int x, long val) { while (x < n) { C[x] += val; x += lowbit(x); } } long getSum(int x) { long res = 0; while (x > 0) { res += C[x]; x -= lowbit(x); } return res; } int binSearch(long sum) { if (sum == 0) { return 0; } int n = C.length; int mx = 1; while (mx < n) { mx <<= 1; } int res = 0; for (int i = mx / 2; i >= 1; i >>= 1) { if (C[res + i] < sum) { sum -= C[res + i]; res += i; } } return res + 1; } } //Binary tree class TreeNode { int val; int tier = -1; TreeNode parent; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } } //binary tree dfs void tierTree(TreeNode root) { if (null == root) { return; } if (null != root.parent) { root.tier = root.parent.tier + 1; } else { root.tier = 0; } tierTree(root.left); tierTree(root.right); } //LCA start TreeNode[][] lca; TreeNode[] tree; void lcaDfsTree(TreeNode root) { if (null == root) { return; } tree[root.val] = root; TreeNode nxt = root.parent; int idx = 0; while (null != nxt) { lca[root.val][idx] = nxt; nxt = lca[nxt.val][idx]; idx++; } lcaDfsTree(root.left); lcaDfsTree(root.right); } TreeNode lcaTree(TreeNode root, int n, TreeNode x, TreeNode y) throws IOException { if (null == root) { return null; } if (-1 == root.tier) { tree = new TreeNode[n + 1]; tierTree(root); } if (null == lca) { lca = new TreeNode[n + 1][31]; lcaDfsTree(root); } int z = Math.abs(x.tier - y.tier); int xx = x.tier > y.tier ? x.val : y.val; while (z > 0) { final int zz = z; int l = (int) binSearch(0, 31 , k -> zz < (1 << k)); xx = lca[xx][l].val; z -= 1 << l; } int yy = y.val; if (x.tier <= y.tier) { yy = x.val; } while (xx != yy) { final int xxx = xx; final int yyy = yy; int l = (int) binSearch(0, 31 , k -> (1 << k) <= tree[xxx].tier && lca[xxx][(int) k] != lca[yyy][(int) k]); xx = lca[xx][l].val; yy = lca[yy][l].val; } return tree[xx]; } //LCA end //graph List<int[]> adj[]; void initGraph(int n, int m, boolean hasW, boolean directed) throws IOException { adj = new List[n + 1]; for (int i = 1; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int f = nextInt(); int t = nextInt(); int w = hasW ? nextInt() : 0; adj[f].add(new int[]{t, w}); if (!directed) { adj[t].add(new int[]{f, w}); } } } long binSearch(long l, long r, BinSearch sort) throws IOException { while (l < r) { long m = l + (r - l) / 2; if (sort.binSearchCmp(m)) { l = m + 1; } else { r = m; } } return l; } void getDiv(Map<Integer, Integer> map, int n) { int sqrt = (int) Math.sqrt(n); for (int i = sqrt; i >= 2; i--) { if (n % i == 0) { getDiv(map, i); getDiv(map, n / i); return; } } map.put(n, map.getOrDefault(n, 0) + 1); } boolean[] generatePrime(int n) { boolean p[] = new boolean[n + 1]; p[2] = true; for (int i = 3; i <= n; i += 2) { p[i] = true; } for (int i = 3; i <= Math.sqrt(n); i += 2) { if (!p[i]) { continue; } for (int j = i * i; j <= n; j += i << 1) { p[j] = false; } } return p; } boolean isPrime(long n) { //determines if n is a prime number int p[] = {2, 3, 5, 233, 331}; int pn = p.length; long s = 0, t = n - 1;//n - 1 = 2^s * t while ((t & 1) == 0) { t >>= 1; ++s; } for (int i = 0; i < pn; ++i) { if (n == p[i]) { return true; } long pt = pow(p[i], t, n); for (int j = 0; j < s; ++j) { long cur = llMod(pt, pt, n); if (cur == 1 && pt != 1 && pt != n - 1) { return false; } pt = cur; } if (pt != 1) { return false; } } return true; } long llMod(long a, long b, long mod) { return (a * b - (long) ((double) a / mod * b + 0.5) * mod + mod) % mod; // long r = 0; // a %= mod; // b %= mod; // while (b > 0) { // if ((b & 1) == 1) { // r = (r + a) % mod; // } // b >>= 1; // a = (a << 1) % mod; // } // return r; } long pow(long a, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans = ans * a; } a = a * a; n >>= 1; } return ans; } long pow(long a, long n, long mod) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans = llMod(ans, a, mod); } a = llMod(a, a, mod); n >>= 1; } return ans; } private long[][] initC(int n) { long c[][] = new long[n][n]; for (int i = 0; i < n; i++) { c[i][0] = 1; } for (int i = 1; i < n; i++) { for (int j = 1; j <= i; j++) { c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; } } return c; } /** * ps: n >= m, choose m from n; */ private int cmn(long n, long m) { if (m > n) { n ^= m; m ^= n; n ^= m; } m = Math.min(m, n - m); long top = 1; long bot = 1; for (long i = n - m + 1; i <= n; i++) { top = (top * i) % MOD; } for (int i = 1; i <= m; i++) { bot = (bot * i) % MOD; } return (int) ((top * pow(bot, MOD - 2, MOD)) % MOD); } long gcd(long a, long b) { if (a < b) { return gcd(b, a); } while (b != 0) { long tmp = a % b; a = b; b = tmp; } return a; } boolean isEven(long n) { return (n & 1) == 0; } interface BinSearch { boolean binSearchCmp(long k) throws IOException; } } private static class InOut { private BufferedReader br; private StreamTokenizer st; private PrintWriter pw; InOut() throws FileNotFoundException { // System.setIn(new FileInputStream("resources/inout/b1.in")); // System.setOut(new PrintStream("resources/inout/b1.out")); br = new BufferedReader(new InputStreamReader(System.in)); st = new StreamTokenizer(br); pw = new PrintWriter(new OutputStreamWriter(System.out)); st.ordinaryChar('\''); st.ordinaryChar('\"'); st.ordinaryChar('/'); } private long[] anLong(int n) throws IOException { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } private String next(int len) throws IOException { char ch[] = new char[len]; int cur = 0; char c; while ((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t') ; do { ch[cur++] = c; } while (!((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t')); return String.valueOf(ch, 0, cur); } private int nextInt() throws IOException { st.nextToken(); return (int) st.nval; } private long nextLong(int n) throws IOException { return Long.parseLong(next(n)); } private double nextDouble() throws IOException { st.nextToken(); return st.nval; } private String[] nextSS(String reg) throws IOException { return br.readLine().split(reg); } private String nextLine() throws IOException { return br.readLine(); } private void print(String s, boolean newLine) { if (null != s) { pw.print(s); } if (newLine) { pw.println(); } } private void format(String format, Object... obj) { pw.format(format, obj); } private void flush() { pw.flush(); } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
88234f7696e8f0071fe9e5f19d00ce83
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.util.*; import java.lang.*; public class Main { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); long t = scanner.nextLong(); while (t-- > 0) { int n = scanner.nextInt(); int m = scanner.nextInt(); boolean impossible = false; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { int a = scanner.nextInt(); if (impossible) continue; if (a > 4) impossible = true; else if ( a>3 ) { if (i == 0 || j == 0 || i == n - 1 || j == m - 1) impossible = true; } else if (a > 2) { if ( (i == 0 || i == n-1) && (j == 0 || j == m - 1) ) impossible = true; } } } if (impossible) System.out.println("NO"); else { System.out.println("YES"); for (int i=0; i<n; i++) { for (int j = 0; j < m; j++) { if ((i == 0 || i == n-1) && (j == 0 || j == m - 1)) System.out.print(2); else if (i == 0 || i == n - 1 || j == 0 || j == m - 1) System.out.print(3); else System.out.print(4); if (j != m-1) System.out.print(" "); } System.out.println(); } } } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
ede3162d013f3a6a0e935c20facd29d0
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); int t=Integer.parseInt(br.readLine()); while(t-->0) { //int n=Integer.parseInt(br.readLine()); String str[]=br.readLine().split(" "); int n=Integer.parseInt(str[0]); int m=Integer.parseInt(str[1]); /*str=br.readLine().split(" ");*/ int arr[][]=new int[n][m]; boolean ok=true; for(int i=0;i<n;i++) { str=br.readLine().split(" "); for(int j=0;j<m;j++) { arr[i][j]=Integer.parseInt(str[j]); //if(arr[i][j]>4) //ok=false; //if((i==0||j==0||i==n-1||j==m-1)&&arr[i][j]==4) //ok=false; //if(((i==0&&j==0)||(i==n-1&&j==m-1))&&arr[i][j]>=3) //ok=false; } } if(!ok) pw.println("NO"); else { //pw.println("YES"); outer:for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(((i==0&&j==0)||(i==n-1&&j==m-1)||(i==0&&j==m-1)||(i==n-1&&j==0))) { if(arr[i][j]>2) { ok=false; break outer; } else arr[i][j]=2; } else if(i==0||i==n-1||j==m-1||j==0) { if(arr[i][j]>3) { ok=false; break outer; } else arr[i][j]=3; } else { if(arr[i][j]>4) { //{ ok=false; break outer; } else arr[i][j]=4; } } } if(!ok) pw.println("NO"); else { pw.println("YES"); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) pw.print(arr[i][j]+" "); pw.println(); } } } //pw.println(); } pw.flush(); pw.close(); } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
a07d210fdd159eebf3e6ff9f535197b4
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author New User */ 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); BNeighborGrid solver = new BNeighborGrid(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) { solver.solve(i, in, out); } out.close(); } static class BNeighborGrid { public void solve(int testNumber, InputReader c, OutputWriter w) { int n = c.readInt(), m = c.readInt(); int mat[][] = new int[n][m]; for (int i = 0; i < n; i++) { mat[i] = c.readIntArray(m); } int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int tot_op = 0; for (int k = 0; k < 4; k++) { if (i + dx[k] < n && i + dx[k] >= 0 && j + dy[k] >= 0 && j + dy[k] < m) { tot_op++; } } if (mat[i][j] > tot_op) { w.printLine("NO"); return; } else { mat[i][j] = tot_op; } } } w.printLine("YES"); for (int i = 0; i < n; i++) { w.printLine(mat[i]); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = readInt(); } return array; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { 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 String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
6fdc6a24cf102856d6de11e866c8cd6d
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.*; import java.util.*; public class check2 { public static void main(String[] args) throws IOException{ Reader sc=new Reader(); PrintWriter out = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int m=sc.nextInt(); int arr[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) arr[i][j]=sc.nextInt(); } boolean b1=true; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { int count=0; if(i>0) count+=1; if(j>0) count+=1; if(i<n-1) count+=1; if(j<m-1) count+=1; if(count<arr[i][j]) b1=false; else arr[i][j]=count; } } if(b1==false) System.out.println("NO"); else { System.out.println("YES"); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ System.out.print(arr[i][j]+" "); } System.out.println(); } } } out.flush(); } 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 nextLine() 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 boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() throws IOException{ 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 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
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
75189fbfe8f58def46f9507a63671d4a
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.util.Scanner; public class BGlobalRound { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(), m = sc.nextInt(); int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { grid[i][j] = sc.nextInt(); } } boolean flag = true; //System.out.println("n:"+n+" "+m); for (int i = 0; i < n && flag; i++) { for (int j = 0; j < m; j++) { //System.out.println(i+" "+j); if (((i==0 && j==0) || (i==0 && j==m-1) || (i==n-1 && j==0) || (i==n-1 && j==m-1)) && grid[i][j] > 2) { flag = false; break; } else if((i==0 || j==0 || j==m-1 || i==n-1) && grid[i][j]>3) { flag = false; break; } else if(grid[i][j]>4) { flag = false; break; } } } if (flag) { System.out.println("YES"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if ((i==0 && j==0) || (i==0 && j==m-1) || (i==n-1 && j==0) || (i==n-1 && j==m-1)) { System.out.print("2 "); } else if(i==0 || j==0 || j==m-1 || i==n-1) { System.out.print("3 "); } else { System.out.print("4 "); } } System.out.println(); } } else System.out.println("NO"); } sc.close(); } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
cc472b944c61975580dd6ea7061892e4
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.util.InputMismatchException; public class Grid { public static boolean isequal(long a[][],long b[][],int n,int m) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]>b[i][j]) return false; } } return true; } public static void main(String[] args) { // TODO Auto-generated method stub InputReader in=new InputReader(System.in); StringBuffer str=new StringBuffer(); int t=in.nextInt(); for(int l=0;l<t;l++) { int n=in.nextInt(); int m=in.nextInt(); long a[][]=new long[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=in.nextLong(); } } long b[][]=new long[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if((i==0&&j==m-1)||i==0&&j==0||j==0&&i==n-1||i==n-1&&j==m-1) b[i][j]=2; else if(i==0||j==0||i==n-1||j==m-1) b[i][j]=3; else b[i][j]=4; } } if(isequal(a,b,n,m)) { System.out.println("YES"); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(b[i][j]+" "); } System.out.println(); } } else System.out.println("NO"); } } 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 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
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
fc2071df1f71bcfd082f7212b6daf16f
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.nio.file.Path; import java.util.*; public class Solution { static final PrintWriter printWriter = new PrintWriter(System.out); static class Pair{ String s; int pos; Pair(String s, int pos){ this.s = s; this.pos = pos; } } static class PairInt{ int first; int second; PairInt(int first,int second){ this.first = first; this.second = second; } } static class PairPair{ PairInt first; int second; PairPair(PairInt first,int second){ this.first = first; this.second = second; } } @SuppressWarnings("unchecked") public static void main(String[] args) throws IOException { FastScanner fs = new FastScanner(); int inputs = fs.nextInt(); while (inputs-- > 0){ int N = fs.nextInt(); int M = fs.nextInt(); int[][] grid = new int[N][M]; for(int i = 0;i<N;i++){ for(int j = 0;j<M;j++){ grid[i][j] = fs.nextInt(); } } boolean isPossible = true; for(int i = 0;i<N;i++){ for(int j = 0;j<M;j++){ int neighbors = noOfNeighbors(grid,i,j); if(grid[i][j] > neighbors){ isPossible = false; break; }else{ grid[i][j] = neighbors; } } if(!isPossible){ break; } } if(isPossible){ printWriter.println("YES"); for(int i = 0;i<N;i++){ for(int j = 0;j<M;j++){ printWriter.print(grid[i][j]+" "); } printWriter.println(); } }else{ printWriter.println("NO"); } } printWriter.flush(); } static int noOfNeighbors(int[][] grid,int x,int y){ int count = 0; if(x-1>=0) count++; if(x+1<grid.length) count++; if(y-1>=0) count++; if(y+1<grid[0].length) count++; return count; } static long binaryExponentiation(long a,long b){ long res = 1; while(b>0){ if((b&1) == 1){ res = res*a; } a = a*a; b = b>>1; } return res; } void reverseSort(int[] arr){ Arrays.sort(arr); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArray(int n,String str) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
f474aa3927866eb8ac0f240a45d33781
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.util.*; import java.util.ArrayList; import java.math.BigInteger; public class Main{ static class Pair{ int x; int y; Pair(){ } Pair(int x,int y){ this.x=x; this.y=y; } } public static void main(String[] args) { Scanner param = new Scanner(System.in); int end=param.nextInt(); c:while(end-->0) { int n=param.nextInt(); int m=param.nextInt(); int arr[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { arr[i][j]=param.nextInt(); } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(arr[i][j]!=0) { int count=0; if(i-1>=0&&j>=0) { if(arr[i-1][j]==0) { arr[i-1][j]=1; } count++; } if(count==arr[i][j])continue; if(i+1<n&&j<m) { if(arr[i+1][j]==0) { arr[i+1][j]=1; } count++; } if(count==arr[i][j])continue; if(i>=0&&j-1>=0) { if(arr[i][j-1]==0) { arr[i][j-1]=1; } count++; } if(count==arr[i][j])continue; if(i<n&&j+1<m) { if(arr[i][j+1]==0) { arr[i][j+1]=1; } count++; } if(count==arr[i][j])continue; System.out.println("NO"); continue c; } } } boolean b=true; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(arr[i][j]!=0) check(arr,i,j); } } b=true; if(b) { System.out.println("YES"); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(arr[i][j]+" "); } System.out.println(); } } else { System.out.println("NO"); } } } public static void check(int arr[][], int i, int j) { int temp=arr[i][j]; // System.out.println(temp); int n=arr.length; int m=arr[0].length; int count=0; if(i-1>=0&&j>=0) { if(arr[i-1][j]!=0)count++; } if(i+1<n&&j<m) { if(arr[i+1][j]!=0)count++; } if(i>=0&&j-1>=0) { if(arr[i][j-1]!=0)count++; } if(i>=0&&j+1<m) { if(arr[i][j+1]!=0)count++; } // System.out.println(count); arr[i][j]=count; } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
b74db1511a0152ff6e877fc706f1a657
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.*; import java.util.*; public class Main { FasterScanner sc = new FasterScanner(); PrintWriter pw = new PrintWriter(System.out); static int MOD = 1000000007; public static void main(String[] args) { Main m = new Main(); m.solve(); m.done(); } public void solve() { int t = sc.nextInt(); for (int tt = 0; tt < t; tt++) { int n = sc.nextInt(); int m = sc.nextInt(); int[][] A = new int[n][m]; for (int i = 0; i < n; i++) A[i] = sc.nextIntArray(m); boolean flag = false; loop: for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int k = A[i][j]; if (k == 0) continue; int cells = 0; if (i > 0) { cells++; if (A[i-1][j] > 0) k--; } if (j > 0) { cells++; if (A[i][j-1] > 0) k--; } if (j < m-1) { cells++; if (A[i][j+1] > 0) k--; } if (i < n-1) { cells++; if (A[i+1][j] > 0) k--; } if (A[i][j] > cells) { pw.println("NO"); flag = true; break loop; } if (k < 0) { A[i][j] += Math.abs(k); if (A[i][j] > 4) { pw.println("NO"); flag = true; break loop; } } } } if (flag) continue; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { A[i][j] = count(i, j, A, n, m); } } pw.println("YES"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { pw.print(A[i][j] + " "); } pw.println(); } } } public int count(int i, int j, int[][] A, int n, int m) { int cnt = 0; if (i > 0) cnt++; if (j > 0) cnt++; if (j < m-1) cnt++; if (i < n-1) cnt++; return cnt; } public void done() { pw.flush(); pw.close(); } } 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
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
80d3586688bcadb6f948f4e298688bea
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Iterator; import java.util.Map; public class GLOBAL9 { public static void main(String[] args) { // TODO Auto-generated method stub InputStream inputStream = System.in; FastReader sc = new FastReader(inputStream); OutputStream outputStream = System.out; PrintWriter w = new PrintWriter(outputStream); int tc =sc.nextInt(); while(tc-->0){ int n = sc.nextInt(); int m = sc.nextInt(); int matrix[][] = new int[n][m]; for(int i =0;i<n;i++) for(int j = 0;j<m;j++) matrix[i][j] = sc.nextInt(); boolean flag = true; for(int i =0;i<n;i++) { for(int j = 0;j<m;j++) { int count = 0; if(matrix[i][j]!=0) { if(i-1>=0) count++; if(j-1>=0) count++; if(j+1<m) count++; if(i+1<n) count++; if(matrix[i][j]>count) flag = false; } } } int count = 0; for(int i =0;i<n;i++) { for(int j = 0;j<m;j++) { if(matrix[i][j]!=0) { count++; break; } } if(count!=0) break; } if(count!=0) { Arrays.fill(matrix[0], 3); Arrays.fill(matrix[n-1], 3); for(int i=1;i<n-1;i++) Arrays.fill(matrix[i], 4); for(int i = 1;i<n-1;i++) { matrix[i][0] = 3; matrix[i][m-1] = 3; } matrix[0][0] = 2; matrix[0][m-1] = 2; matrix[n-1][0] = 2; matrix[n-1][m-1] = 2; } if(flag) { w.println("YES"); for(int i = 0;i<n;i++) { for(int j =0;j<m;j++) w.print(matrix[i][j] + " "); w.println(); } } else w.println("NO"); } w.close(); } public static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private 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 String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public char nextChar() { int c = read(); while (isSpaceChar(c)) c = read(); return (char)c; } 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
0b44ed3451629a200c6c272e78395513
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.util.*; import java.io.*; public class NeighBourGrid { static boolean set(int[][] matrix, int m, int n, int i, int j) { int cnt = 0; if(i - 1 >= 0) ++cnt; if(i + 1 < m) ++cnt; if(j + 1 < n) ++cnt; if(j - 1 >= 0) ++cnt; if(matrix[i][j] > cnt) return false; matrix[i][j] = cnt; return true; } public static void main(String[] args) { // TODO Auto-generated method stub FastScanner in = new FastScanner(); int t = in.nextInt(); while(t-- > 0) { int m, n; m = in.nextInt(); n = in.nextInt(); int[][] mat = new int[m][n]; for(int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { mat[i][j] = in.nextInt(); } } boolean ok = true; for(int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { ok = set(mat, m, n, i, j); if(! ok) break; } if(!ok) break; } if(!ok) { System.out.println("NO"); }else { System.out.println("YES"); for(int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { System.out.print(mat[i][j] + " "); } System.out.println(); } } } } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
3f029443ba527621b3aed8bdb7ebcd25
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.util.Scanner; import java.util.Stack; public class ques { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t--!=0) { int n = s.nextInt(); int m = s.nextInt(); int a[][] = new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j] = s.nextInt(); } } int flag=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { int ans = 0; if(i-1>=0) { ans++; } if(j-1>=0) { ans++; } if(i+1<n) { ans++; } if(j+1<m) { ans++; } if(ans<a[i][j]) { System.out.println("NO"); flag=1; break; } else { a[i][j] = ans; } } if(flag==1) { break; } } if(flag==0) { System.out.println("YES"); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
5a2922cc2a6b9a76c834e632840f9c95
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; import java.util.Scanner; import java.util.*; public class B { public static boolean isSafe(int i,int j,int n,int m) { return i>=0&&i<n&&j>=0&&j<m; } public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(),m=sc.nextInt(); int a[][]=new int[n][m]; boolean b=true; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=sc.nextInt(); // corner cann't be more than 2 if((i==0&&j==0&&a[i][j]>2)||(i==n-1&&j==0&&a[i][j]>2)||(i==0&&j==m-1&&a[i][j]>2)||(i==n-1&&j==m-1&&a[i][j]>2)) b=false; // for sides else if((i==0&&a[i][j]>3)||(j==0&&a[i][j]>3)||(i==n-1&&a[i][j]>3)||(j==m-1&&a[i][j]>3)) b=false; else if(a[i][j]>4) b=false; } } if(!b)System.out.println("NO"); else { System.out.println("YES"); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if((i==0&&j==0)||(i==n-1&&j==0)||(i==0&&j==m-1)||(i==n-1&&j==m-1)) a[i][j]=2; else if((i==0)||(j==0)||i==n-1||j==m-1) a[i][j]=3; else a[i][j]=4; System.out.print(a[i][j]+" "); } System.out.println(); } } } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
b468094827575be7437f8df7932f66f6
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int T = sc.nextInt(); for(int t = 0; t<T; t++){ int n = sc.nextInt(); int m = sc.nextInt(); boolean ok = true; int arr[][] = new int[n][m]; for(int i = 0; i<n; i++){ for(int j = 0; j<m; j++){ arr[i][j] = sc.nextInt(); if(i == 0 || i == n - 1 || j == 0 || j == m - 1){ if((i==0 && j == 0) || (i == n - 1 && j == 0) || (i == 0 && j == m - 1) || (i == n - 1 && j == m - 1)){ if(arr[i][j] >= 3){ ok = false; } else arr[i][j] = 2; } else{ if(arr[i][j] >= 4){ ok = false; } else arr[i][j] = 3; } } else{ if(arr[i][j] > 4){ ok = false; } else arr[i][j] = 4; } } } if(!ok){ out.println("NO"); } else{ out.println("YES"); for(int i = 0; i<n; i++){ for(int j = 0; j<m; j++){ out.print(arr[i][j] + " "); } out.println(""); } } } out.flush(); } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
b62be65c459a2cfd82f1eee55297dbf0
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.BufferedInputStream; import java.io.IOException; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; /** * Created by Harry on 5/31/20. */ public class Test { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(new BufferedInputStream(System.in)); int T = scanner.nextInt(); for(int t=1; t<=T; t++){ int n = scanner.nextInt(); int m = scanner.nextInt(); int[][] grid = new int[n][m]; boolean isResFound = false; label: for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ grid[i][j] = scanner.nextInt(); if(i==0 && j==0 || i==0 && j==m-1 || i==n-1 && j==0 || i==n-1 && j==m-1){ if(grid[i][j]>2){ isResFound = true; } grid[i][j] = 2; } else if(i==0 || i==n-1 || j==0 || j==m-1){ if(grid[i][j]>3){ isResFound = true; } grid[i][j] = 3; } else{ if(grid[i][j]>4){ isResFound = true; } grid[i][j] = 4; } } } if(isResFound){ System.out.println("NO"); } else{ System.out.println("YES"); for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ System.out.print(grid[i][j]+" "); } System.out.println(); } } } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
e4496fc3c20dceb6a940ac6a81580638
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.*; import java.util.*; public class Main{ static Scanner scan = new Scanner(System.in); public static void solve() { int n = scan.nextInt(); int m = scan.nextInt(); int[][] a = new int[n][m]; boolean flag = true; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { int k = scan.nextInt(); if(i!=0 && j!=0 && i!=n-1 && j!=m-1) a[i][j]=4; else if((i==0 && (j==0 || j==m-1)) || (j==0 && (i==0 || i==n-1)) || (i==n-1 && j==m-1)) a[i][j]=2; else a[i][j]=3; if(a[i][j]<k) flag = false; } } if(flag) { System.out.println("YES"); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } else System.out.println("NO"); } public static void main(String[] args) { int t =scan.nextInt(); //int t = 1; while(t-->0) { solve(); } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
6657b330c15e96db0584bfd5c6614320
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.util.*; import java.util.Map.Entry; import java.io.*; import java.math.*; import static java.util.stream.Collectors.*; import static java.util.Map.Entry.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws IOException { final long mod=1000000007; Reader s=new Reader(); PrintWriter pt=new PrintWriter(System.out); // BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int T=s.nextInt(); // int T=Integer.parseInt(br.readLine()); // int T=1; while(T-->0) { int n=s.nextInt(); int m=s.nextInt(); int arr[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) arr[i][j]=s.nextInt(); } boolean b=false; if(arr[0][m-1]>2||arr[0][0]>2||arr[n-1][0]>2||arr[n-1][m-1]>2) b=true; for(int i=0;i<n;i++) if(arr[i][0]>3||arr[i][m-1]>3) b=true; for(int i=0;i<m;i++) if(arr[0][i]>3||arr[n-1][i]>3) b=true; for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(arr[i][j]>4) b=true; if(b) pt.println("NO"); else { pt.println("YES"); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if((i==0&&(j==0||j==m-1))||i==n-1&&(j==0||j==m-1)) pt.print("2 "); else if(i==0||i==n-1||j==0||j==m-1) pt.print( "3 "); else pt.print("4 "); } pt.println(); } // pt.println(); } } pt.close(); } static long no(long n) { return n*(n+1)/2; } static void reverse(int arr[], int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int p2(int n) { int k=0; while(n>1) { if(n%2!=0) return k; n/=2; k++; } return k; } static boolean isp2(int n) { while(n>1) { if(n%2==1) return false; n/=2; } return true; } static int binarySearch(int arr[], int first, int last, int key){ int mid = (first + last)/2; while( first <= last ){ if ( arr[mid] < key ){ first = mid + 1; }else if ( arr[mid] == key ){ return mid; }else{ last = mid - 1; } mid = (first + last)/2; } return -1; } static double gt(double x, double h, double c, double t) { return (x*h+(x-1)*c)/(2*x-1); } static long sq(long x) { return x*(x+1)*(2*x+1)/6; } static long cu(long x) { return x*x*(x+1)*(x+1)/4; } static void print(int a[][]) { for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) System.out.print(a[i][j]+" "); System.out.println(); } } static int max (int x, int y) { return (x > y)? x : y; } static int search(Pair[] p, Pair pair) { int l=0, r=p.length; while (l <= r) { int m = l + (r - l) / 2; if (p[m].compareTo(pair)==0) return m; if (p[m].compareTo(pair)<0) l = m + 1; else r = m - 1; } return -1; } static void pa(int a[]) { for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } static void pa(long a[]) { for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } static void reverseArray(int arr[], int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } static boolean isPalindrome(String s) { int l=s.length(); for(int i=0;i<l/2;i++) { if(s.charAt(i)!=s.charAt(l-i-1)) return false; } return true; } static long nc2(long n, long m) { return (n*(n-1)/2)%m; } static long c(long a) { return a*(a+1)/2; } static int next(int[] arr, int target) { int start = 0, end = arr.length - 1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; // Move to right side if target is // greater. if (arr[mid] <= target) { start = mid + 1; } // Move left side. else { ans = mid; end = mid - 1; } } return ans; } static String toString(char [] c) { StringBuffer b=new StringBuffer(""); for(int i=0;i<c.length;i++) b.append(c[i]); return b.toString(); } static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return power(n, p-2, p); } static long nCrModP(long n, long r, long p) { if (r == 0) return 1; long[] fac = new long[(int) (n+1)]; fac[0] = 1; for (int i = 1 ;i <= n; i++) fac[i] = fac[i-1] * i % p; return (fac[(int) n]* modInverse(fac[(int) r], p) % p * modInverse(fac[(int) (n-r)], p) % p) % p; } static String rev(String str) { return new StringBuffer(str).reverse().toString(); } static long fastpow(long x, long y, long m) { if (y == 0) return 1; long p = fastpow(x, y / 2, m) % m; p = (p * p) % m; if (y % 2 == 0) return p; else return (x * p) % m; } static boolean isPerfectSquare(long l) { return Math.pow((long)Math.sqrt(l),2)==l; } static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int [n1]; int R[] = new int [n2]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } static class Pair implements Comparable<Pair>{ int a; int b; Pair(int a,int b){ this.a=a; this.b=b; } public int compareTo(Pair p){ if(a>p.a) return 1; if(a==p.a) return (b-p.b); return -1; } } 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
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
69c052a5c4b80494245fb565ebc52aa7
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.*; import java.util.*; import javafx.util.Pair; import java.math.*; import java.math.BigInteger; import java.util.LinkedList; import java.util.Queue; public final class CodeForces { static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<ArrayList<Integer>> g; static long mod=1000000007; static boolean set[]; static Pair<Integer,Integer> seg[]; static int A[]; public static void main(String args[])throws IOException { int T=i(); while(T-->0) { int N=i(); int M=i(); long A[][]=new long[N+1][M+1]; long B[][]=new long[N+1][M+1]; boolean f=true; for(int i=1; i<=N; i++) { for(int j=1; j<=M; j++) { A[i][j]=l(); if(i>1 && i<N) { if(j>1 && j<M) { B[i][j]=4; } else if(j>1 && j==M) { B[i][j]=3; } else if(j==1 && j<M) { B[i][j]=3; } } else if(i>1 && i==N) { if(j>1 && j<M) { B[i][j]=3; } else if(j>1 && j==M) { B[i][j]=2; } else if(j==1 && j<M) { B[i][j]=2; } } else if(i==1 && i<N) { if(j>1 && j<M) { B[i][j]=3; } else if(j>1 && j==M) { // System.out.println("Run"); B[i][j]=2; } else if(j==1 && j<M) { B[i][j]=2; } } if(A[i][j]>B[i][j])f=false; ans.append(B[i][j]+" "); } ans.append("\n"); } if(f) { System.out.println("YES"); System.out.print(ans); } else System.out.println("NO"); ans.delete(0,ans.length()); } } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static String swap(String X,int i,int j) { char ch[]=X.toCharArray(); char a=ch[i]; ch[i]=ch[j]; ch[j]=a; return new String(ch); } static int sD(long n) { if (n % 2 == 0 ) return 2; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0 ) return i; } return (int)n; } static void setGraph(int N) { set=new boolean[N+1]; g=new ArrayList<ArrayList<Integer>>(); for(int i=0; i<=N; i++) g.add(new ArrayList<Integer>()); } static void DFS(int N,int d) { set[N]=true; d++; for(int i=0; i<g.get(N).size(); i++) { int c=g.get(N).get(i); if(set[c]==false) { DFS(c,d); } } } static long pow(long a,long b) { long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader 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
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
433edeb2d9c5d8f80f1fd894fdc58ae6
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class B { public static void main(String[] args) { FastScanner fs = new FastScanner(); int t=fs.nextInt(); while(t-->0) { int n=fs.nextInt(); int m=fs.nextInt(); int a[][] = new int[n][m]; int max = Integer.MIN_VALUE; for(int i=0;i<n;i++) { for(int j=0;j< m;j++) { a[i][j]=fs.nextInt(); max =Math.max(max, a[i][j]); } } // if(max>4) { // System.out.println("NO"); // continue; // } int temp =0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if((i==0||i==n-1)&&(j==0||j==m-1)) { if(a[i][j]>2) { temp++; } } else if(i==0||i==n-1||j==0||j==m-1) { if(a[i][j]>3) { temp++; } } else if(a[i][j]>4) temp++; } } if(temp>0) { System.out.println("NO"); continue; } System.out.println("YES"); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if((i==0||i==n-1)&&(j==0||j==m-1)) { System.out.print(2); } else if(i==0||i==n-1||j==0||j==m-1) { System.out.print(3); } else { System.out.print(4); } System.out.print(" "); } System.out.println(); } } } static void sort(int[] a ) { ArrayList<Integer> l = new ArrayList<>(); for(int i:a) l.add(i); Collections.sort(l); for(int i=0;i<l.size();i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while(!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); }catch(IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for(int i=0;i<n;i++)a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
2706f50e125da303cfc925ac3d811362
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.util.*; import java.io.*; public class gfg { static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException{ //Reader.init(System.in); Reader.init(System.in); int t = Reader.nextInt(); while(t-->0){ solve(); } out.close(); } static ArrayList<Integer>[] graph; static int n=0; static int m=0; static int k =0; static long mod =1000000007; static int MOD = 1000000007; static int[] dx = {0,0,1,-1}; static int[] dy = {1,-1,0,0}; static int[] seg = new int[(int)2e5]; static void solve()throws IOException{ n = Reader.nextInt(); m = Reader.nextInt(); int[][] arr= new int[n][m]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if( (i==0 && (j==0 || j==m-1)) || (i==n-1 && (j==0 || j==m-1)) ) arr[i][j]=2; else if(i==0 || i==n-1){ arr[i][j]=3; } else if(j==0 || j==m-1){ arr[i][j]=3; } else arr[i][j]=4; //System.out.print(arr[i][j]+" "); } //System.out.println(); } int[][] temp = new int[n][m]; int flag=0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ temp[i][j]=Reader.nextInt(); if(temp[i][j]>arr[i][j]){ //out.println("NO"); flag=1; } //System.out.print(arr[i][j]+" "); } //System.out.println(); } if(flag==1){ out.println("NO"); return; } out.println("YES"); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ out.print(arr[i][j]+" "); } out.println(); } } static void update(int ind, int l , int r , int pos, int val){ if(l==r){ seg[ind] = val; return ; } int mid = (l+r)/2; if(pos<=mid) update(2*ind,l,mid,pos,val); else update(2*ind+1,mid+1,r,pos,val); seg[ind] = Math.min(seg[2*ind],seg[2*ind+1]); } static int query(int ind, int l, int r, int ll, int rr){ if(ll>rr | l>r) return (int)2e9; if(l==ll && r==rr) return seg[ind]; int mid = (l+r)/2; return Math.min( query(2*ind,l,mid,ll,Math.min(rr,mid)), query(2*ind+1,mid+1,r,Math.max(ll,mid+1),rr) ); } /*static void dfs1(int a,int p){ size[a]=1; for(int v: graph[a]){ if(v!=p){ dfs1(v,a); size[a]+=size[v]; sum[a]+= (size[v]+sum[v]); } } } static void dfs2(int a,int p){ if(p==-1) ans[a]= sum[a]; else{ ans[a] = ans[p] - (sum[a]+size[a]) + (n-size[a]) +sum[a]; } for(int v:graph[a]){ if(v!=p){ dfs2(v,a); } } }*/ // ggratest common divisor static int gcd(int a , int b){ if(b==0) return a; else return gcd(b,a%b); } // least common multiple static int lcm(int a, int b){ return (a*b)/gcd(a,b); } // a^b static long fastpow(long a, long b){ long res = 1;a=a%mod; while(b>0){ if(b%2==1) res = (res*a)%mod; a = (a*a)%mod; b = b>>1; } return res; } // segment tree /* static void update(int ind, int l, int h, int pos, int v){ if(l==h){ seg[ind] = v; return; } int mid = (l+h)/2; if(pos<=mid) update(2*ind,l,mid,pos,v); else update(2*ind+1,mid+1,h,pos,v); seg[ind] = seg[2*ind]^seg[2*ind+1]; } static int query(int ind, int l , int h, int ll , int hh){ if(ll>hh) return 0; if(l==ll && h==hh) return seg[ind]; int mid = (l+h)/2; return query(2*ind,l,mid,ll,Math.min(mid,hh)) ^ query(2*ind+1,mid+1,h,Math.max(mid+1,ll),hh); }*/ // priority queue with comparator // PriorityQueue<Interval> pq = new PriorityQueue<>(new Comparator<Interval>() { // public int compare(Interval a, Interval b){ // int diff1 = a.r - a.l; // int diff2 = b.r - b.l; // if(diff1 == diff2){ // return a.l - b.l; // } // return diff2 - diff1; // } // }); } class trienode{ int v; trienode[] child ; boolean leaf; int cnt; int p=0; trienode(int vv){ child = new trienode[2]; leaf=false; cnt=0; v=vv; } trienode(){ child = new trienode[2]; leaf=false; cnt=0; v=0; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } } class newcomparator implements Comparator<Integer>{ //@Override public int compare(Integer a, Integer b){ return a<=b?1:-1; } } class node { int f; int s;// cost to rreach\ node(int ff,int ss){ f=ff; s=ss; } } class mergesort{ static void sort(int l, int h, int[] arr){ if(l<h){ int mid = (l+h)/2; sort(l,mid,arr); sort(mid+1,h,arr); merge(l,mid,h,arr); } } static void merge(int l, int m , int h , int [] arr){ int[] left = new int[m-l+1]; int[] right = new int[h-m]; for(int i= 0 ;i< m-l+1;i++){ left[i] = arr[l+i]; } for(int i=0;i<h-m;i++){ right[i] = arr[m+1+i]; } //now left and right arrays are assumed to be sorted and we have tp merge them together // int the original aaray. int i=l; int lindex = 0; int rindex = 0; while(lindex<m-l+1 && rindex<h-m){ if(left[lindex]<=right[rindex]){ arr[i]=left[lindex]; lindex++; i++; } else{ arr[i]=right[rindex]; rindex++; i++; } } while(lindex<m-l+1){ arr[i]=left[lindex]; lindex++; i++; } while(rindex<h-m){ arr[i]=right[rindex]; rindex++; i++; } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
df7cd56213aba625745d6d366131a189
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder st = new StringBuilder(); while(t--!=0){ String[] in1 = br.readLine().split(" "); int n = Integer.parseInt(in1[0]); int m = Integer.parseInt(in1[1]); // String[] in= br.readLine().split(" "); int[][] arr = new int[n][m]; int pos = 0,neg = 0; boolean flag = true; // System.out.println(flag); for(int i=0;i<n;i++){ String[] in = br.readLine().split(" "); for(int j=0;j<m;j++){ arr[i][j] = Integer.parseInt(in[j]); } } if(arr[0][0]>2 || arr[0][m-1]>2 || arr[n-1][0]>2 || arr[n-1][m-1]>2){ flag = false; }else{ arr[0][0]=2; arr[0][m-1]=2; arr[n-1][0]=2; arr[n-1][m-1]=2; for(int i=1;i<m-1;i++){ if(arr[0][i]>3 || arr[n-1][i]>3){ flag = false; break; } arr[0][i] = 3; arr[n-1][i] = 3; } for(int i=1;i<n-1;i++){ if(arr[i][0]>3 || arr[i][m-1]>3){ flag = false; break; } arr[i][0] = 3; arr[i][m-1] = 3; } for(int i=1;i<n-1;i++){ for(int j=1;j<m-1;j++){ if(arr[i][j]>4){ flag = false; break; } arr[i][j] = 4; } } } if(!flag){ st.append("NO\n"); }else{ st.append("YES\n"); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ st.append(arr[i][j]+" "); } st.append("\n"); } } } System.out.println(st); } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
3b6b34e486693f08bce9d424d32ec054
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /* * Copyright (c) --> Arpit * Date Created : 17/7/2020 * Have A Good Day ! */ /** * Built using CHelper plug-in * Actual solution is at the top * * @author Arpit */ public class TaskB { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); BNeighborGrid solver = new BNeighborGrid(); solver.solve(1, in, out); out.close(); } static class BNeighborGrid { public void solve(int testNumber, FastReader sc, PrintWriter w) { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int a[][] = new int[n][m]; boolean flag = true; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int cnt = 0; int p = sc.nextInt(); if (i > 0) cnt++; if (i < n - 1) cnt++; if (j > 0) cnt++; if (j < m - 1) cnt++; if (p > cnt) flag = false; a[i][j] = cnt; } } if (!flag) { w.println("No"); } else { w.println("Yes"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { w.print(a[i][j] + " "); } w.println(); } } } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
813da447d851964f0b004428a8355d2d
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int m=sc.nextInt(); int [][] a=new int[n][m]; boolean res=true; 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++) { if((i==0 && j==0) || (i==n-1 && j==0) || (i==0 && j==m-1) || (i==n-1 && j==m-1)) { if(a[i][j]>2) { System.out.println("NO"); res=false; break; } else a[i][j]=2; } else if((i==0 && j>0 && j<m-1) || (i==n-1 && j>0 && j<m-1) || (j==0 && i>0 && i<n-1) || (j==m-1 && i>0 && i<n-1)) { if(a[i][j]>3) { System.out.println("NO"); res=false; break; } else a[i][j]=3; } else { if(a[i][j]>4) { System.out.println("NO"); res=false; break; } else a[i][j]=4; } } if(res==false) break; } if(res==true) { System.out.println("YES"); // for(int i=0;i<n;i++) //{ // System.out.println(Arrays.toString(a[i])); //} for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
882b6092abcd1c33feae16cdebab1808
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.*; import java.util.*; import javafx.util.Pair; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); for(int i=0; i<t; i++) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken()); int[][] arr = new int[n][m]; boolean possible = true; for(int j=0; j<n; j++) { st = new StringTokenizer(br.readLine()); for(int k=0; k<m; k++) arr[j][k] = Integer.parseInt(st.nextToken()); } for(int j=0; j<n; j++) { for(int k=0; k<m; k++) { int cnt = 0; if(j-1 >= 0) cnt++; if(j+1 <= n-1) cnt++; if(k-1 >= 0) cnt++; if(k+1 <= m-1) cnt++; if(arr[j][k] <= cnt) arr[j][k] = cnt; else { possible = false; break; } } } System.out.println(possible? "YES": "NO"); if(possible) { for(int j=0; j<n; j++) { for(int k=0; k<m; k++) { System.out.print(arr[j][k] + " "); } System.out.println(); } } } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
60fb100d08d9919981b843266c983f77
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.lang.reflect.Array; import java.util.*; import java.io.* ; /* NOTES: -more than 10 digits (10^10), use long -prefix sum of 1's and -1's to control flow of highlighting a region -log(a)/log(b) = log base b of a -create a position array if trying to see if elements show up next to each other sequentially i.e. elements 1,2,3 are within 3 spots of each other in an array arr[scannedInt - 1] = i; (position of i at p[i] in original) - use a freq array when counting instances of an element occurring - increment the counter first before simulating anything turn-based (two player game) */ public class Main { static long mod = (long)(Math.pow(10, 9) + 7); public static String alpha = "zabcdefghijklmnopqrstuvwxy"; public static void main(String[] args) throws IOException{ Scanner scan = new Scanner(System.in); long t = scan.nextInt(); //System.out.println(t); while (t-- > 0){ int n = scan.nextInt(); int m = scan.nextInt(); int[][] board = new int[n][m]; for(int i = 0; i<n;i++){ for(int j = 0; j<m; j++){ board[i][j] = scan.nextInt(); } } boolean flag = false; if(board[0][0]>2 || board[0][m-1] >2 || board[n-1][0] >2 || board[n-1][m-1]>2){ flag = true; } for(int i = 0; i<n;i++){ if(board[i][0] >3 || board[i][m-1]>3) { flag = true; break; } } for(int j = 0; j<m; j++){ if(board[0][j] >3 || board[n-1][j]>3) { flag = true; break; } } for(int i = 1; i<n-1;i++){ for(int j = 1; j<m-1; j++){ if(board[i][j]>4){ flag = true; break; } } } if(flag){ System.out.println("No"); //break; } else{ System.out.println("Yes"); for(int i = 0; i<n;i++){ for(int j = 0; j<m; j++){ if(i==0 && j==0 || i==0 && j==m-1|| i==n-1&&j==0 || i==n-1&&j==m-1) board[i][j] = 2; else if((i == 0 || i == n-1) && j>0 && j<m-1) board[i][j] =3; else if ((j == 0 || j == m-1) && i>0 && i<n-1) board[i][j] =3; else board[i][j] = 4; System.out.print(board[i][j]+" "); } System.out.println(); } } } scan.close(); } static boolean isPalindrome(String s){ for(int i = 0; i<s.length()/2; i++){ if(s.charAt(i) != s.charAt(s.length()-i-1)) return false; } return true; } static ArrayList factors(long n ){ ArrayList<Long> facts = new ArrayList<>(); for(int i = 1; i<=(int)Math.sqrt(n); i++){ if(n%i == 0){ if( i == Math.sqrt(n)) facts.add((long)i); else{ facts.add((long)i); facts.add(n/i); } } } Collections.sort(facts); return facts; } static boolean isPrime (int n){ if(factors(n).size() ==2) return true; else return false; } static long GCD(long a, long b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return GCD(a-b, b); return GCD(a, b-a); } static long LCM(long a, long b){ return (a*b)/(GCD(a,b)); } static String rev(String s){ char[] arr = s.toCharArray(); for(int i = 0; i<(s.length()+1)/2; i++){ char temp = arr[i]; arr[i] = arr[s.length()-i-1]; arr[s.length()-i-1] = temp; } String fin = new String(arr); return fin; } static long pow(long a, long N) { if (N == 0) return 1; else if (N == 1) return a; else { long R = pow(a,N/2); if (N % 2 == 0) { return R*R; } else { return R*R*a; } } } static long powMod(long a, long N) { if (N == 0) return 1; else if (N == 1) return a % mod; else { long R = powMod(a,N/2) % mod; R *= R; R %= mod; if (N % 2 == 1) { R *= a; R %= mod; } return R % mod; } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
68dab8d079bf2d39fb147c64038337ad
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.List; public class Main { private static final String NO = "NO"; private static final String YES = "YES"; InputStream is; PrintWriter out; String INPUT = ""; private List<Integer>[] g; private static final long MOD = 998244353; void solve() { int T = ni(); for (int i = 0; i < T; i++) solve(i); } void solve(int T) { int n = ni(); int m = ni(); int a[][] = na(n, m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { int max = 0; if (i > 0) max++; if (i < n - 1) max++; if (j > 0) max++; if (j < m - 1) max++; if (a[i][j] <= max) a[i][j] = max; else { out.println(NO); return; } } out.println(YES); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) out.print(a[i][j] + " "); out.println(); } } // a^b long power(long a, long b) { long x = 1, y = a; while (b > 0) { if (b % 2 != 0) { x = (x * y) % MOD; } y = (y * y) % MOD; b /= 2; } return x % MOD; } private long gcd(long a, long b) { while (a != 0) { long tmp = b % a; b = a; a = tmp; } return b; } 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"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private boolean vis[]; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' // ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n) { if (!(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 List<Integer> na2(int n) { List<Integer> a = new ArrayList<Integer>(); for (int i = 0; i < n; i++) a.add(ni()); return a; } private int[][] na(int n, int m) { int[][] a = new int[n][]; for (int i = 0; i < n; i++) a[i] = na(m); 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(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private long[][] nl(int n, int m) { long[][] a = new long[n][]; for (int i = 0; i < n; i++) a[i] = nl(m); return a; } 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 static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } public class Pair<K, V> { /** * Key of this <code>Pair</code>. */ private K key; /** * Gets the key for this pair. * * @return key for this pair */ public K getKey() { return key; } /** * Value of this this <code>Pair</code>. */ private V value; /** * Gets the value for this pair. * * @return value for this pair */ public V getValue() { return value; } /** * Creates a new pair * * @param key The key for this pair * @param value The value to use for this pair */ public Pair(K key, V value) { this.key = key; this.value = value; } /** * <p> * <code>String</code> representation of this <code>Pair</code>. * </p> * * <p> * The default name/value delimiter '=' is always used. * </p> * * @return <code>String</code> representation of this <code>Pair</code> */ @Override public String toString() { return key + "=" + value; } /** * <p> * Generate a hash code for this <code>Pair</code>. * </p> * * <p> * The hash code is calculated using both the name and the value of the * <code>Pair</code>. * </p> * * @return hash code for this <code>Pair</code> */ @Override public int hashCode() { // name's hashCode is multiplied by an arbitrary prime number (13) // in order to make sure there is a difference in the hashCode between // these two parameters: // name: a value: aa // name: aa value: a return key.hashCode() * 13 + (value == null ? 0 : value.hashCode()); } /** * <p> * Test this <code>Pair</code> for equality with another <code>Object</code>. * </p> * * <p> * If the <code>Object</code> to be tested is not a <code>Pair</code> or is * <code>null</code>, then this method returns <code>false</code>. * </p> * * <p> * Two <code>Pair</code>s are considered equal if and only if both the names and * values are equal. * </p> * * @param o the <code>Object</code> to test for equality with this * <code>Pair</code> * @return <code>true</code> if the given <code>Object</code> is equal to this * <code>Pair</code> else <code>false</code> */ @Override public boolean equals(Object o) { if (this == o) return true; if (o instanceof Pair) { Pair pair = (Pair) o; if (key != null ? !key.equals(pair.key) : pair.key != null) return false; if (value != null ? !value.equals(pair.value) : pair.value != null) return false; return true; } return false; } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
586cd44ed1807eb44be4b5fab825cb15
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Solution{ static int[] coordList; static int n; static int m; public static void main(String[] args) throws IOException{ StringBuilder print = new StringBuilder(); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); long t = Long.parseLong(input.readLine()); while(t-- > 0){ StringTokenizer st = new StringTokenizer(input.readLine()); int max = 0; n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); StringBuilder printMini = new StringBuilder(); boolean works = true; for(int i = 0; i < n; i++){ st = new StringTokenizer(input.readLine()); for(int j = 0; j < m; j++){ max = 4; if(i == 0){ max--; } if(i == n-1){ max--; } if(j == 0){ max--; } if(j == m-1){ max--; } int a = Integer.parseInt(st.nextToken()); printMini.append(max + " "); if(a > max){ works = false; } } printMini.append("\n"); } if(works){ print.append("YES\n"); print.append(printMini); }else{ print.append("NO\n"); } } System.out.println(print.toString()); } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
48698f05ec8f53756ff8f999273d0de6
train_004.jsonl
1593873900
You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k &gt; 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class NeighborGrid { public static void main(String[] args) { // TODO Auto-generated method stub FastReader input=new FastReader(); int t=input.nextInt(); for(int i=0;i<t;i++) { int m,n; n=input.nextInt(); m=input.nextInt(); int a[][]=new int[n][m]; int flag=0; for(int j=0;j<n;j++) { for(int k=0;k<m;k++) { a[j][k]=input.nextInt(); } } for(int j=0;j<n;j++) { for(int k=0;k<m;k++) { if((j==0 && k==0) || (j==0 && k==m-1) || (j==n-1 && k==0) || (j==n-1 && k==m-1)) { if(a[j][k]>2) { flag=1; break; } } else if((j==0 && k>=1 && k<=m-2) || (j==n-1 && k>=1 && k<=m-2) || (k==0 && j>=1 && j<=n-2) || (k==m-1 && j>=1 && j<=n-2)) { if(a[j][k]>3) { flag=1; break; } } else { if(a[j][k]>4) { flag=1; break; } } } if(flag==1) { break; } } if(flag==1) { System.out.println("NO"); } else { System.out.println("YES"); for(int j=0;j<n;j++) { for(int k=0;k<m;k++) { if((j==0 && k==0) || (j==0 && k==m-1) || (j==n-1 && k==0) || (j==n-1 && k==m-1)) { System.out.print(2+" "); } else if((j==0 && k>=1 && k<=m-2) || (j==n-1 && k>=1 && k<=m-2) || (k==0 && j>=1 && j<=n-2) || (k==m-1 && j>=1 && j<=n-2)) { System.out.print(3+" "); } else { System.out.print(4+" "); } } 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; } } }
Java
["5\n3 4\n0 0 0 0\n0 1 0 0\n0 0 0 0\n2 2\n3 0\n0 0\n2 2\n0 0\n0 0\n2 3\n0 0 0\n0 4 0\n4 4\n0 0 0 0\n0 2 0 1\n0 0 0 0\n0 0 0 0"]
1 second
["YES\n0 0 0 0\n0 1 1 0\n0 0 0 0\nNO\nYES\n0 0\n0 0\nNO\nYES\n0 1 0 0\n1 4 2 1\n0 2 0 0\n1 3 1 0"]
NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.
Java 8
standard input
[ "constructive algorithms", "greedy" ]
8afcdfaabba66fb9cefc5b6ceabac0d0
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.
1,200
If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.
standard output
PASSED
6d2ffc31d2a6435d03ab6714a72bc315
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
/* * @author Sane */ import java.io.File; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.FileWriter; import java.util.Scanner; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.OutputStreamWriter; public class TheMain { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] mood = new int[100010]; boolean zeroExists = false; for (int i = 0; i < n; ++i) { mood[i] = sc.nextInt(); zeroExists |= (mood[i] == 0); } if (!zeroExists) { System.out.println("YES"); return; } for (int toCheck = 2; toCheck < n; ++toCheck) if (n % toCheck == 0) for (int i = 0; i < toCheck; ++i) if ( check(toCheck, i, mood, n) && n/toCheck > 2) { System.out.println("YES"); return; } System.out.println("NO"); } public static boolean check(int toCheck, int from, int[] mood, int n) { boolean ok = true; for (int i = from; i < n && ok; i += toCheck) ok &= (mood[i] == 1); return ok; } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
0db093106e1a701edd9998bbd7e5963d
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; public class RoundTableKnights { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); int n = Integer.parseInt(reader.readLine()); String st[] = reader.readLine().split(" "); boolean b[] = new boolean[n]; for (int i = 0; i < st.length; i++) b[i] = (st[i].charAt(0) == '1'); ArrayList<Integer> list = new ArrayList<Integer>(); int sqrt = (int) Math.sqrt(n); for (int i = 3; i <= sqrt; i++) { if ((n % i) == 0 && (n / i) > 2) { list.add(i); list.add(n / i); } } // add with window 1 // add with window 2 if ((n & 1) == 0) { if ((n / 2) > 2) list.add(2); } list.add(1); // System.out.println(list); // System.out.println(Arrays.toString(b)); for (Integer w : list) { for (int i = 0; i < w; i++) { boolean found = true; for (int j = i; j < n; j += w) { // System.out.println(j); if (!b[j]) { found = false; break; } } if (found) { // System.out.println(w+" "+i); System.out.println("YES"); return; } } } System.out.println("NO"); } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
ef7d0d37eefa5f280c0783283cab7682
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } for (int i = 1; i * i <= n; i++) { if (n % i == 0) { if (check(a, i) || check(a, n / i)) { out.println("YES"); return; } } } out.println("NO"); } boolean check(int[] a, int size) { int good = a.length / size; if (good < 3) return false; int[] count = new int[size]; for (int i = 0; i < a.length; i++) { if (a[i] == 1) count[i % size]++; } for (int i = 0; i < count.length; i++) { if (count[i] == good) return true; } return false; } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
f5bf241fa38eea18ee67544cab1a3951
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.*; import java.util.*; public class j { public static void main(String aa[])throws IOException { BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); int flag=0,p=0,i=0,n=0,j=0,m=0; String s; m=Integer.parseInt(b.readLine()); int d[]=new int[m]; s=b.readLine(); StringTokenizer z=new StringTokenizer(s); for(i=0;i<m;i++) d[i]=Integer.parseInt(z.nextToken()); for(i=1;m/i>2;i++) { if(m%i==0) { for(j=0;j<i;j++) { for(p=j;p<m;p+=i) if(d[p]==0) { flag=1; break; } if(flag==0) { System.out.print("YES"); System.exit(0); } flag=0; } } } System.out.print("NO"); } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
e092336b94078d33822f1063c64275de
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static boolean fortunate(int[] v) { int n = v.length; for (int i = 0; i < n / 3; i++) if (v[i] == 1) for (int j = 1; j <= n / 3; j++) { int home = i, iter = home; do{ iter = (iter + j) % n; }while (home != iter && v[iter] == 1); if (home == iter) return true; } return false; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; StringBuilder sb = new StringBuilder(); d: do { line = br.readLine(); if (line == null || line.length() == 0) break d; int n = Integer.parseInt(line); int[] v = retInts(br.readLine()); if (fortunate(v)) sb.append("YES").append("\n"); else sb.append("NO").append("\n"); } while (line != null && line.length() != 0); System.out.print(sb); } public static int[] retInts(String line) { String[] w = line.split(" "); int[] nums = new int[w.length]; for (int i = 0; i < w.length; i++) nums[i] = Integer.parseInt(w[i]); return nums; } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
75a0e1451b1a0e6e7faa4c74c0d3ab51
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static boolean fortunate(int[] v) { int n = v.length, iter = 0; for (int i = 0; i < n / 3; i++) if (v[i] == 1) for (int j = n/3; j >= 1; j--) { iter = i; do iter = (iter + j) % n; while (i != iter && v[iter] == 1); if (i == iter) return true; } return false; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; StringBuilder sb = new StringBuilder(); d: do { line = br.readLine(); if (line == null || line.length() == 0) break d; int n = Integer.parseInt(line); int[] v = retInts(br.readLine()); if (fortunate(v)) sb.append("YES").append("\n"); else sb.append("NO").append("\n"); } while (line != null && line.length() != 0); System.out.print(sb); } public static int[] retInts(String line) { String[] w = line.split(" "); int[] nums = new int[w.length]; for (int i = 0; i < w.length; i++) nums[i] = Integer.parseInt(w[i]); return nums; } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
c1a0f629cc6a21b2902249799bfd4e8c
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static boolean fortunate(int[] v) { int n = v.length, iter = 0; for (int i = 0; i < n / 3; i++) if (v[i] == 1) for (int j = 1; j <= n / 3; j++) { iter = i; do iter = (iter + j) % n; while (i != iter && v[iter] == 1); if (i == iter) return true; } return false; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; StringBuilder sb = new StringBuilder(); d: do { line = br.readLine(); if (line == null || line.length() == 0) break d; int n = Integer.parseInt(line); int[] v = retInts(br.readLine()); if (fortunate(v)) sb.append("YES").append("\n"); else sb.append("NO").append("\n"); } while (line != null && line.length() != 0); System.out.print(sb); } public static int[] retInts(String line) { String[] w = line.split(" "); int[] nums = new int[w.length]; for (int i = 0; i < w.length; i++) nums[i] = Integer.parseInt(w[i]); return nums; } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
e173c84aeb642c207e41d1c78c6a1123
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static boolean fortunate(int[] v) { int n = v.length, iter = 0; //Desde la posicion 0 hasta la posicion n/3 //puede comenzar el poligono tal que contenga //minimo 3 vertices for (int i = 0; i < n / 3; i++) if (v[i] == 1) //Punto de inicio valido //Prueba las diferentes longitudes del poligono for (int j = n/3; j >= 1; j--) { iter = i; do iter = (iter + j) % n; //Avanza de j espacios while (i != iter && v[iter] == 1); //Si volvio a llegar a su posicion //de inicio entonces hay un poligono if (i == iter) return true; } return false; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; StringBuilder sb = new StringBuilder(); d: do { line = br.readLine(); if (line == null || line.length() == 0) break d; int n = Integer.parseInt(line); //Recibe los valores int[] v = retInts(br.readLine()); if (fortunate(v)) sb.append("YES").append("\n"); else sb.append("NO").append("\n"); } while (line != null && line.length() != 0); System.out.print(sb); } public static int[] retInts(String line) { String[] w = line.split(" "); int[] nums = new int[w.length]; for (int i = 0; i < w.length; i++) nums[i] = Integer.parseInt(w[i]); return nums; } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
df3cdecd2369e9f9e2463d4cfbedf626
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.util.*; public class Wampire { static int array[]; static int n; static boolean flag=false; public static int solver(int a){ if(a<3) return 0; int cnt=0; for(int i=0;i<n/a;i++){ for(int j=i;j<n;j+=n/a){ if(array[j]==1) cnt++; } if(cnt==a){ flag=true; //System.out.println(i + " "+n/a); } cnt=0; } return 0; } public static void main(String [] args){ Scanner in = new Scanner(System.in); n=in.nextInt(); int cnt=0; array=new int[n]; for(int i=0;i<n;i++){ array[i]=in.nextInt(); } for(int i=1;i<=Math.sqrt(n);i++){ if(i==1||i==2 && n%i==0) cnt=solver(n/i); else if(n%i==0){ cnt=solver(i); cnt=solver(n/i); } } if(flag) System.out.print("YES"); else System.out.print("NO"); } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
8e9236898660fb560747a5bdac2befee
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.util.*; import java.io.*; public class RoundTableKnights { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); boolean[] knights = new boolean[n]; for(int x = 0; x < n; x++) { if(in.nextInt() == 1) { knights[x] = true; } } ArrayList<Integer> factors = new ArrayList<Integer>(); for(int y = 1; y <= Math.sqrt(n) + 1E-9; y++) { if(n % y == 0) { if(n / y >= 3) { factors.add(y); } if(y >= 3) { factors.add(n / y); } } } for(int z = 0; z < factors.size(); z++) { for(int a = 0; a < factors.get(z); a++) { int b; for(b = a; b < knights.length; b += factors.get(z)) { if(!knights[b]) { break; } } if(b >= knights.length) { System.out.println("YES"); return; } } } System.out.println("NO"); } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
195c64eded428d640a2e95cb343a7a43
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class C71 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); List<Integer> goods = new ArrayList<>(); for (int i = 0; i < n; ++i) { if (in.nextInt() == 1) { goods.add(i); } } List<Integer> factors = new ArrayList<>(); for (int i = 3; i * i <= n; ++i) { if (n % i != 0) { continue; } int a = n / i; if (a != i) { factors.add(a); } factors.add(i); } if (n % 2 == 0 && n / 2 >=3) { factors.add(2); } factors.add(1); int length = factors.size(); int[][] count = new int[length][]; for (int i = 0; i < length; ++i) { count[i] = new int[factors.get(i)]; } for (int i : goods) { for (int j = 0; j < length; ++j) { int f = factors.get(j); int mod = i % f; count[j][mod]++; if (count[j][mod] * f == n) { System.out.println("YES"); return; } } } System.out.println("NO"); } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
40948abe7f700f455c38644e49ffafb1
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { try { new Main().solve(); } catch (Throwable throwable) { throw new RuntimeException(throwable); } } private void solve() throws Throwable { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); boolean[] mood = new boolean[n]; String[] s = in.readLine().split(" "); for (int i = 0; i < n; i++) { mood[i] = s[i].equals("1"); } for (int gap = 1; gap << 1 < n; gap++) { if (n % gap == 0) { for (int startPoint = 0; startPoint < gap; startPoint++) { boolean can = true; for (int current = startPoint; current < n; current += gap) { if (!mood[current]) { can = false; } } if (can) { System.out.println("YES"); return; } } } } System.out.println("NO"); } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
57e4a0f71741ebe40d76c56aa5354248
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(input.readLine()); String line = input.readLine(); line = line.replaceAll(" ", ""); line = line+line.substring(0,line.length()-1); int l; boolean flag; for ( int p = 3; p <= n; p++ ) { if ( n % p == 0 ) { l = n/p-1; for( int i = 0; i < n; i++ ) { if (line.charAt(i) == '1' ) { flag = true; for ( int j = 1; j < p; j++ ) { if ( line.charAt(i+(l+1)*j) == '0' ) { flag = false; break; } } if (flag) { System.out.println("YES"); return; } } } } } System.out.println("NO"); } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
90b1145ae75645c283d29e56b2ac17df
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner sn = new Scanner( System.in ); int n = sn.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = sn.nextInt(); int max = n / 3; for(int i = 1; i <= max; i++){ if(n % i == 0){ for(int j = 0; j < i; j++){ boolean check = true; for(int k = j; k < n; k += i) if(a[k] == 0) check = false; if(check){ System.out.println("YES"); return; } } } } System.out.println("NO"); } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
52d0ac9e33788e147b4bc79b9fd4ddda
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
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.Collection; import java.util.List; import java.util.Locale; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { new Thread(null, new Runnable() { public void run() { try { long prevTime = System.currentTimeMillis(); new Main().run(); System.err.println("Total time: " + (System.currentTimeMillis() - prevTime) + " ms"); System.err.println("Memory status: " + memoryStatus()); } catch (IOException e) { e.printStackTrace(); } } }, "1", 1L << 24).start(); } void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); Object o = solve(); if (o != null) out.println(o); out.close(); in.close(); } private Object solve() throws IOException { int n = ni(); int[] arr = nia(n); int nn = n; List<Integer> list = new ArrayList<Integer>(); for(int i =2;i*i<=nn;i++){ if(nn%i==0){ list.add(i); while(nn%i==0) nn/=i; } } if(nn!=1) list.add(nn); if(n%4==0) list.add(4); if(list.get(0)==2) list.remove(0); boolean[][] ok = new boolean[list.size()][]; for(int i =0;i<ok.length;i++){ ok[i]= new boolean[n/list.get(i)]; Arrays.fill(ok[i], true); } for(int i =0;i<n;i++){ if(arr[i]==0){ for(int j =0;j<ok.length;j++){ ok[j][i%(n/list.get(j))]=false; } } } for(boolean[] bs : ok) for(boolean b : bs) if(b) return "YES"; return "NO"; } BufferedReader in; PrintWriter out; StringTokenizer strTok = new StringTokenizer(""); String nextToken() throws IOException { while (!strTok.hasMoreTokens()) strTok = new StringTokenizer(in.readLine()); return strTok.nextToken(); } int ni() throws IOException { return Integer.parseInt(nextToken()); } long nl() throws IOException { return Long.parseLong(nextToken()); } double nd() throws IOException { return Double.parseDouble(nextToken()); } int[] nia(int size) throws IOException { int[] ret = new int[size]; for (int i = 0; i < size; i++) ret[i] = ni(); return ret; } long[] nla(int size) throws IOException { long[] ret = new long[size]; for (int i = 0; i < size; i++) ret[i] = nl(); return ret; } double[] nda(int size) throws IOException { double[] ret = new double[size]; for (int i = 0; i < size; i++) ret[i] = nd(); return ret; } String nextLine() throws IOException { strTok = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!strTok.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; strTok = new StringTokenizer(s); } return false; } void printRepeat(String s, int count) { for (int i = 0; i < count; i++) out.print(s); } void printArray(int[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(long[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(double[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(double[] array, String spec) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.printf(Locale.US, spec, array[i]); } out.println(); } void printArray(Object[] array) { boolean blank = false; for (Object x : array) { if (blank) out.print(' '); else blank = true; out.print(x); } out.println(); } @SuppressWarnings("rawtypes") void printCollection(Collection collection) { boolean blank = false; for (Object x : collection) { if (blank) out.print(' '); else blank = true; out.print(x); } out.println(); } static String memoryStatus() { return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() >> 20) + "/" + (Runtime.getRuntime().totalMemory() >> 20) + " MB"; } public void pln() { out.println(); } public void pln(int arg) { out.println(arg); } public void pln(long arg) { out.println(arg); } public void pln(double arg) { out.println(arg); } public void pln(String arg) { out.println(arg); } public void pln(boolean arg) { out.println(arg); } public void pln(char arg) { out.println(arg); } public void pln(float arg) { out.println(arg); } public void pln(Object arg) { out.println(arg); } public void p(int arg) { out.print(arg); } public void p(long arg) { out.print(arg); } public void p(double arg) { out.print(arg); } public void p(String arg) { out.print(arg); } public void p(boolean arg) { out.print(arg); } public void p(char arg) { out.print(arg); } public void p(float arg) { out.print(arg); } public void p(Object arg) { out.print(arg); } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
ee5e360bb03d7294490302cbab41ee8d
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.*; import java.util.*; public class RoundTableKnights { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(f.readLine()); boolean[] a = new boolean[n]; StringTokenizer st = new StringTokenizer(f.readLine()); for (int i = 0; i < n; i++) a[i] = st.nextToken().equals("1"); int[] factors = new int[1000]; int m = 0; for (int i = 1; i*i <= n; i++) if (n % i == 0) { factors[m++] = i; if (i != n/i) factors[m++] = n/i; } for (int i = 0; i < m; i++) if (factors[i] >= 3) for (int j = 0; j < n/factors[i]; j++) { boolean b = true; for (int k = j; k < n && b; k += n/factors[i]) b = a[k]; if (b) { System.out.println("YES"); return; } } System.out.println("NO"); } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
375b6d07eb9efed002917cd72d867930
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.IOException; import java.util.*; public class RoundTableKnights { public static void main(String[] args) { FasterScanner sc = new FasterScanner(); int n = sc.nextInt(); int knights[] = sc.nextIntArray(n); boolean check = false; first:for(int i = 1; i < (n+1)/2; i++) { if(n % i != 0) continue; second:for(int j = 0; j < i; j++) { int a = 0; while(a != n) { if(knights[(a+j) % n] == 0) continue second; a += i; } check = true; break first; } } if(check) System.out.println("YES"); else System.out.println("NO"); } public 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
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
9c8b8a9b85f4003e0b5c4b06b55e8ead
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class Main { static int maxn = 100010; static int n; static int a[] = new int[maxn]; public static void main(String[] args) { n = Input.nextInt(); for (int i = 0; i < n; i++) { a[i] = Input.nextInt(); } for (int i = 1; 2 * i < n; i++) { if (n % i == 0) { int f[] = new int[i]; for (int j = 0; j < n; j++) { f[j % i] += a[j]; } for (int j = 0; j < i; j++) { if (f[j] == n / i) { System.out.print("YES"); return; } } } } System.out.print("NO"); } private static class Input { private static StringTokenizer token = null; private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // private static BufferedReader in; // static { // try { // in = new BufferedReader(new FileReader("in.txt")); // } catch (IOException e) { // } // } public static final int nextInt() { return Integer.parseInt(nextString()); } public static final long nextLong() { return Long.parseLong(nextString()); } public static final double nextDouble() { return Double.parseDouble(nextString()); } public static final String nextString() { try { while (token == null || !token.hasMoreTokens()) { token = new StringTokenizer(in.readLine()); } } catch (IOException e) { } return token.nextToken(); } } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
7bdb4d99fc6a94259b0b672829ef8066
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.max; import static java.lang.Math.min; public class Code implements Runnable { public static void main(String[] args) throws IOException { new Thread(new Code()).start(); } int[] dividers(int n) { int[] target = new int[n / 3]; for(int i = 1, j = 0; i <= n / 3; ++i) if(n % i == 0) target[j++] = i; return target; } private void solve() throws IOException { int n = nextInt(); int[] knights = new int[n]; for(int i = 0; i < n; ++i) knights[i] = nextInt(); int[] divs = dividers(n); for(int i = 0; i < divs.length && divs[i] != 0; ++i) { int v = n / divs[i], d = n / v; for(int j = 0; j < d; ++j) { int good = 0, counter = 0; for(int k = j; k < n; k += d, ++counter) good += knights[k]; if(counter < v) break; if(good == v) { writer.println("YES"); return; } } } writer.println("NO"); } private class Pair<E extends Comparable, V extends Comparable> implements Comparable<Pair<E, V>> { public Pair(E first, V second) { this.first = first; this.second = second; } @Override public int compareTo(Pair<E, V> obj) { if(first == obj.first) return second.compareTo(obj.second); return first.compareTo(obj.first); } @Override public boolean equals(Object obj) { Pair other = (Pair)obj; return first.equals(other.first) && second.equals(other.second); } public E first; public V second; } @Override public void run() { try { if(in.equals("")) reader = new BufferedReader(new InputStreamReader(System.in)); else reader = new BufferedReader(new FileReader(in)); if(out.equals("")) writer = new PrintWriter(new OutputStreamWriter(System.out)); else writer = new PrintWriter(new FileWriter(out)); solve(); } catch(IOException e) { e.printStackTrace(); } finally { try { reader.close(); writer.close(); } catch(IOException e) { e.printStackTrace(); } } } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } private float nextFloat() throws IOException { return Float.parseFloat(nextToken()); } private String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(reader.readLine()); return st.nextToken(); } private String in = "", out = ""; private BufferedReader reader; private PrintWriter writer; private StringTokenizer st; }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
f701c5a7eaef57cffe560b6efcbb2628
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Mahmoud Aladdin */ 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); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, Scanner jin, PrintWriter jout) { int n = jin.nextInt(); boolean[] mood = new boolean[n]; for (int i = 0; i < n; i++) { mood[i] = jin.nextInt() == 1; } boolean valid = false; for (int i = 3; !valid && i <= n; i++) { if (n % i == 0) { int skip = n / i; for (int j = 0; !valid && j < skip; j += 1) { valid = true; for (int k = j; valid && k < n; k += skip) { valid &= mood[k]; } } } } jout.println(valid ? "YES" : "NO"); } } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
ab443f1855cc7fea23ea46d9ff527328
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.io.*; import java.util.*; public final class round_table_knights { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static FastScanner sc=new FastScanner(br); static PrintWriter out=new PrintWriter(System.out); public static void main(String args[]) throws Exception { int n=sc.nextInt();int[] a=new int[n];List<Integer> list=new ArrayList<Integer>(); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } for(int i=1;i*i<=n;i++) { if(n%i==0) { list.add(i); if(i!=n/i) { list.add(n/i); } } } Collections.sort(list); boolean res=false; outer: for(int i=list.size()-2;i>=0;i--) { int curr=list.get(i); if(n/curr>=3) { for(int j=0;j<=curr-1;j++) { int left=j,now=(n-(j+1))/curr,k=j+(curr*now),right=n-1-k; if(left+right==curr-1) { boolean ans=true; for(int l=j;l<n;l+=curr) { if(a[l]!=1) { ans=false; } } if(ans) { res=true; break outer; } } } } } out.println(res?"YES":"NO"); out.close(); } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public String next() throws Exception { return nextToken().toString(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
9ef1f586883cd4e75b5095186daa335f
train_004.jsonl
1301410800
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated.On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
256 megabytes
import java.util.*; import java.io.*; //code forces 71C round table knights public class Main{ public static void main(String [] args) throws Exception{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String [] g = in.readLine().split(" "); int limit = (n+1)/2; boolean b = false; ans:for(int i = 1; i<limit; i++){ if(n%i!=0) continue; //System.out.println("test1: "+i); loop:for(int o=0; o<i; o++){ int c = 0; //System.out.println("test2: "+o); while(c!=n){ if(g[(c+o)%n].equals("0")){ continue loop; } //System.out.println((c+o)%n); c+=i; } b = true; break ans; } } if(b) System.out.println("YES"); else System.out.println("NO"); } }
Java
["3\n1 1 1", "6\n1 0 1 1 1 0", "6\n1 0 0 1 0 1"]
0.5 second
["YES", "YES", "NO"]
null
Java 7
standard input
[ "dp", "number theory", "math" ]
d3a0402de1338a1a542a86ac5b484acc
The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
1,600
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
standard output
PASSED
24a572ccece23d6915d989d071445296
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.*; import java.util.*; public class CF265B { private static BufferedReader br; private static StringTokenizer st; private static PrintWriter pw; // private static Timer t = new Timer(); public CF265B() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); } String next() throws IOException { while (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } boolean hasNext() { if (st != null && st.hasMoreElements()) return true; try { while (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine()); } catch (Exception e) { return false; } return true; } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next().replace(',', '.')); } boolean nextBool() throws IOException { String s = next(); if (s.equalsIgnoreCase("true") || s.equals("1")) return true; if (s.equalsIgnoreCase("false") || s.equals("0")) return false; throw new IOException("Boolean expected, String found!"); } void solve() throws IOException{ int n = nextInt(); int[] A = new int[n]; int max = 0; for(int i = 0; i < A.length; i++) { A[i] = nextInt(); if(A[i] == 1) { max = i; } } int count1 = 0, count2 = 0; for(int i = 0; i < A.length; i++) { if(A[i] == 1) { count1++; if(i < A.length-1 && A[i+1] == 0 && i!= max) { count2++; } } } pw.println(count1+count2); pw.flush(); } public static void main(String[] args) throws IOException{ new CF265B().solve(); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
4f83312fb6fcd9e9018ef29bf8e4b142
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.*; import java.io.*; public final class b { public static void main (String[] args) throws Exception { Scanner in = new Scanner(System.in); int N = in.nextInt(); ArrayList<Integer> letters = new ArrayList<Integer>(); for (int i = 0; i < N; i++) { if( in.nextInt() == 1 ) { letters.add(i); } } int opscount = 0; boolean inletter = false; int prev = -5; for (int i: letters) { if (inletter == false) { opscount++; prev = i; inletter = true; } else if (inletter && prev == i - 1) { opscount++; prev = i; } else if (inletter) { opscount += 2; prev = i; } } System.out.println(opscount); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
1cc3f3df8bcfb4993217abbc401e328f
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.PrintWriter; import java.util.Scanner; public class Solution { public static void main(String[] args) { new Solution().run(); } private void run() { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int cntOnes, cntGroups, m; cntOnes = cntGroups = 0; boolean inGroup = false; for (int i = 0; i < n; i++) { m = in.nextInt(); if (m == 1) { cntOnes++; if (!inGroup) { cntGroups++; inGroup = true; } } else { if (inGroup) inGroup = false; } } if (cntOnes == 0) out.print(0); else out.print(cntOnes + cntGroups - 1); in.close(); out.close(); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
34ea7209ebebdbc93bcc2204c517e501
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.*; public class Inbox_100500 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; n = in.nextInt(); int[] letters = new int[n]; for (int i = 0; i < n; i++) letters[i] = in.nextInt(); int ans = 0; for (int i = 0; i < n; i++) { if (letters[i] == 0) { if (i > 0 && i + 1 < n && letters[i + 1] == 1 && ans>0) ans++; continue; } else ans++; } System.out.println(ans); in.close(); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
b7c0ba620aee8ab44c784e28d5a987f0
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class Inbox { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in= new Scanner(System.in); int n=in.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=in.nextInt(); int count=0; for(int i=0;i<n;i++) { if(a[i] == 1) { if(i>1){ if(a[i-1]==0 && count>0) count+=2; else count++; } else count++; } } System.out.println(count); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
e420632b24715f6a6bd71c03530339a5
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.*; import java.lang.*; public class sol{ public static void main(String args[]){ Scanner in = new Scanner(System.in); int ans = 0; int n = in.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i]=in.nextInt(); } boolean flg=true; for(int i=0;i<n;i++){ if(arr[i]==0) continue; if(flg) { ans++; flg=!flg; } else{ if(arr[i-1]==1) ans++; else { ans+=2; } } } System.out.println(ans); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
de0e07e78a3d16149651822611e3a50f
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Main { private static boolean sameset(int a, int b, int[] uf) { return find(a,uf) == find(b,uf); } private static void union(int a, int b, int[] uf) { uf[find(a,uf)] = find(b,uf); } private static int find(int a, int[] uf) { if(a == uf[a]) return a; int root = find(uf[a],uf); uf[a] = root; return root; } private static void BFS(int i, int[][] G, int[][] path) { // System.out.println("source vertex "+i); int[] v = new int[G.length]; // current maximum dist in u---v path ArrayDeque<Integer> A = new ArrayDeque(); A.add(i); while(A.isEmpty() == false){ int u = A.pop(); //System.out.println("for node "+u); for(int a=0;a<v.length;a++){ if(G[u][a] > 0 && v[a]==0 && a!=i){//edge in MST, later make faster //System.out.println(a+" is and edge from "+u); if(G[u][a] > v[u]){ //System.out.println(" and it incrases the sub path max "); v[a] = G[u][a]; } else v[a] = v[u]; path[i][a] = v[a]; path[a][i] = v[a]; // System.out.println("finally path from "+i+" to "+a+" is "+path[i][a]); A.add(a); } } } } private static int numd(int x) { Double xx = Double.parseDouble(String.valueOf(x)); int y = (int) Math.log10(xx); return (y+1); } private static String primeFactor(int n, boolean[] x) { String ans = ""; for(int i=2;i<x.length;i++){ if(i*i > n) break; if(x[i]){ //System.out.println("testing prome "+i); while(n!=1 && i*i <= n){ while(n%i==0){ // System.out.println(i+" can divide "+n); if(ans.equals("")) ans=String.valueOf(i); else ans+=" x "+i; n/=i; } break; } } } if(n!=1){ // System.out.println("Remainig "+n); if(ans.equals("")) ans=String.valueOf(n); else ans+=" x "+n; } return ans; } private static String solve(String s) { return null; } //counts the number of divisors using the fundamental theorem of arithmetic private static int getDivisorCount(int n) { int count = 1; int tmp = n; for(int i=2;i*i<=n;i=(i==2?3:i+2)){ //System.out.println(i+" is odd"); int pow = 0; while(n%i==0){ pow+=1; n/=i; } if(pow != 0){ ///System.out.println(i+" must be a prime"); count*=(pow+1); } } if(n!=1){ //System.out.println(n+" is the last prime factor"); count*=2; } //System.out.println("number of divisors of "+tmp+" is "+count); return count; } private static long nchoosek(int n, int k) { long prod = 1; int j = 1; int st = Math.max(n-k, k)+1; int ed = Math.min(n-k, k); for(int i=st;i<=n;i++){ for(;j<=ed;j++){ if(prod%j==0) prod/=j; else break; } prod*=i; } for(;j<=ed;j++){ prod/=j; } return prod; } //related to extgcd problems private static boolean check() { //System.out.println(a+" "+b+" "+c1+" "+c2); if(n%ibgcd(a,b)!=0) { // System.out.println("not possible"); return false; } extgcd(a,b); long gcd=0,m1=x,m2=y; gcd=d; m1*=(n/d); m2*=(n/d); a/=d; b/=d; double f1 = (m1/b);f1*=-1; double f2 = (m2/a); long le = (long) Math.ceil(f1); long re = (long) Math.floor(f2); if(le > re) return false; long cf=c1*b-c2*a; if(cf*le < cf*re){ x = m1+b*le; y = m2-a*le; }else{ x = m1+b*re; y = m2-a*re; } //System.out.println("upper and lower are "+le+" "+re); return true; } private static void extgcd(long a, long b) { if(b==0){ x=1;y=0;d=a;return; } extgcd(b,a%b); long x1 = y; long y1 = x-(a/b)*y; x=x1; y=y1; } private static Pair floyd_cycle(int z, int I, int m, int x0) { int tor = func(z,I,m,x0); int har = func(z,I,m,tor); //one step ahead of tor //System.out.println("initial value for tor and har "+tor+" "+har); while(tor!=har){ tor = func(z,I,m,tor); har = func(z,I,m,har); har = func(z,I,m,har); //System.out.println("tor and har "+tor+" "+har); } //at r-x%m now int x = 0; har = tor; tor = x0; while(har!=tor){ tor = func(z,I,m,tor); har = func(z,I,m,har); x+=1; //System.out.println("findind x tor and har "+tor+" "+har); } //now at first reapting node int lam = 1; har = func(z,I,m,tor); while(har!=tor){ har = func(z,I,m,har); lam+=1; //System.out.println("finding lam, tor and har "+tor+" "+har); } return new Pair(z,lam); } private static int func(int z, int I, int m, int x0) { return ((z*x0)%m+I%m)%m; } static class Node{ int d=-1; long num; public Node(int a,long c){ this.d = a; this.num = c; } } static class Edge{ String d; int p;//ids of nodes int q; public Edge( String x1, int y1, int w){ this.d = x1; this.p = y1; this.q = w; } } //simple sieve static boolean[] getPrimes(int n){ boolean[] ret = new boolean[n+1]; Arrays.fill(ret, true); ret[0] = ret[1] = false; for(int i=2;i*i <= n; i++){ if(ret[i]){ for(int j=i*i;j<=n;j+=i){ ret[j]=false; } } } return ret; } static long ibgcd(long a, long b){ while(b > 0){ long r = a%b; a=b; b=r; } return a; } //for extgcd static long x,y,d; //output and gdc static long c1,c2,a,b,n; // inputs //static boolean[] p = getPrimes(5000001); static class Pair{ int x; int y; public Pair(int x, int y){ this.x=x; this.y=y; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new PrintWriter(System.out)); int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split(" "); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i] = Integer.parseInt(s[i]); } int[] T = new int[n]; int index = -1; for(int i=0;i<n;i++){ if(a[i]==1){ index = i; break; } } if(index==-1){ System.out.println(0); }else{ T[index] = 1; for(int i=index+1;i<n;i++){ if(a[i]==0) T[i]=T[i-1]; else { T[i] = Math.min((T[i-1]+2), (T[i-1]+(i-index))); index=i; } } System.out.println(T[n-1]); } bw.close(); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
61e44e467b269e7ecdbbe0a59ef7fba6
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.*; public class CF465B_Inbox { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; boolean one = false; for(int i = 0; i < n; i ++){ a[i] = in.nextInt(); if(a[i] == 1) one = true; } if(!one){ System.out.println(0); return; } int ans = 1; int p = 0; while(p < n){ if(a[p] == 0){ p++; continue; } boolean done = true; for(int i = p + 1; i < n; i ++){ if(a[i] == 1){ done = false; if(i - p == 1) ans++; else ans+=2; p = i; break; } } if(done) break; } System.out.println(ans); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
0fe215b2c154aa31a715205c00409e8f
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.awt.List; import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class solver implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } String readString() { try { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine()); } catch (Exception e) { return null; } } return tok.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } int readInt() { return Integer.parseInt(readString()); } long readLong() { return Long.parseLong(readString()); } double readDouble() { return Double.parseDouble(readString()); } public static void main(String[] args) { new Thread(null, new solver(), "", 256 * (1L << 20)).start(); } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } public void run() { try { timeBegin = System.currentTimeMillis(); init(); solve(); out.close(); // time(); } catch (Exception e) { e.printStackTrace(System.err); System.exit(-1); } } void solve() { int n = readInt(); int prev = 0; int ans = 0; boolean inp = false; for (int i=0;i<n;i++) { int x = readInt(); if (x == 1) { if (prev == 1) { ans ++; } else { if (prev == 0) { ans += inp ? 2 : 1; } } inp = true; } prev = x; } out.println(ans); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
2158f5a2d64381d74b43970bc27b733b
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.*; //PrintWriter import java.math.*; //BigInteger, BigDecimal import java.util.*; //StringTokenizer, ArrayList public class R265_Div2_B //Name: Inbox (100500) { FastReader in; PrintWriter out; public static void main(String[] args) { new R265_Div2_B().run(); } void run() { in = new FastReader(System.in); out = new PrintWriter(System.out); solve(); out.close(); } void solve() { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); int cnt = 0, first = 0; for (first = 0; first < n; first++) if (a[first] == 1) break; for (int i = first; i < n; i++) { if (a[i] == 1) { cnt++; if (i > 0 && i > first && a[i-1] == 0) cnt++; } } out.println(cnt); } //----------------------------------------------------- void runWithFiles() { in = new FastReader(new File("input.txt")); try { out = new PrintWriter(new File("output.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } solve(); out.close(); } class FastReader { BufferedReader br; StringTokenizer tokenizer; public FastReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public FastReader(File f) { try { br = new BufferedReader(new FileReader(f)); tokenizer = null; } catch (FileNotFoundException e) { e.printStackTrace(); } } private String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) try { tokenizer = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } return tokenizer.nextToken(); } public String nextLine() { try { return br.readLine(); } catch(Exception e) { throw(new RuntimeException()); } } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } BigInteger nextBigInteger() { return new BigInteger(next()); } BigDecimal nextBigDecimal() { return new BigDecimal(next()); } } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
68911fbf74008c85227d34495aacfc32
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.*; //PrintWriter import java.math.*; //BigInteger, BigDecimal import java.util.*; //StringTokenizer, ArrayList public class R265_Div2_B { FastReader in; PrintWriter out; public static void main(String[] args) { new R265_Div2_B().run(); } void run() { in = new FastReader(System.in); out = new PrintWriter(System.out); solve(); out.close(); } void solve() { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); int cnt = 0, first = 0; for (first = 0; first < n; first++) if (a[first] == 1) break; for (int i = first; i < n; i++) { if (a[i] == 1) { cnt++; if (i > 0 && i > first && a[i-1] == 0) cnt++; } } out.println(cnt); } //----------------------------------------------------- void runWithFiles() { in = new FastReader(new File("input.txt")); try { out = new PrintWriter(new File("output.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } solve(); out.close(); } class FastReader { BufferedReader br; StringTokenizer tokenizer; public FastReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public FastReader(File f) { try { br = new BufferedReader(new FileReader(f)); tokenizer = null; } catch (FileNotFoundException e) { e.printStackTrace(); } } private String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) try { tokenizer = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } return tokenizer.nextToken(); } public String nextLine() { try { return br.readLine(); } catch(Exception e) { throw(new RuntimeException()); } } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } BigInteger nextBigInteger() { return new BigInteger(next()); } BigDecimal nextBigDecimal() { return new BigDecimal(next()); } } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
983fb0db99a95ca0bf226fbaa3143649
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class Test4 { private static int n,unread; private static boolean letters[]; private static int memo[][]; public static void main(String[] args) { BufferedTokenizer io = new BufferedTokenizer(System.in); int firstUnreadIndex=-1; n = io.nextInt(); unread= 0; letters = new boolean[n]; memo = new int[n+1][n+1]; for (int i = 0; i < n; i++) { letters[i] = (io.nextInt()==0)? false:true; if(letters[i]) { unread++; if(firstUnreadIndex==-1) firstUnreadIndex=i; } Arrays.fill(memo[i], -1); } Arrays.fill(memo[n], -1); if(firstUnreadIndex!=-1) io.println(1+(solve(firstUnreadIndex,1))+""); else io.println("0"); } private static int solve(int index, int read) { if(read==unread) return 0; if(memo[index][read]!=-1) return memo[index][read]; int nextIndex = getNextUnread(index); if(nextIndex!=-1) { return memo[index][read]=Math.min(2+solve(nextIndex,read+1),(index+1<n )?(letters[index+1])?1+solve(index+1,read+1):1+solve(index+1,read):1000000000); } else return 0; } private static int getNextUnread(int current) { for (int i = current+1; i < letters.length; i++) { if(letters[i]) return i; } return -1; } public static class BufferedTokenizer { private BufferedReader reader; private StringTokenizer tokenizer; private PrintWriter writer; public BufferedTokenizer(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); writer = new PrintWriter(System.out); 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 void println(String x) { writer.println(x); writer.flush(); } } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
fde9e067039255f19346f1a2ac422134
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class mai { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int p=sc.nextInt(),k=1; sc.nextLine(); int a[]=new int [p]; int b=0; for(int i=0;i<p;i++) { a[i]=sc.nextInt(); } int i; for(i=0;i<p;i++) { if(a[i]==1) { b=i; break; }} if(i==p) k=0; for(int i1=b+1;i1<p;i1++) { if(a[i1]==1 ) { if( a[i1-1]==1) { k++; } else { k=k+2; } } } System.out.println(k); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
5b81f0a5eff831237af0ba370f1d3e07
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.*; public class Inbox { public static void main(String[] args) { Scanner input= new Scanner(System.in); int n= input.nextInt(); int answer=0; boolean flag=false; int temp=0; for(int i=0;i<n;i++){ temp=input.nextInt(); if(temp==1) {answer++; flag=true;} else if(flag) { answer++; flag=false; } } if(!flag)answer--; if(answer<0)answer=0; System.out.println(answer); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
5bd278b505f017a9f1562c61885e313b
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class B_Inbox { static int [] num = new int [1010]; static int [] grid = new int [1010]; static int n; static boolean te = false; static int solve (){ int res = 0 , help = -1; for (int i = 0 ; i < n ; i++){ if (num[i] == 1 ) { if (help == 1) res++; res++; help = 0; } else { if (help == 0) help++; } } return res; } public static void main(String[] args) { Scanner x = new Scanner (System.in); n = x.nextInt(); int temp = 0 ; for (int i = 0 ; i < n ; i++){ num[i] = x.nextInt(); temp += num[i]; } if (temp==0) System.out.print(0); else if (temp == 1) System.out.print(1); else if (temp == n) System.out.print(n ); else System.out.print(solve()); } } /* * else if (help == 1) { res++; help = -10000; }*/
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
9f82921a0ef8b36dfb7a2ba5a4ba8314
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Scanner; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.util.InputMismatchException; /** * Ashesh Vidyut (Drift King) * */ public class B { public static void main(String[] args) { try { InputReader in = new InputReader(System.in); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int n = in.readInt(); int ar[] = new int[n]; for (int i = 0; i < n; i++) { ar[i] = in.readInt(); } long ans = 0l; int i = 0; while(i < n && ar[i] == 0){ i++; } for (; i < n; i++) { if(ar[i] == 1){ ans++; } else{ int j = i + 1; while(j < n && ar[j] == 0){ j++; } i = j; if(i < n){ ans += 2; } } } out.write(Long.toString(ans)); out.newLine(); out.close(); } catch (Exception e) { e.printStackTrace(); } } }class InputReader { private boolean finished = false; 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 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 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 int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { 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 readString() { int length = readInt(); if (length < 0) return null; byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) bytes[i] = (byte) read(); try { return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { return new String(bytes); } } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuffer buf = new StringBuffer(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) return readLine(); else return readLine0(); } public BigInteger readBigInteger() { try { return new BigInteger(readString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double readDouble() { 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, readInt()); 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, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) read(); return value == -1; } public String next() { return readString(); } public boolean readBoolean() { return readInt() == 1; } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
c7bd9616eccb323fb1f2893e4affb613
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class cf265d2b { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); int[] arr = new int[n]; int lastone = -1; for (int i = 0; i < n; i++){ arr[i] = Integer.parseInt(sc.next()); if (arr[i] == 1 && lastone < 0) lastone = i; } if (lastone < 0) { System.out.println("0"); return; } int moves = 0; for (int i = lastone; i < n; i++){ if (arr[i] == 1){ if (lastone < i -1) moves++; moves++; lastone = i; } } System.out.println("" + moves); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
6b27b2f0304d02013987c492e4bc9dad
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.*; import java.util.*; import java.io.DataInputStream; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.Stack; public class fast{ static class Parser { final int BUFFER_SIZE = 1 << 16; DataInputStream din; byte[] buffer; int bufferPointer, bytesRead; public Parser(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public int nextInt() throws Exception { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; return ret; } public long nextLong() throws Exception { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; return ret; } public double nextDouble() throws Exception { double ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; return ret; } public String next() throws Exception { StringBuffer ret=new StringBuffer(); byte c = read(); while (c <= ' ') c = read(); do { ret = ret.append((char)c); c = read(); } while (c > ' '); return ret.toString(); } private void fillBuffer() throws Exception { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws Exception { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } public static boolean ispalin(String str){ StringBuffer buffer=new StringBuffer(str); String rev=buffer.reverse().toString(); if(rev.contentEquals(str)) return true; return false; } public static void main (String[] args) throws Exception{ Parser s = new Parser(System.in); PrintWriter ww = new PrintWriter(System.out,true); int n=s.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=s.nextInt(); int left=0; int right=0; for(int j=0;j<n;j++){ if(arr[j]==1){ left=j; break; } } for(int j=n-1;j>=0;j--){ if(arr[j]==1){ right=j; break; } } int cnt=0; for(int i=left;i<=right;i++){ if(arr[i]==1){ cnt++; } else{ while(i<=right&&arr[i]!=1){ i++; } if(i<=right) {i--; cnt++; } } } ww.println(cnt); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
47a699b582f9753037fcf778c220080b
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.IOException; import java.io.PrintStream; import java.util.Scanner; public class Main1 { public static void main (String[] args) throws IOException{ Scanner in = new Scanner(System.in); PrintStream out = new PrintStream(System.out); int n = in.nextInt(); int result = 0; int pre1 = -1, current = -1; for (int i = 0; i < n; ++i){ current = in.nextInt(); if (current == 1){ if (result == 0 || pre1 == 1) result += 1; else result += 2; } pre1 = current; } out.print(result); in.close(); out.close(); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
ac2919f12769329466957b2cf33ebb13
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class tr { public static void main (String args []){ Scanner s = new Scanner(System.in); int x = s.nextInt(); int list[]=new int[x]; int count = 0; for(int i = 0 ; i<x ; ++i){ list[i] = s.nextInt(); if(list[i] ==1){ if(i>1){ if(list[i-1]==0&&count>0)count+=2; else ++count; }else ++ count; }} System.out.println(count); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
1b08f77441947b383897c8e64e73d416
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Inbox100500 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = in.nextInt(); } if (n == 1) { System.out.println(a[0]); } else { int[] DP = new int[n]; DP[0] = a[0]; DP[1] = a[1] + DP[0]; for (int i = 2; i < DP.length; i++) { DP[i] = a[i] + DP[i - 1] + (DP[i - 1] != 0 && a[i - 1] == 0 && a[i] == 1 ? 1 : 0); } // System.out.println(Arrays.toString(DP)); System.out.println(DP[n - 1]); } } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
451ea0e122c7a6b3daeb5f1d1ed54429
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class B { public static void main(String args[]){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int m[] = new int[n]; for(int i= 0; i < n ; i++) m[i] = in.nextInt(); int cavap = 0; for(int i=0 ; i< n; i++){ if(m[i]==1){ cavap++; i++; while(i<=n-1 && m[i]==1){ cavap++; i++; } cavap++; } } cavap--; if(cavap == -1) System.out.println(0); else System.out.println(cavap); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
6dbf141c6b67a2de186506180f491283
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class Inbox2 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = Integer.parseInt(scan.nextLine()); String[] emails = scan.nextLine().split(" "); int ans = 0; boolean firstOne = false; for (int i = 0; i < n; i++) { if (emails[i].equals("1")) { ans++; firstOne = true; //Encountered 1. } // Don't count 0's if a 1 has not been encountered yet. if (firstOne == true) { if (emails[i].equals("0")) { ans++; //Reset back to false until another 1 is encountered. firstOne = false; } } } // Don't count last number if it is 0 (already read). if (emails[n - 1].equals("0")) { ans--; } // If answer is negative at all (not possible) if (ans < 0) { ans = 0; } System.out.println(ans); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
80a84b06ee3310fdad0c5ee092c2f787
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
//package Codeforces.Div2B_265.Code1; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /* * some cheeky quote */ public class Main { FastScanner in; PrintWriter out; public void solve() throws IOException { int n = in.nextInt(); int a[] = new int[n + 1]; int ca[] = new int[n + 1]; int count = 0; int total = 0; for (int i = 0; i < a.length - 1; i++) { a[i] = in.nextInt(); if (i == 0) { ca[i] = a[i]; } else { ca[i] = ca[i - 1] + a[i]; } } count = a[0]; ca[n] = ca[n - 1]; for (int i = 1; i < ca.length; i++) { if (ca[i] != ca[i - 1]) { count++; } else { if (count != 0) { total += count + 1; count = 0; } } } System.out.println(Math.max(0, total - 1)); } public void run() { try { in = new FastScanner(); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } } public static void main(String[] arg) { new Main().run(); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
e397015b52595d4bb027d9e59e9db3fb
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Inbox100500 { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(f.readLine()); boolean[] b = new boolean[n]; StringTokenizer st = new StringTokenizer(f.readLine()); for (int i = 0; i < n; i++) b[i] = st.nextToken().equals("1"); int count = 0; for (int i = 0; i < n; i++) if (b[i]) count += (i > 0 && b[i-1]) ? 1 : 2; if (count > 0) count--; System.out.println(count); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
3c10ef432fa51eda9c0cdced219eed09
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * Created by Egor on 07/09/14. */ public class TaskB { BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public static void main(String[] args) throws IOException { new TaskB().run(); } public void solve() throws IOException { int n = nextInt(); boolean[] a = new boolean[n]; int current = 99999; int answer = 0; for (int i = 0; i < n; i++) { a[i] = (nextInt() == 1); if (a[i]) { if (i - current < 2) { answer++; } else { answer += 2; } current = i; } } writer.println(answer); } public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(System.out); solve(); writer.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
5175f33ca77d69aea3bc1fce1a19fc04
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
// package IntroToJava; import java.util.Scanner; public class JTest { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] bits = new int[n]; int sum = 0; for(int i=0; i<n; i++){ bits[i] = input.nextInt(); sum += bits[i]; } input.close(); for(int i=0; i<n-1; i++){ if(bits[i] == 1 && bits[i+1] == 0) sum++; } if(bits[n-1] == 0 && sum != 0) sum--; System.out.println(sum); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
c06ade8042e2e8744ccbdb125c2f2313
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; boolean one = false; int r = 0; for(int i = 0; i < n; i++){ a[i] = in.nextInt(); if(!one && a[i] == 1){ one = true; } } if(!one){ System.out.println(0); } else{ boolean mail = false; for(int i = 0; i <n; i++){ if(a[i] == 1){ mail = true; r++; //System.out.println(" i = " + i + " r = " + r); while((i+1 < n) && (a[i+1] == 1)){ r++; i++; //System.out.println(" i = " + i + " r = " + r); } mail = false; r++; } } System.out.println(r-1); } } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
73dbecfa979519b273d13ece228e5ac7
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; import java.io.PrintStream; import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author angrySCV */ 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 { private int colichestvoOperacii = 0; int[] pismo; int n; static int i = 0; public void solve (int testNumber, Scanner in, PrintWriter out) { n = in.nextInt(); pismo = new int[n]; for (; i < n; i++) pismo[i] = in.nextInt(); for (i = 0; i < n; i++) { if (pismo[i] == 1) { colichestvoOperacii++; if ((i + 1 < n) && (pismo[i + 1]) == 0) { if (lastOnlyZeros()==true) break; else colichestvoOperacii++; } } } System.out.println(colichestvoOperacii); } public boolean lastOnlyZeros () { for (int j=i+1; j < n; j++) if (pismo[j] == 1) return false; return true; } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
7f60f056827a2700ab8332699d831c4d
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class Inbox { public static void main(String[] args) { Scanner console=new Scanner(System.in); int n=console.nextInt(); int []s=new int [n]; int []c =new int[n]; int x=0,sum=0,i; for(i=0;i<s.length;i++){ s[i]=console.nextInt(); if(s[i]==1){ c[x]=i; x=x+1; sum=sum+1; } } for( i=1;i<x;i++) if((c[i]-c[i-1])>1) sum=sum+1; System.out.println(sum); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
6a466f2082cc643f6aaf512d6c57bf91
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class M{ public static void main(String[] a) { Scanner s = new Scanner(System.in); int n = s.nextInt(),i = -1,r[] = new int[n] , c = 0; boolean flag = false; for(;++i < n;) r[i] = s.nextInt(); for(i = -1; ++i < n;){ if(r[i] == 1) {c++; flag = true;} else if(r[i] == 0 && flag && i != n-1 && r[i+1] != 0 ){c++; flag = false;} } System.out.println(c); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
3fb4b4999f9b901447d3f5ea2205a976
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) throws IOException { Reader.init(System.in); int n = Reader.nextInt(); int ar[] = new int [n]; int c = 0 ; for (int i = 0; i < n; i++) { ar[i] = Reader.nextInt(); } boolean back = false; for (int i = 0; i < n; i++) { if(ar[i]==1){ if(back){ c++; back=false; } c++; while((i+1)<n && ar[i+1]==1){ c++; i++; } if(i<n)back=true; } } System.out.println(c); } } class suger implements Comparable<suger> { double d; double c; public suger(double d, double c) { this.d= d ; this.c= c; } @Override public int compareTo(suger o) { return Double.compare(this.c,o.c); } } 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()); } static public void nextIntArrays(int[]... arrays) throws IOException { for (int i = 1; i < arrays.length; ++i) { if (arrays[i].length != arrays[0].length) { throw new InputMismatchException("Lengths are different"); } } for (int i = 0; i < arrays[0].length; ++i) { for (int[] array : arrays) { array[i] = nextInt(); } } } static public void nextLineArrays(String[]... arrays) throws IOException { for (int i = 1; i < arrays.length; ++i) { if (arrays[i].length != arrays[0].length) { throw new InputMismatchException("Lengths are different"); } } for (int i = 0; i < arrays[0].length; ++i) { for (String[] array : arrays) { array[i] = reader.readLine(); } } } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
9c0b601601eee137e72bb56d59116674
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; import java.util.Arrays; /** * Built using CHelper plug-in * Actual solution is at the top */ public class MainB { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] s = new int[n]; for (int i = 0; i < n; i++) { s[i] = in.nextInt(); } int c = 0; int swipe = 1; int i = 0; while (i < n && s[i] == 0) { i++; } for (; i < n; i++) { if (s[i] == 1) { c += Math.min(swipe, 2); swipe = 1; } else { swipe++; } } out.write(c + "\n"); } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
d4fef1ea9d569db250412e4a0ea3fecb
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Inbox { public static void main(String[] args) { int n, i, steps = 0; Scanner a = new Scanner(System.in); n = a.nextInt(); int elements[] = new int[n]; int a2[] = new int[n]; for (i = 0; i < n; i++) { elements[i] = a.nextInt(); } if (n > 2) { for (i = 0; i < n - 2; i++) { if (elements[i] == 1) { steps = steps + 1; if ((elements[i + 1] == 0) && (elements[i + 2] == 0)) { steps = steps + 1; } else if ((elements[i + 1] == 0) && (elements[i + 2] == 1)) { steps = steps + 1; } } } }if(n>1){ if (elements[n - 2] == 1) { steps = steps + 1; } } if (elements[n - 1] == 1) { steps = steps + 1; } if(n>2){ if ((elements[n - 2] == 0) && (elements[n - 1] == 0)) { steps = steps - 1; } } if (Arrays.equals(elements, a2)) { steps = 0; } System.out.println(steps); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
3b49a4f163b01f127107a330e8ed3fca
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { static int[] array; static int n; public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader bfr = new BufferedReader( new InputStreamReader(System.in)); String s = ""; while ((s = bfr.readLine()) != null) { n = Integer.parseInt(s); array = new int[n]; StringTokenizer tkn = new StringTokenizer(bfr.readLine()); int ctr = 0; boolean first = true; boolean x = true; for (int i = 0; i < n; i++) { int integer = Integer.parseInt(tkn.nextToken()); if (integer == 1 && (x || first)) { ctr++; x = true; first = false; } else if (integer == 1 && !x) { ctr += 2; x = true; } else { x = false; } } System.out.println(ctr); } } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
93cb77092f6c274553319e9422f64ad4
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class ass { public static void main(String args[]) { Scanner in = new Scanner(System.in); int a[]; int n=in.nextInt(); a=new int[n]; int sum=0,sum1=0; for(int i=0;i<n;i++){ a[i] = in.nextInt(); if(a[i]==1) sum++; if(i!=0&&a[i]==1&&a[i-1]==1) sum1++; } if(sum>0) System.out.print(sum*2-1-sum1); else System.out.print("0"); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
0462132490cd14bc8314af204c0a8ccb
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int [] a = new int[n+1]; int f=0,l=0; for (int i = 1; i <= n; i++) { a[i] = sc.nextInt(); if(a[i]==1){l=i;} f+=a[i]; } if(f==1){System.out.println("1");return;} int k = 0; boolean rost = false; for (int i = 1; i <=l; i++) { if (a[i]==1) { k++; rost = true; }else if(rost){ k++; rost = false; } } System.out.println(k); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
1c43dcdcef372975c5713fc3e8a98e0a
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.*; import java.io.*; public class Codeforces { static long arr[]; static long sum[]; public static void main(String[] args) throws IOException { Reader.init(System.in); int n = Reader.nextInt(); int count = 0; int state = 0; int x = 0; for(int i = 0; i < n; i++){ x = Reader.nextInt(); if(x == 0){ if(state == 1) count++; state = 0; } else { state = 1; count++; } } if(x == 0) count--; System.out.println(Math.max(count, 0)); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; public static int pars(String x) { int num = 0; int i = 0; if (x.charAt(0) == '-') { i = 1; } for (; i < x.length(); i++) { num = num * 10 + (x.charAt(i) - '0'); } if (x.charAt(0) == '-') { return -num; } return num; } static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static void init(FileReader input) { reader = new BufferedReader(input); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return pars(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
26bdfe6be1c744e4d2682d799bb7e256
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Brenner */ 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 [] v = new int[n]; for(int i = 0;i < n; i++){ v[i] = in.nextInt(); } int i = 0; while( i < n && v[i] == 0){ i +=1; } //System.out.println(i); int cont = 0; int cont0 = 0; for(; i < n; i++){ if(v[i] == 1){ if(cont0 >= 2){ cont0 = 0; cont += 1; } else { cont += cont0; cont0 = 0; } cont += 1; } else{ cont0 += 1; } } out.print(cont); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
f802ab19d4e0f2ed71de41e191eaf990
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class Main { public void work() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); boolean a[] = new boolean[n]; for(int i=0; i<n; i++) { if(sc.nextInt() == 0) a[i] = false; else a[i] = true; } boolean firstoneisfound = false; int ans = 0; for(int i=0; i < n; i++) { if(a[i]) //letter status is 1, unread! { ans++; if(i > 0 && !a[i-1] && firstoneisfound) ans++; firstoneisfound = true; } } System.out.println(ans); } public static void main(String[] args) { Main ob = new Main(); ob.work(); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
e0f0cf3d93b5a5b411a1c894e61751c7
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.Scanner; public class CodeforcesProblems { public static void main(String[] arg){ Scanner sIn = new Scanner(System.in); int count = sIn.nextInt(); int[] list = new int[count]; boolean singleMode = false; boolean endWithOne = false; int actions = 0; int index = 0; for ( ; index < count; index++) { list[index] = sIn.nextInt(); if (list[index] == 1){ actions++; singleMode = true; break; } } index++; for ( ; index < count; index++ ){ list[index] = sIn.nextInt(); if (list[index] == 1) { actions++; } else if (list[index] == 0 && singleMode) { int tmpActions = 1; index++; for ( ; index < count; index++){ list[index] = sIn.nextInt(); tmpActions++; if (list[index] == 1) { endWithOne = true; break; } } // 遇‘1’结束还是大于等于count结束 if (endWithOne){ actions = tmpActions >= 2 ? actions + 2 : actions + tmpActions; } endWithOne = false; } } System.out.println(actions); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
8d0481cba2685e1bfdbbc944fb4eb43f
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.*; public class tmp { public static void main(String [] args){ Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int [] arr = new int[n+1]; for(int i=0; i<n; i++) arr[i] = scan.nextInt(); int zerosend = 0; for(int i=n-1; i>=0; i--){ if(arr[i] == 1) break; zerosend++; } int moves = 0; for(int i=0; i<n-zerosend-1; i++){ if(arr[i] == 1 && arr[i+1] == 1){ moves++; }else if(arr[i] == 1 && arr[i+1] == 0) moves+=2; } System.out.println(arr[n-zerosend-1 < 0 ? 0 : n-zerosend-1] == 1 ? moves+1 : moves); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
9117b6fd912189ce6a3528280d0360a7
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CF265_Inbox_100500 { public static void main(String[] args) { MyScanner in = new MyScanner(); int n = in.nextInt(); int[] l = new int[n]; for(int i = 0; i < n; i++) { l[i] = in.nextInt(); } //1 means unread //0 means read int cnt = 0; for(int i = 0; i < n; i++) { if(l[i] == 1) { cnt++; while(l[i] == 1 && i != n-1) //check the latter case { i++; if(l[i] == 1) { cnt++; } } if(i != n-1) { for(int j = i+1; j < n; j++) { if(l[j] == 1) { cnt++; break; } } } } } System.out.println(cnt); } // -----------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
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
c266a5ab57204dec20636cf5b97dec7d
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.*; import java.math.*; import java.io.*; import java.text.*; import java.awt.Point; import static java.util.Arrays.*; import static java.lang.Integer.*; import static java.lang.Double.*; import static java.lang.Long.*; import static java.lang.Short.*; import static java.lang.Math.*; import static java.math.BigInteger.*; import static java.util.Collections.*; public class Main { private Scanner in; private StringTokenizer st; private PrintWriter out; private DecimalFormat fmt = new DecimalFormat("0.0000000000"); public void solve() throws Exception { int n = in.nextInt(); LinkedList<Integer> arr = new LinkedList<Integer>(); for (int i=0; i<n; i++) { arr.add(in.nextInt()); } int ans = 0; int count = 0; boolean first = true; while (!arr.isEmpty()) { int curr = arr.poll(); if (curr == 0) { if (!first) { count ++; } continue; } else { if (count > 0) { ans += 2; } else { ans ++; } count = 0; first = false; } } out.println(ans); } public Main() { this.in = new Scanner(System.in); this.out = new PrintWriter(System.out); } public void end() { try { this.out.flush(); this.out.close(); this.in.close(); } catch (Exception e){ //do nothing then :) } } public static void main(String[] args) throws Exception { Main solver = new Main(); solver.solve(); solver.end(); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
5ee1bf00e7b3debc4ce05d293783e6ee
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Scanner; public class Main { static class TaskB { private int getOperations(int n, boolean[] isUnread) { int gap = 0; // The gap between unread emails int ans = 0; boolean seenUnread = false; // Have we seen an unread mail? for (int i=0; i<n; i++) { if (isUnread[i]) { if (gap > 0) { ans += 2; } else { ans ++; } gap = 0; if (!seenUnread) { seenUnread = true; } } else { if (seenUnread) { gap ++; } } } return ans; } ///////////////////////////////////////////////////////////////////// //***************** IGNORE BELOW **********************************// /////////////////////////////////////////////////////////////////// public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); boolean[] a = new boolean[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt() == 1; int ans = getOperations(n, a); out.println(ans); } } 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(); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output
PASSED
9925914bdd6a6c3ef647b6b53240c3d6
train_004.jsonl
1410103800
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?
256 megabytes
import java.util.*; import javax.jws.Oneway; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); ArrayList<Integer> onesPositions = new ArrayList<Integer>(); for (int i = 0; i < N; i++) { if (in.nextInt() == 1) { onesPositions.add(i); } } int sum = 0; for (int i = 0; i < onesPositions.size(); i++) { if(i == 0){ sum +=1; continue; } int diff = onesPositions.get(i) - onesPositions.get(i - 1); if (diff > 2) { sum += 2; } else { sum += diff; } } System.out.println(sum); } }
Java
["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"]
1 second
["3", "4", "0"]
NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read.
Java 7
standard input
[ "implementation" ]
0fbc306d919d7ffc3cb02239cb9c2ab0
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.
1,000
Print a single number — the minimum number of operations needed to make all the letters read.
standard output