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 | 9933be7885e905b5cd8f874639e0eb95 | train_002.jsonl | 1469804400 | You are given n points on the straight line β the positions (x-coordinates) of the cities and m points on the same line β the positions (x-coordinates) of the cellular towers. All towers work in the same way β they provide cellular network for all cities, which are located at the distance which is no more than r from this tower.Your task is to find minimal r that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than r.If rβ=β0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than r from this tower. | 256 megabytes | import java.util.Scanner;
public class Q4 {
int[] cities;
int[] cells;
public void solver() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
cities = new int[n];
cells = new int[m];
for (int i = 0; i < n; i++) {
cities[i] = sc.nextInt();
}
for (int i = 0; i < m; i++) {
cells[i] = sc.nextInt();
}
long l = 0, r = (long) (2 * Math.pow(10, 9));
while (l + 1 < r) {
long mid = l + (r - l) / 2;
if (check(mid)) {
r = mid;
} else {
l = mid;
}
}
if (check(l)) {
System.out.println(l);
} else {
System.out.println(r);
}
}
boolean check(long len) {
int cell = 0;
for (int i = 0; i < cities.length; i++) {
if (cell >= cells.length) return false;
if (Math.abs(cells[cell] - cities[i]) <= len) {
continue;
} else {
cell++;
i--;
}
}
return true;
}
public static void main(String[] args) {
Q4 test = new Q4();
test.solver();
}
}
| Java | ["3 2\n-2 2 4\n-3 0", "5 3\n1 5 10 14 17\n4 11 15"] | 3 seconds | ["4", "3"] | null | Java 8 | standard input | [
"two pointers",
"binary search",
"implementation"
] | 9fd8e75cb441dc809b1b2c48c4012c76 | The first line contains two positive integers n and m (1ββ€βn,βmββ€β105) β the number of cities and the number of cellular towers. The second line contains a sequence of n integers a1,βa2,β...,βan (β-β109ββ€βaiββ€β109) β the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates ai are given in non-decreasing order. The third line contains a sequence of m integers b1,βb2,β...,βbm (β-β109ββ€βbjββ€β109) β the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates bj are given in non-decreasing order. | 1,500 | Print minimal r so that each city will be covered by cellular network. | standard output | |
PASSED | a4e664af161019a1cb476f4396ccfafc | train_002.jsonl | 1469804400 | You are given n points on the straight line β the positions (x-coordinates) of the cities and m points on the same line β the positions (x-coordinates) of the cellular towers. All towers work in the same way β they provide cellular network for all cities, which are located at the distance which is no more than r from this tower.Your task is to find minimal r that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than r.If rβ=β0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than r from this tower. | 256 megabytes | import java.util.Scanner;
public class Q4 {
int[] cities;
int[] cells;
public void solver() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
cities = new int[n];
cells = new int[m];
for (int i = 0; i < n; i++) {
cities[i] = sc.nextInt();
}
for (int i = 0; i < m; i++) {
cells[i] = sc.nextInt();
}
long l = 0, r = Math.max(cities[n - 1], cells[m - 1]) - Math.min(cells[0], cities[0]);
while (l + 1 < r) {
long mid = l + (r - l) / 2;
if (check(mid)) {
r = mid;
} else {
l = mid;
}
}
if (check(l)) {
System.out.println(l);
} else {
System.out.println(r);
}
}
boolean check(long len) {
int cell = 0;
for (int i = 0; i < cities.length; i++) {
if (cell >= cells.length) return false;
if (Math.abs(cells[cell] - cities[i]) <= len) {
continue;
} else {
cell++;
i--;
}
}
return true;
}
public static void main(String[] args) {
Q4 test = new Q4();
test.solver();
}
}
| Java | ["3 2\n-2 2 4\n-3 0", "5 3\n1 5 10 14 17\n4 11 15"] | 3 seconds | ["4", "3"] | null | Java 8 | standard input | [
"two pointers",
"binary search",
"implementation"
] | 9fd8e75cb441dc809b1b2c48c4012c76 | The first line contains two positive integers n and m (1ββ€βn,βmββ€β105) β the number of cities and the number of cellular towers. The second line contains a sequence of n integers a1,βa2,β...,βan (β-β109ββ€βaiββ€β109) β the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates ai are given in non-decreasing order. The third line contains a sequence of m integers b1,βb2,β...,βbm (β-β109ββ€βbjββ€β109) β the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates bj are given in non-decreasing order. | 1,500 | Print minimal r so that each city will be covered by cellular network. | standard output | |
PASSED | ed53e9dabd9dd5f1a384c87cc53a5ab0 | train_002.jsonl | 1469804400 | You are given n points on the straight line β the positions (x-coordinates) of the cities and m points on the same line β the positions (x-coordinates) of the cellular towers. All towers work in the same way β they provide cellular network for all cities, which are located at the distance which is no more than r from this tower.Your task is to find minimal r that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than r.If rβ=β0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than r from this tower. | 256 megabytes | import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
/**
* Created by Khamid_Sarmanov on 3/1/2016.
*/
public class Main {
public static int search(int value, int[] a) {
int lo = 0;
int hi = a.length - 1;
int lastValue = 0;
while (lo <= hi) {
int mid = (lo + hi) / 2;
lastValue = a[mid];
if (value < lastValue) {
hi = mid - 1;
} else if (value > lastValue) {
lo = mid + 1;
} else {
return lastValue;
}
}
return lastValue;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
HashSet<Integer> aSet = new HashSet<Integer>();
Integer[] bSet = new Integer[m];
TreeSet<Integer> vals = new TreeSet<Integer>();
for (int i = 0; i < n; i++) {
aSet.add(in.nextInt());
}
for (int i = 0; i < m; i++) {
bSet[i] = in.nextInt();
}
Arrays.sort(bSet);
Iterator<Integer> iteratorA = aSet.iterator();
while (iteratorA.hasNext()) {
int key = iteratorA.next();
TreeSet<Integer> difSet = new TreeSet<Integer>();
// Iterator<Integer> iteratorB = bSet.iterator();
int a = Arrays.binarySearch(bSet, key);
if (a < 0) {
a += 1;
a = Math.abs(a);
if (a >= bSet.length) {
a--;
}
if (a > 0 && Math.abs(bSet[a - 1] - key) < Math.abs(bSet[a] - key)) {
a--;
}
}
vals.add(Math.abs(bSet[a] - key));
// System.out.println(bSet[a] + " key " + key + " diff " + Math.abs(bSet[a] - key));
// if ()
// while (iteratorB.hasNext()) {
// int bValue = iteratorB.next();
// int start = Math.min(bValue, key);
// int end = Math.max(bValue, key);
// int dif = Math.abs(start - end);
// difSet.add(dif);
// }
// vals.add(difSet.first());
}
System.out.println(vals.last());
}
} | Java | ["3 2\n-2 2 4\n-3 0", "5 3\n1 5 10 14 17\n4 11 15"] | 3 seconds | ["4", "3"] | null | Java 8 | standard input | [
"two pointers",
"binary search",
"implementation"
] | 9fd8e75cb441dc809b1b2c48c4012c76 | The first line contains two positive integers n and m (1ββ€βn,βmββ€β105) β the number of cities and the number of cellular towers. The second line contains a sequence of n integers a1,βa2,β...,βan (β-β109ββ€βaiββ€β109) β the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates ai are given in non-decreasing order. The third line contains a sequence of m integers b1,βb2,β...,βbm (β-β109ββ€βbjββ€β109) β the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates bj are given in non-decreasing order. | 1,500 | Print minimal r so that each city will be covered by cellular network. | standard output | |
PASSED | 971e2a37c546d09e0b3a05b50346cdf4 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.You are given $$$n$$$ segments on the coordinate axis $$$OX$$$. Segments can intersect, lie inside each other and even coincide. The $$$i$$$-th segment is $$$[l_i; r_i]$$$ ($$$l_i \le r_i$$$) and it covers all integer points $$$j$$$ such that $$$l_i \le j \le r_i$$$.The integer point is called bad if it is covered by strictly more than $$$k$$$ segments.Your task is to remove the minimum number of segments so that there are no bad points at all. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Jaynil
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskD2 solver = new TaskD2();
solver.solve(1, in, out);
out.close();
}
static class TaskD2 {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int k = in.nextInt();
ArrayList<int[]> ll = new ArrayList<>();
ArrayList<int[]> rr = new ArrayList<>();
for (int i = 0; i < n; i++) {
ll.add(new int[]{in.nextInt(), in.nextInt(), i});
}
Collections.sort(ll, (x, y) -> x[0] - y[0]);
int l = 0;
int r = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>((x, y) -> y[0] - x[0]);
PriorityQueue<int[]> q = new PriorityQueue<>((x, y) -> x[0] - y[0]);
int count = 0;
HashSet<Integer> ans = new HashSet<>();
int j = 0;
for (int i = 1; i <= 200000; i++) {
while (j < n && ll.get(j)[0] <= i) {
pq.add(new int[]{ll.get(j)[1], ll.get(j)[2]});
q.add(new int[]{ll.get(j)[1], ll.get(j)[2]});
j++;
count++;
}
while (q.size() > 0 && q.peek()[0] < i) {
int temp[] = q.poll();
if (!ans.contains(temp[1])) {
count--;
}
}
while (count > k) {
int temp[] = pq.poll();
ans.add(temp[1]);
count--;
}
}
// out.println(count);
out.println(ans.size());
for (int x : ans) out.print((x + 1) + " ");
out.println();
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["7 2\n11 11\n9 11\n7 8\n8 9\n7 8\n9 11\n7 9", "5 1\n29 30\n30 30\n29 29\n28 30\n30 30", "6 1\n2 3\n3 3\n2 3\n2 2\n2 3\n2 3"] | 1 second | ["3\n1 4 7", "3\n1 2 4", "4\n1 3 5 6"] | null | Java 11 | standard input | [
"greedy"
] | 7f9c5a137e9304d4d7eee5ee1a891d1d | The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 200$$$) β the number of segments and the maximum number of segments by which each integer point can be covered. The next $$$n$$$ lines contain segments. The $$$i$$$-th line contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le 200$$$) β the endpoints of the $$$i$$$-th segment. | 1,800 | In the first line print one integer $$$m$$$ ($$$0 \le m \le n$$$) β the minimum number of segments you need to remove so that there are no bad points. In the second line print $$$m$$$ distinct integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i \le n$$$) β indices of segments you remove in any order. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 48eb169c25190b3b6a65d51178943f7c | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.You are given $$$n$$$ segments on the coordinate axis $$$OX$$$. Segments can intersect, lie inside each other and even coincide. The $$$i$$$-th segment is $$$[l_i; r_i]$$$ ($$$l_i \le r_i$$$) and it covers all integer points $$$j$$$ such that $$$l_i \le j \le r_i$$$.The integer point is called bad if it is covered by strictly more than $$$k$$$ segments.Your task is to remove the minimum number of segments so that there are no bad points at all. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
public class TooManySegments {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
static int n, k;
static pair[] seg;
static int N = 200002;
static pair pair(int x, int y) {
return new pair(x, y);
}
static class pair {
int x;
int y;
public pair(int x, int y) {
super();
this.x = x;
this.y = y;
}
public boolean equals(Object obj) {
if(obj == null || obj.getClass()!= this.getClass()) return false;
pair p = (pair) obj;
return (this.x==p.x && this.y==p.y);
}
public int hashCode() {
return Objects.hash(x,y);
}
}
static void process() throws Exception {
Map<Integer, List<Integer>> ev = new HashMap<>();
int[] cnt = new int[N];
for (int i = 0; i < n; i++) {
cnt[seg[i].x]++;
cnt[seg[i].y + 1]--;
int idx = i;
ev.compute(seg[i].x, (k, v) -> {
if (v == null) {
v = new ArrayList<>();
}
v.add(idx + 1);
return v;
});
ev.compute(seg[i].y + 1, (k, v) -> {
if (v == null) {
v = new ArrayList<>();
}
v.add(-idx - 1);
return v;
});
}
for (int i = 1; i < N; i++) {
cnt[i] = cnt[i] + cnt[i - 1];
}
int[] ans = new int[n], sub = new int[N];
PriorityQueue<pair> curSegs = new PriorityQueue<>((p1, p2) -> p2.y - p1.y);
int curSeg = 0;
for (int i = 0; i < N; i++) {
curSeg += sub[i];
for (int it: ev.getOrDefault(i, Collections.emptyList())) {
if (it > 0) {
curSegs.add(pair(it - 1, seg[it - 1].y));
} else {
curSegs.remove(pair(-it - 1, seg[-it - 1].y));
}
}
while (cnt[i] - curSeg > k) {
pair p = curSegs.poll();
++curSeg;
--sub[p.y + 1];
ans[p.x] = 1;
}
}
int count = 0;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < n; i++) {
if (ans[i] == 1) {
builder.append(String.format("%d ", i + 1));
count++;
}
}
writer.write(String.format("%d", count));
writer.newLine();
writer.write(builder.toString());
writer.newLine();
}
public static void main(String[] args) throws Exception {
// writer = new BufferedWriter(new FileWriter("output.txt"));
String data[], input;
data = reader.readLine().split(" ");
n = Integer.parseInt(data[0]); k = Integer.parseInt(data[1]);
seg = new pair[n];
for (int i = 0; i < n; i++) {
data = reader.readLine().split(" ");
seg[i] = pair(Integer.parseInt(data[0]) - 1, Integer.parseInt(data[1]) - 1);
}
process();
writer.close();
}
}
| Java | ["7 2\n11 11\n9 11\n7 8\n8 9\n7 8\n9 11\n7 9", "5 1\n29 30\n30 30\n29 29\n28 30\n30 30", "6 1\n2 3\n3 3\n2 3\n2 2\n2 3\n2 3"] | 1 second | ["3\n1 4 7", "3\n1 2 4", "4\n1 3 5 6"] | null | Java 11 | standard input | [
"greedy"
] | 7f9c5a137e9304d4d7eee5ee1a891d1d | The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 200$$$) β the number of segments and the maximum number of segments by which each integer point can be covered. The next $$$n$$$ lines contain segments. The $$$i$$$-th line contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le 200$$$) β the endpoints of the $$$i$$$-th segment. | 1,800 | In the first line print one integer $$$m$$$ ($$$0 \le m \le n$$$) β the minimum number of segments you need to remove so that there are no bad points. In the second line print $$$m$$$ distinct integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i \le n$$$) β indices of segments you remove in any order. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 85607182f4fb05894420e68fa5995562 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.You are given $$$n$$$ segments on the coordinate axis $$$OX$$$. Segments can intersect, lie inside each other and even coincide. The $$$i$$$-th segment is $$$[l_i; r_i]$$$ ($$$l_i \le r_i$$$) and it covers all integer points $$$j$$$ such that $$$l_i \le j \le r_i$$$.The integer point is called bad if it is covered by strictly more than $$$k$$$ segments.Your task is to remove the minimum number of segments so that there are no bad points at all. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class Main {
void run() throws IOException {
int n = nextInt();
int k = nextInt();
int[][] a = new int[n][2];
TreeSet<Event> ts = new TreeSet<>(new EventComp());
for (int i = 0; i < n; i++) {
a[i][0] = nextInt();
a[i][1] = nextInt();
ts.add(new Event(a[i][0], 0, i));
ts.add(new Event(a[i][1], 1, i));
}
int count = 0;
Stack<Integer> ans = new Stack<>();
TreeMap<Integer, TreeSet<Integer>> max_last = new TreeMap<>();
TreeSet<Integer> del = new TreeSet<>();
for (Event e : ts) {
if (del.contains(e.num)) continue;
if (e.type == 0) {
count++;
if (!max_last.containsKey(a[e.num][1])) {
max_last.put(a[e.num][1], new TreeSet<>());
}
max_last.get(a[e.num][1]).add(e.num);
if (count > k) {
int t = max_last.lastKey();
ans.add(max_last.get(t).first());
del.add(ans.peek());
max_last.get(t).remove(max_last.get(t).first());
if (max_last.get(t).size() == 0) max_last.remove(t);
count--;
}
} else {
max_last.get(e.pos).remove(e.num);
if (max_last.get(e.pos).size() == 0) max_last.remove(e.pos);
count--;
}
}
pw.println(ans.size());
while (!ans.isEmpty()) {
pw.print((ans.pop() + 1) + " ");
}
pw.close();
}
class Event {
int pos, type, num;
public Event(int a, int b, int c) {
pos = a;
type = b;
num = c;
}
}
class EventComp implements Comparator<Event> {
@Override
public int compare(Event o1, Event o2) {
if (o1.pos == o2.pos && o1.type == o2.type) return Integer.compare(o1.num, o2.num);
if (o1.pos == o2.pos) return Integer.compare(o1.type, o2.type);
return Integer.compare(o1.pos, o2.pos);
}
}
long mod = (long) 1e9 + 7;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader br = new BufferedReader(new FileReader("qual.in"));
StringTokenizer st = new StringTokenizer("");
PrintWriter pw = new PrintWriter(System.out);
//PrintWriter pw = new PrintWriter("qual.out");
int nextInt() throws IOException {
return Integer.parseInt(next());
}
String next() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public Main() throws FileNotFoundException {
}
public static void main(String[] args) throws IOException {
new Main().run();
}
} | Java | ["7 2\n11 11\n9 11\n7 8\n8 9\n7 8\n9 11\n7 9", "5 1\n29 30\n30 30\n29 29\n28 30\n30 30", "6 1\n2 3\n3 3\n2 3\n2 2\n2 3\n2 3"] | 1 second | ["3\n1 4 7", "3\n1 2 4", "4\n1 3 5 6"] | null | Java 11 | standard input | [
"greedy"
] | 7f9c5a137e9304d4d7eee5ee1a891d1d | The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 200$$$) β the number of segments and the maximum number of segments by which each integer point can be covered. The next $$$n$$$ lines contain segments. The $$$i$$$-th line contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le 200$$$) β the endpoints of the $$$i$$$-th segment. | 1,800 | In the first line print one integer $$$m$$$ ($$$0 \le m \le n$$$) β the minimum number of segments you need to remove so that there are no bad points. In the second line print $$$m$$$ distinct integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i \le n$$$) β indices of segments you remove in any order. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 4a843d566121b095e64ad90eafbacccc | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.You are given $$$n$$$ segments on the coordinate axis $$$OX$$$. Segments can intersect, lie inside each other and even coincide. The $$$i$$$-th segment is $$$[l_i; r_i]$$$ ($$$l_i \le r_i$$$) and it covers all integer points $$$j$$$ such that $$$l_i \le j \le r_i$$$.The integer point is called bad if it is covered by strictly more than $$$k$$$ segments.Your task is to remove the minimum number of segments so that there are no bad points at all. | 256 megabytes | import java.io.*;
import java.math.*;
import java.text.DecimalFormat;
import java.util.*;
public class MainClass
{
InputStream in;
PrintWriter out;
long mod=(long)1e9+7;
int MAX=(int)2e5+7;
double eps=1e-6;
String high="";
void solve()
{
int n = ni();
int k = ni();
int []cnt = new int[MAX];
ArrayList<Pair> seg = new ArrayList<Pair>(MAX);
ArrayList<ArrayList<Integer>> ev = new ArrayList<ArrayList<Integer>>(MAX);
for(int i = 0; i < MAX; i++)
ev.add(new ArrayList<Integer>());
seg.add(new Pair(-1, -1));
for(int i = 1; i <= n; i++) {
int li = ni();
int ri = ni();
seg.add(new Pair(li, ri));
cnt[li]++;
cnt[ri + 1]--;
ev.get(li).add(i);
ev.get(ri + 1).add(-i);
}
for(int i = 1; i < MAX; i++)
cnt[i] += cnt[i - 1];
int curSub = 0;
int []sub = new int[MAX];
int []ans = new int[MAX];
TreeSet<Pair> curSegs = new TreeSet<Pair>();
for(int i = 1; i < MAX; i++) {
curSub += sub[i];
for(int it : ev.get(i)) {
if(it > 0) {
curSegs.add(new Pair(seg.get(it).y, it));
} else {
curSegs.remove(new Pair(seg.get(-it).y, -it));
}
}
while(cnt[i] - curSub > k) {
if(curSegs.isEmpty()) break;
int pos = curSegs.last().y;
curSegs.remove(curSegs.last());
curSub++;
--sub[seg.get(pos).y + 1];
ans[pos] = 1;
}
}
out.println(Arrays.stream(ans).sum());
for(int i = 1; i < MAX; i++) {
if(ans[i] > 0)
out.print(i + " ");
}
}
double dist(double x1,double y1,double x2,double y2)
{
return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}
class Pair implements Comparable<Pair>
{
int x;
int y;
Pair(int a,int b)
{
x=a;
y=b;
}
@Override
public boolean equals(Object o)
{
Pair other = (Pair)o;
return x == other.x && y == other.y;
}
public String toString()
{
return x+" "+y;
}
@Override
public int compareTo(Pair o)
{
if (x==o.x)
return Integer.compare(y, o.y);
else
return Integer.compare(x, o.x);
}
@Override public int hashCode()
{
return Objects.hash(x, y);
}
}
long pow(long x, long n, long M) {
x%=M;
long result = 1;
while (n > 0) {
if (n % 2 == 1)
result = (result * x) % M;
x = (x * x) % M;
n = n / 2;
}
return result;
}
long modInverse(long A, long M) {
extendedEuclid(A, M);
return (EEx % M + M) % M;
}
long EEd, EEx, EEy;
void extendedEuclid(long A, long B) {
if (B == 0) {
EEd = A;
EEx = 1;
EEy = 0;
} else {
extendedEuclid(B, A % B);
long temp = EEx;
EEx = EEy;
EEy = temp - (A / B) * EEy;
}
}
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int min(int a,int b)
{
if(a>b)
return b;
else
return a;
}
long max(long a,long b)
{
if(a>b)
return a;
else
return b;
}
long min(long a,long b)
{
if(a>b)
return b;
else
return a;
}
long add(long a,long b)
{
long x=(a+b);
while(x>=mod) x-=mod;
return x;
}
long sub(long a,long b)
{
long x=(a-b);
while(x<0) x+=mod;
return x;
}
long mul(long a,long b)
{
a%=mod;
b%=mod;
long x=(a*b);
return x%mod;
}
void run() throws Exception
{
String INPUT = "C:/Users/tapan/OneDrive/Desktop/in.txt";
// oj=false;
in = oj ? System.in : new FileInputStream(INPUT);
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception
{
new MainClass().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf)
{
ptrbuf = 0;
try
{
lenbuf = in.read(inbuf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean inSpaceChar(int c)
{
return !(c >= 33 && c <= 126);
}
private int skip()
{
int b;
while ((b = readByte()) != -1 && inSpaceChar(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 (!(inSpaceChar(b)))
{ // when nextLine, (inSpaceChar(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 && !(inSpaceChar(b)))
{
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-')
{
minus = true;
b = readByte();
}
while (true)
{
if (b >= '0' && b <= '9')
{
num = num * 10 + (b - '0');
}
else
{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-')
{
minus = true;
b = readByte();
}
while (true)
{
if (b >= '0' && b <= '9')
{
num = num * 10 + (b - '0');
}
else
{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o)
{
if (!oj)
System.out.println(Arrays.deepToString(o));
}
} | Java | ["7 2\n11 11\n9 11\n7 8\n8 9\n7 8\n9 11\n7 9", "5 1\n29 30\n30 30\n29 29\n28 30\n30 30", "6 1\n2 3\n3 3\n2 3\n2 2\n2 3\n2 3"] | 1 second | ["3\n1 4 7", "3\n1 2 4", "4\n1 3 5 6"] | null | Java 11 | standard input | [
"greedy"
] | 7f9c5a137e9304d4d7eee5ee1a891d1d | The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 200$$$) β the number of segments and the maximum number of segments by which each integer point can be covered. The next $$$n$$$ lines contain segments. The $$$i$$$-th line contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le 200$$$) β the endpoints of the $$$i$$$-th segment. | 1,800 | In the first line print one integer $$$m$$$ ($$$0 \le m \le n$$$) β the minimum number of segments you need to remove so that there are no bad points. In the second line print $$$m$$$ distinct integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i \le n$$$) β indices of segments you remove in any order. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 095fab93b6db038bbe78e22841abcf72 | train_002.jsonl | 1571754900 | The only difference between easy and hard versions is constraints.You are given $$$n$$$ segments on the coordinate axis $$$OX$$$. Segments can intersect, lie inside each other and even coincide. The $$$i$$$-th segment is $$$[l_i; r_i]$$$ ($$$l_i \le r_i$$$) and it covers all integer points $$$j$$$ such that $$$l_i \le j \le r_i$$$.The integer point is called bad if it is covered by strictly more than $$$k$$$ segments.Your task is to remove the minimum number of segments so that there are no bad points at all. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class Main {
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 k = Integer.parseInt(st.nextToken());
ArrayList<Segment> segments = new ArrayList<>();
int[] count = new int[200001];
for (int i=0; i<n; i++) {
st = new StringTokenizer(br.readLine());
segments.add(new Segment(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), i));
for (int j=segments.get(segments.size()-1).getLeft(); j<=segments.get(segments.size()-1).getRight(); j++) {
count[j]++;
}
}
Collections.sort(segments, new java.util.Comparator<Segment>() {
public int compare(Segment a, Segment b) {
if (a.getRight()>b.getRight()) {
return -1;
}
if (a.getRight()<b.getRight()) {
return 1;
}
if (a.getLeft()<b.getLeft()) {
return -1;
}
if (a.getLeft()>b.getLeft()) {
return 1;
}
if (a.getIndex()<b.getIndex()) {
return -1;
}
return 1;
}
});
ArrayList<Integer> done = new ArrayList<>();
boolean[] marked = new boolean[n];
for (int i=0; i<count.length; i++) {
if (count[i]>k) {
// System.out.println("i = " + i);
int num = 0; int initial = count[i];
for (int j=0; j<segments.size(); j++) {
if (!marked[j] && segments.get(j).getLeft()<=i && segments.get(j).getRight()>=i) {
marked[j] = true;
num++;
done.add(j);
// System.out.println("removed segment " + (segments[j][2]+1));
for (int l=segments.get(j).getLeft(); l<=segments.get(j).getRight(); l++) {
count[l]--;
}
}
if (num==initial-k) {
break;
}
}
}
}
System.out.println(done.size());
for (int i=0; i<done.size(); i++) {
System.out.print((segments.get(done.get(i)).getIndex()+1)+" ");
}
}
}
class Segment {
private int left;
private int right;
private int index;
public Segment(int left, int right, int index) {
this.left = left;
this.right = right;
this.index = index;
}
public int getLeft() {
return left;
}
public int getRight() {
return right;
}
public int getIndex() {
return index;
}
}
| Java | ["7 2\n11 11\n9 11\n7 8\n8 9\n7 8\n9 11\n7 9", "5 1\n29 30\n30 30\n29 29\n28 30\n30 30", "6 1\n2 3\n3 3\n2 3\n2 2\n2 3\n2 3"] | 1 second | ["3\n1 4 7", "3\n1 2 4", "4\n1 3 5 6"] | null | Java 11 | standard input | [
"greedy"
] | 7f9c5a137e9304d4d7eee5ee1a891d1d | The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 200$$$) β the number of segments and the maximum number of segments by which each integer point can be covered. The next $$$n$$$ lines contain segments. The $$$i$$$-th line contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le 200$$$) β the endpoints of the $$$i$$$-th segment. | 1,800 | In the first line print one integer $$$m$$$ ($$$0 \le m \le n$$$) β the minimum number of segments you need to remove so that there are no bad points. In the second line print $$$m$$$ distinct integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i \le n$$$) β indices of segments you remove in any order. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 9e20e6ff1dc7d4c19bf555496ddf63ab | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.Scanner;
public class Night {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int f=sc.nextInt();
int compt=0;
for(int i=0;i<n;i++)
for(int j=0;j<f;j++)
if((sc.nextInt()+sc.nextInt())>0)
compt++;
System.out.println(compt);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 66d9ff0df8693bbcebb1c8b9f5a7eade | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.*;
public class hello {
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int a[][] = new int[105][205];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 2 * m; ++j) {
a[i][j] = scan.nextInt();
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (a[i][2 * j] == 1 || a[i][2 * j + 1] == 1)
ans++;
}
}
System.out.println(ans);
}
} | Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | deb6db1994d1272966f6536026e01728 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.Scanner;
public class VitalityAndNight {
public static void main(String[] args){
Scanner stdin = new Scanner(System.in);
int n = stdin.nextInt();
int m = stdin.nextInt();
int[][] windows = new int[n][m*2];
int[][] flats = new int[n][m*2];
for(int i = 0; i < n; i++){
for(int j = 0; j < m*2; j++){
windows[i][j] = stdin.nextInt();
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m*2; j++){
if(windows[i][j] == 1){
flats[i][j/2] = 1;
}
}
}
int lights = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m*2; j++){
if(flats[i][j] == 1){
lights++;
}
}
}
System.out.println(lights);
}
} | Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 0d250c56f93f40ef660a554689b90f54 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String []args)
{
Scanner s=new Scanner (System.in);
int n=s.nextInt(),m=s.nextInt();
int arr[][] = new int[n][m*2+1];
for (int i=0;i<n;i++)
for (int j=0;j<2*m;j++)
arr[i][j]=s.nextInt();
int ans=0;
for (int i=0;i<n;i++)
{
for (int j=0;j+1<2*m;j+=2)
{
if (arr[i][j]==1 || arr[i][j+1]==1)
{
ans++;
}
}
}
System.out.println(ans);
}} | Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | f3ffcb21a69cec3a06b8ede571eca317 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.io.*;
import java.util.*;
public class vitaly {
public static void main(String[] args) throws Exception{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(f.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int counter = 0;
for(int i = 0;i<N;i++){
st = new StringTokenizer(f.readLine());
for(int j = 0;j<M;j++){
int w1 = Integer.parseInt(st.nextToken());
int w2 = Integer.parseInt(st.nextToken());
if(w1 == 1 || w2 ==1) counter ++;
}
}
System.out.println(counter);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 2283ca55c87e5efe301b31e0b7f81247 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int awake = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
boolean temp1 = in.nextInt() == 1;
boolean temp2 = in.nextInt() == 1;
if (temp1||temp2) {
awake++;
}
}
}
System.out.println(awake);
}
} | Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 0c8abad9659bc7d8d8202b6a9ff6c78d | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Anusuya on 11/8/2015.
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
String[] s = bufferRead.readLine().split(" ");
int nFloors = Integer.parseInt(s[0]);
int nHome = Integer.parseInt(s[1]);
List<Integer> lights = new ArrayList<Integer>();
for(int i=0; i<nFloors; i++) {
String[] line = bufferRead.readLine().split(" ");
for(int j=0; j<line.length; j++) {
lights.add(Integer.parseInt(line[j]));
}
}
int count =0;
int i=0;
while(i<lights.size()) {
int sum = lights.get(i) + lights.get(i+1);
if(sum >0) {
count++;
}
i = i+2;
}
System.out.println(count);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 939728465a738f837fb5a3be00baed8a | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.Scanner;
public class CF_597A {
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a,b,n,i,j=0,k=0,count=0;
a=in.nextInt();
b=in.nextInt();
for(i=0;i<a;i++)
{
for(int m=0;m<b*2;m++)
{
n=in.nextInt();
j++;
if(n==1)
{
k++;
}
if(j==2)
{
if(k!=0)
{
count++;
}
j=0;
k=0;
}
}
}
System.out.println(count);
}
} | Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 2eec2534d7ec832ca51c65d4465d8a31 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.Scanner;
/**
*
* @author ingysoft
*/
public class BasicMain {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
String split[] = s.split(" ");
int n = Integer.parseInt(split[0]);
int m = Integer.parseInt(split[1]);
int count = 0;
for(int i = 0; i < n; i++)
{
s = scanner.nextLine();
split = s.split(" ");
for(int j = 0; j < m; j++)
{
if(2*j < split.length && 2*j + 1 < split.length)
{
if("1".equals(split[2*j]) || "1".equals(split[2*j+1]))
{
count++;
}
}
}
}
System.out.println(count);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 59a1a932a91094e836ccd83783b28d8a | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.Scanner;
public class VitalyAndNight {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int count = 0;
for(int i = 0; i < n ; i++) {
int arr[] = new int[2 * m];
for(int j = 0; j < 2 * m; j++) {
arr[j] = sc.nextInt();
}
for(int j = 1; j < 2 * m; j += 2) {
if(arr[j] != 0 || arr[j - 1] != 0) {
count++;
}
}
}
System.out.println(count);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 8feec722f2e82a1700f5a0fd6ec570e9 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Program {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
static final int Mod = 1000000007;
static final double inf = 10000000000.0;
void solve() throws IOException {
int n, m, ans = 0;
n = nextInt();
m = nextInt();
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
{
int x1, x2;
x1 = nextInt();
x2 = nextInt();
if (x1 == 1 || x2 == 1)
ans++;
}
out.println(ans);
}
Program() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new Program();
}
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | ee7918f529fd80417dee00000513ce8d | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.Scanner;
public class A
{
public void solve()
{
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int m = cin.nextInt();
int[][] a = new int[n][m * 2];
for(int i = 0;i < n;i++)
{
for(int j = 0;j < m * 2;j++)
{
a[i][j] = cin.nextInt();
}
}
int count = 0;
for(int i = 0;i < n;i++)
{
for(int j = 0;j < m;j++)
{
if(a[i][j * 2] == 1 || a[i][j * 2 + 1] == 1)
{
count++;
}
}
}
System.out.println(count);
}
public static void main(String[] args)
{
new A().solve();
}
} | Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | bd36664464efdbeac6038dbb9f4a5133 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class A_Div2_330
{
public static void main(String[]arg) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int i,m,n,j;
StringTokenizer st = new StringTokenizer(in.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
int ans = 0;
String a,b;
for(i = 0; i < n; i++)
{
st = new StringTokenizer(in.readLine());
for(j = 0; j < m; j++)
{
a = st.nextToken();
b = st.nextToken();
if(a.equals("1") || b.equals("1"))
ans++;
}
}
System.out.println(ans);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 7054d98287ec5071e138f63a122459f2 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class A {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter out;
public static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) throws IOException {
int res = 0;
int n = nextInt(), m = nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int t1 = nextInt();
int t2 = nextInt();
if ((t1 | t2) == 1)
res++;
}
}
// output
System.out.println(res);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | c6a24cd9f3099824a597f55e92a6a257 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class A {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter out;
public static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) throws IOException {
int res = 0;
int n = nextInt(), m = nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int t1 = nextInt();
int t2 = nextInt();
if ((t1 | t2) == 1)
res++;
}
}
// output
System.out.println(res);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 0c5a112cb1eba91003f85db309fe1909 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.*;
public class a595
{
public static void main(String[] args) {
Scanner ob=new Scanner(System.in);
int n=ob.nextInt();
int m=ob.nextInt();
int ans=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
int a=ob.nextInt();
int b=ob.nextInt();
if(a==1 || b==1)
ans++;
}
}
System.out.println(ans);
}
} | Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 3e45ed6f53686c626fd548bc01e8a44e | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.io.*;
public class VitalyAndNight {
public static void main (String[] args) {
try {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(in);
int lineNum = 0;
String str;
int n = 0;
int m = 0;
if (lineNum == 0 && (str = input.readLine()) != null) {
String[] numbers = str.split(" ");
n = Integer.parseInt(numbers[0]);
m = Integer.parseInt(numbers[1]);
lineNum++;
}
int count = 0;
while (lineNum <= n && (str = input.readLine()) != null) {
for (int i = 0; i < 4 * m; i += 4) {
if (str.charAt(i) == '1' || str.charAt(i + 2) == '1') {
count++;
}
}
lineNum++;
}
System.out.print(count);
} catch (IOException io) {
io.printStackTrace();
}
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 1a9087c94d6b4bebb6a6d5f49f34c793 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.Scanner;
public class Codeforces {
public static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int ans = 0;
int[] nm = inputInts();
int n = nm[0];
int m = nm[1];
for (int i = 0; i < n; i++) {
int[] lights = inputInts();
for (int j = 0; j < m; j++) {
ans += Math.max(lights[2*j], lights[2*j+1]);
}
}
System.out.println(ans);
}
public static int[] inputInts() {
String[] strs = in.nextLine().split(" ");
int[] ints = new int[strs.length];
for (int i = 0; i < strs.length; i++) {
ints[i] = Integer.parseInt(strs[i]);
}
return ints;
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 28363576efd3bba31b3a8f34dea0a8b7 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes |
import java.util.Scanner;
public class Cf_study_again {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int m=s.nextInt();
int count=0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int a=s.nextInt();int b=s.nextInt();
if(a==1 || b==1){
count++;
}
}
}
System.out.println(count);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | f67e12ffba4ba7c494bace1e6d32d8e0 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int m = s.nextInt();
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2 * m; j++) {
int t = s.nextInt();
if (t == 1) {
count++;
if (i == n - 1 && j == 2 * m - 1) break;
if (j % 2 == 0) {
j++;
s.nextInt();
}
}
}
}
System.out.println(count);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 0253a91b5c39f3d6a16ddaa74bc91d0a | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.io.*;
import java.util.StringTokenizer;
/**
* Created by peacefrog on 11/8/15.
* Time : 10:33 PM
*/
public class Task_A {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
PrintWriter out;
long timeBegin, timeEnd;
public void runIO() throws IOException {
timeBegin = System.currentTimeMillis();
InputStream inputStream;
OutputStream outputStream;
if (ONLINE_JUDGE) {
inputStream = System.in;
Reader.init(inputStream);
outputStream = System.out;
out = new PrintWriter(outputStream);
} else {
inputStream = new FileInputStream("/home/peacefrog/Dropbox/IdeaProjects/Problem Solving/input");
Reader.init(inputStream);
out = new PrintWriter("output.txt");
}
solve();
out.flush();
out.close();
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
/*
* Start Solution Here
*/
private void solve() throws IOException {
int n = Reader.nextInt(); //This Variable default in Code Templete
int m = Reader.nextInt();
int [][] flat = new int[n][2*m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2*m; j++) {
flat[i][j] = Reader.nextInt();
}
}
int count = 0 ;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2 * m; j+=2) {
if(flat[i][j] == 1 || flat[i][j+1]==1 ) count++;
}
}
out.println(count);
}
public static void main(String[] args) throws IOException {
new Task_A().runIO();
}
static 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() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
static String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 9981b40feadd245d162c2caec43b0f09 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.Scanner;
public class a {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int c = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
boolean a = in.nextInt() == 1;
boolean b = in.nextInt() == 1;
if(a || b)
c++;
}
}
System.out.println(c);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 74580ef4245984eae258292211742ff7 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.Scanner ;
public class main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner s = new Scanner(System.in) ;
int n = s.nextInt() , m = s.nextInt() ;
int ar [][] = new int[n][2*m] ;
for(int i = 0 ; i < ar.length ; i++)
{ for(int j = 0 ; j < ar[i].length ;j++)
{ ar[i][j] = s.nextInt() ;
}
}
int counter = 0 ;
for(int i = 0 ; i < ar.length ; i++)
{ for(int j = 0 ; j < ar[i].length ;j+=2)
{ if (ar[i][j] == 1 || ar[i][j+1] == 1)
counter++;
}
}
System.out.println(counter);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | f2ac877680e7b35043d54d629433a4f9 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.*;
public class Codef{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int n=in.nextInt(),m=in.nextInt(),ans=0;
int[][] ar=new int[n][m*2];
for (int i=0; i<n; i++)
for (int j=0; j<m*2; j++)
ar[i][j]=in.nextInt();
for (int i=0; i<n; i++)
for (int j=0; j<m; j++){
if ((ar[i][j*2]==1)|(ar[i][j*2+1]==1)) ans++;
}
System.out.println(ans);
}
} | Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | cac8cb961af977d3aa542a0aa302a301 | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class VitalyAndNight {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] ss = bf.readLine().split(" ");
int n = Integer.parseInt(ss[0]);
int m = Integer.parseInt(ss[1]);
int ans = 0;
for(int i=1;i<=n;i++){
ss = bf.readLine().split(" ");
for(int j=1;j<=2*m;j=j+2){
int odd = Integer.parseInt(ss[j-1]);
int even = Integer.parseInt(ss[j]);
if(odd==1 || even==1){
ans++;
}
}
}
System.out.println(ans);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | d9135da9cc48fa5d9e321197382fee5a | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.util.Scanner;
public class VitalyandNight {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int temp1 =sc.nextInt(),temp2=sc.nextInt();
if(temp1 == 1 || temp2==1)
count++;
}
}
System.out.println(count);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | b945e286009f0dbfc66a5be477c05d5e | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Oleksii Sosevych (alexey_sosevich@ukr.net)
*/
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);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2 * m; j += 2) {
int a = in.nextInt();
int b = in.nextInt();
if (a == 1 || b == 1) {
count++;
}
}
}
out.println(count);
}
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 3dd5f78c2ff2853dcd1148a3b32ab98d | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class CF595A {
public static void main(String[] args)throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for(String ln;(ln=in.readLine())!=null;){
StringTokenizer st = new StringTokenizer(ln);
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int count = 0;
for(int i=0;i<n;++i){
st=new StringTokenizer(in.readLine());
for(int j=0;j<2*m;j+=2){
String s = st.nextToken();
if(s.equals("1")| st.nextToken().equals("1"))
count++;
}
}
System.out.println(count);
}
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | 99355bab60b4d8e7073812ed84ce673f | train_002.jsonl | 1447000200 | One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2Β·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2Β·m from left to right, then the j-th flat of the i-th floor has windows 2Β·jβ-β1 and 2Β·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping. | 256 megabytes |
//package solution;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Solution implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
final boolean OJ = System.getProperty("ONLINE_JUDGE") != null;
void init() throws FileNotFoundException {
if (OJ) {
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");
}
}
public void run() {
try {
init();
solve();
out.close();
} catch (Exception e) {
e.printStackTrace();
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());
}
public static void main(String[] args) {
new Thread(null, new Solution(), "", 256 * 1l << 20).start();
}
void solve() throws IOException {
int n, m;
n = readInt();
m = readInt();
int[][] a = new int[n][2 * m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2 * m; j++) {
a[i][j] = readInt();
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2 * m; j += 2) {
if (a[i][j] == 1 || a[i][j + 1] == 1) {
ans++;
}
}
}
out.print(ans);
}
}
| Java | ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"] | 1 second | ["3", "2"] | NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off. | Java 7 | standard input | [
"constructive algorithms",
"implementation"
] | 5b9aed235094de7de36247a3b2a34e0f | The first line of the input contains two integers n and m (1ββ€βn,βmββ€β100)Β β the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2Β·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'. | 800 | Print a single integerΒ β the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping. | standard output | |
PASSED | f0715e4cf2ab9fd9089f79a9290d278c | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
static int nextInt() throws Exception {
in.nextToken();
return (int) in.nval;
}
static int[] cnt = new int[1111111];
public static void main(String[] args) throws Exception {
int n = nextInt();
int m = nextInt();
for (int i = 1; i <= 2 * m; i++) {
int tmp = nextInt();
cnt[tmp]++;
}
long ans = (long) n * (n - 1) * (n - 2) / 3;
for (int i = 1; i <= n; i++)
ans -= cnt[i] * (n - cnt[i] - 1);
System.out.println(ans / 2);
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 2eae255f097a01f0e326da2577c591c4 | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | // Test ..
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Egor Kulikov (egor@egork.net)
*/
public class Main {
public static void main(String[] args) {
//InputStream inputStream = ;
OutputStream outputStream = System.out;
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int count = in.readInt();
int edgeCount = in.readInt();
int[] degree = new int[count];
for (int i = 0; i < 2 * edgeCount; i++)
degree[in.readInt() - 1]++;
long answer = (long) count * (count - 1) * (count - 2) / 6;
answer -= (long) edgeCount * (count - 2);
for (int i : degree)
answer += (long) i * (i - 1) / 2;
out.printLine(answer);
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
} | Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | d866838811cadb6498e2acb2d3c22689 | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | // Test ..
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Egor Kulikov (egor@egork.net)
*/
public class Main {
public static void main(String[] args) {
//InputStream inputStream = ;
OutputStream outputStream = System.out;
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt(), m = in.readInt();
int[] degree = new int[n+1];
for (int i=2*m; i>0; --i)
++degree[in.readInt()];
long a = (long) n * (n - 1) * (n - 2) / 6, b = 0;
for (int i=1;i<=n;++i)
b += (long) degree[i] * (n-1-degree[i]);
b /= 2;
out.printLine(a - b);
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
} | Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | f1de8f66025a5b887a591518ab7c3722 | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes |
// @author Sanzhar
import java.io.*;
import java.util.*;
import java.awt.Point;
public class Template {
BufferedReader in;
PrintWriter out;
StringTokenizer st;
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (Exception e) {
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
public void run() throws Exception {
//in = new BufferedReader(new FileReader("input.txt"));
//out = new PrintWriter(new FileWriter("output.txt"));
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.flush();
out.close();
in.close();
}
public void solve() throws Exception {
int n = nextInt();
int m = nextInt();
long[] d = new long[n];
for (int i = 0; i < m; i++) {
d[nextInt() - 1]++;
d[nextInt() - 1]++;
}
long ans = 0;
for (int i = 0; i < n; i++) {
ans += d[i] * (d[i] - 1) - d[i] * (n - d[i] - 1) + (n - d[i] - 1) * (n - d[i] - 2);
}
ans /= 6;
out.println(ans);
}
public static void main(String[] args) throws Exception {
new Template().run();
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 330ec24ebbbac492a7ec849e604e8fad | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.util.*;
import java.io.*;
public class cf229c {
public static void main(String[] args) {
FastIO in = new FastIO(), out = in;
int n = in.nextInt(), m = in.nextInt();
int[] deg = new int[n];
for(int i=0; i<2*m; i++)
deg[in.nextInt()-1]++;
long ans = 0;
for(int i=0; i<n; i++)
ans += calc(deg[i],n-1);
out.println(ans/6);
out.close();
}
static long calc(long d, long n) {
return choose2(d)*2 + choose2(n-d)*2 - d*(n-d);
}
static long choose2(long v) {
return (v*v-v)/2;
}
static class FastIO extends PrintWriter {
BufferedReader br;
StringTokenizer st;
public FastIO() {
this(System.in,System.out);
}
public FastIO(InputStream in, OutputStream out) {
super(new BufferedWriter(new OutputStreamWriter(out)));
br = new BufferedReader(new InputStreamReader(in));
scanLine();
}
public void scanLine() {
try {
st = new StringTokenizer(br.readLine().trim());
} catch(Exception e) {
throw new RuntimeException(e.getMessage());
}
}
public int numTokens() {
if(!st.hasMoreTokens()) {
scanLine();
return numTokens();
}
return st.countTokens();
}
public String next() {
if(!st.hasMoreTokens()) {
scanLine();
return next();
}
return st.nextToken();
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 4d4e4f6695c306db1d378c07b9b5a5ff | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Crash
*/
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();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int m = in.readInt();
int[] cnt = new int[n];
int[] a = new int[m];
int[] b = new int[m];
Arrays.fill(cnt, 0);
long sum = 0;
for (int i = 0; i < m; i ++) {
a[i] = in.readInt() - 1;
b[i] = in.readInt() - 1;
cnt[a[i]] ++;
cnt[b[i]] ++;
}
long ans = (long)m * (n - 2);
for (int i = 0; i < m; i ++) {
sum += cnt[a[i]] - 1;
sum += cnt[b[i]] - 1;
}
ans -= sum / 2;
ans = (long)n * (n - 1) * (n - 2) / 6 - ans;
out.printLine(ans);
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1000];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 873addb6236596bed81a7f8575bee7eb | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Solution implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer st;
String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
void solve() throws Exception {
int n = nextInt();
int m = nextInt();
long ans = (long)n * (n - 1) * (n - 2) / 6;
int[] s = new int[n];
long an1 = 0;
for (int i = 0; i < m; i++) {
int a = nextInt() - 1;
int b = nextInt() - 1;
s[a]++;
s[b]++;
an1 += n - 2;
}
long an2 = 0;
for (int i = 0; i < n; i++) {
an2 += (long)s[i] * (s[i] - 1) / 2;
}
out.println(ans - an1 + an2);
}
public void run() {
try {
Locale.setDefault(Locale.UK);
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();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
} finally {
out.close();
}
}
public static void main(String[] args) {
// new Thread(null, new Solution(), "1", 1 << 28).start();
(new Solution()).run();
}
} | Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 546e7d1ba7cbbf439dda4e5058815740 | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
public class Main {
private void solve() throws IOException {
long n = nextLong();
int m = nextInt();
final long ub = n - 1;
long arr[] = new long[(int)n];
Arrays.fill(arr, ub);
for (int i = 0; i < m; ++i) {
int a = nextInt() - 1;
int b = nextInt() - 1;
--arr[a];
--arr[b];
}
long wrtr = 0;
for (int i = 0 ; i < n; ++i) {
wrtr += arr[i] * (ub - arr[i]);
}
wrtr /= 2;
long res = n * (n - 1) * (n - 2) / 6 - wrtr;
pw.print(res);
}
public static void main(String[] args) {
new Main().run();
}
public void run() {
try {
pw = new PrintWriter(System.out);
br = new BufferedReader(new InputStreamReader(System.in));
solve();
pw.close();
br.close();
} catch (IOException e) {
System.err.println("IO Error");
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(br.readLine());
}
return tokenizer.nextToken();
}
private BufferedReader br;
private StringTokenizer tokenizer;
private PrintWriter pw;
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 68399674dbde795754287896c273a8e4 | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Nguyen Trung Hieu - vuondenthanhcong@yahoo.com
*/
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();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int count = in.readInt();
int edgeCount = in.readInt();
int[] degree = new int[count];
for (int i = 0; i < 2 * edgeCount; i++)
degree[in.readInt() - 1]++;
long answer = (long)(count) * (count - 1) * (count - 2) / 6;
answer -= (long)(edgeCount) * (count - 2);
for (int i : degree)
answer += (long)(i) * (i - 1) / 2;
out.printLine(answer);
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | bb2772453f88636bd48600957064327b | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.io.*;
import java.util.*;
public class c {
public static void main(String[] args) throws IOException {
input.init(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = input.nextInt(), m = input.nextInt();
long[] ds = new long[n];
for(int i = 0; i<m; i++)
{
int a = input.nextInt()-1, b = input.nextInt()-1;
ds[a]++;
ds[b]++;
}
long res = 0;
for(int i = 0; i<n; i++)
{
res += ds[i] * (ds[i] - 1);
res += (n-1-ds[i]) * (n-2-ds[i]);
res -= ds[i] * (n-1-ds[i]);
}
out.println(res/6);
out.close();
}
public static class input {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 75cffa56e9a933f98a2d0559c2a8a47d | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.io.BufferedWriter;
import java.util.Locale;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Jacob Jiang
*/
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();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int[] deg = new int[n];
for (int i = 0; i < (m << 1); i++) {
deg[in.nextInt() - 1]++;
}
long answer = -calc3(n);
for (int i = 0; i < n; i++) {
answer += calc2(deg[i]) + calc2(n - 1 - deg[i]);
}
out.println(answer >> 1);
}
private long calc2(long x) {
return x * (x - 1) / 2;
}
private long calc3(long x) {
return x * (x - 1) * (x - 2) / 6;
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
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 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 & 15;
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
class OutputWriter {
private PrintWriter writer;
public OutputWriter(OutputStream stream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void println(long x) {
writer.println(x);
}
public void close() {
writer.close();
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 078583e6130ae7dffa4ae9bbb6d2bbae | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Main {
public static InputReader in;
public static PrintWriter out;
public static void main(String[] args) throws IOException {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
boolean showLineError = true;
if (showLineError) {
solve();
out.close();
} else {
try {
solve();
} catch (Exception e) {
} finally {
out.close();
}
}
}
static void debug(Object... os) {
out.println(Arrays.deepToString(os));
}
private static void solve() throws IOException {
long n = in.readInt();
long m = in.readInt();
long[] deg = new long[(int) n];
for(int i =0;i<m;i++){
int u = in.readInt()-1;
int v = in.readInt()-1;
deg[u]++;
deg[v]++;
}
long invalid =0L;
for(int i =0;i<n;i++)
invalid+= (n-1-deg[i])* deg[i];
invalid/=2L;
long total = ( n*(n-1L)*(n-2L) )/6L;
out.println(total-invalid);
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1000];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
} | Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 7a4938513f508a51e96b7d65bf29d5f0 | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Solver {
public static void main(String[] Args) throws NumberFormatException,
IOException {
new Solver().Run();
}
PrintWriter pw;
StringTokenizer Stok;
BufferedReader br;
public String nextToken() throws IOException {
while (Stok == null || !Stok.hasMoreTokens()) {
Stok = new StringTokenizer(br.readLine());
}
return Stok.nextToken();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
public double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
public long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
long n,m;
long[] kolD;
public void Run() throws NumberFormatException, 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));
n=nextInt();
m=nextInt();
kolD=new long[(int) n];
int a,b;
for (int i=0; i<m; i++){
kolD[nextInt()-1]++;
kolD[nextInt()-1]++;
}
long result=0;
long zn;
for (int i=0; i<n; i++){
zn=n-1-kolD[i];
result=result+kolD[i]*(kolD[i]-1)-kolD[i]*zn+zn*(zn-1);
}
pw.println(result/6);
pw.flush();
pw.close();
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 7087ecfc603ab3a78d9f4fa404a5db88 | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* 4 4
* 1 5 3 4
* 1 2
* 1 3
* 2 3
* 3 3
*
*
* @author pttrung
*/
public class A {
public static long Mod = (long) (1e9) + 7;
static int time = 1;
static long[] dp;
public static void main(String[] args) throws FileNotFoundException {
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner();
long n = in.nextInt();
long m = in.nextInt();
if (m == 0) {
long v = n * (n - 1) * (n - 2) / 6;
out.println(v);
} else {
Edge[] data = new Edge[(int) m];
int[] count = new int[(int) n];
for (int i = 0; i < m; i++) {
int a = in.nextInt() - 1;
int b = in.nextInt() - 1;
if (a < b) {
data[i] = new Edge(a, b);
} else {
data[i] = new Edge(b, a);
}
count[a]++;
count[b]++;
}
Arrays.sort(data);
boolean[] check = new boolean[(int) n];
long start = n * (n - 1) * (n - 2) / 6;
long other = 0;
long result = 0;
int[] q = new int[(int) n];
for (int i = 0; i < n; i++) {
if (count[i] > 0) {
other += count[i] * (count[i] - 1) / 2;
}
}
// for(int i = 0; i < m; i++){
// if(!check[data[i].a] || !check[data[i].b]){
// check[data[i].a] = true;
// check[data[i].b] = true;
// }else{
// result++;
// }
// }
// System.out.println(start + " " + other);
// System.out.println(result);
start -= (m * (n - 2) - other);
out.println(start);
}
out.close();
}
static class Edge implements Comparable<Edge> {
int a, b;
Edge(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Edge o) {
if (o.a != a) {
return a - o.a;
}
return b - o.b;
}
}
static int find(int index, int[] u) {
if (index != u[index]) {
return u[index] = find(u[index], u);
}
return index;
}
public static long pow(int a, int b, long mod) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long v = pow(a, b / 2, mod);
if (b % 2 == 0) {
return (v * v) % mod;
} else {
return (((v * v) % mod) * a) % mod;
}
}
public static int[][] powSquareMatrix(int[][] A, long p) {
int[][] unit = new int[A.length][A.length];
for (int i = 0; i < unit.length; i++) {
unit[i][i] = 1;
}
if (p == 0) {
return unit;
}
int[][] val = powSquareMatrix(A, p / 2);
if (p % 2 == 0) {
return mulMatrix(val, val);
} else {
return mulMatrix(A, mulMatrix(val, val));
}
}
public static int[][] mulMatrix(int[][] A, int[][] B) {
int[][] result = new int[A.length][B[0].length];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++) {
long temp = 0;
for (int k = 0; k < A[0].length; k++) {
temp += ((long) A[i][k] * B[k][j] % Mod);
temp %= Mod;
}
temp %= Mod;
result[i][j] = (int) temp;
}
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
static class FT {
int[] data;
FT(int n) {
data = new int[n];
}
void update(int index, int val) {
// System.out.println("UPDATE INDEX " + index);
while (index < data.length) {
data[index] += val;
index += index & (-index);
// System.out.println("NEXT " +index);
}
}
int get(int index) {
// System.out.println("GET INDEX " + index);
int result = 0;
while (index > 0) {
result += data[index];
index -= index & (-index);
// System.out.println("BACK " + index);
}
return result;
}
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
static long pow(long a, int b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2);
if (b % 2 == 0) {
return val * val % Mod;
} else {
return (val * val % Mod) * a % Mod;
}
}
// static Point intersect(Point a, Point b, Point c) {
// double D = cross(a, b);
// if (D != 0) {
// return new Point(cross(c, b) / D, cross(a, c) / D);
// }
// return null;
// }
//
// static Point convert(Point a, double angle) {
// double x = a.x * cos(angle) - a.y * sin(angle);
// double y = a.x * sin(angle) + a.y * cos(angle);
// return new Point(x, y);
// }
static Point minus(Point a, Point b) {
return new Point(a.x - b.x, a.y - b.y);
}
//
// static Point add(Point a, Point b) {
// return new Point(a.x + b.x, a.y + b.y);
// }
//
/**
* Cross product ab*ac
*
* @param a
* @param b
* @param c
* @return
*/
static double cross(Point a, Point b, Point c) {
Point ab = new Point(b.x - a.x, b.y - a.y);
Point ac = new Point(c.x - a.x, c.y - a.y);
return cross(ab, ac);
}
static double cross(Point a, Point b) {
return a.x * b.y - a.y * b.x;
}
/**
* Dot product ab*ac;
*
* @param a
* @param b
* @param c
* @return
*/
static long dot(Point a, Point b, Point c) {
Point ab = new Point(b.x - a.x, b.y - a.y);
Point ac = new Point(c.x - a.x, c.y - a.y);
return dot(ab, ac);
}
static long dot(Point a, Point b) {
long total = a.x * b.x;
total += a.y * b.y;
return total;
}
static double dist(Point a, Point b) {
long total = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
return Math.sqrt(total);
}
static long norm(Point a) {
long result = a.x * a.x;
result += a.y * a.y;
return result;
}
static double dist(Point a, Point b, Point x, boolean isSegment) {
double dist = cross(a, b, x) / dist(a, b);
// System.out.println("DIST " + dist);
if (isSegment) {
Point ab = new Point(b.x - a.x, b.y - a.y);
long dot1 = dot(a, b, x);
long norm = norm(ab);
double u = (double) dot1 / norm;
if (u < 0) {
return dist(a, x);
}
if (u > 1) {
return dist(b, x);
}
}
return Math.abs(dist);
}
static long squareDist(Point a, Point b) {
long x = a.x - b.x;
long y = a.y - b.y;
return x * x + y * y;
}
static class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point{" + "x=" + x + ", y=" + y + '}';
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
//System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
//br = new BufferedReader(new FileReader(new File("A-large (2).in")));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 09d6d3206a8361f6fe656ae1093a1be2 | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* 4 4
* 1 5 3 4
* 1 2
* 1 3
* 2 3
* 3 3
*
*
* @author pttrung
*/
public class A {
public static long Mod = (long) (1e9) + 7;
static int time = 1;
static long[] dp;
public static void main(String[] args) throws FileNotFoundException {
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner();
long n = in.nextInt();
long m = in.nextInt();
if (m == 0) {
long v = n * (n - 1) * (n - 2) / 6;
out.println(v);
} else {
int[] count = new int[(int) n];
for (int i = 0; i < m; i++) {
int a = in.nextInt() - 1;
int b = in.nextInt() - 1;
count[a]++;
count[b]++;
}
// Arrays.sort(data);
long start = n * (n - 1) * (n - 2) / 6;
long other = 0;
long result = 0;
int[] q = new int[(int) n];
for (int i = 0; i < n; i++) {
if (count[i] > 0) {
other += count[i] * (count[i] - 1) / 2;
}
}
start -= (m * (n - 2) - other);
out.println(start);
}
out.close();
}
static class Edge implements Comparable<Edge> {
int a, b;
Edge(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Edge o) {
if (o.a != a) {
return a - o.a;
}
return b - o.b;
}
}
static int find(int index, int[] u) {
if (index != u[index]) {
return u[index] = find(u[index], u);
}
return index;
}
public static long pow(int a, int b, long mod) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long v = pow(a, b / 2, mod);
if (b % 2 == 0) {
return (v * v) % mod;
} else {
return (((v * v) % mod) * a) % mod;
}
}
public static int[][] powSquareMatrix(int[][] A, long p) {
int[][] unit = new int[A.length][A.length];
for (int i = 0; i < unit.length; i++) {
unit[i][i] = 1;
}
if (p == 0) {
return unit;
}
int[][] val = powSquareMatrix(A, p / 2);
if (p % 2 == 0) {
return mulMatrix(val, val);
} else {
return mulMatrix(A, mulMatrix(val, val));
}
}
public static int[][] mulMatrix(int[][] A, int[][] B) {
int[][] result = new int[A.length][B[0].length];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++) {
long temp = 0;
for (int k = 0; k < A[0].length; k++) {
temp += ((long) A[i][k] * B[k][j] % Mod);
temp %= Mod;
}
temp %= Mod;
result[i][j] = (int) temp;
}
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
static class FT {
int[] data;
FT(int n) {
data = new int[n];
}
void update(int index, int val) {
// System.out.println("UPDATE INDEX " + index);
while (index < data.length) {
data[index] += val;
index += index & (-index);
// System.out.println("NEXT " +index);
}
}
int get(int index) {
// System.out.println("GET INDEX " + index);
int result = 0;
while (index > 0) {
result += data[index];
index -= index & (-index);
// System.out.println("BACK " + index);
}
return result;
}
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
static long pow(long a, int b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2);
if (b % 2 == 0) {
return val * val % Mod;
} else {
return (val * val % Mod) * a % Mod;
}
}
// static Point intersect(Point a, Point b, Point c) {
// double D = cross(a, b);
// if (D != 0) {
// return new Point(cross(c, b) / D, cross(a, c) / D);
// }
// return null;
// }
//
// static Point convert(Point a, double angle) {
// double x = a.x * cos(angle) - a.y * sin(angle);
// double y = a.x * sin(angle) + a.y * cos(angle);
// return new Point(x, y);
// }
static Point minus(Point a, Point b) {
return new Point(a.x - b.x, a.y - b.y);
}
//
// static Point add(Point a, Point b) {
// return new Point(a.x + b.x, a.y + b.y);
// }
//
/**
* Cross product ab*ac
*
* @param a
* @param b
* @param c
* @return
*/
static double cross(Point a, Point b, Point c) {
Point ab = new Point(b.x - a.x, b.y - a.y);
Point ac = new Point(c.x - a.x, c.y - a.y);
return cross(ab, ac);
}
static double cross(Point a, Point b) {
return a.x * b.y - a.y * b.x;
}
/**
* Dot product ab*ac;
*
* @param a
* @param b
* @param c
* @return
*/
static long dot(Point a, Point b, Point c) {
Point ab = new Point(b.x - a.x, b.y - a.y);
Point ac = new Point(c.x - a.x, c.y - a.y);
return dot(ab, ac);
}
static long dot(Point a, Point b) {
long total = a.x * b.x;
total += a.y * b.y;
return total;
}
static double dist(Point a, Point b) {
long total = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
return Math.sqrt(total);
}
static long norm(Point a) {
long result = a.x * a.x;
result += a.y * a.y;
return result;
}
static double dist(Point a, Point b, Point x, boolean isSegment) {
double dist = cross(a, b, x) / dist(a, b);
// System.out.println("DIST " + dist);
if (isSegment) {
Point ab = new Point(b.x - a.x, b.y - a.y);
long dot1 = dot(a, b, x);
long norm = norm(ab);
double u = (double) dot1 / norm;
if (u < 0) {
return dist(a, x);
}
if (u > 1) {
return dist(b, x);
}
}
return Math.abs(dist);
}
static long squareDist(Point a, Point b) {
long x = a.x - b.x;
long y = a.y - b.y;
return x * x + y * y;
}
static class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point{" + "x=" + x + ", y=" + y + '}';
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
//System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
//br = new BufferedReader(new FileReader(new File("A-large (2).in")));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 166e83c38562337352132b80d8cb9892 | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Set;
public class ProblemC {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
long time = System.currentTimeMillis();
int n = in.readInt();
int m = in.readInt();
int[] deg = new int[n];
for (int i = 0 ; i < m ; i++) {
int a = in.readInt()-1;
int b = in.readInt()-1;
deg[a]++;
deg[b]++;
}
long ans = 0;
for (int i = 0 ; i < n ; i++) {
ans += (n - 1 - deg[i]) * deg[i];
}
ans /= 2;
out.println(1L * n * (n-1) * (n-2) / 6 - ans);
out.flush();
}
static void pr(int n, int m) {
System.out.println(n + " " + m);
Set<Integer>[] done = new Set[n+1];
for (int i = 0 ; i < n+1 ; i++) {
done[i] = new HashSet<Integer>();
}
for (int i = 0 ; i < m ; i++) {
int a = (int)(Math.random() * n) + 1;
int b = (int)(Math.random() * n) + 1;
if (a == b) {
--i;
continue;
}
if (done[a].contains(b)) {
--i;
continue;
}
System.out.println(a + " " + b);
done[a].add(b);
done[b].add(a);
}
}
public static void debug(Object... o) {
System.err.println(Arrays.deepToString(o));
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | d0893c20091d66f52d08afa9c1747316 | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
/**
* Created with IntelliJ IDEA.
*/
public class ProblemC {
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[] degree = new int[n];
for (int i = 0 ; i < m ; i++) {
int a = in.nextInt()-1;
int b = in.nextInt()-1;
degree[a]++;
degree[b]++;
}
long all = (1L * n) * (n - 1) * (n - 2) / 6;
long not = 0;
for (int i = 0 ; i < n ; i++) {
not += (degree[i] + 0L) * ((n-1) - degree[i] + 0L);
}
not /= 2;
out.println(all - not);
out.flush();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int next() {
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 = next();
while (isSpaceChar(c))
c = next();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = next();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 10937e73a089ad6f3af2202f7db0fad6 | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solution implements Runnable {
private BufferedReader br;
private StringTokenizer Tokenizer;
private PrintWriter out;
public static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
void solve() throws IOException {
int n = nextInt();
int m = nextInt();
int[] deg = new int[n];
Arrays.fill(deg, n - 1);
for (int i = 0; i < m; ++i) {
int u = nextInt();
int v = nextInt();
--deg[u - 1];
--deg[v - 1];
}
long sum = 0;
for (int i = 0; i < n; ++i) {
sum += (long) deg[i] * (n - deg[i] - 1);
}
out.print((long) n * (n - 1) * (n - 2) / 6 - sum / 2);
}
public void run() {
try {
if (ONLINE_JUDGE) {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
br = new BufferedReader(new FileReader(new File("input.txt")));
out = new PrintWriter(new File("output.txt"));
}
solve();
br.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
new Solution().run();
}
public String nextToken() throws IOException {
while (Tokenizer == null || !Tokenizer.hasMoreTokens())
Tokenizer = new StringTokenizer(br.readLine());
return Tokenizer.nextToken();
}
public String nextString() throws IOException {
return nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(nextToken());
}
} | Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | dc3b1f41632a298cbda40cf72c486daf | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.math.*;
import java.awt.geom.Line2D;
import java.io.*;
import java.lang.*;
import java.util.*;
public class C implements Runnable {
public void run() {
int n = nextInt();
int m = nextInt();
ArrayList<Integer>[] gg = new ArrayList[n];
for (int i = 0; i < n; ++i) {
gg[i] = new ArrayList<Integer>();
}
int[] cnt = new int[n];
for (int i = 0; i < m; ++i) {
int a = nextInt() - 1;
int b = nextInt() - 1;
++cnt[a];
++cnt[b];
gg[a].add(b);
}
long ans = 0;
for (int i = 0; i < n; ++i) {
ans += (n-1-cnt[i])*cnt[i];
}
ans /= 2;
ans = 1L*n*(n-1)*(n-2)/6 - ans;
out.println(ans);
out.flush();
}
private static BufferedReader br = null;
private static StringTokenizer stk = null;
private static PrintWriter out = null;
public static void main(String[] args) {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
(new C()).run();
}
public void loadLine() {
try {
stk = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
public String nextLine() {
try {
return br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
public int nextInt() {
while (stk==null || !stk.hasMoreElements()) loadLine();
return Integer.parseInt(stk.nextToken());
}
public long nextLong() {
while (stk==null || !stk.hasMoreElements()) loadLine();
return Long.parseLong(stk.nextToken());
}
public double nextDouble() {
while (stk==null || !stk.hasMoreElements()) loadLine();
return Double.parseDouble(stk.nextToken());
}
public String nextWord() {
while (stk==null || !stk.hasMoreElements()) loadLine();
return (stk.nextToken());
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 71456bdd2c165e2475aeee32d1a06997 | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.math.*;
import java.awt.geom.Line2D;
import java.io.*;
import java.lang.*;
import java.util.*;
public class C implements Runnable {
public void run() {
int n = nextInt();
int m = nextInt();
/*ArrayList<Integer>[] g = new ArrayList[n];
for (int i = 0; i < n; ++i) {
g[i] = new ArrayList<Integer>();
}*/
int[] cnt = new int[n];
for (int i = 0; i < m; ++i) {
int a = nextInt() - 1;
int b = nextInt() - 1;
++cnt[a];
++cnt[b];
// g[a].add(b);
}
long ans = 0;
for (int i = 0; i < n; ++i) {
ans += (n-1-cnt[i])*cnt[i];
}
ans /= 2;
ans = 1L*n*(n-1)*(n-2)/6 - ans;
out.println(ans);
out.flush();
}
private static BufferedReader br = null;
private static StringTokenizer stk = null;
private static PrintWriter out = null;
public static void main(String[] args) {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
(new C()).run();
}
public void loadLine() {
try {
stk = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
public String nextLine() {
try {
return br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
public int nextInt() {
while (stk==null || !stk.hasMoreElements()) loadLine();
return Integer.parseInt(stk.nextToken());
}
public long nextLong() {
while (stk==null || !stk.hasMoreElements()) loadLine();
return Long.parseLong(stk.nextToken());
}
public double nextDouble() {
while (stk==null || !stk.hasMoreElements()) loadLine();
return Double.parseDouble(stk.nextToken());
}
public String nextWord() {
while (stk==null || !stk.hasMoreElements()) loadLine();
return (stk.nextToken());
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | b2b884611aa7a33e403f792250053478 | train_002.jsonl | 1349105400 | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with n vertices, chooses some m edges and keeps them. Bob gets the remaining edges.Alice and Bob are fond of "triangles" in graphs, that is, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 256 megabytes | import java.math.*;
import java.awt.geom.Line2D;
import java.io.*;
import java.lang.*;
import java.util.*;
public class C implements Runnable {
public void run() {
int n = nextInt();
int m = nextInt();
ArrayList<Integer>[] g = new ArrayList[n];
for (int i = 0; i < n; ++i) {
g[i] = new ArrayList<Integer>();
}
int[] cnt = new int[n];
for (int i = 0; i < m; ++i) {
int a = nextInt() - 1;
int b = nextInt() - 1;
++cnt[a];
++cnt[b];
g[a].add(b);
}
long ans = 0;
for (int i = 0; i < n; ++i) {
ans += (n-1-cnt[i])*cnt[i];
}
ans /= 2;
ans = 1L*n*(n-1)*(n-2)/6 - ans;
out.println(ans);
out.flush();
}
private static BufferedReader br = null;
private static StringTokenizer stk = null;
private static PrintWriter out = null;
public static void main(String[] args) {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
(new C()).run();
}
public void loadLine() {
try {
stk = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
public String nextLine() {
try {
return br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
public int nextInt() {
while (stk==null || !stk.hasMoreElements()) loadLine();
return Integer.parseInt(stk.nextToken());
}
public long nextLong() {
while (stk==null || !stk.hasMoreElements()) loadLine();
return Long.parseLong(stk.nextToken());
}
public double nextDouble() {
while (stk==null || !stk.hasMoreElements()) loadLine();
return Double.parseDouble(stk.nextToken());
}
public String nextWord() {
while (stk==null || !stk.hasMoreElements()) loadLine();
return (stk.nextToken());
}
}
| Java | ["5 5\n1 2\n1 3\n2 3\n2 4\n3 4", "5 3\n1 2\n2 3\n1 3"] | 2 seconds | ["3", "4"] | NoteIn the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case the answer to the problem is 4. | Java 7 | standard input | [
"combinatorics",
"graphs",
"math"
] | cbd87a55161ca9c66bf0095dbdce2a9b | The first line contains two space-separated integers n and m (1ββ€βnββ€β106,β0ββ€βmββ€β106) β the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then m lines follow: the i-th line contains two space-separated integers ai, bi (1ββ€βai,βbiββ€βn, aiββ βbi), β the numbers of the two vertices connected by the i-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops. Consider the graph vertices to be indexed in some way from 1 to n. | 1,900 | Print a single number β the total number of cycles of length 3 in Alice and Bob's graphs together. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is advised to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 05a0b6bf4ad77b57d223c9e01d675f94 | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class div2420 {
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;
}
}
public static void main(String args[]) throws Exception {
class prob {
public void solve() {
MyScanner sc = new MyScanner();
int n = sc.nextInt();
ArrayList<Integer> al = new ArrayList<Integer>();
int result= 0;
int idx = 1;
for (int i = 0; i < 2*n; ++i) {
String in = sc.next();
if (in.equals("add")) {
int in2 = sc.nextInt();
al.add(in2);
}
else {
if (al.size() > 0 && al.get(al.size() - 1) != idx) {
al.clear();
++result;
}
else if (al.size() > 0) {
al.remove(al.size() - 1);
}
++idx;
}
}
System.out.println(result);
}
}
prob s = new prob();
s.solve();
}
}
| Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | e343cf4520163b78f4ecd2bbd0ca093b | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | //package HackerEarthA;
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import javafx.scene.layout.Priority;
import java.awt.Point;
/**
*
* @author prabhat // use stringbuilder, priorityQueue
*/
public class easy18{
public static long[] BIT;
public static long[] tree;
public static long[] sum;
public static int mod=1000000007;
public static int[][] dp;
public static boolean[][] isPalin;
public static int max1=1000010;
public static int[][] g;
public static LinkedList<Pair> l[];
public static int n,m,q,k,t,arr[],b[],cnt[],chosen[],pos[],val[],blocksize,count;
public static long V[],low,high,min=Long.MAX_VALUE,cap[],has[],ans,max;
public static ArrayList<Integer> adj[],al;
public static TreeSet<Integer> ts;
public static char[][] s;
public static int depth,mini,maxi;
public static boolean visit[][],isPrime[],used[];
public static int[][] dist;
public static ArrayList<Integer>prime;
public static int[] x={0,1};
public static int[] y={-1,0};
public static ArrayList<Integer> divisor[]=new ArrayList[1500005];
public static int[][] subtree,parent,mat;
public static TreeMap<Integer,Integer> tm;
public static LongPoint[] p;
public static int[][] grid,memo;
public static ArrayList<Pair> list;
public static TrieNode root;
public static LinkedHashMap<String,Integer> map;
public static ArrayList<String> al1;
public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception
{
InputReader in = new InputReader(System.in);
PrintWriter pw=new PrintWriter(System.out);
n=in.ii();
arr=new int[n];
int a=1;
int u=0;
int ans=0;
for(int i=0;i<2*n;i++)
{
String s=in.is();
if(s.equals("add"))arr[u++]=in.ii();
else{
if(u>0&&arr[--u]!=a)
{
u=0;
ans++;
}
a++;
}
}
pw.println(ans);
pw.close();
}
static int orient(Point a,Point b,Point c)
{
//return (int)(c.x-a.x)*(int)(b.y-a.y)-(int)(c.y-a.y)*(int)(b.x-a.x);
Point p1=c.minus(a);
Point p2=b.minus(a);
return (int)(p1.cross(p2));
}
public static class Polygon {
static final double EPS = 1e-15;
public int n;
public Point p[];
Polygon(int n, Point x[]) {
this.n = n;
p = Arrays.copyOf(x, n);
}
long area(){ //returns 2 * area
long ans = 0;
for(int i = 1; i + 1 < n; i++)
ans += p[i].minus(p[0]).cross(p[i + 1].minus(p[0]));
return ans;
}
boolean PointInPolygon(Point q) {
boolean c = false;
for (int i = 0; i < n; i++){
int j = (i+1)%n;
if ((p[i].y <= q.y && q.y < p[j].y ||
p[j].y <= q.y && q.y < p[i].y) &&
q.x < p[i].x + (p[j].x - p[i].x) * (q.y - p[i].y) / (p[j].y - p[i].y))
c = !c;
}
return c;
}
// determine if point is on the boundary of a polygon
boolean PointOnPolygon(Point q) {
for (int i = 0; i < n; i++)
if (ProjectPointSegment(p[i], p[(i+1)%n], q).dist(q) < EPS)
return true;
return false;
}
// project point c onto line segment through a and b
Point ProjectPointSegment(Point a, Point b, Point c) {
double r = b.minus(a).dot(b.minus(a));
if (Math.abs(r) < EPS) return a;
r = c.minus(a).dot(b.minus(a))/r;
if (r < 0) return a;
if (r > 1) return b;
return a.plus(b.minus(a).mul(r));
}
}
public static class Point {
public double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point minus(Point b) {
return new Point(x - b.x, y - b.y);
}
public Point plus(Point a){
return new Point(x + a.x, y + a.y);
}
public double cross(Point b) {
return (double)x * b.y - (double)y * b.x;
}
public double dot(Point b) {
return (double)x * b.x + (double)y * b.y;
}
public Point mul(double r){
return new Point(x * r, y * r);
}
public double dist(Point p){
return Math.sqrt(fastHypt( x - p.x , y - p.y));
}
public double fastHypt(double x, double y){
return x * x + y * y;
}
}
static class Query implements Comparable<Query>{
int index,k;
int L;
int R;
public Query(){}
public Query(int a,int b,int index)
{
this.L=a;
this.R=b;
this.index=index;
}
public int compareTo(Query o)
{
if(L/blocksize!=o.L/blocksize)return L/blocksize-o.L/blocksize;
else return R-o.R;
}
}
static double dist(Point p1,Point p2)
{
return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
static class coder {
int indx;
String name;
coder(int indx,String name)
{
this.indx=indx;
this.name=name;
}
}
static class TrieNode
{
int value; // Only used in leaf nodes
int freq;
TrieNode[] arr = new TrieNode[2];
public TrieNode() {
value = 0;
freq=0;
arr[0] = null;
arr[1] = null;
}
}
static void insert(int pre_xor,int add)
{
TrieNode temp = root;
// Start from the msb, insert all bits of
// pre_xor into Trie
for (int i=31; i>=0; i--)
{
// Find current bit in given prefix
int val = (pre_xor & (1<<i)) >=1 ? 1 : 0;
// Create a new node if needed
if (temp.arr[val] == null)
temp.arr[val] = new TrieNode();
temp = temp.arr[val];
temp.freq+=add;
}
// Store value at leaf node
//temp.value = pre_xor;
}
static long query(int pre_xor)
{
TrieNode temp = root;
long ans=0;
for (int i=31; i>=0; i--)
{
// Find current bit in given prefix
int val = (pre_xor & (1<<i)) >= 1 ? 1 : 0;
if(temp.arr[1-val]!=null&&temp.arr[1-val].freq>0)
{
ans|=1l<<i;
temp=temp.arr[1-val];
}
else if(temp.arr[val]!=null&&temp.arr[val].freq>0)
temp=temp.arr[val];
else break;
}
return ans;
}
static void update(int indx,long val)
{
while(indx<max1)
{
BIT[indx]+=val;
indx+=(indx&(-indx));
}
}
static long query1(int indx)
{
long sum=0;
while(indx>0)
{
sum+=BIT[indx];
indx-=(indx&(-indx));
}
return sum;
}
static int gcd(int a, int b)
{
int res=0;
while(a!=0)
{
res=a;
a=b%a;
b=res;
}
return b;
}
static slope getSlope(Point p, Point q)
{
int dx = (int)(q.x - p.x), dy = (int)(q.y - p.y);
int g = gcd(dx, dy);
return new slope(dy / g, dx / g);
}
static class slope implements Comparable<slope>
{
int x,y;
public slope(int y,int x)
{
this.x=x;
this.y=y;
}
public int compareTo(slope s)
{
if(s.y!=y)return y-s.y;
return x-s.x;
}
}
static class Ball implements Comparable
{
int r;
int occ;
public Ball(int r,int occ)
{
this.r = r;
this.occ = occ;
}
@Override
public int compareTo(Object o) {
Ball b = (Ball) o;
return b.occ-this.occ;
}
}
static class E implements Comparable<E>{
int x,y;
char c;
E(int x,int y,char c)
{
this.x=x;
this.y=y;
this.c=c;
}
public int compareTo(E o)
{
if(x!=o.x)return o.x-x;
else return y-o.y;
}
}
static ArrayList<String> dfs(String s,String[] sarr,HashMap<String, ArrayList<String>> map)
{
if(map.containsKey(s))return map.get(s);
ArrayList<String> res=new ArrayList<>();
if(s.length()==0)
{
res.add("");
return res;
}
for(String word:sarr)
{
if(s.startsWith(word))
{
ArrayList<String > sub=dfs(s.substring(word.length()),sarr,map);
for(String s2:sub)
{
res.add(word+ (s2.isEmpty() ? "" : " ")+s2);
}
}
}
map.put(s,res);
return res;
}
static class SegmentTree{
int n;
int max_bound;
public SegmentTree(int n)
{
this.n=n;
tree=new long[4*n+1];
build(1,0,n-1);
}
void build(int c,int s,int e)
{
if(s==e)
{
tree[c]=arr[s];
return ;
}
int mid=(s+e)>>1;
build(2*c,s,mid);
build(2*c+1,mid+1,e);
tree[c]=(tree[2*c]&tree[2*c+1]);
}
void put(int c,int s, int e,int l,int r,int v) {
if(l>e||r<s||s>e)return ;
if (s == e)
{
tree[c] = arr[s]^v;
return;
}
int mid = (s + e) >> 1;
if (l>mid) put(2*c+1,m+1, e , l,r, v);
else if(r<=mid)put(2*c,s,m,l,r , v);
else{
}
}
long query(int c,int s,int e,int l,int r)
{
if(e<l||s>r)return 0L;
if(s>=l&&e<=r)
{
return tree[c];
}
int mid=(s+e)>>1;
long ans=(1l<<30)-1;
if(l>mid)return query(2*c+1,mid+1,e,l,r);
else if(r<=mid)return query(2*c,s,mid,l,r);
else{
return query(2*c,s,mid,l,r)&query(2*c+1,mid+1,e,l,r);
}
}
}
public static ListNode removeNthFromEnd(ListNode a, int b) {
int cnt=0;
ListNode ra1=a;
ListNode ra2=a;
while(ra1!=null){ra1=ra1.next; cnt++;}
if(b>cnt)return a.next;
else if(b==1&&cnt==1)return null;
else{
int y=cnt-b+1;
int u=0;
ListNode prev=null;
while(a!=null)
{
u++;
if(u==y)
{
if(a.next==null)prev.next=null;
else
{
if(prev==null)return ra2.next;
prev.next=a.next;
a.next=null;
}
break;
}
prev=a;
a=a.next;
}
}
return ra2;
}
static ListNode rev(ListNode a)
{
ListNode prev=null;
ListNode cur=a;
ListNode next=null;
while(cur!=null)
{
next=cur.next;
cur.next=prev;
prev=cur;
cur=next;
}
return prev;
}
static class ListNode {
public int val;
public ListNode next;
ListNode(int x) { val = x; next = null; }
}
public static String add(String s1,String s2)
{
int n=s1.length()-1;
int m=s2.length()-1;
int rem=0;
int carry=0;
int i=n;
int j=m;
String ans="";
while(i>=0&&j>=0)
{
int c1=(int)(s1.charAt(i)-'0');
int c2=(int)(s2.charAt(j)-'0');
int sum=c1+c2;
sum+=carry;
rem=sum%10;
carry=sum/10;
ans=rem+ans;
i--; j--;
}
while(i>=0)
{
int c1=(int)(s1.charAt(i)-'0');
int sum=c1;
sum+=carry;
rem=sum%10;
carry=sum/10;
ans=rem+ans;
i--;
}
while(j>=0)
{
int c2=(int)(s2.charAt(j)-'0');
int sum=c2;
sum+=carry;
rem=sum%10;
carry=sum/10;
ans=rem+ans;
j--;
}
if(carry>0)ans=carry+ans;
return ans;
}
public static String[] multiply(String A, String B) {
int lb=B.length();
char[] a=A.toCharArray();
char[] b=B.toCharArray();
String[] s=new String[lb];
int cnt=0;
int y=0;
String s1="";
q=0;
for(int i=b.length-1;i>=0;i--)
{
int rem=0;
int carry=0;
for(int j=a.length-1;j>=0;j--)
{
int mul=(int)(b[i]-'0')*(int)(a[j]-'0');
mul+=carry;
rem=mul%10;
carry=mul/10;
s1=rem+s1;
}
s1=carry+s1;
s[y++]=s1;
q=Math.max(q,s1.length());
s1="";
for(int i1=0;i1<y;i1++)s1+='0';
}
return s;
}
public static long nCr(long total, long choose)
{
if(total < choose)
return 0;
if(choose == 0 || choose == total)
return 1;
return nCr(total-1,choose-1)+nCr(total-1,choose);
}
static int get(long s)
{
int ans=0;
for(int i=0;i<n;i++)
{
if(p[i].x<=s&&s<=p[i].y)ans++;
}
return ans;
}
static class LongPoint {
public long x;
public long y;
public LongPoint(long x, long y) {
this.x = x;
this.y = y;
}
public LongPoint subtract(LongPoint o) {
return new LongPoint(x - o.x, y - o.y);
}
public long cross(LongPoint o) {
return x * o.y - y * o.x;
}
}
static int CountPs(String s,int n)
{
boolean b=false;
char[] S=s.toCharArray();
int[][] dp=new int[n][n];
boolean[][] p=new boolean[n][n];
for(int i=0;i<n;i++)p[i][i]=true;
for(int i=0;i<n-1;i++)
{
if(S[i]==S[i+1])
{
p[i][i+1]=true;
dp[i][i+1]=0;
b=true;
}
}
for(int gap=2;gap<n;gap++)
{
for(int i=0;i<n-gap;i++)
{
int j=gap+i;
if(S[i]==S[j]&&p[i+1][j-1]){p[i][j]=true;}
if(p[i][j])
dp[i][j]=dp[i][j-1]+dp[i+1][j]+1-dp[i+1][j-1];
else if(!p[i][j]) dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
//if(dp[i][j]>=1)
// return true;
}
}
// return b;
return dp[0][n-1];
}
static void Seive()
{
isPrime=new boolean[1500005];
Arrays.fill(isPrime,true);
for(int i=0;i<1500005;i++){divisor[i]=new ArrayList<>();}
int u=0;
prime=new ArrayList<>();
for(int i=2;i<=(1500000);i++)
{
if(isPrime[i])
{
prime.add(i);
for(int j=i;j<1500000;j+=i)
{
divisor[j].add(i);
isPrime[j]=false;
}
/* for(long x=(divCeil(low,i))*i;x<=high;x+=i)
{
divisor[(int)(x-low)].add(i*1L);
}*/
}
}
}
static long divCeil(long a,long b)
{
return (a+b-1)/b;
}
static long root(int pow, long x) {
long candidate = (long)Math.exp(Math.log(x)/pow);
candidate = Math.max(1, candidate);
while (pow(candidate, pow) <= x) {
candidate++;
}
return candidate-1;
}
static long pow(long x, int pow)
{
long result = 1;
long p = x;
while (pow > 0) {
if ((pow&1) == 1) {
if (result > Long.MAX_VALUE/p) return Long.MAX_VALUE;
result *= p;
}
pow >>>= 1;
if (pow > 0 && p >= 4294967296L) return Long.MAX_VALUE;
p *= p;
}
return result;
}
static boolean valid(int i,int j)
{
if(i>=0&&i<n&&j>=0&&j<m)return true;
return false;
}
private static class DSU{
int[] parent;
int[] rank;
int cnt;
public DSU(int n){
parent=new int[n];
rank=new int[n];
for(int i=0;i<n;i++){
parent[i]=i;
rank[i]=1;
}
cnt=n;
}
int find(int i){
while(parent[i] !=i){
parent[i]=parent[parent[i]];
i=parent[i];
}
return i;
}
int Union(int x, int y){
int xset = find(x);
int yset = find(y);
if(xset!=yset)
cnt--;
if(rank[xset]<rank[yset]){
parent[xset] = yset;
rank[yset]+=rank[xset];
rank[xset]=0;
return yset;
}else{
parent[yset]=xset;
rank[xset]+=rank[yset];
rank[yset]=0;
return xset;
}
}
}
public static int[][] packU(int n, int[] from, int[] to, int max) {
int[][] g = new int[n][];
int[] p = new int[n];
for (int i = 0; i < max; i++) p[from[i]]++;
for (int i = 0; i < max; i++) p[to[i]]++;
for (int i = 0; i < n; i++) g[i] = new int[p[i]];
for (int i = 0; i < max; i++) {
g[from[i]][--p[from[i]]] = to[i];
g[to[i]][--p[to[i]]] = from[i];
}
return g;
}
public static int[][] parents3(int[][] g, int root) {
int n = g.length;
int[] par = new int[n];
Arrays.fill(par, -1);
int[] depth = new int[n];
depth[0] = 0;
int[] q = new int[n];
q[0] = root;
for (int p = 0, r = 1; p < r; p++) {
int cur = q[p];
for (int nex : g[cur]) {
if (par[cur] != nex) {
q[r++] = nex;
par[nex] = cur;
depth[nex] = depth[cur] + 1;
}
}
}
return new int[][]{par, q, depth};
}
public static int lower_bound(int[] nums, int target) {
int low = 0, high = nums.length - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (nums[mid] < target)
low = mid + 1;
else
high = mid;
}
return nums[low] == target ? low : -1;
}
public static int upper_bound(int[] nums, int target) {
int low = 0, high = nums.length - 1;
while (low < high) {
int mid = low + (high + 1 - low) / 2;
if (nums[mid] > target)
high = mid - 1;
else
low = mid;
}
return nums[low] == target ? low : -1;
}
public static boolean palin(String s)
{
int i=0;
int j=s.length()-1;
while(i<j)
{
if(s.charAt(i)==s.charAt(j))
{
i++;
j--;
}
else return false;
}
return true;
}
static int lcm(int a,int b)
{
return (a*b)/(gcd(a,b));
}
public 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;
}
/**
*
* @param n
* @param p
* @return
*/
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;
}
static class Edge implements Comparator<Edge> {
private int u;
private int v;
private long w;
public Edge() {
}
public Edge(int u, int v, long w) {
this.u=u;
this.v=v;
this.w=w;
}
public int getU() {
return u;
}
public void setU(int u) {
this.u = u;
}
public int getV() {
return v;
}
public void setV(int v) {
this.v = v;
}
public long getW() {
return w;
}
public void setW(int w) {
this.w = w;
}
public long compareTo(Edge e)
{
return (this.getW() - e.getW());
}
@Override
public int compare(Edge o1, Edge o2) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
static class Pair implements Comparable<Pair>
{
int x,y;
Pair (int a,int b)
{
this.x=a;
this.y=b;
}
public int compareTo(Pair o) {
// TODO Auto-generated method stub
if(this.x!=o.x)
return -Integer.compare(this.x,o.x);
else
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;
}
public int hashCode() {
return new Integer(x).hashCode() * 31 + new Integer(y).hashCode();
}
}
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private SpaceCharFilter filter;
byte inbuffer[] = new byte[1024];
int lenbuffer = 0, ptrbuffer = 0;
final int M = (int) 1e9 + 7;
int md=(int)(1e7+1);
int[] SMP=new int[md];
final double eps = 1e-6;
final double pi = Math.PI;
PrintWriter out;
String check = "";
InputStream obj = check.isEmpty() ? System.in : new ByteArrayInputStream(check.getBytes());
public InputReader(InputStream stream)
{
this.stream = stream;
}
int readByte() {
if (lenbuffer == -1) throw new InputMismatchException();
if (ptrbuffer >= lenbuffer) {
ptrbuffer = 0;
try {
lenbuffer = obj.read(inbuffer);
} catch (IOException e) {
throw new InputMismatchException();
}
}
if (lenbuffer <= 0) return -1;
return inbuffer[ptrbuffer++];
}
public int read()
{
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;
}
String is() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) // when nextLine, (isSpaceChar(b) && b!=' ')
{
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int ii() {
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();
}
}
public long il() {
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();
}
}
boolean isSpaceChar(int c) {
return (!(c >= 33 && c <= 126));
}
int skip()
{
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
float nf() {
return Float.parseFloat(is());
}
double id() {
return Double.parseDouble(is());
}
char ic() {
return (char) skip();
}
int[] iia(int n) {
int a[] = new int[n];
for (int i = 0; i<n; i++) a[i] = ii();
return a;
}
long[] ila(int n) {
long a[] = new long[n];
for (int i = 0; i <n; i++) a[i] = il();
return a;
}
String[] isa(int n) {
String a[] = new String[n];
for (int i = 0; i < n; i++) a[i] = is();
return a;
}
long mul(long a, long b) { return a * b % M; }
long div(long a, long b)
{
return mul(a, pow(b, M - 2));
}
double[][] idm(int n, int m) {
double a[][] = new double[n][m];
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = id();
return a;
}
int[][] iim(int n, int m) {
int a[][] = new int[n][m];
for (int i = 0; i < n; i++) for (int j = 0; j <m; j++) a[i][j] = ii();
return a;
}
public String readLine() {
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 interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
| Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | 895474417bba5c27d84e48682ee807f7 | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | import java.util.Scanner;
import java.util.Stack;
public class C420C {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
int count = 0;
int goal = 1;
int safe = 0;
int peeked = 0;
int curStack = 0;
Stack<Integer> st = new Stack<Integer>();
int num = 0;
for(int x=0; x<2*n; x++){
String query = sc.nextLine();
if(query.charAt(0) == 'a'){ //add
num = Integer.parseInt(query.split(" ")[1]);
st.push(num);
//System.out.println("pushed "+num);
curStack ++;
}
else{ //remove
if(curStack == 0){
goal ++;
}
else{
num = st.pop();
if(num == goal){
goal ++;
curStack = Math.max(curStack - 1, 0);
}
else{
count ++;
goal ++;
curStack = 0;
}
}
}
}
System.out.print(count);
}
}
| Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | 73170b14464dba7a7d26a55173db2c49 | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | import java.util.*;
import java.io.*;
public class _0821_C_OkabeAndBoxes {
public static void main(String[] args) throws IOException {
int N = readInt(), cnt = 1, ans = 0; Stack<Integer> stk = new Stack<>();
for(int i = 1; i<=2*N; i++) {
if(read().equals("add")) stk.add(readInt()); else {
if(!stk.isEmpty() && stk.pop() != cnt) {stk.clear(); ans++;}
cnt++;
}
}
println(ans); exit();
}
final private static int BUFFER_SIZE = 1 << 16;
private static DataInputStream din = new DataInputStream(System.in);
private static byte[] buffer = new byte[BUFFER_SIZE];
private static int bufferPointer = 0, bytesRead = 0;
static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static String read() throws IOException {
byte[] ret = new byte[10];
int idx = 0;
byte c = Read();
while (c <= ' ') {
c = Read();
}
do {
ret[idx++] = c;
c = Read();
} while (c != -1 && c != ' ' && c != '\n' && c != '\r');
return new String(ret, 0, idx);
}
public static int readInt() throws IOException {
int ret = 0;
byte c = Read();
while (c <= ' ')
c = Read();
do {
ret = ret * 10 + c - '0';
} while ((c = Read()) >= '0' && c <= '9');
return ret;
}
private static void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private static byte Read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
static void print(Object o) {
pr.print(o);
}
static void println(Object o) {
pr.println(o);
}
static void flush() {
pr.flush();
}
static void println() {
pr.println();
}
static void exit() throws IOException {
din.close();
pr.close();
System.exit(0);
}
}
| Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | 4f3704cf496ab8d689c8f43dfac743e0 | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class MainClass
{
public static long hits=0;
public static void main(String[] args) throws IOException
{
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n=in.nextInt();
int n2=2*n;
int c=1,ans=0;
String s="";
Stack<Integer> st=new Stack<Integer>();
for(int i=0;i<n2;i++)
{
s=in.next();
if(s.charAt(0)=='a')
{
st.push(in.nextInt());
}
else
{
if(!st.isEmpty())
{
if(st.peek()==c)st.pop();
else
{
ans++;
while(!st.isEmpty())st.pop();
}
}
c++;
}
}
out.println(ans);
out.flush();
out.close();
}
}
class InputReader{
private final InputStream stream;
private final byte[] buf=new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream){this.stream=stream;}
public int read()throws IOException{
if(curChar>=numChars){
curChar=0;
numChars=stream.read(buf);
if(numChars<=0)
return -1;
}
return buf[curChar++];
}
public final int nextInt()throws IOException{return (int)nextLong();}
public final long nextLong()throws IOException{
int c=read();
while(isSpaceChar(c)){
c=read();
if(c==-1) throw new IOException();
}
boolean negative=false;
if(c=='-'){
negative=true;
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 negative?(-res):(res);
}
public final int[] readIntBrray(int size)throws IOException{
int[] arr=new int[size];
for(int i=0;i<size;i++)arr[i]=nextInt();
return arr;
}
public final String next()throws IOException{
int c=read();
while(isSpaceChar(c))c=read();
StringBuilder res=new StringBuilder();
do{
res.append((char)c);
c=read();
}while(!isSpaceChar(c));
return res.toString();
}
public final String nextLine()throws IOException{
int c=read();
while(isSpaceChar(c))c=read();
StringBuilder res=new StringBuilder();
do{
res.append((char)c);
c=read();
}while(c!='\n'&&c!=-1);
return res.toString();
}
private boolean isSpaceChar(int c){
return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
}
} | Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | 17ed1159ae32de7cce49e144f11839bc | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* Created by mostafa on 7/8/17.
*/
public class C {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner();
int n = sc.nextInt() * 2;
int res = 0;
int next = 1;
Stack<Integer> s = new Stack<>();
while(n-- > 0) {
String comm = sc.next();
if(comm.equals("remove")) {
if(s.size() > 0 && s.peek() != next) {
res++;
s.clear();
}
else if(s.size() > 0)
s.pop();
next++;
}
else {
int num = sc.nextInt();
s.add(num);
}
}
System.out.println(res);
}
static class Scanner {
BufferedReader br; StringTokenizer st;
Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while(st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
}
| Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | 55322c7081fd1659728c3bc87f01cc08 | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void solution(BufferedReader reader, PrintWriter writer)
throws IOException {
In in = new In(reader);
Out out = new Out(writer);
int n = in.nextInt(), cnt = 0, rst = 0;
int[] list = new int[1000000];
int index = 0;
for (int i = 0; i < n + n; i++) {
String s = in.next();
if (s.length() == 3)
list[++index] = in.nextInt();
else {
cnt++;
if (index > 0) {
if (list[index] == cnt)
index--;
else {
rst++;
index = 0;
}
}
}
}
out.println(rst);
}
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(System.out)));
solution(reader, writer);
writer.close();
}
protected static class In {
private BufferedReader reader;
private StringTokenizer tokenizer = new StringTokenizer("");
public In(BufferedReader reader) {
this.reader = reader;
}
public String next() throws IOException {
while (!tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public int[] nextIntArray1(int n) throws IOException {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++)
a[i] = nextInt();
return a;
}
public int[] nextIntArraySorted(int n) throws IOException {
int[] a = nextIntArray(n);
Random r = new Random();
for (int i = 0; i < n; i++) {
int j = i + r.nextInt(n - i);
int t = a[i];
a[i] = a[j];
a[j] = t;
}
Arrays.sort(a);
return a;
}
public long[] nextLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public long[] nextLongArray1(int n) throws IOException {
long[] a = new long[n + 1];
for (int i = 1; i <= n; i++)
a[i] = nextLong();
return a;
}
public long[] nextLongArraySorted(int n) throws IOException {
long[] a = nextLongArray(n);
Random r = new Random();
for (int i = 0; i < n; i++) {
int j = i + r.nextInt(n - i);
long t = a[i];
a[i] = a[j];
a[j] = t;
}
Arrays.sort(a);
return a;
}
}
protected static class Out {
private PrintWriter writer;
private static boolean local = System
.getProperty("ONLINE_JUDGE") == null;
public Out(PrintWriter writer) {
this.writer = writer;
}
public void print(char c) {
writer.print(c);
}
public void print(int a) {
writer.print(a);
}
public void printb(int a) {
writer.print(a);
writer.print(' ');
}
public void println(Object a) {
writer.println(a);
}
public void println(Object[] os) {
for (int i = 0; i < os.length; i++) {
writer.print(os[i]);
writer.print(' ');
}
writer.println();
}
public void println(int[] a) {
for (int i = 0; i < a.length; i++) {
writer.print(a[i]);
writer.print(' ');
}
writer.println();
}
public void println(long[] a) {
for (int i = 0; i < a.length; i++) {
writer.print(a[i]);
writer.print(' ');
}
writer.println();
}
public void flush() {
writer.flush();
}
public static void db(Object... objects) {
if (local)
System.out.println(Arrays.deepToString(objects));
}
}
}
| Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | 6d25d8c85e2089cc3eabe9cb1d0fc94d | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class C {
private static final String REGEX = " ";
private static final Boolean DEBUG = false;
private static final String FILE_NAME = "input.txt";
public static void main(String[] args) throws IOException {
if (DEBUG) {
generate();
}
Solver solver = new Solver();
solver.readData();
solver.solveAndPrint();
}
private static void generate() throws IOException {
// FileWriter writer = new FileWriter("input.txt");
// writer.close();
}
private static class Solver {
int n;
boolean add[];
int[] addNumber;
void readData() throws IOException {
InputStream in = DEBUG ? new FileInputStream(FILE_NAME) : System.in;
Scanner scanner = new Scanner(in);
n = scanner.nextInt();
scanner.nextLine();
add = new boolean[2 * n];
addNumber = new int[n];
int c = 0;
for (int i = 0; i < 2 * n; i++) {
String[] strings = splitString(scanner.nextLine());
if (strings[0].startsWith("a")) {
add[i] = true;
addNumber[c] = Integer.parseInt(strings[1]);
c++;
}
}
scanner.close();
}
void solveAndPrint() {
PriorityQueue<Integer> queue = new PriorityQueue<>();
Deque<Integer> stack = new ArrayDeque<>();
int c = 0;
boolean sorted = true;
int result = 0;
for (int i = 0; i < 2 * n; i++) {
if (add[i]) {
int next = addNumber[c++];
if (!queue.isEmpty() && next > queue.peek()) {
sorted = false;
}
stack.push(next);
queue.offer(next);
} else {
if (!stack.isEmpty() && Objects.equals(stack.peek(), queue.peek())){
stack.pop();
queue.poll();
} else {
if (!sorted) {
sorted = true;
stack = new ArrayDeque<>();
result++;
}
queue.poll();
}
}
}
System.out.println(result);
}
@SuppressWarnings("SameParameterValue")
int[] splitInteger(String string, int n) {
final String[] split = string.split(REGEX, n);
int[] result = new int[split.length];
for (int i = 0; i < n; ++i) {
result[i] = Integer.parseInt(split[i]);
}
return result;
}
public int[] splitInteger(String string) {
return splitInteger(string, 0);
}
@SuppressWarnings("SameParameterValue")
long[] splitLong(String string, int n) {
final String[] split = string.split(REGEX, n);
long[] result = new long[split.length];
for (int i = 0; i < n; ++i) {
result[i] = Long.parseLong(split[i]);
}
return result;
}
public long[] splitLong(String string) {
return splitLong(string, 0);
}
@SuppressWarnings("SameParameterValue")
double[] splitDouble(String string, int n) {
final String[] split = string.split(REGEX, n);
double[] result = new double[split.length];
for (int i = 0; i < n; ++i) {
result[i] = Double.parseDouble(split[i]);
}
return result;
}
public double[] splitDouble(String string) {
return splitDouble(string, 0);
}
@SuppressWarnings("SameParameterValue")
String[] splitString(String string, int n) {
return string.split(REGEX, n);
}
public String[] splitString(String string) {
return splitString(string, 0);
}
public int max(int a, int b) {
return Math.max(a, b);
}
public long max(long a, long b) {
return Math.max(a, b);
}
public int min(int a, int b) {
return Math.min(a, b);
}
public long min(long a, long b) {
return Math.min(a, b);
}
public double max(double a, double b) {
return Math.max(a, b);
}
public double min(double a, double b) {
return Math.min(a, b);
}
private final static int MOD = 1000000009;
int multMod(int a, int b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
int sumMod(int a, int b) {
return ((a % MOD) + (b % MOD)) % MOD;
}
long multMod(long a, long b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
long sumMod(long a, long b) {
return ((a % MOD) + (b % MOD)) % MOD;
}
}
}
| Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | 2af870a290478cd22f8b6c362b44bfaa | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
import java.security.KeyStore.Entry;
public class Q2 {
static ArrayList<Integer> adj[];
static int color[],red[],black[],previs[];
static boolean b[],visited[],possible;
static Map<Long,Long> dict;
static int totalnodes,colored,time,v[],l[],r[];
static int p[];
static HashMap<Integer,int[]> hm;
void solve(){
in=new InputReader(System.in);
w=new PrintWriter(System.out);
int n=in.nextInt();
int max=1;boolean flag=true;
int ans=0;
int pos=0;
int p=0;
int a[]=new int[2*n];
for(int i=1;i<=2*n;i++)
{
String s=in.readString();
//debug(s);
if(s.charAt(0)=='a')
{
int inp=in.nextInt();
a[pos++]=inp;
}
else
{
if(a[--pos]==max++ || pos<p)
;
else
{
p=pos;
ans++;
}
p=Math.min(p, pos);
}
}
w.println(ans);
w.close();
}
public static void main(String[] args) throws IOException{
new Q2().solve();
}
int modularExponentiation(int x,int n,int M)
{
if(n==0)
return 1;
else if(n%2 == 0) //n is even
return modularExponentiation((x*x)%M,n/2,M);
else //n is odd
return (x*modularExponentiation((x*x)%M,(n-1)/2,M))%M;
}
/* void dfs(int u,int low,int high)
{
if(u==-1 || low>high)
return;
if(low<=v[u] && v[u]<=high){
hm.put(v[u],true);
}
dfs(l[u],low,high<v[u]-1?high:v[u]-1);
dfs(r[u],low>v[u]+1?low:v[u]+1,high);
}
int dfs_red(int i)
{
previs[i]=time++;
visited[i]=true;
if(color[i]==0)
red[i]++;
for(int j:adj[i])
{
if(!visited[j])
red[i]+=dfs_red(j);
}
return red[i];
}
int dfs_black(int i)
{
visited[i]=true;
if(color[i]==1)
black[i]++;
for(int j:adj[i])
{
if(!visited[j])
black[i]+=dfs_black(j);
}
return black[i];
}*/
public static class dsu
{
int n;
int parent[];
int s[];
dsu(int n)
{
this.n=n;
parent=new int[n+1];
s=new int[n+1];
for(int i=0;i<=n;i++)
{
parent[i]=i;
s[i]=1;
}
}
int find(int i)
{
if (parent[i] == i)
return i;
else
{
int result = find(parent[i]);
parent[i] = result;
return result;
}
}
}
static InputReader in;
static PrintWriter w;
public boolean oj = System.getProperty("ONLINE_JUDGE") != null;
public void tr(Object... o) {
if (!oj)
System.out.println(Arrays.deepToString(o));
}
public static void seive(long n){
b = new boolean[(int) (n+1)];
Arrays.fill(b, true);
for(int i = 2;i*i<=n;i++){
if(b[i]){
for(int p = 2*i;p<=n;p+=i){
b[p] = false;
}
}
}
}
public static long power(long a,long b,long c)
{
long ans = 1;
while(b!=0)
{
if(b%2==1)
{
ans = ans*a;
ans = ans % c;
}
a = a*a;
a = a%c;
b = b/2;
}
return ans;
}
static class Pair implements Comparable<Pair> {
int u,v,w;
public Pair(){
}
public Pair(int u,int v) {
this.u=u;
this.v=v;
this.w=Math.min(u, v);
}
public int compareTo(Pair other) {
int o=Math.min(other.u,other.v)-Math.min(other.u/2, other.v);
int p=Math.min(this.u,this.v)-Math.min(this.u/2, this.v);
return o-p;
}
/*public String toString() {
return "[u=" + u + ", v=" + v + "]";
}*/
}
static class Node2{
Node2 left = null;
Node2 right = null;
Node2 parent = null;
int data;
}
//binaryStree
static class BinarySearchTree{
Node2 root = null;
int height = 0;
int max = 0;
int cnt = 1;
ArrayList<Integer> parent = new ArrayList<>();
HashMap<Integer, Integer> hm = new HashMap<>();
public void insert(int x){
Node2 n = new Node2();
n.data = x;
if(root==null){
root = n;
}
else{
Node2 temp = root,temp2 = null;
while(temp!=null){
temp2 = temp;
if(x>temp.data) temp = temp.right;
else temp = temp.left;
}
if(x>temp2.data) temp2.right = n;
else temp2.left = n;
n.parent = temp2;
parent.add(temp2.data);
}
}
public Node2 getSomething(int x, int y, Node2 n){
if(n.data==x || n.data==y) return n;
else if(n.data>x && n.data<y) return n;
else if(n.data<x && n.data<y) return getSomething(x,y,n.right);
else return getSomething(x,y,n.left);
}
public Node2 search(int x,Node2 n){
if(x==n.data){
max = Math.max(max, n.data);
return n;
}
if(x>n.data){
max = Math.max(max, n.data);
return search(x,n.right);
}
else{
max = Math.max(max, n.data);
return search(x,n.left);
}
}
public int getHeight(Node2 n){
if(n==null) return 0;
height = 1+ Math.max(getHeight(n.left), getHeight(n.right));
return height;
}
}
public static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public static String rev(String s)
{
StringBuilder sb=new StringBuilder(s);
sb.reverse();
return sb.toString();
}
static long lcm(long a, long b)
{
return a * (b / gcd(a, b));
}
static long gcd(long a, long b)
{
while (b > 0)
{
long temp = b;
b = a % b; // % is remainder
a = temp;
}
return a;
}
public static long max(long x, long y, long z){
if(x>=y && x>=z) return x;
if(y>=x && y>=z) return y;
return z;
}
static int[] sieve(int n,int[] arr)
{
for(int i=2;i*i<=n;i++)
{
if(arr[i]==0)
{
for(int j=i*2;j<=n;j+=i)
arr[j]=1;
}
}
return arr;
}
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) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
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 | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | 9470c4c40d506390c1f3f35f637d99db | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | /*
* @Author Silviase(@silviasetitech)
* For ProCon
*/
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main{
static int MOD = (int)1e9+7;
// for dfs
static int n;
static int ansi;
static int[] w;
static int[] ww;
static boolean[] isvisited;
//
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Deque<Integer> deque = new ArrayDeque<>();
int p = 1;
int ans = 0;
for (int i = 0; i < 2*n; i++) {
String tmp = sc.next();
if (tmp.equals("add")) {
int num = sc.nextInt();
deque.push(num);
}else{ //remove
if (deque.size() != 0) {
int x = deque.peek();
if (x != p) {
ans++;
deque.clear();
}else{
if (deque.size() != 0){
deque.pop();
}
}
}
p++;
}
}
System.out.println(ans);
sc.close();
}
private static long[][] CombinationLong_nCk(long n, long k) {
n++; k++;
long[][] ans = new long[(int)n][(int)k];
for (int i = 0; i < n; i++) {
ans[i][0] = 1;
}
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < k-1; j++) {
if (i < j) {
ans[i][j] = 0;
}else{
ans[i+1][j+1] = ans[i][j] + ans[i][j+1];
}
}
}
return ans;
}
public static int gcd_int(int a, int b) {
if (b == 0) {
return a;
}else{
return gcd_int(b, a%b);
}
}
public static long gcd_long(long a, long b) {
if (a < b) {
long x = a;
a = b;
b = x;
}
if (b == 0) {
return a;
}else{
return gcd_long(b, a%b);
}
}
public static int lcm_int(int a, int b) {
int gcd = gcd_int(a,b);
return a/gcd*b;
}
public static long lcm_long(long a, long b) {
long gcd = gcd_long(a,b);
return a/gcd*b;
}
public static int ManhattanDist(int x, int y, int xx, int yy) {
return(Math.abs(xx-x) + Math.abs(yy-y));
}
public static void dfs(int placep) {
// if went all -> success!
// if not went all -> fail...
/*
dfs
Go All Place that there is wayto to and not having been yet.
if island 1 is start point, dfs(1);
if there is wayto to island 2 and island 3,
- island 2 changes non-visited -> visited, and dfs(2).
- island 3 changes non-visited -> visited, and dfs(3).
*/
isvisited[placep] = true;
boolean success = true;
for (int i = 0; i < n; i++) {
if (isvisited[i] == false) { // not go all place
success = false;
break;
}
}
if (success) {
ansi++;
isvisited[placep] = false;
return;
}
for (int i = 0; i < n; i++) {
if (w[i] == placep && isvisited[ww[i]] == false ) {
dfs(ww[i]);
}else if(ww[i] == placep && isvisited[w[i]] == false){
dfs(w[i]);
}else{
continue;
}
}
isvisited[placep] = false;
return;
}
}
| Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | b37cd77ef00ccbdeac53238d83be98f6 | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Stack<Integer> main = new Stack<>();
// Stack<Integer>
PriorityQueue<Integer> pq = new PriorityQueue<>();
int count = 0;
// int reqtop=1;
// int curtop=1;
int cur = 1;
int mix = 1000000;
for (int i = 0; i < 2 * n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
String c = st.nextToken();
if (c.equals("add")) {
int item = Integer.parseInt(st.nextToken());
main.push(item);
} else {
if(main.isEmpty()){}
else if(main.peek()==cur){main.pop();}
else{count++;
main=new Stack<>();}
cur++;
}
}
System.out.println(count);
}
} | Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | 7a9eced983229e8976c7439aafe4f9d8 | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.AbstractCollection;
import java.util.PriorityQueue;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Shurikat
*/
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) {
int n = in.nextInt();
// boolean sorted = true;
int ans = 0;
PriorityQueue<Integer> min = new PriorityQueue<>();
boolean[] sorted = new boolean[n + 1];
int sort = 1;
boolean mis = false;
for (int i = 0; i < 2 * n; ++i) {
String[] tokens = in.nextLine(10).split(" ");
if (tokens[0].equals("add")) {
int a = Integer.parseInt(tokens[1]);
if (min.isEmpty() || a < min.peek()) {
sorted[min.size() + 1] = true;
if (!mis) {
sort = min.size() + 1;
}
} else {
sorted[min.size() + 1] = false;
mis = true;
}
min.add(a);
} else {
if (!sorted[min.size()] && sort < min.size()) {
sort = min.size();
mis = false;
++ans;
}
min.poll();
--sort;
}
}
out.print(ans);
}
}
static class InputReader implements AutoCloseable {
private final InputStream in;
private int capacity;
private byte[] buffer;
private int len = 0;
private int cur = 0;
public InputReader(InputStream stream) {
this(stream, 100_000);
}
public InputReader(InputStream stream, int capacity) {
this.in = stream;
this.capacity = capacity;
buffer = new byte[capacity];
}
private boolean update() {
if (cur >= len) {
try {
cur = 0;
len = in.read(buffer, 0, capacity);
if (len <= 0)
return false;
} catch (IOException e) {
throw new InputMismatchException();
}
}
return true;
}
private int read() {
if (update())
return buffer[cur++];
else return -1;
}
private boolean isSpace(int c) {
return c == '\n' || c == '\t' || c == '\r' || c == ' ';
}
private boolean isEscape(int c) {
return c == '\n' || c == '\t' || c == '\r' || c == -1;
}
private int readSkipSpace() {
int c;
do {
c = read();
} while (isSpace(c));
return c;
}
public int nextInt() {
int c = readSkipSpace();
if (c < 0)
throw new InputMismatchException();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c == -1) break;
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
if (res < 0)
throw new InputMismatchException();
c = read();
} while (!isSpace(c));
res *= sgn;
return res;
}
public String nextLine(int initialCapacity) {
StringBuilder res = new StringBuilder(initialCapacity);
int c = readSkipSpace();
do {
res.append((char) (c));
c = read();
} while (!isEscape(c));
return res.toString();
}
public void close() throws IOException {
in.close();
}
}
}
| Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | b805aee4d33bedd1d2998dea98c144b6 | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main
{
static int q,mx,sumG,level,sumB,c,n,m,cnt,cnt1,sum;
static char t;
static int arr[];
static int mat[][];
static int freq[];
static int lvl[];
static boolean vis2[][];
static boolean vis[];
static Set<Integer>adj[];
static TreeSet<Integer>set=new TreeSet<>();
static LinkedList<Integer>li=new LinkedList<>();
static LinkedList<Integer>li2=new LinkedList<>();
static PrintWriter pw=new PrintWriter(System.out);
public static void main(String args[])throws
UnsupportedEncodingException, IOException, Exception
{
Reader.init(System.in);
int x=Reader.nextInt();
int cn=0,te=0;boolean fl=false;
vis=new boolean[x+1];
for(int i=0;i<2*x;i++)
{
String s=Reader.next();
if(s.equals("remove"))
{ te++;
if(li.size()>0)
{
int c=li.getLast();
if(c==te)
{li.removeLast();}
else
{
cn++;
li.clear();
}
}
}
else
{
int ele=Reader.nextInt();
li.addLast(ele);
set.add(ele);
vis[ele]=true;
fl=false;
}
}
pw.println(cn);
pw.close();
}
private static long gcd(long a, long b) {
while (b > 0) {
long temp = b;
b = a % b;
a = temp;
}
return a;
}
private static long lcm(long a, long b) {
return a * (b / gcd(a, b));
}
public static boolean isPrime(long n)
{
boolean flag = false;
for(int i = 2; i <= n/2; ++i)
{
if(n% i == 0)
{
flag = true;
break;
}
}
if (!flag)
return true;
else
return false;
}
public static void Bfs()
{
set.add(0);
while(!set.isEmpty())
{
int ele=set.pollFirst();
vis[ele]=true;
pw.print((ele+1)+" ");
for(int j:adj[ele])
{if(!vis[j])set.add(j);}
}
}
public static void dfs(int i)
{
vis[i]=true;
mx++;
if(adj[i].size()==2)sum++;
for(int j:adj[i])
{
if(!vis[j])
dfs(j);
}
}
public static void dfs2(int i,int j)
{
if(i>0&&i<n+2&&j>0&&j<m+2&&mat[i][j]!=-1&&cnt<n+m-1)
{
//vis2[i][j]=true;
li.addLast(mat[i][j]);
if(i==n&&j==m)
{pw.println(li);i=1;j=1;cnt++;li.clear();}
dfs2(i+1,j);
dfs2(i,j+1);
}
}
public static int count()
{
int cnt=0;
int index;
while((index=check())!=-1)
{
dfs(index);cnt++;
}
return cnt;
}
public static int check()
{
for(int i=0;i<vis.length; i++)
if(!vis[i])return i;
return -1;
}
}
class Pair<S extends Comparable<S>, T extends Comparable<T>> implements Comparable<Pair<S, T>> {
S first;
T second;
Pair(S f, T s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair<S, T> o) {
int t = first.compareTo(o.first);
if (t == 0) return second.compareTo(o.second);
return t;
}
@Override
public int hashCode() {
return (31 + first.hashCode()) * 31 + second.hashCode();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
if (o == this) return true;
Pair p = (Pair) o;
return first.equals(p.first) && second.equals(p.second);
}
@Override
public String toString() {
return "Pair{" + first + ", " + second + "}";
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) throws UnsupportedEncodingException {
reader = new BufferedReader(
new InputStreamReader(input, "UTF-8"));
tokenizer = new StringTokenizer("");
}
static void init(String url) throws FileNotFoundException {
reader = new BufferedReader(new FileReader(url));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static String nextLine() throws IOException {
return reader.readLine();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
| Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | 31ad65e49ef5b09621abcd08ef3e353b | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | import java.util.*;
import java.io.*;
public class A
{
public static void main(String ar[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
Stack<Integer> st=new Stack<Integer>();
TreeSet<Integer> ts=new TreeSet<Integer>(); int u=1; int b=0;
for(int i=0;i<2*n;i++)
{
String S=br.readLine();
if(S.equals("remove"))
{
if(st.peek()!=-1 && st.peek()==u)
{ ts.remove(u); st.pop(); u++; }
else if(st.peek()!=-1 && st.peek()!=u)
{ b++; st.add(-1); ts.remove(u); u++; }
else
{ ts.remove(u); u++; }
}
else
{
String s1[]=S.split(" ");
int x=Integer.parseInt(s1[1]);
ts.add(x); st.add(x);
}
}
System.out.println(b);
}
} | Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | 203b0ff0f8606f42b54a020510dcc083 | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | import java.util.*;
import java.io.*;
public class A
{
public static void main(String ar[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
Stack<Integer> st=new Stack<Integer>();
TreeSet<Integer> ts=new TreeSet<Integer>();
int u=1;
int t=0;
for(int i=0;i<2*n;i++)
{
String s1[]=br.readLine().split(" ");
if(s1[0].equals("add"))
{
int x=Integer.parseInt(s1[1]);
st.add(x);
ts.add(x);
}
else
{
if( st.peek()!=-1 && st.peek()!=u)
{
st.push(-1);
ts.remove(u);
t++;
}
else if(st.peek()==-1)
{
ts.remove(u);
}
else
{
st.pop();
ts.remove(u);
}
u++;
}
}
System.out.println(t);
}
} | Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | d30894ef2be034f7c928d85ed731cbff | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter pw = new PrintWriter(System.out);
while (sc.hasNext()) {
int n=sc.nextInt();
int count=0;
int temp=-1;
int cur=1;
boolean p=false;
int max=-1;
Stack<Integer>stack=new Stack<Integer>();
for(int i=0;i<n*2;i++){
String s=sc.next();
if(s.equals("add")){
int index=sc.nextInt();
stack.add(index);
}else{
if(stack.isEmpty()){
cur++;
}else if(stack.peek()!=cur){
if(!stack.isEmpty()){
stack.clear();
cur++;
count++;
}
}
else{
stack.pop();
cur++;
}
}
}
pw.println(count);
pw.flush();
}
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean hasNext() {
while (!st.hasMoreTokens()) {
String line = nextLine();
if (line == null) {
return false;
}
st = new StringTokenizer(line);
}
return true;
}
public String next() {
while(!st.hasMoreTokens()){
st=new StringTokenizer(nextLine());
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String line = "";
try {
line = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return line;
}
} | Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | bc442b3ab6f8355507a7f8433df6b55b | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
public class Prac{
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
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 = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nl() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nia(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public String rs() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
public static class Key {
private final int x;
private final int y;
public Key(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Key)) return false;
Key key = (Key) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
static class Pair{
int x,y;
public Pair(int x,int y){
this.x=x;
this.y=y;
}
}
static PrintWriter w = new PrintWriter(System.out);
static long mod=998244353L,mod1=1000000007,res;
static ArrayList<Integer> arr[];
static boolean ind[];
static int child[],level[];
static int dfs(int i,int p,int l){
int c=0;
level[i]=l;
for(int j:arr[i]){
if(j!=p){
c+=dfs(j,i,l+1);
}
}
child[i]=c;
return c+1;
}
static void dfs1(int i,int p,long c){
if(!ind[i])c++;
else res+=(c);
for(int j:arr[i]){
if(j!=p)dfs1(j,i,c);
}
}
public static void main(String [] args){
InputReader sc=new InputReader(System.in);
int n=sc.ni();
int j=1;
Stack<Integer> s=new Stack<>();
Stack<Integer> re=new Stack<>();
int ans=0;
Set<Integer> reorder=new HashSet<>();
for(int i=0;i<2*n;i++){
String a[]=sc.nextLine().split(" ");
if(a[0].equals("add")){
int x=Integer.parseInt(a[1]);
s.add(x);
}
else{
if(reorder.contains(j)&&s.isEmpty()){
j++;
}
else{
if(s.peek()==j){
j++;
s.pop();
}
else{
while(!s.isEmpty()){
reorder.add(s.pop());
}
ans++;
j++;
}
}
}
}
w.println(ans);
w.close();
}
} | Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | 52bf6caab346cd185e83d9428f74ca5e | train_002.jsonl | 1498401300 | Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
public class Prac{
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
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 = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nl() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nia(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public String rs() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
public static class Key {
private final int x;
private final int y;
public Key(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Key)) return false;
Key key = (Key) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
static class Pair{
int x,y;
public Pair(int x,int y){
this.x=x;
this.y=y;
}
}
static PrintWriter w = new PrintWriter(System.out);
static long mod=998244353L,mod1=1000000007,res;
static ArrayList<Integer> arr[];
static boolean ind[];
static int child[],level[];
static int dfs(int i,int p,int l){
int c=0;
level[i]=l;
for(int j:arr[i]){
if(j!=p){
c+=dfs(j,i,l+1);
}
}
child[i]=c;
return c+1;
}
static void dfs1(int i,int p,long c){
if(!ind[i])c++;
else res+=(c);
for(int j:arr[i]){
if(j!=p)dfs1(j,i,c);
}
}
public static void main(String [] args){
InputReader sc=new InputReader(System.in);
int n=sc.ni();
int j=1;
Stack<Integer> s=new Stack<>();
Stack<Integer> re=new Stack<>();
int ans=0;
boolean v[]=new boolean[n+1];
for(int i=0;i<2*n;i++){
String a[]=sc.nextLine().split(" ");
if(a[0].equals("add")){
int x=Integer.parseInt(a[1]);
s.add(x);
}
else{
if(v[j]&&s.isEmpty()){
}
else if(s.peek()==j){
s.pop();
}
else{
while(!s.isEmpty())v[s.pop()]=true;
ans++;
}
j++;
}
}
w.println(ans);
w.close();
}
} | Java | ["3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove"] | 3 seconds | ["1", "2"] | NoteIn the first sample, Daru should reorder the boxes after adding box 3 to the stack.In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | Java 8 | standard input | [
"data structures",
"greedy",
"trees"
] | 2535fc09ce74b829c26e1ebfc1ee17c6 | The first line of input contains the integer n (1ββ€βnββ€β3Β·105)Β β the number of boxes. Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1ββ€βxββ€βn) follows, indicating that Daru should add the box with number x to the top of the stack. It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | 1,500 | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | standard output | |
PASSED | ea3c40709b90ff7716c8bb84a04d0528 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
private static PrintWriter out;
private static FastReader in;
private static class FastReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public FastReader(InputStream inputStream) {
reader = new BufferedReader(
new InputStreamReader(inputStream), 1 << 16);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
try {
return reader.readLine();
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
}
public static void main(String[] args) throws FileNotFoundException {
/*in = new Scanner(new File("input.txt"));
out = new PrintWriter(new File("output.txt"));*/
in = new FastReader(System.in);
out = new PrintWriter(System.out);
int n = in.nextInt(), m = in.nextInt();
int k = in.nextInt();
int[] ans = new int[m];
Arrays.fill(ans, 0);
in.nextLine();
for (int i = 1; i < n; i++) {
String input = in.nextLine();
for (int j = 0; j < m; j++) {
switch (input.charAt(j)) {
case '.':
break;
case 'R':
if (i + j < m)
ans[j+i]++;
break;
case 'L':
if (j - i >= 0)
ans[j-i]++;
break;
case 'U':
if (i % 2 == 0)
ans[j]++;
break;
case 'D':
break;
}
}
}
for (int i : ans)
out.print(i + " ");
out.println();
out.flush();
}
} | Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | 14a9b20a742db6051cdecab6f34ef425 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
/**
* @author master_j
* @version 0.4
* @since May 3, 2014
*/
public class Main {
private void solve() throws IOException {
int n = io.nI(), m = io.nI(), k = io.nI();
io.wc(".LRUD");
char[][] map = new char[n][];
for(int i = 0; i < n; i++)
map[i] = io.nS().toCharArray();
for(int j = 0; j < m; j++){
int spiders = 0;
for(int i = 0; i < n; i++) {
if(i + i < n && map[i + i][j] == 'U')//look down
spiders++;
if(i + j < m && map[i][i + j] == 'L')//look right
spiders++;
if(0 <= j - i && map[i][j - i] == 'R')//look left
spiders++;
}
io.wf("%d ", spiders);
}
io.wln();
}//2.2250738585072012e-308
public static void main(String[] args) throws IOException {
IO.launchSolution(args);
}
Main(IO io) throws IOException {
this.io = io;
solve();
}
private final IO io;
}
class IO {
static final String _localArg = "master_j";
private static final String _problemName = "";
private static final IO.Mode _inMode = Mode.STD_;
private static final IO.Mode _outMode = Mode.STD_;
private static final boolean _autoFlush = false;
static enum Mode {STD_, _PUT_TXT, PROBNAME_}
private final StreamTokenizer st;
private final BufferedReader br;
private final Reader reader;
private final PrintWriter pw;
private final Writer writer;
static void launchSolution(String[] args) throws IOException {
boolean local = (args.length == 1 && args[0].equals(IO._localArg));
IO io = new IO(local);
long nanoTime = 0;
if (local) {
nanoTime -= System.nanoTime();
io.wln("<output>");
}
io.flush();
new Main(io);
io.flush();
if(local){
io.wln("</output>");
nanoTime += System.nanoTime();
final long D9 = 1000000000, D6 = 1000000, D3 = 1000;
if(nanoTime >= D9)
io.wf("%d.%d seconds\n", nanoTime/D9, nanoTime%D9);
else if(nanoTime >= D6)
io.wf("%d.%d millis\n", nanoTime/D6, nanoTime%D6);
else if(nanoTime >= D3)
io.wf("%d.%d micros\n", nanoTime/D3, nanoTime%D3);
else
io.wf("%d nanos\n", nanoTime);
}
io.close();
}
IO(boolean local) throws IOException {
if(_inMode == Mode.PROBNAME_ || _outMode == Mode.PROBNAME_)
if(_problemName.length() == 0)
throw new IllegalStateException("You imbecile. Where's my <_problemName>?");
if(_problemName.length() > 0)
if(_inMode != Mode.PROBNAME_ && _outMode != Mode.PROBNAME_)
throw new IllegalStateException("You imbecile. What's the <_problemName> for?");
Locale.setDefault(Locale.US);
if (local) {
reader = new FileReader("input.txt");
writer = new OutputStreamWriter(System.out);
} else {
switch (_inMode) {
case STD_:
reader = new InputStreamReader(System.in);
break;
case PROBNAME_:
reader = new FileReader(_problemName + ".in");
break;
case _PUT_TXT:
reader = new FileReader("input.txt");
break;
default:
throw new NullPointerException("You imbecile. Gimme _inMode.");
}
switch (_outMode) {
case STD_:
writer = new OutputStreamWriter(System.out);
break;
case PROBNAME_:
writer = new FileWriter(_problemName + ".out");
break;
case _PUT_TXT:
writer = new FileWriter("output.txt");
break;
default:
throw new NullPointerException("You imbecile. Gimme _outMode.");
}
}
br = new BufferedReader(reader);
st = new StreamTokenizer(br);
pw = new PrintWriter(writer, _autoFlush);
if(local && _autoFlush)
wln("Note: auto-flush is on.");
}
void wln() {pw.println(); }
void wln(boolean x) {pw.println(x);}
void wln(char x) {pw.println(x);}
void wln(char x[]) {pw.println(x);}
void wln(double x) {pw.println(x);}
void wln(float x) {pw.println(x);}
void wln(int x) {pw.println(x);}
void wln(long x) {pw.println(x);}
void wln(Object x) {pw.println(x);}
void wln(String x) {pw.println(x);}
void wf(String f, Object...o){pw.printf(f, o);}
void w(boolean x) {pw.print(x);}
void w(char x) {pw.print(x);}
void w(char x[]) {pw.print(x);}
void w(double x) {pw.print(x);}
void w(float x) {pw.print(x);}
void w(int x) {pw.print(x);}
void w(long x) {pw.print(x);}
void w(Object x) {pw.print(x);}
void w(String x) {pw.print(x);}
int nI() throws IOException {st.nextToken(); return (int)st.nval;}
double nD() throws IOException {st.nextToken(); return st.nval;}
float nF() throws IOException {st.nextToken(); return (float)st.nval;}
long nL() throws IOException {st.nextToken(); return (long)st.nval;}
String nS() throws IOException {st.nextToken(); return st.sval;}
void wc(String x){ wc(x.toCharArray()); }
void wc(char c1, char c2){for(char c = c1; c<=c2; c++)wc(c);}
void wc(char x[]){
for(char c : x)
wc(c);
}
void wc(char x){st.ordinaryChar(x); st.wordChars(x, x);}
public boolean eof() {return st.ttype == StreamTokenizer.TT_EOF;}
public boolean eol() {return st.ttype == StreamTokenizer.TT_EOL;}
void flush(){pw.flush();}
void close() throws IOException{reader.close(); br.close(); flush(); pw.close();}
} | Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | 2bf01ae92349d20a7ab9f910d8086993 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Agostinho Junior (junior94)
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
OutputWriter out;
InputReader in;
public void solve(int testNumber, InputReader in, OutputWriter out) {
this.in = in; this.out = out;
int n = in.readInt();
int m = in.readInt();
int k = in.readInt();
char[][] grid = new char[n][m];
int[] count = new int[m];
for (int i = 0; i < n; i++) {
grid[i] = in.readLine().toCharArray();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (j >= i && grid[i][j - i] == 'R') {
count[j]++;
}
if (j + i < m && grid[i][j + i] == 'L') {
count[j]++;
}
if (i + i < n && grid[i + i][j] == 'U') {
count[j]++;
}
}
}
for (int i = 0; i < m; i++) {
out.printf("%d ", count[i]);
}
}
}
class OutputWriter {
private PrintWriter output;
public OutputWriter() {
this(System.out);
}
public OutputWriter(OutputStream out) {
output = new PrintWriter(out);
}
public OutputWriter(Writer writer) {
output = new PrintWriter(writer);
}
public void printf(String format, Object... o) {
output.printf(format, o);
}
public void close() {
output.close();
}
}
class InputReader {
private BufferedReader input;
private StringTokenizer line = new StringTokenizer("");
public InputReader() {
this(System.in);
}
public InputReader(InputStream in) {
input = new BufferedReader(new InputStreamReader(in));
}
public InputReader(String s) {
try {
input = new BufferedReader(new FileReader(s));
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void fill() {
try {
if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine());
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public int readInt() {
fill();
return Integer.parseInt(line.nextToken());
}
public String readLine() {
String s = "";
try {
s = input.readLine();
} catch(IOException io) {io.printStackTrace(); System.exit(0);}
return s;
}
}
| Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | 69d366e497ea40e1911b363ad677bb93 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.*;
import java.util.StringTokenizer;
/**
* Created by Andrew Govorovsky on 15.06.14
*/
public final class cf436b {
public static void main(String[] args) {
InputReader inputReader = new InputReader(System.in);
PrintWriter outputStreamWriter = new PrintWriter(System.out);
TaskB taskB = new TaskB(inputReader, outputStreamWriter);
taskB.solve();
outputStreamWriter.close();
}
static class TaskB {
private InputReader in;
private PrintWriter out;
public TaskB(InputReader in, PrintWriter out) {
this.in = in;
this.out = out;
}
public void solve() {
int n = in.readInt();
int m = in.readInt();
int k = in.readInt();
int ans[] = new int[m];
for (int i = 0; i < n; i++) {
char[] map;
map = in.nextString().toCharArray();
for (int j = 0; j < m; j++) {
if (map[j] == 'U' && (i % 2 == 0)) {
ans[j]++;
}
if (map[j] == 'L' && (j - i >= 0)) {
ans[j - i]++;
}
if (map[j] == 'R' && (i + j < m)) {
ans[i + j]++;
}
}
}
for (int a : ans) {
out.print(a + " ");
}
}
}
static class InputReader {
private BufferedReader bufferedReader;
private StringTokenizer stringTokenizer;
InputReader(InputStream inputStream) {
this.bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
}
public String nextString() {
while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
try {
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
} catch (IOException | RuntimeException e) {
}
}
return stringTokenizer.nextToken();
}
public int readInt() {
return Integer.parseInt(nextString());
}
}
}
| Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | 2efc209c7d338848ecf0bf0d17e7f5c6 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class B {
static void solve(char[][] f, int k, int[] out) {
int n=f.length, m=f[0].length;
for (int col=0; col<m; col++) {
for (int row=0; row<n; row++) {
if(row+row<n&&f[row+row][col]=='U') {
out[col]++;
}
if(row+col<m&&f[row][row+col]=='L') {
out[col]++;
}
if(col-row>=0&&f[row][col-row]=='R') {
out[col]++;
}
}
}
}
static void run_stream(InputStream ins) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] nmk=br.readLine().split(" ");
int n=Integer.parseInt(nmk[0]);
int m=Integer.parseInt(nmk[1]);
int k=Integer.parseInt(nmk[2]);
char[][] f=new char[n][];
for (int i=0; i<n; i++) {
f[i]=br.readLine().toCharArray();
}
int[] out=new int[m];
solve(f, k, out);
StringBuilder sb=new StringBuilder();
for (int i=0; i<m; i++) {
sb.append(out[i]+" ");
}
System.out.println(sb.toString());
}
public static void main(String[] args) throws IOException {
run_stream(System.in);
}
}
| Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | c3013a8d5f92d75e244d7c2e342e4163 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
/**
* Author: Sergey Paramonov
* Date: 13.06.14
* Time: 19:03
*/
public class Zepto_20140613_B {
Scanner scanner = new Scanner(System.in);
BufferedReader buffered = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer input = new StreamTokenizer(buffered);
static PrintWriter output = new PrintWriter(new BufferedOutputStream(System.out));
int nextInt() throws Exception {
input.nextToken();
return (int) input.nval;
}
static void flushAndClose() {
output.flush();
System.out.flush();
output.close();
}
void main() throws Exception {
String[] str = buffered.readLine().split(" ");
int n = Integer.valueOf(str[0]);
int m = Integer.valueOf(str[1]);
int[] a = new int[m];
Arrays.fill(a, 0);
for (int i=0; i<n; i++) {
String s = buffered.readLine();
for (int j=0; j<m; j++) {
char c = s.charAt(j);
if (c == 'L') {
if (j-i >= 0) {
a[j-i]++;
}
}
if (c == 'R') {
if (j+i < m) {
a[j+i]++;
}
}
if (c == 'U') {
if (i%2 == 0) {
a[j]++;
}
}
}
}
for (int i=0; i<m; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
public static void main(String[] args) throws Exception {
new Zepto_20140613_B().main();
flushAndClose();
}
}
| Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | e8adeccc39c1879d586b996b1eb165e5 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 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 sc =new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
char a[][] = new char[n][m];
for(int i = 0;i<n;i++){
String s = sc.next();
for(int j = 0;j<m;j++){
a[i][j]=s.charAt(j);
}
}
for(int i = 0;i<m;i++){//begin at (i,0)
int x = 0;
int y = i;
int count = 0;
while(0<=x&&x<n&&0<=y&&y<m){
if(a[x][y]=='L'){
count++;
}
x++;
y++;
}
x=0;
y=i;
while(0<=x&&x<n&&0<=y&&y<m){
//System.out.println(i + " " + a[x][y]);
if(a[x][y]=='R'){
count++;
}
x++;
y--;
}
x=0;
y=i;
while(0<=x&&x<n&&0<=y&&y<m){
if(a[x][y]=='U'){
count++;
}
x+=2;
}
System.out.print(count + " ");
}
}
} | Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | e7df4e3e572a60d617621ea153f7fa90 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.*;
import java.util.*;
public class B
{
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));
StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
int n = Integer.parseInt(tokenizer.nextToken());
int m = Integer.parseInt(tokenizer.nextToken());
int k = Integer.parseInt(tokenizer.nextToken());
char [][] map = new char[n][m];
for(int i = 0 ; i < n ; i++)
{
map[i] = reader.readLine().toCharArray();
}
for(int c = 0 ; c < m ; c++)
{
int res = 0;
for(int r = 0 ; r < n ; r++)
{
int t = r;
//up
if(r-t >= 0)
if(map[r-t][c] == 'D')
res++;
//down
if(r+t < n)
if(map[r+t][c] == 'U')
res++;
//left
if(c-t >= 0)
if(map[r][c-t] == 'R')
res++;
//right
if(c+t < m)
if(map[r][c+t] == 'L')
res++;
}
writer.print(res);
if(c == m-1)
writer.println();
else
writer.print(" ");
}
writer.flush();
writer.close();
}
} | Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | f79a4b2ea30c9dff210f572344a1e108 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.util.Scanner;
public class ProblemB {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n, m, k;
n = sc.nextInt();
m = sc.nextInt();
k = sc.nextInt();
sc.nextLine();
String[] mat = new String[n];
for(int i = 0; i < n; ++i) {
mat[i] = sc.nextLine();
}
for(int j = 0; j < m; ++j) {
int c = 0;
for(int i = 1; i < n; ++i) {
if(j - i >= 0 && mat[i].charAt(j - i) == 'R') c++;
if(j + i < m && mat[i].charAt(j + i) == 'L') c++;
if(2*i < n && mat[2*i].charAt(j) == 'U') c++;
}
System.out.print(c + " ");
}
}
}
| Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | 65c1eb264f4fd0015670bcbd8ddedf6c | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static class pair implements Comparable<pair>
{
int a;
int b;
public pair(int pa, int pb)
{
a = pa; b= pb;
}
@Override
public int compareTo(pair o) {
if(this.a < o.a)
return -1;
if(this.a > o.a)
return 1;
return Integer.compare(o.b, this.b);
}
}
public static void main (String[] args) throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] spl = in.readLine().split(" ");
int n = Integer.parseInt(spl[0]);
int m = Integer.parseInt(spl[1]);
int k = Integer.parseInt(spl[2]);
int[] sL = new int[m+n+1];
int[] sR = new int[m+n+1];
int[] sU = new int[m];
for (int i = 0; i < n; i++) {
String linea = in.readLine();
for (int j = 0; j < m; j++) {
char ch = linea.charAt(j);
if(ch=='L')
{
sL[j-i+n]++;
}
if(ch=='R')
{
sR[i+j]++;
}
if(ch=='U')
{
if(i%2==0)
sU[j]++;
}
}
}
StringBuffer ans = new StringBuffer();
for (int j = 0; j < m; j++) {
int act = 0;
act+=sU[j];
act+=sL[j+n];
act+=sR[j];
ans.append(act);
if(j<m-1)
ans.append(" ");
}
System.out.println(ans);
}
} | Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | 051879bf6cf274fc1487479f88b183a8 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int k = in.nextInt();
char[][] a = new char[n][m];
for (int i = 0; i < n; i++) {
String s = in.next();
for (int j = 0; j < m; j++) {
a[i][j] = s.charAt(j);
}
}
for (int j = 0; j < m; j++) {
int ans = 0;
for (int l = 1; l < n; l++) {
if (l * 2 < n && a[l * 2][j] == 'U') {
ans++;
}
if (j - l >= 0 && a[l][j - l] == 'R') {
ans++;
}
if (j + l < m && a[l][j + l] == 'L') {
ans++;
}
}
System.out.print(ans + " ");
}
System.out.print("\n");
}
} | Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | fafd979cc858b04494f24a6de698e1c5 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String args[]) throws NumberFormatException,IOException {
Stdin in = new Stdin();
PrintWriter out = new PrintWriter(System.out);
int n=in.readInt();
int m=in.readInt();
int k=in.readInt();
char[][]cell=new char[n][m];
int count[]=new int[m];
for(int i=0;i<n;i++){
cell[i]=in.readNext().toCharArray();
}
for(int i=1;i<cell.length;i++){
for(int j=0;j<cell[0].length;j++){
if(cell[i][j]=='U'){
if(i%2==0){
count[j]++;
}
}else{
if(cell[i][j]=='L'&&i>0&&j-i>=0){
count[j-i]++;
}else{
if(cell[i][j]=='R'&&j<cell[0].length-1&&j+i<cell[0].length)
count[j+i]++;
}
}
}
}
for(int i=0;i<count.length;i++){
if(i>0)
out.print(" ");
out.print(count[i]);
}
out.flush();
out.close();
}
private static class Stdin {
InputStreamReader read;
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
private Stdin() {
read = new InputStreamReader(System.in);
br = new BufferedReader(read);
}
private String readNext() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
private int readInt() throws IOException, NumberFormatException {
return Integer.parseInt(readNext());
}
private long readLong() throws IOException, NumberFormatException {
return Long.parseLong(readNext());
}
}
}
| Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | 25836587b0ca8601a7e2440041dc6e3d | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class P436B {
@SuppressWarnings("unchecked")
public void run() throws Exception {
int n = nextInt();
int m = nextInt();
int k = nextInt();
String [] f = new String [n];
for (int i = 0; i < n; i++) {
f[i] = nextLine();
}
for (int j = 0; j < m; j++) {
k = 0;
for (int i = 1; i < n; i++) {
if ((i * 2 < n) && (f[i * 2].charAt(j) == 'U')) { k++; }
if ((j + i < m) && (f[i].charAt(j + i) == 'L')) { k++; }
if ((j - i >= 0) && (f[i].charAt(j - i) == 'R')) { k++; }
}
print(k + " ");
}
println();
}
public static void main(String... args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedOutputStream(System.out));
new P436B().run();
br.close();
pw.close();
}
static BufferedReader br;
static PrintWriter pw;
StringTokenizer stok;
String nextToken() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
String s = br.readLine();
if (s == null) { return null; }
stok = new StringTokenizer(s);
}
return stok.nextToken();
}
void print(byte b) { print("" + b); }
void print(int i) { print("" + i); }
void print(long l) { print("" + l); }
void print(double d) { print("" + d); }
void print(char c) { print("" + c); }
void print(Object o) {
if (o instanceof int[]) { print(Arrays.toString((int [])o));
} else if (o instanceof long[]) { print(Arrays.toString((long [])o));
} else if (o instanceof char[]) { print(Arrays.toString((char [])o));
} else if (o instanceof byte[]) { print(Arrays.toString((byte [])o));
} else if (o instanceof short[]) { print(Arrays.toString((short [])o));
} else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o));
} else if (o instanceof float[]) { print(Arrays.toString((float [])o));
} else if (o instanceof double[]) { print(Arrays.toString((double [])o));
} else if (o instanceof Object[]) { print(Arrays.toString((Object [])o));
} else { println("" + o); }
}
void print(String s) { pw.print(s); }
void println() { println(""); }
void println(byte b) { println("" + b); }
void println(int i) { println("" + i); }
void println(long l) { println("" + l); }
void println(double d) { println("" + d); }
void println(char c) { println("" + c); }
void println(Object o) { print("" + o); }
void println(String s) { pw.println(s); }
int nextInt() throws IOException { return Integer.parseInt(nextToken()); }
long nextLong() throws IOException { return Long.parseLong(nextToken()); }
double nextDouble() throws IOException { return Double.parseDouble(nextToken()); }
char nextChar() throws IOException { return (char) (br.read()); }
String next() throws IOException { return nextToken(); }
String nextLine() throws IOException { return br.readLine(); }
int [] readInt(int size) throws IOException {
int [] array = new int [size];
for (int i = 0; i < size; i++) { array[i] = nextInt(); }
return array;
}
long [] readLong(int size) throws IOException {
long [] array = new long [size];
for (int i = 0; i < size; i++) { array[i] = nextLong(); }
return array;
}
double [] readDouble(int size) throws IOException {
double [] array = new double [size];
for (int i = 0; i < size; i++) { array[i] = nextDouble(); }
return array;
}
String [] readLines(int size) throws IOException {
String [] array = new String [size];
for (int i = 0; i < size; i++) { array[i] = nextLine(); }
return array;
}
} | Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | 6c27f2c75ede8cde0cd0de0b25c442fb | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
//TreeSet<Integer> ts = new TreeSet<Integer>();
//TreeMap<Integer,Integer> tm = new TreeMap<Integer,Integer>();
//ArrayList<ArrayList<Integer>> tubes = new ArrayList<ArrayList<Integer>>();
//ArrayList<Integer[]> al = new ArrayList<Integer[]>();
int[] ans = new int[m];
String line;
sc.nextLine();
int x;
for (int i = 0; i < n; i++) {
line = sc.nextLine();
for(int t=0; t < m; t++) {
switch(line.charAt(t)) {
case '.' :case 'D': continue;
case 'U' : if(i%2 == 0)ans[t]++;break;
case 'L' : x = t - i;if(x >= 0 && x < m)ans[x]++;break;
case 'R' : x = t + i;if(x >= 0 && x < m)ans[x]++;break;
}
}
}
for (int i=0; i < m; i++) {
sb.append(ans[i]+" ");
}
System.out.println(sb);
}
}
| Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | b432033dd526075c4b25b854f4f5211e | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | public class B {
public B () {
int N = sc.nextInt(), M = sc.nextInt(); sc.nextInt();
char [][] B = sc.nextChars(N);
int [] T = new int [M];
for (int i : rep(N))
for (int j : rep(M))
switch(B[i][j]) {
case 'U': if (i%2 == 0) ++T[j]; break;
case 'R': if (j+i < M) ++T[j+i]; break;
case 'L': if (j-i >= 0) ++T[j-i]; break;
default: break;
}
exit(T);
}
private static int [] rep(int N) { return rep(0, N); }
private static int [] rep(int S, int T) { if (T <= S) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
////////////////////////////////////////////////////////////////////////////////////
private final static IOUtils.MyScanner sc = new IOUtils.MyScanner();
private static void exit (Object o, Object ... A) { IOUtils.print(o, A); IOUtils.exit(); }
private static class IOUtils {
public static class MyScanner {
public String next() { newLine(); return line[index++]; }
public int nextInt() { return Integer.parseInt(next()); }
public char [] nextChars() { return next ().toCharArray (); }
public char [][] nextChars(int N) {
char [][] res = new char [N][];
for (int i = 0; i < N; ++i)
res[i] = nextChars();
return res;
}
//////////////////////////////////////////////
private boolean eol() { return index == line.length; }
private String readLine() {
try {
return r.readLine();
} catch (Exception e) {
throw new Error (e);
}
}
private final java.io.BufferedReader r;
private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); }
private MyScanner (java.io.BufferedReader r) {
try {
this.r = r;
while (!r.ready())
Thread.sleep(1);
start();
} catch (Exception e) {
throw new Error(e);
}
}
private String [] line;
private int index;
private void newLine() {
if (line == null || eol()) {
line = split(readLine());
index = 0;
}
}
private String [] split(String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
}
private static String build(Object o, Object ... A) { return buildDelim(" ", o, A); }
private static String buildDelim(String delim, Object o, Object ... A) {
StringBuilder b = new StringBuilder();
append(b, o, delim);
for (Object p : A)
append(b, p, delim);
return b.substring(delim.length());
}
//////////////////////////////////////////////////////////////////////////////////
private static void start() { if (t == 0) t = millis(); }
private static void append(StringBuilder b, Object o, String delim) {
if (o.getClass().isArray()) {
int len = java.lang.reflect.Array.getLength(o);
for (int i = 0; i < len; ++i)
append(b, java.lang.reflect.Array.get(o, i), delim);
} else if (o instanceof Iterable<?>)
for (Object p : (Iterable<?>) o)
append(b, p, delim);
else {
if (o instanceof Double)
o = new java.text.DecimalFormat("#.############").format(o);
b.append(delim).append(o);
}
}
private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
private static void print(Object o, Object ... A) { pw.println(build(o, A)); }
private static void err(Object o, Object ... A) { System.err.println(build(o, A)); }
private static void exit() {
IOUtils.pw.close();
System.out.flush();
err("------------------");
err(IOUtils.time());
System.exit(0);
}
private static long t;
private static long millis() { return System.currentTimeMillis(); }
private static String time() { return "Time: " + (millis() - t) / 1000.0; }
}
public static void main (String[] args) { new B(); IOUtils.exit(); }
}
| Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | 878a3850821788b1d07f2ef375f46192 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | public class B {
public B () {
int N = sc.nextInt(), M = sc.nextInt(); sc.nextInt();
char [][] B = sc.nextChars(N);
int [] T = new int [M];
for (int i : rep(N))
for (int j : rep(M))
switch(B[i][j]) {
case 'U': if (i%2 == 0) ++T[j]; break;
case 'R': if (j+i < M) ++T[j+i]; break;
case 'L': if (j-i >= 0) ++T[j-i]; break;
default: break;
}
exit(T);
}
private static int [] rep(int N) { return rep(0, N); }
private static int [] rep(int S, int T) { if (T <= S) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
////////////////////////////////////////////////////////////////////////////////////
private final static IOUtils.MyScanner sc = new IOUtils.MyScanner();
private static void exit (Object o, Object ... A) { IOUtils.print(o, A); IOUtils.exit(); }
private static class IOUtils {
public static class MyScanner {
public String next() { newLine(); return line[index++]; }
public int nextInt() { return Integer.parseInt(next()); }
public char [] nextChars() { return next ().toCharArray (); }
public char [][] nextChars(int N) {
char [][] res = new char [N][];
for (int i = 0; i < N; ++i)
res[i] = nextChars();
return res;
}
//////////////////////////////////////////////
private boolean eol() { return index == line.length; }
private String readLine() {
try {
return r.readLine();
} catch (Exception e) {
throw new Error (e);
}
}
private final java.io.BufferedReader r;
private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); }
private MyScanner (java.io.BufferedReader r) {
try {
this.r = r;
while (!r.ready())
Thread.sleep(1);
start();
} catch (Exception e) {
throw new Error(e);
}
}
private String [] line;
private int index;
private void newLine() {
if (line == null || eol()) {
line = split(readLine());
index = 0;
}
}
private String [] split(String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
}
private static String build(Object o, Object ... A) { return buildDelim(" ", o, A); }
private static String buildDelim(String delim, Object o, Object ... A) {
StringBuilder b = new StringBuilder();
append(b, o, delim);
for (Object p : A)
append(b, p, delim);
return b.substring(delim.length());
}
//////////////////////////////////////////////////////////////////////////////////
private static void start() { if (t == 0) t = millis(); }
private static void append(StringBuilder b, Object o, String delim) {
if (o.getClass().isArray()) {
int len = java.lang.reflect.Array.getLength(o);
for (int i = 0; i < len; ++i)
append(b, java.lang.reflect.Array.get(o, i), delim);
} else if (o instanceof Iterable<?>)
for (Object p : (Iterable<?>) o)
append(b, p, delim);
else {
if (o instanceof Double)
o = new java.text.DecimalFormat("#.############").format(o);
b.append(delim).append(o);
}
}
private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
private static void print(Object o, Object ... A) { pw.println(build(o, A)); }
private static void err(Object o, Object ... A) { System.err.println(build(o, A)); }
private static void exit() {
IOUtils.pw.close();
System.out.flush();
err("------------------");
err(IOUtils.time());
System.exit(0);
}
private static long t;
private static long millis() { return System.currentTimeMillis(); }
private static String time() { return "Time: " + (millis() - t) / 1000.0; }
}
public static void main (String[] args) { new B(); IOUtils.exit(); }
}
| Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | 83f4b486f27f88bd383399b94248e802 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main{
static InputStreamReader inp = new InputStreamReader( System.in );
static BufferedReader buf = new BufferedReader( inp );
static StreamTokenizer tok = new StreamTokenizer( buf );
static OutputStreamWriter out = new OutputStreamWriter( System.out );
static PrintWriter wri = new PrintWriter( out );
public static Scanner scan = new Scanner( System.in );
static int nextInt() throws IOException {
tok.nextToken(); return ( int ) tok.nval;
}
public static int n = 0, m = 0;
static boolean inMap( int x, int y ) {
return x >= 0 && x < n && y >= 0 && y < m;
}
public static void main( String[] args ) throws IOException {
n = nextInt();
m = nextInt();
int num = nextInt();
buf.readLine();
char[][] map = new char[n][m];
for ( int i = 0; i < n; i ++ ) {
map[i] = buf.readLine().toCharArray();
}
final int[] dx = new int[]{ 0, 0, 1, -1 };
final int[] dy = new int[]{ 1, -1, 0, 0 };
int[] tx = new int[4], ty = new int[4];
final char[] r = new char[]{ 'L', 'R', 'U', 'D' };
for ( int i = 0; i < m; i ++ ) {
int ans = 0;
for ( int t = 0; t < 4; t ++ ) {
tx[t] = 0; ty[t] = 0;
}
for ( int j = 1; j < n; j ++ ) {
for ( int k = 0; k < 4; k ++ ) {
tx[k] += dx[k];
ty[k] += dy[k];
if ( inMap( j + tx[k], i + ty[k] ) && map[j + tx[k]][i + ty[k]] == r[k] ) ans ++;
}
}
wri.print( ans + " ");
}
wri.println( "" );
wri.flush();
}
} | Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | d9286fca3b884baef81f286ca9a20d54 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
public class B_Spiders {
public static void main(String[] args) throws java.io.IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String[] params = input.readLine().split(" ");
final int N = Integer.parseInt(params[0]);
final int M = Integer.parseInt(params[1]);
char[][] park = new char[N][];
for (int i = 0 ; i < N ; i++) {
park[i] = input.readLine().toCharArray();
}
StringBuilder sb = new StringBuilder();
for (int start = 0 ; start < M ; start++) {
int count = 0;
for (int i = 0 ; i < N ; i += 2) {
if (park[i][start] == 'U') {
count++;
}
}
for (int i = 0 ; i < Math.min(start + 1 , N) ; i++) {
if (park[i][start - i] == 'R') {
count++;
}
}
for (int i = 0 ; i < Math.min(M - start , N) ; i++) {
if (park[i][start + i] == 'L') {
count++;
}
}
sb.append(count).append(" ");
}
System.out.println(sb.toString().trim());
}
}
| Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | b49b3bdf1f62dc6e54c3f862ef51702d | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.StringTokenizer;
public class B {
private final static long START_TIME=System.currentTimeMillis();
private final static boolean LOG_ENABLED=true;
private final static boolean ONLINE_JUDGE = LOG_ENABLED && (System.getProperty("ONLINE_JUDGE") != null);
private final static String SYSTEM_ENCODING="utf-8";
private static class Logger{
private final PrintWriter logWriter=Util.newPrintWriter(System.err,true);
private final DecimalFormat df=new DecimalFormat("0.000");
private void message(String type, String message, Object ... params){
if(ONLINE_JUDGE){
return;
}
logWriter.printf("["+type+"] "+df.format((System.currentTimeMillis()-START_TIME)/1000.0)+": "+message+"\r\n", params);
}
public void debug(String message, Object ... params){
message("DEBUG", message, params);
}
}
private final static class Util{
public static PrintWriter newPrintWriter(OutputStream out, boolean autoFlush){
try {
return new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(out), SYSTEM_ENCODING),autoFlush);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}
}
public final static class FastScanner{
private BufferedReader reader;
private StringTokenizer currentTokenizer;
public FastScanner(Reader reader) {
if(reader instanceof BufferedReader){
this.reader=(BufferedReader) reader;
}else{
this.reader=new BufferedReader(reader);
}
}
public String next(){
if(currentTokenizer==null || !currentTokenizer.hasMoreTokens()){
try {
currentTokenizer=new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
return currentTokenizer.nextToken();
}
public int nextInt(){
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
}
private final static Logger log=new Logger();
private final BufferedReader reader;
private final FastScanner in;
private final PrintWriter out=Util.newPrintWriter(System.out,false);
public B(BufferedReader reader){
this.reader=reader;
in=new FastScanner(this.reader);
}
public static void main(String[] args) throws IOException {
log.debug("Started");
try{
new B(new BufferedReader(new InputStreamReader(System.in, SYSTEM_ENCODING))).run();
}finally{
log.debug("Stopped");
}
}
void run(){
solve();
out.flush();
}
private void solve(){
int n = in.nextInt();
int m = in.nextInt();
int k = in.nextInt();
char [][] field=new char[n][m];
for(int r=0;r<n;r++){
field[r]=in.next().toCharArray();
}
for(int c=0;c<m;c++){
if(c>0){
out.print(" ");
}
out.print(getCount(c,field, n, m));
}
out.println();
}
private int getCount(int c, char[][] field,int n, int m) {
int result=0;
for(int i=0;i<n;i++){
if(field[i][c]=='U'&&i%2==0){
result++;
}
int rc = c-i;
if(rc>=0 && field[i][rc]=='R'){
result++;
}
int lc = c+i;
if(lc<m && field[i][lc]=='L'){
result++;
}
}
return result;
}
} | Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | 7296bef4a30f9de5f5c6dc4fd6f1b493 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Nipuna Samarasekara
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
/////////////////////////////////////////////////////////////
public void solve(int testNumber, FastScanner in, FastPrinter out) {
int m=in.nextInt(),n=in.nextInt(),k=in.nextInt();
String[] S= new String[m];
for (int i = 0; i < m; i++) {
S[i]=in.next();
}
int[] ans= new int[n];
for (int i = 1; i < m; i++) {
for (int j = 0; j < n; j++) {
if(S[i].charAt(j)=='U'&&i%2==0)ans[j]++;
if(S[i].charAt(j)=='L'){
int h=i;
if(j-h>=0)ans[j-h]++;
}
if(S[i].charAt(j)=='R'){
int h=i;
if(j+h<n)ans[j+h]++;
}
}
}
for (int i = 0; i < n; i++) {
out.print(ans[i] + " ");
}
out.println();
}
}
class FastScanner extends BufferedReader {
public FastScanner(InputStream is) {
super(new InputStreamReader(is));
}
public int read() {
try {
int ret = super.read();
// if (isEOF && ret < 0) {
// throw new InputMismatchException();
// }
// isEOF = ret == -1;
return ret;
} catch (IOException e) {
throw new InputMismatchException();
}
}
public String next() {
StringBuilder sb = new StringBuilder();
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
if (c < 0) {
return null;
}
while (c >= 0 && !isWhiteSpace(c)) {
sb.appendCodePoint(c);
c = read();
}
return sb.toString();
}
static boolean isWhiteSpace(int c) {
return c >= 0 && c <= 32;
}
public int nextInt() {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int ret = 0;
while (c >= 0 && !isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new NumberFormatException("digit expected " + (char) c
+ " found");
}
ret = ret * 10 + c - '0';
c = read();
}
return ret * sgn;
}
public String readLine() {
try {
return super.readLine();
} catch (IOException e) {
return null;
}
}
}
class FastPrinter extends PrintWriter {
public FastPrinter(OutputStream out) {
super(out);
}
public FastPrinter(Writer out) {
super(out);
}
}
| Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | 14e8cb16a5d205eae8a87a986dcf9727 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import static java.util.Collections.reverseOrder;
/**
* Created with IntelliJ IDEA.
* User: AUtemuratov
* Date: 07.04.14
* Time: 15:43
* To change this template use File | Settings | File Templates.
*/
public class Main {
static FastReader fr = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw = new PrintWriter(System.out);
static int n,m,k,ans,cnt,min = Integer.MAX_VALUE,max = Integer.MIN_VALUE;
static boolean ok,w;
static char c[][] = new char[2222][2222];
public static void main(String[] args) throws Exception {
n = fr.nextInt();
m = fr.nextInt();
k = fr.nextInt();
for (int i=0; i<n; i++) {
String s = fr.nextToken();
for (int j=0; j<m; j++) {
c[i][j+1] = s.charAt(j);
}
}
for (int i=1; i<=m; i++) {
pw.print(makeOperation(i) + " ");
}
pw.close();
}
private static int makeOperation(int p) {
int t = 0;
for (int i=1; i<=n; i++) {
if (check(p+i,1)) {
if (c[i][p+i]=='L') {
t++;
}
}
if (check(p-i,2)) {
if (c[i][p-i]=='R') {
t++;
}
}
if (check(i+i,3)) {
if (c[i+i][p]=='U') {
t++;
}
}
}
return t;
}
private static boolean check(int p,int t) {
if (t==1) {
if (p>m) return false;
} else if (t==2) {
if (p<1) return false;
} else {
if (p>n) return false;
}
return true;
}
}
class Pair {
int l,r;
public Pair(int pos, int dif) {
this.l = pos;
this.r = dif;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
if (l != pair.l) return false;
if (r != pair.r) return false;
return true;
}
@Override
public int hashCode() {
int result = l;
result = 31 * result + r;
return result;
}
}
class FastReader {
BufferedReader bf;
StringTokenizer tk = null;
String DELIM = " ";
public FastReader(BufferedReader bf) {
this.bf = bf;
}
public String nextToken() throws Exception {
if(tk==null || !tk.hasMoreTokens()) {
tk = new StringTokenizer(bf.readLine(),DELIM);
}
return tk.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public int nextLong() throws Exception {
return Integer.parseInt(nextToken());
}
public int nextDouble() throws Exception {
return Integer.parseInt(nextToken());
}
} | Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | e9fd8cd27c6713224844e2670b33552f | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.util.*;
import java.io.*;
public class B
{
static char map[][];
public static void main(String[]args)throws IOException
{
DataInputStream in=new DataInputStream(System.in);
String S=in.readLine();
StringTokenizer s=new StringTokenizer(S);
int n=Integer.parseInt(s.nextToken());
int m=Integer.parseInt(s.nextToken());
int k=Integer.parseInt(s.nextToken());
map=new char[n][m];
for(int i=0;i<n;i++)
{
S=in.readLine();
for(int j=0;j<m;j++)
map[i][j]=S.charAt(j);
}
int ans[]=new int[m];
Arrays.fill(ans,0);
for(int j=0;j<m;j++)
{
for(int i=1;i<n;i++)
{
if(i+j<m && map[i][i+j]=='L')ans[j]++;
if(2*i<n && map[2*i][j]=='U')ans[j]++;
if(j-i>=0&& map[i][j-i]=='R')ans[j]++;
}
}
for(int j=0;j<m;j++)System.out.print(ans[j]+" ");
}
}
| Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | 1db28bb913237c7ac1211ed11a5b05fb | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes |
import java.io.*;
import java.util.*;
import java.awt.Point;
public class Template {
BufferedReader in;
PrintWriter out;
StringTokenizer st;
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (Exception e) {
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
public void run() throws Exception {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.flush();
out.close();
in.close();
}
public void solve() throws Exception {
int n = nextInt();
int m = nextInt();
int k = nextInt();
int[] ans = new int[m];
for (int i = 0; i < n; i++) {
char[] c = next().toCharArray();
for (int j = 0; j < m; j++) {
if (c[j] == 'R' && j + i < m) {
ans[j + i]++;
}
if (c[j] == 'L' && j - i >= 0) {
ans[j - i]++;
}
if (c[j] == 'U' && i % 2 == 0) {
ans[j]++;
}
}
}
for (int j = 0; j < m; j++) {
out.print(ans[j] + " ");
}
out.println();
}
public static void main(String[] args) throws Exception {
new Template().run();
}
} | Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output | |
PASSED | a831ee93e9a9c3ee8d2d14dad2d97f60 | train_002.jsonl | 1402673400 | Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular nβΓβm field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class B {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
// Scanner scan = new Scanner(System.in);
// PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// int n = Integer.parseInt(bf.readLine());
StringTokenizer st = new StringTokenizer(bf.readLine());
// int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
char[][] board = new char[n][m];
for(int i=0; i<n; i++) {
char[] c = bf.readLine().toCharArray();
for(int j=0; j<m; j++) {
board[i][j] = c[j];
}
}
int[] counters = new int[m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(board[i][j] != '.') {
if(board[i][j] == 'R')
if(i+j < m)
counters[i+j]++;
if(board[i][j] == 'L')
if(j-i >= 0)
counters[j-i]++;
if(board[i][j] == 'U')
if(i%2 == 0)
counters[j]++;
}
}
}
for(int i=0; i<m; i++) System.out.print(counters[i] + " ");
// int n = scan.nextInt();
// out.close(); System.exit(0);
}
}
| Java | ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"] | 3 seconds | ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"] | NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character "*" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. | Java 7 | standard input | [
"implementation",
"math"
] | d8c89bb83592a1ff1b639f7d53056d67 | The first line contains three integers n,βm,βk (2ββ€βn,βmββ€β2000;Β 0ββ€βkββ€βm(nβ-β1)). Each of the next n lines contains m characters β the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals ".", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: "L" (meaning that this cell has a spider at time 0, moving left), "R" (a spider moving right), "U" (a spider moving up), "D" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders. | 1,400 | Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.