Cod sursa(job #1699613)

Utilizator VladBogdanVlad Iulian Bogdan VladBogdan Data 7 mai 2016 22:14:17
Problema Stramosi Scor 30
Compilator java Status done
Runda Arhiva de probleme Marime 3.73 kb
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;
	
	static int[][] cache;
	
	public static void addToCache(int person, int n_stramos, int stramos) {
		cache[person][n_stramos] = stramos;
//		System.out.println("Se adauga in cache pentru persoana " +  person + " al " + n_stramos + " stramos este persoana " + 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");
		int[] lineCache = cache[person];
		
		for (int i = n_stramos; i >= 0; i--) {
			if (lineCache[i] != -1) {
//				System.out.println("Se ia din cache pentru persoana " + person + " al " + i + " stramos este " +lineCache[i]);
				return new Pair(lineCache[i], i);
			}
		}
		
		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 int[n + 1][n + 1];
		 
		 for (int i = 0; i <= n; i++) {
			 for (int j = 0; j <= n; j++) {
				 cache[i][j] = -1;
			 }
		 }
//		 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();
	}
}