Pagini recente » Cod sursa (job #1588750) | Cod sursa (job #1124112) | Cod sursa (job #1513290) | Cod sursa (job #2408099) | Cod sursa (job #1699606)
import java.awt.geom.Area;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
class Pair {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "(" + x + " " + y + ")";
}
}
public class Main {
static ArrayList<ArrayList<Integer>> cache;
public static void addToCache(int person, int n_stramos, int stramos) {
ArrayList<Integer> personStramosi = cache.get(person);
if (personStramosi == null) {
personStramosi = new ArrayList<Integer>(Collections.nCopies(n_stramos + 1, -1));
personStramosi.set(n_stramos, stramos);
// System.out.println("Se adauga in cache pentru persoana " + person + " al " + n_stramos + " stramos este persoana " + stramos);
cache.set(person, personStramosi);
} else {
if (personStramosi.size() < n_stramos) {
ArrayList<Integer> newStramosCache = new ArrayList<Integer>(Collections.nCopies(n_stramos + 1, -1));
newStramosCache.addAll(personStramosi);
newStramosCache.set(n_stramos, stramos);
// System.out.println("2. Se adauga in cache pentru persoana " + person + " al " + n_stramos + " stramos este persoana " + stramos);
cache.set(person, newStramosCache);
} else {
personStramosi.set(n_stramos, stramos);
}
}
}
public static Pair getFromCache(int person, int n_stramos) {
// System.out.println("se cauta in cache pentru persoana " + person + " al " + n_stramos + " stramos ");
ArrayList<Integer> personStramosi = cache.get(person);
if (personStramosi == null) {
return null;
} else {
if (personStramosi.size() + 1 < n_stramos) {
for (int i = personStramosi.size() -1 ; i >= 0; i--) {
if (personStramosi.get(i) != -1) {
// System.out.println("Se ia din cache pentru persoana " + person + " al " + i + " stramos este " + personStramosi.get(i));
return new Pair(personStramosi.get(i), i);
}
}
} else {
// System.out.println("Se ia din cache pentru persoana " + person + " al " + n_stramos + " stramos este " + personStramosi.get(n_stramos));
return new Pair(personStramosi.get(n_stramos), n_stramos);
}
}
return null;
}
public static void main(String[] args) throws IOException {
ArrayList<Integer> array = new ArrayList<Integer>();
ArrayList<Pair> pairs = new ArrayList<Pair>();
File file = new File("stramosi.in");
BufferedReader reader = null;
reader = new BufferedReader(new FileReader(file));
String text = null;
String first = reader.readLine();
String[] strings = first.split(" ");
int n = Integer.parseInt(strings[0]);
cache = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < n+1; i++){
cache.add(null);
}
// int m = Integer.parseInt(strings[1]);
first = reader.readLine();
strings = first.split(" ");
for (String s : strings) {
array.add(Integer.parseInt(s));
}
while ((text = reader.readLine()) != null) {
strings = text.split(" ");
pairs.add(new Pair(Integer.parseInt(strings[0]), Integer
.parseInt(strings[1])));
}
Writer wr = new FileWriter("stramosi.out");
for (Pair p : pairs) {
int index = p.x;
int nr = p.y;
while (nr != 0) {
Pair aux = getFromCache(index, nr);
if (aux != null) {
index = aux.x;
nr -= aux.y;
} else {
index = array.get(index - 1);
nr--;
}
if (index == 0)
break;
}
addToCache(p.x, p.y, index);
// System.out.println(index);
wr.write(Integer.toString(index));
wr.write("\n");
}
wr.close();
reader.close();
}
}