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 | 87a589c795545ddffd88f8876e49e31a | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.*;
import java.util.*;
public class cf2 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int sum=0;
for(int i=0;i<a.length;i++){
sum=sum | a[i];
}
System.out.println(sum);
}
sc.close();
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 9816cfd070f69e6010481cbf7bf48ae3 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes |
// Java is like Alzheimer's, it starts off slow, but eventually, your memory is gone.
import java.io.*;
import java.util.*;
public class Aqueous {
static MyScanner sc = new MyScanner();
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int a[] = new int[n];
long sum = 0;
HashSet<Integer> hs = new HashSet<>();
for(int i = 0; i<n; i++) {
a[i] =sc.nextInt();
String s = Integer.toBinaryString(a[i]);
for(int j = s.length()-1; j>=0; j--) {
if(s.charAt(j)=='1') {
if(!hs.contains(s.length()-1-j)) {
sum += 1<<(s.length()-1)-j;
hs.add(s.length()-1-j);
}
}
}
}
pw.println(sum);
}
pw.close();
}
static void ruffleSort(int a[]) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < n; i++) {
int oi = r.nextInt(n);
int temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long a[]) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < n; i++) {
int oi = r.nextInt(n);
long temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | b26aefb06c265f3bf9028e5609f8910c | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class TaskA {
static void solve(FastScanner fs) {
int n = fs.nextInt();
int a[] = takeArrayInput(fs, n);
int res = 0;
for(int i = 0; i < n; i++) {
res = res | a[i];
}
System.out.println(res);
}
static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
static int[] takeArrayInput(FastScanner fs, int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = fs.nextInt();
}
return a;
}
static void printArrayWithSpace(int a[]) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.print("\n");
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static int countOdd(int L, int R) {
int N = (R - L) / 2;
if (R % 2 != 0 || L % 2 != 0)
N++;
return N;
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int c = fs.nextInt();
while (c-- > 0) {
solve(fs);
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | cebf4e61a8a521effc886195ef125b57 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
for (int i = 0; i < n; i++) {
test(sc);
}
sc.close();
}
public static void test(Scanner sc) {
int count = sc.nextInt();
List<char[]> binaries = new ArrayList<>();
int maxLength = 0;
for (int i = 0; i < count; i++) {
var a = sc.nextInt();
var chars = Integer.toBinaryString(a).toCharArray();
if (maxLength < chars.length) {
maxLength = chars.length;
}
binaries.add(chars);
}
for (int i = 0; i < maxLength; i++) {
boolean has1 = false;
for (char[] chars : binaries) {
var pos = chars.length - 1 - i;
if (pos >= 0) {
if (has1) {
chars[pos] = '0';
} else if (chars[pos] == '1') {
has1 = true;
}
}
}
}
int sum = 0;
for (char[] chars : binaries) {
sum += Integer.parseInt(new String(chars), 2);
}
System.out.println(sum);
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | a1262922efae06d0c6310c7fef78c1e7 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.BufferedReader;
import java.lang.*;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.util.*;
import java.util.LinkedList;
public class CEdu{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void sort(long[] arr) {
ArrayList<Long> a = new ArrayList<>();
for (long i : arr) {
a.add(i);
}
Collections.sort(a);
for (int i = 0; i < a.size(); i++) {
arr[i] = a.get(i);
}
}
static boolean get(char[] ch, int i, long k) {
long ope = 0;
for(int j = i; j >=0; j--) {
long ele = ((ch[j]-'0') + ope)%10;
long req = 10 - ele;
if(req == 10) {
req = 0;
}
if(ope+req <= k) {
ope = ope + req;
if(j == 0) {
return true;
}
}else {
return false;
}
}
return false;
}
static void solve(long a, long b, long c) {
System.out.println((a|b) & (b|c) & (c|a));
}
static long get(long[] arr , int low, int high) {
long len = high-low+1;
for(int i = low; i <= high; i++) {
if(arr[i] == 0) {
len++;
}
}
return len;
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
int t = sc.nextInt();
test: while(t-- > 0) {
int n = sc.nextInt();
long[] arr = new long[n];
long s = 0;
for(int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
s = s + arr[i];
}
long o = arr[0];
for(int i = 1; i < n; i++) {
o = o|arr[i];
}
System.out.println(o);
// if(s % 2 == 0) {
// System.out.println(s/2);
// }else {
// System.out.println(s);
// }
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | d5b61132923eb18c4b11e1c7a97f40df | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.Scanner;
public class PA{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i<t; i++)
{
int n = in.nextInt();
int[] arr = new int[n];
for (int j = 0; j<n; j++)
{
arr[j] = in.nextInt();
}
int a = arr[0];
for (int j = 0; j<n; j++)
{
a = a|arr[j];
}
System.out.println(a);
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 41ab798ad97c71567086c7b4afb5bbe0 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class cf772a {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; ++i) {
int num = Integer.parseInt(br.readLine());
String[] strings = br.readLine().split(" ");
long result = 0;
for (int j = 0; j < num; ++j) {
result = result | Long.parseLong(strings[j]);
}
System.out.println(result);
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | e4e71c786d985513689a7f5e32519154 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static BufferedReader br;
static StringBuilder sb = new StringBuilder();
static int index;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
sb = new StringBuilder();
int t = toi(br.readLine());
while(t-- > 0) {
int n = toi(br.readLine());
int[] arr = getArr();
long sum = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < 31; j++)
if((sum & 1<<j) == 0 && (arr[i] & 1<<j) != 0) sum += 1<<j;
sb.append(sum).append("\n");
}
print(sb);
}
static int toi(String s) { return Integer.parseInt(s); }
static long tol(String s) { return Long.parseLong(s); }
static String[] getLine() throws IOException { return br.readLine().split(" "); }
static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
static <T> void print(T s) { System.out.print(s); }
static <T> void println(T s) { System.out.println(s); }
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | cad6b7b60a14159525d5117c5790f408 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
public class q1 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
String input[] = br.readLine().split(" ");
int arr[] = new int[n];
int mask = 0;
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(input[i]);
mask |= arr[i];
}
System.out.println(mask);
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | bcbf1d339bc446d1ed919f04383e3e1d | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
import java.io.PrintWriter;
import java.io.OutputStream;
public class Solution{
public static void main(String[] args){
FastReader sc=new FastReader();
int n=sc.nextInt();
for(int i=0;i<n;i++){
int p=sc.nextInt();
int count=0;
for(int j=0;j<p;j++){
count=count|sc.nextInt();
}
System.out.println(count);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | a52633457e6fe16112749e9f34d34305 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | // Working program with FastReader
import java.io.*;
import java.util.*;
//
public class Example {
static BufferedWriter bw;
static {
bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void print(String a) throws IOException {
bw.write(a);
}
public static void printSp(String a) throws IOException {
bw.write(a + " ");
}
public static void println(String a) throws IOException {
bw.write(a + "\n");
}
static int min = Integer.MAX_VALUE;
static boolean tt = true;
static long mod= (long) (1e9+7);
static long minDIff=Long.MAX_VALUE;
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
// BufferedWriter print = new BufferedWriter(new OutputStreamWriter(System.out));
int t=sc.nextInt();
while (t>0){
t--;
int n=sc.nextInt();
long sum=0;
for(int i=0;i<n;i++){
int a=sc.nextInt();
sum=sum|a;
}
System.out.println(sum);
}
}
private static void premutate(char[] cc, int i, int i1,Set<String> set,List<String> list) {
if(i==i1){
if(set.add(String.valueOf(cc))){
list.add(String.valueOf(cc));
}
}else{
for(int i2=i;i2<=i1;i2++){
swap(cc,i,i2);
premutate(cc,i+1,i1,set,list);
swap(cc,i,i2);
}
}
}
public static void isolve(long curr,long sum,int[]ar,int i){
if(i==ar.length){
minDIff=Math.min(minDIff,Math.abs(2*curr-sum));
return;
}
minDIff=Math.min(minDIff,Math.abs(2*curr-sum));
isolve(curr,sum,ar,i+1);
isolve(curr+ar[i],sum,ar,i+1);
}
private static void solve(int s, int d, int h, int n) {
if(n==1){
System.out.println(s+" "+d);
}else{
solve(s,h,d,n-1);
System.out.println(s+" "+d);
solve(h,d,s,n-1);
}
}
private static long power(long b, long c,long mod) {
long ans=1;
while (c>0){
if(c%2==0){
c=c/2;
b=((b*b)%mod);
}else{
c--;
ans=((ans*b)%mod);
}
}
return ans;
}
private static void swap(char[] ar, int i, int i1) {
char temp = ar[i];
ar[i] = ar[i1];
ar[i1] = temp;
}
private static void reverse(int[] ar, int i, int j) {
while (i < j) {
int temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
i++;
j--;
}
}
private static boolean isPalindrome(String s) {
int i = 0;
int j = s.length() - 1;
while (i <= j) {
if (s.charAt(i) == s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
public static Stack<Integer> reverseTheGroups(Stack<Integer> s, int n, int k) {
// Write your code here.
Deque<Integer> qu= new LinkedList<>();
while(s.size()>=k){
Stack<Integer> temp= new Stack<>();
int tempnum=k;
while(tempnum!=0){
tempnum--;
temp.push(s.pop());
}
while(temp.size()>0){
qu.addLast(temp.pop());
}
}
while(s.size()>0){
qu.add(s.pop());
}
Stack<Integer> st= new Stack<>();
while(qu.size()>0){
st.push(qu.pollLast());
}
return st;
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | bc513e75f96fad17736794ee7dc2b801 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
AMinOrSum solver = new AMinOrSum();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class AMinOrSum {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int[] arr = in.nextIntArray(n);
int[] f = new int[32];
long val = 0;
for (int a : arr) {
val |= a;
for (int j = 0; j <= 30; j++) {
if (((1 << j) & a) != 0) f[j]++;
}
}
out.println(val);
long ans = 0;
for (int i = 0; i <= 30; i++) {
if (f[i] > 0)
ans += (1L << i) * ((Math.max(1, f[i] - 1)));
}
// out.println(ans);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(long i) {
writer.println(i);
}
}
static class InputReader {
BufferedReader reader;
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[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 2fbf20c5825feecdd29af2ebe2eb4f64 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
// Compiler version JDK 11.0.2
public class Dcoder
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
{
int n=s.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=s.nextInt();
int sol=0;
for(int i:arr)
sol|=i;
System.out.println(sol);
}
s.close();
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 88153d20bfff8647da1a0d0312ad97c4 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution
{
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int ans=0;
int[] arr=new int[n];
for (int i = 0; i <n ; i++) {
arr[i]=sc.nextInt();
ans=ans|arr[i];
}
System.out.println(ans);
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 197e9636bbecf9cb050890b96cc33ee0 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
public class Solution
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
for(int t= in.nextInt();t>0;t--)
{
int n = in.nextInt();
int a = in.nextInt();
for (int i =1;i<n;i++)
a|= in.nextInt();
System.out.println(a);
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 1f636b0b2b1747bef383dd6ebfb12f82 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.*;
import java.util.*;
public class A {
static PrintWriter pw = new PrintWriter(System.out);
static Scanner sc = new Scanner(System.in);
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
}
public static void main(String[] args) throws IOException
{
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int elem = sc.nextInt();
for(int i = 1; i < n; i++)
{
elem |= sc.nextInt();
}
pw.println(elem);
}
pw.flush();
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 6fe77f5fd21cce1e23c55eab57c6740d | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
public class A_Min_Or_Sum{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t=s.nextInt();
for(int k=0;k<t;k++){
int n = s.nextInt();
int a = s.nextInt();
for(int i=1; i<n; i++)
{
a|=s.nextInt();
}
System.out.println(a);
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | c759b20b11d740a9dbed0c3995a257e6 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*; import java.io.*; import java.math.*;
public class Main{
//見なくていいよ ここから------------------------------------------
static class InputIterator{
ArrayList<String> inputLine = new ArrayList<>(1024);
int index = 0; int max; String read;
InputIterator(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
while((read = br.readLine()) != null){
inputLine.addAll(Arrays.asList(read.split(" ")));
}
}catch(IOException e){}
max = inputLine.size();
}
boolean hasNext(){return (index < max);}
String next(){
if(hasNext()){
return inputLine.get(index++);
}else{
throw new IndexOutOfBoundsException("There is no more input");
}
}
}
static HashMap<Integer, String> CONVSTR = new HashMap<>();
static InputIterator ii = new InputIterator();//This class cannot be used in reactive problem.
static PrintWriter out = new PrintWriter(System.out);
static void flush(){out.flush();}
static void myout(Object t){out.println(t);}
static void myerr(Object... t){System.err.print("debug:");System.err.println(Arrays.deepToString(t));}
static String next(){return ii.next();}
static boolean hasNext(){return ii.hasNext();}
static int nextInt(){return Integer.parseInt(next());}
static long nextLong(){return Long.parseLong(next());}
static double nextDouble(){return Double.parseDouble(next());}
static ArrayList<String> nextCharArray(){return myconv(next(), 0);}
static ArrayList<String> nextStrArray(int size){
ArrayList<String> ret = new ArrayList<>(size);
for(int i = 0; i < size; i++){
ret.add(next());
}
return ret;
}
static ArrayList<Integer> nextIntArray(int size){
ArrayList<Integer> ret = new ArrayList<>(size);
for(int i = 0; i < size; i++){
ret.add(Integer.parseInt(next()));
}
return ret;
}
static ArrayList<Long> nextLongArray(int size){
ArrayList<Long> ret = new ArrayList<>(size);
for(int i = 0; i < size; i++){
ret.add(Long.parseLong(next()));
}
return ret;
}
@SuppressWarnings("unchecked")
static String myconv(Object list, int no){//only join
StringBuilder sb = new StringBuilder("");
String joinString = CONVSTR.get(no);
if(list instanceof String[]){
return String.join(joinString, (String[])list);
}else if(list instanceof long[]){
long[] tmp = (long[])list;
if(tmp.length == 0){
return "";
}
sb.append(String.valueOf(tmp[0]));
for(int i = 1; i < tmp.length; i++){
sb.append(joinString).append(String.valueOf(tmp[i]));
}
return sb.toString();
}else if(list instanceof int[]){
int[] tmp = (int[])list;
if(tmp.length == 0){
return "";
}
sb.append(String.valueOf(tmp[0]));
for(int i = 1; i < tmp.length; i++){
sb.append(joinString).append(String.valueOf(tmp[i]));
}
return sb.toString();
}else if(list instanceof ArrayList){
ArrayList tmp = (ArrayList)list;
if(tmp.size() == 0){
return "";
}
sb.append(tmp.get(0));
for(int i = 1; i < tmp.size(); i++){
sb.append(joinString).append(tmp.get(i));
}
return sb.toString();
}else{
throw new ClassCastException("Don't join");
}
}
static ArrayList<String> myconv(String str, int no){//only split
String splitString = CONVSTR.get(no);
return new ArrayList<String>(Arrays.asList(str.split(splitString)));
}
static ArrayList<String> myconv(String str, String no){
return new ArrayList<String>(Arrays.asList(str.split(no)));
}
public static void main(String[] args){
CONVSTR.put(8, " "); CONVSTR.put(9, "\n"); CONVSTR.put(0, "");
solve();flush();
}
//見なくていいよ ここまで------------------------------------------
//このコードをコンパイルするときは、「-encoding UTF-8」を指定すること
static void solve(){//ここがメイン関数
int t = nextInt();
while(hasNext()){
int N = nextInt();
int or = 0;
for(int i = 0; i < N; i++){
or |= nextInt();
}
myout(or);
}
}
//メソッド追加エリア ここから
//メソッド追加エリア ここまで
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 9be8d0e5bc7ee1f1d9b4c66ba85c0517 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | // package fareeda.ragab.random;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class MinOrSum {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tk;
int T = Integer.parseInt(input.readLine());
while ((T--) != 0) {
int n = Integer.parseInt(input.readLine());
tk = new StringTokenizer(input.readLine());
int res = 0;
for (int i = 0; i < n; i++) {
int x = Integer.parseInt(tk.nextToken());
res |= x;
}
System.out.println(res);
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 76a41d53f9a2e1a12d7f7aa9fee5299c | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++)
arr[i] = sc.nextInt();
for(int i=0; i<n; i++) {
int msb = (int)(Math.log(arr[i])/Math.log(2));
for(int bit=0; bit<=msb; bit++){
for(int j=i+1; j<n; j++){
if(((arr[i] >> bit) & 1) == 1 && ((arr[j] >> bit) & 1) == 1){
arr[j] &= ~(1 << bit);
}
}
}
}
int sum = 0;
for(int i=0; i<n; i++)
sum += arr[i];
System.out.println(sum);
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 3e465a936519fc1a6a1c5fad70b428b0 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | //Utilities
import java.io.*;
import java.util.*;
public class a {
static int t;
static int n;
static int[] a;
static int res;
public static void main(String[] args) throws IOException {
t = in.iscan();
while (t-- > 0) {
n = in.iscan(); a = new int[n];
res = 0;
for (int i = 0; i < n; i++) {
a[i] = in.iscan();
res |= a[i];
}
out.println(res);
}
out.close();
}
static INPUT in = new INPUT(System.in);
static PrintWriter out = new PrintWriter(System.out);
private static class INPUT {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public INPUT (InputStream stream) {
this.stream = stream;
}
public INPUT (String file) throws IOException {
this.stream = new FileInputStream (file);
}
public int cscan () throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read (buf);
}
if (numChars == -1)
return numChars;
return buf[curChar++];
}
public int iscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
int res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public String sscan () throws IOException {
int c = cscan ();
while (space (c))
c = cscan ();
StringBuilder res = new StringBuilder ();
do {
res.appendCodePoint (c);
c = cscan ();
}
while (!space (c));
return res.toString ();
}
public double dscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
double res = 0;
while (!space (c) && c != '.') {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
res *= 10;
res += c - '0';
c = cscan ();
}
if (c == '.') {
c = cscan ();
double m = 1;
while (!space (c)) {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
m /= 10;
res += (c - '0') * m;
c = cscan ();
}
}
return res * sgn;
}
public long lscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
long res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public boolean space (int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
public static class UTILITIES {
static final double EPS = 10e-6;
public static void sort(int[] a, boolean increasing) {
ArrayList<Integer> arr = new ArrayList<Integer>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(long[] a, boolean increasing) {
ArrayList<Long> arr = new ArrayList<Long>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(double[] a, boolean increasing) {
ArrayList<Double> arr = new ArrayList<Double>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static int lower_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int upper_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static void updateMap(HashMap<Integer, Integer> map, int key, int v) {
if (!map.containsKey(key)) {
map.put(key, v);
}
else {
map.put(key, map.get(key) + v);
}
if (map.get(key) == 0) {
map.remove(key);
}
}
public static long gcd (long a, long b) {
return b == 0 ? a : gcd (b, a % b);
}
public static long lcm (long a, long b) {
return a * b / gcd (a, b);
}
public static long fast_pow_mod (long b, long x, int mod) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod;
return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod;
}
public static long fast_pow (long b, long x) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow (b * b, x / 2);
return b * fast_pow (b * b, x / 2);
}
public static long choose (long n, long k) {
k = Math.min (k, n - k);
long val = 1;
for (int i = 0; i < k; ++i)
val = val * (n - i) / (i + 1);
return val;
}
public static long permute (int n, int k) {
if (n < k) return 0;
long val = 1;
for (int i = 0; i < k; ++i)
val = (val * (n - i));
return val;
}
// start of permutation and lower/upper bound template
public static void nextPermutation(int[] nums) {
//find first decreasing digit
int mark = -1;
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break;
}
}
if (mark == -1) {
reverse(nums, 0, nums.length - 1);
return;
}
int idx = nums.length-1;
for (int i = nums.length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1, nums.length - 1);
}
public static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
public static void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
static int lower_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= cmp) high = mid;
else low = mid + 1;
}
return low;
}
static int upper_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > cmp) high = mid;
else low = mid + 1;
}
return low;
}
// end of permutation and lower/upper bound template
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 756e295a5614a18c4dfd626bdc872fe6 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
public class main {
public static void main (String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
long[] a = new long[n];
long ans = 0;
for(int i=0;i<n;i++)
{
a[i] = sc.nextLong();
ans|=a[i];
}
System.out.println(ans);
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | bdd52e02ad02a42ae9c03773becf3c76 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
public class One {
static class FastScanner
{
BufferedReader br;
StringTokenizer st;
public FastScanner()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static class Pair implements Comparable<Pair>
{
long x,y;
Pair(long x, long y)
{
this.x = (int) x;
this.y = (int) y;
}
public int compareTo(Pair o)
{
return (int) (o.x-this.x);
}
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long lcm(long a,long b) {
return ((a*b)/gcd(a,b));
}
static final int mod=1_000_000_007;
static long add(long a, long b) {
return (a+b)%mod;
}
static long sub(long a, long b) {
return ((a-b)%mod+mod)%mod;
}
static long mul(long a, long b) {
return (a*b)%mod;
}
private static int[] rarr(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=sc.nextInt();
return a;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void reverse(int []arr,int i,int j) {
while(i<j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
}
public static void sort1(long[] arr) {
ArrayList<Long> ls = new ArrayList<>();
for (long x : arr) {
ls.add(x);
}
Collections.sort(ls);
for (int i = 0; i < arr.length; i++) {
arr[i] = ls.get(i);
}
}
static long exp(long base, long exp) {
if (exp==0) return 1;
long half=exp(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static FastScanner sc=new FastScanner();
public static void main(String[] args){
int t=sc.nextInt();
while(t--!=0) {
int n=sc.nextInt();
int []arr=new int[n];
int ans=0;
for(int i=0;i<arr.length;i++) {
arr[i]=sc.nextInt();
ans|=arr[i];
}
System.out.println(ans);
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | bfa0d5db4458136ef67f3c30eb267e97 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static Scanner obj = new Scanner(System.in);
public static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int len = obj.nextInt();
while (len-- != 0) {
int n=obj.nextInt();
long ans=0;
for(int i=0;i<n;i++)
{
ans|=obj.nextLong();
}
out.println(ans);
}
out.flush();
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 942795c3a70b8c83d7def1b78453f28e | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | /*
#: 1635A
Difficulty: 800
*/
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class MinOrSum
{
public static void main(String[] args) throws IOException
{
Scanner sc= new Scanner();
StringBuilder ans=new StringBuilder();
int t=sc.nextInt();
while(t-->0)
{
int a=0;
int n=sc.nextInt();
while(n-->0)
a|=sc.nextInt();
ans.append(a).append('\n');
}
System.out.println(ans);
}
static final int mod=1000000007;
static class Scanner//Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Scanner()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Scanner(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String nextLine() throws IOException
{
byte[] buf = new byte[1<<16]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == 10)
if (cnt != 0)
break;
else
continue;
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt-1);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
ret += (c - '0') / (div *= 10);
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | dadac61113ec990061b0f0ae06c79bfa | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
public class thisDaMain {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while (t>0){
int n = sc.nextInt();
ArrayList<Integer> al = new ArrayList<>();
for(int i =0;i<n;i++){
al.add(sc.nextInt());
}
int c = al.get(0);
for(int i=1;i<n;i++){
c= c|al.get(i);
}
System.out.println(c);
t--;
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 36cd5d68ef50f8b3b709b5e46cdf8b37 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.security.cert.X509CRL;
import java.util.*;
import java.lang.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
@SuppressWarnings("unused")
public class Main {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static String OUTPUT = "";
//global
private final static long BASE = 998244353L;
private final static int ALPHABET = (int)('z') - (int)('a') + 1;
//private final static int BASE = 1000000007;
private final static int INF_I = (1<<31)-1;
private final static long INF_L = (1l<<63)-1;
private final static int MAXN = 200100;
private final static int MAXK = 31;
private final static int[] DX = {-1,0,1,0};
private final static int[] DY = {0,1,0,-1};
private static long pw(int a,int n) {
if (n==0) return 1l;
if (n==1) return 1l*a;
long tmp = pw(a, n/2);
tmp = tmp * tmp %BASE;
if (n%2==0) return tmp;
return tmp*a%BASE;
}
static void solve() {
int ntest = readInt();
for (int test=0;test<ntest;test++) {
int N = readInt();
int[] A = readIntArray(N);
int ans = 0;
for (int i=0;i<N;i++) ans|=A[i];
out.println(ans);
}
}
public static void main(String[] args) throws Exception
{
long S = System.currentTimeMillis();
if (INPUT=="") {
is = System.in;
} else {
File file = new File(INPUT);
is = new FileInputStream(file);
}
if (OUTPUT == "") out = new PrintWriter(System.out);
else out = new PrintWriter(OUTPUT);
solve();
out.flush();
long G = System.currentTimeMillis();
}
private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> {
private T x;
private T y;
public Point(T x, T y) {
this.x = x;
this.y = y;
}
public T getX() {return x;}
public T getY() {return y;}
@Override
public int compareTo(Point<T> o) {
int cmp = x.compareTo(o.getX());
if (cmp==0) return y.compareTo(o.getY());
return cmp;
}
}
private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> {
public ClassComparator() {}
@Override
public int compare(T a, T b) {
return a.compareTo(b);
}
}
private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> {
public ListComparator() {}
@Override
public int compare(List<T> o1, List<T> o2) {
for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) {
int c = o1.get(i).compareTo(o2.get(i));
if (c != 0) {
return c;
}
}
return Integer.compare(o1.size(), o2.size());
}
}
private static boolean eof()
{
if(lenbuf == -1)return true;
int lptr = ptrbuf;
while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
try {
is.mark(1000);
while(true){
int b = is.read();
if(b == -1){
is.reset();
return true;
}else if(!isSpaceChar(b)){
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inbuf = new byte[1024];
static int lenbuf = 0, ptrbuf = 0;
private static int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
// private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); }
private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private static double readDouble() { return Double.parseDouble(readString()); }
private static char readChar() { return (char)skip(); }
private static String readString()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private static char[] readChar(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] readTable(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = readChar(m);
return map;
}
private static int[] readIntArray(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = readInt();
return a;
}
private static long[] readLongArray(int n) {
long[] a = new long[n];
for (int i=0;i<n;i++) a[i] = readLong();
return a;
}
private static int readInt()
{
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 static long readLong()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 3e056e4de2df475cf237bfe5ee553de4 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.*;
import java.util.Arrays;
public class B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
String[] input = br.readLine().split(" ");
int[] A = new int[n];
for (int i = 0; i < n; i++) {
A[i] = Integer.parseInt(input[i]);
}
Arrays.sort(A);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
A[i] ^= (A[j] & A[i]);
}
}
int sum = 0;
for (int s : A) {
sum += s;
}
bw.write(sum + "\n");
}
br.close();
bw.close();
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | fe064e20885549425f16b900d9472b0f | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes |
import java.io.*;
import java.util.*;
public class CODE {
public static void main(String[] args) {
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println("Error");
}
FastReader scn = new FastReader();
int n = scn.nextInt();
for (int l = 0; l < n; l++) {
int x = scn.nextInt();
long[] arr = new long[x];
long ans = scn.nextLong();
for (int i = 1; i < x; i++) {
arr[i] = scn.nextLong();
ans = (long)ans | (long)arr[i];
}
System.out.println(ans);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 7d50a3abee479aff974ec49b59fe9135 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
in = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
try {
int t = in.nextInt(); while(t-- > 0) { solve(); out.println();}
// solve();
} finally {
out.close();
}
return;
}
public static void solve() {
int n = in.nextInt();
int ans = 0;
for(int i = 0; i < n; i++) {
int curr = in.nextInt();
ans |= curr;
}
out.print(ans);
}
//-------------- Helper methods-------------------
public static int[] fillArray(int n) {
int[] array = new int[n];
for(int i = 0; i < n; i++) {
array[i] = in.nextInt();
}
return array;
}
public static char[] fillArray() {
char[] array = in.next().toCharArray();
return array;
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
public static MyScanner in;
//-----------MyScanner class for faster input----------x§x
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;
}
}
static void shuffleArray(int[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
//--------------------------------------------------------
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | f1ede1b8ba5b52fb422831c94b7e3849 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int t=in.nextInt();
while(t-->0){
int n=in.nextInt();
int[] array=new int[n];
for (int i = 0; i <n; i++) {
array[i]=in.nextInt();
}
/// 1 ,2
for (int i = 0; i < n-1; i++) {
int sum=0;
for (int j = i+1; j <i+2; j++) {//j=2; 2<3;
sum=array[i]|array[j]; // 11
array[i+1]=sum;
}
}
System.out.println(array[array.length-1]);
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 03e1e1f687e9039401b7dfe2288d1065 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes |
// Java is like Alzheimer's, it starts off slow, but eventually, your memory is gone.
import java.io.*;
import java.util.*;
public class Aqueous {
static MyScanner sc = new MyScanner();
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int a[] = new int[n];
long sum = 0;
HashSet<Integer> hs = new HashSet<>();
for(int i = 0; i<n; i++) {
a[i] =sc.nextInt();
String s = Integer.toBinaryString(a[i]);
for(int j = s.length()-1; j>=0; j--) {
if(s.charAt(j)=='1') {
if(!hs.contains(s.length()-1-j)) {
sum += 1<<(s.length()-1)-j;
hs.add(s.length()-1-j);
}
}
}
}
pw.println(sum);
}
pw.close();
}
static void ruffleSort(int a[]) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < n; i++) {
int oi = r.nextInt(n);
int temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long a[]) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < n; i++) {
int oi = r.nextInt(n);
long temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | e493ee74af2f8c30e8fcd4206e801703 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class MaximalAnd {
static int[] memo;
public static void main(String[] args) throws Exception {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int arr[] = new int[n];
int ans = 0;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
ans |= arr[i];
}
pw.println(ans);
}
pw.flush();
}
// isPrime
public static boolean isPrime(long n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}
// get the nth fibonacci number using BigInteger
// public static BigInteger fib(int n) {
// BigInteger a = BigInteger.valueOf(0);
// BigInteger b = BigInteger.valueOf(1);
// for (int i = 0; i < n; i++) {
// BigInteger c = a.add(b);
// a = b;
// b = c;
// }
// return b;
// }
// // isPrime using BigInteger
// public static boolean isPrime(BigInteger n) {
// if (n.compareTo(BigInteger.valueOf(2)) < 0)
// return false;
// if (n.compareTo(BigInteger.valueOf(2)) == 0)
// return true;
// if (n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0)) == 0)
// return false;
// BigInteger d = n.subtract(BigInteger.valueOf(1));
// BigInteger s = d.divide(BigInteger.valueOf(2));
// while (s.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0)) == 0) {
// s = s.divide(BigInteger.valueOf(2));
// }
// for (BigInteger i = BigInteger.valueOf(2); i.compareTo(s) <= 0; i =
// i.add(BigInteger.valueOf(1))) {
// if (n.mod(i).compareTo(BigInteger.valueOf(0)) == 0)
// return false;
// }
// return true;
// }
static class fenwickTree {
static long[] tree;
static int[] arr;
static int n;
public fenwickTree(int[] arr) {
this.arr = arr;
n = arr.length;
tree = new long[n + 1];
long[] prefixSum = new long[n];
prefixSum[0] = arr[0];
for (int i = 1; i < n; i++) {
prefixSum[i] = prefixSum[i - 1] + arr[i];
}
for (int i = 1; i <= n; i++) {
int lsb = LSB(i);
tree[i] = prefixSum[i - 1] - (i == lsb ? 0 : prefixSum[i - 1 - lsb]);
}
}
public static long getSum(int l, int r) {
if (l > r)
return 0;
return getPrefixSum(r) - getPrefixSum(l - 1);
}
public static long getPrefixSum(int idx) {
long ans = 0;
while (idx > 0) {
ans += tree[idx];
idx -= LSB(idx);
}
return ans;
}
public static void updatePoint(int idx, int newVal) {
int change = newVal - arr[idx - 1];
arr[idx - 1] = newVal;
while (idx <= n) {
tree[idx] += change;
idx += LSB(idx);
}
}
public static int LSB(int x) {
return x & -x;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
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[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
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 Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
static long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 1dd29f5980a10a7b9ddda8f49b2deeeb | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
import java.util.stream.*;
public class Sequence {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
scan.nextLine();
StringBuilder result = new StringBuilder();
for(int i = 0; i < t; i++) {
int n = scan.nextInt();
long overall = 0l;
for(int j = 0; j < n; j++) {
long next = scan.nextLong();
overall = overall | next;
}
result.append(overall + "\n");
}
System.out.println(result);
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | a20cdc01525e3ebbfc4dd2999fb1857a | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class cf1635A {
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t-->0) {
long sol = 0;
int n = sc.nextInt();
for(int i=0; i<n; i++) sol |= sc.nextLong();
System.out.println(sol);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | bcab9563a3d78a3e140bf2adcb78c787 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner in = new Scanner(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int n = in.nextInt();
int ans = 0;
for (int i = 0; i < n; i++) {
int a = in.nextInt();
ans |= a;
}
out.println(ans);
}
out.close();
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 22a32c21617cc167dae93296e3b8fdec | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int t=in.nextInt();
for(int c=0;c<t;c++)
{
int n=in.nextInt();
int result=0;
int value=0;
for(int i=0;i<n;i++)
{
value=in.nextInt();
result=result|value;
}
System.out.println(result);
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 41cd71bb248856c266ee0e15331aa7ff | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.*;
import java.util.*;
public class Q1635B {
static int mod = (int) (1e9 + 7);
static void solve() {
int n=i();
long[]arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=l();
}
long val=arr[0];
for(int i=1;i<arr.length;i++){
val|=arr[i];
}
System.out.println(val);
}
public static void main(String[] args) {
int test = i();
while (test-- > 0) {
solve();
}
}
// -----> POWER ---> long power(long x, long y) <---- power
// -----> LCM ---> long lcm(long x, long y) <---- lcm
// -----> GCD ---> long gcd(long x, long y) <---- gcd
// -----> NCR ---> long ncr(int n, int r) <---- ncr
// -----> (SORTING OF LONG, CHAR,INT) -->long[] sortLong(long[] a2)<--
// -----> (INPUT OF INT,LONG ,STRING) -> int i() long l() String s()<-
// tempstart
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static InputReader in = new InputReader(System.in);
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 245abc4e151c140c1973cc449af90957 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
import java.io.*;
public class Question4 {
static boolean multipleTC = true;
final static int Mod = 1000000007;
final static int Mod2 = 998244353;
final double PI = 3.14159265358979323846;
int MAX = 1000000007;
void pre() throws Exception {
}
long[] calMax(List<Long> list, int strtIn, int endIn, int n){
long total = 1;
long ans[] = new long[3];
for(int i=1;i<(list.size()-1);i++)
total *= list.get(i);
pn(total);
if(total > 0) {
ans[0] = total;
ans[1] = strtIn - 1;
ans[2] = n - endIn;
return ans;
}
int i = strtIn, j = endIn;
long prefixProd = 1, suffixProd = 1;
boolean flag1 = false, flag2 = false;
while(!flag1 || !flag2) {
if(!flag1) {
prefixProd *= list.get(i);
if(list.get(i) < 0) {
flag1 = true;
}
else
i++;
}
if(!flag2) {
suffixProd *= list.get(j);
if(list.get(j) < 0) {
flag2 = false;
}
else
j--;
}
}
if(total/prefixProd >= total/suffixProd) {
total /= prefixProd;
ans[0] = total;
ans[1] = i;
ans[2] = n - endIn;
return ans;
}
total /= suffixProd;
ans[0] = total;
ans[1] = strtIn - 1;
ans[2] = (n - (j-1));
return ans;
}
void solve(int t) throws Exception {
int n = ni();
long ans = 0;
for(int i=1;i<=n;i++) {
long ele = nl();
ans = ans | ele;
}
pn(ans);
}
double dist(int x1, int y1, int x2, int y2) {
double a = x1 - x2, b = y1 - y2;
return Math.sqrt((a * a) + (b * b));
}
int[] readArr(int n) throws Exception {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
void sort(int arr[], int left, int right) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = left; i <= right; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = left; i <= right; i++)
arr[i] = list.get(i - left);
}
void sort(int arr[]) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < arr.length; i++)
arr[i] = list.get(i);
}
public long max(long... arr) {
long max = arr[0];
for (long itr : arr)
max = Math.max(max, itr);
return max;
}
public int max(int... arr) {
int max = arr[0];
for (int itr : arr)
max = Math.max(max, itr);
return max;
}
public long min(long... arr) {
long min = arr[0];
for (long itr : arr)
min = Math.min(min, itr);
return min;
}
public int min(int... arr) {
int min = arr[0];
for (int itr : arr)
min = Math.min(min, itr);
return min;
}
public long sum(long... arr) {
long sum = 0;
for (long itr : arr)
sum += itr;
return sum;
}
public long sum(int... arr) {
long sum = 0;
for (int itr : arr)
sum += itr;
return sum;
}
String bin(long n) {
return Long.toBinaryString(n);
}
String bin(int n) {
return Integer.toBinaryString(n);
}
static int bitCount(int x) {
return x == 0 ? 0 : (1 + bitCount(x & (x - 1)));
}
static void dbg(Object... o) {
System.err.println(Arrays.deepToString(o));
}
int bit(long n) {
return (n == 0) ? 0 : (1 + bit(n & (n - 1)));
}
int abs(int a) {
return (a < 0) ? -a : a;
}
long abs(long a) {
return (a < 0) ? -a : a;
}
void p(Object o) {
out.print(o);
}
void pn(Object o) {
out.println(o);
}
void pni(Object o) {
out.println(o);
out.flush();
}
void pn(int[] arr) {
int n = arr.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(arr[i] + " ");
}
pn(sb);
}
void pn(long[] arr) {
int n = arr.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(arr[i] + " ");
}
pn(sb);
}
String n() throws Exception {
return in.next();
}
String nln() throws Exception {
return in.nextLine();
}
int ni() throws Exception {
return Integer.parseInt(in.next());
}
long nl() throws Exception {
return Long.parseLong(in.next());
}
double nd() throws Exception {
return Double.parseDouble(in.next());
}
public static void main(String[] args) throws Exception {
new Question4().run();
}
FastReader in;
PrintWriter out;
void run() throws Exception {
in = new FastReader();
out = new PrintWriter(System.out);
int T = (multipleTC) ? ni() : 1;
pre();
for (int t = 1; t <= T; t++)
solve(t);
out.flush();
out.close();
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception {
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
throw new Exception(e.toString());
}
return str;
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 285198e4bdd7f2a2dab84eac4a574e6c | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
outer: while(x-->0)
{
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
if(n==1)
System.out.println(a[0]);
else{
int ans=a[0]|a[1];
for(int i=2;i<n;i++)
ans|=a[i];
System.out.println(ans);
}
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 23c58fe45ec3193068caf08fdfe0ee73 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;
// import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Set;
import java.util.Stack;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
// import java.util.Set;
// import java.util.TreeSet;
// import java.util.stream.Collectors;
// import java.util.stream.Stream;
// import java.util.Stack;
// import java.util.Optional;
// import java.util.HashSet;
import java.io.*;
public class sol {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int nn=sc.nextInt();
int maxvalue=0;
for(int i=0;i<nn;i++){
int temp=sc.nextInt();
maxvalue=maxvalue|temp;
}
// String temp=Integer.toBinaryString(maxvalue);
System.out.println(maxvalue);
// System.out.println(Integer.parseInt(temp.replaceAll("0","1"),2));
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 56552adfb6573e369acb7fe720cd8a38 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | public class MinOrSum {
private static final java.util.Scanner sc=new java.util.Scanner(System.in);
public void Solve(int test)
{
Integer[] arr;
int ans=0;
while (test>0)
{
int size= sc.nextInt();
arr=new Integer[size];
for (int i = 0; i <size; i++)
arr[i]= sc.nextInt();
java.util.List<Integer> num=java.util.Arrays.asList(arr);
ans= num.stream().reduce(0,(a,b)->a | b);
System.out.println(ans);
test--;
}
}
public static void main(String[] args) {
new MinOrSum().Solve(sc.nextInt());
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | f8412d6ef996bc8f273dc71c39fe08cb | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import static java.lang.Math.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class temp {
// Let's Go!! ------------->
static FastScanner sc;
static PrintWriter out;
public static void main(String[] args) {
sc = new FastScanner();
out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0) {
int ans = 0;
int n = ni();
for(int i = 0; i< n; i++) {
ans |= ni();
}
pn(ans);
}
// -------END-------
out.close();
}
// <-----------------------Template --------------------->
// Static Initializations ------>
static final long MOD = (long)1e9+7;
static final int MAX = (int)1e5+1, INF = (int)1e9;
// GCD -------->
static long gcdl(long a, long b){return (b==0)?a:gcdl(b,a%b);}
static int gcdi(int a, int b){return (b==0)?a:gcdi(b,a%b);}
// Pair Class ----------->
static class pair{
int first, second;
pair(int first, int second) {
this.first = first;
this.second = second;
}
}
// Ruffle Sort
static void ruffleSort(int[] a) {
//Shuffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n), temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
// Fast I/O
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next(){
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
int nextInt() {return Integer.parseInt(next());}
long nextLong() {return Long.parseLong(next());}
double nextDouble() {return Double.parseDouble(next());}
}
//Quick Print Statements ---->
static void p(Object o){out.print(o);}
static void pn(Object o){out.println(o);}
static void pni(Object o){out.println(o);out.flush();}
// Quick Input Statements ----->
static String n(){return sc.next();}
static String nln(){return sc.nextLine();}
static int ni(){return Integer.parseInt(sc.next());}
static long nl(){return Long.parseLong(sc.next());}
static double nd(){return Double.parseDouble(sc.next());}
//Integer Array Input ---->
static int[] readIntArr(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]= sc.nextInt();
return a;
}
//Long Array Input ----->
static long[] readLongArr(int N){
long[] a = new long[N];
for(int i = 0; i<N; i++)a[i] = sc.nextLong();
return a;
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | e3fd0e2e2aacdd450dd5805b889ba1aa | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 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 {
void solve() throws IOException {
int t = nextInt();
for (int tt = 0; tt < t; ++tt) {
int n = nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; ++i) {
arr[i] = nextInt();
}
/*
1 2 3 4 5
1011
0110
1111
101 => 001, 5 -> 1 (diff 4)
111 => 011, 7 -> 3 (diff 4)
5 7 9 1
i
j
01
11
01
10
100 * 100 * 1000 = 10.000.000 * 30
*/
long sum = 0;
int[] count = new int[31];
/*
1 3 2, sum=6
0,2,2
*/
for (int i = 0; i < arr.length; ++i) {
sum += arr[i];
int tmp = arr[i];
int c = 0;
while (tmp > 0) {
if ((tmp & 1) == 1) {
++count[c];
}
++c;
tmp >>= 1;
}
}
for (int i = 0; i < count.length; ++i) {
if (count[i] > 1)
sum -= (1L << i) * (count[i] - 1);
}
writer.println(sum);
}
}
String rev(String s) {
char[] arr = s.toCharArray();
for (int i = 0; i < arr.length / 2; ++i) {
char tmp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = tmp;
}
return new String(arr);
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
StringTokenizer tokenizer;
PrintWriter writer;
BufferedReader reader;
public void run() {
try {
writer = new PrintWriter(System.out);
reader = new BufferedReader(new InputStreamReader(System.in));
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new A().run();
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 17ef33c606f3830d7ea9022c735e0f26 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.Scanner;
public class MinOrSum {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int i=0; i<t; i++){
int n = s.nextInt();
int[] nums = new int[n];
int sum = 0;
for(int j=0; j<n; j++){
nums[j] = s.nextInt();
}
for(int j=0; j<n; j++){
sum = sum | nums[j];
}
System.out.println(sum);
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | bab46f4f533cf637cee5c725c059da64 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
public class A_Min_Or_Sum {
public static void main(String[] args) {
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
FastReader f = new FastReader();
int t = f.nextInt();
while(t-- > 0){
int n = f.nextInt();
int ans = 0;
for(int i = 0; i < n; i++) {
ans |= f.nextInt();
}
out.println(ans);
}
out.close();
}
public static void allDivisors(int n) {
for(int i = 1; i*i <= n; i++) {
if(n%i == 0) {
System.out.println(i + " ");
if(i != n/i) {
System.out.println(n/i + " ");
}
}
}
}
public static boolean isPrime(int n) {
if(n < 1) return false;
if(n == 2 || n == 3) return true;
if(n % 2 == 0 || n % 3 == 0) return false;
for(int i = 5; i*i <= n; i += 6) {
if(n % i == 0 || n % (i+2) == 0) {
return false;
}
}
return true;
}
public static int Gcd(int a, int b) {
int dividend = a > b ? a : b;
int divisor = a < b ? a : b;
while(divisor > 0) {
int reminder = dividend % divisor;
dividend = divisor;
divisor = reminder;
}
return dividend;
}
public static int lcm1(int a, int b) {
int lcm = Gcd(a, b);
int hcf = (a * b) / lcm;
return hcf;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
boolean nextBoolean() {
return Boolean.parseBoolean(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
/**
Dec Char Dec Char Dec Char Dec Char
--------- --------- --------- ----------
0 NUL (null) 32 SPACE 64 @ 96 `
1 SOH (start of heading) 33 ! 65 A 97 a
2 STX (start of text) 34 " 66 B 98 b
3 ETX (end of text) 35 # 67 C 99 c
4 EOT (end of transmission) 36 $ 68 D 100 d
5 ENQ (enquiry) 37 % 69 E 101 e
6 ACK (acknowledge) 38 & 70 F 102 f
7 BEL (bell) 39 ' 71 G 103 g
8 BS (backspace) 40 ( 72 H 104 h
9 TAB (horizontal tab) 41 ) 73 I 105 i
10 LF (NL line feed, new line) 42 * 74 J 106 j
11 VT (vertical tab) 43 + 75 K 107 k
12 FF (NP form feed, new page) 44 , 76 L 108 l
13 CR (carriage return) 45 - 77 M 109 m
14 SO (shift out) 46 . 78 N 110 n
15 SI (shift in) 47 / 79 O 111 o
16 DLE (data link escape) 48 0 80 P 112 p
17 DC1 (device control 1) 49 1 81 Q 113 q
18 DC2 (device control 2) 50 2 82 R 114 r
19 DC3 (device control 3) 51 3 83 S 115 s
20 DC4 (device control 4) 52 4 84 T 116 t
21 NAK (negative acknowledge) 53 5 85 U 117 u
22 SYN (synchronous idle) 54 6 86 V 118 v
23 ETB (end of trans. block) 55 7 87 W 119 w
24 CAN (cancel) 56 8 88 X 120 x
25 EM (end of medium) 57 9 89 Y 121 y
26 SUB (substitute) 58 : 90 Z 122 z
27 ESC (escape) 59 ; 91 [ 123 {
28 FS (file separator) 60 < 92 \ 124 |
29 GS (group separator) 61 = 93 ] 125 }
30 RS (record separator) 62 > 94 ^ 126 ~
31 US (unit separator) 63 ? 95 _ 127 DEL
*/
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 5acfbc38ee685fbfecd92feb13fa673e | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
import java.io.*;
public class B {
static FastScanner fs = new FastScanner();
static PrintWriter pw = new PrintWriter(System.out);
static StringBuilder sb = new StringBuilder("");
public static void main(String[] args) {
int t = fs.nextInt();
for (int tt = 0; tt < t; tt++) {
int n = fs.nextInt();
int ans = 0;
for (int i = 0; i < n; i++) {
int x = fs.nextInt();
ans |= x;
}
sb.append(ans + "\n");
}
pw.print(sb.toString());
pw.close();
}
static void sort(long[] a) {
ArrayList<Long> al = new ArrayList<>();
for (long i : a)
al.add(i);
Collections.sort(al);
for (int i = 0; i < a.length; i++)
a[i] = al.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {while (!st.hasMoreTokens()) try {st = new StringTokenizer(br.readLine());} catch (IOException e) {}return st.nextToken();}
int nextInt() {return Integer.parseInt(next());}
long nextLong() {return Long.parseLong(next());}
double nextDouble() {return Double.parseDouble(next());}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 44fe019e926aa672cf2c5cf27d9b71b4 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
int qwe = in.nextInt();
while (qwe-- > 0) {
int n = in.nextInt();
int sum = 0;
for (int i = 0; i < n; i++) {
sum|=in.nextInt();
}
System.out.println(sum);
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(in), 16384);
eat("");
}
public void eat(String s) {
st = new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
return null;
}
}
public boolean hasNext() {
while (!st.hasMoreTokens()) {
String s = nextLine();
if (s == null)
return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
static FastScanner in = new FastScanner(System.in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 073d35897ab36b3a0244d299d98df5c7 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
// when can't think of anything -->>
// 1. In sorting questions try to think about all possibilities like sorting from start, end, middle.
// 2. Two pointers, brute force.
// 3. In graph query questions try to solve it reversely or try to process all the queries in a single parse.
// 4. If order does not matter then just sort the data if constraints allow. It'll never harm.
// 5. In greedy problems if you are just overwhelmed by the possibilities and stuck, try to code whatever you come up with.
// 6. Think like a robot, just consider all the possibilities not probabilities if you still can't solve.
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter writer = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int ans = 0;
for (int i = 0; i < n; i++) {
ans|=sc.nextInt();
}
writer.println(ans);
}
writer.flush();
writer.close();
}
private static boolean isPrime(int c) {
for (int i = 2; i*i <= c; i++) {
if(c%i==0)return false;
}
return true;
}
private static int find(int a , int arr[]) {
if(arr[a] != a) return arr[a] = find(arr[a], arr);
return arr[a];
}
private static void union(int a, int b, int arr[]) {
int aa = find(a,arr);
int bb = find(b, arr);
arr[aa] = bb;
}
private static int gcd(int a, int b) {
if(a==0)return b;
return gcd(b%a, a);
}
public static int[] readIntArray(int size, FastReader s) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = s.nextInt();
}
return array;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
class Pair implements Comparable<Pair>{
int a;
int b;
Pair(int a, int b){
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null || obj.getClass()!= this.getClass()) return false;
Pair pair = (Pair) obj;
return (pair.a == this.a && pair.b == this.b);
}
@Override
public int hashCode()
{
return Objects.hash(a,b);
}
@Override
public int compareTo(Pair o) {
if(this.a == o.a) {
return Integer.compare(this.b, o.b);
}else {
return Integer.compare(this.a, o.a);
}
}
@Override
public String toString() {
return this.a + " " + this.b;
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | de00c07482143193d179d7462bb7a9a6 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.StringTokenizer;
public class D {
{
MULTI_TEST = true;
FILE_NAME = "";
NEED_FILE_IO = false;
INF = (long) 1e9;
} // fields
void solve() {
int n = ri();
int[] a = ria(n);
int res = a[0];
for(var el : a) {
res|=el;
}
out.println(res);
}
public static void main(String[] args) {
new D().run();
} //main
@SuppressWarnings("unused")
long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
@SuppressWarnings("unused")
long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
@SuppressWarnings("unused")
int gcd(int a, int b) {
return (int) gcd((long) a, b);
}
@SuppressWarnings("unused")
int lcm(int a, int b) {
return (int) lcm(a, (long) b);
}
@SuppressWarnings("unused")
int sqrtInt(int x) {
return (int) sqrtLong(x);
}
@SuppressWarnings("unused")
long sqrtLong(long x) {
long root = (long) Math.sqrt(x);
while (root * root > x) --root;
while ((root + 1) * (root + 1) <= x) ++root;
return root;
}
@SuppressWarnings("unused")
int cbrtLong(long x) {
int cbrt = (int) Math.cbrt(x);
while ((long) cbrt * cbrt >= x) {
cbrt--;
}
while ((long) (cbrt + 1) * (cbrt + 1) <= x) cbrt++;
return cbrt;
}
@SuppressWarnings("unused")
long binpow(long a, long power) {
return binpow(a, power, INF);
}
@SuppressWarnings("unused")
long binpow(long a, long power, long modulo) {
if (power == 0) return 1 % modulo;
if (power % 2 == 1) {
long b = binpow(a, power - 1, modulo) % modulo;
return ((a % modulo) * b) % modulo;
} else {
long b = binpow(a, power / 2, modulo) % modulo;
return (b * b) % modulo;
}
}
@SuppressWarnings("unused")
long fastMod(String s1, long n2) {
long num = 0;
for (int i = 0; i < s1.length() - 1; i++) {
num += Integer.parseInt(String.valueOf(s1.charAt(i)));
num *= 10;
if (num >= n2) {
num = num % n2;
}
}
return (num + Integer.parseInt(String.valueOf(s1.charAt(s1.length() - 1)))) % n2;
}
@SuppressWarnings("unused")
long factorialMod(long n, long p) {
long res = 1;
while (n > 1) {
res = (res * ((n / p) % 2 == 1 ? p - 1 : 1)) % p;
for (int i = 2; i <= n % p; ++i)
res = (res * i) % p;
n /= p;
}
return res % p;
}
@SuppressWarnings("unused")
boolean isPrime(int number) {
for (int i = 2; i * i <= number; i++) {
if (number % i == 0) {
return false;
}
}
return number > 1;
}
@SuppressWarnings("unused")
boolean[] primes(int border) {
boolean[] isPrimes = new boolean[border + 1];
Arrays.fill(isPrimes, true);
isPrimes[0] = false;
isPrimes[1] = false;
for (int i = 2; i < border + 1; i++) {
if (!isPrimes[i]) continue;
for (int k = i * 2; k < border; k += i) {
isPrimes[k] = false;
}
}
return isPrimes;
} //Number theory
@SuppressWarnings("unused")
void sort(int[] a) {
int n = a.length;
Integer[] arr = new Integer[n];
for (int i = 0; i < n; i++) {
arr[i] = a[i];
}
Arrays.sort(arr);
for (int i = 0; i < n; i++) {
a[i] = arr[i];
}
}
@SuppressWarnings("unused")
void sort(long[] a) {
int n = a.length;
Long[] arr = new Long[n];
for (int i = 0; i < n; i++) {
arr[i] = a[i];
}
Arrays.sort(arr);
for (int i = 0; i < n; i++) {
a[i] = arr[i];
}
}
@SuppressWarnings("unused")
int max(int[] a) {
int n = a.length;
int max = Integer.MIN_VALUE;
for (int j : a) {
if (j > max) max = j;
}
return max;
}
@SuppressWarnings("unused")
long max(long[] a) {
int n = a.length;
long max = Long.MIN_VALUE;
for (long l : a) {
if (l > max) max = l;
}
return max;
}
@SuppressWarnings("unused")
int maxIndex(int[] a) {
int n = a.length;
int max = Integer.MIN_VALUE;
int index = 0;
for (int i = 0; i < n; i++) {
if (a[i] > max) {
max = a[i];
index = i;
}
}
return index;
}
@SuppressWarnings("unused")
int maxIndex(long[] a) {
int n = a.length;
long max = Long.MIN_VALUE;
int index = 0;
for (int i = 0; i < n; i++) {
if (a[i] > max) {
max = a[i];
index = i;
}
}
return index;
}
@SuppressWarnings("unused")
int min(int[] a) {
int n = a.length;
int min = Integer.MAX_VALUE;
for (int j : a) {
if (j < min) min = j;
}
return min;
}
@SuppressWarnings("unused")
int minIndex(int[] a) {
int n = a.length;
int min = Integer.MAX_VALUE;
int index = 0;
for (int i = 0; i < n; i++) {
if (a[i] < min) {
min = a[i];
index = i;
}
}
return index;
}
@SuppressWarnings("unused")
int minIndex(long[] a) {
int n = a.length;
long min = Long.MAX_VALUE;
int index = 0;
for (int i = 0; i < n; i++) {
if (a[i] < min) {
min = a[i];
index = i;
}
}
return index;
}
@SuppressWarnings("unused")
long min(long[] a) {
int n = a.length;
long min = Long.MAX_VALUE;
for (long l : a) {
if (l < min) min = l;
}
return min;
}
@SuppressWarnings("unused")
long sum(int[] a) {
int n = a.length;
long sum = 0;
for (int j : a) {
sum += j;
}
return sum;
}
@SuppressWarnings("unused")
long sum(long[] a) {
int n = a.length;
long sum = 0;
for (long l : a) {
sum += l;
}
return sum;
} //Arrays Operations
@SuppressWarnings("unused")
String readLine() {
try {
return in.readLine();
} catch (Exception e) {
throw new InputMismatchException();
}
}
@SuppressWarnings("unused")
String rs() {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(readLine());
}
return tok.nextToken();
}
@SuppressWarnings("unused")
int ri() {
return Integer.parseInt(rs());
}
@SuppressWarnings("unused")
long rl() {
return Long.parseLong(rs());
}
@SuppressWarnings("unused")
double rd() {
return Double.parseDouble(rs());
}
@SuppressWarnings("unused")
int[] ria(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ri();
}
return a;
}
@SuppressWarnings("unused")
int[] riawd(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ri() - 1;
}
return a;
}
@SuppressWarnings("unused")
long[] rla(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = rl();
}
return a;
}
@SuppressWarnings("unused")
private boolean yesNo(boolean yes, String yesString, String noString) {
out.println(yes ? yesString : noString);
return yes;
} //fastIO
void run() {
try {
long start = System.currentTimeMillis();
initIO();
if (MULTI_TEST) {
long t = rl();
while (t-- > 0) {
solve();
}
} else {
solve();
}
out.close();
System.err.println("Time(ms) - " + (System.currentTimeMillis() - start));
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
void initConsoleIO() {
out = new PrintWriter(System.out);
in = new BufferedReader(new InputStreamReader(System.in));
}
void initFileIO(String inName, String outName) throws FileNotFoundException {
out = new PrintWriter(outName);
in = new BufferedReader(new FileReader(inName));
}
void initIO() throws FileNotFoundException {
if (!FILE_NAME.isEmpty()) {
initFileIO(FILE_NAME + ".in", FILE_NAME + ".out");
} else {
if (NEED_FILE_IO && new File("input.txt").exists()) {
initFileIO("input.txt", "output.txt");
} else {
initConsoleIO();
}
}
tok = new StringTokenizer("");
} //initIO
private final String FILE_NAME;
private final boolean MULTI_TEST;
private final boolean NEED_FILE_IO;
private final long INF;
BufferedReader in;
PrintWriter out;
StringTokenizer tok; //fields
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | db29fbb9daad7c8c993239d52f81fa7c | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.*;
import java.util.*;
public class JavaApplication {
static BufferedReader in;
static StringTokenizer st;
String getLine() throws IOException {
return in.readLine();
}
String getToken() throws IOException {
if (st == null || !st.hasMoreTokens())
st = new StringTokenizer(getLine());
return st.nextToken();
}
int getInt() throws IOException {
return Integer.parseInt(getToken());
}
long getLong() throws IOException {
return Long.parseLong(getToken());
}
void Solve() throws IOException {
int t = getInt();
while (t-- > 0) {
int n = getInt();
int a[] = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
a[i] = getInt();
sum |= a[i];
}
System.out.println(sum);
}
}
public static void main(String[] args) throws java.lang.Exception {
in = new BufferedReader(new InputStreamReader(System.in));
new JavaApplication().Solve();
return;
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | c13beeb1b11b835294ab908f184d4d7c | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static FastScanner sc;
static PrintWriter pw;
public static void main(String[] args) throws Exception{
sc = new FastScanner();
pw = new PrintWriter(System.out);
try{
int T = sc.ni();
while(T-->0){
int n = sc.ni();
long a[] = sc.longArray(n);
long ans = 0;
for(int i=0; i<n; i++){
ans = ans|a[i];
}
pw.println(ans);
}
}catch(Exception e){
return;
}
pw.close();
}
static void swap(int a[], int i, int j){
while(i<j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
st = null;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
int[] intArray(int N) {
int[] ret = new int[N];
for (int i = 0; i < N; i++)
ret[i] = ni();
return ret;
}
long nl() {
return Long.parseLong(next());
}
long[] longArray(int N) {
long[] ret = new long[N];
for (int i = 0; i < N; i++)
ret[i] = nl();
return ret;
}
double nd() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 57954d161bf1c61f3fd1383a43460830 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static final int INF = Integer.MAX_VALUE;
static final int NINF = Integer.MIN_VALUE;
static final long LINF = Long.MAX_VALUE;
static final long LNINF = Long.MIN_VALUE;
static Reader reader;
static Writer writer;
static PrintWriter out;
static FastScanner fs;
static void solve() {
int n = fs.nextInt();
int[] a = fs.readArrayInt(n);
sort(a);
for (int i = n - 1; i > 0; i--) {
for (int j = i - 1; j >= 0; j--) {
for (int k = 0; k <= 31; k++) {
if ((a[j]&(1<<k)) > 0 ) {
a[i] &= (~(1<<k));
}
}
}
}
int s = 0;
for(int i : a)s += i;
out.print(s + "\n");
}
public static void main(String[] args) {
setReaderWriter();
fs = new FastScanner(reader);
testForTestcases();
// solve();
out.close();
}
static void setReaderWriter() {
reader = new InputStreamReader(System.in);
writer = new OutputStreamWriter(System.out);
out=new PrintWriter(writer);
}
static void testForTestcases() {
int T = fs.nextInt();
while (T-- > 0) {
solve();
}
}
static boolean isInteger(double val) {
return !(val - (long)val > 0);
}
static void swap(int[] a , int i , int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
static int opposite(int n, int x) {
return (n-1)^x;
}
static Pair print(int a, int b) {
out.println(a+" "+b);
return new Pair(a, b);
}
static class Pair {
int a, b;
public Pair(int a, int b) {
this.a=a;
this.b=b;
}
}
static final Random random=new Random();
static final int mod=1_000_000_007;
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long add(long a, long b) {
return (a+b)%mod;
}
static long sub(long a, long b) {
return ((a-b)%mod+mod)%mod;
}
static long mul(long a, long b) {
return (a*b)%mod;
}
static long exp(long base, long exp) {
if (exp==0) return 1;
long half=exp(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static long[] factorials=new long[2_000_001];
static long[] invFactorials=new long[2_000_001];
static void precompFacts() {
factorials[0]=invFactorials[0]=1;
for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i);
invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2);
for (int i=invFactorials.length-2; i>=0; i--)
invFactorials[i]=mul(invFactorials[i+1], i+1);
}
static long nCk(int n, int k) {
return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k]));
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(Reader reader) {
br =new BufferedReader(reader);
st =new StringTokenizer("");
}
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArrayInt(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
char[] readCharArray() {
return next().toCharArray();
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 31315966dce7ff7b1f64362cfe6faf6d | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
import java.io.*;
public class Min_Or_Sum
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int test=sc.nextInt();
while(test>0)
{
int n=sc.nextInt();
long arr[]=new long[n];
long brr[]=new long[31];
for(int i=0; i<n; i++)
{
//System.
arr[i]=sc.nextLong();
//System.out.println("ARR "+arr[i]);
int j=0;
while(j<31 && arr[i]>0)
{
long mod=arr[i]%2;
if(mod==1)
brr[j]=1;
arr[i]=arr[i]/2;
j++;
}
}
long ans=0;
for(int i=0; i<brr.length; i++)
{
if(brr[i]==1)
ans=ans+(long)Math.pow(2,i);
}
System.out.println(ans);
test--;
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | c6c7e5d6403b3d09f43e643ea1856bd3 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static MyScanner sc;
static PrintWriter out;
static {
sc = new MyScanner();
out = new PrintWriter(System.out);
}
public static void solve() {
int n = sc.nextInt();
Integer[] a = new Integer[n];
int ans = 0;
for(int i = 0; i < n; i++)
ans |= sc.nextInt();
out.println(ans);
}
public static void main(String[] args) {
int t = sc.nextInt();
while(t-- > 0)
solve();
out.flush();
}
}
class MyScanner {
BufferedReader br;
StringTokenizer tok;
MyScanner() {
try { br = new BufferedReader(new InputStreamReader(System.in)); }
catch(Exception e) { System.out.println(e); }
tok = new StringTokenizer("");
}
public String next() {
try {
while(!tok.hasMoreTokens()) tok = new StringTokenizer(br.readLine());
}
catch(Exception e) { System.out.println(e); }
return tok.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 8c24ea51400246e1bdd0a53392a18a85 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t=s.nextInt();
for(int k=0;k<t;k++){
int n = s.nextInt();
int a = s.nextInt();
for(int i=1; i<n; i++)
{
a|=s.nextInt();
}
System.out.println(a);
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 858bc81d41fc0456584ec3fdff653d2e | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.*;
import java.util.*;
public class codeforces {
static class in {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static String next() throws IOException {
while (!tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
//System.out.println(" I WAS CALLED");
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class AVLTreePBDS { private Node root; private boolean multi;
AVLTreePBDS(boolean multi) {
this.root = null;
this.multi = multi;
}
int size() {
return size(root);
}
boolean isEmpty() {
return size(root) == 0;
}
boolean contains(int key) {
return contains(root, key);
}
void add(int key) {
root = add(root, key);
}
void remove(int key) {
root = remove(root, key);
}
Integer first() {
Node min = findMin(root);
return min != null ? min.key : null;
}
Integer last() {
Node max = findMax(root);
return max != null ? max.key : null;
}
Integer poolFirst() {
Node min = findMin(root);
if (min != null) {
remove(min.key);
return min.key;
}
return null;
}
Integer poolLast() {
Node max = findMax(root);
if (max != null) {
remove(max.key);
return max.key;
}
return null;
}
// min >= key
Integer ceiling(int key) {
return contains(key) ? key : higher(key);
}
// max <= key
Integer floor(int key) {
return contains(key) ? key : lower(key);
}
// min > key
Integer higher(int key) {
Node min = higher(root, key);
return min == null ? null : min.key;
}
private Node higher(Node cur, int key) {
if (cur == null)
return null;
if (cur.key <= key)
return higher(cur.right, key);
// cur.key > key
Node left = higher(cur.left, key);
return left == null ? cur : left;
}
// max < key
Integer lower(int key) {
Node max = lower(root, key);
return max == null ? null : max.key;
}
private Node lower(Node cur, int key) {
if (cur == null)
return null;
if (cur.key >= key)
return lower(cur.left, key);
// cur.key < key
Node right = lower(cur.right, key);
return right == null ? cur : right;
}
private class Node {
int key, height, size;
Node left, right;
Node(int key) {
this.key = key;
height = size = 1;
left = right = null;
}
private void preOrder(Node root , StringBuilder ans) {
if(root == null) return;
if(root.left != null) preOrder(root.left,ans );
ans.append(root.key+",");
if(root.right!=null) preOrder(root.right, ans);
}
public String toString() {
StringBuilder res = new StringBuilder();
preOrder(root, res);
return "[" + String.valueOf(res.substring(0 , res.length()-1)) +"]" ;
}
}
private int height(Node cur) {
return cur == null ? 0 : cur.height;
}
private int balanceFactor(Node cur) {
return height(cur.right) - height(cur.left);
}
private int size(Node cur) {
return cur == null ? 0 : cur.size;
}
// fixVertex
private void fixHeightAndSize(Node cur) {
cur.height = Math.max(height(cur.left), height(cur.right)) + 1;
cur.size = size(cur.left) + size(cur.right) + 1;
}
private Node rotateRight(Node cur) {
Node prevLeft = cur.left;
cur.left = prevLeft.right;
prevLeft.right = cur;
fixHeightAndSize(cur);
fixHeightAndSize(prevLeft);
return prevLeft;
}
private Node rotateLeft(Node cur) {
Node prevRight = cur.right;
cur.right = prevRight.left;
prevRight.left = cur;
fixHeightAndSize(cur);
fixHeightAndSize(prevRight);
return prevRight;
}
private Node balance(Node cur) {
fixHeightAndSize(cur);
if (balanceFactor(cur) == 2) {
if (balanceFactor(cur.right) < 0)
cur.right = rotateRight(cur.right);
return rotateLeft(cur);
}
if (balanceFactor(cur) == -2) {
if (balanceFactor(cur.left) > 0)
cur.left = rotateLeft(cur.left);
return rotateRight(cur);
}
return cur;
}
private boolean contains(Node cur, int key) {
if (cur == null)
return false;
else if (key < cur.key)
return contains(cur.left, key);
else if (key > cur.key)
return contains(cur.right, key);
else
return true;
}
private Node add(Node cur, int key) {
if (cur == null)
return new Node(key);
if (key < cur.key)
cur.left = add(cur.left, key);
else if (key > cur.key || multi)
cur.right = add(cur.right, key);
return balance(cur);
}
private Node findMin(Node cur) {
return cur.left != null ? findMin(cur.left) : cur;
}
private Node findMax(Node cur) {
return cur.right != null ? findMax(cur.right) : cur;
}
private Node removeMin(Node cur) {
if (cur.left == null)
return cur.right;
cur.left = removeMin(cur.left);
return balance(cur);
}
private Node removeMax(Node cur) {
if (cur.right == null)
return cur.left;
cur.right = removeMax(cur.right);
return balance(cur);
}
private Node remove(Node cur, int key) {
if (cur == null)
return null;
if (key < cur.key)
cur.left = remove(cur.left, key);
else if (key > cur.key)
cur.right = remove(cur.right, key);
else { // k == cur.key
Node prevLeft = cur.left;
Node prevRight = cur.right;
if (prevRight == null)
return prevLeft;
Node min = findMin(prevRight);
min.right = removeMin(prevRight);
min.left = prevLeft;
return balance(min);
}
return balance(cur);
}
int orderOfKey(int key) {
return orderOfKey(root, key);
}
// count < key
private int orderOfKey(Node cur, int key) {
if (cur == null)
return 0;
if (cur.key < key)
return size(cur.left) + 1 + orderOfKey(cur.right, key);
if(cur.key > key || (multi && cur.left!=null && cur.left.key == key))
return orderOfKey(cur.left, key);
// cur.key == key
return size(cur.left);
}
Integer findByOrder(int pos) {
return size(root) > pos ? findByOrder(root, pos) : null;
}
// get i-th
private int findByOrder(Node cur, int pos) {
if (size(cur.left) > pos)
return findByOrder(cur.left, pos);
if (size(cur.left) == pos)
return cur.key;
// size(cur.left) < pos
return findByOrder(cur.right, pos - 1 - size(cur.left));
}
public String toString() {
return String.valueOf(this.root) ;
}
}
public static void main(String[] args) throws IOException {
int t= in.nextInt();
StringBuilder st = new StringBuilder();
while (t>0){
int n=in.nextInt();
int ans=0;
for(int i=0;i<n;i++)ans=ans|in.nextInt();
st.append(ans).append('\n');
t--;
}
System.out.println(st);
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 4be1ee411cb3dd37271012dd94439507 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main extends PrintWriter {
static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));Main() { super(System.out); }public static void main(String[] args) throws IOException{ Main d1=new Main ();d1.main();d1.flush(); }
void main() throws IOException {
StringBuilder sb = new StringBuilder();
int t = 1;
t = i(s()[0]);
while (t-- > 0) {
int n = i(s()[0]);
int[] a=new int[n]; arri(a, n);
int ans = 0;
for(int i = 0 ; i < n; i++){
ans |= a[i];
}
sb.append(ans + "\n");
}
System.out.println(sb);
}
static String[] s() throws IOException { return s.readLine().trim().split("\\s+"); }
static int i(String ss) {return Integer.parseInt(ss); }
static long l(String ss) {return Long.parseLong(ss); }
public void arr(long[] a,int n) throws IOException {String[] s2=s();for(int i=0;i<n;i++){ a[i]=l(s2[i]); }}
public void arri(int[] a,int n) throws IOException {String[] s2=s();for(int i=0;i<n;i++){ a[i]=(i(s2[i])); }}
}class Pair{
int start, end;
public Pair(long a, long b){
this.start = (int)a ; this.end = (int)b;
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | c71f8400df188ac28a6852a1a5492714 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
import java.io.*;
public class solution{
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}
catch(IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
}
public static void main(String[] args){
FastReader in = new FastReader();
int t= in.nextInt();
while(t-->0){
int n = in.nextInt();
int ret = in.nextInt();
n--;
while(n-->0){
ret|=in.nextInt();
}
System.out.println(ret);
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 306c8f16ad391c18fa398382e87dffd8 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
import java.io.*;
public class HelloWorld{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int temp=0;
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
temp=temp|a[i];
}
System.out.println(temp);
}
}}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 5b6bfc78f9fc4df536c4d32eca3218fc | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class R772A {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(System.out);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0)
{
StringTokenizer st= new StringTokenizer(br.readLine());
int n= Integer.parseInt(st.nextToken());
st= new StringTokenizer(br.readLine());
int sum=0;
for(int i=0; i<n; i++)
sum|=Integer.parseInt(st.nextToken());
pw.println(sum);
pw.flush();
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 11 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 110fa40548a547befab9be4eef2ff4a1 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | //package com.tdorosz._1635;
import java.util.Scanner;
public class MinOrSum {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int testCases = scanner.nextInt();
for (int i = 0; i < testCases; i++) {
executeTestCase(scanner);
}
}
private static void executeTestCase(Scanner scanner) {
int numbersCount = scanner.nextInt();
int result = 0;
for (int i = 0; i < numbersCount; i++) {
int number = scanner.nextInt();
result |= number;
}
System.out.println(result);
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 17 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 8d52845c55262bda3e5ff7c97eec0343 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | //package com.tdorosz._1635;
import java.util.Scanner;
public class MinOrSum {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int testCases = scanner.nextInt();
for (int i = 0; i < testCases; i++) {
executeTestCase(scanner);
}
}
private static void executeTestCase(Scanner scanner) {
int numbersCount = scanner.nextInt();
int result = 0;
for (int i = 0; i < numbersCount; i++) {
int number = scanner.nextInt();
int bitPosition = 0;
while (number > 0) {
if (number % 2 == 1) {
result = result | (1 << bitPosition);
}
bitPosition++;
number /= 2;
}
}
System.out.println(result);
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 17 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | c02f21b094b287aeb19c298bcaf41c15 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | //package cf;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.*;
import java.util.StringTokenizer;
public class cftt {
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
}
catch (IOException e) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void debug(int mat[][])
{
for(int i=0; i<mat.length; i++) {
System.out.print(mat[i][0]+" "+mat[i][1]);
System.out.println();
}
}
public static void main(String[] args) {
FastScanner r = new FastScanner();
PrintWriter out=new PrintWriter(System.out);
// System.out.println("input");
int tc=r.nextInt();
while(tc-->0)
{
int n=r.nextInt();
int x[]=new int[n];
int mask=0;
for(int i=0; i<n; i++) {
x[i]=r.nextInt();
mask|=x[i];
}
System.out.println(mask);
//out.close();
}
}
}
class pair{
int x;
int y;
int cost;
public pair(int x,int y,int cost) {
this.x=x;
this.y=y;
this.cost=cost;
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 17 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 1cc6a8d96f87b06be6f90bc71b99e7a9 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.Scanner;
public class MinOrSum {
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int cases = sc.nextInt();
for(int i = 0; i<cases;i++){
int size = sc.nextInt();
int sum = 0;
for(int j = 0; j<size;j++){
sum|=sc.nextInt();
}
System.out.println(sum);
}
}
}
| Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 17 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 4e8c346d9dce6d19992bf56097120714 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-- > 0){
int n = sc.nextInt();
int[] arr =new int[n];
int ans = 0;
for(int i = 0; i < n; i++){
arr[i] = sc.nextInt();
if(i == 0) ans = arr[i];
else{
ans |= arr[i];
}
}
System.out.println(ans);
}
sc.close();
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 17 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 973178d633a8e1efd1e22d0ff6cab228 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \leq i < j \leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times. | 256 megabytes | import java.util.*;
public class A_Min_Or_Sum{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t=s.nextInt();
for(int k=0;k<t;k++){
int n = s.nextInt();
int a = s.nextInt();
for(int i=1; i<n; i++)
{
a|=s.nextInt();
}
System.out.println(a);
}
}
} | Java | ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"] | 1 second | ["3\n31\n6\n7"] | NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations. | Java 17 | standard input | [
"bitmasks",
"greedy"
] | 7fca54a73076ddfeb30b921b9e341f26 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \leq n \leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots ,a_n$$$ $$$(0 \leq a_i < 2^{30})$$$. | 800 | For each test case, print one number in a line — the minimum possible sum of the array. | standard output | |
PASSED | 536e91cf9770eb28f94dde671acde912 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | //Utilities
import java.io.*;
import java.util.*;
public class a {
static int t;
static int n, m;
static int[] type, u, v;
static ArrayList<Edge>[] adj;
static ArrayList<Integer>[] posGraph;
static boolean[] vis, done;
static Stack<Integer> topo;
static char[] ori;
static int[] pos;
static boolean flag = false;
public static void main(String[] args) throws IOException {
t = 1;
outer : while (t-- > 0) {
n = in.iscan(); m = in.iscan();
adj = new ArrayList[n+1]; ori = new char[n+1]; pos = new int[n+1];
for (int i = 0; i <= n; i++) {
adj[i] = new ArrayList<Edge>();
}
type = new int[m]; u = new int[m]; v = new int[m];
for (int i = 0; i < m; i++) {
type[i] = in.iscan(); u[i] = in.iscan(); v[i] = in.iscan();
adj[u[i]].add(new Edge(v[i], type[i])); adj[v[i]].add(new Edge(u[i], type[i]));
}
// for (int i = 1; i <= n; i++) {
// out.println(adj[i]);
// }
// condition 1: adj graph must be bipartite (equivalent to having no odd length cycles)
// condition 2: the position graph has no cycles
// where a directed edge from u->v in the position graph means that the position of u must be less than v
// An answer exists iff both conditions 1 and 2 are satisfied
posGraph = new ArrayList[n+1];
for (int i = 0; i <= n; i++) {
posGraph[i] = new ArrayList<Integer>();
}
Arrays.fill(ori, '$');
for (int i = 1; i <= n; i++) {
if (ori[i] == '$') {
flag = false;
ori[i] = 'L';
dfsOddCycle(i);
if (flag) {
out.println("NO");
continue outer;
}
}
}
vis = new boolean[n+1]; done = new boolean[n+1];
topo = new Stack<Integer>();
for (int i = 1; i <= n; i++) {
if (!done[i]) {
flag = false;
checkPosCycle(i, -1);
if (flag) {
out.println("NO");
continue outer;
}
}
}
int p = 0;
while (!topo.isEmpty()) {
pos[topo.pop()] = p++;
}
out.println("YES");
for (int i = 1; i <= n; i++) {
out.println(ori[i] + " " + pos[i]);
}
}
out.close();
}
static void dfsOddCycle(int i) {
for (Edge u : adj[i]) {
if (ori[u.to] == '$') {
if (u.type == 1) {
if (ori[i] == 'L') {
ori[u.to] = 'R';
posGraph[i].add(u.to);
}
else { // ori[i] = 'R'
ori[u.to] = 'L';
posGraph[u.to].add(i);
}
}
else {
if (ori[i] == 'L') {
ori[u.to] = 'R';
posGraph[u.to].add(i);
}
else { // ori[i] = 'R'
ori[u.to] = 'L';
posGraph[i].add(u.to);
}
}
dfsOddCycle(u.to);
if (flag) {
return;
}
}
else {
if (ori[u.to] == ori[i]) {
flag = true;
return;
}
else {
if (u.type == 1) {
if (ori[i] == 'L') {
//ori[u.to] = 'R';
posGraph[i].add(u.to);
}
else { // ori[i] = 'R'
//ori[u.to] = 'L';
posGraph[u.to].add(i);
}
}
else {
if (ori[i] == 'L') {
//ori[u.to] = 'R';
posGraph[u.to].add(i);
}
else { // ori[i] = 'R'
//ori[u.to] = 'L';
posGraph[i].add(u.to);
}
}
}
}
}
}
static void checkPosCycle(int idx, int prev) {
vis[idx] = true;
for (int u : posGraph[idx]) {
if (u == prev || done[u]) continue;
if (vis[u]) {
flag = true;
return;
}
checkPosCycle(u, idx);
if (flag) {
return;
}
}
topo.push(idx);
done[idx] = true;
}
static class Edge {
int to, type;
Edge(int to, int type){
this.to = to;
this.type = type;
}
public String toString() {
return "(" + to + ", " + type + ")";
}
public int hashCode() { return Objects.hash(to, type); }
public boolean equals(Object o) {
Edge e = (Edge)o;
return to == e.to && type == e.type;
}
}
static INPUT in = new INPUT(System.in);
static PrintWriter out = new PrintWriter(System.out);
private static class INPUT {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public INPUT (InputStream stream) {
this.stream = stream;
}
public INPUT (String file) throws IOException {
this.stream = new FileInputStream (file);
}
public int cscan () throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read (buf);
}
if (numChars == -1)
return numChars;
return buf[curChar++];
}
public int iscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
int res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public String sscan () throws IOException {
int c = cscan ();
while (space (c))
c = cscan ();
StringBuilder res = new StringBuilder ();
do {
res.appendCodePoint (c);
c = cscan ();
}
while (!space (c));
return res.toString ();
}
public double dscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
double res = 0;
while (!space (c) && c != '.') {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
res *= 10;
res += c - '0';
c = cscan ();
}
if (c == '.') {
c = cscan ();
double m = 1;
while (!space (c)) {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
m /= 10;
res += (c - '0') * m;
c = cscan ();
}
}
return res * sgn;
}
public long lscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
long res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public boolean space (int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
public static class UTILITIES {
static final double EPS = 10e-6;
public static void sort(int[] a, boolean increasing) {
ArrayList<Integer> arr = new ArrayList<Integer>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(long[] a, boolean increasing) {
ArrayList<Long> arr = new ArrayList<Long>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(double[] a, boolean increasing) {
ArrayList<Double> arr = new ArrayList<Double>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static int lower_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int upper_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static void updateMap(HashMap<Integer, Integer> map, int key, int v) {
if (!map.containsKey(key)) {
map.put(key, v);
}
else {
map.put(key, map.get(key) + v);
}
if (map.get(key) == 0) {
map.remove(key);
}
}
public static long gcd (long a, long b) {
return b == 0 ? a : gcd (b, a % b);
}
public static long lcm (long a, long b) {
return a * b / gcd (a, b);
}
public static long fast_pow_mod (long b, long x, int mod) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod;
return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod;
}
public static long fast_pow (long b, long x) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow (b * b, x / 2);
return b * fast_pow (b * b, x / 2);
}
public static long choose (long n, long k) {
k = Math.min (k, n - k);
long val = 1;
for (int i = 0; i < k; ++i)
val = val * (n - i) / (i + 1);
return val;
}
public static long permute (int n, int k) {
if (n < k) return 0;
long val = 1;
for (int i = 0; i < k; ++i)
val = (val * (n - i));
return val;
}
// start of permutation and lower/upper bound template
public static void nextPermutation(int[] nums) {
//find first decreasing digit
int mark = -1;
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break;
}
}
if (mark == -1) {
reverse(nums, 0, nums.length - 1);
return;
}
int idx = nums.length-1;
for (int i = nums.length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1, nums.length - 1);
}
public static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
public static void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
static int lower_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= cmp) high = mid;
else low = mid + 1;
}
return low;
}
static int upper_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > cmp) high = mid;
else low = mid + 1;
}
return low;
}
// end of permutation and lower/upper bound template
}
} | Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | be2ffa1beb89c90aebf9f23bd605ef97 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | /*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
static class Pair {
int type;
int firstCar;
int secondCar;
Pair(int type, int firstCar, int secondCar) {
this.type = type;
this.firstCar = firstCar;
this.secondCar = secondCar;
}
}
static int MAX = 200005;
static Map<Integer, List<Integer>> graph = new HashMap<>();
static boolean[] visited = new boolean[MAX];
static int[] color = new int[MAX];
static int[] inDegree = new int[MAX];
static int[] locations = new int[MAX];
static int n, m;
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = 1;
for (int t = 1; t <= test; t++) {
solve();
}
out.close();
}
private static void solve() {
n = sc.nextInt();
m = sc.nextInt();
initialize();
List<Pair> relationships = new ArrayList<>();
for (int i = 0; i < m; i++) {
int type = sc.nextInt();
int firstCar = sc.nextInt() - 1;
int secondCar = sc.nextInt() - 1;
relationships.add(new Pair(type, firstCar, secondCar));
graph.get(firstCar).add(secondCar);
graph.get(secondCar).add(firstCar);
}
if (!isBipartite()) {
out.println("NO");
return;
}
graph.clear();
initialize();
restoreTheOrientationsAndTheLocations(relationships);
}
private static void restoreTheOrientationsAndTheLocations(List<Pair> relationships) {
for (int i = 0; i < m; i++) {
int firstCar = relationships.get(i).firstCar;
int secondCar = relationships.get(i).secondCar;
if (relationships.get(i).type - 1 == color[firstCar]) {
graph.get(firstCar).add(secondCar);
inDegree[secondCar]++;
}else {
graph.get(secondCar).add(firstCar);
inDegree[firstCar]++;
}
}
if (topologicalOrdering() == -1) {
out.println("NO");
return;
}
out.println("YES");
for (int i = 0; i < n; i++) {
out.println((color[i] == 0 ? 'L' : 'R') + " " + locations[i]);
}
}
private static int topologicalOrdering() {
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < n; i++) {
if (inDegree[i] == 0) {
q.add(i);
}
}
int currLocation = 0;
while (!q.isEmpty()) {
int currCar = q.poll();
locations[currCar] = currLocation++;
for (int adjacentCar : graph.get(currCar)) {
if (--inDegree[adjacentCar] == 0) {
q.add(adjacentCar);
}
}
}
return currLocation == n ? n : -1;
}
private static boolean isBipartite() {
boolean ok = true;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
ok &= dfs(i, 0);
}
}
return ok;
}
private static boolean dfs(int currNode, int assignColor) {
visited[currNode] = true;
color[currNode] = assignColor;
for (int adjacentNode : graph.get(currNode)) {
if (visited[adjacentNode]) {
if (color[currNode] == color[adjacentNode]) {
return false;
}
}else {
dfs(adjacentNode, assignColor ^ 1);
}
}
return true;
}
private static void initialize() {
for (int i = 0; i < n; i++) {
graph.put(i, new ArrayList<>());
}
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.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 lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | ddbb4745d5f333c4a1ce9112ce3fd69a | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | // package c1635;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Random;
import java.util.StringTokenizer;
//
// Codeforces Round #772 (Div. 2) 2022-02-20 06:35
// E. Cars
// https://codeforces.com/contest/1635/problem/E
// time limit per test 2 seconds; memory limit per test 512 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// There are n cars on a coordinate axis OX. Each car is located at an integer point initially and
// no two cars are located at the same point. Also, each car is oriented either left or right, and
// they can move at any constant positive speed in that direction at any moment.
//
// More formally, we can describe the i-th car with a letter and an integer: its orientation ori_i
// and its location x_i. If ori_i = L, then x_i is decreasing at a constant rate with respect to
// time. Similarly, if ori_i = R, then x_i is increasing at a constant rate with respect to time.
//
// We call two cars if they never end up in the same point regardless of their speed. In other
// words, they won't share the same coordinate at any moment.
//
// We call two cars if they always end up in the same point regardless of their speed. In other
// words, they must share the same coordinate at some moment.
//
// Unfortunately, we lost all information about our cars, but we do remember m relationships. There
// are two types of relationships:
//
// 1 i j --i-th car and j-th car are .
//
// 2 i j --i-th car and j-th car are .
//
// Restore the orientations and the locations of the cars satisfying the relationships, or report
// that it is impossible. If there are multiple solutions, you can output any.
//
// Note that if two cars share the same coordinate, they will intersect, but at the same moment they
// will continue their movement in their directions.
//
// Input
//
// The first line contains two integers, n and m (2 <= n <= 2 * 10^5; 1 <= m <= min(2 * 10^5,
// \frac{n(n-1)}{2}) -- the number of cars and the number of restrictions respectively.
//
// Each of the next m lines contains three integers, type, i, and j (1 <= type <= 2; 1 <= i,j <= n;
// i!=j).
//
// If type = 1, i-th car and j-th car are . Otherwise, i-th car and j-th car are .
//
// It is guaranteed that for each pair of cars, there are at most 1 relationship between.
//
// Output
//
// In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore
// the orientations and the locations of the cars satisfying the relationships.
//
// If the answer is "YES", print n lines each containing a symbol and an integer: ori_i and x_i
// (ori_i \in {L, R}; -10^9 <= x_i <= 10^9) -- representing the information of the i-th car.
//
// If the orientation is left, then ori_i = L. Otherwise ori_i = R.
//
// x_i is the where the i-th car is located. Note that all x_i should be .
//
// We can prove that if there exists a solution, then there must be a solution satisfying the
// constraints on x_i.
//
// Example
/*
input:
4 4
1 1 2
1 2 3
2 3 4
2 4 1
output:
YES
R 0
L -3
R 5
L 6
input:
3 3
1 1 2
1 2 3
1 1 3
output:
NO
*/
//
public class C1635E {
static final int MOD = 998244353;
static final Random RAND = new Random();
static int[][] solve(int n, int[][] rels) {
// type 1 relationship (irrelevant) means either <--i j--> or <--j i-->
// type 2 relationship (destined) means either i--> <--j or j--> <--i
UnionFind uf = new UnionFind(n);
for (int[] v : rels) {
uf.union(v[1], v[2]);
}
// nbs[i] contains list of (j, rel) pairs
List<List<int[]>> nbs = new ArrayList<>();
for (int i = 0; i < n; i++) {
nbs.add(new ArrayList<>());
}
for (int[] v : rels) {
nbs.get(v[1]).add(new int[] {v[2], v[0]});
nbs.get(v[2]).add(new int[] {v[1], v[0]});
}
int[] dirs = new int[n];
// List of next nodes in ordered graph for each group
List<List<Integer>> children = new ArrayList<>();
List<List<Integer>> parents = new ArrayList<>();
for (int i = 0; i < n; i++) {
children.add(new ArrayList<>());
parents.add(new ArrayList<>());
}
int[] indegs = new int[n];
int[] ranks = new int[n];
Map<Integer, List<Integer>> groups = uf.getGroups();
for (List<Integer> cars : groups.values()) {
boolean ok = getDirs(cars, nbs, dirs);
if (!ok) {
return null;
}
ok = getOrders(cars, nbs, dirs, children, parents, indegs, ranks);
if (!ok) {
return null;
}
}
int[][] ans = new int[n][2];
int x = 0;
for (List<Integer> cars : groups.values()) {
for (int car : cars) {
ans[car][0] = dirs[car];
ans[car][1] = x++;
}
}
for (int i = 0; i < n; i++) {
if (dirs[i] == 0) {
ans[i][0] = 2;
ans[i][1] = x++;
}
}
return ans;
}
// Check whether relation within a group is compatible direction wise
static boolean getDirs(List<Integer> cars, List<List<int[]>> nbs, int[] dirs) {
Queue<Integer> q = new LinkedList<>();
int v0 = cars.get(0);
q.add(v0);
// WLOG assume this car is facing L
dirs[v0] = 1;
while (!q.isEmpty()) {
int v = q.poll();
myAssert(dirs[v] == 1 || dirs[v] == 2);
int dir = dirs[v] == 1 ? 2 : 1;
for (int[] e : nbs.get(v)) {
int w = e[0];
if (dirs[w] == dir) {
continue;
}
if (dirs[w] != 0) {
return false;
}
dirs[w] = dir;
q.add(w);
}
}
return true;
}
// Check whether relation within a group is compatible order wise, after already known their directions.
// Returns false if NOT compatible or true if compatible and sort cars into such order.
static boolean getOrders(List<Integer> cars, List<List<int[]>> nbs,
int[] dirs,
List<List<Integer>> children,
List<List<Integer>> parents,
int[] indegs, int[] ranks) {
int m = cars.size();
Collections.sort(cars);
for (int v : cars) {
for (int[] e : nbs.get(v)) {
int w = e[0];
if (w < v) {
continue;
}
myAssert(dirs[v] != dirs[w]);
if (e[1] == 1) {
// irrelevant
if (dirs[v] == 1) {
// <--v w-->
children.get(v).add(w);
parents.get(w).add(v);
indegs[w]++; // indicate has parent(s)
} else {
// <--w v-->
children.get(w).add(v);
parents.get(v).add(w);
indegs[v]++;
}
} else {
// destined
if (dirs[v] == 1) {
// w--> <--v
children.get(w).add(v);
parents.get(v).add(w);
indegs[v]++;
} else {
// v--> <--w
children.get(v).add(w);
parents.get(w).add(v);
indegs[w]++;
}
}
}
}
// Order will be compatible iff there is no loop
Queue<Integer> q = new LinkedList<>();
for (int v : cars) {
if (indegs[v] == 0) {
q.add(v);
}
}
if (q.isEmpty()) {
return false;
}
while (!q.isEmpty()) {
int v = q.poll();
ranks[v] = 1;
for (int w : parents.get(v)) {
ranks[v] = Math.max(ranks[v], ranks[w] + 1);
}
for (int w : children.get(v)) {
if (ranks[w] != 0 && ranks[w] < ranks[v]) {
// Detected loop involves w
return false;
}
indegs[w]--;
if (indegs[w] == 0) {
q.add(w);
}
}
}
// If there is any unvisited nodes by now (indicated by ranks[v] = 0), they are part of a loop
for (int v : cars) {
if (ranks[v] == 0) {
return false;
}
}
Collections.sort(cars, (x,y)->ranks[x] - ranks[y]);
return true;
}
static class UnionFind {
int n;
int m; // number of groups
int[] gids;
public UnionFind(int n) {
this.n = n;
this.m = n;
this.gids = new int[n];
for (int i = 0; i < n; i++) {
gids[i] = i;
}
}
public boolean union(int i, int j) {
int ri = find(i);
int rj = find(j);
if (ri != rj) {
int id = Math.min(ri, rj);
// Note that we must set gids[ri] instead of gids[i] etc
gids[ri] = id;
gids[rj] = id;
m--;
return true;
}
return false;
}
public int find(int i) {
while (i != gids[i]) {
gids[i] = gids[gids[i]];
i = gids[i];
}
return i;
}
public boolean isSingleGroup() {
return m == 1;
}
public int getNumGroups() {
return m;
}
public Map<Integer, List<Integer>> getGroups() {
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < n; i++) {
int id = find(i);
map.computeIfAbsent(id, v -> new ArrayList<>()).add(i);
}
return map;
}
}
static boolean test = false;
static void doTest() {
if (!test) {
return;
}
output(solve(2, new int[][] {{1,1,0}}));
long t0 = System.currentTimeMillis();
System.out.format("%d msec\n", System.currentTimeMillis() - t0);
System.exit(0);
}
public static void main(String[] args) {
doTest();
MyScanner in = new MyScanner();
int n = in.nextInt();
int m = in.nextInt();
int[][] rels = new int[m][3];
for (int i = 0; i < m; i++) {
rels[i][0] = in.nextInt();
rels[i][1] = in.nextInt() - 1;
rels[i][2] = in.nextInt() - 1;
}
int[][] ans = solve(n, rels);
output(ans);
}
static void output(int[][] a) {
if (a == null) {
System.out.println("NO");
return;
}
StringBuilder sb = new StringBuilder();
sb.append("YES\n");
for (int[] v : a) {
sb.append(v[0] == 1 ? 'L':'R');
sb.append(' ');
sb.append(v[1]);
sb.append('\n');
if (sb.length() > 500) {
System.out.print(sb.toString());
sb.setLength(0);
}
}
System.out.print(sb.toString());
}
static void myAssert(boolean cond) {
if (!cond) {
throw new RuntimeException("Unexpected");
}
}
static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
String cname = MethodHandles.lookup().lookupClass().getCanonicalName();
cname = cname.lastIndexOf('.') > 0 ? cname.substring(0, cname.lastIndexOf('.')) : cname;
final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in");
br = new BufferedReader(new InputStreamReader(fin.exists()
? new FileInputStream(fin) : System.in));
} catch (Exception e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
public String next() {
try {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 00792218e9d925c937f0bdeaf4fc32de | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | //package com.example.practice.codeforces.sc2200;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.StringTokenizer;
//E. Cars
public class Solution4 {
public static void main (String [] args) throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
// input file name goes above
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out")));
StringTokenizer st = new StringTokenizer(input.readLine());
final int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken());
int[][] rs = new int[m][];
for (int i=0;i<m;++i){
st = new StringTokenizer(input.readLine());
rs[i] = new int[]{Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())};
}
long[] res = calc(n, m, rs);
if (res==null){
System.out.println("NO");
}else{
System.out.println("YES");
for (int i=1;i<=n;++i){
long a = res[i];
if((a>>32) == 1){
System.out.println("L " + ((int)a));
}else{
System.out.println("R " + ((int)a));
}
}
}
//out.close(); // close the output file
}
private static long[] calc(final int n, final int m, int[][] rs) {
int[] cr = new int[n+1], rd = new int[n+1];
ArrayList<Integer>[] als = new ArrayList[n+1], als2 = new ArrayList[n+1];
for (int i=1;i<=n;++i){
als[i] = new ArrayList<>();
als2[i] = new ArrayList<>();
}
for (int[] e : rs){
als[e[1]].add(e[2]);
als[e[2]].add(e[1]);
}
for (int i=1;i<=n;++i){
if(cr[i]==0){
ArrayDeque<Integer> ll = new ArrayDeque<>();
ll.add(i);
cr[i] = 1;
while (!ll.isEmpty()){
int x = ll.poll();
for (int a : als[x]){
if(cr[a]==0){
cr[a] = cr[x]==1 ? 2 : 1;
ll.add(a);
}else if(cr[a]==cr[x]){
return null;
}
}
}
}
}
for (int[] e : rs){
if (e[0]==1){
if (cr[e[1]]==1){
als2[e[1]].add(e[2]);
rd[e[2]]++;
}else{
als2[e[2]].add(e[1]);
rd[e[1]]++;
}
}else{
if (cr[e[1]]==1){
als2[e[2]].add(e[1]);
rd[e[1]]++;
}else{
als2[e[1]].add(e[2]);
rd[e[2]]++;
}
}
}
long[] res = new long[n+1];
int cur = 1;
ArrayDeque<Integer> ll = new ArrayDeque<>();
for (int i=1;i<=n;++i){
if(rd[i]==0){
ll.add(i);
}
}
while (!ll.isEmpty()){
int x = ll.poll();
long t = cr[x];
res[x] = (t<<32) + cur++;
for (int a : als2[x]){
rd[a]--;
if (rd[a]==0){
ll.add(a);
}
}
}
return cur <= n ? null : res;
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 867a6dacc280f49e4edd05fa9eb6e118 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | //package com.example.practice.codeforces.sc2200;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
//E. Cars
public class Solution4 {
public static void main (String [] args) throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
// input file name goes above
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out")));
StringTokenizer st = new StringTokenizer(input.readLine());
final int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken());
int[][] rs = new int[m][];
for (int i=0;i<m;++i){
st = new StringTokenizer(input.readLine());
rs[i] = new int[]{Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())};
}
long[] res = calc(n, m, rs);
if (res==null){
System.out.println("NO");
}else{
System.out.println("YES");
for (int i=1;i<=n;++i){
long a = res[i];
if((a>>32) == 1){
System.out.println("L " + ((int)a));
}else{
System.out.println("R " + ((int)a));
}
}
}
//out.close(); // close the output file
}
private static long[] calc(final int n, final int m, int[][] rs) {
int[] cr = new int[n+1], rd = new int[n+1];
ArrayList<Integer>[] als = new ArrayList[n+1], als2 = new ArrayList[n+1];
for (int i=1;i<=n;++i){
als[i] = new ArrayList<>();
als2[i] = new ArrayList<>();
}
for (int[] e : rs){
als[e[1]].add(e[2]);
als[e[2]].add(e[1]);
}
for (int i=1;i<=n;++i){
if(cr[i]==0){
ArrayDeque<Integer> ll = new ArrayDeque<>();
ll.add(i);
cr[i] = 1;
while (!ll.isEmpty()){
int x = ll.poll();
for (int a : als[x]){
if(cr[a]==0){
cr[a] = cr[x]==1 ? 2 : 1;
ll.add(a);
}else if(cr[a]==cr[x]){
return null;
}
}
}
}
}
for (int[] e : rs){
if (e[0]==1){
if (cr[e[1]]==1){
als2[e[1]].add(e[2]);
rd[e[2]]++;
}else{
als2[e[2]].add(e[1]);
rd[e[1]]++;
}
}else{
if (cr[e[1]]==1){
als2[e[2]].add(e[1]);
rd[e[1]]++;
}else{
als2[e[1]].add(e[2]);
rd[e[2]]++;
}
}
}
long[] res = new long[n+1];
int cur = 1;
ArrayDeque<Integer> ll = new ArrayDeque<>();
for (int i=1;i<=n;++i){
if(rd[i]==0){
ll.add(i);
}
}
while (!ll.isEmpty()){
int x = ll.poll();
long t = cr[x];
res[x] = (t<<32) + cur++;
for (int a : als2[x]){
rd[a]--;
if (rd[a]==0){
ll.add(a);
}
}
}
return cur <= n ? null : res;
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 981d4c298acd2becbe6a4a864d8a6e62 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | //package com.example.practice.codeforces.sc2200;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
//E. Cars
public class Solution4 {
public static void main (String [] args) throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
// input file name goes above
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out")));
StringTokenizer st = new StringTokenizer(input.readLine());
final int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken());
int[][] rs = new int[m][];
for (int i=0;i<m;++i){
st = new StringTokenizer(input.readLine());
rs[i] = new int[]{Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())};
}
int[][] res = calc(n, m, rs);
if (res==null){
System.out.println("NO");
}else{
System.out.println("YES");
for (int i=1;i<=n;++i){
if (res[i][1]==1){
System.out.println("L " + res[i][0]);
}else{
System.out.println("R " + res[i][0]);
}
}
}
//out.close(); // close the output file
}
private static int[][] calc(final int n, final int m, int[][] rs) {
int[] cr = new int[n+1], rd = new int[n+1];
ArrayList<Integer>[] als = new ArrayList[n+1], als2 = new ArrayList[n+1];
for (int i=1;i<=n;++i){
als[i] = new ArrayList<>();
als2[i] = new ArrayList<>();
}
for (int[] e : rs){
als[e[1]].add(e[2]);
als[e[2]].add(e[1]);
}
for (int i=1;i<=n;++i){
if(cr[i]==0){
ArrayDeque<Integer> ll = new ArrayDeque<>();
ll.add(i);
cr[i] = 1;
while (!ll.isEmpty()){
int x = ll.poll();
for (int a : als[x]){
if(cr[a]==0){
cr[a] = cr[x]==1 ? 2 : 1;
ll.add(a);
}else if(cr[a]==cr[x]){
return null;
}
}
}
}
}
for (int[] e : rs){
if (e[0]==1){
if (cr[e[1]]==1){
als2[e[1]].add(e[2]);
rd[e[2]]++;
}else{
als2[e[2]].add(e[1]);
rd[e[1]]++;
}
}else{
if (cr[e[1]]==1){
als2[e[2]].add(e[1]);
rd[e[1]]++;
}else{
als2[e[1]].add(e[2]);
rd[e[2]]++;
}
}
}
int[][] res = new int[n+1][];
int cur = 1;
ArrayDeque<Integer> ll = new ArrayDeque<>();
for (int i=1;i<=n;++i){
if(rd[i]==0){
ll.add(i);
}
}
while (!ll.isEmpty()){
int x = ll.poll();
res[x] = new int[]{cur++, cr[x]};
for (int a : als2[x]){
rd[a]--;
if (rd[a]==0){
ll.add(a);
}
}
}
return cur <= n ? null : res;
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 064da735576a216e552c9d2af0cb869b | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.io.*;
import java.util.*;
public class Cars {
public static void main(String[] args) {
InputReader reader = new InputReader(System.in);
PrintWriter writer = new PrintWriter(System.out, false);
int N = reader.nextInt();
int M = reader.nextInt();
int[][] edges = new int[M][3];
for (int i = 0; i < M; i++) {
int type = reader.nextInt();
int u = reader.nextInt() - 1;
int v = reader.nextInt() - 1;
edges[i] = new int[]{type, u, v};
}
List<List<Integer>> G = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
G.add(new ArrayList<>());
}
for (int[] edge : edges) {
G.get(edge[1]).add(edge[2]);
G.get(edge[2]).add(edge[1]);
}
int[] color = new int[N];
Arrays.fill(color, -1);
for (int i = 0; i < N; i++) {
if (color[i] != -1) continue;
color[i] = 0;
Queue<Integer> queue = new LinkedList<>();
queue.add(i);
while (!queue.isEmpty()) {
int u = queue.remove();
for (int v : G.get(u)) {
if (color[v] == -1) {
color[v] = color[u] ^ 1;
queue.add(v);
} else if (color[v] == color[u]) {
writer.println("NO");
writer.close();
System.exit(0);
}
}
}
}
G = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
G.add(new ArrayList<>());
}
for (int[] edge : edges) {
int type = edge[0];
int u = edge[1];
int v = edge[2];
if (color[u] == 1) {
int tmp = u;
u = v;
v = tmp;
}
if (type == 1) {
G.get(u).add(v);
} else {
G.get(v).add(u);
}
}
int[] inDegree = new int[N];
for (int i = 0; i < N; i++) {
for (int j : G.get(i)) {
inDegree[j]++;
}
}
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < N; i++) {
if (inDegree[i] == 0) {
queue.add(i);
}
}
int[] top = new int[N];
int order = 0;
while (!queue.isEmpty()) {
int u = queue.remove();
top[u] = order++;
for (int v : G.get(u)) {
if (--inDegree[v] == 0) {
queue.add(v);
}
}
}
if (order != N) {
writer.println("NO");
writer.close();
System.exit(0);
}
writer.println("YES");
for (int i = 0; i < N; i++) {
writer.println((color[i] == 0 ? 'L' : 'R') + " " + top[i]);
}
writer.close();
System.exit(0);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 66e9164abe77c019512c6650f6cf5852 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.io.*;
import java.util.*;
public class E {
static HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
static int[] dir, pos;
static int[][] cons;
static boolean[] vis;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
dir = new int[n+1];
pos = new int[n+1];
cons = new int[n+1][2];
vis = new boolean[n+1];
for(int i = 1; i <= n; i++)
map.put(i, new ArrayList<Integer>());
for(int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int type = Integer.parseInt(st.nextToken());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
if(type == 1) {
cons[u][1]++;
cons[v][1]++;
}else {
cons[u][0]++;
cons[v][0]++;
}
map.get(u).add(type == 1 ? v : -v);
map.get(v).add(type == 1 ? u : -u);
}
boolean cont = true;
Queue<Integer> q = new LinkedList<Integer>();
for(int i = 1; i <= n; i++) {
if(dir[i] == 0)
cont = !dfs(i, 1) ? false : cont;
if(cons[i][0] == 0 || cons[i][1] == 0){
q.add(i);
vis[i] = true;
}
}
if(!cont)
pw.println("NO");
else {
int hit = 0;
ArrayList<Integer> left = new ArrayList<Integer>();
ArrayList<Integer> right = new ArrayList<Integer>();
while(!q.isEmpty()) {
int v = q.poll();
hit++;
if(cons[v][0] == 0) {
//depending on dir
if(dir[v] == 1){
right.add(v);
pos[v] = n - right.size();
}else{
left.add(v);
pos[v] = left.size()-1;
}
}else {
//depending on dir
if(dir[v] == 1){
left.add(v);
pos[v] = left.size()-1;
}else{
right.add(v);
pos[v] = n - right.size();
}
}
for(Integer u : map.get(v)) {
if(vis[u < 0 ? -u : u])
continue;
if(u < 0)
cons[-u][0]--;
else
cons[u][1]--;
if(cons[u < 0 ? -u : u][0] == 0 || cons[u < 0 ? -u : u][1] == 0) {
q.add(u < 0 ? -u : u);
vis[u < 0 ? -u : u] = true;
}
}
}
if(hit != n)
pw.println("NO");
else {
pw.println("YES");
for(int i = 1; i <= n; i++)
pw.println((dir[i] == -1 ? 'L' : 'R') + " " + pos[i]);
}
}
pw.close();
}
static boolean dfs(int n, int d) {
boolean res = true;
dir[n] = d;
for(Integer v : map.get(n)) {
v = (v < 0) ? -v : v;
if(dir[v] == 0)
res = !dfs(v, -d) ? false : res;
else if(dir[v] == d)
return false;
}
return res;
}
} | Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 3dd27eea7a372b8bdd59782355718823 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.util.*;
import java.io.*;
public class E {
static IOHandler sc = new IOHandler();
static StringBuilder toPrint = new StringBuilder();
static char [] vals;
static boolean [] visited;
public static void main(String[] args) {
// TODO Auto-generated method stub
solve();
System.out.println(toPrint);
}
private static void solve() {
int n = sc.nextInt();
int m = sc.nextInt();
List<Integer> [] rgraph = new List[n + 1];
List<Integer> [] igraph = new List[n + 1];
List<Integer> [] cgraph = new List[n + 1];
visited = new boolean[n + 1];
vals = new char[n + 1];
Set<Integer> [] before = new Set[n + 1];
int [] parents = new int [n + 1];
for (int i = 1; i <= n; ++i) {
rgraph[i] = new ArrayList<>();
igraph[i] = new ArrayList<>();
cgraph[i] = new ArrayList<>();
before[i] = new HashSet<>();
}
List<Integer> [] curGraph;
int x,y;
for (int i = 0 ; i < m; ++i) {
curGraph = sc.nextInt() == 1 ? igraph : rgraph;
x = sc.nextInt();
y = sc.nextInt();
curGraph[x].add(y);
curGraph[y].add(x);
cgraph[x].add(y);
cgraph[y].add(x);
}
for (int i = 1; i <= n; ++i) {
if (visited[i] || cgraph[i].size() == 0) continue;
if (!bfs(i , 'R', cgraph)) {
print("NO");
return;
}
}
for (int i = 1; i <= n; ++i) {
for (int child : igraph[i]) {
if (child > i) continue;
if (vals[i] == 'R') {
before[child].add(i);
++parents[i];
}
else {
before[i].add(child);
++parents[child];
}
}
for (int child : rgraph[i]) {
if (child > i) continue;
if (vals[i] == 'L') {
before[child].add(i);
++parents[i];
}
else {
before[i].add(child);
++parents[child];
}
}
}
Queue<Integer> queue = new LinkedList<>();
for (int i = 1; i <= n; ++i) {
if (parents[i] == 0) {
queue.add(i);
}
}
int ordered = 0;
int [] map = new int [n + 1];
int current;
while (!queue.isEmpty()) {
current = queue.remove();
map[current] = ordered++;
for (int child : before[current]) {
parents[child]--;
if (parents[child] == 0)
queue.add(child);
}
}
if (ordered != n) {
print("NO");
return;
}
print("YES");
print("\n");
int idx = 0;
for (int i = 1; i <= n; ++i) {
print( (vals[i] == 'L' ? 'L' : 'R') + " " + map[i] );
if (i != n)
print("\n");
}
}
private static boolean bfs(int node, char val, List<Integer> [] graph) {
if (visited[node] && val != vals[node])
return false;
else if (visited[node])
return true;
visited[node] = true;
vals[node] = val;
Queue<Integer> queue = new LinkedList<>();
queue.add(node);
int current;
while (!queue.isEmpty()) {
int size = queue.size();
val = val == 'L' ? 'R' : 'L';
while (size-- > 0) {
current = queue.remove();
for (int child : graph[current]) {
if (visited[child]) {
if (vals[child] != val)
return false;
continue;
}
visited[child] = true;
vals[child] = val;
queue.add(child);
}
}
}
return true;
}
public static void print(long result) {
toPrint.append(result);
}
public static void print(String result) {
toPrint.append(result);
if (toPrint.length() > 50_000) {
System.out.println(toPrint);
toPrint = new StringBuilder();
}
}
public static void print(int [] result) {
for (int i = 0; i < result.length; ++i) {
toPrint.append(result[i]);
if (i != result.length - 1)
toPrint.append(" ");
}
}
private static class IOHandler {
BufferedReader br;
StringTokenizer st;
public IOHandler() {
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());
}
int [] readArray(int n) {
int [] res = new int [n];
for (int i = 0; i < n; ++i)
res[i] = nextInt();
return res;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 5027fe2390b71c2f7f197ff8d03d193a | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | /*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
static class Pair {
int type;
int firstCar;
int secondCar;
Pair(int type, int firstCar, int secondCar) {
this.type = type;
this.firstCar = firstCar;
this.secondCar = secondCar;
}
}
static int MAX = 200005;
static Map<Integer, List<Integer>> graph = new HashMap<>();
static boolean[] visited = new boolean[MAX];
static int[] color = new int[MAX];
static int[] inDegree = new int[MAX];
static int[] locations = new int[MAX];
static int n, m;
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = 1;
for (int t = 1; t <= test; t++) {
solve();
}
out.close();
}
private static void solve() {
n = sc.nextInt();
m = sc.nextInt();
initialize();
List<Pair> relationships = new ArrayList<>();
for (int i = 0; i < m; i++) {
int type = sc.nextInt();
int firstCar = sc.nextInt() - 1;
int secondCar = sc.nextInt() - 1;
relationships.add(new Pair(type, firstCar, secondCar));
graph.get(firstCar).add(secondCar);
graph.get(secondCar).add(firstCar);
}
if (!isBipartite()) {
out.println("NO");
return;
}
graph.clear();
initialize();
restoreTheOrientationsAndTheLocations(relationships);
}
private static void restoreTheOrientationsAndTheLocations(List<Pair> relationships) {
for (int i = 0; i < m; i++) {
int firstCar = relationships.get(i).firstCar;
int secondCar = relationships.get(i).secondCar;
if (relationships.get(i).type - 1 == color[firstCar]) {
graph.get(firstCar).add(secondCar);
inDegree[secondCar]++;
}else {
graph.get(secondCar).add(firstCar);
inDegree[firstCar]++;
}
}
if (topologicalOrdering() == -1) {
out.println("NO");
return;
}
out.println("YES");
for (int i = 0; i < n; i++) {
out.println((color[i] == 0 ? 'L' : 'R') + " " + locations[i]);
}
}
private static int topologicalOrdering() {
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < n; i++) {
if (inDegree[i] == 0) {
q.add(i);
}
}
int currLocation = 0;
while (!q.isEmpty()) {
int currCar = q.poll();
locations[currCar] = currLocation++;
for (int adjacentCar : graph.get(currCar)) {
if (--inDegree[adjacentCar] == 0) {
q.add(adjacentCar);
}
}
}
return currLocation == n ? n : -1;
}
private static boolean isBipartite() {
boolean ok = true;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
ok &= dfs(i, 0);
}
}
return ok;
}
private static boolean dfs(int currNode, int assignColor) {
visited[currNode] = true;
color[currNode] = assignColor;
for (int adjacentNode : graph.get(currNode)) {
if (visited[adjacentNode]) {
if (color[currNode] == color[adjacentNode]) {
return false;
}
}else {
dfs(adjacentNode, assignColor ^ 1);
}
}
return true;
}
private static void initialize() {
for (int i = 0; i < n; i++) {
graph.put(i, new ArrayList<>());
}
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.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 lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | e4cfcb3a2227b432e9e1548566bb4650 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | /*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
static class Pair {
int type;
int firstCar;
int secondCar;
Pair(int type, int firstCar, int secondCar) {
this.type = type;
this.firstCar = firstCar;
this.secondCar = secondCar;
}
}
static int MAX = 200005;
static Map<Integer, List<Integer>> graph = new HashMap<>();
static boolean[] visited = new boolean[MAX];
static int[] color = new int[MAX];
static int[] inDegree = new int[MAX];
static int[] locations = new int[MAX];
static boolean ok;
static int n, m;
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = 1;
for (int t = 1; t <= test; t++) {
solve();
}
out.close();
}
private static void solve() {
n = sc.nextInt();
m = sc.nextInt();
initialize();
List<Pair> relationships = new ArrayList<>();
for (int i = 0; i < m; i++) {
int type = sc.nextInt();
int firstCar = sc.nextInt() - 1;
int secondCar = sc.nextInt() - 1;
relationships.add(new Pair(type, firstCar, secondCar));
graph.get(firstCar).add(secondCar);
graph.get(secondCar).add(firstCar);
}
if (!isBipartite()) {
out.println("NO");
return;
}
graph.clear();
initialize();
restoreTheOrientationsAndTheLocations(relationships);
}
private static void restoreTheOrientationsAndTheLocations(List<Pair> relationships) {
for (int i = 0; i < m; i++) {
int firstCar = relationships.get(i).firstCar;
int secondCar = relationships.get(i).secondCar;
if (relationships.get(i).type - 1 == color[firstCar]) {
graph.get(firstCar).add(secondCar);
inDegree[secondCar]++;
}else {
graph.get(secondCar).add(firstCar);
inDegree[firstCar]++;
}
}
if (topologicalOrdering() == -1) {
out.println("NO");
return;
}
out.println("YES");
for (int i = 0; i < n; i++) {
out.println((color[i] == 0 ? 'L' : 'R') + " " + locations[i]);
}
}
private static int topologicalOrdering() {
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < n; i++) {
if (inDegree[i] == 0) {
q.add(i);
}
}
int currLocation = 0;
while (!q.isEmpty()) {
int currCar = q.poll();
locations[currCar] = currLocation++;
for (int adjacentCar : graph.get(currCar)) {
if (--inDegree[adjacentCar] == 0) {
q.add(adjacentCar);
}
}
}
return currLocation == n ? n : -1;
}
private static boolean isBipartite() {
ok = true;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
dfs(i, 0);
if (!ok) {
return false;
}
}
}
return true;
}
private static void dfs(int currNode, int assignColor) {
visited[currNode] = true;
color[currNode] = assignColor;
for (int adjacentNode : graph.get(currNode)) {
if (visited[adjacentNode]) {
if (color[currNode] == color[adjacentNode]) {
ok = false;
}
}else {
dfs(adjacentNode, assignColor ^ 1);
}
}
}
private static void initialize() {
for (int i = 0; i < n; i++) {
graph.put(i, new ArrayList<>());
}
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.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 lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 85faab0c35a7a65ff679fb5c2c11d82f | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
import static java.lang.Math.max;
public class Main {
static final int MOD = (int) (1e9 + 7);
static Scanner sc;
static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
static void dfs(int u, int dir) {
directions[u] = dir;
int undir;
if (dir == 0) undir = 1;
else undir = 0;
for (Integer v : type1[u]) {
if (directions[u] == directions[v]) ok = false;
if (directions[v] == -1) {
dfs(v, undir);
}
}
for (Integer v : type2[u]) {
if (directions[u] == directions[v]) ok = false;
if (directions[v] == -1) {
dfs(v, undir);
}
}
}
static boolean ok = true;
static int n;
static ArrayList<Integer>[] type1;
static ArrayList<Integer>[] type2;
static Integer[] directions;
static void solve() {
int n = sc.nextInt();
int m = sc.nextInt();
type1 = new ArrayList[n];
type2 = new ArrayList[n];
directions = new Integer[n];
for (int i = 0; i < n; i++) {
type1[i] = new ArrayList<>();
type2[i] = new ArrayList<>();
directions[i] = -1;
}
for (int i = 0; i < m; i++) {
int type = sc.nextInt();
int u = sc.nextInt() - 1;
int v = sc.nextInt() - 1;
if (type == 1) {
type1[u].add(v);
type1[v].add(u);
}
else if (type == 2) {
type2[u].add(v);
type2[v].add(u);
}
}
for (int i = 0; i < n; i++) {
if (directions[i] == -1) dfs(i, 0);
}
if (!ok) {
pw.print("NO");
return;
}
Queue<Integer> q = new ArrayDeque<>();
Integer[] kol = new Integer[n];
for (int i = 0; i < n; i++) {
if (directions[i] == 0) {
kol[i] = type1[i].size();
if (kol[i] == 0)
q.add(i);
}
else if (directions[i] == 1) {
kol[i] = type2[i].size();
if (kol[i] == 0)
q.add(i);
}
}
int curr = 0;
Integer[] x = new Integer[n];
while (!q.isEmpty()) {
int u = q.poll();
x[u] = curr++;
if (directions[u] == 0) {
for (var i : type2[u]) {
kol[i]--;
if (kol[i] == 0) q.add(i);
}
}
else {
for (var i : type1[u]) {
kol[i]--;
if (kol[i] == 0) q.add(i);
}
}
}
for (int i = 0; i < n; i++) if (kol[i] != 0) { pw.print("NO"); return; }
pw.println("YES");
for (int i = 0; i < n; i++) {
if (directions[i] == 0) pw.print("R ");
else pw.print("L ");
pw.println(x[i]);
}
}
public static void main(String[] args) throws IOException {
// if (args.length >= 1 && args[0].equals("666EGOR777")) {
// sc = new FastReader("input.txt");
// } else {
sc = new Scanner(System.in);
// }
int tests = 1;
// tests = sc.nextInt();
for (int test = 0; test < tests; test++) {
solve();
pw.println();
}
pw.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String filePath) throws FileNotFoundException {
br = new BufferedReader(new FileReader(filePath));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | d8b3a86aa2b55c386bb7ac365ff8ec61 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
import static java.lang.Math.max;
public class Main {
static final int MOD = (int) (1e9 + 7);
static FastReader sc;
static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
static void dfs(int u, int dir) {
directions[u] = dir;
int undir;
if (dir == 0) undir = 1;
else undir = 0;
for (Integer v : type1[u]) {
if (directions[u] == directions[v]) ok = false;
if (directions[v] == -1) {
dfs(v, undir);
}
}
for (Integer v : type2[u]) {
if (directions[u] == directions[v]) ok = false;
if (directions[v] == -1) {
dfs(v, undir);
}
}
}
static boolean ok = true;
static int n;
static ArrayList<Integer>[] type1;
static ArrayList<Integer>[] type2;
static Integer[] directions;
static void solve() {
int n = sc.nextInt();
int m = sc.nextInt();
type1 = new ArrayList[n];
type2 = new ArrayList[n];
directions = new Integer[n];
for (int i = 0; i < n; i++) {
type1[i] = new ArrayList<>();
type2[i] = new ArrayList<>();
directions[i] = -1;
}
for (int i = 0; i < m; i++) {
int type = sc.nextInt();
int u = sc.nextInt() - 1;
int v = sc.nextInt() - 1;
if (type == 1) {
type1[u].add(v);
type1[v].add(u);
}
else if (type == 2) {
type2[u].add(v);
type2[v].add(u);
}
}
for (int i = 0; i < n; i++) {
if (directions[i] == -1) dfs(i, 0);
}
if (!ok) {
pw.print("NO");
return;
}
Queue<Integer> q = new ArrayDeque<>();
Integer[] kol = new Integer[n];
for (int i = 0; i < n; i++) {
if (directions[i] == 0) {
kol[i] = type1[i].size();
if (kol[i] == 0)
q.add(i);
}
else if (directions[i] == 1) {
kol[i] = type2[i].size();
if (kol[i] == 0)
q.add(i);
}
}
int curr = 0;
Integer[] x = new Integer[n];
while (!q.isEmpty()) {
int u = q.poll();
x[u] = curr++;
if (directions[u] == 0) {
for (var i : type2[u]) {
kol[i]--;
if (kol[i] == 0) q.add(i);
}
}
else {
for (var i : type1[u]) {
kol[i]--;
if (kol[i] == 0) q.add(i);
}
}
}
for (int i = 0; i < n; i++) if (kol[i] != 0) { pw.print("NO"); return; }
pw.println("YES");
for (int i = 0; i < n; i++) {
if (directions[i] == 0) pw.print("R ");
else pw.print("L ");
pw.println(x[i]);
}
}
public static void main(String[] args) throws IOException {
if (args.length >= 1 && args[0].equals("666EGOR777")) {
sc = new FastReader("input.txt");
} else {
sc = new FastReader();
}
int tests = 1;
// tests = sc.nextInt();
for (int test = 0; test < tests; test++) {
solve();
pw.println();
}
pw.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String filePath) throws FileNotFoundException {
br = new BufferedReader(new FileReader(filePath));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 235c5df141ffefb0985a52922d52b1cd | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Random;
import java.io.FileWriter;
import java.io.PrintWriter;
/*
Solution Created: 21:02:35 20/02/2022
Custom Competitive programming helper.
*/
public class Main {
static class TopologicalSort{
private boolean possible, run;
private int n;
private int[] inDeg, topSort;
private ArrayList<Integer> adj[];
public TopologicalSort(ArrayList<Integer> adj[]) {
run = possible = false;
inDeg = new int[n = adj.length];
topSort = new int[n];
this.adj = adj;
}
public boolean possible() {
if(!run) throw new RuntimeException("Call run first!");
return possible;
}
public int[] getTopSort() {
if(!run) throw new RuntimeException("Call run first!");
if(!possible) throw new RuntimeException("Graph is not bipartite!");
return topSort;
}
public void run() {
Arrays.fill(topSort, -1);
Queue<Integer> q = new LinkedList<>();
for(int i = 0; i<n; i++) {
for(int j : adj[i]) inDeg[j]++;
}
for(int i = 0; i<n; i++) if(inDeg[i] == 0) q.add(i);
int idx = 0;
while(!q.isEmpty()) {
int i = q.poll();
topSort[i] = idx++;
for(int j : adj[i]) {
if(--inDeg[j] == 0) q.add(j);
}
}
possible = idx==n;
run = true;
}
}
static class BipartiteColoring{
private boolean possible, run;
private int n;
private int[] col;
private ArrayList<Integer> adj[];
public BipartiteColoring(ArrayList<Integer> adj[]) {
run = possible = false;
col = new int[n = adj.length];
this.adj = adj;
}
public boolean possible() {
if(!run) throw new RuntimeException("Call run first!");
return possible;
}
public int[] getColoring() {
if(!run) throw new RuntimeException("Call run first!");
if(!possible) throw new RuntimeException("Graph is not bipartite!");
return col;
}
public void run() {
Arrays.fill(col, -1);
for(int i = 0; i<n; i++) dfs(i, 0);
possible = true;
for(int i = 0; i<n; i++) for(int j : adj[i]) if(col[i] == col[j]) possible = false;
run = true;
}
private void dfs(int i, int color) {
if(col[i] != -1) return;
col[i] = color;
for(int j : adj[i]) dfs(j,color^1);
}
}
public static void solve() {
int n = in.nextInt(), m = in.nextInt();
ArrayList<Integer> adj[] = new ArrayList[n], adj2[] = new ArrayList[n];
for(int i = 0; i<n; i++) {
adj[i] = new ArrayList<>();
adj2[i] = new ArrayList<>();
}
int[][] e = new int[m][3];
for(int i = 0; i<m; i++) {
e[i] = in.na(3);
e[i][1]--;
e[i][2]--;
adj[e[i][1]].add(e[i][2]);
adj[e[i][2]].add(e[i][1]);
}
BipartiteColoring bc = new BipartiteColoring(adj);
bc.run();
if(!bc.possible()) {
out.println("NO");
return;
}
int[] c = bc.getColoring();
//c[0] == left dir, c[1] == right dir
for(int[] edge : e) {
int w = edge[0], u = edge[1], v = edge[2];
if((w == 1 && c[u]==1) || (w == 2 && c[u]==0)) {
int tmp = u;
u = v;
v = tmp;
}
adj2[u].add(v);
}
TopologicalSort ts = new TopologicalSort(adj2);
ts.run();
if(!ts.possible()) {
out.println("NO");
return;
}
int[] top = ts.getTopSort();
out.println("YES");
for(int i = 0; i<n; i++) out.println((c[i]==0?"L":"R") +" "+top[i]);
}
public static void main(String[] args) {
in = new Reader();
out = new Writer();
int t = 1;
while(t-->0) solve();
out.exit();
}
static Reader in; static Writer out;
static class Reader {
static BufferedReader br;
static StringTokenizer st;
public Reader() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(String f){
try {
this.br = new BufferedReader(new FileReader(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
public double[] nd(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextDouble();
return a;
}
public long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
public char[] nca() {
return next().toCharArray();
}
public String[] ns(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) a[i] = next();
return a;
}
public int nextInt() {
ensureNext();
return Integer.parseInt(st.nextToken());
}
public double nextDouble() {
ensureNext();
return Double.parseDouble(st.nextToken());
}
public Long nextLong() {
ensureNext();
return Long.parseLong(st.nextToken());
}
public String next() {
ensureNext();
return st.nextToken();
}
public String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private void ensureNext() {
if (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static class Util{
private static Random random = new Random();
static long[] fact;
public static void initFactorial(int n, long mod) {
fact = new long[n+1];
fact[0] = 1;
for (int i = 1; i < n+1; i++) fact[i] = (fact[i - 1] * i) % mod;
}
public static long modInverse(long a, long MOD) {
long[] gcdE = gcdExtended(a, MOD);
if (gcdE[0] != 1) return -1; // Inverted doesn't exist
long x = gcdE[1];
return (x % MOD + MOD) % MOD;
}
public static long[] gcdExtended(long p, long q) {
if (q == 0) return new long[] { p, 1, 0 };
long[] vals = gcdExtended(q, p % q);
long tmp = vals[2];
vals[2] = vals[1] - (p / q) * vals[2];
vals[1] = tmp;
return vals;
}
public static long nCr(int n, int r, long MOD) {
if (r == 0) return 1;
return (fact[n] * modInverse(fact[r], MOD) % MOD * modInverse(fact[n - r], MOD) % MOD) % MOD;
}
public static long nCr(int n, int r) {
return (fact[n]/fact[r])/fact[n-r];
}
public static long nPr(int n, int r, long MOD) {
if (r == 0) return 1;
return (fact[n] * modInverse(fact[n - r], MOD) % MOD) % MOD;
}
public static long nPr(int n, int r) {
return fact[n]/fact[n-r];
}
public static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static boolean[] getSieve(int n) {
boolean[] isPrime = new boolean[n+1];
for (int i = 2; i <= n; i++) isPrime[i] = true;
for (int i = 2; i*i <= n; i++) if (isPrime[i])
for (int j = i; i*j <= n; j++) isPrime[i*j] = false;
return isPrime;
}
static long pow(long x, long pow, long mod){
long res = 1;
x = x % mod;
if (x == 0) return 0;
while (pow > 0){
if ((pow & 1) != 0) res = (res * x) % mod;
pow >>= 1;
x = (x * x) % mod;
}
return res;
}
public static int gcd(int a, int b) {
int tmp = 0;
while(b != 0) {
tmp = b;
b = a%b;
a = tmp;
}
return a;
}
public static long gcd(long a, long b) {
long tmp = 0;
while(b != 0) {
tmp = b;
b = a%b;
a = tmp;
}
return a;
}
public static int random(int min, int max) {
return random.nextInt(max-min+1)+min;
}
public static void dbg(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public static void reverse(int[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
int tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(int[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(long[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
long tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(long[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(float[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
float tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(float[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(double[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
double tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(double[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(char[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
char tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(char[] s) {
reverse(s, 0, s.length-1);
}
public static <T> void reverse(T[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
T tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static <T> void reverse(T[] s) {
reverse(s, 0, s.length-1);
}
public static void shuffle(int[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
int t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(long[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
long t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(float[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
float t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(double[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
double t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(char[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
char t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static <T> void shuffle(T[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
T t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void sortArray(int[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(long[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(float[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(double[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(char[] a) {
shuffle(a);
Arrays.sort(a);
}
public static <T extends Comparable<T>> void sortArray(T[] a) {
Arrays.sort(a);
}
public static int[][] rotate90(int[][] a){
int n = a.length, m = a[0].length;
int[][] ans = new int[m][n];
for(int i = 0; i<n; i++) for(int j = 0; j<m; j++) ans[m-j-1][i] = a[i][j];
return ans;
}
public static char[][] rotate90(char[][] a){
int n = a.length, m = a[0].length;
char[][] ans = new char[m][n];
for(int i = 0; i<n; i++) for(int j = 0; j<m; j++) ans[m-j-1][i] = a[i][j];
return ans;
}
}
static class Writer {
private PrintWriter pw;
public Writer(){
pw = new PrintWriter(System.out);
}
public Writer(String f){
try {
pw = new PrintWriter(new FileWriter(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public void yesNo(boolean condition) {
println(condition?"YES":"NO");
}
public void printArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void printArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void print(Object o) {
pw.print(o.toString());
}
public void println(Object o) {
pw.println(o.toString());
}
public void println() {
pw.println();
}
public void flush() {
pw.flush();
}
public void exit() {
pw.close();
}
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 00f07b0124107813aa32ffdf31b02442 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.util.*;
import java.io.*;
public class Solution
{
private static class FastIO {
private static class FastReader
{
BufferedReader br;
StringTokenizer st;
FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
private static PrintWriter out = new PrintWriter(System.out);
private static FastReader in = new FastReader();
public void print(String s) {out.print(s);}
public void println(String s) {out.println(s);}
public void println() {
println("");
}
public void print(int i) {out.print(i);}
public void print(long i) {out.print(i);}
public void print(char i) {out.print(i);}
public void print(double i) {out.print(i);}
public void println(int i) {out.println(i);}
public void println(long i) {out.println(i);}
public void println(char i) {out.println(i);}
public void println(double i) {out.println(i);}
public static int[] getIntArray(int n) {
int[] res = new int[n];
for(int i = 0; i < n; i++) {
res[i] = in.nextInt();
}
return res;
}
public static List<Integer> getIntList(int n) {
List<Integer> list = new ArrayList<>();
for(int i = 0; i < n; i++) {
list.add(in.nextInt());
}
return list;
}
public static void printKickstartCase(int i) {
out.print("Case #" + i + ": ");
}
public String next() {return in.next();}
int nextInt() { return in.nextInt(); }
char nextChar() {return in.next().charAt(0);}
long nextLong() { return in.nextLong(); }
double nextDouble() { return in.nextDouble(); }
String nextLine() {return in.nextLine();}
public void close() {
out.flush();
out.close();
}
}
private static final FastIO io = new FastIO();
private static int M = 1000000007;
private static class Pair {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
private static class Graph {
int n; //number of vertices
Map<Integer, List<Integer>> map = new HashMap<>(); //adjacency list
int[] indegree;
Graph(int n) {
this.n = n;
map = new HashMap<>();
for(int i = 0; i < n; i++) {
map.put(i, new ArrayList<>());
}
indegree = new int[n];
}
public void addEdge(int from, int to) {
map.get(from).add(to);
indegree[to] ++;
}
public List<Integer> getNeighbours(int i) {
return map.get(i);
}
public void readEdges(int m, int offset) {
FastIO io = new FastIO();
for(int i = 0; i < m; i++) {
int a = io.nextInt() - offset;
int b = io.nextInt() - offset;
addEdge(a, b);
}
}
public int[] topSort() {
Queue<Integer> q = new LinkedList<>();
int[] res = new int[n];
int ptr = 0;
for(int i = 0; i < n; i++) {
if(indegree[i] == 0) {
q.add(i);
}
}
while(!q.isEmpty()) {
int i = q.poll();
res[ptr ++] = i;
for(int neighbour : getNeighbours(i)) {
indegree[neighbour] --;
if(indegree[neighbour] == 0) {
q.add(neighbour);
}
}
}
if(ptr == n) {
return res;
}
return new int[0];
}
}
private static class UndirectedGraph extends Graph{
UndirectedGraph(int n) {
super(n);
}
private static class Pair {
int node_id;
int color;
public Pair(int node_id, int color) {
this.node_id = node_id;
this.color = color;
}
}
public boolean isBipartite() {
return twoColor().length > 0;
}
public int[] twoColor() {
int[] res = new int[n];
Arrays.fill(res, -1);
Queue<Pair> q = new LinkedList<>();
for(int i = 0; i < n; i++) {
if(res[i] != -1) {
continue;
}
res[i] = 0;
q.add(new Pair(i, 0));
while(!q.isEmpty()) {
Pair p = q.poll();
int node_id = p.node_id;
int color = p.color;
List<Integer> neighbours = getNeighbours(node_id);
for(int neighbour : neighbours) {
if(res[neighbour] == -1) {
q.add(new Pair(neighbour, color ^ 1));
res[neighbour] = color ^ 1;
}
else if(res[neighbour] == color) {
return new int[0];
}
}
}
}
return res;
}
@Override
public void addEdge(int x, int y) {
super.addEdge(x, y);
super.addEdge(y, x);
}
@Override
public void readEdges(int m, int offset) {
FastIO io = new FastIO();
for(int i = 0; i < m; i++) {
int a = io.nextInt() - offset;
int b = io.nextInt() - offset;
addEdge(a, b);
}
}
}
public static void main(String[] args)
{
int t = 1;
for(int z = 0; z < t; z ++) {
int n = io.nextInt();
int m = io.nextInt();
List<Pair> irrelevant = new ArrayList<>();
List<Pair> relevant = new ArrayList<>();
UndirectedGraph g1 = new UndirectedGraph(n);
for(int i = 0; i < m; i++) {
int type = io.nextInt();
int x = io.nextInt() - 1;
int y = io.nextInt() - 1;
g1.addEdge(x, y);
if(type == 1) {
irrelevant.add(new Pair(x, y));
}
else {
relevant.add(new Pair(x, y));
}
}
int[] color = g1.twoColor();
if(color.length == 0) {
io.println("NO");
continue;
}
Graph g2 = new Graph(n);
for(Pair p : irrelevant) {
int x = p.x;
int y = p.y;
int blue = color[x] == 0 ? x : y;
int red = color[x] == 0 ? y : x;
g2.addEdge(blue, red);
}
for(Pair p : relevant) {
int x = p.x;
int y = p.y;
int blue = color[x] == 0 ? x : y;
int red = color[x] == 0 ? y : x;
g2.addEdge(red, blue);
}
int[] top_sort_order = g2.topSort();
if(top_sort_order.length == 0) {
io.println("NO");
}
else {
int[] res = new int[n];
int x = 0;
io.println("YES");
for(int i : top_sort_order) {
res[i] = x;
x ++;
}
for(int i = 0; i < n; i++) {
String c = color[i] == 0 ? "L" : "R";
io.println(c + " " + res[i]);
}
}
}
io.close();
}
} | Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 140b450dd4c0fa067bf7596f8bd6d088 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | // coached by rainboy
import java.io.*;
import java.util.*;
public class CF1635E extends PrintWriter {
CF1635E() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1635E o = new CF1635E(); o.main(); o.flush();
}
int[] eo; int[][] ej, et;
void append(int i, int j, int t) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = Arrays.copyOf(ej[i], o << 1);
et[i] = Arrays.copyOf(et[i], o << 1);
}
ej[i][o] = j;
et[i][o] = t;
}
boolean[] visited;
int[] qu, dd, ii; int k;
char[] ori; int[] xx;
boolean dfs(int i, char lr) {
if (ori[i] != 0)
return ori[i] == lr;
ii[k++] = i;
ori[i] = lr;
lr = lr == 'L' ? 'R' : 'L';
for (int o = eo[i]; o-- > 0; ) {
int j = ej[i][o];
if (!dfs(j, lr))
return false;
}
return true;
}
int x;
boolean topo() {
int head = 0, cnt = 0;
for (int h = 0; h < k; h++) {
int i = ii[h];
for (int o = eo[i]; o-- > 0; ) {
int t = et[i][o];
if (ori[i] == 'R' && t == 1 || ori[i] == 'L' && t == 2)
dd[i]++;
}
if (dd[i] == 0)
qu[cnt++] = i;
}
while (cnt-- > 0) {
int i = qu[head++];
for (int o = eo[i]; o-- > 0; ) {
int j = ej[i][o];
int t = et[i][o];
if (ori[i] == 'R' && t == 2 || ori[i] == 'L' && t == 1)
if (--dd[j] == 0)
qu[head + cnt++] = j;
}
}
if (head != k)
return false;
for (int h = 0; h < k; h++) {
int i = qu[h];
xx[i] = x++;
}
return true;
}
void main() {
int n = sc.nextInt();
int m = sc.nextInt();
eo = new int[n]; ej = new int[n][2]; et = new int[n][2];
while (m-- > 0) {
int t = sc.nextInt();
int i = sc.nextInt() - 1;
int j = sc.nextInt() - 1;
append(i, j, t);
append(j, i, t);
}
qu = new int[n];
dd = new int[n];
ii = new int[n];
ori = new char[n]; xx = new int[n];
for (int i = 0; i < n; i++) {
k = 0;
if (ori[i] == 0)
if (!dfs(i, 'R') || !topo()) {
println("NO");
return;
}
}
println("YES");
for (int i = 0; i < n; i++)
println(ori[i] + " " + xx[i]);
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 30a14525753df792323859bac160ed95 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.io.*;
import java.util.*;
public class Cars {
//io
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(System.out);
static boolean debug = false;
//param
static int N;
static int M;
static Restriction[] restrictions;
//coloring
static ArrayList<Integer>[] adjList;
static int[] orientation;
static boolean impossible = false;
//top sort
static ArrayList<Integer>[] toRight;
static boolean[] visited;
static ArrayList<Integer> sort = new ArrayList<>();
public static void main(String[] args) throws IOException {
//parse input
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
restrictions = new Restriction[M];
for (int i=0;i<M;i++){
st = new StringTokenizer(br.readLine());
restrictions[i] = new Restriction(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
}
//part 1: assign coloring (if cant, NO) 1:left facing, 2:right facing
adjList = new ArrayList[N+1];
orientation = new int[N+1];
for (int i=1;i<=N;i++){
adjList[i] = new ArrayList<>();
}
for (Restriction r : restrictions){
adjList[r.u].add(r.v);
adjList[r.v].add(r.u);
}
for (int i=1;i<=N;i++){
if (orientation[i]==0){
orientation[i]=1;
dfs(i);
}
}
if (debug) System.out.println(Arrays.toString(orientation));
if (debug) System.out.println(Arrays.toString(adjList));
if (impossible) {
out.println("NO");
out.close();
return;
}
//part 2: create directed graph. arrow signifies the child car is to its right
toRight = new ArrayList[N+1];
for (int i=1;i<=N;i++){
toRight[i] = new ArrayList<>();
}
for (Restriction r : restrictions){
if ((orientation[r.u]==1&&r.type==1)||(orientation[r.u]==2&&r.type==2)){
toRight[r.u].add(r.v);
}
else {
toRight[r.v].add(r.u);
}
}
if (debug) System.out.println(Arrays.toString(toRight));
//part 3: top sort the directed graph. if its not DAG, print impossible
visited = new boolean[N+1];
for (int i=1;i<=N;i++){
topSort(i);
}
Collections.reverse(sort);
if (debug) System.out.println(sort);
int[] depth = new int[N+1];
for (int i=0;i<sort.size();i++){
depth[sort.get(i)]=i;
}
if (debug) System.out.println(Arrays.toString(depth));
for (int u=1;u<=N;u++){
for (int v : toRight[u]){
if (depth[u]>depth[v]) {
out.println("NO");
out.close();
return;
}
}
}
//turn in answer
out.println("YES");
for (int i=1;i<=N;i++){
if (orientation[i]==1) out.print("L ");
else out.print("R ");
out.print(depth[i]);
out.println();
}
out.close();
}
public static void topSort(int node){
if (visited[node]) return;
visited[node] = true;
for (int child : toRight[node]){
topSort(child);
}
sort.add(node);
}
public static void dfs(int node){
for (int child : adjList[node]){
if (orientation[child]!=0){
if (orientation[child]==orientation[node]) impossible=true;
}
else {
orientation[child]=3-orientation[node];
dfs(child);
}
}
}
private static class Restriction {
int type;
int u;
int v;
public Restriction(int t, int u, int v){
type=t;
this.u=u;
this.v=v;
}
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | b07608d8d7155a0a15cd6ddad856707a | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.io.*;
import java.util.*;
import java.util.function.Function;
import java.util.stream.IntStream;
/*
polyakoff
*/
public class Main {
static FastReader in;
static PrintWriter out;
static Random rand = new Random();
static final int oo = (int) 2e9 + 10;
static final long OO = (long) 2e18 + 10;
static final int MOD = (int) 1e9 + 7;
static ArrayList<To>[] g;
static int[] c;
static class To {
int v, type;
To(int v, int type) {
this.v = v;
this.type = type;
}
}
static class Pair {
int v, min;
Pair(int v, int min) {
this.v = v;
this.min = min;
}
}
static boolean bipartite;
static void dfs(int v) {
for (var to : g[v]) {
if (c[to.v] == 0) {
c[to.v] = 3 - c[v];
dfs(to.v);
} else if (c[to.v] == c[v])
bipartite = false;
}
}
static void solve() {
int n = in.nextInt();
int m = in.nextInt();
g = new ArrayList[n];
Arrays.setAll(g, i -> new ArrayList<>());
for (int i = 0; i < m; i++) {
int type = in.nextInt();
int v = in.nextInt() - 1;
int u = in.nextInt() - 1;
g[v].add(new To(u, type));
g[u].add(new To(v, type));
}
bipartite = true;
c = new int[n];
for (int v = 0; v < n; v++) {
if (c[v] == 0) {
c[v] = 1;
dfs(v);
}
}
if (!bipartite) {
out.println("NO");
return;
}
int[][] cnt = new int[n][2];
for (int v = 0; v < n; v++) {
for (var to : g[v]) {
cnt[v][to.type - 1]++;
}
}
PriorityQueue<Pair> pq = new PriorityQueue<>(Comparator.comparingInt(p -> p.min));
for (int v = 0; v < n; v++) {
pq.add(new Pair(v, Math.min(cnt[v][0], cnt[v][1])));
}
int[] x = new int[n];
int low = 1, high = n;
while (!pq.isEmpty()) {
Pair fi = pq.poll();
int v = fi.v, min = fi.min;
if (x[v] != 0)
continue;
if (min != 0) {
out.println("NO");
return;
}
if ((c[v] == 1 && cnt[v][0] == 0) || (c[v] == 2 && cnt[v][1] == 0))
x[v] = low++;
else
x[v] = high--;
for (var to : g[v]) {
if (x[to.v] != 0)
continue;
cnt[v][to.type - 1]--;
cnt[to.v][to.type - 1]--;
pq.add(new Pair(to.v, Math.min(cnt[to.v][0], cnt[to.v][1])));
}
}
out.println("YES");
for (int v = 0; v < n; v++) {
out.println((c[v] == 1 ? 'R' : 'L') + " " + x[v]);
}
}
public static void main(String[] args) {
in = new FastReader();
out = new PrintWriter(System.out);
int t = 1;
// t = in.nextInt();
while (t-- > 0) {
solve();
}
out.flush();
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader() {
this(System.in);
}
FastReader(String file) throws FileNotFoundException {
this(new FileInputStream(file));
}
FastReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
String nextLine() {
String line;
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 68246e540bc05266d3e8fadfad413338 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.io.*;
import java.util.*;
public class Solution {
static int M = 1_000_000_007;
static Random rng = new Random();
private static int[][] testCase(int n, int p, int[][] tij) {
List<Integer>[] adjList = new List[n], directedAdjList = new List[n];
List<Integer> order = new ArrayList<>();
int[][] ans = new int[n][2];
int[] visited = new int[n];
for (int i = 0; i < n; i++) {
adjList[i] = new ArrayList<>();
directedAdjList[i] = new ArrayList<>();
}
for (int[] edge : tij) {
adjList[edge[1] - 1].add(edge[2] - 1);
adjList[edge[2] - 1].add(edge[1] - 1);
}
for (int i = 0; i < n; i++) {
if (ans[i][0] == 0) {
if (!getDirections(adjList, i, 1, ans)) {
return null;
}
}
}
//L = -1, R = 1
for (int[] edge : tij) {
if (edge[0] == 1) { //irrelevant
if (ans[edge[1] - 1][0] == 1) {
directedAdjList[edge[1] - 1].add(edge[2] - 1);
} else {
directedAdjList[edge[2] - 1].add(edge[1] - 1);
}
} else {
if (ans[edge[1] - 1][0] == 1) {
directedAdjList[edge[2] - 1].add(edge[1] - 1);
} else {
directedAdjList[edge[1] - 1].add(edge[2] - 1);
}
}
}
Arrays.fill(visited, 0);
for (int i = 0; i < n; i++) {
if (!topoSort(directedAdjList, i, visited, order)) {
return null;
}
}
for (int i = 0; i < n; i++) {
ans[order.get(i)][1] = i;
}
return ans;
}
private static boolean topoSort(List<Integer>[] directedAdjList, int curr, int[] visited, List<Integer> order) {
if (visited[curr] == 2) {
return true;
} else if (visited[curr] == 1) {
return false;
} else {
visited[curr] = 1;
for (int v : directedAdjList[curr]) {
if (!topoSort(directedAdjList, v, visited, order)) {
return false;
}
}
visited[curr] = 2;
order.add(curr);
return true;
}
}
private static boolean getDirections(List<Integer>[] adjList, int curr, int direction, int[][] ans) {
ans[curr][0] = direction;
for (int v : adjList[curr]) {
if (ans[v][0] == 0) {
if (!getDirections(adjList, v, -direction, ans)) {
return false;
}
} else {
if (ans[v][0] == direction) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = 1; // Scanner has functions to read ints, longs, strings, chars, etc.
//in.nextLine();
for (int tt = 1; tt <= t; ++tt) {
int n = in.nextInt(), m = in.nextInt();
int[][] tij = new int[m][3];
for (int i = 0; i < m; i++) {
tij[i] = in.readArray(3);
}
int[][] ans = testCase(n, m, tij);
if (ans == null) {
out.println("NO");
} else {
out.println("YES");
for (int[] an : ans) {
out.println(String.format("%s %d", an[0] == 1 ? 'R' : 'L', an[1]));
}
}
}
out.close();
}
private static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
boolean hasNext() {
return st.hasMoreTokens();
}
char[] readCharArray(int n) {
char[] arr = new char[n];
try {
br.read(arr);
br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return arr;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
private static void sort(int[] arr) {
int temp, idx;
for (int i = arr.length - 1; i > 0; i--) {
idx = rng.nextInt(i + 1);
temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr);
}
private static void sort(long[] arr) {
long temp;
int idx;
for (int i = arr.length - 1; i > 0; i--) {
idx = rng.nextInt(i + 1);
temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr);
}
private static <T> void sort(T[] arr) {
T temp;
int idx;
for (int i = arr.length - 1; i > 0; i--) {
idx = rng.nextInt(i + 1);
temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr);
}
private static <T> void sort(T[] arr, Comparator<? super T> cmp) {
T temp;
int idx;
for (int i = arr.length - 1; i > 0; i--) {
idx = rng.nextInt(i + 1);
temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr, cmp);
}
private static <T extends Comparable<? super T>> void sort(List<T> list) {
T temp;
int idx;
for (int i = list.size() - 1; i > 0; i--) {
idx = rng.nextInt(i + 1);
temp = list.get(i);
list.set(i, list.get(idx));
list.set(idx, temp);
}
Collections.sort(list);
}
private static <T> void sort(List<T> list, Comparator<? super T> cmp) {
T temp;
int idx;
for (int i = list.size() - 1; i > 0; i--) {
idx = rng.nextInt(i + 1);
temp = list.get(i);
list.set(i, list.get(idx));
list.set(idx, temp);
}
Collections.sort(list, cmp);
}
static class DSU {
int[] parent, rank;
public DSU(int n) {
parent = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
public int find(int i) {
if (parent[i] == i) {
return i;
} else {
int res = find(parent[i]);
parent[i] = res;
return res;
}
}
public boolean isSameSet(int i, int j) {
return find(i) == find(j);
}
public void union(int i, int j) {
int iParent = find(i), jParent = find(j);
if (iParent != jParent) {
if (rank[iParent] > rank[jParent]) {
parent[jParent] = iParent;
} else {
parent[iParent] = jParent;
if (rank[iParent] == rank[jParent]) {
rank[jParent]++;
}
}
}
}
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | a35826ec1b56707f175f89276357b5d2 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;
public class E {
public static FastScanner s = new FastScanner();
public static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
solve(s.nextInt(), s.nextInt());
out.close();
}
public static void solve(int c, int r) {
int[][] res = new int[r][3];
for (int i = 0; i < r; i++) res[i] = s.readArray(3);
LinkedList<int[]>[] nodes = new LinkedList[c];
for (int i = 0; i < c; i++) nodes[i] = new LinkedList<>();
for (int i = 0; i < r; i++) {
nodes[res[i][1]].add(new int[]{res[i][2], res[i][0] + 1});
nodes[res[i][2]].add(new int[]{res[i][1], res[i][0] + 1});
}
int[] ori = new int[c];
LinkedList<Integer> updates = new LinkedList<>();
int t;
for (int i = 0; i < c; i++) {
if (ori[i] != 0) continue;
updates.add(i);
ori[i] = 1;
while (updates.size() != 0) {
t = updates.pollLast();
for (int[] e: nodes[t]) {
if (ori[e[0]] == 0) {
ori[e[0]] = 3 ^ ori[t];
updates.add(e[0]);
} else {
if ((ori[e[0]] ^ ori[t]) != 3) {
out.println("NO");
return;
}
}
}
}
}
for (int i = 0; i < c; i++) {
for (int[] e: nodes[i]) {
e[1] = (e[1] ^ ori[i]) - 1;
}
}
TreeSet<int[]>[] setNodes = new TreeSet[c];
for (int i = 0; i < c; i++) {
setNodes[i] = new TreeSet<>((x, y) -> x[1] == y[1] ? y[0] - x[0] : y[1] - x[1]);
for (int[] e: nodes[i]) {
setNodes[i].add(e);
}
}
LinkedList<Integer> order = new LinkedList<>();
LinkedList<Integer> upd = new LinkedList<>();
boolean[] done = new boolean[c];
for (int i = 0; i < c; i++) upd.add(i);
while (upd.size() > 0) {
t = upd.pollLast();
if (done[t]) continue;
if (setNodes[t].size() == 0) {
order.add(t);
done[t] = true;
for (int[] e: setNodes[t]) {
upd.add(e[0]);
setNodes[e[0]].remove(new int[]{t, 1 - e[1]});
}
continue;
}
if (setNodes[t].first()[1] < 0) {
order.add(t);
done[t] = true;
for (int[] e: setNodes[t]) {
upd.add(e[0]);
setNodes[e[0]].remove(new int[]{t, 1 - e[1]});
}
}
}
TreeMap<Integer, String> sout = new TreeMap<>();
int i = 0;
if (order.size() != c) {
System.out.println("NO");
} else {
System.out.println("YES");
for (int z: order) {
sout.put(z, (ori[z] == 1 ? "L" : "R") + " " + i++);
}
for (Map.Entry<Integer, String> e: sout.entrySet()) out.println(e.getValue());
}
}
static class FastScanner {//copied from secondthread
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i<n; i++) a[i]=nextInt()-1;
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | a323470c1f06eee9cd7ff7baa21628cf | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author null
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Input in = new Input(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
static class TaskE {
List<Integer>[][] e;
char[] d;
boolean[] b;
List<Integer> order = new ArrayList<>();
private void solve(Input in, PrintWriter out) throws Exception {
int n = in.readInt();
int m = in.readInt();
e = new List[2][n];
for (int i = 0; i < n; i++) {
e[0][i] = new ArrayList<>();
e[1][i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int t = in.readInt() - 1;
int v = in.readInt() - 1;
int u = in.readInt() - 1;
e[t][u].add(v);
e[t][v].add(u);
}
d = new char[n];
Arrays.fill(d, 'E');
boolean ok = true;
for (int i = 0; i < n; i++) {
if (d[i] == 'E') {
d[i] = 'L';
if (!dfsDir(i)) {
ok = false;
break;
}
}
}
int[] x = new int[n];
if (ok) {
b = new boolean[n];
Arrays.fill(b, false);
for (int i = 0; i < n; i++) {
if (!b[i]) {
dfsTS(i);
}
}
for (int i = 0; i < n; i++) {
x[order.get(i)] = i;
}
for (int v = 0; v < n; v++) {
for (int u : e[0][v]) {
if ((x[v] < x[u]) && ((d[v] != 'L') || d[u] != 'R')) {
ok = false;
break;
}
if ((x[v] > x[u]) && ((d[v] != 'R') || d[u] != 'L')) {
ok = false;
break;
}
}
}
}
if (ok) {
StringBuilder buf = new StringBuilder("YES\n");
for (int i = 0; i < n; i++) {
buf.append(d[i]).append(' ').append(x[i]).append('\n');
}
out.println(buf.toString());
} else {
out.println("NO");
}
}
private boolean dfsDir(int v) {
for (int t = 0; t < 2; t++) {
for (int u : e[t][v]) {
if (d[v] == d[u]) {
return false;
}
if (d[u] == 'E') {
d[u] = (d[v] == 'L') ? 'R' : 'L';
dfsDir(u);
}
}
}
return true;
}
void dfsTS(int v) {
b[v] = true;
List<Integer> E = (d[v] == 'L') ? e[1][v] : e[0][v];
for (int u : E) {
if (!b[u]) {
dfsTS(u);
}
}
order.add(v);
}
public void solve(int testNumber, Input in, PrintWriter out) {
try {
solve(in, out);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
static class Input {
public final BufferedReader reader;
private String line = "";
private int pos = 0;
public Input(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
private boolean isSpace(char ch) {
return ch <= 32;
}
public String readWord() throws IOException {
skip();
int start = pos;
while (pos < line.length() && !isSpace(line.charAt(pos))) {
pos++;
}
return line.substring(start, pos);
}
public int readInt() throws IOException {
return Integer.parseInt(readWord());
}
private void skip() throws IOException {
while (true) {
if (pos >= line.length()) {
line = reader.readLine();
pos = 0;
}
while (pos < line.length() && isSpace(line.charAt(pos))) {
pos++;
}
if (pos < line.length()) {
return;
}
}
}
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 11 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | fa174f7c705c26fc4a5ec0af2d07bc20 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Map.Entry;
import java.util.Random;
import java.util.TreeSet;
public final class CF_772_D2_E
{
static boolean verb=true;
static void log(Object X){if (verb) System.err.println(X);}
static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}}
static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}}
static void logWln(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");}}
static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}}
static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}}
static void logWln(Object X){if (verb) System.err.print(X);}
static void info(Object o){ System.out.println(o);}
static void output(Object o){outputWln(""+o+"\n"); }
static void outputFlush(Object o){try {out.write(""+ o+"\n");out.flush();} catch (Exception e) {}}
static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}}
// Global vars
static BufferedWriter out;
static InputReader reader;
static void test() {
log("testing");
Random r=new Random();
int NTESTS=1000000;
int NMAX=22;
int VMAX=10;
for (int t=0;t<NTESTS;t++) {
}
log("testing done");
}
static class Composite implements Comparable<Composite>{
int v;
int idx;
public int compareTo(Composite X) {
if (v!=X.v)
return v-X.v;
return idx-X.idx;
}
public Composite(int v, int idx) {
this.v = v;
this.idx = idx;
}
}
static int[] bicolor(ArrayList<Integer>[] desc) {
log("bicolor");
int n=desc.length;
int[] stack=new int[n];
int[] color=new int[n];
int c=1;
int st=0;
for (int i=0;i<n;i++) {
if (color[i]==0) {
st=0;
stack[st++]=i;
color[i]=c++;
while (st>0) {
int u=stack[--st];
for (int v:desc[u]) {
if (color[v]==0) {
color[v]=-color[u];
stack[st++]=v;
} else {
if (color[v]!=-color[u]) {
log("issue with v:"+v+" from u:"+u+" "+color[v]+" "+color[u]);
return null;
}
}
}
}
}
}
log("done");
return color;
}
static int[] checkCycleAndTopologicalSort(ArrayList<Integer>[] desc,int[] up) {
log("check cycle");
int n=desc.length;
int[] h=new int[n];
int[] stack=new int[n];
int[] ptr=new int[n];
boolean[] visited=new boolean[n];
boolean[] onstack=new boolean[n];
int st=0;
for (int i=0;i<n;i++) {
if (up[i]==0) {
st=0;
stack[st++]=i;
visited[i]=true;
onstack[i]=true;
while (st>0) {
int u=stack[st-1];
if (ptr[u]<desc[u].size()) {
int v=desc[u].get(ptr[u]++);
if (!visited[v]) {
stack[st++]=v;
visited[v]=true;
onstack[v]=true;
} else {
if (onstack[v]) {
return null;
}
}
}
else {
for (int v:desc[u]) {
h[u]=Math.max(h[u],h[v]);
}
h[u]++;
st--;
onstack[u]=false;
}
}
}
}
for (int i=0;i<n;i++) {
if (!visited[i]) {
log("non visited:"+i);
return null;
}
}
log("done");
return h;
}
static long mod=1000000007;
static int IRRE=1;
static int DEST=2;
static void process() throws Exception {
out = new BufferedWriter(new OutputStreamWriter(System.out));
reader = new InputReader(System.in);
int n=reader.readInt();
int m=reader.readInt();
ArrayList<Integer>[] friends=new ArrayList[n];
ArrayList<Integer>[] coll=new ArrayList[n];
for (int u=0;u<n;u++) {
friends[u]=new ArrayList<Integer>();
coll[u]=new ArrayList<Integer>();
}
int[] x=new int[m];
int[] y=new int[m];
int[] tp=new int[m];
for (int i=0;i<m;i++) {
int ty=reader.readInt();
int u=reader.readInt()-1;
int v=reader.readInt()-1;
friends[u].add(v);
friends[v].add(u);
coll[u].add(ty);
coll[v].add(ty);
x[i]=u;
y[i]=v;
tp[i]=ty;
}
log("read");
boolean ok=true;
int[] color=bicolor(friends);
int[] h=null;
if (color==null)
ok=false;
else {
ArrayList<Integer>[] left=new ArrayList[n];
for (int u=0;u<n;u++)
left[u]=new ArrayList<Integer>();
int[] rgt=new int[n];
for (int i=0;i<m;i++) {
int u=x[i];
int v=y[i];
if (color[u]>0) {
if (tp[i]==IRRE) {
left[u].add(v);
rgt[v]++;
} else {
left[v].add(u);
rgt[u]++;
}
} else {
if (tp[i]==IRRE) {
left[v].add(u);
rgt[u]++;
} else {
left[u].add(v);
rgt[v]++;
}
}
}
h=checkCycleAndTopologicalSort(left,rgt);
if (h==null) {
ok=false;
}
}
if (!ok) {
output("NO");
} else {
output("YES");
Composite[] ar=new Composite[n];
for (int i=0;i<n;i++) {
ar[i]=new Composite(h[i],i);
}
Arrays.sort(ar);
int[] pos=new int[n];
for (int i=0;i<n;i++)
pos[ar[i].idx]=i;
for (int i=0;i<n;i++) {
String sens="L";
if (color[i]>0)
sens="R";
output(sens+" "+pos[i]);
}
}
try {
out.close();
} catch (Exception Ex) {
}
}
public static void main(String[] args) throws Exception {
process();
}
static final 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;
}
private int read() throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read(buf);
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public final String readString() 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 readString(int L) throws IOException {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder(L);
do {
res.append((char) c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public final int readInt() throws IOException {
int c = read();
boolean neg = false;
while (isSpaceChar(c)) {
c = read();
}
char d = (char) c;
// log("d:"+d);
if (d == '-') {
neg = true;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
// log("res:"+res);
if (neg)
return -res;
return res;
}
public final long readLong() throws IOException {
int c = read();
boolean neg = false;
while (isSpaceChar(c)) {
c = read();
}
char d = (char) c;
// log("d:"+d);
if (d == '-') {
neg = true;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
// log("res:"+res);
if (neg)
return -res;
return res;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
/*
1
10
1 0 0 0 0 1 0 0 1 1
1
12
1 0 0 1 0 0 1 0 0 0 0 1
1
12
1 0 0 1 0 0 1 0 0 0 0 1
3
12
1 0 0 1 0 0 1 0 0 0 0 1
10
1 0 0 0 0 1 0 0 1 1
12
1 0 0 1 0 0 1 0 0 0 0 1
1
11
1 0 0 0 0 0 0 0 1 1 1
19
1 1 0 1 1 1 1 1 1 0 1 0 0 0 0 1 0 1 1
19
1 0 0 1 1 0 0 1 1 0 0 0 1 1 0 0 0 1 0
19
1 0 0 0 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
1
20
1 1 0 0 0 1 0 1 1 1 0 1 1 1 1 1 0 0 1 0
*/ | Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 8 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | bbf8d7e7630a778f20daa4c0e9bcc9c8 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.util.*;
import java.io.*;
public class cf1635e {
static class Edge {
public int u, v, t;
public Edge(int x, int y, int z) {u = x; v = y; t = z;}
public int get(int a) {
if (a == u) return v;
else return u;
}
}
static boolean[] visited;
static boolean[] direction;
static boolean[] onStack;
static ArrayList<Integer> cyc;
static boolean works = true;
static ArrayList<ArrayList<Edge>> adj = new ArrayList<ArrayList<Edge>>();
static ArrayList<ArrayList<Integer>> adj2 = new ArrayList<ArrayList<Integer>>();
static ArrayList<Integer> ans = new ArrayList<>();
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
for (int i = 0; i < n; i++) adj.add(new ArrayList<Edge>());
visited = new boolean[n];
direction = new boolean[n];
Edge[] edges = new Edge[m];
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int t = Integer.parseInt(st.nextToken());
int u = Integer.parseInt(st.nextToken()) - 1;
int v = Integer.parseInt(st.nextToken()) - 1;
Edge e = new Edge(u, v, t);
edges[i] = e;
adj.get(u).add(e);
adj.get(v).add(e);
}
for (int i = 0; i < n; i++) {
if (!visited[i]) dfs(i, i, true);
}
if (!works) {
pw.println("NO");
pw.close();
return;
}
for (int i = 0; i < n; i++) adj2.add(new ArrayList<Integer>());
for (Edge e : edges) {
if (e.t == 1) {
if (!direction[e.u]) {
int temp = e.v;
e.v = e.u;
e.u = temp;
}
} else {
if (direction[e.u]) {
int temp = e.v;
e.v = e.u;
e.u = temp;
}
}
adj2.get(e.u).add(e.v);
}
visited = new boolean[n];
onStack = new boolean[n];
cyc = new ArrayList<Integer>();
for (int i = 0; i < n && cyc.size() == 0; i++) {
cycle(i);
}
if (cyc.size() != 0) {
pw.println("NO");
pw.close();
return;
}
visited = new boolean[n];
for (int i = 0; i < n; i++) {
if (!visited[i]) {
dfs2(i);
}
}
int[] pos = new int[n];
for (int i = 0; i < ans.size(); i++) {
pos[ans.get(i)] = i;
}
pw.println("YES");
for (int i = 0; i < n; i++) {
char d = direction[i] ? 'R' : 'L';
pw.println(d + " " + pos[i]);
}
pw.close();
}
static void dfs(int node, int parent, boolean d) {
if (visited[node] && direction[node] != d) works = false;
if (visited[node]) return;
visited[node] = true;
direction[node] = d;
for (Edge e : adj.get(node)) {
int v = e.get(node);
if (v != parent) {
dfs(v, node, !d);
}
}
}
public static boolean cycle(int n) {
visited[n] = onStack[n] = true;
for (int u : adj2.get(n)) {
if (onStack[u]) {
cyc.add(n);
onStack[n] = onStack[u] = false;
return true;
} else if (!visited[u]) {
if (cycle(u)) {
if (onStack[n]) {
cyc.add(n);
onStack[n] = false;
return true;
} else {
cyc.add(n);
return false;
}
}
if (!cyc.isEmpty())
return false;
}
}
onStack[n] = false;
return false;
}
static void dfs2(int node) {
if (visited[node]) {
return;
}
visited[node] = true;
for (int a : adj2.get(node)) {
if (!visited[a]) dfs2(a);
}
ans.add(node);
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 8 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 2e1d4908edabb8b2e45045bc5247fbd2 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {new Main().run();}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
void run() {
work();
out.flush();
}
long mod=998244353;
long gcd(long a,long b) {
return a==0?b:gcd(b%a,a);
}
ArrayList<int[]>[] graph;
int[] color;
void work() {
int n=ni(),m=ni();
graph=new ArrayList[n];
for(int i=0;i<n;i++)graph[i]=new ArrayList<>();
for(int i=0;i<m;i++){
int type=ni(),s=ni()-1,e=ni()-1;
graph[s].add(new int[]{e,type});
graph[e].add(new int[]{s,type});
}
color=new int[n];
for(int i=0;i<n;i++){
if(color[i]==0&&!dfs(i,1)){
out.println("NO");
return;
}
}
//1:L,2:R
int[] degree=new int[n];
for(int i=0;i<n;i++){
for(int[] g:graph[i]){
int nn=g[0];
int type=g[1];
if(color[i]==1){//L
if(type==2){
degree[i]++;
}
}else{//R
if(type==1){
degree[i]++;
}
}
}
}
LinkedList<Integer> q1=new LinkedList<>();
LinkedList<Integer> q2=new LinkedList<>();
for(int i=0;i<n;i++){
if(color[i]==1&°ree[i]==0){
q1.add(i);
}
if(color[i]==2&°ree[i]==0){
q2.add(i);
}
}
int cur=0;
int[][] ret=new int[n][2];
while(q1.size()>0||q2.size()>0){
while(q1.size()>0){
int node=q1.poll();
ret[node][0]=-1;
ret[node][1]=cur;
cur++;
for(int[] g:graph[node]){
int nn=g[0];
int type=g[1];
if(type==1){
degree[nn]--;
if(degree[nn]==0){
q2.add(nn);
}
}
}
}
while(q2.size()>0){
int node=q2.poll();
ret[node][0]=1;
ret[node][1]=cur;
cur++;
for(int[] g:graph[node]){
int nn=g[0];
int type=g[1];
if(type==2&&color[nn]==1){
degree[nn]--;
if(degree[nn]==0){
q1.add(nn);
}
}
}
}
}
if(cur<n){
out.println("NO");
return;
}
out.println("YES");
for(int[] r:ret){
out.println((r[0]==-1?"L":"R")+" "+r[1]);
}
}
private boolean dfs(int node,int col) {
if(color[node]!=0){
if(color[node]!=col){
return false;
}else{
return true;
}
}
color[node]=col;
for(int[] g:graph[node]){
int nn=g[0];
int type=g[1];
if(!dfs(nn,col==1?2:1)){
return false;
}
}
return true;
}
private ArrayList<Integer>[] ng(int n, int m) {
ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
int s=in.nextInt()-1,e=in.nextInt()-1;
graph[s].add(e);
graph[e].add(s);
}
return graph;
}
private ArrayList<long[]>[] ngw(int n, int m) {
ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong();
graph[(int)s].add(new long[] {e,w});
graph[(int)e].add(new long[] {s,w});
}
return graph;
}
private ArrayList<int[]>[] ngwi(int n, int m) {
ArrayList<int[]>[] graph=(ArrayList<int[]>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
int s=ni()-1,e=ni()-1,w=ni();
graph[s].add(new int[] {e,w});
graph[e].add(new int[] {s,w});
}
return graph;
}
private int ni() {
return in.nextInt();
}
private long nl() {
return in.nextLong();
}
private double nd() {
return in.nextDouble();
}
private String ns() {
return in.next();
}
private long[] na(int n) {
long[] A=new long[n];
for(int i=0;i<n;i++) {
A[i]=in.nextLong();
}
return A;
}
private int[] nia(int n) {
int[] A=new int[n];
for(int i=0;i<n;i++) {
A[i]=in.nextInt();
}
return A;
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
InputStreamReader input;//no buffer
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(boolean isBuffer)
{
if(!isBuffer){
input=new InputStreamReader(System.in);
}else{
br=new BufferedReader(new InputStreamReader(System.in));
}
}
public boolean hasNext(){
try{
String s=br.readLine();
if(s==null){
return false;
}
st=new StringTokenizer(s);
}catch(IOException e){
e.printStackTrace();
}
return true;
}
public String next()
{
if(input!=null){
try {
StringBuilder sb=new StringBuilder();
int ch=input.read();
while(ch=='\n'||ch=='\r'||ch==32){
ch=input.read();
}
while(ch!='\n'&&ch!='\r'&&ch!=32){
sb.append((char)ch);
ch=input.read();
}
return sb.toString();
}catch (Exception e){
e.printStackTrace();
}
}
while(st==null || !st.hasMoreElements())//回车,空行情况
{
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt()
{
return (int)nextLong();
}
public long nextLong() {
try {
if(input!=null){
long ret=0;
int b=input.read();
while(b<'0'||b>'9'){
b=input.read();
}
while(b>='0'&&b<='9'){
ret=ret*10+(b-'0');
b=input.read();
}
return ret;
}
}catch (Exception e){
e.printStackTrace();
}
return Long.parseLong(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
} | Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 8 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 0ea0eba8e70aaa4b8c579a53503a2f06 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.util.*;
import java.io.*;
public class E_Cars{
public static void solve(){
}
public static void main(String args[])throws IOException{
Reader sc=new Reader();
int n=sc.nextInt();
int m=sc.nextInt();
Graph g=new Graph(n);
for(int i=0;i<m;i++){
int type=sc.nextInt();
int u=sc.nextInt();
int v=sc.nextInt();
g.addEdge(u, v);
g.addEdgeToEdgeList(u, v, type);
}
if(!g.isBipartiteGraph()){
System.out.println("NO");
return;
}
g.clear();
for(Edge i:g.edge){
if(g.col[i.u]==1){
int temp=i.u;
i.u=i.v;
i.v=temp;
}
if(i.type==1){
g.addSingleEdge(i.u, i.v);
}else{
g.addSingleEdge(i.v, i.u);
}
}
if(!g.TopologicalSort()){
System.out.println("NO");
return ;
}
System.out.println("YES");
StringBuffer res=new StringBuffer();
for(int i=0;i<n;i++){
res.append((g.col[i]==0?"L":"R")+" "+g.top[i]);
res.append("\n");
}
System.out.println(res);
}
public static <K,V> Map<K,V> getLimitedSizedCache(int size){
/*Returns an unlimited sized map*/
if(size==0){
return (LinkedHashMap<K, V>) Collections.synchronizedMap(new LinkedHashMap<K, V>());
}
/*Returns the map with the limited size*/
Map<K, V> linkedHashMap = Collections.synchronizedMap(new LinkedHashMap<K, V>() {
protected boolean removeEldestEntry(Map.Entry<K, V> eldest)
{
return size() > size;
}
});
return linkedHashMap;
}
}
class BinarySearch<T extends Comparable<T>> {
T ele[];
int n;
public BinarySearch(T ele[],int n){
this.ele=(T[]) ele;
Arrays.sort(this.ele);
this.n=n;
}
public int lower_bound(T x){
//Return next smallest element greater than ewqual to the current element
int left=0;
int right=n-1;
while(left<=right){
int mid=left+(right-left)/2;
if(x.compareTo(ele[mid])==0)return mid;
if(x.compareTo(ele[mid])>0)left=mid+1;
else right=mid-1;
}
if(left ==n)return -1;
return left;
}
public int upper_bound(T x){
//Returns the highest element lss than equal to the current element
int left=0;
int right=n-1;
while(left<=right){
int mid=left+(right-left)/2;
if(x.compareTo(ele[mid])==0)return mid;
if(x.compareTo(ele[mid])>0)left=mid+1;
else right=mid-1;
}
return right;
}
}
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[1000000]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
class Data implements Comparable<Data>{
int num;
public Data(int num){
this.num=num;
}
public int compareTo(Data o){
return -o.num+num;
}
public String toString(){
return num+" ";
}
}
class Binary{
public String convertToBinaryString(long ele){
StringBuffer res=new StringBuffer();
while(ele>0){
if(ele%2==0)res.append(0+"");
else res.append(1+"");
ele=ele/2;
}
return res.reverse().toString();
}
}
class FenwickTree{
int bit[];
int size;
FenwickTree(int n){
this.size=n;
bit=new int[size];
}
public void modify(int index,int value){
while(index<size){
bit[index]+=value;
index=(index|(index+1));
}
}
public int get(int index){
int ans=0;
while(index>=0){
ans+=bit[index];
index=(index&(index+1))-1;
}
return ans;
}
}
class PAndC{
long c[][];
long mod;
public PAndC(int n,long mod){
c=new long[n+1][n+1];
this.mod=mod;
build(n);
}
public void build(int n){
for(int i=0;i<=n;i++){
c[i][0]=1;
c[i][i]=1;
for(int j=1;j<i;j++){
c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
}
}
}
}
class Trie{
int trie[][];
int revind[];
int root[];
int tind,n;
int sz[];
int drev[];
public Trie(){
trie=new int[1000000][2];
root=new int[600000];
sz=new int[1000000];
tind=0;
n=0;
revind=new int[1000000];
drev=new int[20];
}
public void add(int ele){
// System.out.println(root[n]+" ");
n++;
tind++;
revind[tind]=n;
root[n]=tind;
addimpl(root[n-1],root[n],ele);
}
public void addimpl(int prev_root,int cur_root,int ele){
for(int i=18;i>=0;i--){
int edge=(ele&(1<<i))>0?1:0;
trie[cur_root][1-edge]=trie[prev_root][1-edge];
sz[cur_root]=sz[trie[cur_root][1-edge]];
tind++;
drev[i]=cur_root;
revind[tind]=n;
trie[cur_root][edge]=tind;
cur_root=tind;
prev_root=trie[prev_root][edge];
}
sz[cur_root]+=sz[prev_root]+1;
for(int i=0;i<=18;i++){
sz[drev[i]]=sz[trie[drev[i]][0]]+sz[trie[drev[i]][1]];
}
}
public void findmaxxor(int l,int r,int x){
int ans=0;
int cur_root=root[r];
for(int i=18;i>=0;i--){
int edge=(x&(1<<i))>0?1:0;
if(revind[trie[cur_root][1-edge]]>=l){
cur_root=trie[cur_root][1-edge];
ans+=(1-edge)*(1<<i);
}else{
cur_root=trie[cur_root][edge];
ans+=(edge)*(1<<i);
}
}
System.out.println(ans);
}
public void findKthStatistic(int l,int r,int k){
//System.out.println("In 3");
int curr=root[r];
int curl=root[l-1];
int ans=0;
for(int i=18;i>=0;i--){
for(int j=0;j<2;j++){
if(sz[trie[curr][j]]-sz[trie[curl][j]]<k)
k-=sz[trie[curr][j]]-sz[trie[curl][j]];
else{
curr=trie[curr][j];
curl=trie[curl][j];
ans+=(j)*(1<<i);
break;
}
}
}
System.out.println(ans);
}
public void findSmallest(int l,int r,int x){
//System.out.println("In 4");
int curr=root[r];
int curl=root[l-1];
int countl=0,countr=0;
// System.out.println(curl+" "+curr);
for(int i=18;i>=0;i--){
int edge=(x&(1<<i))>0?1:0;
// System.out.println(trie[curl][edge]+" "+trie[curr][edge]+" "+sz[curl]+" "+sz[curr]);
if(edge==1){
countr+=sz[trie[curr][0]];
countl+=sz[trie[curl][0]];
}
curr=trie[curr][edge];
curl=trie[curl][edge];
}
countl+=sz[curl];
countr+=sz[curr];
System.out.println(countr-countl);
}
}
class Printer{
public <T> T printArray(T obj[] ,String details){
System.out.println(details);
for(int i=0;i<obj.length;i++)
System.out.print(obj[i]+" ");
System.out.println();
return obj[0];
}
public <T> void print(T obj,String details){
System.out.println(details+" "+obj);
}
}
class Edge{
int type;
int v,u;
public Edge(int type,int u,int v){
this.v=v;
this.u=u;
this.type=type;
}
public String toString(){
return u+" "+v+" "+type;
}
}
class Graph{
int nv; //0 indexing i.e vertices starts from 0 input as 1 indexed for add Edge
List<List<Integer>> adj;
List<Edge> edge;
boolean visited[];
Integer col[];
Printer print=new Printer();
int top[];
public Graph(int n){
adj=new ArrayList<>();
edge=new ArrayList<>();
this.nv=n;
// visited=new boolean[nv];
for(int i=0;i<n;i++)
adj.add(new ArrayList<Integer>());
}
public boolean TopologicalSort() {
top=new int[nv];
int cnt=0;
int indegree[]=new int[nv];
for(int i=0;i<nv;i++){
for(int j:adj.get(i)){
indegree[j]++;
}
}
Deque<Integer> q=new LinkedList<Integer>();
for(int i=0;i<nv;i++){
if(indegree[i]==0){
q.addLast(i);
}
}
while(q.size()>0){
int tele=q.pop();
top[tele]=cnt++;
for(int j:adj.get(tele)){
indegree[j]--;
if(indegree[j]==0)
q.addLast(j);
}
}
return cnt==nv;
}
public void addSingleEdge(int u, int v) {
adj.get(u).add(v);
}
public void addEdge(int u,int v){
u--;v--;
adj.get(v).add(u);
adj.get(u).add(v);
}
public void addEdgeToEdgeList(int u, int v, int type) {
u--;v--;
edge.add(new Edge(type,u,v));
}
public boolean isBipartiteGraph(){
col=new Integer[nv];
visited=new boolean[nv];
for(int i=0;i<nv;i++){
if(visited[i]==false){
col[i]=0;
dfs(i);
}
}
for(int i=0;i<nv;i++)
for(Integer j:adj.get(i))
if(col[i]==col[j])
return false;
return true;
}
public void dfs(int u){
visited[u]=true;
for(Integer i:adj.get(u)){
if(visited[i]==false)
{
col[i]=1^col[u];
dfs(i);
}
}
}
long maxweight;
public void clear(){
this.adj=new ArrayList<>();
for(int i=0;i<nv;i++)
adj.add(new ArrayList<Integer>());
}
// public void solve() {
// maxweight=(1l<<32)-1;
// dfsutil(31);
// System.out.println(maxweight);
// }
// public void dfsutil(int msb){
// if(msb<0)return;
// maxweight-=(1l<<msb);
// visited=new boolean[nv];
// //dfscheck(0,maxweight);
// for(int i=0;i<nv;i++)
// {
// if(visited[i]==false)
// {maxweight+=(1<<msb);
// break;}
// }
// dfsutil(msb-1);
// }
} | Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 8 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 944b910c2532d56bd47fba88911ea0ab | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.util.*;
import java.io.*;
public class Omar {
static PrintWriter pw;
static Scanner sc;
static ArrayList<Integer>[] graph;
static Boolean[] colors;
static boolean[] vis;
static boolean checkBipartite(int u, boolean color) {
boolean f = true;
vis[u] = true;
colors[u] = color;
for (int v : graph[u]) {
if (!vis[v]) {
f &= checkBipartite(v, !color);
} else {
if (color == colors[v])
return false;
}
}
return f;
}
public static void main(String[] args) throws IOException, InterruptedException {
pw = new PrintWriter(System.out);
sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
graph = new ArrayList[n];
for (int i = 0; i < n; i++)
graph[i] = new ArrayList<>();
vis = new boolean[n];
Queue<int[]> edges = new LinkedList<>();
for (int i = 0; i < m; i++) {
int type = sc.nextInt();
int u = sc.nextInt() - 1, v = sc.nextInt() - 1;
graph[u].add(v);
graph[v].add(u);
edges.add(new int[] { type, u, v });
}
colors = new Boolean[n];
boolean f=true;
for(int i=0;i<n;i++) {
if(colors[i]==null) {
f&=checkBipartite(i, false);
}
}
if(!f) {
System.out.println("NO");
return;
}
for (int i = 0; i < n; i++)
graph[i] = new ArrayList<>();
int[] depth = new int[n];
while (!edges.isEmpty()) {
int[] cur = edges.poll();
if (cur[0] == 2) {
if (!colors[cur[1]]) {
graph[cur[1]].add(cur[2]);
depth[cur[2]]++;
} else {
graph[cur[2]].add(cur[1]);
depth[cur[1]]++;
}
} else {
if (!colors[cur[1]]) {
graph[cur[2]].add(cur[1]);
depth[cur[1]]++;
} else {
graph[cur[1]].add(cur[2]);
depth[cur[2]]++;
}
}
}
Queue<Integer> q = new LinkedList<>();
LinkedList<Integer> ans = new LinkedList<>();
for (int i = 0; i < n; i++) {
if (depth[i] == 0) {
q.add(i);
ans.add(i);
}
}
while (!q.isEmpty()) {
int cur = q.poll();
for(int x:graph[cur]) {
if(--depth[x]==0) {
ans.add(x);
q.add(x);
}
}
}
if(ans.size()!=n) {
pw.println("NO");
}
else {
pw.println("YES");
int [] idx=new int[n];
for(int i=0;i<n;i++) {
idx[ans.pollFirst()]=i;
}
for(int i=0;i<n;i++) {
pw.print(colors[i]?"L ":"R ");
pw.println(idx[i]);
}
}
pw.flush();
}
static class pair implements Comparable<pair> {
// long x,y;
int x, y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws IOException {
br = new BufferedReader(new FileReader(file));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String readAllLines(BufferedReader reader) throws IOException {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
return content.toString();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
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[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
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 Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 8 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 7e4a364c3e23ca355473cb41b0e9b92e | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | /*
Setting up my ambitions
Check 'em one at a time, yeah, I made it
Now it's time for ignition
I'm the start, heat it up
They follow, burning up a chain reaction, oh-oh-oh
Go fire it up, oh, oh, no
Assemble the crowd now, now
Line 'em up on the ground
No leaving anyone behind
*/
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class x1635E
{
static ArrayDeque<Integer>[] edges;
static ArrayDeque<Integer>[] edgesTop;
public static void main(String hi[]) throws Exception
{
FastScanner infile = new FastScanner();
int N = infile.nextInt();
int M = infile.nextInt();
edges = new ArrayDeque[N+1];
edgesTop = new ArrayDeque[N+1];
for(int i=1; i <= N; i++)
{
edges[i] = new ArrayDeque<Integer>();
edgesTop[i] = new ArrayDeque<Integer>();
}
HashMap<Long, Integer> map = new HashMap<Long, Integer>();
while(M-->0)
{
int type = infile.nextInt();
int a = infile.nextInt();
int b = infile.nextInt();
edges[a].add(b); edges[b].add(a);
map.put(get(a, b), type);
}
if(!bipartite(N))
{
System.out.println("NO");
return;
}
char[] resD = new char[N+1];
int[] resX = new int[N+1];
boolean[] seen = new boolean[N+1];
seenTS = new boolean[N+1];
int boof = -1000000;
int[] tsLoc = new int[N+1];
for(int root=1; root <= N; root++)
{
if(seen[root])
continue;
seen[root] = true;
ArrayList<Integer> ls = new ArrayList<Integer>();
ArrayDeque<Integer> q = new ArrayDeque<Integer>();
q.add(root);
while(q.size() > 0)
{
int curr = q.poll();
ls.add(curr);//new Car(curr, color[curr]));
for(int next: edges[curr])
if(!seen[next])
{
seen[next] = true;
q.add(next);
}
}
for(int v: ls)
if(color[v] == 0) //left
{
for(int next: edges[v])
{
long key = get(v, next);
if(map.get(key) == 1)
edgesTop[v].add(next);
else
edgesTop[next].add(v);
}
}
//perform topsort
ArrayList<Car> order = new ArrayList<Car>();
for(int v: ls)
if(!seenTS[v])
dfs(v, order);
Collections.reverse(order);
for(int i=0; i < order.size(); i++)
tsLoc[order.get(i).id] = i;
//verify topsort exists
for(int v: ls)
if(color[v] == 0)
for(int next: edges[v])
{
long key = get(v, next);
if(map.get(key) == 1)
{
if(tsLoc[v] > tsLoc[next])
{
System.out.println("NO");
return;
}
}
else
{
if(tsLoc[v] < tsLoc[next])
{
System.out.println("NO");
return;
}
}
}
for(Car c: order)
{
if(c.dir == 0)
resD[c.id] = 'L';
else
resD[c.id] = 'R';
resX[c.id] = boof++;
}
}
StringBuilder sb = new StringBuilder("YES\n");
for(int c=1; c <= N; c++)
sb.append(resD[c]+" "+resX[c]).append("\n");
System.out.print(sb);
}
static final long CONS = 1L<<19;
public static long get(int a, int b)
{
return min(a, b)*CONS+max(a, b);
}
static int[] color;
public static boolean bipartite(int N)
{
color = new int[N+1];
Arrays.fill(color, -1);
for(int root=1; root <= N; root++)
if(color[root] == -1)
{
color[root] = 0;
ArrayDeque<Integer> q = new ArrayDeque<Integer>();
q.add(root);
while(q.size() > 0)
{
int curr = q.poll();
for(int next: edges[curr])
{
if(color[next] == -1)
{
color[next] = color[curr]^1;
q.add(next);
}
else if(color[next] == color[curr])
return false;
}
}
}
return true;
}
static boolean[] seenTS;
public static void dfs(int curr, ArrayList<Car> ls)
{
seenTS[curr] = true;
for(int next: edgesTop[curr])
if(!seenTS[next])
dfs(next, ls);
ls.add(new Car(curr, color[curr]));
}
}
class Car
{
public int id;
public int dir; //left=0, right=1
public Car(int a, int d)
{
id = a;
dir = d;
}
}
/*
Irrelevant: <-- -->
Destined: --> <--
if graph of relations is not bipartite, it's impossible
Otherwise, always possible?
Black nodes = left
White nodes = right
What order to assign coordinates (assuming we do increasing x_i)
Use top sort (make directed edges)
nope lol
sort with comparator(x,y):
//assume x.dir != y.dir and x.dir == 'L'
if(edge(x,y) exists):
if(edge.type == 1) //irrelevant
return -1;
else //destined
return 1;
else
return 0
*/
class FastScanner
{
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
} | Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 8 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | d43691510906902a14cc335bf22d2d88 | train_108.jsonl | 1645367700 | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships:$$$1$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are irrelevant.$$$2$$$ $$$i$$$ $$$j$$$ —$$$i$$$-th car and $$$j$$$-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. | 512 megabytes | import java.util.*;
import java.io.*;
public class E1635 {
static ArrayList<Integer>[] g1, g2;
static int[] color;
static int n;
public static boolean bipartCheck() {
color = new int[n];
for (int i = 0; i < n; i++) {
if (color[i] != 0)
continue;
Queue<Integer> q = new ArrayDeque<>();
q.add(i);
color[i] = 1;
while (!q.isEmpty()) {
int cur = q.poll();
for (int x : g1[cur]) {
if (color[x] == color[cur])
return false;
if (color[x] == 0) {
color[x] = 3 - color[cur];
q.add(x);
}
}
}
}
return true;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
n = sc.nextInt();
int m = sc.nextInt();
int[][] edges = new int[m][3];
g1 = new ArrayList[n];
for (int i = 0; i < n; i++) {
g1[i] = new ArrayList<>();
}
g2 = new ArrayList[n];
for (int i = 0; i < n; i++) {
g2[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < 3; j++) {
edges[i][j] = sc.nextInt();
}
edges[i][1]--;
edges[i][2]--;
g1[edges[i][1]].add(edges[i][2]);
g1[edges[i][2]].add(edges[i][1]);
}
boolean valid = bipartCheck();
if (!valid) {
pw.println("NO");
pw.close();
return;
}
int[] indeg = new int[n];
for (int i = 0; i < m; i++) {
if (edges[i][0] == 1) {
if (color[edges[i][1]] == 2) {
g2[edges[i][1]].add(edges[i][2]);
indeg[edges[i][2]]++;
} else {
g2[edges[i][2]].add(edges[i][1]);
indeg[edges[i][1]]++;
}
} else {
if (color[edges[i][1]] == 1) {
g2[edges[i][1]].add(edges[i][2]);
indeg[edges[i][2]]++;
} else {
g2[edges[i][2]].add(edges[i][1]);
indeg[edges[i][1]]++;
}
}
}
Queue<Integer> q = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
if (indeg[i] == 0)
q.add(i);
}
ArrayList<Integer> order = new ArrayList<>();
while (!q.isEmpty()) {
int cur = q.poll();
order.add(cur);
for (int x : g2[cur]) {
if (--indeg[x] == 0) {
q.add(x);
}
}
}
if (order.size() != n) {
pw.println("NO");
pw.close();
return;
}
int[] ans = new int[n];
for (int i = 0; i < order.size(); i++) {
ans[order.get(i)] = i;
}
pw.println("YES");
for (int i = 0; i < n; i++) {
pw.println((color[i] == 1 ? "R " : "L ") + ans[i]);
}
pw.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader f) {
br = new BufferedReader(f);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(next());
}
return arr;
}
}
}
| Java | ["4 4\n1 1 2\n1 2 3\n2 3 4\n2 4 1", "3 3\n1 1 2\n1 2 3\n1 1 3"] | 2 seconds | ["YES\nR 0\nL -3\nR 5\nL 6", "NO"] | null | Java 8 | standard input | [
"2-sat",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | 1c03ad9c0aacfbc9e40edc018a2526d3 | The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$ — the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 \leq type \leq 2; 1 \leq i,j \leq n; i≠j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. | 2,200 | In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i \in \{L, R\}; -10^9 \leq x_i \leq 10^9)$$$ — representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | standard output | |
PASSED | 41c97e14725dade081175cc43dcc5937 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.BiFunction;
public class Solution {
// VM options: -Xmx1024m -Xss1024m
long start = System.currentTimeMillis();
private static void shuffle(int[] a) {
Random r = new Random();
for (int i = 1; i < a.length; i++) {
int j = r.nextInt(i + 1);
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
private void solve() throws IOException {
int n = nextInt();
p = nextInt();
p2c = Math.min(p, 30);
p2 = 1L << p2c;
res = 0;
int[] a = nextIntArray(n);
shuffle(a);
Arrays.sort(a);
b = new HashSet<>();
for (int i = 0; i < n; i++) {
int t = a[i];
boolean contains = false;
while (t > 0) {
if (b.contains(t)) {
contains = true;
break;
}
if ((t & 1) == 1) {
t >>= 1;
} else if ((t & 3) == 0) {
t >>= 2;
} else {
break;
}
}
if (!contains) {
b.add(a[i]);
add(a[i]);
}
}
out.println(res);
if (isDebug) {
out.flush();
out.println("bsize: " + b.size());
out.println("time: " + (System.currentTimeMillis() - start));
}
}
private static final int MOD = 1000000007;
private static final long[] f = new long[200013];
private static final long[] ff = new long[f.length];
static {
f[0] = 1;
f[1] = 1;
ff[0] = 1;
ff[1] = 2;
for (int i = 2; i < f.length; i++) {
f[i] = f[i - 2] + f[i - 1];
if (f[i] >= MOD) f[i] -= MOD;
ff[i] = ff[i - 1] + f[i];
if (ff[i] >= MOD) ff[i] -= MOD;
}
}
private HashSet<Integer> b;
private long p2;
private int p2c;
private long res;
private int p;
private void add(long i) {
if (i >= p2) {
int left = p - digits(i, p2, p2c);
if (left > 0) {
res = res + ff[left];
if (res >= MOD) res -= MOD;
}
} else {
res++;
if (res >= MOD) res -= MOD;
add((i << 1) + 1);
add(i << 2);
}
}
private int digits(long n, long t, int cc) {
while (t <= n) {
cc++;
t <<= 1;
}
return cc;
}
private static final String inputFilename = "input.txt";
private static final String outputFilename = "output.txt";
private BufferedReader in;
private StringTokenizer line;
private PrintWriter out;
private final boolean isDebug;
private Solution(boolean isDebug) {
this.isDebug = isDebug;
}
public static void main(String[] args) throws IOException {
new Solution(Arrays.asList(args).contains("DEBUG_MODE")).run(args);
}
private void run(String[] args) throws IOException {
if (isDebug) {
in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilename)));
// in = new BufferedReader(new InputStreamReader(System.in));
} else {
in = new BufferedReader(new InputStreamReader(System.in));
}
out = new PrintWriter(System.out);
// out = new PrintWriter(outputFilename);
int t = isDebug ? nextInt() : 1;
// int t = nextInt();
// int t = 1;
for (int i = 0; i < t; i++) {
// out.print("Case #" + (i + 1) + ": ");
solve();
out.flush();
}
in.close();
out.flush();
out.close();
}
private void println(Object... objects) {
boolean isFirst = true;
for (Object o : objects) {
if (!isFirst) {
out.print(" ");
} else {
isFirst = false;
}
out.print(o.toString());
}
out.println();
}
private int[] nextIntArray(int n) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
private long[] nextLongArray(int n) throws IOException {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private String nextToken() throws IOException {
while (line == null || !line.hasMoreTokens()) {
line = new StringTokenizer(in.readLine());
}
return line.nextToken();
}
private static void assertPredicate(boolean p) {
if (!p) {
throw new RuntimeException();
}
}
private static void assertPredicate(boolean p, String message) {
if (!p) {
throw new RuntimeException(message);
}
}
private static void assertNotEqual(int unexpected, int actual) {
if (actual == unexpected) {
throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual);
}
}
private static void assertEqual(int expected, int actual) {
if (expected != actual) {
throw new RuntimeException("assertEqual: " + expected + " != " + actual);
}
}
}
| Java | ["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"] | 2 seconds | ["9", "14", "448201910"] | NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$. | Java 8 | standard input | [
"bitmasks",
"dp",
"math",
"matrices",
"number theory",
"strings"
] | fbb6d21b757d8d56396af1253ec9e719 | The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct. | 1,800 | Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$. | standard output | |
PASSED | 2bb67fee8826c29279eeadfd20065934 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.BiFunction;
public class Solution {
// VM options: -Xmx1024m -Xss1024m
long start = System.currentTimeMillis();
private void solve() throws IOException {
int n = nextInt();
p = nextInt();
p2c = Math.min(p, 30);
p2 = 1L << p2c;
res = 0;
int[] a = nextIntArray(n);
Arrays.sort(a);
b = new HashSet<>();
for (int i = 0; i < n; i++) {
int t = a[i];
boolean contains = false;
while (t > 0) {
if (b.contains(t)) {
contains = true;
break;
}
if ((t & 1) == 1) {
t >>= 1;
} else if ((t & 3) == 0) {
t >>= 2;
} else {
break;
}
}
if (!contains) {
b.add(a[i]);
add(a[i]);
}
}
out.println(res);
if (isDebug) {
out.flush();
out.println("bsize: " + b.size());
out.println("time: " + (System.currentTimeMillis() - start));
}
}
private static final int MOD = 1000000007;
private static final long[] f = new long[200013];
private static final long[] ff = new long[f.length];
static {
f[0] = 1;
f[1] = 1;
ff[0] = 1;
ff[1] = 2;
for (int i = 2; i < f.length; i++) {
f[i] = f[i - 2] + f[i - 1];
if (f[i] >= MOD) f[i] -= MOD;
ff[i] = ff[i - 1] + f[i];
if (ff[i] >= MOD) ff[i] -= MOD;
}
}
private HashSet<Integer> b;
private long p2;
private int p2c;
private long res;
private int p;
private void add(long i) {
if (i >= p2) {
int left = p - digits(i, p2, p2c);
if (left > 0) {
res = res + ff[left];
if (res >= MOD) res -= MOD;
}
} else {
res++;
if (res >= MOD) res -= MOD;
add((i << 1) + 1);
add(i << 2);
}
}
private int digits(long n, long t, int cc) {
while (t <= n) {
cc++;
t <<= 1;
}
return cc;
}
private static final String inputFilename = "input.txt";
private static final String outputFilename = "output.txt";
private BufferedReader in;
private StringTokenizer line;
private PrintWriter out;
private final boolean isDebug;
private Solution(boolean isDebug) {
this.isDebug = isDebug;
}
public static void main(String[] args) throws IOException {
new Solution(Arrays.asList(args).contains("DEBUG_MODE")).run(args);
}
private void run(String[] args) throws IOException {
if (isDebug) {
in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilename)));
// in = new BufferedReader(new InputStreamReader(System.in));
} else {
in = new BufferedReader(new InputStreamReader(System.in));
}
out = new PrintWriter(System.out);
// out = new PrintWriter(outputFilename);
int t = isDebug ? nextInt() : 1;
// int t = nextInt();
// int t = 1;
for (int i = 0; i < t; i++) {
// out.print("Case #" + (i + 1) + ": ");
solve();
out.flush();
}
in.close();
out.flush();
out.close();
}
private void println(Object... objects) {
boolean isFirst = true;
for (Object o : objects) {
if (!isFirst) {
out.print(" ");
} else {
isFirst = false;
}
out.print(o.toString());
}
out.println();
}
private int[] nextIntArray(int n) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
private long[] nextLongArray(int n) throws IOException {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private String nextToken() throws IOException {
while (line == null || !line.hasMoreTokens()) {
line = new StringTokenizer(in.readLine());
}
return line.nextToken();
}
private static void assertPredicate(boolean p) {
if (!p) {
throw new RuntimeException();
}
}
private static void assertPredicate(boolean p, String message) {
if (!p) {
throw new RuntimeException(message);
}
}
private static void assertNotEqual(int unexpected, int actual) {
if (actual == unexpected) {
throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual);
}
}
private static void assertEqual(int expected, int actual) {
if (expected != actual) {
throw new RuntimeException("assertEqual: " + expected + " != " + actual);
}
}
}
| Java | ["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"] | 2 seconds | ["9", "14", "448201910"] | NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$. | Java 8 | standard input | [
"bitmasks",
"dp",
"math",
"matrices",
"number theory",
"strings"
] | fbb6d21b757d8d56396af1253ec9e719 | The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct. | 1,800 | Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$. | standard output | |
PASSED | 0f15140e736e4898eb8b84202544017f | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.BiFunction;
public class Solution {
// VM options: -Xmx1024m -Xss1024m
long start = System.currentTimeMillis();
private void solve() throws IOException {
int n = nextInt();
p = nextInt();
p2c = Math.min(p, 30);
p2 = 1L << p2c;
res = 0;
int[] a = nextIntArray(n);
Arrays.sort(a);
b = new HashSet<>();
for (int i = 0; i < n; i++) {
int t = a[i];
boolean contains = false;
while (t > 0) {
if (b.contains(t)) {
contains = true;
break;
}
if ((t & 1) == 1) {
t >>= 1;
} else if ((t & 3) == 0) {
t >>= 2;
} else {
break;
}
}
if (!contains) {
b.add(a[i]);
add(a[i]);
}
}
out.println(res);
if (isDebug) {
out.flush();
out.println("bsize: " + b.size());
out.println("time: " + (System.currentTimeMillis() - start));
}
}
private static final int MOD = 1000000007;
private static final long[] f = new long[200013];
private static final long[] ff = new long[f.length];
static {
f[0] = 1;
f[1] = 1;
ff[0] = 1;
ff[1] = 2;
for (int i = 2; i < f.length; i++) {
f[i] = f[i - 2] + f[i - 1];
if (f[i] >= MOD) f[i] -= MOD;
ff[i] = (ff[i - 1] + f[i]) % MOD;
if (ff[i] >= MOD) ff[i] -= MOD;
}
}
private HashSet<Integer> b;
private long p2;
private int p2c;
private long res;
private int p;
private void add(long i) {
if (i >= p2) {
int left = p - digits(i, p2, p2c);
if (left > 0) {
res = res + ff[left];
if (res >= MOD) res -= MOD;
}
} else {
res++;
if (res >= MOD) res -= MOD;
add((i << 1) + 1);
add(i << 2);
}
}
private int digits(long n, long t, int cc) {
while (t <= n) {
cc++;
t <<= 1;
}
return cc;
}
private static final String inputFilename = "input.txt";
private static final String outputFilename = "output.txt";
private BufferedReader in;
private StringTokenizer line;
private PrintWriter out;
private final boolean isDebug;
private Solution(boolean isDebug) {
this.isDebug = isDebug;
}
public static void main(String[] args) throws IOException {
new Solution(Arrays.asList(args).contains("DEBUG_MODE")).run(args);
}
private void run(String[] args) throws IOException {
if (isDebug) {
in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilename)));
// in = new BufferedReader(new InputStreamReader(System.in));
} else {
in = new BufferedReader(new InputStreamReader(System.in));
}
out = new PrintWriter(System.out);
// out = new PrintWriter(outputFilename);
int t = isDebug ? nextInt() : 1;
// int t = nextInt();
// int t = 1;
for (int i = 0; i < t; i++) {
// out.print("Case #" + (i + 1) + ": ");
solve();
out.flush();
}
in.close();
out.flush();
out.close();
}
private void println(Object... objects) {
boolean isFirst = true;
for (Object o : objects) {
if (!isFirst) {
out.print(" ");
} else {
isFirst = false;
}
out.print(o.toString());
}
out.println();
}
private int[] nextIntArray(int n) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
private long[] nextLongArray(int n) throws IOException {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private String nextToken() throws IOException {
while (line == null || !line.hasMoreTokens()) {
line = new StringTokenizer(in.readLine());
}
return line.nextToken();
}
private static void assertPredicate(boolean p) {
if (!p) {
throw new RuntimeException();
}
}
private static void assertPredicate(boolean p, String message) {
if (!p) {
throw new RuntimeException(message);
}
}
private static void assertNotEqual(int unexpected, int actual) {
if (actual == unexpected) {
throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual);
}
}
private static void assertEqual(int expected, int actual) {
if (expected != actual) {
throw new RuntimeException("assertEqual: " + expected + " != " + actual);
}
}
}
| Java | ["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"] | 2 seconds | ["9", "14", "448201910"] | NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$. | Java 8 | standard input | [
"bitmasks",
"dp",
"math",
"matrices",
"number theory",
"strings"
] | fbb6d21b757d8d56396af1253ec9e719 | The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct. | 1,800 | Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$. | standard output | |
PASSED | b447a61de18470fb89119c1cb64ea018 | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.BiFunction;
public class Solution {
// VM options: -Xmx1024m -Xss1024m
long start = System.currentTimeMillis();
private void solve() throws IOException {
int n = nextInt();
p = nextInt();
p2c = Math.min(p, 30);
p2 = 1L << p2c;
res = 0;
int[] a = nextIntArray(n);
Arrays.sort(a);
b = new HashSet<>();
for (int i = 0; i < n; i++) {
int t = a[i];
boolean contains = false;
while (t > 0) {
if (b.contains(t)) {
contains = true;
break;
}
if ((t & 1) == 1) {
t >>= 1;
} else if ((t & 3) == 0) {
t >>= 2;
} else {
break;
}
}
if (!contains) {
b.add(a[i]);
add(a[i]);
}
}
out.println(res);
if (isDebug) {
out.flush();
out.println("bsize: " + b.size());
out.println("time: " + (System.currentTimeMillis() - start));
}
}
private static final int MOD = 1000000007;
private static final long[] f = new long[200013];
private static final long[] ff = new long[f.length];
static {
f[0] = 1;
f[1] = 1;
ff[0] = 1;
ff[1] = 2;
for (int i = 2; i < f.length; i++) {
f[i] = f[i - 2] + f[i - 1];
if (f[i] >= MOD) f[i] -= MOD;
ff[i] = (ff[i - 1] + f[i]) % MOD;
if (ff[i] >= MOD) ff[i] -= MOD;
}
}
private HashSet<Integer> b;
private long p2;
private int p2c;
private long res;
private int p;
private void add(long i) {
if (i >= p2) {
int left = p - digits(i, p2, p2c);
if (left > 0) {
res = res + ff[left];
if (res >= MOD) res -= MOD;
}
} else {
res++;
if (res >= MOD) res -= MOD;
add((i << 1) + 1);
add(i << 2);
}
}
private int digits(long n, long t, int cc) {
while (t <= n) {
cc++;
t <<= 1;
}
return cc;
}
private static final String inputFilename = "input.txt";
private static final String outputFilename = "output.txt";
private BufferedReader in;
private StringTokenizer line;
private PrintWriter out;
private final boolean isDebug;
private Solution(boolean isDebug) {
this.isDebug = isDebug;
}
public static void main(String[] args) throws IOException {
new Solution(Arrays.asList(args).contains("DEBUG_MODE")).run(args);
}
private void run(String[] args) throws IOException {
if (isDebug) {
in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilename)));
// in = new BufferedReader(new InputStreamReader(System.in));
} else {
in = new BufferedReader(new InputStreamReader(System.in));
}
out = new PrintWriter(System.out);
// out = new PrintWriter(outputFilename);
int t = isDebug ? nextInt() : 1;
// int t = nextInt();
// int t = 1;
for (int i = 0; i < t; i++) {
// out.print("Case #" + (i + 1) + ": ");
solve();
out.flush();
}
in.close();
out.flush();
out.close();
}
private void println(Object... objects) {
boolean isFirst = true;
for (Object o : objects) {
if (!isFirst) {
out.print(" ");
} else {
isFirst = false;
}
out.print(o.toString());
}
out.println();
}
private int[] nextIntArray(int n) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
private long[] nextLongArray(int n) throws IOException {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private String nextToken() throws IOException {
while (line == null || !line.hasMoreTokens()) {
line = new StringTokenizer(in.readLine());
}
return line.nextToken();
}
private static void assertPredicate(boolean p) {
if (!p) {
throw new RuntimeException();
}
}
private static void assertPredicate(boolean p, String message) {
if (!p) {
throw new RuntimeException(message);
}
}
private static void assertNotEqual(int unexpected, int actual) {
if (actual == unexpected) {
throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual);
}
}
private static void assertEqual(int expected, int actual) {
if (expected != actual) {
throw new RuntimeException("assertEqual: " + expected + " != " + actual);
}
}
}
| Java | ["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"] | 2 seconds | ["9", "14", "448201910"] | NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$. | Java 8 | standard input | [
"bitmasks",
"dp",
"math",
"matrices",
"number theory",
"strings"
] | fbb6d21b757d8d56396af1253ec9e719 | The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct. | 1,800 | Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$. | standard output | |
PASSED | 142c8b941eeed1f46a6ebcb9bbf488ed | train_108.jsonl | 1645367700 | You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
public class Main {
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
long MOD = 1000000007;
int n = nextInt();
int p = nextInt();
long[] a = new long[n];
long[] menshe = new long[2000000];
long[] rovno = new long[2000000];
TreeSet<Long> treeSet = new TreeSet<>();
ArrayList<Long> arrayList = new ArrayList<>();
for (int i = 0; i < n; i++) {
a[i] = nextLong();
arrayList.add(a[i]);
treeSet.add(a[i]);
}
check(arrayList, treeSet);
for (int i = 0; i < arrayList.size(); i++) {
double log = Math.log((double)arrayList.get(i)) / Math.log(2);
int intLog = (int) Math.ceil(log);
if (log < Math.ceil(log)) {
menshe[intLog]++;
} else {
rovno[intLog]++;
}
}
int uk = 0;
for (int i = 0; i <= p; i++) {
menshe[i] %= MOD;
menshe[i + 1] += menshe[i] % MOD;
menshe[i + 2] += rovno[i] % MOD;
menshe[i + 2] += menshe[i] % MOD;
if (rovno[i] == 1) {
rovno[i + 2] = 1;
}
}
long ans = 0;
for (int i = 0; i <= p; i++) {
ans += menshe[i] % MOD;
ans %= MOD;
if (i != p) {
ans += rovno[i];
}
}
ans %= MOD;
out.println(ans);
out.println();
in.close();
out.close();
}
public static void check(ArrayList<Long> arrayList, TreeSet<Long> treeSet) {
for (int j = arrayList.size() - 1; j >= 0; j--) {
long numb = arrayList.get(j);
while (numb > 0) {
if (numb % 4 == 0) {
numb /= 4;
} else if (numb % 2 == 1) {
numb /= 2;
} else {
break;
}
if (treeSet.contains(numb)) {
arrayList.remove(j);
break;
}
}
}
}
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
static int nextInt() throws IOException {
return parseInt(next());
}
static long nextLong() throws IOException {
return parseLong(next());
}
static double nextDouble() throws IOException {
return parseDouble(next());
}
static String next() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
}
class Triple {
int x;
int y;
int z;
public Triple(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
| Java | ["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"] | 2 seconds | ["9", "14", "448201910"] | NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$. | Java 8 | standard input | [
"bitmasks",
"dp",
"math",
"matrices",
"number theory",
"strings"
] | fbb6d21b757d8d56396af1253ec9e719 | The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct. | 1,800 | Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.