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
c3e13c8075c07d6c3a9dbaf815c6aa67
train_004.jsonl
1355047200
Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.
256 megabytes
import java.io.*; import java.util.HashMap; import java.util.Locale; import java.util.StringTokenizer; /** * Created with IntelliJ IDEA. * User: KarN * Date: 09.12.12 * Time: 18:26 * To change this template use File | Settings | File Templates. */ public class Solution implements Runnable { int solve() throws Throwable { int n = in.nextInt(); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int[] ansl = new int[n]; int[] ansr = new int[n]; int k = 0; int diff = 0; for (int i = 0; i < 2 * n; i++) { int cur = in.nextInt(); if (map.containsKey(cur)) { ansl[k++] = map.get(cur); ansr[k - 1] = i + 1; map.remove(cur); } else map.put(cur, i + 1); } if (map.isEmpty()) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(ansl[i]); sb.append(' '); sb.append(ansr[i]); sb.append('\n'); } out.print(sb.toString()); } else out.println(-1); return 0; } public static void main(String[] args) { new Solution().run(); } final int USE_FILES = 1; final int USE_STDIO = 0; final int DEFAULT = -1; int IO_MODE = USE_FILES; FastScanner in; PrintWriter out; @Override public void run() { boolean ONLINE_JUDGE = false; try { long startTime = System.currentTimeMillis(); try { ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; Locale.setDefault(Locale.US); } catch (Throwable t) { System.err.println("Operation is forbidden"); } if (IO_MODE == USE_FILES || (IO_MODE == DEFAULT && !ONLINE_JUDGE)) { in = new FastScanner(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } else { in = new FastScanner(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } int ret = solve(); in.close(); out.close(); long endTime = System.currentTimeMillis(); long freeMemory = Runtime.getRuntime().freeMemory(); long totalMemory = Runtime.getRuntime().totalMemory(); System.err.printf("Time = %.3f s\n", (endTime - startTime) / 1000.0); System.err.printf("Memory = %.3f MB\n", (totalMemory - freeMemory) / (float)(1 << 20)); if (ret != 0) System.exit(ret); } catch (Throwable t) { t.printStackTrace(ONLINE_JUDGE ? System.out : System.err); System.exit(-1); } } @SuppressWarnings("unused") class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader reader) throws Throwable { br = new BufferedReader(reader); } public String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public void close() throws IOException { br.close(); } } }
Java
["3\n20 30 10 30 20 10", "1\n1 2"]
1 second
["4 2\n1 5\n6 3", "-1"]
null
Java 7
input.txt
[ "constructive algorithms", "sortings" ]
0352429425782d7fe44818594eb0660c
The first line contains integer n (1 ≀ n ≀ 3Β·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≀ ai ≀ 5000) β€” the numbers that are written on the cards. The numbers on the line are separated by single spaces.
1,200
If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line β€” the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.
output.txt
PASSED
62e14693d3ddb08dc6a9698e27c861f1
train_004.jsonl
1355047200
Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.
256 megabytes
import java.io.*; import java.util.HashMap; import java.util.Locale; import java.util.StringTokenizer; /** * Created with IntelliJ IDEA. * User: KarN * Date: 09.12.12 * Time: 18:26 * To change this template use File | Settings | File Templates. */ public class Solution implements Runnable { final static int ALPHABET = 6000; int solve() throws Throwable { int n = in.nextInt(); //HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int[] map = new int[ALPHABET]; int[] ansl = new int[n]; int[] ansr = new int[n]; int k = 0; int diff = 0; for (int i = 0; i < 2 * n; i++) { int cur = in.nextInt(); if (map[cur] != 0) { ansl[k++] = map[cur]; ansr[k - 1] = i + 1; map[cur] = 0; diff--; } else { diff++; map[cur] = i + 1; } } if (diff == 0) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(ansl[i]); sb.append(' '); sb.append(ansr[i]); sb.append('\n'); } out.print(sb.toString()); } else out.println(-1); return 0; } public static void main(String[] args) { new Solution().run(); } final int USE_FILES = 1; final int USE_STDIO = 0; final int DEFAULT = -1; int IO_MODE = USE_FILES; FastScanner in; PrintWriter out; @Override public void run() { boolean ONLINE_JUDGE = false; try { long startTime = System.currentTimeMillis(); try { ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; Locale.setDefault(Locale.US); } catch (Throwable t) { System.err.println("Operation is forbidden"); } if (IO_MODE == USE_FILES || (IO_MODE == DEFAULT && !ONLINE_JUDGE)) { in = new FastScanner(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } else { in = new FastScanner(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } int ret = solve(); in.close(); out.close(); long endTime = System.currentTimeMillis(); long freeMemory = Runtime.getRuntime().freeMemory(); long totalMemory = Runtime.getRuntime().totalMemory(); System.err.printf("Time = %.3f s\n", (endTime - startTime) / 1000.0); System.err.printf("Memory = %.3f MB\n", (totalMemory - freeMemory) / (float)(1 << 20)); if (ret != 0) System.exit(ret); } catch (Throwable t) { t.printStackTrace(ONLINE_JUDGE ? System.out : System.err); System.exit(-1); } } @SuppressWarnings("unused") class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader reader) throws Throwable { br = new BufferedReader(reader); } public String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public void close() throws IOException { br.close(); } } }
Java
["3\n20 30 10 30 20 10", "1\n1 2"]
1 second
["4 2\n1 5\n6 3", "-1"]
null
Java 7
input.txt
[ "constructive algorithms", "sortings" ]
0352429425782d7fe44818594eb0660c
The first line contains integer n (1 ≀ n ≀ 3Β·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≀ ai ≀ 5000) β€” the numbers that are written on the cards. The numbers on the line are separated by single spaces.
1,200
If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line β€” the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.
output.txt
PASSED
f2db8acbcf8a9e1c9e0914df3de65461
train_004.jsonl
1355047200
Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.
256 megabytes
import java.io.*; import java.util.HashMap; import java.util.Hashtable; import java.util.Locale; import java.util.StringTokenizer; /** * Created with IntelliJ IDEA. * User: KarN * Date: 09.12.12 * Time: 18:26 * To change this template use File | Settings | File Templates. */ public class Solution implements Runnable { int solve() throws Throwable { int n = in.nextInt(); Hashtable<Integer, Integer> map = new Hashtable<Integer, Integer>(); int[] ansl = new int[n]; int[] ansr = new int[n]; int k = 0; int diff = 0; for (int i = 0; i < 2 * n; i++) { int cur = in.nextInt(); if (map.containsKey(cur)) { ansl[k++] = map.get(cur); ansr[k - 1] = i + 1; map.remove(cur); } else map.put(cur, i + 1); } if (map.isEmpty()) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(ansl[i]); sb.append(' '); sb.append(ansr[i]); sb.append('\n'); } out.print(sb.toString()); } else out.println(-1); return 0; } public static void main(String[] args) { new Solution().run(); } final int USE_FILES = 1; final int USE_STDIO = 0; final int DEFAULT = -1; int IO_MODE = USE_FILES; FastScanner in; PrintWriter out; @Override public void run() { boolean ONLINE_JUDGE = false; try { long startTime = System.currentTimeMillis(); try { ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; Locale.setDefault(Locale.US); } catch (Throwable t) { System.err.println("Operation is forbidden"); } if (IO_MODE == USE_FILES || (IO_MODE == DEFAULT && !ONLINE_JUDGE)) { in = new FastScanner(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } else { in = new FastScanner(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } int ret = solve(); in.close(); out.close(); long endTime = System.currentTimeMillis(); long freeMemory = Runtime.getRuntime().freeMemory(); long totalMemory = Runtime.getRuntime().totalMemory(); System.err.printf("Time = %.3f s\n", (endTime - startTime) / 1000.0); System.err.printf("Memory = %.3f MB\n", (totalMemory - freeMemory) / (float)(1 << 20)); if (ret != 0) System.exit(ret); } catch (Throwable t) { t.printStackTrace(ONLINE_JUDGE ? System.out : System.err); System.exit(-1); } } @SuppressWarnings("unused") class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader reader) throws Throwable { br = new BufferedReader(reader); } public String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public void close() throws IOException { br.close(); } } }
Java
["3\n20 30 10 30 20 10", "1\n1 2"]
1 second
["4 2\n1 5\n6 3", "-1"]
null
Java 7
input.txt
[ "constructive algorithms", "sortings" ]
0352429425782d7fe44818594eb0660c
The first line contains integer n (1 ≀ n ≀ 3Β·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≀ ai ≀ 5000) β€” the numbers that are written on the cards. The numbers on the line are separated by single spaces.
1,200
If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line β€” the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.
output.txt
PASSED
9924078ec82f9b3167e444d5ec7c2422
train_004.jsonl
1355047200
Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Solution { BufferedReader in; StringTokenizer st; PrintWriter out; void solve() throws IOException { int n = ni(); ArrayList<int[]> ret = new ArrayList<int[]>(); int[] v = new int[5010]; Arrays.fill(v, -1); for (int i = 0; i < 2 * n; ++i) { int a = ni(); if (v[a] == -1) { v[a] = i + 1; } else { ret.add(new int[] { i + 1, v[a] }); v[a] = -1; } } boolean all = true; for (int i = 0; i < v.length; ++i) if (v[i] != -1) { all = false; break; } if (all) { for (int[] a : ret) out.println(a[0] + " " + a[1]); } else { out.println(-1); } } public Solution() throws IOException { Locale.setDefault(Locale.US); in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); solve(); in.close(); out.close(); } String ns() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int ni() throws IOException { return Integer.valueOf(ns()); } long nl() throws IOException { return Long.valueOf(ns()); } double nd() throws IOException { return Double.valueOf(ns()); } public static void main(String[] args) throws IOException { new Solution(); } }
Java
["3\n20 30 10 30 20 10", "1\n1 2"]
1 second
["4 2\n1 5\n6 3", "-1"]
null
Java 7
input.txt
[ "constructive algorithms", "sortings" ]
0352429425782d7fe44818594eb0660c
The first line contains integer n (1 ≀ n ≀ 3Β·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≀ ai ≀ 5000) β€” the numbers that are written on the cards. The numbers on the line are separated by single spaces.
1,200
If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line β€” the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.
output.txt
PASSED
8e149724be28ccc2bec6219495d99f47
train_004.jsonl
1355047200
Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; public class Main { private static final int maxn = 50005; private static final int a[] = new int[maxn]; public static void main(String[] args) throws Exception { String name = "input.txt"; BufferedReader br = new BufferedReader(new FileReader(name)); int n = Integer.parseInt(br.readLine()); String aux[] = br.readLine().split(" "); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 2 * n; i++) { int pos = Integer.parseInt(aux[i]); if (a[pos] == 0) { a[pos] = i + 1; } else { sb.append( (i + 1) + " " + a[pos] + "\n"); a[pos] = 0; } } for (int i = 0; i < maxn; i++) { if (a[i] != 0) { sb = new StringBuilder("-1\n"); break; } } FileWriter fr = new FileWriter("output.txt"); fr.write(sb.toString()); fr.close(); br.close(); } }
Java
["3\n20 30 10 30 20 10", "1\n1 2"]
1 second
["4 2\n1 5\n6 3", "-1"]
null
Java 7
input.txt
[ "constructive algorithms", "sortings" ]
0352429425782d7fe44818594eb0660c
The first line contains integer n (1 ≀ n ≀ 3Β·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≀ ai ≀ 5000) β€” the numbers that are written on the cards. The numbers on the line are separated by single spaces.
1,200
If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line β€” the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.
output.txt
PASSED
3ca579e5716a11f3244e99d5cb4e8089
train_004.jsonl
1355047200
Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.StringTokenizer; public class A { BufferedReader in; StringTokenizer str; PrintWriter out; String SK; String next() throws IOException { while ((str == null) || (!str.hasMoreTokens())) { SK = in.readLine(); if (SK == null) return null; str = new StringTokenizer(SK); } return str.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } void solve() throws IOException { int n = nextInt(); int[] ns = new int[5001]; Arrays.fill(ns, -1); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 2*n; i++){ int k = nextInt(); if (ns[k] == -1){ ns[k] = i + 1; } else { sb.append(ns[k]).append(" ").append(i + 1).append("\n"); ns[k] = -1; } } for (int sh : ns){ if (sh != -1){ out.println(-1); return; } } out.print(sb.toString()); } void run() throws IOException { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); solve(); out.close(); } public static void main(String[] args) throws IOException { new A().run(); } }
Java
["3\n20 30 10 30 20 10", "1\n1 2"]
1 second
["4 2\n1 5\n6 3", "-1"]
null
Java 7
input.txt
[ "constructive algorithms", "sortings" ]
0352429425782d7fe44818594eb0660c
The first line contains integer n (1 ≀ n ≀ 3Β·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≀ ai ≀ 5000) β€” the numbers that are written on the cards. The numbers on the line are separated by single spaces.
1,200
If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line β€” the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.
output.txt
PASSED
393c62655a045631766d368cf6973eab
train_004.jsonl
1355047200
Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.
256 megabytes
import java.io.*; import java.util.*; public class A { void run() throws IOException { int n = ni(); int m = 5000; Vect[] b = new Vect[m]; for (int i = 0; i < m; i++) b[i] = new Vect(); for (int i = 0; i < n * 2; i++) b[ni() - 1].push(i + 1); for (int i = 0; i < m; i++) { if (b[i].size % 2 != 0) { pw.print(-1); return; } } for (int i = 0; i < m; i++) while (b[i].notempty()) pw.println(b[i].pop() + " " + b[i].pop()); } private static class Vect { int size; int[] elements; public Vect() { elements = new int[16]; } public void push(int element) { if (elements.length == size) { int[] newElements = new int[elements.length * 2]; for (int i = 0; i < size; i++) newElements[i] = elements[i]; elements = newElements; } elements[size++] = element; } public int pop() { return elements[--size]; } public boolean notempty() { return size != 0; } } public A(BufferedReader _br, PrintWriter _pw) { br = _br; pw = _pw; } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } String nl() throws IOException { return br.readLine(); } PrintWriter pw; BufferedReader br; StringTokenizer st; public static void main(String[] args) throws IOException { long timeout = System.currentTimeMillis(); BufferedReader _br = new BufferedReader(new FileReader("input.txt")); PrintWriter _pw = new PrintWriter("output.txt"); new A(_br, _pw).run(); System.out.println(System.currentTimeMillis() - timeout); _br.close(); _pw.close(); } }
Java
["3\n20 30 10 30 20 10", "1\n1 2"]
1 second
["4 2\n1 5\n6 3", "-1"]
null
Java 7
input.txt
[ "constructive algorithms", "sortings" ]
0352429425782d7fe44818594eb0660c
The first line contains integer n (1 ≀ n ≀ 3Β·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≀ ai ≀ 5000) β€” the numbers that are written on the cards. The numbers on the line are separated by single spaces.
1,200
If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line β€” the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.
output.txt
PASSED
0562a74a55cdba035088c5a05f301be0
train_004.jsonl
1355047200
Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.
256 megabytes
import java.io.*; import java.util.*; public class A implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); public static void main(String[] args) { new Thread(null, new A(), "", 256 * (1L << 20)).start(); } public void run() { try { /*long t1 = System.currentTimeMillis();*/ //in = new BufferedReader(new InputStreamReader(System.in)); //out = new PrintWriter(System.out); in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); solve(); in.close(); out.close(); /*long t2 = System.currentTimeMillis(); System.err.println("Time = " + (t2 - t1));*/ } catch (Throwable t) { t.printStackTrace(System.err); System.exit(-1); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } int countX = 5001; int[] a; int[] x; int[] prev; void solve() throws IOException { int n = readInt(); a = new int[2*n]; x = new int[countX]; prev = new int[countX]; for(int i=0; i<countX; ++i) { prev[i] = -1; x[i] = 0; } for(int i=0; i<2*n; ++i) { a[i] = readInt(); x[a[i]]++; } for(int i=0; i<countX; ++i) if(x[i] % 2 == 1) { out.println(-1); return; } for(int i=0; i<2*n; ++i) { if(prev[a[i]] == -1) prev[a[i]] = i + 1; else { out.println(prev[a[i]] + " " + (i + 1)); prev[a[i]] = -1; } } } }
Java
["3\n20 30 10 30 20 10", "1\n1 2"]
1 second
["4 2\n1 5\n6 3", "-1"]
null
Java 7
input.txt
[ "constructive algorithms", "sortings" ]
0352429425782d7fe44818594eb0660c
The first line contains integer n (1 ≀ n ≀ 3Β·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≀ ai ≀ 5000) β€” the numbers that are written on the cards. The numbers on the line are separated by single spaces.
1,200
If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line β€” the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.
output.txt
PASSED
be370badacdd5803ebb9d9f053519e99
train_004.jsonl
1355047200
Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.
256 megabytes
import java.awt.Point; import java.io.*; import java.math.*; import java.util.*; import java.util.Map.Entry; import static java.lang.Math.*; /** * Codeforces Round 155 (Div 2) * @author dalex * Actual solution is at the bottom */ public class A implements Runnable { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok; public static void main(String[] args) { new Thread(null, new A(), "", 128 * (1L << 20)).start(); } public void run() { try { long startTime = System.currentTimeMillis(); Locale.setDefault(Locale.US); // if (ONLINE_JUDGE) { // in = new BufferedReader(new InputStreamReader(System.in)); // out = new PrintWriter(System.out); // } else { // in = new BufferedReader(new FileReader("input.txt")); // out = new PrintWriter("output.txt"); // } in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); tok = new StringTokenizer(""); solve(); in.close(); out.close(); long freeMemory = Runtime.getRuntime().freeMemory(); long totalMemory = Runtime.getRuntime().totalMemory(); long endTime = System.currentTimeMillis(); System.err.printf("Time = %.3f ms\n", (endTime - startTime) / 1000.0); System.err.printf("Memory = %.3f MB\n", (totalMemory - freeMemory) / (double) (1 << 20)); } catch (Throwable t) { t.printStackTrace(System.err); System.exit(-1); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { String line = in.readLine(); if (line == null) return null; tok = new StringTokenizer(line); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } int[] readIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = readInt(); } return a; } long[] readLongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = readLong(); } return a; } double[] readDoubleArray(int n) throws IOException { double[] a = new double[n]; for (int i = 0; i < n; i++) { a[i] = readDouble(); } return a; } int[][] readIntMatrix(int n, int m) throws IOException { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = readInt(); } } return a; } long[][] readLongMatrix(int n, int m) throws IOException { long[][] a = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = readLong(); } } return a; } double[][] readDoubleMatrix(int n, int m) throws IOException { double[][] a = new double[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = readDouble(); } } return a; } void debug(Object... o) { if (!ONLINE_JUDGE) { System.err.println(Arrays.deepToString(o)); } } static class Mergesort { private Mergesort() {} public static void sort(int[] a) { mergesort(a, 0, a.length - 1); } public static void sort(long[] a) { mergesort(a, 0, a.length - 1); } public static void sort(double[] a) { mergesort(a, 0, a.length - 1); } private static final int MAGIC_VALUE = 50; private static void mergesort(int[] a, int leftIndex, int rightIndex) { if (leftIndex < rightIndex) { if (rightIndex - leftIndex <= MAGIC_VALUE) { insertionSort(a, leftIndex, rightIndex); } else { int middleIndex = (leftIndex + rightIndex) >> 1; mergesort(a, leftIndex, middleIndex); mergesort(a, middleIndex + 1, rightIndex); merge(a, leftIndex, middleIndex, rightIndex); } } } private static void mergesort(long[] a, int leftIndex, int rightIndex) { if (leftIndex < rightIndex) { if (rightIndex - leftIndex <= MAGIC_VALUE) { insertionSort(a, leftIndex, rightIndex); } else { int middleIndex = (leftIndex + rightIndex) >> 1; mergesort(a, leftIndex, middleIndex); mergesort(a, middleIndex + 1, rightIndex); merge(a, leftIndex, middleIndex, rightIndex); } } } private static void mergesort(double[] a, int leftIndex, int rightIndex) { if (leftIndex < rightIndex) { if (rightIndex - leftIndex <= MAGIC_VALUE) { insertionSort(a, leftIndex, rightIndex); } else { int middleIndex = (leftIndex + rightIndex) >> 1; mergesort(a, leftIndex, middleIndex); mergesort(a, middleIndex + 1, rightIndex); merge(a, leftIndex, middleIndex, rightIndex); } } } private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - leftIndex + 1; int length2 = rightIndex - middleIndex; int[] leftArray = new int[length1]; int[] rightArray = new int[length2]; System.arraycopy(a, leftIndex, leftArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = leftArray[i++]; } else { a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++]; } } } private static void merge(long[] a, int leftIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - leftIndex + 1; int length2 = rightIndex - middleIndex; long[] leftArray = new long[length1]; long[] rightArray = new long[length2]; System.arraycopy(a, leftIndex, leftArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = leftArray[i++]; } else { a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++]; } } } private static void merge(double[] a, int leftIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - leftIndex + 1; int length2 = rightIndex - middleIndex; double[] leftArray = new double[length1]; double[] rightArray = new double[length2]; System.arraycopy(a, leftIndex, leftArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = leftArray[i++]; } else { a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++]; } } } private static void insertionSort(int[] a, int leftIndex, int rightIndex) { for (int i = leftIndex + 1; i <= rightIndex; i++) { int current = a[i]; int j = i - 1; while (j >= leftIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } private static void insertionSort(long[] a, int leftIndex, int rightIndex) { for (int i = leftIndex + 1; i <= rightIndex; i++) { long current = a[i]; int j = i - 1; while (j >= leftIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } private static void insertionSort(double[] a, int leftIndex, int rightIndex) { for (int i = leftIndex + 1; i <= rightIndex; i++) { double current = a[i]; int j = i - 1; while (j >= leftIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } } static class Complex { public double real; public double imag; public Complex(double real) { this.real = real; this.imag = 0; } public Complex(double real, double imag) { this.real = real; this.imag = imag; } public Complex add(Complex other) { return new Complex(this.real + other.real, this.imag + other.imag); } public Complex subtract(Complex other) { return new Complex(this.real - other.real, this.imag - other.imag); } public Complex multiply(Complex other) { return new Complex(this.real * other.real - this.imag * other.imag, this.real * other.imag + other.real * this.imag); } public void multiply(double alpha) { real *= alpha; imag *= alpha; } public void divide(double alpha) { real /= alpha; imag /= alpha; } public double angle() { return atan2(imag, real); } public double length() { return sqrt(real * real + imag * imag); } public void normalize() { double length = length(); if (length == 0) return; real /= length; imag /= length; } @Override public int hashCode() { final int prime = 31; int result = 1; long temp; temp = Double.doubleToLongBits(imag); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(real); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Complex other = (Complex) obj; if (Double.doubleToLongBits(imag) != Double.doubleToLongBits(other.imag)) return false; if (Double.doubleToLongBits(real) != Double.doubleToLongBits(other.real)) return false; return true; } @Override public String toString() { char sign = signum(imag) < 0 ? '-' : '+'; return String.format("%.8f %c %.8f*i", real, sign, abs(imag)); } } static class Pair<F extends Comparable<F>, S extends Comparable<S>> implements Comparable<Pair<F, S>> { public F first; public S second; public Pair(F first, S second) { this.first = first; this.second = second; } public int compareTo(Pair<F, S> other) { int firstCompare = this.first.compareTo(other.first); if (firstCompare != 0) { return firstCompare; } return this.second.compareTo(other.second); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((first == null) ? 0 : first.hashCode()); result = prime * result + ((second == null) ? 0 : second.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Pair<F, S> other = (Pair<F, S>) obj; if (first == null) { if (other.first != null) return false; } else if (!first.equals(other.first)) return false; if (second == null) { if (other.second != null) return false; } else if (!second.equals(other.second)) return false; return true; } @Override public String toString() { return String.format("(%s %s)", first == null ? "null" : first.toString(), second == null ? "null" : second.toString()); } } class Card implements Comparable<Card> { int x, i; public Card(int x, int i) { this.x = x; this.i = i; } @Override public int compareTo(Card o) { return x - o.x; } } void solve() throws IOException { int n = readInt() * 2; Card[] a = new Card[n]; for (int i = 0; i < n; i++) { a[i] = new Card(readInt(), i); } Arrays.sort(a); for (int i = 0; i < n; i += 2) { if (a[i].x != a[i+1].x) { out.println(-1); return; } } for (int i = 0; i < n; i += 2) { out.println((a[i].i + 1) + " " + (a[i+1].i + 1)); } } }
Java
["3\n20 30 10 30 20 10", "1\n1 2"]
1 second
["4 2\n1 5\n6 3", "-1"]
null
Java 7
input.txt
[ "constructive algorithms", "sortings" ]
0352429425782d7fe44818594eb0660c
The first line contains integer n (1 ≀ n ≀ 3Β·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≀ ai ≀ 5000) β€” the numbers that are written on the cards. The numbers on the line are separated by single spaces.
1,200
If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line β€” the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.
output.txt
PASSED
77cbd00ebece58ae3f1ac9c1f543ab32
train_004.jsonl
1355047200
Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; public class Div2A { private static final int maxn = 50005; private static final int a[] = new int[maxn]; public static void main(String[] args) throws Exception { String name = "input.txt"; BufferedReader br = new BufferedReader(new FileReader(name)); int n = Integer.parseInt(br.readLine()); String aux[] = br.readLine().split(" "); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 2 * n; i++) { int pos = Integer.parseInt(aux[i]); if (a[pos] == 0) { a[pos] = i + 1; } else { sb.append( (i + 1) + " " + a[pos] + "\n"); a[pos] = 0; } } for (int i = 0; i < maxn; i++) { if (a[i] != 0) { sb = new StringBuilder("-1\n"); break; } } FileWriter fr = new FileWriter("output.txt"); fr.write(sb.toString()); fr.close(); br.close(); } }
Java
["3\n20 30 10 30 20 10", "1\n1 2"]
1 second
["4 2\n1 5\n6 3", "-1"]
null
Java 7
input.txt
[ "constructive algorithms", "sortings" ]
0352429425782d7fe44818594eb0660c
The first line contains integer n (1 ≀ n ≀ 3Β·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≀ ai ≀ 5000) β€” the numbers that are written on the cards. The numbers on the line are separated by single spaces.
1,200
If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line β€” the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.
output.txt
PASSED
2d665865362f48c626d69fb8c8e0d803
train_004.jsonl
1355047200
Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; /** * * @author sukhdeep */ public class c { public static void main(String arg[]) throws IOException { BufferedReader r=new BufferedReader(new FileReader("input.txt")); BufferedWriter write=new BufferedWriter(new FileWriter("output.txt")); int n=Integer.parseInt(r.readLine()); n=2*n; int a[][]=new int[n][2]; StringTokenizer str=new StringTokenizer(r.readLine()); for(int i=0;i<n;i++){ a[i][0]=Integer.parseInt(str.nextToken()); a[i][1]=i+1; } Arrays.sort(a, new Comparator<int[]>() { public int compare(int[] aa, int[] b) { if(aa[0] != b[0])return (aa[0] - b[0]); return -(aa[1] - b[1]); } }); StringBuilder s=new StringBuilder(""); int flag=0; for(int i=0;i<n;i++) { if(a[i][0]==a[i+1][0]) { s.append(a[i][1]); s.append(" "); s.append(a[i+1][1]);s.append("\n"); i++; }else { flag=1; break; } } if(flag==1) { write.append("-1"); } else { write.append(s); } write.flush(); write.close(); } }
Java
["3\n20 30 10 30 20 10", "1\n1 2"]
1 second
["4 2\n1 5\n6 3", "-1"]
null
Java 7
input.txt
[ "constructive algorithms", "sortings" ]
0352429425782d7fe44818594eb0660c
The first line contains integer n (1 ≀ n ≀ 3Β·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≀ ai ≀ 5000) β€” the numbers that are written on the cards. The numbers on the line are separated by single spaces.
1,200
If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line β€” the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.
output.txt
PASSED
6b19a25e8618a4a2b109991fe9a249e7
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.LinkedList; public class TheMonsterC459 { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); String s=in.readLine(); char[] chars=new char[s.length()]; for(int i=0;i<s.length();i++) chars[i]=s.charAt(i); int tot=0,l,r; for(int i=0;i<chars.length;i++) { l=0;r=0; for(int j=i;j<chars.length;j++) { if(chars[j]==')') { l--; r--; } else if(chars[j]=='(') { l++; r++; } else { l--; r++; } if(r<0)break; if(l<0)l+=2; if(l==0)tot++; } } out.println(tot); in.close(); out.close(); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
cddc5fdbd5720bb65e655d497ded639f
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.*; import java.util.*; public class CODEFORCES { @SuppressWarnings("rawtypes") static InputReader in; static PrintWriter out; static void solve() { String a = in.nextLine(); long ans = 0; for (int i = 0; i < a.length(); i++) { int x = 0, z = 0, zy = 0; for (int j = i; j < a.length(); j++) { if (a.charAt(j) == '(') x++; else if (a.charAt(j) == ')') x--; else { if (x > 0) zy++; else z++; } if (x < 0) { x++; if (zy > 0) { zy--; z++; } else if (z > 0) z--; else break; } if (x < zy) { zy--; z++; } //debug(x + " " + zy + " " + z); if (x == zy && z % 2 == 0) { ans++; //debug(i + " " + j); } } //debug("RP"); } out.println(ans); } @SuppressWarnings("rawtypes") static void soln() { in = new InputReader(System.in); out = new PrintWriter(System.out); solve(); out.flush(); } static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { try { soln(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } // To Get Input // Some Buffer Methods static class InputReader<SpaceCharFilter> { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(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; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
4308e162f769cc721d73f702a919e9fd
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.flush();out.close(); } static class TaskE { class pair{ String n,id;pair(String s1,String s2){ n=s1;id=s2; } } public void solve(int testNumber, InputReader in, PrintWriter out) { String s=in.next().trim(); int l=s.length();int ans=0; for(int i=0;i<l;i++){ int nl=0,nr=0,nq=0,sq=0; for(int j=i;j<l;j++){ if(s.charAt(j)=='(')nr++; else if(s.charAt(j)==')'){ if(nr!=0)--nr; else if(nq!=0)--nq; else{ if(sq==0)++nl; else{ --sq;++nr; } } } else{ if(nr!=0){ --nr;++sq; } else if(nq!=0){ --nq;++sq; } else nq++; } if(nr==0&&nl==0&&nq==0){++ans; } } } out.print(ans); } } static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); st = null; } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
84078ae30381aa2a34c898ea6a4fcc08
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class MainC { static int mod = (int) (1e9+7); static InputReader in; static PrintWriter out; static String s; public static void main(String[] args) { in = new InputReader(System.in); out = new PrintWriter(System.out); s = in.readString(); int n = s.length(); int cnt = 0; for(int j = 0; j < n; j++){ int open = 0; int tmp = 0; for(int i = j; i < n; i++){ if(s.charAt(i) == '(') open++; else if(s.charAt(i) == ')') open--; else{ open--; tmp++; } if(open < 0){ open += 2; tmp--; } if(tmp < 0) break; if(open == 0) cnt++; } } out.println(cnt); out.close(); } static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static class Pair implements Comparable<Pair> { int x,y; int i; Pair (int x,int y) { this.x = x; this.y = y; } Pair (int x,int y, int i) { this.x = x; this.y = y; this.i = i; } public int compareTo(Pair o) { if(this.x != o.x) return -Integer.compare(this.x, o.x); return -Integer.compare(this.y,o.y); //return 0; } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair)o; return p.x == x && p.y==y; } return false; } @Override public String toString() { return x + " "+ y + " "+i; } /*public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); }*/ } static long add(long a,long b){ long x=(a+b); while(x>=mod) x-=mod; return x; } static long sub(long a,long b){ long x=(a-b); while(x<0) x+=mod; return x; } static long mul(long a,long b){ long x=(a*b); while(x>=mod) x-=mod; return x; } static String rev(String s){ StringBuilder sb=new StringBuilder(s); sb.reverse(); return sb.toString(); } static long gcd(long x,long y) { if(y==0) return x; else return gcd(y,x%y); } static int gcd(int x,int y) { if(y==0) return x; else return gcd(y,x%y); } static long pow(long n,long p,long m) { long result = 1; if(p==0) return 1; while(p!=0) { if(p%2==1) result *= n; if(result>=m) result%=m; p >>=1; n*=n; if(n>=m) n%=m; } return result; } static long pow(long n,long p) { long result = 1; if(p==0) return 1; while(p!=0) { if(p%2==1) result *= n; p >>=1; n*=n; } return result; } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(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; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
f48264476245d0f58e42bbda8b8bcc33
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.*; import java.util.*; public class Code1206 { public static void main(String args[]){ Reader s = new Reader(); char ch[] = s.next().toCharArray(); int n = ch.length; int ans = 0; for (int i = 0; i < n-1 ; i++) { int start = 0; int que = 0; for (int j = i; j < n ; j++) { if(j==i){ if(ch[j] == '?' || ch[j] == '('){ start++; } else{ break; } } else{ if(ch[j] == '('){ start++; } else if(ch[j] == '?'){ que++; } else{ start--; } if(start<0){ break; } if(start < que){ que--; start++; } if((j-i+1)%2 ==0 && start == que){ ans++; } } } } System.out.println(ans); } static class Reader { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public Reader() { this(System.in); } public Reader(InputStream is) { mIs = is; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = mIs.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 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(); } double nextDouble() { return Double.parseDouble(next()); } 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 boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
fa599b569cd3909f0bde808cae089bd5
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * @author Don Li */ public class TheMonster3 { void solve() { char[] s = in.nextToken().toCharArray(); int n = s.length; boolean[][] f = new boolean[n][n]; for (int i = 0; i < n; i++) { int cnt = 0; for (int j = i; j < n; j++) { if (s[j] == ')') cnt--; else cnt++; if (cnt < 0) break; f[i][j] = true; } } boolean[][] g = new boolean[n][n]; for (int j = 0; j < n; j++) { int cnt = 0; for (int i = j; i >= 0; i--) { if (s[i] == '(') cnt--; else cnt++; if (cnt < 0) break; g[i][j] = true; } } int ans = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if ((j - i + 1) % 2 == 0 && f[i][j] && g[i][j]) { ans++; } } } out.println(ans); } public static void main(String[] args) { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); new TheMonster3().solve(); out.close(); } static FastScanner in; static PrintWriter out; static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
7ac81bcf4dba217219c27a51fa22a4a4
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; /** * @author Don Li */ public class TheMonster { int N = (int) 5e3 + 10; boolean[][] ok = new boolean[N][N]; void solve() { char[] s = in.nextToken().toCharArray(); int n = s.length; List<Integer>[] prev = new List[n]; for (int i = 0; i < n; i++) prev[i] = new ArrayList<>(); for (int i = 1; i < n; i++) { for (int j = i - 1; j >= 0; j -= 2) { if (ok[j][i]) continue; if (pair(s[j], s[i]) && correct(j + 1, i - 1)) { ok[j][i] = true; if (j - 1 >= 0) for (int k : prev[j - 1]) ok[k][i] = true; } } for (int j = 0; j < i; j++) if (ok[j][i]) prev[i].add(j); } int ans = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (ok[i][j]) ans++; } } out.println(ans); } boolean correct(int i, int j) { return i > j || ok[i][j]; } boolean pair(char a, char b) { return a == '(' && (b == ')' || b == '?') || a == '?' && (b == ')' || b == '?'); } public static void main(String[] args) { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); new TheMonster().solve(); out.close(); } static FastScanner in; static PrintWriter out; static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
217fd06f5cab230f7631be2b05f3b4da
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * @author Don Li */ public class TheMonster2 { void solve() { char[] s = in.nextToken().toCharArray(); int n = s.length; int ans = 0; for (int i = 0; i < n; i++) { int open = 0, qmark = 0; for (int j = i; j < n; j++) { if (s[j] == '(') open++; else if (s[j] == ')') open--; else qmark++; if (qmark > 0 && qmark > open) { qmark--; open++; } if (open < 0) break; if ((j - i + 1) % 2 == 0 && qmark >= open) ans++; } } out.println(ans); } public static void main(String[] args) { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); new TheMonster2().solve(); out.close(); } static FastScanner in; static PrintWriter out; static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
2900ca73c6fc91a647961cf27098ff5d
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
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 */ 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 in, PrintWriter out) { String s = in.nextLine().trim(); int n = s.length(); int ans = 0; for (int i = 0; i < n; i++) { // System.out.println("Substring at "+i); int score = 0; int q = 0; // int tans=0; for (int j = i; j < n; j++) { // System.out.println("Char "+s.charAt(j)); if (s.charAt(j) == '(') { score++; } else if (s.charAt(j) == ')') { score--; } else { q++; } // System.out.println(j+" qmarks "+q+" score "+score); if (score < 0) { // tans=0; break; } if (q > score) { q--; score++; } // System.out.println(j+" qmarks "+q+" score "+score); if ((q + score) % 2 == 0) { if (q >= score) { // tans++; ans++; // System.out.println("Tans "+tans); } } } // System.out.println(i+" "+tans); // ans+=tans; } // out.println(ans); out.println(ans); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
05e6053ef43bf6ab70bd66d635d2eb61
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String[] args) { InputReader in = new InputReader(); PrintWriter out = new PrintWriter(System.out); Thread th = new Thread(null, new Runnable(){public void run(){new Task1().solve(in, out);}},"Task1",1<<24); try{ th.start(); th.join(); } catch(InterruptedException e){} out.close(); } static class Task1{ static final long MAX = Long.MAX_VALUE; public void solve(InputReader in, PrintWriter out){ String ques = in.next(); long ans = 0; for(int start=0; start<ques.length(); start++){ long open=0, close=0, stock=0, curr_open = 0; boolean valid = true, curr_valid = true; for(int end=start; valid && end<ques.length(); end++){ if(ques.charAt(end)=='('){ open++; curr_open++; } else if(ques.charAt(end)==')'){ close++; curr_open--; } else if(ques.charAt(end)=='?'){ stock++; curr_open--; } if(curr_open>0) curr_valid = false; if(curr_open<=0){ curr_open = 0; curr_valid = true; } if(close>open+stock){ valid = false; } else if(valid && ((open+close+stock)%2==0) && (open+stock>=close) && (close+stock>=open) && curr_valid){ //out.println(ques.substring(start, end+1)+"\t"+open+" "+close+" "+stock); ans++; } //out.println(ans); } } out.println(ans); } long gcd(long a, long b){ while(a>0){ long rem = b%a; b = a; a = rem; } return b; } long expo(long a, long b, long MOD){ long result = 1; while (b>0){ if (b%2==1) result=(result*a)%MOD; b>>=1; a=(a*a)%MOD; } return result%MOD; } long inverseModullo(long numerator, long denominator, long MOD){ return ((numerator )*(expo(denominator, MOD-2, MOD))) ; } } static class InputReader{ final InputStream stream; final byte[] buf = new byte[8192]; int curChar, numChars; SpaceCharFilter filter; public InputReader(){ this.stream = System.in; } 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 long nextLong(){ int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if(c == '-'){ sgn = -1; c = read(); } long res = 0; do{ if(c<'0' || c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while(!isSpaceChar(c)); return res*sgn; } public String 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
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
f115d3f787906c5698ad04e46e85c824
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.*; public class C459Div2 { public static void main(String[] args) { new C459Div2(System.in, System.out); } static class Solver implements Runnable { int n, start; char[] s; BufferedReader in; PrintWriter out; void solve() throws IOException { s = in.readLine().toCharArray(); n = s.length; int ans = 0; for (int i = 0; i < n; i++) { int cnt = 0; int qsns = 0; for (int j = i; j < n; j++) { if (s[j] == '(') cnt++; else if (s[j] == ')') cnt--; else qsns++; if (cnt < 0) break; if (qsns > cnt) { qsns--; cnt++; } if ((j - i + 1) % 2 == 0 && cnt >= 0 && qsns >= cnt) ans++; } } out.println(ans); } public Solver(BufferedReader in, PrintWriter out) { this.in = in; this.out = out; } @Override public void run() { try { solve(); } catch (IOException e) { e.printStackTrace(); } } } private C459Div2(InputStream inputStream, OutputStream outputStream) { BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); PrintWriter out = new PrintWriter(outputStream); Thread thread = new Thread(null, new Solver(in, out), "C459Div2", 1 << 29); try { thread.start(); thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } out.flush(); out.close(); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
c2b72eeb4b4ff751f5d9cdf91a98bda5
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.*; public class C459Div2 { public static void main(String[] args) { new C459Div2(System.in, System.out); } static class Solver implements Runnable { int n, start; char[] s; BufferedReader in; PrintWriter out; void solve() throws IOException { s = in.readLine().toCharArray(); n = s.length; int ans = 0; for (int i = 0; i < n; i++) { int cnt = 0; int qsns = 0; for (int j = i; j < n; j++) { if (s[j] == '(') cnt++; else if (s[j] == ')') { if (cnt > 0) cnt--; else break; } else qsns++; if (cnt < 0) break; if (qsns > cnt) { // System.out.println("****[" + i + ", " + j + "], qsn : " + qsns + ", cnt : " + cnt); qsns--; cnt++; } if ((j - i + 1) % 2 == 0 && cnt >= 0 && qsns >= cnt) { // System.out.println("[" + i + ", " + j + "], qsn : " + qsns + ", cnt : " + cnt); ans++; } } } out.println(ans); } public Solver(BufferedReader in, PrintWriter out) { this.in = in; this.out = out; } @Override public void run() { try { solve(); } catch (IOException e) { e.printStackTrace(); } } } private C459Div2(InputStream inputStream, OutputStream outputStream) { BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); PrintWriter out = new PrintWriter(outputStream); Thread thread = new Thread(null, new Solver(in, out), "C459Div2", 1 << 29); try { thread.start(); thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } out.flush(); out.close(); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
ed49f9e3fecfc91ab380481826684471
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author rizhiy */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { char[] ch = in.next().toCharArray(); int n = ch.length; int[][] m = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { m[i][j] = ((j - i + 1) % 2 == 0 ? 1 : 0); } } for (int a = 0; a < n; ++a) { int sum = 0; for (int b = a; b < n; ++b) { if (ch[b] == ')') --sum; else ++sum; if (sum < 0) { m[a][b] = 0; for (int i = b + 1; i < n; ++i) m[a][i] = 0; break; } } } for (int a = n - 1; a >= 0; --a) { int sum = 0; for (int b = a; b >= 0; --b) { if (ch[b] == '(') --sum; else ++sum; if (sum < 0) { m[b][a] = 0; for (int i = b - 1; i >= 0; --i) m[i][a] = 0; break; } } } int answ = 0; for (int i = 0; i < n; i++) for (int j = i; j < n; j++) answ += m[i][j]; out.println(answ); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
fca571ac1023fdebc47069f18d7c0352
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.*; public class _918C3 { static char s[]; static int len; public static void main(String[] args) { Scanner in = new Scanner(System.in); s = in.next().toCharArray(); len = s.length; int res =0 ; for(int i = 0; i < len; i++) { // System.out.println("Start: "+i); int c = 0; int clo = 0; int ll=0; for(int j = i; j < len; j++) { //System.out.print("("+c+","+clo+","+ll+") -> "); ll++; if(s[j] == '(') { c++; } else if(s[j] == ')') { c--; } else if(s[j] == '?') { clo++; c--; } if(c < 0) { while(c < 0) { clo--; c+=2; } if(clo < 0) break; } //System.out.println("("+c+","+clo+","+ll+")"); //if(ll%2==1)continue; if(c==0) res++; } } System.out.println(res); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
45211dc464d39ca17c5af89597ce727a
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.*; import java.util.*; public class Solution{ InputStream is; PrintWriter out; String INPUT = ""; public void solve(){ char[] a=ns().toCharArray(); int n=a.length; boolean[][] memo=new boolean[n][n]; for(int i=0; i<n; i++){ if(a[i]==')')continue; int left=0, normal=0, right=0; for(int j=i; j<n; j++){ if(a[j]=='?')normal+=1; if(a[j]==')'){right+=1;} if(a[j]=='('){left+=1;continue;} //out.println(i+" "+j+" "+left+" "+normal+" "+right); if(left+normal<right)break; if((j-i+1)%2==0 && Math.abs(left-right)<=normal)memo[i][j]=true; } } for(int i=n-1; i>=0; i--){ if(a[i]=='(')continue; int left=0, normal=0, right=0; for(int j=i; j>=0; j--){ if(a[j]=='?')normal+=1; if(a[j]=='('){left+=1;} if(a[j]==')'){right+=1;continue;} // out.println(i+" "+j+" "+left+" "+normal+" "+right); if(right+normal<left){ while(j>=0){ memo[j][i]=false; j--; } break; } if((j-i+1)%2==0 && Math.abs(right-left)>normal && memo[j][i])memo[j][i]=false; } } int ans=0; for(int i=0; i<n; i++){ for(int j=i; j<n; j++){ //out.print(memo[i][j]?"1 ":"0 "); if(memo[i][j])ans+=1; } //out.println(); } out.println(ans); } void run(){ is = new DataInputStream(System.in); out = new PrintWriter(System.out); int t=1;while(t-->0)solve(); out.flush(); } public static void main(String[] args)throws Exception{new Solution().run();} //Fast I/O code is copied from uwi code. private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte(){ if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns(){ int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n){ char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m){ char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n){ int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni(){ int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl(){ long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } static int i(long x){return (int)Math.round(x);} static class Pair implements Comparable<Pair>{ long fs,sc; Pair(long a,long b){ fs=a;sc=b; } public int compareTo(Pair p){ if(this.fs>p.fs)return 1; else if(this.fs<p.fs)return -1; else{ return i(this.sc-p.sc); } //return i(this.sc-p.sc); } public String toString(){ return "("+fs+","+sc+")"; } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
2ad3b8c845c894ec782ff503ba8a2a19
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { String str = in.readLine(); int n = str.length(); boolean[][] f = new boolean[n][n]; boolean[][] g = new boolean[n][n]; for (int i = 0; i < n; i++) { int open = 0, close = 0; for (int j = i; j < n; j++) { if (str.charAt(j) != ')') open++; else close++; if (open >= close) f[i][j] = true; else break; } open = 0; close = 0; for (int j = i; j >= 0; j--) { if (str.charAt(j) != '(') close++; else open++; if (close >= open) g[j][i] = true; else break; } } int ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j += 2) { if (f[i][j] && g[i][j]) ans++; } } out.println(ans); } } 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 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++]; } private String readLine0() { StringBuilder buf = new StringBuilder(); 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; } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
46ee594de069695015fc98c113b7bbc2
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class code7 { InputStream is; PrintWriter out; static long mod=pow(10,9)+7; int dx[]= {0,0,1,-1},dy[]={+1,-1,0,0}; char[][] grid; void solve() { String s=ns(); int count=0; for(int i=0;i<s.length();i++) { int open=0,que=0,close=0; for(int j=i;j<s.length();j++) { if(s.charAt(j)=='(') open++; else if(s.charAt(j)=='?') que++; else open--; if(open<0) break; if(que>open) { que--; open++; } if((j-i+1)%2==0&&que==open) count++; } } System.out.println(count); } ArrayList<Integer>al []; void take(int n,int m) { al=new ArrayList[n]; for(int i=0;i<n;i++) al[i]=new ArrayList<Integer>(); for(int i=0;i<m;i++) { int x=ni()-1; int y=ni()-1; al[x].add(y); al[y].add(x); } } int arr[][]; int small[]; void pre(int n) { small=new int[n+1]; for(int i=2;i*i<=n;i++) { for(int j=i;j*i<=n;j++) { if(small[i*j]==0) small[i*j]=i; } } for(int i=0;i<=n;i++) { if(small[i]==0) small[i]=i; } } public static int count(int x) { int num=0; while(x!=0) { x=x&(x-1); num++; } return num; } static long d, x, y; void extendedEuclid(long A, long B) { if(B == 0) { d = A; x = 1; y = 0; } else { extendedEuclid(B, A%B); long temp = x; x = y; y = temp - (A/B)*y; } } long modInverse(long A,long M) //A and M are coprime { extendedEuclid(A,M); return (x%M+M)%M; //x may be negative } public static void mergeSort(int[] arr, int l ,int r){ if((r-l)>=1){ int mid = (l+r)/2; mergeSort(arr,l,mid); mergeSort(arr,mid+1,r); merge(arr,l,r,mid); } } public static void merge(int arr[], int l, int r, int mid){ int n1 = (mid-l+1), n2 = (r-mid); int left[] = new int[n1]; int right[] = new int[n2]; for(int i =0 ;i<n1;i++) left[i] = arr[l+i]; for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i]; int i =0, j =0, k = l; while(i<n1 && j<n2){ if(left[i]>right[j]){ arr[k++] = right[j++]; } else{ arr[k++] = left[i++]; } } while(i<n1) arr[k++] = left[i++]; while(j<n2) arr[k++] = right[j++]; } public static void mergeSort(long[] arr, int l ,int r){ if((r-l)>=1){ int mid = (l+r)/2; mergeSort(arr,l,mid); mergeSort(arr,mid+1,r); merge(arr,l,r,mid); } } public static void merge(long arr[], int l, int r, int mid){ int n1 = (mid-l+1), n2 = (r-mid); long left[] = new long[n1]; long right[] = new long[n2]; for(int i =0 ;i<n1;i++) left[i] = arr[l+i]; for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i]; int i =0, j =0, k = l; while(i<n1 && j<n2){ if(left[i]>right[j]){ arr[k++] = right[j++]; } else{ arr[k++] = left[i++]; } } while(i<n1) arr[k++] = left[i++]; while(j<n2) arr[k++] = right[j++]; } static class Pair implements Comparable<Pair>{ int x; int y,k,i; Pair (int x,int y){ this.x=x; this.y=y; } public int compareTo(Pair o) { if(this.x!=o.x) return Long.compare(this.x,o.x); return this.y-o.y; } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair)o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode()*31 + new Long(y).hashCode() ; } @Override public String toString() { return "("+x + " " + y +" "+k+" "+i+" )"; } } public static boolean isPal(String s){ for(int i=0, j=s.length()-1;i<=j;i++,j--){ if(s.charAt(i)!=s.charAt(j)) return false; } return true; } public static String rev(String s){ StringBuilder sb=new StringBuilder(s); sb.reverse(); return sb.toString(); } public static long gcd(long x,long y){ if(x%y==0) return y; else return gcd(y,x%y); } public static int gcd(int x,int y){ if(y==0) return x; return gcd(y,x%y); } public static long gcdExtended(long a,long b,long[] x){ if(a==0){ x[0]=0; x[1]=1; return b; } long[] y=new long[2]; long gcd=gcdExtended(b%a, a, y); x[0]=y[1]-(b/a)*y[0]; x[1]=y[0]; return gcd; } public static int abs(int a,int b){ return (int)Math.abs(a-b); } public static long abs(long a,long b){ return (long)Math.abs(a-b); } public static int max(int a,int b){ if(a>b) return a; else return b; } public static int min(int a,int b){ if(a>b) return b; else return a; } public static long max(long a,long b){ if(a>b) return a; else return b; } public static long min(long a,long b){ if(a>b) return b; else return a; } public static long pow(long n,long p,long m){ long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; if(result>=m) result%=m; p >>=1; n*=n; if(n>=m) n%=m; } return result; } public static long pow(long n,long p){ long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; p >>=1; n*=n; } return result; } public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } void run() throws Exception { is = System.in; out = new PrintWriter(System.out); solve(); out.flush(); } public static void main(String[] args) throws Exception { new Thread(null, new Runnable() { public void run() { try { new code7().run(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
c517d0f4582992c2fcf1fa505c1b0c34
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class code7 { InputStream is; PrintWriter out; static long mod=pow(10,9)+7; int dx[]= {0,0,1,-1},dy[]={+1,-1,0,0}; char[][] grid; void solve() { String s=ns(); int count=0; for(int i=0;i<s.length();i++) { int open=0,que=0,close=0; for(int j=i;j<s.length();j++) { if(s.charAt(j)=='(') open++; else if(s.charAt(j)=='?') que++; else open--; if(open<0) break; if(que>open) { que--; open++; } if((j-i+1)%2==0&&que>=open) count++; } } System.out.println(count); } ArrayList<Integer>al []; void take(int n,int m) { al=new ArrayList[n]; for(int i=0;i<n;i++) al[i]=new ArrayList<Integer>(); for(int i=0;i<m;i++) { int x=ni()-1; int y=ni()-1; al[x].add(y); al[y].add(x); } } int arr[][]; int small[]; void pre(int n) { small=new int[n+1]; for(int i=2;i*i<=n;i++) { for(int j=i;j*i<=n;j++) { if(small[i*j]==0) small[i*j]=i; } } for(int i=0;i<=n;i++) { if(small[i]==0) small[i]=i; } } public static int count(int x) { int num=0; while(x!=0) { x=x&(x-1); num++; } return num; } static long d, x, y; void extendedEuclid(long A, long B) { if(B == 0) { d = A; x = 1; y = 0; } else { extendedEuclid(B, A%B); long temp = x; x = y; y = temp - (A/B)*y; } } long modInverse(long A,long M) //A and M are coprime { extendedEuclid(A,M); return (x%M+M)%M; //x may be negative } public static void mergeSort(int[] arr, int l ,int r){ if((r-l)>=1){ int mid = (l+r)/2; mergeSort(arr,l,mid); mergeSort(arr,mid+1,r); merge(arr,l,r,mid); } } public static void merge(int arr[], int l, int r, int mid){ int n1 = (mid-l+1), n2 = (r-mid); int left[] = new int[n1]; int right[] = new int[n2]; for(int i =0 ;i<n1;i++) left[i] = arr[l+i]; for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i]; int i =0, j =0, k = l; while(i<n1 && j<n2){ if(left[i]>right[j]){ arr[k++] = right[j++]; } else{ arr[k++] = left[i++]; } } while(i<n1) arr[k++] = left[i++]; while(j<n2) arr[k++] = right[j++]; } public static void mergeSort(long[] arr, int l ,int r){ if((r-l)>=1){ int mid = (l+r)/2; mergeSort(arr,l,mid); mergeSort(arr,mid+1,r); merge(arr,l,r,mid); } } public static void merge(long arr[], int l, int r, int mid){ int n1 = (mid-l+1), n2 = (r-mid); long left[] = new long[n1]; long right[] = new long[n2]; for(int i =0 ;i<n1;i++) left[i] = arr[l+i]; for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i]; int i =0, j =0, k = l; while(i<n1 && j<n2){ if(left[i]>right[j]){ arr[k++] = right[j++]; } else{ arr[k++] = left[i++]; } } while(i<n1) arr[k++] = left[i++]; while(j<n2) arr[k++] = right[j++]; } static class Pair implements Comparable<Pair>{ int x; int y,k,i; Pair (int x,int y){ this.x=x; this.y=y; } public int compareTo(Pair o) { if(this.x!=o.x) return Long.compare(this.x,o.x); return this.y-o.y; } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair)o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode()*31 + new Long(y).hashCode() ; } @Override public String toString() { return "("+x + " " + y +" "+k+" "+i+" )"; } } public static boolean isPal(String s){ for(int i=0, j=s.length()-1;i<=j;i++,j--){ if(s.charAt(i)!=s.charAt(j)) return false; } return true; } public static String rev(String s){ StringBuilder sb=new StringBuilder(s); sb.reverse(); return sb.toString(); } public static long gcd(long x,long y){ if(x%y==0) return y; else return gcd(y,x%y); } public static int gcd(int x,int y){ if(y==0) return x; return gcd(y,x%y); } public static long gcdExtended(long a,long b,long[] x){ if(a==0){ x[0]=0; x[1]=1; return b; } long[] y=new long[2]; long gcd=gcdExtended(b%a, a, y); x[0]=y[1]-(b/a)*y[0]; x[1]=y[0]; return gcd; } public static int abs(int a,int b){ return (int)Math.abs(a-b); } public static long abs(long a,long b){ return (long)Math.abs(a-b); } public static int max(int a,int b){ if(a>b) return a; else return b; } public static int min(int a,int b){ if(a>b) return b; else return a; } public static long max(long a,long b){ if(a>b) return a; else return b; } public static long min(long a,long b){ if(a>b) return b; else return a; } public static long pow(long n,long p,long m){ long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; if(result>=m) result%=m; p >>=1; n*=n; if(n>=m) n%=m; } return result; } public static long pow(long n,long p){ long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; p >>=1; n*=n; } return result; } public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } void run() throws Exception { is = System.in; out = new PrintWriter(System.out); solve(); out.flush(); } public static void main(String[] args) throws Exception { new Thread(null, new Runnable() { public void run() { try { new code7().run(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
bab99f4487b3135c696dda33fce50bc9
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.*; import java.io.*; public class c { public static void main(String args[]) throws IOException{ BufferedReader f=new BufferedReader(new InputStreamReader(System.in)); String s=f.readLine(); int count=0; for(int i=0;i<s.length();i++){ //dont want to start at ) or will go negative while(i<s.length()&&s.charAt(i)==')') i++; int layer=0; int numPos=1; for(int j=i+1;j<=s.length();j++){ //want to make sure wont go negative on starting ? if(j==i+1&&s.charAt(j-1)=='?'){ layer=1; } else{ if(s.charAt(j-1)=='(') layer++; else if(s.charAt(j-1)==')') layer--; else{ layer--; numPos++; } if(layer==0){ count++; //System.out.println(i+" "+j); } if(layer==-1){ layer+=2; numPos-=1; } //0 pos means stop if(numPos==0){ j=s.length()+1; } } } } System.out.println(count); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
753aff2f1d0366d3c22bbf42e13ad6e1
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { // Use the Scanner class Scanner sc = new Scanner(System.in); /* int n = sc.nextInt(); // read input as integer long k = sc.nextLong(); // read input as long double d = sc.nextDouble(); // read input as double String str = sc.next(); // read input as String String s = sc.nextLine(); // read whole line as String */ String input = sc.nextLine(); int N = input.length(); boolean[][] f = new boolean[N][N]; boolean[][] g = new boolean[N][N]; for(int i=0; i<N; i++){ int curr = 0; boolean ok = true; for(int j=i; j<N; j++){ if(input.charAt(j) == ')'){ curr--; }else{ curr++; } if(curr<0){ ok = false; } f[i][j] = ok; } } for(int i=N-1; i>=0; i--){ int curr = 0; boolean ok = true; for(int j=i; j>=0; j--){ if(input.charAt(j) == '('){ curr--; }else{ curr++; } if(curr<0){ ok = false; } g[j][i] = ok; } } int count = 0; for(int i=0; i<N; i++){ for(int j=i; j<N; j++){ if(f[i][j] && g[i][j] && (j-i+1 )%2 == 0){ count++; } } } System.out.println(count); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
55c580a382916034da64950e47f51027
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; 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 sc, PrintWriter out) { char[] s = sc.next().toCharArray(); int n = s.length; int cnt = 0; boolean ok[][] = new boolean[s.length][s.length]; for (int i = 0; i < n; i++) { int open = 0, closed = 0, mark = 0; for (int j = i; j < n; j++) { if (s[j] == '(') open++; else if (s[j] == ')') closed++; else mark++; if (open + mark < closed) break; ok[i][j] = true; } } for (int i = n - 1; i >= 0; i--) { int open = 0, closed = 0, mark = 0; for (int j = i; j >= 0; j--) { if (s[j] == '(') open++; else if (s[j] == '(') closed++; else mark++; if (closed + mark < open) break; if (ok[j][i] && ((j - i + 1) & 1) == 0) { cnt++; } } } out.println(cnt); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { } ; } return st.nextToken(); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
45593e4e0651add26e23edb77ed18ca0
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.HashSet; import java.util.Map; import java.util.Scanner; import java.util.Set; public class c{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); String S = scan.next(); int n = S.length(); int ans = 0; for(int i=0;i<n;++i){ int qcnt = 0; int num = 0; //ι–‰γ˜γ‚Œγ‚‹γͺγ‚‰ι–‰γ˜γ‚‹ for(int j=i;j<n;++j){ if(S.charAt(j) == '?'){ if(num>0){ --num; ++qcnt; } else ++num; }else{ if(S.charAt(j) == '(')++num; else{ if(num>0)--num; else if(qcnt>0){ --qcnt; num++; }else break; } } if(num==0)++ans; } } System.out.println(ans); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
1da47f2f752ee30edb1d3ada3403ec93
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import sun.reflect.generics.tree.Tree; import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; public class Template { class Edge{ int to; int num; Edge(int to, int num){ this.to = to; this.num = num; } } class Vertex implements Comparable<Vertex>{ int ind; int dist; int num; Vertex(int ind, int dist, int num){ this.ind = ind; this.dist = dist; this.num = num; } @Override public int compareTo(Vertex o) { return Integer.compare(this.dist, o.dist); } } String fileName = ""; TreeSet<Integer>[] nums = new TreeSet[46]; int MODULO = 1000*1000*1000+7; void solve() throws IOException { char[] s = readString().toCharArray(); int n = s.length; int ans = 0; for(int i=0; i<n; ++i){ int q = 0; int b = 0; int q1 = 0; for(int j=i; j<n; ++j){ if(s[j] == '(') b++; if(s[j] == ')') b--; if(s[j] == '?') q++; if(b<0 && q == 0 && q1 == 0){ break; } if(b<0 && q>0) { q--; b++; } if(b<0 && q1>0) { q1--; q++; b++; } if(b>0 && s[j] == '?'){ q1++; q--; b--; } if((j-i)%2 == 1){ //out.println(i+1+" "+(j+1)+" "+q+" "+b); if(b == 0 && q%2 == 0){ ans++; //out.println(i+1+" "+(j+1) +"!!!!!"); } } } } out.println(ans); } //////////////////////////////////////////////////////////// class Pair{ int num; int val; Pair(int num, int val ){ this.num = num; this.val = val; } } class SegmentTree { int[] t; int[] nums; SegmentTree(int[] a) { int n = a.length - 1; t = new int[n * 4]; nums = new int[n*4]; build(a, 1, 1, n); } Pair max1; Pair max2; void build(int[] a, int v, int tl, int tr) { if (tl == tr) { t[v] = a[tl]; nums[v] = tl; return; } int mid = (tr + tl) / 2; build(a, 2 * v, tl, mid); build(a, 2 * v + 1, mid + 1, tr); t[v] = Math.max(t[2 * v], t[2 * v + 1]); if(t[v] == t[2*v]){ nums[v] = nums[2*v]; }else{ nums[v] = nums[2*v+1]; } } void update(int v, int tl, int tr, int pos, int value) { if (tl == tr) { t[v] = value; return; } int mid = (tl + tr) / 2; if (pos <= mid) { update(2 * v, tl, mid, pos, value); } else { update(2 * v + 1, mid + 1, tr, pos, value); } t[v] = Math.max(t[2 * v], t[2 * v + 1]); if(t[v] == t[2*v]){ nums[v] = nums[2*v]; }else{ nums[v] = nums[2*v+1]; } } Pair getMax(int v, int tl, int tr, int l, int r) { if (l > r) { return new Pair(-1,-1000 * 1000); } if (tl == tr) { return new Pair(nums[v],t[v]); } if (l == tl && r == tr) { return new Pair(nums[v],t[v]); } int mid = (tl + tr) / 2; max1 = getMax(2 * v, tl, mid, l, Math.min(mid, r)); max2 = getMax(2 * v + 1, mid + 1, tr, Math.max(mid + 1, l), r); int max = Math.max(max1.val, max2.val); if(max1.val == max){ return max1; }else{ return max2; } } } class Fenwik { long[] t; int length; Fenwik(int[] a) { length = a.length + 100; t = new long[length]; for (int i = 0; i < a.length; ++i) { inc(i, a[i]); } } void inc(int ind, int delta) { for (; ind < length; ind = ind | (ind + 1)) { t[ind] += delta; } } long getSum(int r) { long sum = 0; for (; r >= 0; r = (r & (r + 1)) - 1) { sum += t[r]; } return sum; } } int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); } long binPow(long a, long b, long m) { if (b == 0) { return 1; } if (b % 2 == 1) { return ((a % m) * (binPow(a, b - 1, m) % m)) % m; } else { long c = binPow(a, b / 2, m); return (c * c) % m; } } public static void main(String[] args) throws NumberFormatException, IOException { // TODO Auto-generated method stub new Template().run(); } void run() throws NumberFormatException, IOException { solve(); out.close(); }; BufferedReader in; PrintWriter out; StringTokenizer tok; String delim = " "; Random rnd = new Random(); Template() throws FileNotFoundException { try { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } catch (Exception e) { if (fileName.isEmpty()) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader(fileName + ".in")); out = new PrintWriter(fileName + ".out"); } } tok = new StringTokenizer(""); } String readLine() throws IOException { return in.readLine(); } String readString() throws IOException { while (!tok.hasMoreTokens()) { String nextLine = readLine(); if (null == nextLine) { return null; } tok = new StringTokenizer(nextLine); } return tok.nextToken(); } int readInt() throws NumberFormatException, IOException { return Integer.parseInt(readString()); } byte readByte() throws NumberFormatException, IOException { return Byte.parseByte(readString()); } int[] readIntArray (int n) throws NumberFormatException, IOException { int[] a = new int[n]; for(int i=0; i<n; ++i){ a[i] = readInt(); } return a; } long readLong() throws NumberFormatException, IOException { return Long.parseLong(readString()); } double readDouble() throws NumberFormatException, IOException { return Double.parseDouble(readString()); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
da3d1716e7f9c8fd0667beb8129eb739
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; // import java.awt.Point; public class Main { InputStream is; PrintWriter out; String INPUT = ""; long mod = 1_000_000_007; long inf = Long.MAX_VALUE; boolean[][] used; void solve(){ char[] c = ns().toCharArray(); int n = c.length; int min = 0; int max = 0; int ans = 0; for(int i = 0; i < n; i++){ max = 0; min = 0; for(int j = i; j < n; j++){ if(c[j]=='('){ max++; min++; } else if(c[j]==')'){ max--; min--; if(max<0) break; if(min<=0&&(j-i)%2==1){ ans++; // out.println(i+" "+j); } if(min<0) min += 2; } else{ max++; min--; if(max<0) break; if(min<=0&&(j-i)%2==1){ ans++; // out.println(i+" "+j); } if(min<0) min += 2; } } } out.println(ans); } 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]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b) && b != ' ')){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
0bf163c9535c5f918fac1d7f1a5284f3
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.*; import java.io.*; public class C { void solve(BufferedReader in) throws Exception { String s = in.readLine(); int c = 0; for (int i = 0; i<s.length() - 1; i++) { if (s.charAt(i) == ')') continue; int l = 1, r= 1; for(int j = i+1; j<s.length(); j++) { char cc = s.charAt(j); if(cc == '?') { r++; l--; if (l == -1) l = 1; } else if(cc=='(') { l++; r++; } else { r--; l--; if (l == -1) l = 1; if (r < l) break; } if(l == 0 && (j-i)%2 == 1) c++; } } System.out.println(c); } int toInt(String s) {return Integer.parseInt(s);} int[] toInts(String s) { String[] a = s.split(" "); int[] o = new int[a.length]; for(int i = 0; i<a.length; i++) o[i] = toInt(a[i]); return o; } void e(Object o) { System.err.println(o); } public static void main(String[] args) throws Exception{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); (new C()).solve(in); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
f72ff0747ff15c3374d2811efc8b32a5
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
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 Codechef { long MOD = 1000000007L; int INF = 1000000001; public class Obj implements Comparable<Obj>{ public int t; public int h; public Obj(int t_, int h_){ t = t_; h = h_; } public int compareTo(Obj other){ return t - other.t; } } public static void main (String[] args) throws java.lang.Exception { // your code goes here Codechef cf = new Codechef(); cf.Solve(); } boolean[] vis; ArrayList<ArrayList<Integer>> con, par; public int GCD(int a, int b){ while (b > 0){ int tmp = b; b = a % b; a = tmp; } return a; } public void Solve(){ MyScanner sc = new MyScanner(); String s = sc.nextLine(); char[] arr = s.toCharArray(); int n = arr.length; int res = 0; int[][] c1 = new int[n][n]; int[][] c2 = new int[n][n]; for (int i = 0; i < n; ++i){ int l = 0; int r = 0; int q = 0; for (int j = i; j < n; ++j){ if (arr[j] == '('){ l++; }else if (arr[j] == ')'){ r++; }else{ q++; } if (r > (l + q)){ break; } int k = j - i + 1; // System.out.println(i + " j: " + j + " " + l + " " + r + " " + q + " k: " + k); if (k % 2 != 0) continue; k = k / 2; if ((l > k) || (r > k)) continue; if (arr[j] == ')'){ // System.out.println("Incre: " + i + " j: " + j + " " + l + " " + r + " " + q + " k: " + k); c1[i][j] = 1; }else if (arr[j] == '?'){ int mr = r + 1; int mq = q - 1; if (mr <= (mq + l)){ // System.out.println("Incre: " + i + " j: " + j + " " + l + " " + r + " " + q + " k: " + k); c1[i][j] = 1; } } } // System.out.println(i + " " + res); } for (int i = n - 1; i >= 0; --i){ int l = 0; int r = 0; int q = 0; for (int j = i; j >= 0; --j){ if (arr[j] == '('){ l++; }else if (arr[j] == ')'){ r++; }else{ q++; } if (l > (r + q)){ break; } int k = i - j + 1; // System.out.println(i + " j: " + j + " " + l + " " + r + " " + q + " k: " + k); if (k % 2 != 0) continue; k = k / 2; if ((l > k) || (r > k)) continue; if (arr[j] == '('){ // System.out.println("Incre: " + i + " j: " + j + " " + l + " " + r + " " + q + " k: " + k); c2[j][i] = 1; }else if (arr[j] == '?'){ int ml = l + 1; int mq = q - 1; if (ml <= (mq + r)){ // System.out.println("Incre: " + i + " j: " + j + " " + l + " " + r + " " + q + " k: " + k); c2[j][i] = 1; } } } } for (int i = 0; i < n; ++i){ for (int j = 0; j < n; ++j){ if ((c1[i][j] == 1) && (c2[i][j] == 1)){ res++; } } } out = new PrintWriter(new BufferedOutputStream(System.out)); out.println(res); out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
1746a50cbab7250a53bf64a71390ca6e
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.*; import java.io.*; public class Main{ public static void main(String args[]){ Scanner cin=new Scanner(System.in); String s=cin.next(); int ans=0; for(int i=0;i<s.length();i++){ int l=0,ct=0; for(int j=i;j<s.length();j++){ if(s.charAt(j)=='(') l++; else if(s.charAt(j)==')') l--; else if(s.charAt(j)=='?'){ l--;ct++; } if(l==0) ans++; else if(l<0&&ct>0){ l+=2;ct--; } else if(l<0&&ct==0) break; } } System.out.println(ans); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
ee5325184cf62aa4928148a9d3552a34
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner s=new Scanner (System.in); String str=s.next(); int p=str.length(),ans=0; char[]ar=str.toCharArray(); for (int l = 0; l < p; ++l) { int mx = 0, mi = 0; for (int r = l; r < p; ++r) { if (ar[r] == ')') { mx--; mi--; } else { if (ar[r] == '(') { mi++; mx++; } else { mi--; mx++; } } if (mi < 0) mi = 0; if (mx < 0) break; if (mi == 0 && (r - l) % 2==1) ans++; } } System.out.print(ans); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
ef35687dcf0ed8ba2c512be295a30238
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); char[] s = input.next().toCharArray(); boolean[][] ans1 = new boolean[5000][5000]; boolean[][] ans2 = new boolean[5000][5000]; for (int i = 0; i < s.length; ++i) { int lcnt = 0, rcnt = 0, qcnt = 0; for (int j = i; j < s.length; ++j) { if (s[j] == '(') ++lcnt; else if (s[j] == ')') ++rcnt; else ++qcnt; if (lcnt + qcnt < rcnt) break; else ans1[i][j] = true; } } for (int i = s.length - 1; i >= 0; --i) { int lcnt = 0, rcnt = 0, qcnt = 0; for (int j = i; j >= 0; --j) { if (s[j] == '(') ++lcnt; else if (s[j] == ')') ++rcnt; else ++qcnt; if (rcnt + qcnt < lcnt) break; else ans2[j][i] = true; } } int res = 0; for (int i = 0; i < s.length; ++i) { for (int j = i + 1; j < s.length; j += 2) { if (ans1[i][j] && ans2[i][j]) ++res; } } System.out.println(res); input.close(); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
84cd119b6c9b1fe241ea4173f42e08ca
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int len = str.length(); int ans = 0; for(int i=0;i<len;i++) { int l = 0; int r = 0; for(int j=i;j<len;j++) { if(str.charAt(j) == '(') l++; else if(str.charAt(j) == ')') l--; else { l--; r++; } if(l == 0) ans++; else if(l < 0 && r > 0) { l += 2; r--; } else if(l < 0 && r == 0) break; } } System.out.println(ans); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
e104d9e7d35565370f6db62dcb6573c1
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int len = str.length(); int ans = 0; for(int i=0;i<len;i++) { int score = 0; int qmarks = 0; for(int j=i;j<len;j++) { if(str.charAt(j) == '(') score++; else if(str.charAt(j) == ')') score--; else { score--; qmarks++; } if(score == 0) ans++; else if(score < 0 && qmarks > 0) { score += 2; qmarks--; } else if(score < 0 && qmarks == 0) break; } } System.out.println(ans); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
ece4d3b2cbf31c4fd26e430ac7af1d24
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.Scanner; public class C { public static void main(String[] args) { char[] s = new Scanner(System.in).next().toCharArray(); int n = s.length; int ans = 0; for (int i = 0; i < n; i++) { int open = 0; int q = 0; for (int j = i; j < n; j++) { if (s[j] == '(') open++; else if (s[j] == ')') open--; else q++; if (open + q < 0) break; if (open == q) ans++; else if (q > open) { int tmp = q; q = open; open = tmp; } } } System.out.println(ans); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
72692ebbc02d31e8f111de99006f0473
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { InputReader reader = new InputReader(System.in); char[] s = reader.next().toCharArray(); long count = 0; int n = s.length; for (int i = 0; i < n; ++i) { if (s[i] == ')') continue; int max = 0; int min = 0; for (int j = i; j < n; j++) { if (s[j] == '(') { max++; min++; } else if (s[j] == ')') { max--; min--; } else { max++; min--; } if (min < 0) { min += 2; } if (max < 0) break; if (((j - i + 1) & 1) == 0 && min == 0) { count++; } } } System.out.println(count); } static class InputReader { StringTokenizer tokenizer; BufferedReader reader; String token; String temp; public InputReader(InputStream stream) { tokenizer = null; reader = new BufferedReader(new InputStreamReader(stream)); } public InputReader(FileInputStream stream) { tokenizer = null; reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() throws IOException { return reader.readLine(); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { if (temp != null) { tokenizer = new StringTokenizer(temp); temp = null; } else { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException e) { } } return tokenizer.nextToken(); } public double nextDouble() { return Double.parseDouble(next()); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
cd615e4f4e80f9597fdc39fd3a720061
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.Scanner; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); boolean[][] b = new boolean[s.length()][s.length()]; int ans = 0; for (int i = 0; i < s.length(); i++) { int cnt1 = 0, cnt2 = 0, cnt3 = 0; for (int j = i; j < s.length(); j++) { if (s.charAt(j) == '(') cnt1++; else if (s.charAt(j) == ')') cnt2++; else cnt3++; if (cnt2 > cnt1 + cnt3) break; if (((j - i) & 1) == 0) continue; if (cnt1 <= cnt2 + cnt3) { b[i][j] = true; } } } for (int i = s.length() - 1; i >= 0; i--) { int cnt1 = 0, cnt2 = 0, cnt3 = 0; for (int j = i; j > 0; j--) { if (s.charAt(j) == '(') cnt1++; else if (s.charAt(j) == ')') cnt2++; else cnt3++; if (cnt1 > cnt2 + cnt3) { while (j >= 0) b[j--][i] = false; break; } if (((i - j) & 1) == 0) continue; } } for (int i = 0; i < s.length(); i++) { for (int j = i; j < s.length(); j++) { if(b[i][j]) ans++; } } System.out.println(ans); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
9d697bb51a916873c43ce82c6d0f26cd
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
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 */ 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); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, OutputWriter out) { String s = in.next(); int res = 0; for (int left = 0; left < s.length(); left++) { int canSwitchToOpen = 0; int balance = 0; for (int right = left; right < s.length(); right++) { if (s.charAt(right) == '(') { balance++; } else if (s.charAt(right) == ')') { if (balance == 0) { if (canSwitchToOpen > 0) { canSwitchToOpen--; balance += 2; } } balance--; } else if (s.charAt(right) == '?') { if (balance > 0) { balance--; canSwitchToOpen++; } else if (balance == 0) { balance++; } } else throw new RuntimeException(); if (balance < 0) { break; } if (balance == 0) { res++; } } } out.printLine(res); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public 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 close() { writer.close(); } public void printLine(int i) { writer.println(i); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
edd15b574ba9ca811246136df0c2c376
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.*; import java.io.*; public class B { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); char[] arr = in.readLine().toCharArray(); int n = 0; for(int i = 0; i < arr.length; i++) { //System.err.println(i); int min = 1; int max = 1; if(arr[i] == ')') continue; for(int j = i + 1; j < arr.length; j++) { if(arr[j] == ')') { min--; max--; //invariant: min is always greater than 0 if(min < 0) { min += 2; if(min > max) break; } } if(arr[j] == '(') { min++; max++; } if(arr[j] == '?') { min--; max++; //invariant: min is always greater than 0 if(min < 0) { min += 2; if(min > max) break; } } if(min == 0) n++; //System.err.println(min + " <min | max> " + max); } } System.out.println(n); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
9ca52e850209298b4f46d5758ec255a9
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws Exception { Reader r = new Reader(); String s = r.next(); int cnt = 0; for (int i = 0; i <= s.length(); i++) { int a=0, b=0; for (int j = i; j < s.length(); j++) { switch (s.charAt(j)) { case '(': a += 1; break; case ')': a -= 1; break; case '?': b += 1; break; } if (a + b < 0) { break; } if (a < b) { a++; b--; } if (a == b) { cnt++; } } } System.out.println(cnt); } } class Reader { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer tokenizer; String next() throws Exception { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } int nextInt() throws Exception { return Integer.valueOf(next()); } long nextLong() throws Exception { return Long.valueOf(next()); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
23da0cdc92b8e4c26ac6943341f5d36e
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Round459 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader in=new FastReader(); public static void main(String[] args) { solveC(); } private static void solveC() { String s=in.next(); int n=s.length(); int[][] dp=new int[n][n]; for(int i=0;i<n;i++) { for(int j=i;j<n;j++) { if((j-i+1)%2==1)dp[i][j]=1; } } for(int i=0;i<n;i++) { int cnt=0; for(int j=i;j<n;j++) { if(s.charAt(j)==')')cnt--; else cnt++; if(cnt<0) { for(int k=j;k<n;k++) { dp[i][k]=1; } break; } } } for(int i=n-1;i>=0;i--) { int cnt=0; for(int j=i;j>=0;j--) { if(s.charAt(j)=='(')cnt--; else cnt++; if(cnt<0) { for(int k=j;k>=0;k--) { dp[k][i]=1; } break; } } } int ans=0; for(int i=0;i<n;i++) { for(int j=i;j<n;j++) { if(dp[i][j]==0)ans++; } } System.out.println(ans); } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
ad2456bc679cae62c2a1c71c105785fb
train_004.jsonl
1517236500
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: Empty string is a correct bracket sequence. if s is a correct bracket sequence, then (s) is also a correct bracket sequence. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≀ l ≀ r ≀ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.
256 megabytes
import java.util.*; import java.io.*; public class tr0 { static PrintWriter out; static StringBuilder sb; static final double EPS = 1e-9; static long mod = 1000000007; static int inf = (int) 1e9 + 2; static long[] fac; static Long[] a; static int[] si; static ArrayList<Integer> primes; static ArrayList<Integer>[] ad; static ArrayList<qu>[] d; static edge[] ed; static int[] l, ch; static int[] occ, p; static Queue<Integer>[] can; static String s; static int[] memo; static int n, m, k; static int[] v; static int[] wi; static int[] da; static long x; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); out = new PrintWriter(System.out); String s = sc.nextLine(); int n = s.length(); int ans = 0; int[][] bit = new int[n][n]; for (int i = 0; i < n; i++) { int op = 0; int cl = 0; int q = 0; int p = 0; if (s.charAt(i) == ')') continue; if (s.charAt(i) == '(') op++; else q++; p++; for (int j = i + 1; j < n; j++) { if (s.charAt(j) == '(') { op++; p++; continue; } if (s.charAt(j) == ')') { cl++; p--; } else { p++; q++; } if (p < 0) break; if ((j - i) % 2 == 0) continue; // System.out.println(i+" "+j); bit[i][j]++; } } // for(int []a:bit) //System.out.println(Arrays.toString(a)); for (int i = n - 1; i > 0; i--) { int op=0; int other=1; if(s.charAt(i)=='(') continue; for(int j=i-1;j>=0;j--) { if(s.charAt(j)=='(') op++; else other++; if(other-op<0) break; if ((i - j) % 2 == 0) continue; // System.out.println(i+" "+j+" "+other+" "+op); if(bit[j][i]==1) ans++; } } out.print(ans); out.flush(); } static class qu implements Comparable<qu> { int a; int b; qu(int y, int g) { a = y; b = g; } public String toString() { return a + " " + b; } public int compareTo(qu o) { if (a == o.a) return b - o.b; return a - o.a; } } static class seg implements Comparable<seg> { int a; int b; int l; int r; int bit; seg(int s, int e, int x, int y, int bi) { a = s; b = e; l = x; r = y; bit = bi; } public String toString() { return a + " " + b + " " + l + " " + r + " " + bit; } public int compareTo(seg o) { // if(a==o.a) return bit - o.bit; // return } } static class pair implements Comparable<pair> { long to; long number; pair(long t, long n) { number = n; to = t; } public String toString() { return to + " " + number; } @Override public int compareTo(pair o) { if (o.to < to) return 1; return -1; } } static long modPow(long a, int e) { long res = 1; while (e > 0) { if ((e & 1) == 1) res = (res * a) % mod; a = (a * a) % mod; e >>= 1; } return res; } static long inver(long x) { long a = x; long e = (mod - 2); long res = 1; while (e > 0) { if ((e & 1) == 1) { res = ((1l * res * a) % mod); } a = ((1l * a * a) % mod); e >>= 1; } return res % mod; } static class edge implements Comparable<edge> { int from; int to; int number; edge(int f, int t, int n) { from = f; to = t; number = n; } public String toString() { return from + " " + to + " " + number; } public int compareTo(edge f) { return f.number - number; } } static void seive(int N) { si = new int[N]; primes = new ArrayList<>(); si[1] = 1; for (int i = 2; i < N; i++) { if (si[i] == 0) { si[i] = i; primes.add(i); } for (int j = 0; j < primes.size() && primes.get(j) <= si[i] && (i * primes.get(j)) < N; j++) si[primes.get(j) * i] = primes.get(j); } } static long fac(int n) { if (n == 0) return fac[n] = 1; if (n == 1) return fac[n] = 1; long ans = 1; for (int i = 1; i <= n; i++) fac[i] = ans = (i % mod * ans % mod) % mod; return ans % mod; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static class unionfind { int[] p; int[] size; int[] max; int num; unionfind(int n) { p = new int[n]; size = new int[n]; max = new int[n]; for (int i = 0; i < n; i++) { p[i] = i; max[i] = i; } Arrays.fill(size, 1); num = n; } int findSet(int v) { if (v == p[v]) return v; max[v] = Math.max(max[v], max[p[v]]); p[v] = findSet(p[v]); max[v] = Math.max(max[v], max[p[v]]); return p[v]; } boolean sameSet(int a, int b) { a = findSet(a); b = findSet(b); if (a == b) return true; return false; } int max() { int max = 0; for (int i = 0; i < size.length; i++) if (size[i] > max) max = size[i]; return max; } boolean combine(int a, int b) { a = findSet(a); b = findSet(b); if (a == b) return true; // System.out.println(num+" ppp"); num--; if (size[a] > size[b]) { p[b] = a; max[a] = Math.max(max[a], max[b]); size[a] += size[b]; } else { p[a] = b; max[b] = Math.max(max[a], max[b]); size[b] += size[a]; } return false; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
Java
["((?))", "??()??"]
1 second
["4", "7"]
NoteFor the first sample testcase, the pretty substrings of s are: "(?" which can be transformed to "()". "?)" which can be transformed to "()". "((?)" which can be transformed to "(())". "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of s are: "??" which can be transformed to "()". "()". "??()" which can be transformed to "()()". "?()?" which can be transformed to "(())". "??" which can be transformed to "()". "()??" which can be transformed to "()()". "??()??" which can be transformed to "()()()".
Java 8
standard input
[ "dp", "greedy", "math", "implementation", "data structures" ]
6b0d00ecfa260a33e313ae60d8f9ee06
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≀ |s| ≀ 5000).
1,800
Print the answer to Will's puzzle in the first and only line of output.
standard output
PASSED
68ed00a74255698b8233585cc0333778
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { public static int getCityWinner(String[] array) { int index=0,max=Integer.parseInt(array[0]); for(int i=1;i<array.length;i++){ if(Integer.parseInt(array[i])>max){ index=i; max=Integer.parseInt(array[i]); } else if(Integer.parseInt(array[i])==max){ index=Math.min(i,index); } } return index; } public static void main (String[] args) throws java.lang.Exception { int candidates,cities; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String input=br.readLine(); String[] tokens=input.split(" "); int[] count=new int[Integer.parseInt(tokens[0])]; int max=0; int index=0; for(int i=0;i<Integer.parseInt(tokens[1]);i++) { String[] token=br.readLine().split(" "); int change=getCityWinner(token); count[change]++; if(count[change]>max) { max=count[change]; index=change; } else if(count[change]==max){ index=Math.min(change,index); } } System.out.println(index+1); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
adda63dc2b43f0499328787657ae5235
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
//package Codeforces.Div2A_316.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 col = in.nextInt(); int row = in.nextInt(); int best = -1; int result[] = new int[col]; for (int i = 0; i < row; i++) { int max = -1; int winner = -1; for (int j = 0; j < col; j++) { int temp = in.nextInt(); if (max < temp) { max = temp; winner = j; } } result[winner]++; best = Math.max(result[winner], best); } for (int i = 0; i < result.length; i++) { if (best == result[i]) { System.out.println(i + 1); return; } } } 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
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
f9ec49a8a28b462c3a06411bff5d38e4
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.Arrays; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.TreeMap; public class Elections { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[] rowColArgs = scanner.nextLine().split(" "); int rows = Integer.parseInt(rowColArgs[1]); int cols = Integer.parseInt(rowColArgs[0]); int[][] electionMatrix = new int[rows][cols]; int[] maxVotes = new int[rows]; int[] winnerIndexes = new int[rows]; for (int i = 0; i < rows; i++) { String[] indivRow = scanner.nextLine().split(" "); int tempMax = -1; int winnerIndex = -1; for (int j = 0; j < cols; j++) { int currVotes = Integer.parseInt(indivRow[j]); electionMatrix[i][j] = currVotes; if (currVotes > tempMax) { tempMax = currVotes; winnerIndex = j; } } maxVotes[i] = tempMax; winnerIndexes[i] = winnerIndex; } Map<Integer, Integer> citiesWonByCand = new TreeMap<>(); for (int i = 0; i < winnerIndexes.length; i++) { if (!citiesWonByCand.containsKey(winnerIndexes[i])) { citiesWonByCand.put(winnerIndexes[i], 0); } citiesWonByCand.put(winnerIndexes[i], citiesWonByCand.get(winnerIndexes[i]) + 1); } int maxWins = -1; int maxWinKey = -1; for(Entry<Integer, Integer> entry : citiesWonByCand.entrySet()) { if (entry.getKey() != null) { if (entry.getValue() > maxWins) { maxWins = entry.getValue(); maxWinKey = entry.getKey(); } } } System.out.print(maxWinKey + 1); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
405bca07cec853cc9c2196fbb4d02134
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Elections { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int m = scan.nextInt(); int[][] arr = new int[n][2]; for (int i = 0; i < n;i++){ arr[i][0] = (i+1); } for (int i = 0; i < m; i++){ int candidate = 0; int curr = 0; for (int j = 0; j < n; j++){ int ab = scan.nextInt(); if (curr < ab){ candidate = j; curr = ab; } } arr[candidate][1] += 1; } int temp = 0; int candidates = 0; for (int i = 0; i < n; i++){ int joe = arr[i][1]; if (joe > temp){ temp = joe; candidates = i; } } System.out.println(candidates + 1); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
8fcd40a67818a9691138135d633c6bd4
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.Scanner; public class Elections { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int m = scan.nextInt(); int[][] arr = new int[n][2]; for (int i = 0; i < n;i++){ arr[i][0] = (i+1); } for (int i = 0; i < m; i++){ int candidate = 0; int curr = 0; for (int j = 0; j < n; j++){ int ab = scan.nextInt(); if (curr < ab){ candidate = j; curr = ab; } } arr[candidate][1] += 1; } int temp = 0; int candidates = 0; for (int i = 0; i < n; i++){ int joe = arr[i][1]; if (joe > temp){ temp = joe; candidates = i; } } System.out.println(candidates + 1); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
4a844debe534e1ed6f506dd5d83b86a2
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.util.Random; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } static class Task { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[] votes = new int[n]; for (int i = 0; i < m; i++) { int max = -1; int maxIndex = -1; for (int j = 0; j < n; j++) { int next = in.nextInt(); if (next > max) { max = next; maxIndex = j; } } votes[maxIndex]++; } int max = -1; int maxIndex = -1; for (int i = 0; i < n; i ++) { if (votes[i] > max) { max = votes[i]; maxIndex = i; } } System.out.println(maxIndex + 1); } } static class InputReader { public BufferedReader reader; public StringTokenizer st; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); st = null; } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
d48cb4d662c128c2b81e1cf9f949a1c0
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.io.*; import java.util.StringTokenizer; /** * Created by sbabkin on 9/14/2015. */ public class SolverA { public static void main(String[] args) throws IOException { new SolverA().Run(); } BufferedReader br; PrintWriter pw; StringTokenizer stok; private String nextToken() throws IOException { while (stok==null || !stok.hasMoreTokens()){ stok = new StringTokenizer(br.readLine()); } return stok.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } private void Run() throws IOException { // br=new BufferedReader(new FileReader("input.txt")); // pw=new PrintWriter("output.txt"); br=new BufferedReader(new InputStreamReader(System.in)); pw=new PrintWriter(new OutputStreamWriter(System.out)); solve(); pw.flush(); pw.close(); } private void solve() throws IOException { int n=nextInt(); int m=nextInt(); int[] kolVotes =new int[n]; for (int i=0; i<m; i++) { int curCan=1; int kol=0; for (int j=0; j<n; j++) { int val=nextInt(); if (val>kol) { curCan=j+1; kol=val; } } kolVotes[curCan-1]++; } int result=1; int kol=0; for (int i=0; i<n; i++) { if (kolVotes[i]>kol) { result=i+1; kol=kolVotes[i]; } } pw.println(result); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
5a75576b842514d119432180d19f3057
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Elections { public static void main(String[] args)throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int m, n; StringTokenizer st = new StringTokenizer(br.readLine()); n=Integer.parseInt(st.nextToken()); m=Integer.parseInt(st.nextToken()); int votes[][] = new int[n][m]; for(int city=0; city<m; city++){ st= new StringTokenizer(br.readLine()); for(int candidate =0; candidate<n; candidate++){ votes[candidate][city] = Integer.parseInt(st.nextToken()); } } int winnerInCities[] = new int[n]; for(int city=0; city<m; city++){ int winnerId=-1; int winnerVotes=-1; for(int candidate =0; candidate<n; candidate++){ if(votes[candidate][city]>winnerVotes){ winnerId=candidate; winnerVotes=votes[candidate][city]; } } winnerInCities[winnerId]++; } int winnerId=-1; int winnerVotes=-1; for(int candidate =0; candidate<n; candidate++){ if(winnerInCities[candidate]>winnerVotes){ winnerId=candidate; winnerVotes=winnerInCities[candidate]; } } System.out.println(winnerId+1); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
6b99aa8426fbf452155dcbbaf64b152c
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { Scanner Reader = new Scanner(System.in); int n = Reader.nextInt(); int m = Reader.nextInt(); int [][] ar = new int [m][2]; int max = Integer.MIN_VALUE; for (int i = 0; i < m; i++) { max = Integer.MIN_VALUE; for (int j = 0; j < n; j++) { int v = Reader.nextInt(); if(v>max){ max = v; ar[i][0] = v; ar[i][1] = j ; } } } int[]fre = new int [n]; for (int i = 0; i < ar.length; i++) { fre[ar[i][1]]++; } max = Integer.MIN_VALUE; int index = Integer.MAX_VALUE; for (int i = 0; i < fre.length; i++) { if(fre[i]>max){ max = fre[i]; index = i; } } System.out.println((index+1)); } private static boolean isSorted(int[] ar) { for (int i = 0; i < ar.length - 1; i++) { if (ar[i]>ar[i+1]) { return false; } } return true; } private static boolean thereIsNodistenct(int[] ar) { for (int i = 0; i < ar.length - 1; i++) { if (ar[i] == ar[i + 1]) { return true; } } return false; } public static void sort(int[] a, int low, int high) { int N = high - low; if (N <= 1) { return; } int mid = low + N / 2; // recursively sort sort(a, low, mid); sort(a, mid, high); // merge two sorted subarrays int[] temp = new int[N]; int i = low, j = mid; for (int k = 0; k < N; k++) { if (i == mid) { temp[k] = a[j++]; } else if (j == high) { temp[k] = a[i++]; } else if (a[j] < a[i]) { temp[k] = a[j++]; } else { temp[k] = a[i++]; } } for (int k = 0; k < N; k++) { a[low + k] = temp[k]; } } class dragons implements Comparable<dragons> { int power, bounes; public dragons(int p, int b) { this.power = p; this.bounes = b; } @Override public int compareTo(dragons o) { return Integer.compare(this.power, o.power); } } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** * call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** * get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } 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(); } } } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
8b6d8197bf6bfb13190f9c93ed4c7027
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); //inputs int n = in.nextInt(); // int m = in.nextInt(); int [] v= new int[n]; int[][] arr = new int[m][n]; for(int i=0 ; i<m ; i++) for(int j=0 ; j<n ; j++) arr[i][j] = in.nextInt(); int max=0 , index=0 ; for(int j=0 ; j<m ; j++){ for(int i=0 ; i<n ; i++){ if(arr[j][i] > max){ max = arr[j][i]; index = i; } } v[index]++; index=0; max=0; } int max1=0; for(int i=0 ; i<n ; i++) if(v[i] > max1){ max1 = v[i]; index = i+1; } System.out.println(index); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
a99ce5629d1f570b3ea2d7cbdada6567
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.StringTokenizer; public class Solution{ public static void main(String args[]) throws Exception { FastScanner sc = new FastScanner(); int n = sc.nextInt(); int m = sc.nextInt(); Map<Integer,Integer> data = new HashMap<Integer,Integer>(); for(int i=0;i<m;i++){ long[] seats = new long[n]; for(int j=0;j<n;j++){ seats[j] = sc.nextLong(); } int city_winner = checkWinner(seats); if(data.containsKey(city_winner)) data.put(city_winner, data.get(city_winner)+1); else data.put(city_winner, 1); } data = sortByComparator(data); for (Map.Entry<Integer, Integer> entry : data.entrySet()) { System.out.println(entry.getKey()); break; } } public static int checkWinner(long[] seats){ long max = -1; int max_pos = -1; for(int i=0;i<seats.length;i++){ if(seats[i] > max){ max = seats[i]; max_pos = i+1; } } return max_pos; } private static Map<Integer, Integer> sortByComparator(Map<Integer, Integer> unsortMap) { // Convert Map to List List<Map.Entry<Integer, Integer>> list = new LinkedList<Map.Entry<Integer, Integer>>(unsortMap.entrySet()); // Sort list with comparator, to compare the Map values Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { if(o1.getValue()==o2.getValue()){ if(o1.getKey() < o2.getKey()){ return -1; } else return 1; } else{ return o2.getValue().compareTo(o1.getValue()); } } }); // Convert sorted map back to a Map Map<Integer, Integer> sortedMap = new LinkedHashMap<Integer, Integer>(); for (Iterator<Map.Entry<Integer, Integer>> it = list.iterator(); it.hasNext();) { Map.Entry<Integer, Integer> entry = it.next(); sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } public static 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
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
96515d84d3e12a977d37d6210d5601e9
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Codeforces { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner (System.in); int n=input.nextInt(); //n=number of candidates int m=input.nextInt(); // m=number of cities // to get the winner in the first stage int [][] votes=new int [m][n]; for (int i=0;i<m;i++){ for (int j=0;j<n;j++){ votes[i][j]=input .nextInt(); } } // array to get the winner in each city int [] winner = new int [m]; int max; for (int i = 0; i <m ; i++) {// to take each city. max=votes[i][0]; for (int j = 0; j < n; j++) {//to compare the candidates of the higher votes in each city //winner[i]=j; if (max<votes[i][j]){ winner[i]=j; max=votes[i][j]; } // else{ // winner[i]=0; fhmtha // } } } // the array winner has the candidates who have gotten higher votes in each city // to get the winner in the second stage Arrays.sort(winner); int value; int repeat=0; value=winner[0]; int maxrepeat=0; int maxvalue=0; for (int i = 0; i < winner.length; i++) { if(winner[i]==value){ repeat ++; if (repeat> maxrepeat ){ maxrepeat=repeat; maxvalue= winner[i]; } }else if(winner[i]!=value) { repeat=1; value=winner[i]; } } System.out.println(maxvalue+1); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
e862f26dadf771f5f070e669bad91607
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(); List<Integer> a; List<Integer> b = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { b.add(0); } for (int i = 0; i < m; i++) { a = new ArrayList<Integer>(); for (int j = 0; j < n; j++) { a.add(in.nextInt()); } int max = Collections.max(a); int id = a.indexOf(max); b.set(id, b.get(id) + 1); } System.out.println(b.indexOf(Collections.max(b)) + 1); in.close(); System.exit(0); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
5ef380c6678149749aa0dc90e654fa4f
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.Scanner; public class Elections { public static void main(String[] args) { Scanner ob=new Scanner(System.in); int n,m,i,j; n= ob.nextInt(); m= ob.nextInt(); int[] V=new int[n]; int lar; int[] W=new int[n]; for(i=0;i<m;i++) {lar=0; for(j=0;j<n;j++) { V[j]=ob.nextInt(); if(V[j]>V[lar]) lar = j; } W[lar]++; } int winner=0; for(i=0;i<n;i++) { if(W[i]>W[winner]) winner=i; } System.out.println(winner+1); ob.close(); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
e922ad00333d19c3e0f6ed66a00c1945
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.io.PrintWriter; import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int m = in.nextInt(); int n = in.nextInt(); int[][] vote = new int[n][m]; HashMap<Integer, Integer> c = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { int maxOfCity = 0; int maxOfCityVal = 0; for (int j = 0; j < m; j++) { vote[i][j] = in.nextInt(); if (vote[i][j] > maxOfCityVal) { maxOfCity = j; maxOfCityVal = vote[i][j]; } } if (c.containsKey(maxOfCity)) { c.put(maxOfCity, c.get(maxOfCity) + 1); } else { c.put(maxOfCity, 1); } } int winner = 0; int winnerVal = 0; for (int x = 0; x < m; x++) { if (c.containsKey(x)) { if (c.get(x) > winnerVal) { winnerVal = c.get(x); winner = x; } } } out.print(winner + 1); out.flush(); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
4a265e5e3ae57c52c07540375e624e20
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { public static void main (String[] args) throws java.lang.Exception { Scanner s = new Scanner(System.in); int n,m; n = s.nextInt(); m = s.nextInt(); long[][] mat = new long[m][n]; for(int i=0;i<m;i++){ for(int j=0;j<n;j++) mat[i][j] = s.nextLong(); } long[] cand_win = new long[n]; long[] city_win = new long[m]; long temp=0,index=0; for(int i1 = 0;i1<m;i1++){ index=0;temp=0; for(int j1 = 0;j1<n;j1++){ if(mat[i1][j1]>temp){ temp = mat[i1][j1]; index = j1; } } city_win[i1] = index; cand_win[(int)(index)]++; } // for(int b=0;b<n;b++) // System.out.print(cand_win[b]+" "); long max=0; for(int w=0;w<n;w++){ if(cand_win[w]>max){ max = cand_win[w]; index = w; } } System.out.println(index+1); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
8634bcb80010e05cdd953965d6233226
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.TreeMap; public class P570A { private void run() { int n = nextInt(); int m = nextInt(); Map<Integer, Integer> winnersOfCities = new HashMap<Integer, Integer>(); for (int i = 0; i < m; i++) { Integer index = -1; Integer max = -1; for (int j = 0; j < n; j++) { Integer value = nextInt(); if (value > max) { max = value; index = j; } } if (!winnersOfCities.containsKey(index)) { winnersOfCities.put(index, 0); } winnersOfCities.put(index, winnersOfCities.get(index) + 1); } Integer index = -1; Integer max = -1; for (Map.Entry<Integer, Integer> entry : winnersOfCities.entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); index = entry.getKey(); } else if (entry.getValue().intValue() == max.intValue() && entry.getKey() < index) { index = entry.getKey(); } } System.out.println(index + 1); } private char[] incrementWord(char[] input, char maxLetter) { int currentIndex = input.length - 1; while (currentIndex >= 0 && input[currentIndex] == maxLetter) { currentIndex--; } if (currentIndex < 0) { return input; } input[currentIndex] = (char) (input[currentIndex] + 1); for (int i = currentIndex + 1; i < input.length; i++) { input[i] = 'a'; } return input; } private int getFree(Integer currentFree, Map<Integer, Integer> count) { while (count.containsKey(currentFree)) { currentFree = currentFree + 1; } return currentFree; } private double computeArea(double side1, double side2, double side3) { double p = (side1 + side2 + side3) / 2d; return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3)); } private int greaterThan(List<Integer> indexesP2, Integer j) { for (int i = 0; i < indexesP2.size(); i++) { Integer number = indexesP2.get(i); if (number > j) { return indexesP2.size() - i; } } return 0; } public static void main(String[] args) { P570A notes = new P570A(); notes.run(); notes.close(); } private Scanner sc; private P570A() { this.sc = new Scanner(System.in); } private int[] asInteger(String[] values) { int[] ret = new int[values.length]; for (int i = 0; i < values.length; i++) { String val = values[i]; ret[i] = Integer.valueOf(val).intValue(); } return ret; } private String nextLine() { return sc.nextLine(); } private int nextInt() { return sc.nextInt(); } private String readLine() { if (sc.hasNextLine()) { return (sc.nextLine()); } else { return null; } } private void close() { try { sc.close(); } catch (Exception e) { //log } } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
d082d8d1c7a25b9f650a35b0489ed393
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.*; import java.io.*; public class Codeforces { public static void main(String[] args) throws IOException { Reader.init(System.in); int n = Reader.nextInt(), m = Reader.nextInt(); int arr[] = new int[n]; for(int i = 0; i < m; i++){ int index = -1, max = -1; for(int j = 0; j < n; j++){ int x = Reader.nextInt(); if(x > max){ max = x; index = j; } else if(x == max && j < index) index = j; } arr[index]++; } int max = -1, index = -1; for(int i = 0; i < n; i++){ if(arr[i] > max){ max = arr[i]; index = i; } } System.out.println(index+1); } } 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
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
b753bee70a4e4179849953e1e562fa02
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.IOException; import java.util.StringTokenizer; import java.util.HashMap; import java.util.Map; import java.lang.String; import java.lang.Math; import java.util.Arrays; import java.util.Comparator; public class Template { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } } class Node implements Comparable<Node>{ int index; int value; public Node(int index, int val) { this.index = index; this.value = val; } @Override public int compareTo(Node paramT) { if (this.value == paramT.value) { return this.index - paramT.index; } return paramT.value - this.value; } @Override public String toString() { return this.index + " " + this.value; } } class GaoComparator implements Comparator<Node> { @Override public int compare(Node paramT1, Node paramT2) { return paramT1.index - paramT2.index; } } class Task { public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(), m = in.nextInt(); Node[][] a = new Node[m][n]; for (int i = 0; i < m; ++i) { a[i] = new Node[n]; for (int j = 0; j < n; ++j) { a[i][j] = new Node(j+1, in.nextInt()); } Arrays.sort(a[i]); } int[] ind = new int[m]; for (int i = 0; i < m; ++i) { ind[i] = a[i][0].index; } Arrays.sort(ind); int winner = 0, wincnt = 0, curcnt = 0, current = ind[0]; for (int i = 0; i < m; ++i) { if (ind[i] == current) { ++curcnt; } else { if (curcnt > wincnt) { wincnt = curcnt; winner = current; } curcnt = 1; current = ind[i]; } } if (curcnt > wincnt) { winner = current; } out.println(winner); } } class InputReader { BufferedReader reader; StringTokenizer tokenize; InputReader(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenize = null; } public String next() { while (tokenize == null || !tokenize.hasMoreTokens()) { try{ tokenize = new StringTokenizer(reader.readLine()); }catch(IOException e) { throw new RuntimeException(e); } } return tokenize.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
f5a9c6e083c0fa22689c493204c05e90
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int candidate=scan.nextInt(); int city=scan.nextInt(); int[][] elections=new int[city][candidate]; int[] cityResult=new int[city]; for (int i = 0; i < elections.length; i++) { for (int j = 0; j < elections[i].length; j++) { elections[i][j]=scan.nextInt(); } } for (int i = 0; i < elections.length; i++) { int max=elections[i][0]; cityResult[i]=1; for (int j = 0; j < elections[i].length; j++) { if(elections[i][j]>max){ max=elections[i][j]; cityResult[i]=j+1; } } } int []finalResult=new int[candidate]; // second stage int winner=0; for (int i = 0; i < cityResult.length; i++) { finalResult[cityResult[i]-1]++; if(finalResult[cityResult[i]-1]>winner){ winner=finalResult[cityResult[i]-1]; } } // winner for (int i = 0; i < finalResult.length; i++) { if(finalResult[i]==winner){ System.out.println(i+1); break; } } } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
2e5b4442d6317994ac4cbc953130a00c
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.Scanner; public class Main { int x; public static void main(String[] args) { Scanner scan=new Scanner(System.in); int candidate=scan.nextInt(); int city=scan.nextInt(); int[][] elections=new int[city][candidate]; int[] cityResult=new int[city]; for (int i = 0; i < elections.length; i++) { for (int j = 0; j < elections[i].length; j++) { elections[i][j]=scan.nextInt(); } } for (int i = 0; i < elections.length; i++) { int max=elections[i][0]; cityResult[i]=1; for (int j = 0; j < elections[i].length; j++) { if(elections[i][j]>max){ max=elections[i][j]; cityResult[i]=j+1; } } } int []finalResult=new int[candidate]; // second stage int winner=0; for (int i = 0; i < cityResult.length; i++) { finalResult[cityResult[i]-1]++; if(finalResult[cityResult[i]-1]>winner){ winner=finalResult[cityResult[i]-1]; } } // winner for (int i = 0; i < finalResult.length; i++) { if(finalResult[i]==winner){ System.out.println(i+1); break; } } } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
86d6e43dc322473eadbba7ee43025e7e
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class A570 { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] input = bf.readLine().split(" "); int n = Integer.parseInt(input[0]); int m = Integer.parseInt(input[1]); int[] count = new int[n]; //processing for (int i = 0; i < m; i++) { input = bf.readLine().split(" "); int max = 0; int mem_index = 0; for (int j = 0; j < n; j++) { if (Integer.parseInt(input[j]) > max) { max = Integer.parseInt(input[j]); mem_index = j; } } count[mem_index]++; } //print winner int max_count = 0; int mem_winnerIndex = 0; for (int i = 0; i < n; i++) { if (count[i] > max_count) { max_count = count[i]; mem_winnerIndex = i; } } System.out.println(mem_winnerIndex+1); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
5d4e6953f9d689897e8ebf0acef8b5a5
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.*; public class votee { public static void main(String args[]) { Scanner e=new Scanner(System.in); int n=e.nextInt(); int m=e.nextInt(); int a[][]=new int[m][n]; int t,i,j; int b[]=new int[m]; int c[]=new int[n]; for(i=0;i<m;i++) { t=0; for(j=0;j<n;j++) { a[i][j]=e.nextInt(); if(a[i][j]>t) { t=a[i][j]; b[i]=j; } } } for(i=0;i<m;i++) { c[b[i]]+=1; } int p=0,f=0; for(i=0;i<n;i++) { if(c[i]>p) { f=i; p=c[i]; } //System.out.println(c[i]+" "); } System.out.println((f+1)); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
096653b17acbc386de5d2e3b0da7f8c3
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
/** * * @author Faruk */ import java.util.Arrays; import java.util.Scanner; import java.util.HashSet; import java.util.HashMap; import java.util.ArrayList; public class tmp { public static void main(String [] args){ Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int m = scan.nextInt(); int [] citywin = new int[n]; int tmp=0,max=-1,maxind=0; for(int i = 0; i < m; i++){ for(int j=0; j<n; j++){ tmp = scan.nextInt(); if(max < tmp){ max = tmp; maxind = j; } } max = -1; citywin[maxind] += 1; } int z = -1; for(int i=0; i<n; i++){ if(citywin[i] > z){ z = citywin[i]; maxind = i+1; } } System.out.println(maxind); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
7f584bf7fedaaf2eed0342f35fce9867
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.*; public class A { int b; public static void main(String[] args) { Scanner sc= new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] a = new int[m]; for (int i = 0; i < m; i++) { int max = -1; for (int j = 0; j < n; j++) { int t = sc.nextInt(); if (max < t) { max = t; a[i] = j; } } } int[] b = new int[n]; for (int i = 0; i < m; i++) { b[a[i]]++; } int max = 0; for (int i = 1; i < n; i++) { if (b[max] < b[i]) { max = i; } } System.out.println(max + 1); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
a6167c1c9095e2a94db250abbe735e98
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.Scanner; /** * * @author VMCUONG */ public class Problem570A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(), arr[] = s.split(" "); int n = Integer.parseInt(arr[0]), m = Integer.parseInt(arr[1]); int count[] = new int[n], win [] = new int[n]; for(; m-- > 0;){ arr = sc.nextLine().split(" "); for(int i = 0 ; i < n; i++) count[i] = new Integer(arr[i]); int best = 0; for( int i = 1; i < n ; i++) if(count[i]>count[best]) best = i; win[best]++; } int bestIndex = 0; for(int i = 1 ; i < n ; i++) if( win[i] > win[bestIndex]) bestIndex = i; System.out.println(bestIndex+1); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
bff58aa86541a29dbd84e40dd0755a07
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.lang.*; import java.util.Arrays; import java.util.Scanner; /** * * @author Mido */ public class Test { /** * @param args the command */ public static void main(String[] args) { int n,m,i,j,mx,ind = 0,ans=0; int x; int []votes=new int[100]; Scanner sc=new Scanner(System.in); n=sc.nextInt(); m=sc.nextInt(); for(i=0;i<m;i++) { mx=-1; for(j=0;j<n;j++) { x=sc.nextInt(); if(x>mx) { mx=x; ind=j; } } votes[ind]++; } mx=0; for(j=0;j<n;j++) { if(mx<votes[j]) { mx=votes[j]; ans=j; } } System.out.println(ans+1); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
3eac9198aaa3fde55d2eed51209ff693
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
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 A { 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(); int m = in.nextInt(); int[][] arr = new int[m][n]; for (int i=0; i<m; i++) { for (int j=0; j<n; j++) { arr[i][j] = in.nextInt(); } } int[] votes = new int[n]; for (int i=0; i<m; i++) { int most = arr[i][0]; int ind = 0; for (int j=1; j<n; j++) { if (arr[i][j] > most) { most = arr[i][j]; ind = j; } } votes[ind]++; } int most = votes[0]; int ind = 0; for (int i=1; i<n; i++) { if (votes[i] > most) { most = votes[i]; ind = i; } } out.println((ind + 1)); } public A() { 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 { A solver = new A(); solver.solve(); solver.end(); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
1572aa11b38584b7d59b0e27b3b1a193
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
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.StringTokenizer; public class R316A { public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int m = in.nextInt(); int[][] a = new int[m][n]; int[] c = new int[n]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { a[i][j] = in.nextInt(); } } for (int i = 0; i < a.length; i++) { int max = Integer.MIN_VALUE; int maxCand = -1; for (int j = 0; j < a[i].length; j++) { if (a[i][j] > max) { max = a[i][j]; maxCand = j; } } c[maxCand]++; // if (!map.containsKey(maxCand)) { // map.put(maxCand, 0); // } // map.put(maxCand, map.get(maxCand)+1); } int max = Integer.MIN_VALUE; int maxCand = -1; for (int i = 0; i < c.length; i++) { if (c[i] > max) { max = c[i]; maxCand = i; } } out.println(maxCand+1); out.flush(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
4161d668c5715a30e23b4392c6b87cd9
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class A_Elections { public static int winner(int[] candidates){ long max = 0; int candidate = -1; for(int i = 0; i < candidates.length; i++){ if(candidates[i] > max){ max = candidates[i]; candidate = i; } } return candidate; } public static void main(String args[]) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int[] candidates = new int[n]; int temp; long max; int candidate; for(int i = 0; i < m; i++){ st = new StringTokenizer(br.readLine()); max = -1; candidate = -1; for(int j = 0; j < n; j++){ temp = Integer.parseInt(st.nextToken()); if(temp > max){ max = temp; candidate = j; } } candidates[candidate]++; } System.out.println(winner(candidates)+1); br.close(); System.exit(0); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
9cfa07a4727068e352f58e5d17163d87
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.io.*; import java.util.*; public final class elections { static FastScanner sc=new FastScanner(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter out=new PrintWriter(System.out); public static void main(String args[]) throws Exception { int n=sc.nextInt(),m=sc.nextInt(); int[] res=new int[n]; for(int i=0;i<m;i++) { int curr_max=-1,curr_idx=-1; for(int j=0;j<n;j++) { int val=sc.nextInt(); if(val>curr_max) { curr_max=val; curr_idx=j; } } res[curr_idx]++; } int ans=-1,max=-1; for(int i=0;i<n;i++) { if(res[i]>max) { max=res[i]; ans=i; } } out.println(ans+1); 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 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
807aec3a8804efe62c4f6c36163a8b8c
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class A { public static void main(String[] args) { try (final Scanner sc = new Scanner(System.in)) { final int n = sc.nextInt(); final int m = sc.nextInt(); int[] voted = new int[n]; for(int i = 0; i < m ; i++){ int max = -1, max_pos = -1; for(int j = 0; j < n; j++){ final int a = sc.nextInt(); if(max < a){ max = a; max_pos = j; } } voted[max_pos]++; } int max = -1, max_pos = -1; for(int j = 0; j < n; j++){ if(max < voted[j]){ max = voted[j]; max_pos = j; } } System.out.println(max_pos + 1); } } public static class Scanner implements Closeable { private BufferedReader br; private StringTokenizer tok; public Scanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } private void getLine() { try { while (!hasNext()) { tok = new StringTokenizer(br.readLine()); } } catch (IOException e) { /* ignore */ } } private boolean hasNext() { return tok != null && tok.hasMoreTokens(); } public String next() { getLine(); return tok.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public void close() { try { br.close(); } catch (IOException e) { /* ignore */ } } } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
b7691344f70daac1d8670ea212b68a13
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.Scanner; public class _570A { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); short nrCandidates = scanner.nextShort(); short nrCities = scanner.nextShort(); short[] nrWins = new short[nrCandidates]; short maxCandidate; int maxVotes; int votes; for (short city = 0; city < nrCities; ++city) { maxCandidate = 0; maxVotes = scanner.nextInt(); for (short candidate = 1; candidate < nrCandidates; ++candidate) { votes = scanner.nextInt(); if (votes > maxVotes) { maxVotes = votes; maxCandidate = candidate; } } ++nrWins[maxCandidate]; } for (short candidate = 0; candidate < nrCandidates; ++candidate) System.err.print(" " + nrWins[candidate]); System.out.println(); maxCandidate = 0; int maxWins = nrWins[0]; for (short candidate = 1; candidate < nrCandidates; ++candidate) { if (nrWins[candidate] > maxWins) { maxWins = nrWins[candidate]; maxCandidate = candidate; } } System.out.println(maxCandidate + 1); } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
fae1385ae6c59d3e3b3c7792ad5a47e8
train_004.jsonl
1439483400
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.
256 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Test { private static int[][] input = new int[200][200]; public static void main(String[] args) { Scanner scan = new Scanner(System.in); int i , j , n , m , max = - 1 , ans = - 1; n = scan.nextInt(); m = scan.nextInt(); for (i = 1;i <= m;i ++) for (j = 1;j <= n;j ++) input[i][j] = scan.nextInt(); List<Integer> list = new ArrayList<Integer>(); for (i = 1;i <= m;i ++) list.add(getIndex(i , n)); Collections.sort(list); for (i = 0;i < list.size();i ++) { j = i; int count = 0; while (j < list.size() && (list.get(i) == list.get(j))) { j ++; count ++; } i = j - 1; if (count > max) { max = count; ans = list.get(i); } else if (count == max) { if (list.get(i) < ans) ans = list.get(i); } } System.out.println(ans); } private static int getIndex(int row , int m) { int i , max = - 1 , ans = - 1; for (i = 1;i <= m;i ++) { if (input[row][i] > max) { max = input[row][i]; ans = i; } } return ans; } }
Java
["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"]
1 second
["2", "1"]
NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
Java 7
standard input
[ "implementation" ]
b20e98f2ea0eb48f790dcc5dd39344d3
The first line of the input contains two integers n, m (1 ≀ n, m ≀ 100) β€” the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≀ j ≀ n, 1 ≀ i ≀ m, 0 ≀ aij ≀ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.
1,100
Print a single number β€” the index of the candidate who won the elections. The candidates are indexed starting from one.
standard output
PASSED
df3fc33bc91514df6aeb97ade398d9c6
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.util.Arrays; 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); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int[][] b = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { b[i][j] = in.nextInt(); } } int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { Arrays.fill(a[i], 1); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (b[i][j] == 0) { for (int k = 0; k < n; k++) { a[k][j] = 0; } for (int k = 0; k < m; k++) { a[i][k] = 0; } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (b[i][j] == 1) { boolean flag = false; for (int k = 0; k < n && !flag; k++) { if (a[k][j] == 1) flag = true; } for (int k = 0; k < m && !flag; k++) { if (a[i][k] == 1) flag = true; } if (!flag) { out.println("NO"); return; } } else { for (int k = 0; k < n; k++) { if (a[k][j] == 1) { out.println("NO"); return; } } for (int k = 0; k < m; k++) { if (a[i][k] == 1) { out.println("NO"); return; } } } } } out.println("YES"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { out.printf("%d ", a[i][j]); } out.println(); } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
c6355698bbd1808df4fa38aea9b19151
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.util.*; import java.io.*; public class B { public static PrintStream out = System.out; public static InputReader in = new InputReader(System.in); public static void main(String args[]) { int N, M; N = in.nextInt(); M = in.nextInt(); int[][] b = new int[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { b[i][j] = in.nextInt(); } } int[][] a = new int[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { boolean ok = true; for (int k = 0; k < N; k++) { if (b[k][j] != 1) ok = false; } for (int k = 0; k < M; k++) { if (b[i][k] != 1) ok = false; } if (ok) a[i][j] = 1; } } boolean yes = true; int[][] c = new int[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { boolean ok = false; for (int k = 0; k < N; k++) { if (a[k][j] == 1) ok = true; } for (int k = 0; k < M; k++) { if (a[i][k] == 1) ok = true; } if (ok) c[i][j] = 1; } } for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (c[i][j] != b[i][j]) { yes = false; } } } if (!yes) { out.println("NO"); } else { out.println("YES"); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (j != 0) out.print(" "); out.print(a[i][j]); } out.println(); } } } } 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 double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
01e7f838c64941014d56e1c19e858734
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; public class ORInMatrix { public static void main(String[] args) { BufferedReader br = null; br = new BufferedReader(new InputStreamReader(System.in)); try { int m, n; String[] temp; temp = br.readLine().split(" "); m = Integer.parseInt(temp[0]); n = Integer.parseInt(temp[1]); int i, j; int[][] matrixB = new int[m][n]; for (i = 0; i < m; i++) { temp = br.readLine().split(" "); for (j = 0; j < n; j++) { matrixB[i][j] = Integer.parseInt(temp[j]); } } int[] oneZeroRows = new int[m]; int[] oneZeroCols = new int[n]; for (i = 0; i < m; i++) { oneZeroRows[i] = 0; for (j = 0; j < n; j++) { if (matrixB[i][j] == 0) { oneZeroRows[i] = 1; break; } } } for (j = 0; j < n; j++) { oneZeroCols[j] = 0; for (i = 0; i < m; i++) { if (matrixB[i][j] == 0) { oneZeroCols[j] = 1; break; } } } int[][] guessMatrixA = new int[m][n]; for (i = 0; i < m; i++) for (j = 0; j < n; j++) if (oneZeroRows[i] == 1 || oneZeroCols[j] == 1) { guessMatrixA[i][j] = 0; } else { guessMatrixA[i][j] = 1; } int[] oneOneRows = new int[m]; int[] oneOneCols = new int[n]; for (i = 0; i < m; i++) { oneOneRows[i] = 0; for (j = 0; j < n; j++) { if (guessMatrixA[i][j] == 1) { oneOneRows[i] = 1; break; } } } for (j = 0; j < n; j++) { oneOneCols[j] = 0; for (i = 0; i < m; i++) { if (guessMatrixA[i][j] == 1) { oneOneCols[j] = 1; break; } } } int guessMatrixB[][] = new int[m][n]; for (i = 0; i < m; i++) for (j = 0; j < n; j++) if (oneOneRows[i] == 1 || oneOneCols[j] == 1) { guessMatrixB[i][j] = 1; } else { guessMatrixB[i][j] = 0; } boolean correct = true; for (i = 0; i < m; i++) for (j = 0; j < n; j++) if (matrixB[i][j] != guessMatrixB[i][j]) { correct = false; break; } if (!correct) { System.out.println("NO"); } else { System.out.println("YES"); for (i = 0; i < m; i++) { System.out.print(guessMatrixA[i][0]); for (j = 1; j < n; j++) System.out.print(" " + guessMatrixA[i][j]); System.out.println(); } } } catch (Exception e) { e.printStackTrace(); } } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
7f3c7bb5a54ce7fc3400cb74dce2a473
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.io.*; import java.util.*; public class B { void solve() throws IOException { in = new InputReader("__std"); out = new OutputWriter("__std"); int n = in.readInt(); int m = in.readInt(); int[][] b = new int[n][m]; int[][] a = new int[n][m]; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { b[i][j] = in.readInt(); a[i][j] = 1; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (b[i][j] == 0) { for (int k = 0; k < n; ++k) { a[k][j] = 0; } for (int k = 0; k < m; ++k) { a[i][k] = 0; } } } } boolean flag = true; for (int i = 0; i < n && flag; ++i) { for (int j = 0; j < m && flag; ++j) { if (b[i][j] == 1) { flag = false; for (int k = 0; k < n && !flag; ++k) { flag = a[k][j] == 1; } for (int k = 0; k < m && !flag; ++k) { flag = a[i][k] == 1; } } } } if (flag) { out.println("YES"); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { out.print(a[i][j] + " "); } out.println(); } } else { out.println("NO"); } exit(); } void exit() { //System.err.println((System.currentTimeMillis() - startTime) + " ms"); out.close(); System.exit(0); } InputReader in; OutputWriter out; //long startTime = System.currentTimeMillis(); public static void main(String[] args) throws IOException { new B().solve(); } class InputReader { private InputStream stream; private byte[] buffer = new byte[1024]; private int pos, len; private int cur; private StringBuilder sb = new StringBuilder(32); InputReader(String name) throws IOException { if (name.equals("__std")) { stream = System.in; } else { stream = new FileInputStream(name); } cur = read(); } private int read() throws IOException { if (len == -1) { throw new EOFException(); } if (pos >= len) { pos = 0; len = stream.read(buffer); if (len == -1) return -1; } return buffer[pos++]; } char readChar() throws IOException { if (cur == -1) { throw new EOFException(); } char res = (char) cur; cur = read(); return res; } int readInt() throws IOException { if (cur == -1) { throw new EOFException(); } while (whitespace()) { cur = read(); } if (cur == -1) { throw new EOFException(); } int sign = 1; if (cur == '-') { sign = -1; cur = read(); } int res = 0; while (!whitespace()) { if (cur < '0' || cur > '9') { throw new NumberFormatException(); } res *= 10; res += cur - '0'; cur = read(); } return res * sign; } long readLong() throws IOException { if (cur == -1) { throw new EOFException(); } return Long.parseLong(readToken()); } double readDouble() throws IOException { if (cur == -1) { throw new EOFException(); } return Double.parseDouble(readToken()); } String readLine() throws IOException { if (cur == -1) { throw new EOFException(); } sb.setLength(0); while (cur != -1 && cur != '\r' && cur != '\n') { sb.append((char) cur); cur = read(); } if (cur == '\r') { cur = read(); } if (cur == '\n') { cur = read(); } return sb.toString(); } String readToken() throws IOException { if (cur == -1) { throw new EOFException(); } while (whitespace()) { cur = read(); } if (cur == -1) { throw new EOFException(); } sb.setLength(0); while (!whitespace()) { sb.append((char) cur); cur = read(); } return sb.toString(); } boolean whitespace() { return cur == ' ' || cur == '\t' || cur == '\r' || cur == '\n' || cur == -1; } boolean eof() { return cur == -1; } } class OutputWriter { private PrintWriter writer; OutputWriter(String name) throws IOException { if (name.equals("__std")) { writer = new PrintWriter(System.out); } else { writer = new PrintWriter(name); } } void print(String format, Object ... args) { writer.print(new Formatter(Locale.US).format(format, args)); } void println(String format, Object ... args) { writer.println(new Formatter(Locale.US).format(format, args)); } void print(Object value) { writer.print(value); } void println(Object value) { writer.println(value); } void println() { writer.println(); } void close() { writer.close(); } } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
1c27a20442755cb56b383f95528a2256
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.util.*; import java.io.*; public class ORInMatrix486B { static void go() { int m = in.nextInt(); int n = in.nextInt(); boolean[] rAllZero = new boolean[m]; boolean[] cAllZero = new boolean[n]; int countR = 0; int countC = 0; int[][] A = new int[m][n]; for (int i = 0; i < m; i++) A[i] = in.nextIntArray(n); for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { if (A[i][j] == 0) { if (!rAllZero[i]) { rAllZero[i] = true; countR++; } if (!cAllZero[j]) { cAllZero[j] = true; countC++; } } } if (countR == m) { for (int i = 0; i < n; i++) cAllZero[i] = true; } if (countC == n) { for (int i = 0; i < m; i++) rAllZero[i] = true; } for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { if (A[i][j] == 1 && rAllZero[i] && cAllZero[j]) { out.println("NO"); return; } } for (int i = 0; i < m; i++) { Arrays.fill(A[i], rAllZero[i] ? 0 : 1); } for (int i = 0; i < n; i++) { if (cAllZero[i]) { for (int j = 0; j < m; j++) A[j][i] = 0; } } out.println("YES"); for (int[] row : A) { for (int x : row) out.print(x + " "); out.println(); } } static InputReader in; static PrintWriter out; public static void main(String[] args) { in = new InputReader(System.in); out = new PrintWriter(System.out); go(); out.close(); } 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 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[] nextIntArray(int len) { int[] ret = new int[len]; for (int i = 0; i < len; i++) ret[i] = nextInt(); return ret; } public long[] nextLongArray(int len) { long[] ret = new long[len]; for (int i = 0; i < len; i++) ret[i] = nextLong(); return ret; } public int nextInt() { return (int) nextLong(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder sb = new StringBuilder(1024); do { sb.append((char) c); c = read(); } while (!isSpaceChar(c)); return sb.toString(); } public static boolean isSpaceChar(int c) { switch (c) { case -1: case ' ': case '\n': case '\r': case '\t': return true; default: return false; } } } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
b13e757a2de4139035017170d150fca8
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.util.*; import java.io.*; public class ORInMatrix486B { static void go() { int m = in.nextInt(); int n = in.nextInt(); boolean[] rAllZero = new boolean[m]; boolean[] cAllZero = new boolean[n]; int countR = 0; int countC = 0; boolean allZero = false; int[][] A = new int[m][n]; for (int i = 0; i < m; i++) A[i] = in.nextIntArray(n); for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { if (A[i][j] == 0) { if (!rAllZero[i]) { rAllZero[i] = true; countR++; } if (!cAllZero[j]) { cAllZero[j] = true; countC++; } } } if (countR == m || countC == n) { allZero = true; } for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { if (A[i][j] == 1 && ((rAllZero[i] && cAllZero[j]) || allZero)) { out.println("NO"); return; } } for (int i = 0; i < m; i++) { Arrays.fill(A[i], rAllZero[i] ? 0 : 1); } for (int i = 0; i < n; i++) { if (cAllZero[i]) { for (int j = 0; j < m; j++) A[j][i] = 0; } } out.println("YES"); for (int[] row : A) { for (int x : row) out.print(x + " "); out.println(); } } static InputReader in; static PrintWriter out; public static void main(String[] args) { in = new InputReader(System.in); out = new PrintWriter(System.out); go(); out.close(); } 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 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[] nextIntArray(int len) { int[] ret = new int[len]; for (int i = 0; i < len; i++) ret[i] = nextInt(); return ret; } public long[] nextLongArray(int len) { long[] ret = new long[len]; for (int i = 0; i < len; i++) ret[i] = nextLong(); return ret; } public int nextInt() { return (int) nextLong(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder sb = new StringBuilder(1024); do { sb.append((char) c); c = read(); } while (!isSpaceChar(c)); return sb.toString(); } public static boolean isSpaceChar(int c) { switch (c) { case -1: case ' ': case '\n': case '\r': case '\t': return true; default: return false; } } } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
c4ae0f41af222d1c151e46088bc790ab
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.math.BigInteger; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); int[][] a=new int[n][m]; int[] row=new int[n]; int[] col=new int[m]; boolean zero=true; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j]=sc.nextInt(); row[i]+=a[i][j]; col[j]+=a[i][j]; if(a[i][j]!=0)zero=false; } } boolean f=false; boolean ff=false; boolean ffc=false; for (int i = 0; i < n; i++) { if(row[i]==m&&!ff)ff=true; for (int j = 0; j < m; j++) { if(col[j]==n&&!ffc)ffc=true; if(a[i][j]==1) { if(row[i]!=m&&col[j]!=n) {f=true; break; } } } if(f) break; } if(f||(!ff||!ffc)&&!zero) { System.out.println("NO"); } else { int num=0; System.out.println("YES"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if(row[i]==m&&col[j]==n) a[i][j]=1; else { a[i][j]=0; } System.out.print((j==m-1)?a[i][j]+"":a[i][j]+" "); } System.out.println(""); } } } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
a395c7a697d20169c14bae7e95af1478
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.util.*; import java.io.*; public class Main { static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws Exception { new Main(); out.flush(); } Scanner scan = new Scanner(System.in); Main() throws Exception { int m = scan.nextInt(); int n = scan.nextInt(); boolean[][] bm = new boolean[m][n]; for (int i = 0;i < m ;i++){ for (int j = 0 ; j< n ; j++){ if (scan.nextInt() == 1){ bm[i][j] = true; } } } boolean[][] am = new boolean[m][n]; ArrayList<Integer> ml = new ArrayList<>(); ArrayList<Integer> nl = new ArrayList<>(); for (int i = 0; i < m; i++){ boolean ok = true; for (int j = 0; j < n;j++){ if (!bm[i][j]){ ok = false; } } if (ok){ for (int j = 0; j < n;j++){ am[i][j] = true; } ml.add(i); } } for (int i = 0; i < n; i++){ boolean ok = true; for (int j = 0; j < m;j++){ if (!bm[j][i]){ ok = false; } } if (ok){ for (int j = 0; j < m;j++){ am[j][i] = true; } nl.add(i); } } // for (boolean[] e: am){ // px(e); // } HashSet<Integer> s1 = new HashSet<>(); for (int e : ml){ for (int f: nl){ s1.add(100 * e + f ); } } boolean ok = true; for (int i = 0 ;i < m; i++){ if (!Arrays.equals(bm[i], am[i])){ ok = false; } } if (nl.size() > 0 && ml.size() == 0){ ok = false; } if (nl.size() == 0 && ml.size() > 0){ ok = false; } if (ok){ out.println("YES"); for (int i = 0; i < m; i++){ for (int j = 0; j < n;j ++){ if (j != 0) out.print(' '); int k = 100 * i + j; if (s1.contains(k) ){ out.print('1'); } else { out.print('0'); } } out.println(); } } else { out.println("NO"); } } static void px(Object...objects){ System.out.println(Arrays.deepToString(objects)); } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
3cf6218f8f91c71d05a6963da95a8e00
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.util.Scanner; public class B { public static void main(String args[]){ Scanner scan = new Scanner(System.in); int n,m; m=scan.nextInt(); n=scan.nextInt(); int B[][] = new int[m][n]; for (int i=0;i<m;i++){ for (int j=0;j<n;j++){ B[i][j]=scan.nextInt(); } } int A[][]=new int[m][n]; for (int i=0;i<m;i++){ for (int j=0;j<n;j++){ A[i][j]=1; } } for (int i=0;i<m;i++){ for (int j=0;j<n;j++){ if(B[i][j]==0){ for(int k=0;k<n;k++){ A[i][k]=0; } for(int l=0;l<m;l++){ A[l][j]=0; } } } } boolean flag=true; exit: for (int i=0;i<m;i++){ for (int j=0;j<n;j++){ if(B[i][j]==1){ flag=false; for(int k=0;k<n;k++){ if(A[i][k]==1){flag=true;} } for(int l=0;l<m;l++){ if(A[l][j]==1){flag=true;} } } if (B[i][j]!=0&!flag){System.out.println("NO"); break exit;} } } if(flag){ System.out.println("YES"); for (int z=0;z<m;z++){ for (int x=0;x<n;x++){ System.out.print(A[z][x]+" "); } System.out.println(); } } } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
bfb4e5cba935cdb573bfbdc4faf892ac
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class OrInMatrix { static short m,n; public static void main(String[] args) throws IOException { BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(bf.readLine()); m=Short.parseShort(st.nextToken()); n=Short.parseShort(st.nextToken()); short[][] arr=new short[m][n]; short[][] output=new short[m][n]; for (int i = 0; i < m; i++) { st=new StringTokenizer(bf.readLine()); int j=0; while(st.hasMoreTokens()){ arr[i][j]=Short.parseShort(st.nextToken()); output[i][j]=1; ++j; } } int raws=0,cols=0; for (int i = 0; i < m; i++) { for (int j = 0; j <n; j++) { if(arr[i][j]==1){ if(!checkCol(arr,j)){ if(!checkRaw(arr, i)){ System.out.println("NO");return; } else ++raws; } else ++cols; } else{ makeZero(output,i,j); } } }//System.out.println(raws+" "+cols); if(raws==0 && cols!= m*n && cols!=0 || cols==0 && raws!=0){ System.out.println("NO");return; } System.out.println("YES"); String result=""; for (int i = 0; i < output.length; i++) { for (int j = 0; j < n; j++) { result+=output[i][j]+" "; } result+="\n"; } System.out.println(result); } public static boolean checkCol(short [][] arr,int col){ for (int i = 0; i < m; i++) if(arr[i][col]==0) return false; return true; } public static boolean checkRaw(short [][] arr,int raw){ for (int i = 0; i < n; i++) if(arr[raw][i]==0) return false; return true; } public static void makeZero(short[][] out,int i,int j){ for (int j2 = 0; j2 < out.length; j2++) { out[j2][j]=0; } for (int j2 = 0; j2 < n; j2++) { out[i][j2]=0; } } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
097dec3eed2bb9cc0d875cd3cb8c394a
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.io.*; public class Task486B { public static void main(String... args) throws NumberFormatException, IOException { Solution.main(System.in, System.out); } static class Scanner { private final BufferedReader br; private String[] cache; private int cacheIndex; Scanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); cache = new String[0]; cacheIndex = 0; } int nextInt() throws IOException { if (cacheIndex >= cache.length) { cache = br.readLine().split(" "); cacheIndex = 0; } return Integer.parseInt(cache[cacheIndex++]); } long nextLong() throws IOException { if (cacheIndex >= cache.length) { cache = br.readLine().split(" "); cacheIndex = 0; } return Long.parseLong(cache[cacheIndex++]); } String next() throws IOException { if (cacheIndex >= cache.length) { cache = br.readLine().split(" "); cacheIndex = 0; } return cache[cacheIndex++]; } void close() throws IOException { br.close(); } } static class Solution { public static void main(InputStream is, OutputStream os) throws NumberFormatException, IOException { PrintWriter pw = new PrintWriter(os); Scanner sc = new Scanner(is); int m = sc.nextInt(); int n = sc.nextInt(); int[][] b = new int[m][]; int[][] aCand = new int[m][]; for (int i = 0; i < m; i++) { b[i] = new int[n]; aCand[i] = new int[n]; } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { b[i][j] = sc.nextInt(); } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { aCand[i][j] = 1; for (int k = 0; k < m && aCand[i][j] == 1; k++) { if (b[k][j] == 0) { aCand[i][j] = 0; } } for (int k = 0; k < n && aCand[i][j] == 1; k++) { if (b[i][k] == 0) { aCand[i][j] = 0; } } } } boolean retVal = true; for (int i = 0; i < m && retVal; i++) { for (int j = 0; j < n && retVal; j++) { int newB = 0; for (int k = 0; k < m && newB == 0; k++) { if (aCand[k][j] == 1) { newB = 1; } } for (int k = 0; k < n && newB == 0; k++) { if (aCand[i][k] == 1) { newB = 1; } } if (newB != b[i][j]) { retVal = false; } } } if (retVal) { pw.println("YES"); for (int i = 0; i < m; i++) { for(int j = 0 ; j < n ; j++){ pw.print(aCand[i][j]); pw.print(" "); } pw.println(); } } else { pw.println("NO"); } pw.flush(); sc.close(); } } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
718fa93df632e1ad94b6831c406eaee9
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.text.*; public class Main { public static void main(String[] args)throws java.lang.Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(System.out); int n,m,i,j,v,k; boolean found,foundr0,foundc0; StringTokenizer st1=new StringTokenizer(in.readLine()); m=Integer.parseInt(st1.nextToken()); n=Integer.parseInt(st1.nextToken()); int[][] a=new int[m][n]; int[][] b=new int[m][n]; int[][] c=new int[m][n]; for(i=0;i<m;i++) { StringTokenizer st2=new StringTokenizer(in.readLine()); for(j=0;j<n;j++) { a[i][j]=Integer.parseInt(st2.nextToken()); } } found=false; for(i=0;i<m;i++) { for(j=0;j<n;j++) { v=a[i][j]; if(v==1) { foundr0=false; foundc0=false; for(k=0;k<m;k++) { if(k!=i) { if(a[k][j]==0) foundr0=true; } } for(k=0;k<n;k++) { if(k!=j) { if(a[i][k]==0) foundc0=true; } } if(foundr0 && foundc0) { found=true; } else { if(!foundr0 && !foundc0) b[i][j]=1; else b[i][j]=0; } } else { b[i][j]=0; } } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { for(k=0;k<m;k++) { if(b[k][j]==1) c[i][j]=1; } for(k=0;k<n;k++) { if(b[i][k]==1) c[i][j]=1; } } } /* for(i=0;i<m;i++) { out.print(c[i][0]); for(j=1;j<n;j++) { out.print(" "+c[i][j]); } out.println(); } */ for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(c[i][j]!=a[i][j]) found=true; } } if(found) { out.println("NO"); } else { out.println("YES"); for(i=0;i<m;i++) { out.print(b[i][0]); for(j=1;j<n;j++) { out.print(" "+b[i][j]); } out.println(); } } out.flush(); out.close(); } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
7c35daea1f67fb48a0025e745ec17a4f
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.lang.*; import java.io.*; import java.util.*; public class Matrix { public static void main(String[] args) throws java.lang.Exception { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(in, out); out.close(); } } class TaskA { public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(), m = in.nextInt(); int[][] b = new int[n][m]; int[][] a = new int[n][m]; int[][] d = new int[n][m]; int[] rs = new int[n]; int[] cs = new int[m]; int i, j, k; for (i=0; i<n; ++i) { for (j=0; j<m; ++j) { b[i][j] = in.nextInt(); a[i][j] = 1; } } for (i=0; i<n; ++i) { for (j=0; j<m; ++j) { if (b[i][j] == 0) { for (k=0; k<m; ++k) a[i][k] = 0; for (k=0; k<n; ++k) a[k][j] = 0; } } } for (i=0; i<n; ++i) { for (j=0; j<m; ++j) { rs[i] |= a[i][j]; cs[j] |= a[i][j]; } } for (i=0; i<n; ++i) { for (j=0; j<m; ++j) { d[i][j] = rs[i] | cs[j]; if (d[i][j] != b[i][j]) { out.println("NO"); return ; } } } out.println("YES"); for (i=0; i<n; ++i) { for (j=0; j<m; ++j) { out.print(a[i][j] + " "); } out.println(); } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer==null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
4b2df13e477449fc859a7eed53158b52
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String ar[]) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int arr[][] = new int[n][m]; int res[][] = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = in.nextInt(); res[i][j] = -1; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (arr[i][j] != 0) continue; for (int k = 0; k < m; k++) { res[i][k] = 0; } for (int k = 0; k < n; k++) { res[k][j] = 0; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (arr[i][j] != 1) continue; boolean done = false; for (int k = 0; k < m; k++) { if (res[i][k] == 0) continue; done = true; res[i][k] = 1; } for (int k = 0; k < n; k++) { if (res[k][j] == 0) continue; done = true; res[k][j] = 1; } if (!done) { System.out.println("NO"); return; } } } System.out.println("YES"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { System.out.print(res[i][j] + " "); } System.out.println(); } } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
e28c99acd8467eaab80602aa9da5056f
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.util.Scanner; public class ans { public static void change(int b[][], int i, int k){ for(int r=0;r<b.length;r++){ b[r][k]=0; } for(int c=0;c<b[0].length;c++){ b[i][c]=0; } } public static boolean check(int b[][], int i, int j){ boolean flag= false; for(int r=0;r<b.length;r++){ if(b[r][j]==-1) { b[r][j]=1; flag=true; } if(b[r][j]==1) flag=true; } for(int c=0;c<b[0].length;c++){ if(b[i][c]==-1) { b[i][c]=1; flag=true; } if(b[i][c]==1) flag = true; } return flag; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner s= new Scanner (System.in); int row= s.nextInt(); int col= s.nextInt(); int a[][]= new int [row][col]; int b[][]= new int [row][col]; for(int i=0;i<row;i++){ for(int k=0;k<col;k++){ a[i][k]=s.nextInt(); b[i][k]=-1; } } for(int i=0;i<row;i++){ for(int k=0;k<col;k++){ if(a[i][k]==0){ change(b,i,k); } } } boolean brek=false; for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ if(a[i][j]==1){ boolean ans=check(b, i, j); if(ans==false){ System.out.println("NO"); brek=true; break; } } } if(brek==true) break; } if(brek==false){ System.out.println("YES"); for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ System.out.print(b[i][j]+" "); } System.out.println(); } } } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output
PASSED
9b4ae54f39ed9f0ee671f32b650c339d
train_004.jsonl
1415718000
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≀ i ≀ m) and column j (1 ≀ j ≀ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
256 megabytes
import java.io.*; import java.util.*; public class Solution { private static void solve(InputReader in, OutputWriter out) { int n, m; n = in.nextInt(); m = in.nextInt(); Set<Integer> h = new HashSet<Integer>(); Set<Integer> v = new HashSet<Integer>(); int[][] a = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { a[i][j] = in.nextInt(); if (a[i][j] == 0) { h.add(i); v.add(j); } } Set<Integer> hhas = new HashSet<Integer>(); Set<Integer> vhas = new HashSet<Integer>(); Set<Integer> hset = new HashSet<Integer>(); Set<Integer> vset = new HashSet<Integer>(); int[][] b = new int[n][m]; boolean error = false; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) if (a[i][j] == 1) { if (h.contains(i) && v.contains(j)) { error = true; break; } else if (!h.contains(i) && !v.contains(j)) { b[i][j] = 1; hset.add(i); vset.add(j); } else { if (h.contains(i)) vhas.add(j); else hhas.add(i); } } if (error) break; } hhas.removeAll(hset); vhas.removeAll(vset); if (error || hhas.size() > 0 || vhas.size() > 0) out.print("NO"); else { out.println("YES"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) out.print(b[i][j] + " "); out.print('\n'); } } } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); solve(in, out); in.close(); out.close(); } private static class InputReader { private BufferedReader br; private StringTokenizer st; InputReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); st = null; } String nextLine() { String line = null; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } String next() { while (st == null || !st.hasMoreTokens()) { String line = nextLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } private static class OutputWriter { BufferedWriter bw; OutputWriter(OutputStream os) { bw = new BufferedWriter(new OutputStreamWriter(os)); } void print(int i) { print(Integer.toString(i)); } void println(int i) { println(Integer.toString(i)); } void print(long l) { print(Long.toString(l)); } void println(long l) { println(Long.toString(l)); } void print(double d) { print(Double.toString(d)); } void println(double d) { println(Double.toString(d)); } void print(boolean b) { print(Boolean.toString(b)); } void println(boolean b) { println(Boolean.toString(b)); } void print(char c) { try { bw.write(c); } catch (IOException e) { e.printStackTrace(); } } void println(char c) { println(Character.toString(c)); } void print(String s) { try { bw.write(s); } catch (IOException e) { e.printStackTrace(); } } void println(String s) { print(s); print('\n'); } void close() { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Java
["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"]
1 second
["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"]
null
Java 7
standard input
[ "implementation", "hashing", "greedy" ]
bacd613dc8a91cee8bef87b787e878ca
The first line contains two integer m and n (1 ≀ m, n ≀ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
1,300
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
standard output