import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; public class Flip { private int n; private int m; private Scanner scanner = new Scanner(System.in); ArrayList> operations; ArrayList listaNr; public Flip(){ } public void setN(int value) { this.n = value; } public void setM(int value) { this.m = value; } public void constructList() { listaNr = new ArrayList(); for(int i=0; i>(); List list = new ArrayList(); File file = new File("inputFile.txt"); if(file.exists()){ try { list = Files.readAllLines(file.toPath(),Charset.defaultCharset()); } catch (IOException ex) { ex.printStackTrace(); } if(list.isEmpty()) return; } int i = 0; ArrayList listaOp; for(String line : list){ String [] res = line.split(" "); if(i == 0) { this.n = Integer.parseInt(res[0]); this.m = Integer.parseInt(res[1]); } else { if (res[0].equals("1")) { listaOp = new ArrayList(); listaOp.add(Integer.parseInt(res[0])); listaOp.add(Integer.parseInt(res[1])); listaOp.add(Integer.parseInt(res[2])); operations.add(listaOp); } if (res[0].equals("2")) { listaOp = new ArrayList(); listaOp.add(Integer.parseInt(res[0])); listaOp.add(Integer.parseInt(res[1])); operations.add(listaOp); } } i++; } } public void changeValues(int x, int y) { for(int i = x - 1; i <= y - 1; i++) { if (listaNr.get(i) == 0) listaNr.set(i, 1); else listaNr.set(i, 0); } } public int[] getBoundaries(int p) { p--; int[] boundaries = new int[3]; int lower = p, upper = p; int nr = listaNr.get(p); boundaries[0] = nr; //System.out.print(" "+nr + " " + p +" \n"); boolean ok = true; while(lower > 0) { lower--; if (listaNr.get(lower) != nr) { ok = false; boundaries[1] = lower + 2; //System.out.print("lower1 " + (lower + 2) + "in lista " + listaNr.get(lower) + " \n"); break; } } if (ok){ boundaries[1] = lower + 1; //System.out.print("lower2 " + (lower + 1) + " \n"); } //System.out.print("lower" + lower + "\n"); ok = true; while(upper < listaNr.size() - 1) { upper++; //System.out.print("cacatu: " + listaNr.get(upper) +" " + nr + "\n"); if (listaNr.get(upper) != nr) { ok = false; boundaries[2] = upper; //System.out.print("upper1 " + upper + "in lista " + listaNr.get(lower) + " \n"); break; } } if (ok) { boundaries[2] = upper + 1; //System.out.print("upper2 " + (upper + 1) + " \n"); } //System.out.print("upper " + upper + "\n"); //System.out.print("Boundaries: " + boundaries[0] + " " + boundaries[1] + " " + boundaries[2] + "\n"); return boundaries; } public ArrayList calculate() { ArrayList result = new ArrayList(); System.out.println("Size operations: " + operations.size()); for(ArrayList lista: operations) { if (lista.get(0) == 1) { this.changeValues(lista.get(1), lista.get(2)); } if (lista.get(0) == 2) { result.add(this.getBoundaries(lista.get(1))); } } return result; } public String toString() { String text = "Flip: \n"; this.readOperations(); this.constructList(); ArrayList result = this.calculate(); for(int[] lista: result) { text = text + lista[0] + " " + lista[1]+ " " + lista[2] + "\n"; } return text; } }