Cod sursa(job #1699602)

Utilizator VladBogdanVlad Iulian Bogdan VladBogdan Data 7 mai 2016 21:42:34
Problema Stramosi Scor 0
Compilator java Status done
Runda Arhiva de probleme Marime 3.05 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;
	
	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);
			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);
				
				cache.set(person, newStramosCache);
			} else {
				personStramosi.set(n_stramos, stramos);
			}
		}
	}
	
	public static Pair getFromCache(int person, int n_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)
						return new Pair(personStramosi.get(i), i);
				}
			} else {
				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;
				}
				
				index = array.get(index - 1);

				if (index == 0)
					break;
				nr--;
			}

			addToCache(p.x, p.y, index);
			wr.write(Integer.toString(index));
			wr.write("\n");
		}
		wr.close();

		reader.close();
	}
}