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
sequencelengths 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 | d54cfcbeddea9898307be648c6f20e0f | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class CC {
static StringTokenizer st;
static BufferedReader br;
static PrintWriter pw;
public static void main(String[] args) throws IOException{
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
String s = next();
String t = next();
int p1 = 0;
for (int i = 0; i < s.length(); i++) {
boolean found = false;
for (int j = p1; j < t.length(); j++) {
if (s.charAt(i)==t.charAt(j)) {
found = true;
p1 = j+1;
break;
}
}
if (!found) {
System.out.println(0);
return;
}
}
int p2 = t.length()-1;
for (int i = s.length()-1; i >= 0; i--) {
boolean found = false;
for (int j = p2; j >= 0; j--) {
if (s.charAt(i)==t.charAt(j)) {
found = true;
p2 = j-1;
break;
}
}
if (!found) {
System.out.println(0);
return;
}
}
p1--;
int ans = Math.max(0, p2-p1+1);
System.out.println(ans);
pw.close();
}
private static int nextInt() throws IOException {
return Integer.parseInt(next());
}
private static long nextLong() throws IOException {
return Long.parseLong(next());
}
private static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
private static String next() throws IOException{
while (st==null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 914389213eb26fed8297618982e1f44b | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.Scanner;
import java.util.TreeSet;
/**
*
* @author таня
*/
public class KF {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
B jk = new B();
}
}
class B {
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner(System.in);
String t = in.next();
String s = in.next();
char str[];
char name[];
int NI = 0;
int RI = s.length() - 1;
boolean fl = false;
public B() {
str = s.toCharArray();
name = t.toCharArray();
forward();
if (fl) {
reverse();
if (RI - NI > 0) {
out.print(RI - NI);
} else {
out.print(0);
}
} else {
out.print(0);
}
out.flush();
}
void forward() {
char elName = name[0];
int cI = 0;
for (int i = 0; i < str.length; i++) {
if (elName == str[i]) {
if (cI < name.length - 1) {
cI++;
elName = name[cI];
NI = i;
} else {
NI = i;
fl = true;
break;
}
}
}
}
void reverse() {
char elName = name[name.length - 1];
int cI = name.length - 1;
for (int i = str.length - 1; i >= 0; i--) {
if (elName == str[i]) {
if (cI > 0) {
cI--;
elName = name[cI];
RI = i;
} else {
RI = i;
break;
}
}
}
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 1d7c376ee4a7a86c5255a1a809b4b9d3 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.InputStream;
import java.util.NoSuchElementException;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.IOException;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Nguyen Trung Hieu - [email protected]
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
String name = in.readString();
String text = in.readString();
int from = 0;
int to = 0;
int i = 0;
int j = 0;
while (i < name.length() && j < text.length()) {
if (name.charAt(i) == text.charAt(j)) {
i++;
j++;
} else {
j++;
}
}
from = j - 1;
i = name.length() - 1;
j = text.length() - 1;
while (i >= 0 && j >= 0) {
if (name.charAt(i) == text.charAt(j)) {
i--;
j--;
} else {
j--;
}
}
to = j + 1;
out.printLine(Math.max(to - from, 0));
}
}
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 boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 4ab6811dc482cd6d97fc687da1ef2c39 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.util.*;
import java.io.*;
public class KnightTemplar {
public static void main(String [] args)throws Exception{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String s=bf.readLine();
String t=bf.readLine();
int leftPtr=0;
int rightPtr=0;
int ptr=0;
for(int i=0;i<t.length() && ptr < s.length();i++){
if(t.charAt(i)==s.charAt(ptr)){
ptr++;leftPtr=i;
//System.out.println(ptr+" "+t.length()+" "+t.charAt(ptr)+" "+leftPtr);
}
}
//System.out.println(ptr);
ptr=s.length()-1;
for(int i=t.length()-1;i>=0 && ptr >=0;i--){
if(t.charAt(i)==s.charAt(ptr)){
ptr--;rightPtr=i;
}
}
//System.out.println(leftPtr+" "+rightPtr);
if(rightPtr <= leftPtr)System.out.print("0");
else System.out.print((rightPtr-leftPtr));
}
} | Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 65c230066cb39665c155785e1b689123 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.lang.*;
import java.io.*;
import java.util.*;
public class CF{
public static boolean isSubstring(String a, String b){ //runs in O(|B| + |A| time)
//checks if a is substring of b
int i1 = 0;
for (int i = 0; i < b.length(); i++){
if (a.charAt(i1) == b.charAt(i)){
i1++;
}
if (i1 == a.length()) return true;
}
if (i1 == a.length()) return true;
return false;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = sc.nextLine();
if (!isSubstring(a, b)){
System.out.println(0);
}
else{
int r2 = 0;
int r1 = 0;
int i1 = 0;
int i2 = a.length() - 1;
for (int i = 0; i < b.length(); i++){
if (a.charAt(i1) == b.charAt(i)){
i1++;
}
if (i1 == a.length()){
r1 = i;
break;
}
}
for (int i = b.length()-1; i >= 0; i--){
if (a.charAt(i2) == b.charAt(i)){
i2--;
}
if (i2 == -1){
r2 = i;
break;
}
}
if (r2 < r1){
System.out.println(0);
}
else{
System.out.println(r2-r1);
}
}
}
} | Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 35544cdb702dd8494fc0b655522cb66a | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Andrey Rechitsky ([email protected])
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, FastScanner in, FastPrinter out) {
char[] s = in.next().toCharArray();
char[] t = in.next().toCharArray();
int start = 0;
int end = 0;
int i = 0;
for (start = 0; start < t.length; start++) {
if (i>=s.length) break;
if (t[start]==s[i]) i++;
}
i = s.length - 1;
for (end = t.length; end > 0; end--) {
if (i<0) break;
if (t[end-1]==s[i]) i--;
}
out.printLine(end>=start?end-start+1:0);
}
}
class FastScanner {
private BufferedReader reader;
private StringTokenizer st;
public FastScanner(InputStream stream) {
this.reader = new BufferedReader(new InputStreamReader(stream));
this.st = new StringTokenizer("");
}
public String next(){
while (!st.hasMoreTokens()){
st = new StringTokenizer(readLine());
}
return st.nextToken();
}
private String readLine() {
try {
String line = reader.readLine();
if (line==null) throw new InputMismatchException();
return line;
} catch (IOException e) {
throw new InputMismatchException();
}
}
}
class FastPrinter {
private final PrintWriter writer;
public FastPrinter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public void close() {
writer.close();
}
public void printLine(long i) {
writer.println(i);
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 231170e70598fbe9e8d972ab3f1f125b | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution {
StringTokenizer st;
BufferedReader in;
PrintWriter out;
void solve() throws IOException {
//in = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt"), "ISO-8859-1"));
//out = new PrintWriter(new OutputStreamWriter(new FileOutputStream("output.txt"), "ISO-8859-1"));
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
Locale.setDefault(Locale.ENGLISH);
String t = in.readLine();
String s = in.readLine();
int l = 0;
for (int i = 0; i < t.length() && l < s.length(); l++) {
if (t.charAt(i) == s.charAt(l)) {
i++;
}
}
l--;
int r = s.length() - 1;
for (int i = t.length() - 1; i >= 0 && r >= 0; r--) {
if (t.charAt(i) == s.charAt(r)) {
i--;
}
}
r++;
out.print(Math.max(0, r - l));
in.close();
out.close();
}
public static void main(String[] args) throws IOException {
new Solution().solve();
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
} | Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | acc86b520fb93b8bb3f510f6b76208b7 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.util.*;
import java.io.*;
public class c {
public static void main(String[] args) throws IOException
{
input.init(System.in);
String s = input.next(), t = input.next();
int a = first(s, t), b = last(s, t);
int res = 0;
if(a > -1 && b > -1 && b > a) res = b-a;
System.out.println(res);
}
static int first(String s, String t)
{
int pos = 0;
int i = 0;
for(i = 0; i<t.length() && pos < s.length(); i++)
{
if(t.charAt(i) == s.charAt(pos))
{
pos++;
}
}
return pos == s.length() ? i - 1 : -1;
}
static int last(String s, String t)
{
int pos = s.length()-1;
int i = t.length()-1;
for(i = t.length()-1; i >= 0 && pos >= 0; i--)
{
if(t.charAt(i) == s.charAt(pos)) pos--;
}
return pos == -1 ? i + 1 : -1;
}
public static class input {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( next() );
}
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | be37090da79427f31d96db7ed249836c | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | //package acc;
import java.util.*;
public class ACC {
//************Main*****************//
public static void main(String[] args)
{
Scanner in=new Scanner (System.in);
String s=in.next();
String s1=in.next();
int indl=0;
int i;
for( i=0;i<s1.length();i++){
if(s.charAt(indl)==s1.charAt(i)) indl++;
if(indl==s.length()) break;
}
int indr=s.length()-1;
int j;
for(j=s1.length()-1;j>=0;j--){
if(s.charAt(indr)==s1.charAt(j)) indr--;
if(indr<0) break;
}
if(i>=j) System.out.print(0);
else System.out.print(j-i);
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 1c9b1ae95239d93f8c48bca3f44df6c0 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
BufferedReader in;
PrintWriter out;
StringTokenizer st;
char[] s, t;
int n, m;
void solve() throws IOException {
s = in.readLine().toCharArray();
t = in.readLine().toCharArray();
n = s.length;
m = t.length;
int l = 0, r = m - 1;
boolean was_left = false, was_right = false;
for (int left_idx = 0; l < m; l++) {
if (s[left_idx] == t[l]) {
left_idx++;
if (left_idx == n) {
was_left = true;
break;
}
}
}
if (!was_left) {
out.println(0);
return;
}
for (int right_idx = n - 1; r >= 0; r--) {
if (s[right_idx] == t[r]) {
right_idx--;
if (right_idx == -1) {
was_right = true;
break;
}
}
}
if (!was_right || r < l) {
out.println(0);
return;
}
out.println(r - l);
}
Main() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
}
String ns() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int ni() throws IOException {
return Integer.parseInt(ns());
}
double nd() throws IOException {
return Double.parseDouble(ns());
}
long nl() throws IOException {
return Long.parseLong(ns());
}
public static void main(String[] args) throws IOException {
new Main();
}
} | Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 39f16067f51bc497b9d99a6540924756 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.*;
import java.util.*;
public class NameQuest {
public static void main(String[] args) throws IOException {
in.init(System.in);
String needle = in.next();
String haystack = in.next();
int begin = -1;
int end = -2;
int p = 0;
int n = needle.length();
int m = haystack.length();
for(int i = 0; i < m; i ++)
{
if(needle.charAt(p) == haystack.charAt(i))
{
p++;
if(p == n)
{
begin = i;
break;
}
}
}
p = n - 1;
for(int i = m - 1; i > -1; i--)
{
if(needle.charAt(p) == haystack.charAt(i))
{
p--;
if(p == -1)
{
end = i;
break;
}
}
}
System.out.println(end < begin ? 0 : end - begin);
}
public static class in {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream in) {
reader = new BufferedReader(
new InputStreamReader(in) );
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( next() );
}
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 255e0f00f08487bdbea58ced58d52700 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String t = sc.nextLine();
int l = 0;
int si = 0;
while (l < t.length() && si < s.length()) {
if (s.charAt(si) == t.charAt(l)) {
si++;
}
l++;
}
l--;
int r = t.length() - 1;
si = s.length() - 1;
while (r >= 0 && si >= 0) {
if (s.charAt(si) == t.charAt(r)) {
si--;
}
r--;
}
r++;
System.out.println(l < r ? r - l : 0);
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | a945766467f57e3c45cf503ce6704f00 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class C {
public static void main (String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s = bf.readLine();
String t = bf.readLine();
int q = 0;
int w = 0;
int index = 0;
boolean buff = false;
for (int i = 0; i < t.length(); i++) {
if (s.charAt(index) == t.charAt(i)) {
index++;
if (s.length() == index) {
buff = true;
q = i;
break;
}
}
}
if (!buff) {
System.out.println("0");
return;
}
index--;
buff = false;
for (int i = t.length() - 1; i > q; i--) {
if (s.charAt(index) == t.charAt(i)) {
index--;
if (-1 == index) {
buff = true;
w = i;
break;
}
}
}
if (buff == false) {
System.out.println("0");
return;
}
System.out.println(w - q);
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 2c8d209bd776ac639a5283c3ae4512f1 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.io.*;
@SuppressWarnings("unused")
public class Main {
public static void main(String[] args) throws IOException {
new Main().run();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
private void run() throws IOException {
if (new File("input.txt").exists())
in = new BufferedReader(new FileReader("input.txt"));
else
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
}
private void solve() throws IOException {
char s[] = nextToken().toCharArray();
int n = s.length;
char t[] = nextToken().toCharArray();
int m = t.length;
int l = -1;
int r = m;
for (int i = 0; i < n; i++) {
while (l < m - 1 && s[i] != t[l + 1])
l++;
l++;
}
for (int i = n - 1; i > -1; i--) {
while (r > 0 && s[i] != t[r - 1])
r--;
r--;
}
// System.err.println(l + " " + r);
if (l >= r)
out.println(0);
else
out.println(r - l);
}
void chk(boolean b) {
if (b)
return;
System.out.println(new Error().getStackTrace()[1]);
exit(999);
}
void deb(String fmt, Object... args) {
System.out.printf(Locale.US, fmt + "%n", args);
}
String nextToken() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
boolean EOF() throws IOException {
while (!st.hasMoreTokens()) {
String s = in.readLine();
if (s == null)
return true;
st = new StringTokenizer(s);
}
return false;
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | fa54a84472729fa92cadc49e3296c014 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char[] name = scanner.next().toCharArray();
char[] chars = scanner.next().toCharArray();
int left = 0;
int right = chars.length - 1;
int nameIndex = 0;
while (nameIndex < name.length && left < chars.length) {
while (left < chars.length && chars[left] != name[nameIndex]) {
++left;
}
++nameIndex;
++left;
}
--left;
nameIndex = name.length - 1;
while (nameIndex >= 0 && right >= 0) {
while (right >= 0 && chars[right] != name[nameIndex]) {
--right;
}
--nameIndex;
--right;
}
++right;
System.out.println(right - left > 0 ? right - left : 0);
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 845c9a7c6322572b625e19f40378daf4 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.util.Scanner;
/**
* http://codeforces.ru/contest/523/problem/C
*
* Created by duviteck on 14/03/15.
*/
public class Problem_C {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char[] s = scanner.nextLine().toCharArray();
char[] t = scanner.nextLine().toCharArray();
scanner.close();
int bestFirstEnd = findFromStart(s, t);
int bestSecondStart = findFromEnd(s, t);
int ans = Math.max(0, bestSecondStart - bestFirstEnd);
System.out.println(ans);
}
private static int findFromStart(char[] s, char[] t) {
int len = t.length;
int checkIndex = 0;
for (int i = 0; i < len; i++) {
if (s[checkIndex] == t[i]) {
checkIndex++;
if (checkIndex == s.length) {
return i;
}
}
}
return t.length;
}
private static int findFromEnd(char[] s, char[] t) {
int checkIndex = s.length - 1;
for (int i = t.length - 1; i >= 0; i--) {
if (s[checkIndex] == t[i]) {
checkIndex--;
if (checkIndex < 0) {
return i;
}
}
}
return 0;
}
} | Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | b45df5b5d7a3b4ef40b3aae3bed41e5e | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* #
* @author pttrung
*/
public class C_VK2015_Qual2 {
public static long MOD = 1000000007;
public static void main(String[] args) throws FileNotFoundException {
// PrintWriter out = new PrintWriter(new FileOutputStream(new File(
// "output.txt")));
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner();
String s = in.next();
String t = in.next();
int first = -1;
int last = -1;
int cur = 0;
for(int i = 0; i < t.length() && cur < s.length(); i++){
if(s.charAt(cur) == t.charAt(i)){
cur++;
if(cur == s.length()){
first = i;
}
}
}
cur = s.length() - 1;
for(int i = t.length() - 1; i >= 0 && cur >= 0; i--){
if(s.charAt(cur) == t.charAt(i)){
cur--;
if(cur < 0){
last = i;
}
}
}
if(first == -1 || first >= last){
out.println(0);
}else{
out.println(last - first);
}
out.close();
}
public static int[] KMP(String val) {
int i = 0;
int j = -1;
int[] result = new int[val.length() + 1];
result[0] = -1;
while (i < val.length()) {
while (j >= 0 && val.charAt(j) != val.charAt(i)) {
j = result[j];
}
j++;
i++;
result[i] = j;
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static int digit(long n) {
int result = 0;
while (n > 0) {
n /= 10;
result++;
}
return result;
}
public static double dist(long a, long b, long x, long y) {
double val = (b - a) * (b - a) + (x - y) * (x - y);
val = Math.sqrt(val);
double other = x * x + a * a;
other = Math.sqrt(other);
return val + other;
}
public static class Point implements Comparable<Point> {
int x, y;
public Point(int start, int end) {
this.x = start;
this.y = end;
}
@Override
public int hashCode() {
int hash = 5;
hash = 47 * hash + this.x;
hash = 47 * hash + this.y;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Point other = (Point) obj;
if (this.x != other.x) {
return false;
}
if (this.y != other.y) {
return false;
}
return true;
}
@Override
public int compareTo(Point o) {
return x - o.x;
}
}
public static class FT {
long[] data;
FT(int n) {
data = new long[n];
}
public void update(int index, long value) {
while (index < data.length) {
data[index] += value;
index += (index & (-index));
}
}
public long get(int index) {
long result = 0;
while (index > 0) {
result += data[index];
index -= (index & (-index));
}
return result;
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long pow(long a, long b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2);
if (b % 2 == 0) {
return val * val % MOD;
} else {
return val * (val * a % MOD) % MOD;
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
// System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 6392528c3ea491d13a84a59a654ddfa1 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.util.Scanner;
/**
* Created by bulbigood on 14.03.15.
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.next();
String str = sc.next();
int h = 0;
int a = 0;
boolean est = false;
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) == name.charAt(h))
h++;
if(h == name.length()){
est = true;
a = i;
h--;
break;
}
}
if(est){
for(int i = str.length() - 1; i >= 0; i--){
if(str.charAt(i) == name.charAt(h))
h--;
if(h == -1){
if(i - a <= 0)
System.out.println(0);
else
System.out.println(i - a);
break;
}
}
} else System.out.println(0);
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | a69bedae61abb88ed18fb8a931dcfffd | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Artem Gilmudinov
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Reader in = new Reader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, Reader in, PrintWriter out) {
String name = in.rl();
String s = in.rl();
int n, m;
n = name.length(); m = s.length();
int maxRight, meet;
maxRight = meet = 0;
for(int i = n - 1, j = m - 1; i >= 0; i--, j--) {
for(; j >= 0; j--) {
if(s.charAt(j) == name.charAt(i)) {
meet++;
maxRight = j;
break;
}
}
}
if(meet != n) {
out.println(0);
return;
}
meet = 0;
int maxLeft = 0;
for(int i = 0, j = 0; i < n; i++, j++) {
for(; j < maxRight; j++) {
if(s.charAt(j) == name.charAt(i)) {
meet++;
maxLeft = j;
break;
}
}
}
if(meet != n) {
out.println(0);
return;
}
out.println(maxRight - maxLeft);
}
}
class Reader {
private BufferedReader in;
public Reader(InputStream in) {
this.in = new BufferedReader(new InputStreamReader(in));
}
public String rl() {
try {
return in.readLine();
} catch(IOException e) {
throw new RuntimeException(e);
}
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 302452e75c1f731cb0b500c50502f0a3 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class VKCupC2 {
static BufferedReader in;
static PrintWriter out;
static StringTokenizer st;
static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String args[]) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
String s = in.readLine();
String t = in.readLine();
int n = s.length();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = s.charAt(i);
}
int m = t.length();
int b[] = new int[m];
int head = 0;
int pos1 = m;
for (int j = 0; j < m; j++) {
b[j] = t.charAt(j);
if (head < n && b[j] == a[head]) {
head++;
if (head == n) {
pos1 = j;
}
}
}
int tail = n - 1;
int pos2 = -1;
if (pos1 < m - n) {
for (int j = m - 1; j >= 0; j--) {
if (tail >= 0 && b[j] == a[tail]) {
tail--;
if (tail == -1) {
pos2 = j;
break;
}
}
}
}
int ans = pos2 - pos1;
if (ans > 0) {
out.println(ans);
} else {
out.println(0);
}
in.close();
out.close();
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 3648f2382b6582c512c3c054ee4ecccd | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.util.Scanner;
/**
* Created by loremon long time ago.
*/
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
String s1, s2;
char [] a, b;
int n, m, i, j, ans=1000000000;
ArrayList <Integer> begin = new ArrayList<Integer>(), end = new ArrayList<Integer>();
s1 = read.nextLine();
s2 = read.nextLine();
n = s1.length();
m = s2.length();
a = s1.toCharArray();
b = s2.toCharArray();
i = 0;
for (j = 0; j < m; j++) {
if (a[i] == b[j]) i++;
if (i ==n ) {
ans = j;
break;
}
}
if (ans == 1000000000) {
System.out.print(0);
return;
}
i = n - 1;
for (j = m - 1; j >= 0; j--) {
if (a[i] == b[j]) i--;
if (i < 0) {
ans = j - ans;
break;
}
}
if (ans < 0) ans = 0;
System.out.print(ans);
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 05451d3329b9e3ea713af77cdbd2fb51 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution {
static BufferedReader cin;
static StringTokenizer tok;
public static String nextToken() throws Exception {
if (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(cin.readLine());
}
return tok.nextToken();
}
public static int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public static double nextDouble() throws NumberFormatException, Exception {
return Double.parseDouble(nextToken());
}
public static void main(String[] args) throws Exception {
cin = new BufferedReader(new InputStreamReader(System.in));
String s = cin.readLine();
String t = cin.readLine();
int si = 0;
int leftI = -1;
for (int i = 0; i < t.length(); i++) {
if (t.charAt(i) == s.charAt(si)) {
si++;
if (si >= s.length()) {
leftI = i;
break;
}
}
}
si = s.length() - 1;
int rightI = -1;
for (int i = t.length() - 1; i >= 0; i--) {
if (t.charAt(i) == s.charAt(si)) {
si--;
if (si < 0) {
rightI = i;
break;
}
}
}
System.out.println(Math.max(0, rightI - leftI));
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 072ffb7f7f27c64553c3843355bbd529 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class C {
FastScanner in;
PrintWriter out;
public static void main(String args[] ) throws Exception {
new C().run();
}
void solve() throws IOException {
String s = in.next();
String t = in.next();
int n = s.length();
int m = t.length();
int[] L = new int[m];
int[] R = new int[m];
for (int i = 0; i < m; i++) {
L[i] = (i==0 ? 0 : L[i-1]);
if (L[i] < n && s.charAt(L[i]) == t.charAt(i))
L[i]++;
}
for (int i = m-1; i >= 0; i--) {
R[i] = (i == m-1 ? n-1 : R[i+1]);
if (R[i] >= 0 && s.charAt(R[i]) == t.charAt(i))
R[i]--;
}
int cnt = 0;
for (int i = 0; i < m-1; i++) {
if (L[i] == n && R[i+1] == -1)
cnt++;
}
out.println(cnt);
}
void run() throws IOException {
in = new FastScanner(System.in);
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
out.close();
}
class FastScanner {
StringTokenizer st;
BufferedReader bf;
public FastScanner(InputStream is) {
bf = new BufferedReader(new InputStreamReader(is));
}
public FastScanner(File f){
try{
bf = new BufferedReader(new FileReader(f));
}
catch(IOException ex){
ex.printStackTrace();
}
}
String next(){
while(st == null || !st.hasMoreTokens()){
try{
st = new StringTokenizer(bf.readLine());
}
catch(Exception ex){
ex.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());
}
short nextShort(){
return Short.parseShort(next());
}
int[] nextIntArray(int n){
int a[] = new int[n];
for(int i = 0; i < n; ++i)
a[i] = Integer.parseInt(next());
return a;
}
long[] nextLongArray(int n){
long a[] = new long[n];
for(int i = 0; i < n; ++i)
a[i] = Long.parseLong(next());
return a;
}
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 1c9fb4555bbfa8bacd77fc904e74dec6 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) throws IOException {
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
//Scanner scan = new Scanner(System.in);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
String n = scan.readLine();
String m = scan.readLine();
int c = 0;
boolean s = true;
for (int i = 0; i < m.length(); i++) {
if (n.charAt(c) == m.charAt(i)) {
if (c == n.length() - 1) {
c = i;
s = false;
break;
}
c++;
}
}
if (s || c == m.length() - 1) {
System.out.println(0);
System.exit(0);
}
int f = n.length()-1;
boolean wow = true;
for (int i = m.length()-1; i > c; i--) {
if (n.charAt(f) == m.charAt(i)) {
if (f == 0) {
f = i;
wow = false;
break;
}
f--;
}
}
if (wow) {
System.out.println(0);
System.exit(0);
}
//System.out.println(f + " " + c);
pw.write(f-c + "\n");
pw.close();
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | ec391f173df81e8b0a99c6c1d2e8d947 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.*;
import java.util.*;
public final class name_quest
{
static FastScanner sc=new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out=new PrintWriter(System.out);
static ArrayList<Integer>[] al;
@SuppressWarnings("unchecked")
public static void main(String args[]) throws Exception
{
char[] a=sc.next().toCharArray(),b=sc.next().toCharArray();
al=new ArrayList[123];
for(int i=97;i<=122;i++)
{
al[i]=new ArrayList<Integer>();
al[i].add(-1);
}
for(int i=0;i<b.length;i++)
{
al[b[i]].add(i);
}
int first=-1,last=-1,prev=-1;
boolean first_now=true,last_now=true;
for(int i=0;i<a.length;i++)
{
int low=0,high=al[a[i]].size()-1;
while(low<high)
{
int mid=(low+high)>>1;
if(al[a[i]].get(mid)>prev)
{
high=mid;
}
else
{
low=mid+1;
}
}
if(al[a[i]].get(low)>prev)
{
prev=al[a[i]].get(low);
}
else
{
first_now=false;
break;
}
}
first=prev;prev=b.length;
for(int i=a.length-1;i>=0;i--)
{
int low=0,high=al[a[i]].size()-1;
while(low<high)
{
int mid=(low+high+1)>>1;
if(al[a[i]].get(mid)<prev)
{
low=mid;
}
else
{
high=mid-1;
}
}
if(al[a[i]].get(low)<prev && al[a[i]].get(low)!=-1)
{
prev=al[a[i]].get(low);
}
else
{
last_now=false;
break;
}
}
last=prev;
if(last>first && first_now && last_now)
{
out.println(last-first);
}
else
{
out.println(0);
}
out.close();
}
}
class FastScanner
{
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public String next() throws Exception {
return nextToken().toString();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
} | Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | d6b426f03f86f9bae26815e72e9aa0e2 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
// final Scanner sc = new Scanner(System.in);
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
// String s = sc.nextLine();
int si = 0;
String t = br.readLine();
// String t = sc.nextLine();
int ti = 0;
int posLR = 0;
int posRL = 0;
// from the bottom to the top
for (ti = 0; ti < t.length(); ti++) {
if (t.charAt(ti) == s.charAt(si)) {
if (si == s.length() - 1) {
si = 0;
posLR = ti;
break;
}
si++;
}
}
si = s.length() - 1;
// from the top to the bottom
for (ti = t.length() - 1; ti >= 0; ti--) {
if (t.charAt(ti) == s.charAt(si)) {
if (si == 0) {
posRL = ti;
break;
}
si--;
}
}
if (posLR < posRL) {
System.out.println(posRL - posLR);
} else {
System.out.println(0);
}
}
} | Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 2e6eaa8f5e088ee5996bcaaeb4e11951 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author coldhands
*/
import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class C implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args) {
new Thread(null, new C(), "", 256 * (1L << 20)).start();
}
public void run() {
try {
long t1 = System.currentTimeMillis();
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
// in = new BufferedReader(new FileReader("src/input.txt"));
Locale.setDefault(Locale.US);
solve();
in.close();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
private String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
private int readInt() throws IOException {
return Integer.parseInt(readString());
}
private long readLong() throws IOException {
return Long.parseLong(readString());
}
private double readDouble() throws IOException {
return Double.parseDouble(readString());
}
// solution
private void solve() throws IOException {
String pattern = in.readLine();
String s = in.readLine();
int l = 0;
for (char ch : pattern.toCharArray()) {
while (l < s.length() && s.charAt(l) != ch)
l++;
l++;
}
int r = s.length() - 1;
StringBuilder sb = new StringBuilder();
sb.append(pattern);
for (char ch : sb.reverse().toString().toCharArray())
{
while (r >= 0 && s.charAt(r) != ch)
r--;
r--;
}
// System.err.println(l + " " + r);
out.println(Math.max(0, r - l + 2));
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 813448fed4530e058f2464b6e968cff4 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
import static java.lang.Math.*;
public class Solution implements Runnable {
void solve() throws Exception {
String s = sc.nextToken();
String t = sc.nextToken();
int lf = t.length();
for (int i = 0, j = 0; i < t.length() && j < s.length(); i++) {
if (t.charAt(i) == s.charAt(j)) {
j++;
if (j == s.length())
lf = i;
}
}
int rg = -1;
for (int i = t.length() - 1, j = s.length() - 1; i >= 0 && j >= 0; --i) {
if (t.charAt(i) == s.charAt(j)) {
--j;
if (j == -1) {
rg = i;
}
}
}
out.println(max(0, rg - lf));
}
BufferedReader in;
PrintWriter out;
FastScanner sc;
static Throwable throwable;
public static void main(String[] args) throws Throwable {
Thread thread = new Thread(null, new Solution(), "", (1 << 26));
thread.start();
thread.join();
thread.run();
if (throwable != null)
throw throwable;
}
public void run() {
try {
//in = new BufferedReader(new FileReader(".in"));
//out = new PrintWriter(new FileWriter(".out"));
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new FastScanner(in);
solve();
} catch (Exception e) {
throwable = e;
} finally {
out.close();
}
}
}
class FastScanner {
BufferedReader reader;
StringTokenizer strTok;
FastScanner(BufferedReader reader) {
this.reader = reader;
}
public String nextToken() throws Exception {
while (strTok == null || !strTok.hasMoreTokens())
strTok = new StringTokenizer(reader.readLine());
return strTok.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
} | Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 1bf9ad8b091ba1c4ac0162e52042c5ed | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.next();
String t = input.next();
boolean stop = false;
int pos_s = 0;
int pos_1 = -1;
int pos_2 = -1;
boolean found = false;
for(int i = 0; i < t.length(); i++){
if(t.charAt(i) == s.charAt(pos_s)){
if(pos_s == s.length() - 1){
pos_1 = i;
break;
}
else
pos_s++;
}
}
if(pos_1 > -1){
for(int i = t.length() - 1; i > pos_1; i--){
//System.out.println("t[" + i + "]: " + t.charAt(i));
if(t.charAt(i) == s.charAt(pos_s)){
if(pos_s == 0){
pos_2 = i;
break;
}
else
pos_s--;
}
}
}
//System.out.println("pos_1: " + pos_1);
//System.out.println("pos_2: " + pos_2);
if(pos_1 == -1)
System.out.println(0);
else{
if(pos_2 == -1)
System.out.println(0);
else
System.out.println(pos_2 - pos_1);
}
/*
for(int i = 0; i < s.length(); i++){
if(t.indexOf(s.charAt(i)) != -1)
t = t.substring(t.indexOf(s.charAt(i)) + 1);
else{
stop = true;
break;
}
}
if(!stop)
for(int i = s.length() - 1; i >= 0; i--){
if(t.indexOf(s.charAt(i)) != -1)
t = t.substring(0, t.lastIndexOf(s.charAt(i)));
else{
stop = true;
break;
}
}
if(stop)
System.out.println(0);
else
System.out.println(t.length() + 1);
*/
input.close();
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 93119d09683753666359a895e167546b | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.*;
import java.util.*;
public class VK_C {
static Scanner in = new Scanner(System.in);;
static PrintStream out = System.out;
static char[] s, t;
public static void main(String[] args) {
s = in.nextLine().toCharArray();
t = in.nextLine().toCharArray();
int i=0;
int j=0;
int first_end=-1;
int last_begin=-1;
while(i<t.length){
if(t[i] == s[j]){
if(j<s.length-1)
j++;
else {
first_end = i;
break;
}
}
i++;
}
i=t.length-1;
j=s.length-1;
while(i>0){
if(t[i] == s[j]){
if(j>0)
j--;
else {
last_begin = i;
break;
}
}
i--;
}
if(first_end>=0 && last_begin>=0 && first_end < last_begin)
out.print(last_begin-first_end);
else
out.print(0);
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 9a637ec623df28d76e6c679bda82b847 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static class NameSearcher {
String s = null;
String t = null;
public void setS(String s) {
this.s = s;
}
public void setT(String t) {
this.t = t;
}
public int getNumber() {
if (s.length() >= t.length()) {
return 0;
}
int first = indexOf();
int last = lastIndexOf();
if (first == -1 || last == -1) {
return 0;
}
return last - first > 0 ? last - first : 0;
}
private int indexOf() {
int index = 0;
for (int i = 0; i < t.length(); i++) {
if (s.charAt(index) == t.charAt(i)) {
index++;
}
if (index == s.length()) {
return i;
}
}
return -1;
}
private int lastIndexOf() {
int index = s.length() - 1;
for (int i = t.length() - 1; i >= 0; i--) {
if (s.charAt(index) == t.charAt(i)) {
index--;
}
if (index == -1) {
return i;
}
}
return -1;
}
}
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
NameSearcher nm = new NameSearcher();
nm.setS(br.readLine());
nm.setT(br.readLine());
System.out.print(nm.getNumber());
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 1d4c2cf6358c57e57bc6207760ff310b | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
private static class Geometry {
public static boolean check3DotsOnLine(int x1, int y1, int x2, int y2, int x3, int y3) {
return ((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) == 0;
}
public static boolean check3DotsOnLine(long x1, long y1, long x2, long y2, long x3, long y3) {
return ((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) == 0;
}
}
public static Scanner sc;
public static PrintWriter pw;
public static void main(String[] args) {
sc = new Scanner(System.in);
pw = new PrintWriter(System.out);
new Main().run();
sc.close();
pw.flush();
pw.close();
}
private void run() {
readData();
solve();
}
String s, t;
private void readData() {
s = sc.next();
t = sc.next();
}
private void solve() {
int l = 0, r = 0;
int k = 0;
int n = t.length();
int m = s.length();
for (int i=0;i<n;i++) {
if (t.charAt(i) == s.charAt(k)) {
k++;
if (k == m) {
l = i;
break;
}
}
}
k = m-1;
for (int i=n-1;i>=0;i--) {
if (t.charAt(i) == s.charAt(k)) {
k--;
if (k == -1) {
r = i;
break;
}
}
}
pw.println(Math.max(0, r-l));
}
} | Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 3b989fc45adcff81b4fcb6eda48e47fb | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class C {
static InputStreamReader isr = new InputStreamReader(System.in);
static BufferedReader br = new BufferedReader(isr);
static int[] readIntArray() throws IOException {
String[] v = br.readLine().split(" ");
int[] ans = new int[v.length];
for (int i = 0; i < ans.length; i++) {
ans[i] = Integer.valueOf(v[i]);
}
return ans;
}
static int S, T;
static char[] s, t;
static int[] fallback;
public static void main(String[] args) throws IOException {
// s = "participate in parachute".toCharArray();
// t = "participate in parachute asdf participate in parachute sdf participate in parachute".toCharArray();
s = br.readLine().toCharArray();
t = br.readLine().toCharArray();
S = s.length;
T = t.length;
int si = 0;
int[] b = new int[2];
boolean[] bf = new boolean[2];
for (int ti = 0; ti < T; ti++) {
if (s[si] == t[ti]){
si++;
if (si == S) {
b[0] = ti+1;
bf[0] = true;
break;
}
}
}
si = S - 1;
for (int ti = T - 1; ti >= 0; ti--) {
if (s[si] == t[ti]){
si--;
if (si == -1) {
b[1] = ti;
bf[1] = true;
break;
}
}
}
if (bf[0] && bf[1])
System.out.println(Math.max(b[1] - b[0] + 1, 0));
else
System.out.println(0);
// System.out.println(System.currentTimeMillis() - start);
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 01da3e49b62a1f3ff314ded5f49f112d | train_000.jsonl | 1564497300 | There is a country with $$$n$$$ citizens. The $$$i$$$-th of them initially has $$$a_{i}$$$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have.Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $$$x$$$ are paid accordingly so that after the payout they have exactly $$$x$$$ money. In this case the citizens don't send a receipt.You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
public static void process()throws IOException
{
int n=ni(),arr[]=new int[n+1];
for(int i=1;i<=n;i++) arr[i]=ni();
int q=ni(),query_no_2[]=new int[q+1],query_no_1[]=new int[n+1];
for(int i=1;i<=q;i++){
int q_type=ni();
if(q_type==1){
int p=ni(),x=ni();
query_no_1[p]=i;
arr[p]=x;
}
else{
int x=ni();
query_no_2[i]=x;
}
}
for(int i=q-1;i>=0;i--)
query_no_2[i]=Math.max(query_no_2[i], query_no_2[i+1]);
for(int i=1;i<=n;i++)
arr[i]=Math.max(arr[i], query_no_2[query_no_1[i]]);
for(int i=1;i<=n;i++) p(arr[i]+" ");
p("\n");
}
static FastReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
out = new PrintWriter(System.out);
sc=new FastReader();
long s = System.currentTimeMillis();
int t=1;
//t=ni();
while(t-->0)
process();
out.flush();
System.err.println(System.currentTimeMillis()-s+"ms");
}
static void pn(Object o){out.println(o);}
static void p(Object o){out.print(o);}
static int ni()throws IOException{return Integer.parseInt(sc.next());}
static long nl()throws IOException{return Long.parseLong(sc.next());}
static double nd()throws IOException{return Double.parseDouble(sc.next());}
static String nln()throws IOException{return sc.nextLine();}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
static long mod=(long)1e9+7l;
static<T> void r_sort(T arr[],int n){
Random r = new Random();
for (int i = n-1; i > 0; i--){
int j = r.nextInt(i+1);
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Arrays.sort(arr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
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();
}
String nextLine(){
String str = "";
try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }
return str;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
} | Java | ["4\n1 2 3 4\n3\n2 3\n1 2 2\n2 1", "5\n3 50 2 1 10\n3\n1 2 0\n2 8\n1 3 20"] | 2 seconds | ["3 2 3 4", "8 8 20 8 10"] | NoteIn the first example the balances change as follows: 1 2 3 4 $$$\rightarrow$$$ 3 3 3 4 $$$\rightarrow$$$ 3 2 3 4 $$$\rightarrow$$$ 3 2 3 4In the second example the balances change as follows: 3 50 2 1 10 $$$\rightarrow$$$ 3 0 2 1 10 $$$\rightarrow$$$ 8 8 8 8 10 $$$\rightarrow$$$ 8 8 20 8 10 | Java 11 | standard input | [
"data structures",
"binary search",
"sortings",
"brute force"
] | 7b788c660fb8ca703af0030f4c84ce96 | The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^{5}$$$) — the numer of citizens. The next line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_{i} \le 10^{9}$$$) — the initial balances of citizens. The next line contains a single integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^{5}$$$) — the number of events. Each of the next $$$q$$$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($$$1 \le p \le n$$$, $$$0 \le x \le 10^{9}$$$), or 2 x ($$$0 \le x \le 10^{9}$$$). In the first case we have a receipt that the balance of the $$$p$$$-th person becomes equal to $$$x$$$. In the second case we have a payoff with parameter $$$x$$$. | 1,600 | Print $$$n$$$ integers — the balances of all citizens after all events. | standard output | |
PASSED | dee0b1932d837a2ab622e2ae78cdd6f5 | train_000.jsonl | 1564497300 | There is a country with $$$n$$$ citizens. The $$$i$$$-th of them initially has $$$a_{i}$$$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have.Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $$$x$$$ are paid accordingly so that after the payout they have exactly $$$x$$$ money. In this case the citizens don't send a receipt.You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class B
{
public static void process()throws IOException
{
int n = ni(), arr[] = new int[n+1], latest_query[] = new int[n+1];
for(int i = 1; i<=n; i++) arr[i] = ni();
int q = ni(), query[] = new int[q+2]; // query[] holds the threshold for all ele after 'i'th query
for(int i = 1; i <= q; i++){
int choice = ni();
if(choice == 1){
int p = ni(), x = ni();
latest_query[p] = i;
arr[p] = x;
}
else{
query[i] = ni();
}
}
for(int i = q; i >= 0; i--)
query[i] = Math.max(query[i], query[i+1]);
for(int i = 1; i<=n; i++)
arr[i] = Math.max(arr[i], query[latest_query[i]]);
for(int i = 1; i<=n; i++) p(arr[i]+" ");
p("\n");
}
static FastReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
out = new PrintWriter(System.out);
sc=new FastReader();
long s = System.currentTimeMillis();
int t=1;
//t=ni();
while(t-->0)
process();
out.flush();
System.err.println(System.currentTimeMillis()-s+"ms");
}
static final long mod=(long)1e9+7l;
static void pn(Object o){out.println(o);}
static void p(Object o){out.print(o);}
static int ni()throws IOException{return Integer.parseInt(sc.next());}
static long nl()throws IOException{return Long.parseLong(sc.next());}
static double nd()throws IOException{return Double.parseDouble(sc.next());}
static String nln()throws IOException{return sc.nextLine();}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); }
}
return st.nextToken();
}
String nextLine(){
String str = "";
try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }
return str;
}
}
} | Java | ["4\n1 2 3 4\n3\n2 3\n1 2 2\n2 1", "5\n3 50 2 1 10\n3\n1 2 0\n2 8\n1 3 20"] | 2 seconds | ["3 2 3 4", "8 8 20 8 10"] | NoteIn the first example the balances change as follows: 1 2 3 4 $$$\rightarrow$$$ 3 3 3 4 $$$\rightarrow$$$ 3 2 3 4 $$$\rightarrow$$$ 3 2 3 4In the second example the balances change as follows: 3 50 2 1 10 $$$\rightarrow$$$ 3 0 2 1 10 $$$\rightarrow$$$ 8 8 8 8 10 $$$\rightarrow$$$ 8 8 20 8 10 | Java 11 | standard input | [
"data structures",
"binary search",
"sortings",
"brute force"
] | 7b788c660fb8ca703af0030f4c84ce96 | The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^{5}$$$) — the numer of citizens. The next line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_{i} \le 10^{9}$$$) — the initial balances of citizens. The next line contains a single integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^{5}$$$) — the number of events. Each of the next $$$q$$$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($$$1 \le p \le n$$$, $$$0 \le x \le 10^{9}$$$), or 2 x ($$$0 \le x \le 10^{9}$$$). In the first case we have a receipt that the balance of the $$$p$$$-th person becomes equal to $$$x$$$. In the second case we have a payoff with parameter $$$x$$$. | 1,600 | Print $$$n$$$ integers — the balances of all citizens after all events. | standard output | |
PASSED | a3e0466bc1b14e2da21f0ba32999807b | train_000.jsonl | 1564497300 | There is a country with $$$n$$$ citizens. The $$$i$$$-th of them initially has $$$a_{i}$$$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have.Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $$$x$$$ are paid accordingly so that after the payout they have exactly $$$x$$$ money. In this case the citizens don't send a receipt.You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.InputMismatchException;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
Output out = new Output(outputStream);
BWelfareState solver = new BWelfareState();
solver.solve(1, in, out);
out.close();
}
}
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", Runtime.getRuntime().freeMemory() >> 1L);
thread.start();
thread.join();
}
static class BWelfareState {
public BWelfareState() {
}
public void solve(int kase, InputReader in, Output pw) {
int n = in.nextInt();
int[] arr = in.nextInt(n);
boolean[] set = new boolean[n];
int q = in.nextInt();
int[][] queries = new int[q][];
for(int i = 0; i<q; i++) {
int type = in.nextInt();
if(type==1) {
queries[i] = new int[] {type, in.nextInt()-1, in.nextInt()};
}else {
queries[i] = new int[] {type, in.nextInt()};
}
}
int max = 0;
for(int i = q-1; i>=0; i--) {
if(queries[i][0]==1) {
if(!set[queries[i][1]]) {
arr[queries[i][1]] = Math.max(max, queries[i][2]);
set[queries[i][1]] = true;
}
}else {
max = Math.max(max, queries[i][1]);
}
}
for(int i = 0; i<n; i++) {
if(!set[i]) {
arr[i] = Math.max(arr[i], max);
}
}
pw.println(arr);
}
}
static class Output implements Closeable, Flushable {
public StringBuilder sb;
public OutputStream os;
public int BUFFER_SIZE;
public boolean autoFlush;
public String LineSeparator;
public Output(OutputStream os) {
this(os, 1<<16);
}
public Output(OutputStream os, int bs) {
BUFFER_SIZE = bs;
sb = new StringBuilder(BUFFER_SIZE);
this.os = new BufferedOutputStream(os, 1<<17);
autoFlush = false;
LineSeparator = System.lineSeparator();
}
public void print(int i) {
print(String.valueOf(i));
}
public void print(String s) {
sb.append(s);
if(autoFlush) {
flush();
}else if(sb.length()>BUFFER_SIZE >> 1) {
flushToBuffer();
}
}
public void println() {
sb.append(LineSeparator);
}
public void println(int[] arr) {
for(int i = 0; i<arr.length; i++) {
if(i!=0) {
print(" ");
}
print(arr[i]);
}
println();
}
private void flushToBuffer() {
try {
os.write(sb.toString().getBytes());
}catch(IOException e) {
e.printStackTrace();
}
sb = new StringBuilder(BUFFER_SIZE);
}
public void flush() {
try {
flushToBuffer();
os.flush();
}catch(IOException e) {
e.printStackTrace();
}
}
public void close() {
flush();
try {
os.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
static interface InputReader {
int nextInt();
default int[] nextInt(int n) {
int[] ret = new int[n];
for(int i = 0; i<n; i++) {
ret[i] = nextInt();
}
return ret;
}
}
static class FastReader implements InputReader {
final private int BUFFER_SIZE = 1<<16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer;
private int bytesRead;
public FastReader(InputStream is) {
din = new DataInputStream(is);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public int nextInt() {
int ret = 0;
byte c = skipToDigit();
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;
}
private boolean isDigit(byte b) {
return b>='0'&&b<='9';
}
private byte skipToDigit() {
byte ret;
while(!isDigit(ret = read())&&ret!='-') ;
return ret;
}
private void fillBuffer() {
try {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
}catch(IOException e) {
e.printStackTrace();
throw new InputMismatchException();
}
if(bytesRead==-1) {
buffer[0] = -1;
}
}
private byte read() {
if(bytesRead==-1) {
throw new InputMismatchException();
}else if(bufferPointer==bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
}
}
| Java | ["4\n1 2 3 4\n3\n2 3\n1 2 2\n2 1", "5\n3 50 2 1 10\n3\n1 2 0\n2 8\n1 3 20"] | 2 seconds | ["3 2 3 4", "8 8 20 8 10"] | NoteIn the first example the balances change as follows: 1 2 3 4 $$$\rightarrow$$$ 3 3 3 4 $$$\rightarrow$$$ 3 2 3 4 $$$\rightarrow$$$ 3 2 3 4In the second example the balances change as follows: 3 50 2 1 10 $$$\rightarrow$$$ 3 0 2 1 10 $$$\rightarrow$$$ 8 8 8 8 10 $$$\rightarrow$$$ 8 8 20 8 10 | Java 11 | standard input | [
"data structures",
"binary search",
"sortings",
"brute force"
] | 7b788c660fb8ca703af0030f4c84ce96 | The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^{5}$$$) — the numer of citizens. The next line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_{i} \le 10^{9}$$$) — the initial balances of citizens. The next line contains a single integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^{5}$$$) — the number of events. Each of the next $$$q$$$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($$$1 \le p \le n$$$, $$$0 \le x \le 10^{9}$$$), or 2 x ($$$0 \le x \le 10^{9}$$$). In the first case we have a receipt that the balance of the $$$p$$$-th person becomes equal to $$$x$$$. In the second case we have a payoff with parameter $$$x$$$. | 1,600 | Print $$$n$$$ integers — the balances of all citizens after all events. | standard output | |
PASSED | 1aa7b6132b7e12b88b2f34f28fa97e26 | train_000.jsonl | 1564497300 | There is a country with $$$n$$$ citizens. The $$$i$$$-th of them initially has $$$a_{i}$$$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have.Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $$$x$$$ are paid accordingly so that after the payout they have exactly $$$x$$$ money. In this case the citizens don't send a receipt.You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. | 256 megabytes | import java.io.*;
import java.util.*;
public class A {
static FastReader f = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int n = f.nextInt();
Pair[] pairs = new Pair[n];
for(int i=0;i<n;i++) {
pairs[i] = new Pair(f.nextInt());
}
int q = f.nextInt();
int[] suffMax = new int[q+1];
for (int i=0;i<q;i++) {
int op = f.nextInt();
if(op == 1) {
int p = f.nextInt() - 1;
int x = f.nextInt();
pairs[p].pos = i;
pairs[p].val = x;
} else {
suffMax[i] = f.nextInt();
}
}
for(int i=q-1, max = 0;i>=0;i--) {
max = Math.max(max, suffMax[i]);
suffMax[i] = max;
}
for(int i=0;i<n;i++) {
int now = Math.max(pairs[i].val, suffMax[pairs[i].pos+1]);
out.print(now);
out.print(" ");
}
out.close();
}
static class Pair {
int pos = -1;
int val;
Pair(int val) {
this.val = val;
}
}
//fast input reader
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n1 2 3 4\n3\n2 3\n1 2 2\n2 1", "5\n3 50 2 1 10\n3\n1 2 0\n2 8\n1 3 20"] | 2 seconds | ["3 2 3 4", "8 8 20 8 10"] | NoteIn the first example the balances change as follows: 1 2 3 4 $$$\rightarrow$$$ 3 3 3 4 $$$\rightarrow$$$ 3 2 3 4 $$$\rightarrow$$$ 3 2 3 4In the second example the balances change as follows: 3 50 2 1 10 $$$\rightarrow$$$ 3 0 2 1 10 $$$\rightarrow$$$ 8 8 8 8 10 $$$\rightarrow$$$ 8 8 20 8 10 | Java 11 | standard input | [
"data structures",
"binary search",
"sortings",
"brute force"
] | 7b788c660fb8ca703af0030f4c84ce96 | The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^{5}$$$) — the numer of citizens. The next line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_{i} \le 10^{9}$$$) — the initial balances of citizens. The next line contains a single integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^{5}$$$) — the number of events. Each of the next $$$q$$$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($$$1 \le p \le n$$$, $$$0 \le x \le 10^{9}$$$), or 2 x ($$$0 \le x \le 10^{9}$$$). In the first case we have a receipt that the balance of the $$$p$$$-th person becomes equal to $$$x$$$. In the second case we have a payoff with parameter $$$x$$$. | 1,600 | Print $$$n$$$ integers — the balances of all citizens after all events. | standard output | |
PASSED | e8ec299cdfe826632c4d6aef9f15fca6 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | public class C {
public Object solve () {
int N = sc.nextInt();
long [] B = sc.nextLongs();
long [] res = new long [N];
int M = N/2;
for (int i : rep(M))
res[M+i] = B[M-1-i];
for (int i : rep(1, M)) {
int j = N-1-i;
if (res[i] < res[i-1]) {
long D = res[i-1] - res[i];
res[i] += D;
res[j] -= D;
}
if (res[j] > res[j+1]) {
long D = res[j] - res[j+1];
res[j] -= D;
res[i] += D;
}
}
return exit(res);
}
private static final boolean ONE_TEST_CASE = true;
private static void init () {
}
private static int [] rep (int N) { return rep(0, N); }
private static int [] rep (int S, int T) { if (S >= T) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
//////////////////////////////////////////////////////////////////////////////////// OFF
private static IOUtils.MyScanner sc = new IOUtils.MyScanner();
private static Object print (Object o, Object ... A) { IOUtils.print(o, A); return null; }
private static Object exit (Object o, Object ... A) { print(o, A); IOUtils.exit(); return null; }
private static class IOUtils {
public static class MyScanner {
public String next () { newLine(); return line[index++]; }
public int nextInt () { return Integer.parseInt(next()); }
public String nextLine () { line = null; return readLine(); }
public String [] nextStrings () { return split(nextLine()); }
public long[] nextLongs () { return nextStream().mapToLong(Long::parseLong).toArray(); }
//////////////////////////////////////////////
private boolean eol () { return index == line.length; }
private String readLine () {
try {
return r.readLine();
} catch (Exception e) {
throw new Error (e);
}
}
private final java.io.BufferedReader r;
private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); }
private MyScanner (java.io.BufferedReader r) {
try {
this.r = r;
while (!r.ready())
Thread.sleep(1);
start();
} catch (Exception e) {
throw new Error(e);
}
}
private String [] line;
private int index;
private void newLine () {
if (line == null || eol()) {
line = split(readLine());
index = 0;
}
}
private java.util.stream.Stream<String> nextStream () { return java.util.Arrays.stream(nextStrings()); }
private String [] split (String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
}
private static String build (Object o, Object ... A) { return buildDelim(" ", o, A); }
private static String buildDelim (String delim, Object o, Object ... A) {
StringBuilder b = new StringBuilder();
append(b, o, delim);
for (Object p : A)
append(b, p, delim);
return b.substring(delim.length());
}
//////////////////////////////////////////////////////////////////////////////////
private static final java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########");
private static void start () { if (t == 0) t = millis(); }
private static void append (java.util.function.Consumer<Object> f, java.util.function.Consumer<Object> g, final Object o) {
if (o.getClass().isArray()) {
int len = java.lang.reflect.Array.getLength(o);
for (int i = 0; i < len; ++i)
f.accept(java.lang.reflect.Array.get(o, i));
}
else if (o instanceof Iterable<?>)
((Iterable<?>)o).forEach(f::accept);
else
g.accept(o instanceof Double ? formatter.format(o) : o);
}
private static void append (final StringBuilder b, Object o, final String delim) {
append(x -> { append(b, x, delim); }, x -> b.append(delim).append(x), o);
}
private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
private static void print (Object o, Object ... A) {
pw.println(build(o, A));
if (DEBUG)
pw.flush();
}
private static void err (Object o, Object ... A) { System.err.println(build(o, A)); }
private static boolean DEBUG;
private static void write (Object o) {
err(o, '(', time(), ')');
if (!DEBUG)
pw.println(o);
}
private static void exit () {
IOUtils.pw.close();
System.out.flush();
err("------------------");
err(time());
System.exit(0);
}
private static long t;
private static long millis () { return System.currentTimeMillis(); }
private static String time () { return "Time: " + (millis() - t) / 1000.0; }
private static void run (int N) {
try { DEBUG = System.getProperties().containsKey("DEBUG"); }
catch (Throwable t) {}
for (int n = 1; n <= N; ++n) {
Object res = new C().solve();
if (res != null)
write("Case #" + n + ": " + build(res));
}
exit();
}
}
////////////////////////////////////////////////////////////////////////////////////
public static void main (String[] args) {
init();
int N = ONE_TEST_CASE ? 1 : sc.nextInt();
IOUtils.run(N);
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 8afb0851ad23b5208551c29e23fff7a0 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import javafx.util.Pair;
public class CodeForce {
static boolean flag = false;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n=Integer.parseInt(br.readLine());
long[] arr=new long[n/2];
String[] sr=br.readLine().split(" ");
for(int i=0;i<n/2;i++)
{
arr[i]=Long.parseLong(sr[i]);
}
long[] org=new long[n];
long min=0;
long max=Long.MAX_VALUE;
for(int k=0;k<n/2;k++) {
org[k]=Math.max(min, arr[k]-max);
org[n-k-1]=arr[k]-org[k];
min=org[k];
max=org[n-k-1];
}
for(int i=0;i<n;i++)
System.out.print(org[i]+" ");
}}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 9a919fbe5a7194847e953557db27836c | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class A_GENERAL {
static long seive_size = (long) 1e6;
static ArrayList<Integer> primes = new ArrayList<>();
static boolean[] set = new boolean[(int) seive_size+1];
static ArrayList<Integer>[] adj;
static boolean[] visited;
public static void main(String[] args) {
MyScanner sc = new MyScanner();
int n = sc.nextInt();
long[] b = new long[(int)n/2];
long[] a = new long[(int)n];
long max = -1;
for(int i = 0; i < n/2; i++) {
b[i] = sc.nextLong();
if(max == -1 || b[i] <= max) {
if(i == 0) a[i] = 0;
else a[i] = a[i-1];
a[n-i-1] = b[i]-a[i];
max = a[n-i-1];
}
else {
a[i] = b[i]-max;
a[i] = Math.max(a[i], a[i-1]);
a[n-i-1] = b[i]-a[i];
max = a[n-1-i];
}
}
for(int i = 0; i < n; i++)
System.out.print(a[i] + " ");
}
public static class Pair {
long f;
long s;
Pair(long f, long s) {
this.f = f;
this.s = s;
}
}
// print string "s" multiple times
// prefer to use this function for multiple printing
public static String mp(String s, int times) {
return String.valueOf(new char[times]).replace("\0", s);
}
// take log with base 2
public static long log2(long k) {
return 63-Long.numberOfLeadingZeros(k);
}
// using lambda function for sorting
public static void lambdaSort(long[][] arr) {
Arrays.sort(arr, (a, b) -> Double.compare(a[0], b[0]));
}
// (n choose k) = (n/k) * ((n-1) choose (k-1))
public static long choose(long n, long k) {
return (k == 0) ? 1 : (n*choose(n-1, k-1))/k;
}
// just for keeping gcd function for other personal purposes
public static long gcd(long a, long b) {
return (a == 0) ? b : gcd(b%a, a);
}
public static long max(long... as) {
long max = Long.MIN_VALUE;
for (long a : as) max = Math.max(a, max);
return max;
}
public static long min(int... as) {
long min = Long.MAX_VALUE;
for (long a : as) min = Math.min(a, min);
return min;
}
// ======================= binary search (lower and upper bound) =======================
public static int lowerBound(long[] a, int x) {
int lo = 0;
int hi = a.length-1;
int ans = -1;
while(lo <= hi) {
int mid = (lo+hi)/2;
if(x < a[mid]) {
hi = mid-1;
} else if(x > a[mid]) {
lo = mid+1;
} else if(lo != hi) {
hi = mid-1; // for first occurrence
ans = mid;
} else {
return mid;
}
}
return ans;
}
public static int upperBound(long[] a, long x) {
int lo = 0;
int hi = a.length-1;
int ans = -1;
while(lo <= hi) {
int mid = (lo+hi)/2;
if(x < a[mid]) {
hi = mid-1;
} else if(x > a[mid]) {
lo = mid+1;
} else if(lo != hi) {
lo = mid+1; // for last occurrence
ans = mid;
} else {
return mid;
}
}
return ans;
}
public static void generatePrimes() {
// set.add(0);
// set.add(1);
Arrays.fill(set, true);
set[0] = false;
set[1] = false;
for(int i = 2; i <= seive_size; i++) {
if(set[i]) {
for(long j = (long) i*i; j <= seive_size; j+=i) set[(int)j] = false;
primes.add(i);
}
}
}
public static boolean isPrime(long N) {
if(N <= seive_size) return set[(int)N];
for (int i = 0; i < (int)primes.size(); i++)
if (N % primes.get(i) == 0) return false;
return true;
}
// ================ Permutation of String ====================
public static void permute(String str) {
permute(str, 0, str.length()-1);
}
public static void permute(String str, int l, int r)
{
if (l == r)
System.out.println(str);
else
{
for (int i = l; i <= r; i++)
{
str = swap(str,l,i);
permute(str, l+1, r);
str = swap(str,l,i);
}
}
}
public static String swap(String a, int i, int j)
{
char temp;
char[] charArray = a.toCharArray();
temp = charArray[i] ;
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(charArray);
}
// Union-find
void makeSet(int parent[], int rank[]) {
int i;
for(i = 0; i < parent.length; i++) {
parent[i] = i;
rank[i] = 0;
}
}
public static int find(int u, int parent[]) {
if(parent[u] == u) return u;
int v = find(parent[u], parent);
parent[u] = v;
return v;
}
public static boolean connected(int u, int v, int[] parent) {
return find(u, parent) == find(v, parent);
}
public static void Union(int u, int v, int rank[], int parent[]) {
int x = find(u, parent); //root of u
int y = find(v, parent); //root of v
if(x == y) return;
if(rank[x] == rank[y]) {
parent[y] = x;
rank[x]++;
}
else if(rank[x] > rank[y]) {
parent[y] = x;
}
else {
parent[x] = y;
}
}
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;}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
int[] nextIntArray(int n, int delta) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt() + delta;
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
} | Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 66d29d30f2fa673d6f82743ee7503808 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
long[] tmp = new long[n / 2];
long arr[] = new long[n];
for(int i = 0;i < n / 2;i++)
tmp[i] = sc.nextLong();
arr[0] = 0;
arr[n - 1] = tmp[0];
for(int i = 1;i < n / 2;i++) {
long curMin = arr[i - 1];
if(tmp[i] - curMin > arr[n - i]) {
arr[n - i - 1] = arr[n - i];
arr[i] = tmp[i] - arr[n - i - 1];
} else {
arr[n - i - 1] = tmp[i] - curMin;
arr[i] = curMin;
}
}
for(int i = 0;i < n;i++)
out.print(arr[i] + " ");
out.flush();
out.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
Scanner(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
String nextLine() throws IOException {
return br.readLine();
}
boolean ready() throws IOException {
return br.ready();
}
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 658fe3c3d7e52b98e6e5298a0a118daa | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import sun.applet.Main;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by jai on 15/12/18.
*/
public class p1093c {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long a[] = new long[n];
long min = 0;
long max = Long.MAX_VALUE;
for (int i = 0; i < n/2; i++) {
long sum = sc.nextLong();
a[n-1-i] = Math.min(max , sum - min );
max = a[n - 1 - i];
a[i] =sum - a[n - 1 - i];
min = a[i];
}
for (int i = 0; i < n; i++) {
System.out.println(a[i]);
}
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 2e9a95668ebdbb52701d941c5b07171a | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
public class _c7 {
public static void main(String args[]){
PrintWriter out = new PrintWriter(System.out);
Reader in = new Reader();
int n = in.nextInt();
long a[] = new long[n];
long b[] = new long[n/2];
for(int i=0;i<n/2;i++){
b[i] = in.nextLong();
}
a[0] = 0;
a[n-1] = b[0];
for(int i=1;i<n/2;i++){
long x = b[i] - a[n-i] - a[i-1];
if(x<0){
x = 0;
}
a[i] = a[i-1] + x;
a[n-1-i] = b[i] - a[i];
}
for(int i=0;i<n;i++){
out.print(a[i]+" ");
}
out.flush();
out.close();
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String next() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
double nextDouble()
{
return Double.parseDouble(next());
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 3d74b2655ea9359e34696c4d32e61b49 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main implements Runnable {
int maxn = (int)1e5+11;
int inf = (int)1e9;
long mod = (long)1e9+7;
int n,m,k;
int a[] = new int[n];
void solve() throws Exception {
n = in.iInt();
long a[] = new long[n+1];
long b[] = new long[n+1];
for (int i=1; i<=n/2; i++) {
b[i] = in.lLong();
}
a[n/2] = b[n/2]/2;
a[n/2+1] = b[n/2]-a[n/2];
for (int i=n/2-1; i>=1; i--) {
long val = b[i];
a[n-i+1] = a[n-i];
val-=a[n-i+1];
if (val>0L) {
if (val>a[i+1]) {
a[i] = a[i+1];
a[n-i+1] += val-a[i];
} else {
a[i] = val;
}
}
}
for (int i=1; i<=n; i++) {
out.print(a[i] + " ");
}
}
class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return x == pair.x &&
y == pair.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
String fileInName = "";
static Throwable throwable;
public static void main (String [] args) throws Throwable {
Thread thread = new Thread(null, new Main(), "", (1 << 26));
thread.start();
thread.join();
thread.run();
if (throwable != null)
throw throwable;
}
FastReader in;
PrintWriter out;
public void run() {
try {
if (!fileInName.isEmpty()) {
in = new FastReader(new BufferedReader(new FileReader(fileInName+".in")));
out = new PrintWriter(new BufferedWriter(new FileWriter(fileInName+".out")));
} else {
in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
}
solve();
} catch(Exception e) {
throwable = e;
} finally {
out.close();
}
}
class FastReader {
BufferedReader bf;
StringTokenizer tk = null;
public FastReader(BufferedReader bf) {
this.bf = bf;
}
public Integer iInt() throws Exception {
return Integer.parseInt(sString());
}
public Long lLong() throws Exception {
return Long.parseLong(sString());
}
public Double dDouble() throws Exception {
return Double.parseDouble(sString());
}
public String sString () throws Exception {
if (tk==null || !tk.hasMoreTokens()) {
tk = new StringTokenizer(bf.readLine());
}
if (!tk.hasMoreTokens()) return sString();
else
return tk.nextToken();
}
}
} | Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | ff6cbed1e8c06c8e4b90ee7d01aea6ca | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class JavaApplication7 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int n;
long k,t = 0;
Scanner in=new Scanner(System.in);
n = in.nextInt();
List<Long> list = new ArrayList();
for (int i=0; i<n; i++)
list.add(t);
k = in.nextLong();
list.set(0, t);
list.set(n-1, k);
t = k;
for (int i=1; i<n/2; i++)
{
k = in.nextLong();
while (true){
if (k-t >= list.get(i-1)){
list.set(n-i-1, t);
list.set(i, k-t);
break;
}
t = k - list.get(i-1);
}
}
for (int i =0; i<n; i++)
System.out.print(list.get(i) + " ");
System.out.println();
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | f682bd29e8298140fabadd556cd340f4 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class JavaApplication7 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int n;
long k,t = 0;
Scanner in=new Scanner(System.in);
n = in.nextInt();
List<Long> list = new ArrayList();
for (int i=0; i<n; i++)
list.add(t);
k = in.nextLong();
list.set(0, t);
list.set(n-1, k);
t = k;
for (int i=1; i<n/2; i++)
{
k = in.nextLong();
if (k-t < list.get(i-1))
t = k - list.get(i-1);
list.set(n-i-1, t);
list.set(i, k-t);
}
for (int i =0; i<n; i++)
System.out.print(list.get(i) + " ");
System.out.println();
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 654354fbe702e8287c9bba9a2e7944c4 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args)
{
FastReader in = new FastReader();
int n = in.nextInt();
long[] ans = new long[n];
for(int i = 0; i < n/2; i++)
{
if(i != 0)
{
long last = ans[ans.length-i];
long next = in.nextLong();
long dif = Math.max(next-last,ans[i-1]);
if(dif > 0)
{
ans[ans.length-1-i] = next-dif;
ans[i] = dif;
}
else
{
ans[ans.length-1-i] = next;
}
}
else
{
ans[ans.length-1] = in.nextLong();
}
System.out.print(ans[i] + " ");
}
for(int i = n/2; i < n; i++)
System.out.print(ans[i] + " ");
/*String str = "" + ans[0];
for(int i = 1; i < ans.length; i++)
str += " " + ans[i];
System.out.println(str);*/
}
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\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 9cb6b95689e861563b334f49da20a85f | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.util.*;
public class C1093{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int t = n/2;
long b[] = new long[t];
for(int i = 0;i<t;i++){
b[i] = sc.nextLong();
}
long a[] = new long[n];
for(int i = t-1;i>=0;i--){
// for(int j = t-1;j>=0;j--){
long temp = (b[i])/2;
if(i == (t-1)){
a[i] = temp;
a[i+1] = b[i]-temp;
}
else{
a[i] = temp;
a[n-i-1] = b[i]-temp;
if(a[i]>a[i+1]){
long temp1 = a[i]-a[i+1];
a[i] = a[i]-temp1;
a[n-i-1] += temp1;
}
if(a[n-i-1]<a[n-i-2]){
long temp1 = a[n-i-2]-a[n-i-1];
a[n-i-1] += temp1;
a[i] -= temp1;
}
}
}
// }
for(int i = 0;i<n;i++){
System.out.print(a[i]+" ");
}
}
} | Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | deeaa81c0a6250a5ff981803c8fab0b8 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.net.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class cf
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
long[] a=new long[n];
long c=0;
long t=in.nextLong();
a[n-1]=t;
a[0]=0;
for(int i=n-2;i>=n/2;i--)
{
t=in.nextLong();
a[i]=Math.min(t,a[i+1]);
a[n-i-1]=t-a[i];
if(a[n-i-1]<a[n-i-2])
{
t=a[n-i-2]-a[n-i-1];
a[n-i-1]=a[n-i-2];
a[i]-=t;
}
}
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
}
} | Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | e1731907ad75fee3743e30ec90007c95 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | //package Codeforces;
import java.util.*;
public class exam {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long[] arr = new long[n / 2];
for (int i = 0; i < n / 2; i++)
arr[i] = in.nextLong();
long[] ans = new long[n];
for (int i = 0; i < n / 2; i++) {
ans[i] = 0;
ans[n - 1 - i] = arr[i];
if (i == 0)
continue;
else {
if (ans[n - 1 - i] > ans[n - i]) {
ans[n - 1 - i] -= arr[i] - ans[n - i];
ans[i] = arr[i] - ans[n - 1 - i];
}
if (ans[i] < ans[i - 1]) {
ans[i] = ans[i - 1];
ans[n - 1 - i] = arr[i] - ans[i];
}
}
}
for (long i : ans)
System.out.print(i + " ");
}
}
/*
* 10 55 50 51 49 30
*/ | Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 48f011a1f0f13e20ef5d561616bdd468 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
static ArrayList<Integer> a[];
static boolean[] v;
static class pair implements Comparable<pair>{
int a,b;
pair(int x,int y){
a=x;b=y;
}
public int compareTo(pair t){
if(t.a==this.a)
return this.b-t.b;
return this.a-t.a;
}
}
public static ArrayList<pair> bfs(String[] a,int r,int c){
ArrayList<pair> ans=new ArrayList<>();
Queue<pair> q=new LinkedList<>();
int[][] dxy={{-1,0,1,0},{0,-1,0,1}};
q.add(new pair(r,c));
HashSet<String> h=new HashSet<>();
while(!q.isEmpty()){
pair f=q.poll();
ans.add(f);h.add(f.a+" "+f.b);
for(int i=0;i<4;i++){
int dx=f.a+dxy[0][i];
int dy=f.b+dxy[1][i];
if(dx<0||dy<0||dx>=a.length||dy>=a.length||h.contains(dx+" "+dy)||
a[dx].charAt(dy)=='1')
continue;
q.add(new pair(dx,dy));
h.add(dx+" "+dy);
}
}
return ans;
}
public static void dfs(ArrayList<Integer> a[],boolean[] v,int n,ArrayList<Integer> l){
v[n]=true;
l.add(n);
for(int i=0;i<a[n].size();i++){
if(!v[a[n].get(i)]){
dfs(a,v,a[n].get(i),l);
}
}
}
/*
1 1 1 1 1
3 3 3
2 3 3 2 1 3
1
2 2 2 2
4 4 4 1 4
*/
public static int pow(int a, int n) {
long ans = 1;
long base = a;
while (n != 0) {
if ((n & 1) == 1) {
ans *= base;
ans %= 1000000007;
}
base = (base * base) % 1000000007;
n >>= 1;
}
return (int) ans % 1000000007;
}
public static int find(int x){
int i=1;int f=0;
while(x>0){
if((x&1)==0)f=i;
x>>=1;i++;
}
return f;
}
public static boolean good(int x){
if((((x+1)&x))==0)
return true;
return false;
}
public static long f(long n){
long val=2;
long preval=0;int i=1;
while(n>=val){
i++;
preval=val;
val=3*i+val-1;
}
return preval;
}
public static void bfs(HashSet<Integer>[] a,int n){
PriorityQueue<Integer> p=new PriorityQueue<>();
boolean v[]=new boolean[n];
v[0]=true;
p.add(0);
StringBuilder st=new StringBuilder();
while(!p.isEmpty()){
int f=p.poll();
v[f]=true;
st.append(f+1+" ");
for(int i:a[f]){
if(!v[i]){
p.add(i);v[i]=true;}
}
}
System.out.println(st);
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
// int t = s.nextInt();
// while(t-- > 0) {
int n=s.nextInt();
long[]b=new long[n/2];
for(int i=0;i<n/2;i++){
b[i]=s.nextLong();
}
long a[]=new long[n];
a[n-1]=b[0];
for(int i=1;i<=n/2-1;i++){
if(b[i]>b[i-1]){
a[n-i-1]=a[n-i];
a[i]=b[i]-a[n-i-1];
}else{
a[i]=a[i-1];
a[n-i-1]=b[i]-a[i];
}
}
StringBuilder st=new StringBuilder();
for(int i=0;i<n;i++)
st.append(a[i]+" ");
System.out.println(st);
// }
}
} | Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 2fd3444f230b547babfb3aa308f5e858 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.util.*;
import java.util.Map.Entry;
import java.lang.*;
import java.math.*;
import java.text.*;
import java.io.*;
public final class Solve {
static PrintWriter out = new PrintWriter(System.out);
static void flush() {
out.flush();
}
static void run(long s, long e) {
NumberFormat formatter = new DecimalFormat("#0.00000");
System.out.print("Execution time is " + formatter.format((e - s) / 1000d) + " seconds");
}
static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
static boolean isPalindrome(String str1, String str2) {
String str3 = str1+str2;
int i = 0, j = str3.length()-1;
while(i < j) {
char a = str3.charAt(i), b = str3.charAt(j);
if(a != b) return false;
i++;j--;
}
return true;
}
static boolean isPalindrome(String str) {
int i = 0, j = str.length()-1;
while(i < j) {
char a = str.charAt(i), b = str.charAt(j);
if(a != b) return false;
i++;j--;
}
return true;
}
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());}
static int fact(int n) {
if(n == 1) return 1;
return n * fact(n-1);
}
public int[] readIntArray(int n) {
int[] arr = new int[n];
for(int i=0; i<n; ++i)
arr[i]=nextInt();
return arr;
}
public long[] readLongArray(int n) {
long[] arr = new long[n];
for(int i=0; i<n; ++i)
arr[i]=nextLong();
return arr;
}
public int[][] readIntArray(int m, int n){
int[][] arr = new int[m][n];
for(int i = 0;i<m;i++)
for(int j = 0;j<n;j++)
arr[i][j] = nextInt();
return arr;
}
public String[] readStringArray(int n) {
String[] arr = new String[n];
for(int i=0; i<n; ++i)
arr[i]= nextLine();
return arr;
}
double nextDouble() {return Double.parseDouble(next());}
String nextLine() {
String str = "";
try{str = br.readLine();}
catch (IOException e) {e.printStackTrace();}
return str;}
}
static void solve(long[] arr, int n){
long[] t = new long[n];
int p = 0;
for(int i =n-1;i >=n/2;i--) {
t[i] = arr[p++];
}
// for(int i = 0;i<t.length;i++) {
// out.print(t[i]+" ");
// }
// out.println();
int i = 1;
int j = n-2;
long max1 = t[n-1];
long max2 = 0;
while(i < j) {
if(t[j] > t[j+1]) {
if(t[j] - t[j+1] == t[i-1] - t[i]) {
t[j] = t[j+1];
t[i] = t[i-1];
}
else if(t[j] - t[j+1] < t[i-1] - t[i]) {
t[i] = t[i-1];
t[j] = t[j] - t[i];
}
else {
t[i] = t[j] - t[j+1];
t[j] = t[j] - t[i];
}
max2 = t[i];
max1 = t[j];
}
else if(t[j] == t[j+1] && t[i] != t[i-1]) {
t[i] = max2;
t[j] = t[j] - t[i];
max1 = t[j];
}
else {
long x = t[j];
t[i] = max2;
t[j] = x - t[i];
max1 = t[j];
}
// out.println(t[i]+" "+t[j]);
// out.println(max2+" "+max1);
i++;
j--;
}
for(long k:t) {
out.print(k+" ");
}
}
public static void main(String args[]) throws IOException {
FastReader sc = new FastReader();
long s1 = System.currentTimeMillis();
int n = sc.nextInt();
long[] arr = sc.readLongArray(n/2);
solve(arr,n);
flush();
long e = System.currentTimeMillis();
// run(s1,e);
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 8552f4b90025b07e468b195cd9b8a746 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static long[] f(long l,long r, long c) {
long a = l;
long b = r;
long[] p = new long[2];
p[0] = a;
p[1] = b;
if (a + b == c) return p;
if ((a + b) < c) {
a = c - b;
} else {
b = c - a;
}
p[0] = a;
p[1] = b;
return p;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long[] a = new long[n];
long[] b = new long[n/2];
for (int i = 0; i < n/2; i++) {
b[i] = in.nextLong();
}
long[] p = new long[2];
a[0] = 0;
a[n - 1] = b[0];
p[0] = 0;
p[1] = b[0];
for (int i = 1; i < n/2; i++) {
p = f(p[0],p[1],b[i]);
a[i] = p[0];
a[n - i - 1] = p[1];
}
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
} | Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 065a2b01af23b526b102086f462bb5c3 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]) {
MyScanner in = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
// System.out.println(Long.MAX_VALUE);
int n = in.nextInt();
int mid = n / 2;
long a[] = new long[n];
for(int i = 0; i < mid; i++) {
a[i] = 0;
a[n-i-1] = in.nextLong();
}
long dl, dr;
for (int i=1;i<mid;i++) {
dl = 0;
dr = 0;
if (a[i-1] > a[i]) {
dl = a[i-1] - a[i];
}
if (a[n-i-1] > a[n-i]) {
dr = a[n-i-1] - a[n-i];
}
if (dl > dr) {
a[i] = a[i] + dl;
a[n-i-1] = a[n-i-1] - dl;
} else {
a[i] = a[i] + dr;
a[n-i-1] = a[n-i-1] - dr;
}
}
for(int i = 0; i < n; i++) {
out.print(a[i]);
out.print(" ");
}
out.println();
out.close();
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 4564b84ab3740920f5f1d729a64861ea | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Solution {
public static long[] toLongArray(String s) {
String nums[] = s.split("\\s");
return IntStream.range(0, nums.length)
.mapToLong(index -> Long.parseLong(nums[index]))
.toArray();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
long[] a = new long[n];
long[] b = toLongArray(scanner.nextLine());
a[0] = 0;
for(int i = 0; i < b.length; i++) {
if(i == 0) {
a[0] = 0;
a[n - 1] = b[0];
}
else {
a[n - 1 - i] = Math.min(b[i], a[n - i]);
a[i] = b[i] - a[n - 1 - i];
if(a[i] < a[i - 1]) {
a[i] = a[i - 1];
a[n - 1 - i] = b[i] - a[i];
}
}
}
System.out.println(Arrays.stream(a)
.boxed()
.map(e -> e.toString())
.collect(Collectors.joining(" ")));
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | f62b8f8941de9146a8cbae82a7cb36fe | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import org.omg.CORBA.MARSHAL;
import java.awt.event.InputEvent;
import java.awt.image.AreaAveragingScaleFilter;
import java.io.*;
import java.net.CookieHandler;
import java.util.*;
import java.math.*;
import java.lang.*;
public class Main implements Runnable {
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new Main(),"Main",1<<26).start();
}
static long gcd(long a,long b){ if(b==0)return a;return gcd(b,a%b); }
static long modPow(long a,long p,long m){ if(a==1)return 1;long ans=1;while (p>0){ if(p%2==1)ans=(ans*a)%m;a=(a*a)%m;p>>=1; }return ans; }
static long modInv(long a,long m){return modPow(a,m-2,m);}
public void run() {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n=sc.nextInt();
int m=n/2;
long b[]=new long[m];
for (int i = 0; i <m; i++) {
b[i]=sc.nextLong();
}
long a[]=new long[n];
a[n-1]=b[0];
int cc=1;
for (int i = n-2; i >=n/2 ; i--) {
a[i]=Math.min(a[i+1],b[cc]);
a[n-i-1]=b[cc]-a[i];
if(a[n-i-1]<a[n-i-1-1]){
a[n-i-1]=a[n-i-1-1];
a[i]=b[cc]-a[n-i-1];
}
cc++;
}
for (int i = 0; i <n ; i++) {
out.print(a[i]+" ");
}
out.close();
}
} | Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 168b767f8fba04943c9c79b69394da16 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes |
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class q54 {
static final double EPS = 1e-9;
static pair[] x = new pair[3];
static int ac1 = -1;
static int ac2 = -1;
static int noox = -1;
static int nooy = -1;
static int same = -1;
static int grid[][] = new int[1001][1001];
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long x[] = new long[n / 2];
for (int i = 0; i < n / 2; i++) {
x[i] = sc.nextlong();
}
long y[] = new long[n];
long k = x[(n / 2) - 1];
long l = k/2, r = k/2;
if (k % 2 == 0) {
l = k/2; r = k/2;
} else {
l = k/2; r = (k/2)+1;
}
for (int i = 0; i < n/2; i++) {
if (i == 0) {
y[(n / 2) - 1] = l;
y[(n / 2)] = r;
} else {
long z = x[(n/2)-1-i];
if(z==(r+l)) {
y[(n / 2) + i] = r;
y[(n / 2) -1- i] = l;
}else if(z>(r+l)) {
y[(n / 2) + i] = r+(z-l-r);
y[(n / 2)-1 - i] = l;
r= r+(z-l-r);
}else if(z<(r+l)) {
y[(n / 2)+ i] = r;
y[(n / 2) -1- i] = l-(l+r-z);
l= l-(l+r-z);
}
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(y[i] + " ");
}
System.out.println(sb.toString());
}
static boolean tsamever() {
if (x[0].x == x[1].x) {
ac1 = x[0].y;
ac2 = x[1].y;
noox = x[2].y;
nooy = x[2].x;
same = x[0].x;
return true;
}
if (x[2].x == x[1].x) {
same = x[1].x;
ac1 = x[2].y;
ac2 = x[1].y;
noox = x[0].y;
nooy = x[0].x;
return true;
}
if (x[0].x == x[2].x) {
same = x[0].x;
ac1 = x[0].y;
ac2 = x[2].y;
noox = x[1].y;
nooy = x[1].x;
return true;
}
return false;
}
public static class pair implements Comparable<pair> {
int x, y;
pair(int a, int b) {
x = a;
y = b;
}
public int compareTo(pair p) {
if (Math.abs(x - p.x) > EPS)
return x > p.x ? 1 : -1;
if (Math.abs(y - p.y) > EPS)
return y > p.y ? 1 : -1;
return 0;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public Scanner(String file) throws FileNotFoundException {
br = new BufferedReader(new FileReader(file));
}
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 long nextlong() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
long 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 boolean ready() throws IOException {
return br.ready();
}
}
} | Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 2c6fe24f2fa1615d08f04eb12e9876d4 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main extends Thread {
boolean[] prime;
FastScanner sc;
PrintWriter pw;
long startTime = System.currentTimeMillis();
final class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
public long nlo() {
return Long.parseLong(next());
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
return st.nextToken();
}
public int ni() {
return Integer.parseInt(next());
}
public String nli() {
String line = "";
if (st.hasMoreTokens()) line = st.nextToken();
else try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
while (st.hasMoreTokens()) line += " " + st.nextToken();
return line;
}
public double nd() {
return Double.parseDouble(next());
}
}
public Main(ThreadGroup t,Runnable r,String s,long d )
{
super(t,r,s,d);
}
public void run()
{
sc=new FastScanner();
pw=new PrintWriter(System.out);
solve();
pw.flush();
pw.close();
}
public static void main(String[] args)
{
new Main(null,null,"",1<<25).start();
}
/////////////------------------------------------//////////////
////////////------------------Main-Logic--------//////////////
///////////-------------------------------------//////////////
public void solve() {
int t=1;
while(t-->0)
{
int n=sc.ni();
long[] arr=new long[n];
for(int i=n-1;i>=(n/2);i--)
arr[i]=sc.nlo();
for(int i=n-2;i>=(n/2);i--)
{
int j=n-1-i;
long y=Math.min(arr[i],arr[i+1]);
long z=Math.max(arr[i]-y,arr[j-1]);
arr[j]=z;
arr[i]=arr[i]-z;
}
for(int i=0;i<n;i++)
pw.print(arr[i]+" ");
}
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 6fded10eaf6bbdfe707f406963ad7082 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes |
import java.math.BigInteger;
import java.util.Scanner;
public class TestClass1 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String name = s.nextLine();
int t = Integer.parseInt(name);
name = s.nextLine();
String[] spl = name.split(" ");
BigInteger[] b = new BigInteger[t/2];
BigInteger[] a = new BigInteger[t];
for(int i =0;i<t/2;i++)
{
b[i] = new BigInteger(spl[i]);
}
BigInteger inc =new BigInteger("0");
for(int i =0;i<t/2;i++) {
if(i!=0 && b[i].compareTo(a[t-i])==1) {
inc = b[i].subtract(a[t-i]);
if(a[i-1].compareTo(inc)==1) {
inc = a[i-1];
}
}
a[i] = inc;
a[t-1-i] = b[i].subtract(inc);
}
for(int i =0;i<t;i++)
{
System.out.print(a[i]);
if(i!=t-1) {
System.out.print(" ");
}
// System.out.println();
}
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 6a90a8dee5a7f701084fb1eeb3b53aef | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.util.*;
public class C{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int nb = sc.nextInt();
long tab[] = new long[nb];
long min=0;
long max=sc.nextLong();
tab[0]=min;
tab[nb-1]=max;
long encours;
for (int i=1;i<(nb/2);i++){
encours=sc.nextLong()-min;
if (encours>max){
min+=encours-max;
encours-= encours-max;
}
tab[i]=min;
tab[nb-i-1]=encours;
max=encours;
}
for (int i=0;i<nb;i++){
System.out.print(tab[i]+" ");
}
}
} | Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 7cd200d48e9b8b54188dc0343dec18dc | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class ProblemC {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
long [] nums = new long[n/2];
for (int i = 0; i < nums.length; i++) {
nums[i] = scanner.nextLong();
}
Arrays.stream(solve(nums)).forEach(a -> System.out.print(a + " "));
}
private static long [] solve(long [] nums) {
long [] results = new long[nums.length * 2];
long lastMax = nums[0];
long lastMin = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] - lastMax < 0) {
results[i] = lastMin;
results[results.length - i - 1] = nums[i] - lastMin;
lastMax = nums[i]- lastMin;
} else {
if (nums[i] - lastMax < lastMin) {
results[i] = lastMin;
results[results.length - i - 1] = nums[i] - lastMin;
lastMax = nums[i] - lastMin;
continue;
}
results[i] = nums[i] - lastMax;
results[results.length - i - 1] = lastMax;
lastMin = nums[i] - lastMax;
}
}
return results;
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | d2d5c37cf8706e08c1a720bcc618d949 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class ProblemC {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
long[] nums = new long[n / 2];
for (int i = 0; i < nums.length; i++) {
nums[i] = scanner.nextLong();
}
Arrays.stream(solve(nums)).forEach(a -> System.out.print(a + " "));
}
private static long[] solve(long[] nums) {
long[] results = new long[nums.length * 2];
long lastMax = nums[0];
long lastMin = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] - lastMax < lastMin) {
results[i] = lastMin;
results[results.length - i - 1] = nums[i] - lastMin;
lastMax = nums[i] - lastMin;
} else {
results[i] = nums[i] - lastMax;
results[results.length - i - 1] = lastMax;
lastMin = nums[i] - lastMax;
}
}
return results;
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | c55226990927540394cf98b28975b90d | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes |
import java.util.*;
import java.io.*;
import java.lang.reflect.Array;
import java.math.*;
public class Main {
static int N = (int)1e5 + 10;
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(System.in);
long[] b = new long[N];
long[][] a = new long[2][N];
int n = in.nextInt(); n /= 2;
for(int i = 0; i < n; i++)
b[i] = in.nextLong();
a[0][0] = 0; a[1][0] = b[0];
for(int i = 1; i < n; i++) {
if(b[i] <= b[i-1]) {
a[0][i] = a[0][i-1];
a[1][i] = b[i] - a[0][i];
} else {
a[1][i] = a[1][i-1];
a[0][i] = b[i] - a[1][i];
}
}
StringBuilder xp = new StringBuilder();
for(int i = 0; i < n; i++) xp.append(a[0][i] + " ");
for(int i = n-1; i >= 0; i--) xp.append(a[1][i] + " ");
System.out.println(xp.toString().trim());
in.close();
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | d55f07ff24bfeb5f066dc99c935243b4 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes |
import java.util.*;
import java.io.*;
import java.lang.reflect.Array;
import java.math.*;
public class Main {
static int N = (int)1e5 + 10;
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(System.in);
long[] b = new long[N];
long[][] a = new long[2][N];
int n = in.nextInt(); n /= 2;
for(int i = 0; i < n; i++)
b[i] = in.nextLong();
a[0][0] = 0; a[1][0] = b[0];
for(int i = 1; i < n; i++) {
if(b[i] <= b[i-1]) {
a[0][i] = a[0][i-1];
a[1][i] = b[i] - a[0][i];
} else {
a[1][i] = a[1][i-1];
a[0][i] = b[i] - a[1][i];
}
}
StringBuilder xp = new StringBuilder();
for(int i = 0; i < n; i++) xp.append(a[0][i] + " ");
for(int i = n-1; i >= 0; i--) xp.append(a[1][i] + " ");
System.out.println(xp);
in.close();
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | 9628782008d2ace8aa4e6c1d61dd9e8f | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes |
import java.util.*;
import java.io.*;
import java.lang.reflect.Array;
import java.math.*;
public class Main {
static int N = (int)1e5 + 10;
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(System.in);
long[] b = new long[N];
long[][] a = new long[2][N];
int n = in.nextInt(); n /= 2;
for(int i = 0; i < n; i++)
b[i] = in.nextLong();
a[0][0] = 0; a[1][0] = b[0];
for(int i = 1; i < n; i++) {
if(b[i] <= b[i-1]) {
a[0][i] = a[0][i-1];
a[1][i] = b[i] - a[0][i];
} else {
a[1][i] = a[1][i-1];
a[0][i] = b[i] - a[1][i];
}
}
for(int i = 0; i < n; i++) System.out.print(a[0][i] + " ");
for(int i = n-1; i >= 0; i--) System.out.print(a[1][i] + " ");
System.out.println();
in.close();
}
}
| Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | e5503161d4a07d1d64bf5426998a9896 | train_000.jsonl | 1544884500 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \le a_2 \le \dots \le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 256 megabytes | import java.util.*;
public class C {
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long[]b=new long[n/2];
long[]b2=new long[n/2];
for(int i=0;i<n/2;i++)
b[i]=sc.nextLong();
long r=Long.MAX_VALUE,p=0;
for(int i=0;i<n/2;i++){
b2[i]=Math.min(r,b[i]-p);
b[i]-=b2[i];
r=b2[i];
p=b[i];
}
for(int i=0;i<n/2;i++)
System.out.print(b[i]+" ");
for(int i=n/2-1;i>=0;i--)
System.out.print(b2[i]+" ");
sc.close();
}
} | Java | ["4\n5 6", "6\n2 1 2"] | 2 seconds | ["2 3 3 3", "0 0 1 1 1 2"] | null | Java 8 | standard input | [
"greedy"
] | 4585419ab2b7200770cfe1e607161e9f | The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\frac n 2$$$ integers $$$b_1, b_2, \dots, b_{\frac n 2}$$$ ($$$0 \le b_i \le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. | 1,300 | Print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$) in a single line. $$$a_1 \le a_2 \le \dots \le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$. | standard output | |
PASSED | a2e1b1f566c831af83f4d806ef52dafd | train_000.jsonl | 1380295800 | You are given an array a1, a2, ..., an and m sets S1, S2, ..., Sm of indices of elements of this array. Let's denote Sk = {Sk, i} (1 ≤ i ≤ |Sk|). In other words, Sk, i is some element from set Sk.In this problem you have to answer q queries of the two types: Find the sum of elements with indices from set Sk: . The query format is "? k". Add number x to all elements at indices from set Sk: aSk, i is replaced by aSk, i + x for all i (1 ≤ i ≤ |Sk|). The query format is "+ k x". After each first type query print the required sum. | 256 megabytes | import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Alexandr Azizyan
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
}
class TaskE {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int q = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = in.nextInt();
final int C = (int) Math.sqrt(m);
long[] sum = new long[m];
int[] pos = new int[m];
ArrayList<Integer> big = new ArrayList<Integer>();
ArrayList<Integer>[] s = new ArrayList[m];
for (int i = 0; i < m; i++)
s[i] = new ArrayList<Integer>();
ArrayList<Integer>[] rs = new ArrayList[n];
for (int i = 0; i < n; i++)
rs[i] = new ArrayList<Integer>();
for (int i = 0; i < m; i++) {
int size = in.nextInt();
while (size-- > 0) {
int S = in.nextInt() - 1;
s[i].add(S);
rs[S].add(i);
sum[i] += a[S];
}
if (s[i].size() > C) {
pos[i] = big.size();
big.add(i);
} else
pos[i] = -1;
}
int[][] b = new int[big.size()][m];
for (int i = 0; i < big.size(); i++) {
int x = big.get(i);
for (int y : s[x])
for (int z : rs[y])
b[i][z]++;
}
long[] change = new long[big.size()];
long[] add = new long[n];
long[] d = new long[big.size()];
while (q-- > 0) {
String c = in.next();
int x = in.nextInt() - 1;
if (c.equals("?")) {
long ans = sum[x];
for (int i = 0; i < big.size(); i++)
ans += change[i] * b[i][x];
if (pos[x] > - 1)
ans += d[pos[x]];
else
for (int i : s[x])
ans += add[i];
out.println(ans);
} else {
int y = in.nextInt();
if (pos[x] == -1) {
for (int i : s[x])
add[i] += y;
for (int i = 0; i < big.size(); i++)
d[i] += (long) y * b[i][x];
} else
change[pos[x]] += y;
}
}
}
}
| Java | ["5 3 5\n5 -5 5 1 -4\n2 1 2\n4 2 1 4 5\n2 2 5\n? 2\n+ 3 4\n? 1\n+ 2 1\n? 2"] | 3 seconds | ["-3\n4\n9"] | null | Java 7 | standard input | [] | 9e1b04e8049eeb87060a0439e31530b5 | The first line contains integers n, m, q (1 ≤ n, m, q ≤ 105). The second line contains n integers a1, a2, ..., an (|ai| ≤ 108) — elements of array a. Each of the following m lines describes one set of indices. The k-th line first contains a positive integer, representing the number of elements in set (|Sk|), then follow |Sk| distinct integers Sk, 1, Sk, 2, ..., Sk, |Sk| (1 ≤ Sk, i ≤ n) — elements of set Sk. The next q lines contain queries. Each query looks like either "? k" or "+ k x" and sits on a single line. For all queries the following limits are held: 1 ≤ k ≤ m, |x| ≤ 108. The queries are given in order they need to be answered. It is guaranteed that the sum of sizes of all sets Sk doesn't exceed 105. | 2,500 | After each first type query print the required sum on a single line. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 999f1d5bce01b2e082001c94af01bd94 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
public class Codeforces{
private static long [] catalan= new long[2501];
private static final int MOD=1000000007;
void pre() throws Exception{}
void solve(int TC) throws Exception{
int t=ni();
while(t--!=0) {
//int a=ni();
int n=ni();
int odd=0;
int even=0;
int a[]=new int[2*n+1];
int odd_index=0;
int even_index=0;
for(int i=1;i<2*n+1;i++) {
a[i]=ni();
if(a[i]%2==0) {even++;even_index=i;}
else {odd++;
odd_index=i;
}
}
//pn(odd);
int count=0;
if(odd%2!=0) {
count=1;
odd--;
a[odd_index]=Integer.MAX_VALUE;
}
if(count==1) {
even--;
a[even_index]=Integer.MAX_VALUE;
count=2;
}
if(count==0) {
if(even==0) {
odd-=2;
int xx=0;
int i=1;
while(xx!=2) {
if(a[i]%2!=0&&a[i]!=Integer.MAX_VALUE) {
a[i]=Integer.MAX_VALUE;
xx++;
}
i++;
}
}
else {
even-=2;
int xx=0;
int i=1;
while(xx!=2) {
if(a[i]%2==0&&a[i]!=Integer.MAX_VALUE) {
a[i]=Integer.MAX_VALUE;
xx++;
}
i++;
}
}
}
while(even!=0) {
for(int i=1;i<2*n+1;i++) {
for(int j=i+1;j<2*n+1;j++) {
if(a[i]%2==0&&a[j]%2==0&&a[i]!=Integer.MAX_VALUE&&a[j]!=Integer.MAX_VALUE) {
pn(i + " " + j);
even-=2;
a[i]=Integer.MAX_VALUE;
a[j]=Integer.MAX_VALUE;
break;
}
if(even<=0)break;
}
if(even<=0)break;
}
}
while(odd!=0) {
for(int i=1;i<2*n+1;i++) {
for(int j=i+1;j<2*n+1;j++) {
if(a[i]%2!=0&&a[j]%2!=0&&a[i]!=Integer.MAX_VALUE&&a[j]!=Integer.MAX_VALUE) {
pn(i + " " + j);
odd-=2;
a[i]=Integer.MAX_VALUE;
a[j]=Integer.MAX_VALUE;
break;
}
if(odd<=0)break;
}
if(odd<=0)break;
}
}
}
}
// class TrieNode{
// TrieNode[] children=new TrieNode[26];
// boolean isEnd;
// TrieNode() {
// isEnd=false;
// for(int i=0;i<26;i++)
// children[i]=null;
// }
// }
//
// static TrieNode root;
//
// void insert(String s) {
// TrieNode pCrawl=root;
// int index=0;
// int len=s.length();
// for(int i=0;i<len;i++) {
// index=s.charAt(i)-'a';
// if(pCrawl.children[index]!=null)
// pCrawl.children[index]=new TrieNode();
// pCrawl = pCrawl.children[index];
//
// }
// pCrawl.isEnd= true;
//
// }
static boolean multipleTC = false, memory = false;
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();
}
public static void main(String[] args) throws Exception{
if(memory)new Thread(null, new Runnable() {public void run(){try{new Codeforces().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start();
else new Codeforces().run();
}
void p(Object o){out.print(o);}
void pn(Object o){out.println(o);}
void nd(Object o){out.println(o);}
void pni(Object o){out.println(o);out.flush();}
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());}
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 | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 21428f328b945ecd72dfbbf39bc9510b | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.Math;
public class Main{
static int maxDivisor(int n){
for (int i=n-1;i>=1;i--){
if(n%i==0){
return i;
}
}
return -1;
}
public static void main(String[] args){
FastScanner s = new FastScanner(System.in);
int T= s.nextInt();
while(T-->0){
int n=s.nextInt();
int[] a= new int[2*n];
int[] parity= new int[2*n];
int even=0;
int odd=0;
ArrayList<Integer> oddidx=new ArrayList<>();
ArrayList<Integer> evenidx=new ArrayList<>();
for(int i=0;i<2*n;i++){
a[i]=s.nextInt();
parity[i]=a[i]%2;
if(parity[i]==1){
oddidx.add(i);
}else{
evenidx.add(i);
}
even=even+1-parity[i];
odd=odd+parity[i];
}
// System.out.println`(evenidx);
// System.out.println(oddidx);
if(odd%2==0){
if(odd==0){
for(int i=0;i<n-1;i++){
System.out.println((1+evenidx.get(2*i))+" "+(1+evenidx.get(2*i+1)));
}
}else if(even==0){
for(int i=0;i<n-1;i++){
System.out.println((1+oddidx.get(2*i))+" "+(1+oddidx.get(2*i+1)));
}
}else{
oddidx.remove(oddidx.size()-1);
oddidx.remove(oddidx.size()-1);
for(int i=0;i<evenidx.size()/2;i++){
System.out.println((1+evenidx.get(2*i))+" "+(1+evenidx.get(2*i+1)));
}
for(int i=0;i<oddidx.size()/2;i++){
System.out.println((1+oddidx.get(2*i))+" "+(1+oddidx.get(2*i+1)));
}
}
}else{
evenidx.remove(evenidx.size()-1);
oddidx.remove(oddidx.size()-1);
for(int i=0;i<oddidx.size()/2;i++){
System.out.println((1+oddidx.get(2*i))+" "+(1+oddidx.get(2*i+1)));
}
for(int i=0;i<evenidx.size()/2;i++){
System.out.println((1+evenidx.get(2*i))+" "+(1+evenidx.get(2*i+1)));
}
}
// System.out.println("aaaaaaaaaaaa");
}
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new
InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
}
| Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | ffcc20870091f0b80f16d9455ebc50b3 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.util.*;
public class Main{
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[2*n];
ArrayList<Integer> listo = new ArrayList<Integer>();
ArrayList<Integer> liste = new ArrayList<Integer>();
for(int i=0; i<2*n; i++){
arr[i] = s.nextInt();
if(arr[i]%2==0)
liste.add(i+1);
else
listo.add(i+1);
}
int count = n-1;
// System.out.println("count: "+count);
if(listo.size()==0){
for(int i=0; i<liste.size()-1;i=i+2){
System.out.print(liste.get(i)+" "+liste.get(i+1));
System.out.println();
count--;
if(count==0)
break;
}
}else if(liste.size()==0){
for(int i=0; i<listo.size()-1;i=i+2){
System.out.print(listo.get(i)+" "+listo.get(i+1));
System.out.println();
count--;
if(count==0)
break;
}
} else if(listo.size()%2==0 && liste.size()%2==0){
for(int i=0; i<listo.size()-1; i=i+2){
System.out.print(listo.get(i)+" "+listo.get(i+1));
System.out.println();
count--;
if(count==0)
break;
}
if(count>0){
for(int i=0; i<liste.size()-1; i=i+2){
System.out.print(liste.get(i)+" "+liste.get(i+1));
System.out.println();
count--;
if(count==0)
break;
}
}
} else if(listo.size()%2!=0 && liste.size()%2!=0){
for(int i=0; i<listo.size()-2; i=i+2){
System.out.print(listo.get(i)+" "+listo.get(i+1));
System.out.println();
count--;
if(count==0)
break;
}
if(count>0){
for(int i=0; i<liste.size()-2; i=i+2){
System.out.print(liste.get(i)+" "+liste.get(i+1));
System.out.println();
count--;
if(count==0)
break;
}
// if(count==0)
// break;
}
}
// System.out.println();
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 87d45b0029f1e1db83be6f436a20806c | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scanner=new Scanner(System.in);
int t=scanner.nextInt();
while(t>0){
int flag=0;
ArrayList<Integer> a=new ArrayList<Integer>();
ArrayList<Integer> b=new ArrayList<Integer>();
int n=scanner.nextInt();
for(int i=1;i<=2*n;i++){
int k=scanner.nextInt();
if(k%2==0) a.add(i);
else b.add(i);
}
if(a.size()/2>=n-1) {
int j=0;
for(int i=0;i<n-1;i+=1){
System.out.println(a.get(j)+" "+a.get(j+1));
j+=2;
}
}
else {
if(a.size()%2==0){
flag=1;
int j=0;
for(int i=0;i<a.size()/2;i+=1){
System.out.println(a.get(j)+" "+a.get(j+1));
j+=2;
}
}
else{
int j=0;
for(int i=0;i<a.size()/2;i+=1){
System.out.println(a.get(j)+" "+a.get(j+1));
j+=2;
}
}
if(flag==1){
int j=0;
for(int i=0;i<(n-1)-(a.size())/2;i+=1){
System.out.println(b.get(j)+" "+b.get(j+1));
j+=2;
}
}
else{
int j=0;
for(int i=0;i<(n-1)-(a.size()-1)/2;i+=1){
System.out.println(b.get(j)+" "+b.get(j+1));
j+=2;
}
}
}
t-=1;
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 3a1d3792b38e75ef73a649ccb4ff4c42 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scanner=new Scanner(System.in);
int t=scanner.nextInt();
while(t>0){
int flag=0;
ArrayList<Integer> a=new ArrayList<Integer>();
ArrayList<Integer> b=new ArrayList<Integer>();
int n=scanner.nextInt();
for(int i=1;i<=2*n;i++){
int k=scanner.nextInt();
if(k%2==0) a.add(i);
else b.add(i);
}
if(a.size()/2>=n-1) {
int j=0;
for(int i=0;i<n-1;i+=1){
System.out.println(a.get(j)+" "+a.get(j+1));
j+=2;
}
}
else {
if(a.size()%2==0){
flag=1;
}
int k=0;
for(int i=0;i<a.size()/2;i+=1){
System.out.println(a.get(k)+" "+a.get(k+1));
k+=2;
}
int l=0;
for(int i=0;i<(n-1)-(a.size())/2;i+=1){
System.out.println(b.get(l)+" "+b.get(l+1));
l+=2;
}
}
t-=1;
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | e6421d821283bbb4d3905ef6cc36326a | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scanner=new Scanner(System.in);
int t=scanner.nextInt();
while(t>0){
int flag=0;
ArrayList<Integer> a=new ArrayList<Integer>();
ArrayList<Integer> b=new ArrayList<Integer>();
int n=scanner.nextInt();
for(int i=1;i<=2*n;i++){
int k=scanner.nextInt();
if(k%2==0) a.add(i);
else b.add(i);
}
if(a.size()/2>=n-1) {
int j=0;
for(int i=0;i<n-1;i+=1){
System.out.println(a.get(j)+" "+a.get(j+1));
j+=2;
}
}
else {
if(a.size()%2==0){
flag=1;
}
int k=0;
for(int i=0;i<a.size()/2;i+=1){
System.out.println(a.get(k)+" "+a.get(k+1));
k+=2;
}
if(flag==1){
int j=0;
for(int i=0;i<(n-1)-(a.size())/2;i+=1){
System.out.println(b.get(j)+" "+b.get(j+1));
j+=2;
}
}
else{
int j=0;
for(int i=0;i<(n-1)-(a.size()-1)/2;i+=1){
System.out.println(b.get(j)+" "+b.get(j+1));
j+=2;
}
}
}
t-=1;
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 876d956477f6047273458e05b789ede1 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes |
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int tc = input.nextInt();
while(tc-->0)
{
int n = input.nextInt();
int a[] = new int[n*2];
int count=0;
for (int i = 0; i < n*2; i++) {
a[i] = input.nextInt();
}
for (int i = 0; i < n*2; i++) {
for (int j = i+1; j < n*2; j++) {
if(count<n-1&&(a[i]+a[j])%2==0&&a[i]!=-1 &&a[j]!=-1)
{
System.out.println((i+1)+" "+(j+1));
a[i]=-1;
a[j]=-1;
count++;
}
}
}
}
}
}
| Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | da0608a9768431305e890a4a08539a1f | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.InputMismatchException;
public class A {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
InputReader s = new InputReader(System.in);
PrintWriter p = new PrintWriter(System.out);
int t = s.nextInt();
while(t-->0){
int n = s.nextInt();
ArrayList<pair> odd = new ArrayList<>();
ArrayList<pair> even = new ArrayList<>();
for(int i = 0; i < 2*n; i++){
int val = s.nextInt();
if(val%2==0){
even.add(new pair(val, i+1));
}else
odd.add(new pair(val, i+1));
}
if(odd.size()%2==0){
if(even.size()>0){
even.remove(0);
even.remove(0);
}else{
odd.remove(0);
odd.remove(0);
}
}else{
even.remove(0);
odd.remove(0);
}
for(int i = 0; i < odd.size(); i+=2){
p.println(odd.get(i).y+" "+odd.get(i+1).y);
}
for(int i = 0; i < even.size(); i+=2){
p.println(even.get(i).y+" "+even.get(i+1).y);
}
}
p.flush();
p.close();
}
public static boolean prime(long n) {
if (n == 1) {
return false;
}
if (n == 2) {
return true;
}
for (long i = 2; i <= (long) Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
public static ArrayList Divisors(long n) {
ArrayList<Long> div = new ArrayList<>();
for (long i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
div.add(i);
if (n / i != i)
div.add(n / i);
}
}
return div;
}
public static int BinarySearch(long[] a, long k) {
int n = a.length;
int i = 0, j = n - 1;
int mid = 0;
if (k < a[0])
return 0;
else if (k >= a[n - 1])
return n;
else {
while (j - i > 1) {
mid = (i + j) / 2;
if (k >= a[mid])
i = mid;
else
j = mid;
}
}
return i + 1;
}
public static long GCD(long a, long b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
public static long LCM(long a, long b) {
return (a * b) / GCD(a, b);
}
static class pair implements Comparable<pair> {
Integer x, y;
pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if (result == 0)
result = y.compareTo(o.y);
return result;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x - x == 0 && p.y - y == 0;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class CodeX {
public static void sort(long arr[]) {
merge_sort(arr, 0, arr.length - 1);
}
private static void merge_sort(long A[], long start, long end) {
if (start < end) {
long mid = (start + end) / 2;
merge_sort(A, start, mid);
merge_sort(A, mid + 1, end);
merge(A, start, mid, end);
}
}
private static void merge(long A[], long start, long mid, long end) {
long p = start, q = mid + 1;
long Arr[] = new long[(int) (end - start + 1)];
long k = 0;
for (int i = (int) start; i <= end; i++) {
if (p > mid)
Arr[(int) k++] = A[(int) q++];
else if (q > end)
Arr[(int) k++] = A[(int) p++];
else if (A[(int) p] < A[(int) q])
Arr[(int) k++] = A[(int) p++];
else
Arr[(int) k++] = A[(int) q++];
}
for (int i = 0; i < k; i++) {
A[(int) start++] = Arr[i];
}
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 38211e32261e1e767645c55287a39894 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
public class Solution
{
public static int gcd(int a,int b){
if(a==1 ||b==1)
return 1;
if(b==0)
return a;
return gcd(b, a%b);
}
public static HashMap<Integer,Integer> primeFactors(int n)
{
HashMap<Integer,Integer> h=new HashMap<Integer,Integer> ();
while (n%2==0)
{
if(h.containsKey(2))
h.put(2,h.get(2)+1);
else
h.put(2,1);
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
if(h.containsKey(i))
h.put(i,h.get(i)+1);
else
h.put(i,1);
n /= i;
}
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (n > 2) {
if(h.containsKey(n))
h.put(n,h.get(n)+1);
else
h.put(n,1);
}
return h;
}
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
StringBuilder out=new StringBuilder();
int t=Integer.parseInt(br.readLine());
while(t-->0){
ArrayList<Integer> e=new ArrayList<>();
ArrayList<Integer> o=new ArrayList<>();
int n=Integer.parseInt(br.readLine());
StringTokenizer st=new StringTokenizer(br.readLine());
int arr[]=new int[2*n];
for(int i=0;i<(2*n);i++){
arr[i]=Integer.parseInt(st.nextToken());
if(arr[i]%2==0)
e.add(i+1);
else
o.add(i+1);
}
// System.out.print(o.size()+"/");
if(e.size()==0){
for(int k=0;k<o.size()-3;k=k+2){
out.append(o.get(k)+" "+o.get(k+1)+"\n");
}
}
else if(o.size()==0){
for(int k=0;k<e.size()-3;k=k+2){
out.append(e.get(k)+" "+e.get(k+1)+"\n");
}
}
else if(e.size()%2==0 && o.size()%2==0){
for(int k=0;k<e.size()-1;k=k+2){
out.append(e.get(k)+" "+e.get(k+1)+"\n");
}
for(int k=0;k<o.size()-3;k=k+2){
out.append(o.get(k)+" "+o.get(k+1)+"\n");
}
}else{
for(int k=0;k<e.size()-2;k=k+2){
out.append(e.get(k)+" "+e.get(k+1)+"\n");
}
for(int k=0;k<o.size()-2;k=k+2){
out.append(o.get(k)+" "+o.get(k+1)+"\n");
}
}
// out.append("*");
}
System.out.println(out);
}}
// class Graph
// {
// private int V; // No. of vertices
// // Array of lists for Adjacency List Representation
// ArrayList<pair> adj[];
// // Constructor
// Graph(int v)
// {
// V = v;
// adj = new ArrayList[v];
// for (int i=0; i<v; ++i)
// adj[i] = new ArrayList();
// }
// //Function to add an edge into the graph
// void addEdge(int v, int w,int x)
// {
// adj[v].add(new pair(w,x));
// adj[w].add(new pair(v,x));// Add w to v's list.
// }
// // A function used by DFS
// }
// class pair{
// int x;
// int y;
// public pair(int xx,int yy){
// x=xx;
// y=yy;
// }
// }
class SortT implements Comparator<String>
{
public int compare(String a, String b)
{
return a.length() - b.length();
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | b7b0324c85cc65408fb73bbbc5907f9d | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes |
import java.util.*;
import java.io.*;
public class GCD_Compression {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader t = new FastReader();
PrintWriter o = new PrintWriter(System.out);
int test = t.nextInt();
while (test-- > 0) {
List<Integer> odd = new ArrayList<>();
List<Integer> even = new ArrayList<>();
int n = t.nextInt();
for (int i = 0; i < 2 * n; ++i) {
int x = t.nextInt();
if ((x & 1) == 1)
odd.add(i + 1);
else
even.add(i + 1);
}
int i = 0, j = 0, count = 0;
while (count < n - 1) {
if (i <= odd.size() - 2) {
o.println(odd.get(i) + " " + odd.get(i + 1));
i += 2;
count++;
} else {
o.println(even.get(j) + " " + even.get(j + 1));
count++;
j += 2;
}
}
}
o.flush();
o.close();
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | eb3c91bcb908e17f6224c52900025d96 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | /*
Aman Agarwal
algo.java
*/
import java.util.*;
import java.io.*;
public class C
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static int ni()throws IOException{return sc.nextInt();}
static long nl()throws IOException{return sc.nextLong();}
static int[][] nai2(int n,int m)throws IOException{int a[][] = new int[n][m];for(int i=0;i<n;i++)for(int j=0;j<m;j++)a[i][j] = ni();return a;}
static int[] nai(int N,int start)throws IOException{int[]A=new int[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;}
static Integer[] naI(int N,int start)throws IOException{Integer[]A=new Integer[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;}
static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
static void print(int arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();}
static void print(long arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} // return the number of set bits.
static boolean isPrime(int number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}int sqrt = (int) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;}
static boolean isPrime(long number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}long sqrt = (long) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;}
static int power(int n,int p){if(p==0)return 1;int a = power(n,p/2);a = a*a;int b = p & 1;if(b!=0){a = n*a;}return a;}
static long power(long n,long p){if(p==0)return 1;long a = power(n,p/2);a = a*a;long b = p & 1;if(b!=0){a = n*a;}return a;}
static void reverse(int[] a) {int b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}}
static void reverse(long[] a) {long b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}}
static void swap(int a[],int i,int j){a[i] = a[i] ^ a[j];a[j] = a[j] ^ a[i];a[i] = a[i] ^ a[j];}
static void swap(long a[],int i,int j){a[i] = a[i] ^ a[j];a[j] = a[j] ^ a[i];a[i] = a[i] ^ a[j];}
static int count(int n){int c=0;while(n>0){c++;n = n/10;}return c;}
static int[] prefix_sum(int a[],int n){int s[] = new int[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;}
static long[] prefix_sum_int(int a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;}
static long[] prefix_sum_Integer(Integer a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;}
static long[] prefix_sum_long(long a[],int n){long s[] = new long[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;}
static boolean isPerfectSquare(double x){double sr = Math.sqrt(x);return ((sr - Math.floor(sr)) == 0);}
static ArrayList<Integer> sieve(int n) {int k=0; boolean prime[] = new boolean[n+1];ArrayList<Integer> p_arr = new ArrayList<>();for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ k=p;if(prime[p] == true) { p_arr.add(p);for(int i = p*2; i <= n; i += p) prime[i] = false; } }for(int i = k+1;i<=n;i++){if(prime[i]==true)p_arr.add(i);}return p_arr;}
static boolean[] seive_check(int n) {boolean prime[] = new boolean[n+1];for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ if(prime[p] == true) { for(int i = p*2; i <= n; i += p) prime[i] = false; } }prime[1]=false;return prime;}
static int get_bits(int n){int p=0;while(n>0){p++;n = n>>1;}return p;}
static int get_bits(long n){int p=0;while(n>0){p++;n = n>>1;}return p;}
static int get_2_power(int n){if((n & (n-1))==0)return get_bits(n)-1;return -1;}
static int get_2_power(long n){if((n & (n-1))==0)return get_bits(n)-1;return -1;}
static void close(){out.flush();}
/*-------------------------Main Code Starts(algo.java)----------------------------------*/
static public void solve() throws IOException
{
int n = ni();
int arr[] = new int[2*n];
ArrayList<Integer>even = new ArrayList<>();
ArrayList<Integer>odd = new ArrayList<>();
int k=0,l=0;
for(int i=0;i<2*n;i++)
{
arr[i] = ni();
if(arr[i]%2==1)
{
odd.add(i);
}
else
{
even.add(i);
}
}
int ans = 0;
if(odd.size()>1)
{
for(int i=0;i<odd.size();i=i+2)
{
if(i+1<odd.size())
{
System.out.print(odd.get(i)+1+" ");
System.out.println(odd.get(i+1)+1);
ans++;
}
if(ans==n-1)
{
return;
}
}
}
if(even.size()>1)
{
for(int i=0;i<even.size();i=i+2)
{
if(i+1<even.size())
{
System.out.print(even.get(i)+1+" ");
System.out.println(even.get(i+1)+1);
ans++;
}
if(ans==n-1)
{
return;
}
}
}
}
public static void main(String[] args) throws IOException
{
int test = 1;
test = sc.nextInt();
while(test-- > 0)
{
solve();
}
close();
}
}
| Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 62bbcb96219540ed97ce61fe2589c9b2 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.StringTokenizer;
/**
* I realized that you might have doubt in this question, so if at some point in my
* code you find anything that you don't understand, please do not disturb me.
*
* - Sheldon Cooper
*/
public class Howard {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
PrintWriter pw = new PrintWriter(System.out);
try {
int t = Integer.parseInt(br.readLine());
while(t-->0) {
int n = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
LinkedList<Integer> ll1 = new LinkedList<>();
LinkedList<Integer> ll2 = new LinkedList<>();
for(int i=0 ; i<2*n ; i++) {
int x = Integer.parseInt(st.nextToken());
if(x%2 == 0) ll1.add((i+1));
else ll2.add((i+1));
}
if(ll1.size()%2 == 1) {
ll1.poll();
ll2.poll();
}
else {
if(ll1.size()!=0) {
ll1.poll();
ll1.poll();
}
else {
ll2.poll();
ll2.poll();
}
}
while(ll1.size() != 0) {
int x = ll1.poll();
int y = ll1.poll();
pw.println(x + " " + y);
}
while(ll2.size() != 0) {
int x = ll2.poll();
int y = ll2.poll();
pw.println(x + " " + y);
}
}
}
finally {
pw.flush();
pw.close();
}
}
}
| Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | b71413ed7b5e4d212dbda1999341250a | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void test() {
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int size = sc.nextInt();
ArrayList<Integer> values = new ArrayList<>();
for (int j = 0; j < 2 * size; j++) {
values.add(sc.nextInt());
}
printCompressionPath(values);
}
}
public static void printCompressionPath(ArrayList<Integer> values) {
Stack<IndexElement> evens = new Stack<>();
Stack<IndexElement> odds = new Stack<>();
for (int i = 0; i < values.size(); i++) {
IndexElement v = new IndexElement(i + 1, values.get(i));
if (values.get(i) % 2 == 0) {
evens.add(v);
} else {
odds.add(v);
}
}
if (odds.size() % 2 == 1) {
odds.pop();
evens.pop();
} else if (evens.size() > 1) {
evens.pop();
evens.pop();
} else {
odds.pop();
odds.pop();
}
while (odds.size() >= 2) {
System.out.println(odds.pop() + " " + odds.pop());
}
while (evens.size() >= 2) {
System.out.println(evens.pop() + " " + evens.pop());
}
}
static class IndexElement {
int index;
int element;
public IndexElement(int i, int e) {
index = i;
element = e;
}
public String toString() {
return "" + index;
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | ceab4a5e073bc2e702d62fdfbdbebeb0 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class AA {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(in.nextInt(), in, out);
out.close();
}
static class TaskA {
long mod = (long)(998244353);
long fact[];
int depth[];
int parentTable[][];
int degree[];
ArrayList<Integer> leaves;
int start = 0;
int end = 0;
int length = Integer.MAX_VALUE;
public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
while(testNumber-->0){
int n = in.nextInt();
ArrayList<Integer> odd = new ArrayList<>();
ArrayList<Integer> even = new ArrayList<>();
for(int i=0;i<2*n;i++){
int x = in.nextInt();
if(x%2 == 0)
even.add(i+1);
else
odd.add(i+1);
}
// out.println(odd);
// out.println(even);
int i = 0,j = 0;
if(odd.size() == 0){
i = 0;
j = 2;
}
else if(even.size() == 0){
i = 2;
j = 0;
}
else if(odd.size()%2 == 0){
i = 2;
j = 0;
// while(i<odd.size() && j<even.size()){
// out.println(odd.get(i) + " " + even.get(j));
// i++;
// j++;
// }
}
else{
i = 1;
j = 1;
}
while(i<odd.size()){
out.println(odd.get(i) + " " + odd.get(i+1));
i+=2;
}
while(j<even.size()){
out.println(even.get(j) + " " + even.get(j+1));
j+=2;
}
while(i<odd.size()){
out.println(odd.get(i) + " " + odd.get(i+2));
i+=2;
}
while(j<even.size()){
out.println(even.get(j) + " " + even.get(j+1));
j+=2;
}
}
}
public void dfs(ArrayList<ArrayList<Integer>> a , int index , int parent , int visited[] , int p[]){
depth[index] = 1+depth[parent];
p[index] = parent;
visited[index] = 1;
int l = a.get(index).size();
// for(int i=0;i<l;i++){
// if(visited[a.get(index).get(i)] == 1)
// continue;
// dfs(a , a.get(index).get(i) , index , visited);
// }
for(int i=0;i<l;i++){
if(a.get(index).get(i) == parent)
continue;
if(visited[a.get(index).get(i)] == 1){
int x = Math.abs(depth[index] - depth[a.get(index).get(i)]);
if(x<length){
length = x;
start = a.get(index).get(i);
end = index;
}
continue;
}
dfs(a , a.get(index).get(i) , index , visited , p);
}
}
public int distance(ArrayList<ArrayList<Integer>> a , int u , int v){
return depth[u]+depth[v] - 2*depth[lca(a , u , v)];
}
// public void dfs(ArrayList<ArrayList<Integer>> a , int index , int parent){
// parentTable[index][0] = parent;
// depth[index] = depth[parent]+1;
// if(a.get(index).size()==1)
// leaves.add(index);
// for(int i=1;i<parentTable[index].length;i++)
// parentTable[index][i] = parentTable[parentTable[index][i-1]][i-1];
// for(int i=0;i<a.get(index).size();i++){
// if(a.get(index).get(i)==parent)
// continue;
// dfs(a , a.get(index).get(i) , index);
// }
// }
public int lca(ArrayList<ArrayList<Integer>> a , int u , int v){
if(depth[v]<depth[u]){
int x = u;
u = v;
v = x;
}
int diff = depth[v] - depth[u];
for(int i=0;i<parentTable[v].length;i++){
// checking whether the ith bit is set in the diff variable
if(((diff>>i)&1) == 1)
v = parentTable[v][i];
}
if(v == u)
return v;
for(int i=parentTable[v].length-1;i>=0;i--){
if(parentTable[u][i] != parentTable[v][i]){
v = parentTable[v][i];
u = parentTable[u][i];
}
}
return parentTable[u][0];
}
// for the min max problems
public void build(int lookup[][] , int arr[], int n) {
for (int i = 0; i < n; i++)
lookup[i][0] = arr[i];
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 0; (i + (1 << j) - 1) < n; i++) {
if (lookup[i][j - 1] > lookup[i + (1 << (j - 1))][j - 1])
lookup[i][j] = lookup[i][j - 1];
else
lookup[i][j] = lookup[i + (1 << (j - 1))][j - 1];
}
}
}
public int query(int lookup[][] , int L, int R) {
int j = (int)(Math.log(R - L + 1)/Math.log(2));
if (lookup[L][j] >= lookup[R - (1 << j) + 1][j])
return lookup[L][j];
else
return lookup[R - (1 << j) + 1][j];
}
// for printing purposes
public void print1d(int a[] , PrintWriter out){
for(int i=0;i<a.length;i++)
out.print(a[i] + " ");
out.println();
}
public void print2d(int a[][] , PrintWriter out){
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++)
out.print(a[i][j] + " ");
out.println();
}
out.println();
}
public void sieve(int a[]){
a[0] = a[1] = 1;
int i;
for(i=2;i*i<=a.length;i++){
if(a[i] != 0)
continue;
a[i] = i;
for(int k = (i)*(i);k<a.length;k+=i){
if(a[k] != 0)
continue;
a[k] = i;
}
}
}
public int [][] matrixExpo(int c[][] , int n){
int a[][] = new int[c.length][c[0].length];
int b[][] = new int[a.length][a[0].length];
for(int i=0;i<c.length;i++)
for(int j=0;j<c[0].length;j++)
a[i][j] = c[i][j];
for(int i=0;i<a.length;i++)
b[i][i] = 1;
while(n!=1){
if(n%2 == 1){
b = matrixMultiply(a , a);
n--;
}
a = matrixMultiply(a , a);
n/=2;
}
return matrixMultiply(a , b);
}
public int [][] matrixMultiply(int a[][] , int b[][]){
int r1 = a.length;
int c1 = a[0].length;
int c2 = b[0].length;
int c[][] = new int[r1][c2];
for(int i=0;i<r1;i++){
for(int j=0;j<c2;j++){
for(int k=0;k<c1;k++)
c[i][j] += a[i][k]*b[k][j];
}
}
return c;
}
public long nCrPFermet(int n , int r , long p){
if(r==0)
return 1l;
// long fact[] = new long[n+1];
// fact[0] = 1;
// for(int i=1;i<=n;i++)
// fact[i] = (i*fact[i-1])%p;
long modInverseR = pow(fact[r] , p-2 , p);
long modInverseNR = pow(fact[n-r] , p-2 , p);
long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p;
return w;
}
public long pow(long a, long b, long m) {
a %= m;
long res = 1;
while (b > 0) {
long x = b&1;
if (x == 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
public long pow(long a, long b) {
long res = 1;
while (b > 0) {
long x = b&1;
if (x == 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
public void swap(int a[] , int p1 , int p2){
int x = a[p1];
a[p1] = a[p2];
a[p2] = x;
}
public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) {
if (start > end) {
return;
}
int mid = (start + end) / 2;
a.add(mid);
sortedArrayToBST(a, start, mid - 1);
sortedArrayToBST(a, mid + 1, end);
}
class Combine{
int value;
int delete;
Combine(int val , int delete){
this.value = val;
this.delete = delete;
}
}
class Sort2 implements Comparator<Combine>{
public int compare(Combine a , Combine b){
if(a.value > b.value)
return 1;
else if(a.value == b.value && a.delete>b.delete)
return 1;
else if(a.value == b.value && a.delete == b.delete)
return 0;
return -1;
}
}
public int lowerLastBound(ArrayList<Integer> a , int x){
int l = 0;
int r = a.size()-1;
if(a.get(l)>=x)
return -1;
if(a.get(r)<x)
return r;
int mid = -1;
while(l<=r){
mid = (l+r)/2;
if(a.get(mid) == x && a.get(mid-1)<x)
return mid-1;
else if(a.get(mid)>=x)
r = mid-1;
else if(a.get(mid)<x && a.get(mid+1)>=x)
return mid;
else if(a.get(mid)<x && a.get(mid+1)<x)
l = mid+1;
}
return mid;
}
public int upperFirstBound(ArrayList<Integer> a , Integer x){
int l = 0;
int r = a.size()-1;
if(a.get(l)>x)
return l;
if(a.get(r)<=x)
return r+1;
int mid = -1;
while(l<=r){
mid = (l+r)/2;
if(a.get(mid) == x && a.get(mid+1)>x)
return mid+1;
else if(a.get(mid)<=x)
l = mid+1;
else if(a.get(mid)>x && a.get(mid-1)<=x)
return mid;
else if(a.get(mid)>x && a.get(mid-1)>x)
r = mid-1;
}
return mid;
}
public int lowerLastBound(int a[] , int x){
int l = 0;
int r = a.length-1;
if(a[l]>=x)
return -1;
if(a[r]<x)
return r;
int mid = -1;
while(l<=r){
mid = (l+r)/2;
if(a[mid] == x && a[mid-1]<x)
return mid-1;
else if(a[mid]>=x)
r = mid-1;
else if(a[mid]<x && a[mid+1]>=x)
return mid;
else if(a[mid]<x && a[mid+1]<x)
l = mid+1;
}
return mid;
}
public int upperFirstBound(long a[] , long x){
int l = 0;
int r = a.length-1;
if(a[l]>x)
return l;
if(a[r]<=x)
return r+1;
int mid = -1;
while(l<=r){
mid = (l+r)/2;
if(a[mid] == x && a[mid+1]>x)
return mid+1;
else if(a[mid]<=x)
l = mid+1;
else if(a[mid]>x && a[mid-1]<=x)
return mid;
else if(a[mid]>x && a[mid-1]>x)
r = mid-1;
}
return mid;
}
public long log(float number , int base){
return (long) Math.ceil((Math.log(number) / Math.log(base)) + 1e-9);
}
public long gcd(long a , long b){
if(a<b){
long c = b;
b = a;
a = c;
}
while(b!=0){
long c = a;
a = b;
b = c%a;
}
return a;
}
public long[] gcdEx(long p, long q) {
if (q == 0)
return new long[] { p, 1, 0 };
long[] vals = gcdEx(q, p % q);
long d = vals[0];
long a = vals[2];
long b = vals[1] - (p / q) * vals[2];
// 0->gcd 1->xValue 2->yValue
return new long[] { d, a, b };
}
public void sievePhi(int a[]){
a[0] = 0;
a[1] = 1;
for(int i=2;i<a.length;i++)
a[i] = i-1;
for(int i=2;i<a.length;i++)
for(int j = 2*i;j<a.length;j+=i)
a[j] -= a[i];
}
public void lcmSum(long a[]){
int sievePhi[] = new int[(int)1e6 + 1];
sievePhi(sievePhi);
a[0] = 0;
for(int i=1;i<a.length;i++)
for(int j = i;j<a.length;j+=i)
a[j] += (long)i*sievePhi[i];
}
static class AVLTree{
Node root;
public AVLTree(){
this.root = null;
}
public int height(Node n){
return (n == null ? 0 : n.height);
}
public int getBalance(Node n){
return (n == null ? 0 : height(n.left) - height(n.right));
}
public Node rotateRight(Node a){
Node b = a.left;
Node br = b.right;
a.lSum -= b.lSum;
a.lCount -= b.lCount;
b.rSum += a.rSum;
b.rCount += a.rCount;
b.right = a;
a.left = br;
a.height = 1 + Math.max(height(a.left) , height(a.right));
b.height = 1 + Math.max(height(b.left) , height(b.right));
return b;
}
public Node rotateLeft(Node a){
Node b = a.right;
Node bl = b.left;
a.rSum -= b.rSum;
a.rCount -= b.rCount;
b.lSum += a.lSum;
b.lCount += a.lCount;
b.left = a;
a.right = bl;
a.height = 1 + Math.max(height(a.left) , height(a.right));
b.height = 1 + Math.max(height(b.left) , height(b.right));
return b;
}
public Node insert(Node root , long value){
if(root == null){
return new Node(value);
}
if(value<=root.value){
root.lCount++;
root.lSum += value;
root.left = insert(root.left , value);
}
if(value>root.value){
root.rCount++;
root.rSum += value;
root.right = insert(root.right , value);
}
// updating the height of the root
root.height = 1 + Math.max(height(root.left) , height(root.right));
int balance = getBalance(root);
//ll
if(balance>1 && value<=root.left.value)
return rotateRight(root);
//rr
if(balance<-1 && value>root.right.value)
return rotateLeft(root);
//lr
if(balance>1 && value>root.left.value){
root.left = rotateLeft(root.left);
return rotateRight(root);
}
//rl
if(balance<-1 && value<=root.right.value){
root.right = rotateRight(root.right);
return rotateLeft(root);
}
return root;
}
public void insertElement(long value){
this.root = insert(root , value);
}
public int getElementLessThanK(long k){
int count = 0;
Node temp = root;
while(temp!=null){
if(temp.value == k){
if(temp.left == null || temp.left.value<k){
count += temp.lCount;
return count-1;
}
else
temp = temp.left;
}
else if(temp.value>k){
temp = temp.left;
}
else{
count += temp.lCount;
temp = temp.right;
}
}
return count;
}
public void inorder(Node root , PrintWriter out){
Node temp = root;
if(temp!=null){
inorder(temp.left , out);
out.println(temp.value + " " + temp.lCount + " " + temp.rCount);
inorder(temp.right , out);
}
}
}
static class Node{
long value;
long lCount , rCount;
long lSum , rSum;
Node left , right;
int height;
public Node(long value){
this.value = value;
left = null;
right = null;
lCount = 1;
rCount = 1;
lSum = value;
rSum = value;
height = 1;
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | d23979a65404a92a40490bbe39d3ad25 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
/**
*
* @author milon
*/
public class GCD_Compression {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
int T=fs.nextInt();
for(int tt=0;tt<T;tt++){
int n=fs.nextInt()*2;
int[] a=fs.readArray(n);
ArrayList<Integer> evens=new ArrayList<>(),odds=new ArrayList<>();
for(int i=0;i<n;i++){
if(a[i]%2==0){
evens.add(i+1);
}else{
odds.add(i+1);
}
}
if(evens.size()%2==0){
if(evens.size()>0){
evens.remove(0);
}else{
odds.remove(0);
}
}
for(int i=0;i+1<evens.size();i+=2){
System.out.println(evens.get(i)+" "+evens.get(i+1));
}
for(int i=0;i+1<odds.size();i+=2){
System.out.println(odds.get(i)+" "+odds.get(i+1));
}
}
}
}
class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
| Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 30f9121e63469c602186c5315601a8fe | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.awt.Point;
//ArrayList<Integer> ar=new ArrayList<Integer>();
//HashSet<Integer> h=new HashSet<Integer>();
//HashMap<Integer,Integer> h=new HashMap<Integer, Integer>();
//int b[]=new int[m];
//int a[]=new int[n];
//for(i=0;i<n;i++)
//a[i]=sc.ni();
//out.println();
//out.print(+" ");
public class Main {
//static final long MOD = 998244353L;
//static final long INF = 1000000000000000007L;
static final long MOD = 1000000007L;
static final int INF = 1000000007;
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int T=sc.ni();
while(T-->0)
{
int n=sc.ni();
int ev=0,od=0;
int i;
int a[]=new int[2*n+1];
for( i=1;i<=2*n;i++){
a[i]=sc.ni();
if(a[i]%2==0)ev++;
else if(a[i]%2==1)od++;
}
int even[]=new int[ev];
int odd[]=new int[od];
int x=0,y=0;
for(i=1;i<=2*n;i++){
if(a[i]%2==0)
even[x++]=i;
else if(a[i]%2==1)
odd[y++]=i;
}
if(od%2==1 && ev%2==1){
if(ev>1){
for(i=1;i<ev;i+=2)
out.println(even[i]+" "+even[i+1]);
}
if(od>1){
for(i=1;i<od;i+=2)
out.println(odd[i]+" "+odd[i+1]);
}
}
else if(od%2==0 && ev%2==0){
if(ev>2){
for(i=2;i<ev;i+=2)
out.println(even[i]+" "+even[i+1]);
}
if(ev==0){
for(i=2;i<od;i+=2)
out.println(odd[i]+" "+odd[i+1]);
continue;
}
for(i=0;i<od;i+=2)
out.println(odd[i]+" "+odd[i+1]);
}
// ArrayList<Integer> even=new ArrayList<Integer>();
// ArrayList<Integer> odd=new ArrayList<Integer>();
// for( i=1;i<=2*n;i++){
// if(a[i]%2==0) even.add(i);
// else if(a[i]%2==1) odd.add(i);
// }
// if(od%2==1 && ev%2==1){
// odd.remove(0);
// even.remove(0);
// }
// else if(ev%2==0){
// even.remove(0);
// even.remove(1);
// }
// for( i=0;i<even.size();i+=2){
// out.println(even.get(i)+" "+even.get(i+1));
// }
// for( i=0;i<odd.size();i+=2)
// out.println(odd.get(i)+" "+odd.get(i+1));
}out.flush();
}
public static long dist(long[] p1, long[] p2) {
return (Math.abs(p2[0]-p1[0])+Math.abs(p2[1]-p1[1]));
}
//Find the GCD of two numbers
public static long gcd(long a, long b) {
if (a < b) return gcd(b,a);
if (b == 0)
return a;
else
return gcd(b,a%b);
}
//Fast exponentiation (x^y mod m)
public static long power(long x, long y, long m) {
if (y < 0) return 0L;
long ans = 1;
x %= m;
while (y > 0) {
if(y % 2 == 1)
ans = (ans * x) % m;
y /= 2;
x = (x * x) % m;
}
return ans;
}
public static int[] shuffle(int[] array) {
Random rgen = new Random();
for (int i = 0; i < array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
int temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
return array;
}
public static long[] shuffle(long[] array) {
Random rgen = new Random();
for (int i = 0; i < array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
long temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
return array;
}
public static int[][] shuffle(int[][] array) {
Random rgen = new Random();
for (int i = 0; i < array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
int[] temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
return array;
}
public static int[][] sort(int[][] array) {
//Sort an array (immune to quicksort TLE)
Arrays.sort(array, new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b) {
return a[1]-b[1]; //ascending order
}
});
return array;
}
public static long[][] sort(long[][] array) {
//Sort an array (immune to quicksort TLE)
Random rgen = new Random();
for (int i = 0; i < array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
long[] temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
Arrays.sort(array, new Comparator<long[]>() {
@Override
public int compare(long[] a, long[] b) {
if (a[0] < b[0])
return -1;
else if (a[0] > b[0])
return 1;
else
return 0;
}
});
return array;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
long nl() {
return Long.parseLong(next());
}
double nd() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | adce047fd13c339fca83096f0122acd8 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
public static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a*b)/gcd(a, b);
}
public static void sortbyColumn(int arr[][], int col)
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
});
}
static long func(long a[],long size,int s){
long max1=a[s];
long maxc=a[s];
for(int i=s+1;i<size;i++){
maxc=Math.max(a[i],maxc+a[i]);
max1=Math.max(maxc,max1);
}
return max1;
}
public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
//static LinkedList<Integer> li;
//static LinkedList<Integer> ans;
static int ans1=0,ans2=0,k;
static long dist[];
static int visited[];
//static int arr[];
static ArrayList<Integer> adj[];
static int color[];
public static void main(String args[]){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int t,i,j;
t=in.nextInt();
while(t-->0){
int n=in.nextInt();
int arr[]=new int[2*n],oc=0,ec=0;
LinkedList<Integer> odd=new LinkedList<>();
LinkedList<Integer> even=new LinkedList<>();
for(i=0;i<2*n;i++){
arr[i]=in.nextInt();
if(arr[i]%2==0){
even.add(i+1);
}else{
odd.add(i+1);
}
}
//w.println(odd+" "+even);
if(odd.size()%2!=0){
odd.removeFirst();
}
int c=0;
while(odd.size()>=2){
if(c==n-1){
break;
}
w.println(odd.removeFirst()+" "+odd.removeFirst());
c++;
}
while(even.size()>=2){
if(c==n-1){
break;
}
w.println(even.removeFirst()+" "+even.removeFirst());
c++;
}
//w.println(t);
}
w.close();
}
}
| Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | d3197e3b9c32ce9a58e12ef4169ec6d7 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Solution {
static final FS sc = new FS();
static final PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
List<Integer> odd = new ArrayList<>(),
even = new ArrayList<>();
for(int i = 1;i <= 2 * n;i++) {
int num = sc.nextInt();
if (num % 2 == 0) {
even.add(i);
} else {
odd.add(i);
}
}
int count = 0;
for(int i = 0;i < odd.size() - 1;i += 2) {
if (count == n - 1) {
break;
}
pw.println(odd.get(i) + " " + odd.get(i + 1));
count++;
}
for(int i = 0;i < even.size() - 1;i += 2) {
if (count == n - 1) {
break;
}
pw.println(even.get(i) + " " + even.get(i + 1));
count++;
}
}
pw.flush();
}
static class FS {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch(Exception ignored) {}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 6be2ebf3b5e8e52a8ac89b36afacc836 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | // Working program using Reader Class
import java.io.*;
import java.util.*;
public class Main
{
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws IOException
{
Reader sc=new Reader();
PrintWriter out = new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
Queue<Integer> odd=new LinkedList<Integer>(),even=new LinkedList<Integer>();
for(int i=0;i<2*n;i++){
Integer num=sc.nextInt();
if(num%2==0){
even.add(i+1);
}else{
odd.add(i+1);
}
}
//System.out.println(odd.size()+" "+even.size());
for(int i=1;i<n;i++){
if(odd.size()>1){
out.println(odd.poll()+" "+odd.poll());
}else{
out.println(even.poll()+" "+even.poll());
}
}
}
out.flush();
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 7a8a351ee0dbe5237db172a83fa11d72 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.util.*;
import java.lang.*;
public final class TestB
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
int n=sc.nextInt();
int val=2*n;
int[] a=new int[val];
for(int j=0;j<val;j++)
{
int v=sc.nextInt();
a[j]=v;
}
int count=0;
boolean[] visited=new boolean[val];
Arrays.fill(visited,false);
for(int j=0;j<val;j++)
{
//int temp=a[j];
if(visited[j]==true)
continue;
for(int k=j+1;k<val;k++)
{
if((a[j]+a[k])%2==0 && visited[k]==false)
{
System.out.println((j+1)+" "+(k+1));
visited[j]=true;
visited[k]=true;
count++;
if(count==n-1)
{
k=val;j=val;
}
else
{
break;
}
}
}
}
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 8d6ef405e8cea94bc9ddd0b2b04b7d2c | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Integer.*;
import static java.lang.Long.*;
import static java.lang.System.*;
public class B {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st;
int t = parseInt(in.readLine());
for (int t1 = 0; t1 < t; t1++) {
int n = parseInt(in.readLine());
ArrayDeque<Integer> even = new ArrayDeque<>();
ArrayDeque<Integer> odd = new ArrayDeque<>();
st = new StringTokenizer(in.readLine());
for (int i = 0; i < 2 * n; i++) {
int a = parseInt(st.nextToken());
if (a % 2 == 0) even.add((i + 1));
else odd.add((i + 1));
}
pw.print(solve(even,odd,n));
}
pw.close();
}
static StringBuilder solve(ArrayDeque<Integer> even, ArrayDeque<Integer> odd, int n) {
StringBuilder sb = new StringBuilder();
while ((n - 1) != 0) {
if (even.size() >= 2 && (n - 1) != 0) {
sb.append(even.remove() + " " + even.remove() + "\n");
n--;
}
if (odd.size() >= 2 && (n - 1) != 0) {
sb.append(odd.remove() + " " + odd.remove()+ "\n");
n--;
}
}
return sb;
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 762069bf83099b910704e7af6ddff0a1 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.util.*;
import java.math.*;
public class GDCCompression
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int runs = sc.nextInt();
while(runs-->0)
{
int n = sc.nextInt();
ArrayList<Integer> even = new ArrayList<>();
ArrayList<Integer> odd = new ArrayList<>();
for(int i = 0;i<n*2;i++)
{
int temp = sc.nextInt();
if(temp%2==0)
even.add(i);
else
{
odd.add(i);
}
}
for (int i = 0; i < n - 1; i++) {
if (odd.size() >= 2) {
System.out.println((odd.remove(0) + 1) + " " + (odd.remove(0) + 1));
} else {
System.out.println((even.remove(0) + 1) + " " + (even.remove(0) + 1));
}
}
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 97cf0aef2b64cc1939214507e4b1a085 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | //package test.codeforce.c651;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @author Lin
* @version 1.0
* @date 2020/6/20 22:36
*/
public class B
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int testNum = input.nextInt();
StringBuffer stringBuffer = new StringBuffer();
for (int rr = 0; rr < testNum; rr++) {
int n = input.nextInt();
int ans = 0;
//������������
List<Integer> oddList = new ArrayList<>();
List<Integer> evenList = new ArrayList<>();
for (int i = 0; i < 2*n; i++) {
int a = input.nextInt();
if (a % 2 == 1)
{
oddList.add(i+1);
}else {
evenList.add(i+1);
}
}
if (oddList.size() % 2 == 0)
{
if (oddList.size() == 0)
{
evenList.remove(0);
evenList.remove(0);
}else {
oddList.remove(0);
oddList.remove(0);
}
}else {
oddList.remove(0);
evenList.remove(0);
}
for (int i = 0; i < oddList.size(); i=i+2)
{
stringBuffer.append(oddList.get(i) + " " + oddList.get(i + 1)+"\n");
}
for (int i = 0; i < evenList.size(); i=i+2)
{
stringBuffer.append(evenList.get(i) + " " + evenList.get(i + 1)+"\n");
}
}
System.out.println(stringBuffer.toString());
}
}
| Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 5495cd9031e8649607139eb1d8f29a55 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main {
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[2*n];
for(int i=0;i<2*n;i++){
arr[i]=s.nextInt();
}
ArrayList<Integer> even=new ArrayList<Integer>();
ArrayList<Integer> odd=new ArrayList<Integer>();
for(int i=0;i<2*n;i++){
if(arr[i]%2==0){
even.add(i+1);
}
else
odd.add(i+1);
}
int flag=0;
if(even.size()%2==1){
even.remove(0);
flag=1;
}
else{
if(even.size()>=2 && flag==0){
flag=1;
even.remove(0);
even.remove(0);}
}
if(odd.size()%2==1){
odd.remove(0);
flag=1;
}
else{
if(flag==0 && odd.size()>=2){
flag=1;
odd.remove(0);
odd.remove(0);}
}
for(int i=0;i<even.size();i=i+2){
System.out.println(even.get(i)+" "+even.get(i+1));
}
for(int i=0;i<odd.size();i=i+2){
System.out.println(odd.get(i)+" "+odd.get(i+1));
}
t--;
}
}
}
| Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 8de10eef1d05001e753a7e8f5059b1bc | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.util.Scanner;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.*;
import java.util.List;
import java.lang.Math;
import java.util.HashMap;
public class javasample {
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1) return false;
if (n <= 3) return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static int useBs(int[] arr, int targetValue) {
int a = Arrays.binarySearch(arr, targetValue);
if(a >=0 && a<arr.length)
return a;
else
return -1;
}
public static void printArray(int []a) {
for(int i=0;i<a.length;i++) {
System.out.println(a[i]+" ");
}
System.out.println();
}
static boolean isPalindrome(String s) {
int l=s.length();
for(int i=0;i<l/2;i++)
{
if(s.charAt(i)!=s.charAt(l-i-1))
return false;
}
return true;
}
static String revString(String str)
{
return new StringBuffer(str).reverse().toString();
}
static String sortString(String s) {
char a[]=s.toCharArray();
Arrays.sort(a);
return new String(a);
}
static int sumList(List<Integer>a) {
return a.stream().mapToInt(Integer::intValue).sum();
}
static int sumArray(int []a) {
int sum=0;
for(int i=0;i<a.length;i++) {
sum+=a[i];
}
return sum;
}
public static void main(String[]args) throws java.lang.Exception{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
// int n=sc.nextInt();
// if(n==1||n==6) {
// System.out.println("FastestFinger");
// }else {
// if(n%2!=0||n==2) {
// System.out.println("Ashishgup");
// }else {
// if(Math.log(n)/Math.log(2)-(int)(Math.log(n)/Math.log(2))==0) {
// System.out.println("FastestFinger");
// }else {
// int k=0;
// while(n>1) {
// n/=2;
// k+=1;
// }
// if(k==1) {
// if(isPrime(n/2))
// System.out.println("FastestFinger");
// else
// System.out.println("Ashishgup");
//
// }
// else
// System.out.println("Ashishgup");
// }
// }
// }
int n=sc.nextInt();
int a[]=new int[2*n];
List<Integer>e=new ArrayList<Integer>();
List<Integer>o=new ArrayList<Integer>();
for(int i=0;i<2*n;i++) {
a[i]=sc.nextInt();
if(a[i]%2==0)e.add(i+1);
else o.add(i+1);
}
if(e.size()%2!=0)e.remove(e.size()-1);
e.sort(null);
if(o.size()%2!=0)o.remove(o.size()-1);
o.sort(null);
e.addAll(o);
for(int i=0;i<2*(n-1);i+=2) {
System.out.println(e.get(i)+" "+e.get(i+1));
}
}
}
}
| Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 163e0140aa872d24bf368cb908833fca | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.*;
import java.util.*;
public class MyClass {
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());
int[] arr=new int[2*n];
String[] str=br.readLine().split(" ");
for(int i=0;i<2*n;i++)
arr[i]=Integer.parseInt(str[i]);
ArrayList<Integer> ale=new ArrayList();
ArrayList<Integer> alo=new ArrayList();
for(int i=0;i<2*n;i++){
if(arr[i]%2==0)
ale.add(i);
else
alo.add(i);
}
int esize=ale.size();
int osize=alo.size();
int j=0,k=0;
for(int i=0;i<n-1;i++){
if(j<esize-1){
int x=ale.get(j)+1;
int y=ale.get(j+1)+1;
bw.write(x+" "+y+"\n");
j+=2;
}
else if(j>=esize-1 && k<osize-1){
int x=alo.get(k)+1;
int y=alo.get(k+1)+1;
bw.write(x+" "+y+"\n");
k+=2;
}
}
}
bw.flush();
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 558b05a2684885b51772d76851e0a37d | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.time.LocalTime;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class B {
public static void main(String[] args) {
FastScanner scan = new FastScanner();
int t = scan.nextInt();
for(int tt=0; tt<t; tt++) {
int n = scan.nextInt();
ArrayList<Integer> even = new ArrayList<>(), odd = new ArrayList<>();
for(int i=0; i<2*n; i++) {
int num = scan.nextInt();
if(num % 2 == 0) even.add(i+1);
else odd.add(i+1);
}
int count = 0;
for(int i=0; i+1<even.size() && count<n-1; i+=2) {
System.out.println(even.get(i) + " "+ even.get(i+1));
count++;
}
for(int i=0; i+1<odd.size() && count<n-1; i+=2) {
System.out.println(odd.get(i) +" " + odd.get(i+1));
count++;
}
}
}
public static void sort(int [] a) {
ArrayList<Integer> b = new ArrayList<>();
for(int i: a) b.add(i);
Collections.sort(b);
for(int i=0; i<a.length; i++) a[i]= b.get(i);
}
static class FastScanner{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e){
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int [] readArray(int n) {
int [] a = new int[n];
for(int i=0; i<n ; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 0de0414a8f0636944db5e11d76625687 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.time.LocalTime;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class B {
public static void main(String[] args) {
FastScanner scan = new FastScanner();
int t = scan.nextInt();
for(int tt=0; tt<t; tt++) {
int n = scan.nextInt();
ArrayList<Integer> even = new ArrayList<>(), odd = new ArrayList<>();
for(int i=0; i<2*n; i++) {
int num = scan.nextInt();
if(num % 2 == 0) even.add(i+1);
else odd.add(i+1);
}
int count = 0;
for(int i=0; i+1<even.size() && count<n-1; i+=2) {
System.out.println(even.get(i) + " "+ even.get(i+1));
count++;
}
for(int i=0; i+1<odd.size() && count<n-1; i+=2) {
System.out.println(odd.get(i) +" " + odd.get(i+1));
count++;
}
}
}
public static void sort(int [] a) {
ArrayList<Integer> b = new ArrayList<>();
for(int i: a) b.add(i);
Collections.sort(b);
for(int i=0; i<a.length; i++) a[i]= b.get(i);
}
static class FastScanner{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e){
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int [] readArray(int n) {
int [] a = new int[n];
for(int i=0; i<n ; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | af56a73855b6ea1ae0e9e1451a4d1f10 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
String line[] = null;
int array[] = null;
ArrayList<Integer> even = new ArrayList<Integer>();
ArrayList<Integer> odd = new ArrayList<Integer>();
while (test != 0) {
int len = Integer.parseInt(br.readLine());
line = br.readLine().split(" ");
array = new int[2 * len];
even.clear();
odd.clear();
for (int i = 0; i < line.length; i++) {
array[i] = Integer.parseInt(line[i]);
if (array[i] % 2 == 0) {
even.add(i+1);
} else {
odd.add(i+1);
}
}
int count = 0;
if (even.size() > 1) {
int n = 0;
while (n + 1 < even.size() && count < len-1) {
System.out.println(even.get(n) + " " + even.get(n + 1));
n = n + 2;
count++;
}
}
if (odd.size() > 1) {
int n = 0;
while (n + 1 < odd.size() && count < len-1) {
System.out.println(odd.get(n) + " " + odd.get(n + 1));
n = n + 2;
count++;
}
}
--test;
}
}
}
| Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 8b310d9e4a7ee85b2fb767bd4b6950ea | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
static PrintWriter out=new PrintWriter(System.out);
public static void main (String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] input=br.readLine().trim().split(" ");
int numTestCases=Integer.parseInt(input[0]);
while(numTestCases-->0) {
input=br.readLine().trim().split(" ");
int n=Integer.parseInt(input[0]);
int[] arr=new int[2*n];
input=br.readLine().trim().split(" ");
for(int i=0;i<2*n;i++) {
arr[i]=Integer.parseInt(input[i]);
}
printSteps(arr);
}
out.flush();
out.close();
}
public static void printSteps(int[] arr)
{
int n=arr.length;
ArrayList<Integer> oddIndices=new ArrayList<>();
ArrayList<Integer> evenIndices=new ArrayList<>();
for(int i=0;i<n;i++) {
if(arr[i]%2==0) {
evenIndices.add(i);
}
else {
oddIndices.add(i);
}
}
int oddSize=oddIndices.size();
int evenSize=evenIndices.size();
if(evenSize==0) {
for(int i=0;i<oddSize-2;i+=2)
{
int index1=oddIndices.get(i)+1;
int index2=oddIndices.get(i+1)+1;
out.println(index1+" "+index2);
}
return;
}
if(oddSize==0) {
for(int i=0;i<evenSize-2;i+=2) {
int index1=evenIndices.get(i)+1;
int index2=evenIndices.get(i+1)+1;
out.println(index1+" "+index2);
}
return;
}
if(oddSize%2!=0 && evenSize%2!=0) {
for(int i=0;i<oddSize-1;i+=2) {
int index1=oddIndices.get(i)+1;
int index2=oddIndices.get(i+1)+1;
out.println(index1+" "+index2);
}
for(int i=0;i<evenSize-1;i+=2) {
int index1=evenIndices.get(i)+1;
int index2=evenIndices.get(i+1)+1;
out.println(index1+" "+index2);
}
}
else {
for(int i=0;i<oddSize-2;i+=2) {
int index1=oddIndices.get(i)+1;
int index2=oddIndices.get(i+1)+1;
out.println(index1+" "+index2);
}
for(int i=0;i<evenSize;i+=2) {
int index1=evenIndices.get(i)+1;
int index2=evenIndices.get(i+1)+1;
out.println(index1+" "+index2);
}
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 09eb846dfe9d8923ae41480db4206c8d | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 256 megabytes | import java.io.*;
public class Compression
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int test=Integer.parseInt(br.readLine());
for(int t=1;t<=test;t++)
{
int n=Integer.parseInt(br.readLine());
String s[]=br.readLine().split(" ");
int a[]=new int[2*n];
for(int x=0;x<2*n;x++)
{
a[x]=Integer.parseInt(s[x]);
}
int count=n-1;
for(int x=0;x<2*n;x++)
{
int index=0;
if(a[x]!=0)
{
for(int y=x+1;y<2*n;y++)
{
if(a[y]!=0)
{
if(a[x]%2==a[y]%2)
{
index=y;
a[x]=0;
a[y]=0;
count--;
break;
}
}
}
if(index!=0)
{
if(count==0)
{
System.out.println((x+1)+" "+(index+1));
break;
}
else
System.out.println((x+1)+" "+(index+1));
}
}
}
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 391c46796766a0b8bd53e3c8137506f8 | train_000.jsonl | 1592663700 | Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so. | 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();
Map<Integer,Integer> even = new HashMap<Integer,Integer>();
Map<Integer,Integer> odd = new HashMap<Integer,Integer>();
int a[] = new int[2*n];
int size = n-1;
for(int i=0;i<2*n;i++) {
a[i] = sc.nextInt();
}
for(int i=0;i<2*n;i++) {
if(a[i]%2==0) even.put(i+1,a[i]);
else odd.put(i+1,a[i]);
}
ArrayList<Integer> evenIndex = new ArrayList<>(even.keySet());
ArrayList<Integer> oddIndex = new ArrayList<>(odd.keySet());
int evenSize = 0;
int oddSize = 0;
int count=0;
if(even.size()>=2) {
for(int i=0;i<evenIndex.size();i+=2) {
if((i+1)==evenIndex.size()) break;
System.out.println(evenIndex.get(i)+" "+evenIndex.get(i+1));
count++;
if(count==size) break;
}
}
if(count<size) {
if(odd.size()>=2) {
for(int i=0;i<oddIndex.size();i+=2) {
if((i+1)==oddIndex.size()) break;
System.out.println(oddIndex.get(i)+" "+oddIndex.get(i+1));
count++;
if(count==size || (i+1)==oddIndex.size()) break;
}
}
}
t--;
}
}
} | Java | ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"] | 1 second | ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"] | NoteIn the first test case, $$$b = \{3+6, 4+5\} = \{9, 9\}$$$ and $$$\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \{9+10\} = \{19\}$$$ and $$$\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \{1+2, 3+3, 4+5, 90+3\} = \{3, 6, 9, 93\}$$$ and $$$\mathrm{gcd}(3, 6, 9, 93) = 3$$$. | Java 8 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 96fac9f9377bf03e144067bf93716d3d | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$. | 1,100 | For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any. | standard output | |
PASSED | 49fef5a0d65f50c4cc603ae04e44c76f | train_000.jsonl | 1304175600 | Fox Ciel saw a large field while she was on a bus. The field was a n × m rectangle divided into 1 × 1 cells. Some cells were wasteland, and other each cell contained crop plants: either carrots or kiwis or grapes. After seeing the field carefully, Ciel found that the crop plants of each cell were planted in following procedure: Assume that the rows are numbered 1 to n from top to bottom and the columns are numbered 1 to m from left to right, and a cell in row i and column j is represented as (i, j). First, each field is either cultivated or waste. Crop plants will be planted in the cultivated cells in the order of (1, 1) → ... → (1, m) → (2, 1) → ... → (2, m) → ... → (n, 1) → ... → (n, m). Waste cells will be ignored. Crop plants (either carrots or kiwis or grapes) will be planted in each cell one after another cyclically. Carrots will be planted in the first cell, then kiwis in the second one, grapes in the third one, carrots in the forth one, kiwis in the fifth one, and so on. The following figure will show you the example of this procedure. Here, a white square represents a cultivated cell, and a black square represents a waste cell. Now she is wondering how to determine the crop plants in some certain cells. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class B_79_Colorful_Field {
@SuppressWarnings("unused")
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt(), m = s.nextInt(), k = s.nextInt(), t = s.nextInt();
int[] is = new int[k];
for (int i = 0; i < k; ++i) {
is[i] = (s.nextInt() - 1) * m + s.nextInt() - 1;
}
Arrays.sort(is);
String[] r = { "Carrots", "Kiwis", "Grapes" };
for (; t-- > 0;) {
int key = (s.nextInt() - 1) * m + s.nextInt() - 1;
if (Arrays.binarySearch(is, key) >= 0) {
System.out.println("Waste");
continue;
}
int c = 0;
for (; c < is.length && is[c] < key; ++c)
;
System.out.println(r[(key - c) % 3]);
}
}
}
| Java | ["4 5 5 6\n4 3\n1 3\n3 3\n2 5\n3 2\n1 3\n1 4\n2 3\n2 4\n1 1\n1 1"] | 2 seconds | ["Waste\nGrapes\nCarrots\nKiwis\nCarrots\nCarrots"] | NoteThe sample corresponds to the figure in the statement. | Java 7 | standard input | [
"implementation",
"sortings"
] | bfef3f835357dae290620efabe650580 | In the first line there are four positive integers n, m, k, t (1 ≤ n ≤ 4·104, 1 ≤ m ≤ 4·104, 1 ≤ k ≤ 103, 1 ≤ t ≤ 103), each of which represents the height of the field, the width of the field, the number of waste cells and the number of queries that ask the kind of crop plants in a certain cell. Following each k lines contains two integers a, b (1 ≤ a ≤ n, 1 ≤ b ≤ m), which denotes a cell (a, b) is waste. It is guaranteed that the same cell will not appear twice in this section. Following each t lines contains two integers i, j (1 ≤ i ≤ n, 1 ≤ j ≤ m), which is a query that asks you the kind of crop plants of a cell (i, j). | 1,400 | For each query, if the cell is waste, print Waste. Otherwise, print the name of crop plants in the cell: either Carrots or Kiwis or Grapes. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.