Pagini recente » Cod sursa (job #1810776) | Cod sursa (job #2118690) | Istoria paginii runda/zxzx._jskds | Cod sursa (job #1976365) | Cod sursa (job #1357276)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
class MinHeap<T extends Comparable<T>> {
private List<T> values = new ArrayList<T>();
private Map<T, Integer> positions;
public MinHeap(Map<T, Integer> positions) {
this.positions = positions;
}
public T deleteValueFromPosition(int position) {
if (position < 0 || position >= values.size()) {
return null;
}
T value = values.get(position);
swap(position, values.size() - 1);
values.remove(values.size() - 1);
positions.remove(value);
int fatherPosition = fatherPosition(position);
if (fatherPosition >= 0
&& values.get(position).compareTo(values.get(fatherPosition)) < 0) {
percolate(position);
} else {
sift(position);
}
return value;
}
public void insertValue(T value) {
values.add(value);
positions.put(value, values.size() - 1);
percolate(values.size() - 1);
}
public T getMinValue() {
if (values.size() <= 0) {
return null;
}
return values.get(0);
}
private void sift(int position) {
// Going downwards
T currentValue = values.get(position);
int childPosition = getPositionOfMinimumChild(position);
if (childPosition < 0) {
return;
}
T minChildValue = values.get(childPosition);
if (minChildValue.compareTo(currentValue) >= 0) {
return;
}
swap(position, childPosition);
sift(childPosition);
}
private void swap(int position1, int position2) {
T value1 = values.get(position1);
T value2 = values.get(position2);
positions.put(value1, position2);
positions.put(value2, position1);
values.set(position1, value2);
values.set(position2, value1);
}
private int getPositionOfMinimumChild(int position) {
int childPosition = -1;
T value = values.get(position);
int leftPosition = leftChildPosition(position);
int rightPosition = rightChildPosition(position);
if (leftPosition >= 0 && values.get(leftPosition).compareTo(value) < 0) {
childPosition = leftPosition;
}
if (rightPosition >= 0
&& values.get(rightPosition).compareTo(value) < 0) {
childPosition = rightPosition;
}
return childPosition;
}
private void percolate(int position) {
// Going upwards
T value = values.get(position);
int fatherPosition = fatherPosition(position);
if (fatherPosition < 0) {
return;
}
if (value.compareTo(values.get(fatherPosition)) >= 0) {
return;
}
swap(position, fatherPosition);
percolate(fatherPosition);
}
private int fatherPosition(int position) {
if (position <= 0) {
return -1;
}
return (position - 1) / 2;
}
private int leftChildPosition(int position) {
int leftChildPosition = position * 2 + 1;
if (leftChildPosition >= values.size()) {
return -1;
}
return leftChildPosition;
}
private int rightChildPosition(int position) {
int rightChildPosition = (position + 1) * 2;
if (rightChildPosition >= values.size()) {
return -1;
}
return rightChildPosition;
}
}
public class Heaps {
public static void main(String args[]) throws IOException {
InputStream inputStream = new FileInputStream("heapuri.in");
Scanner scanner = new Scanner(inputStream);
OutputStream outputStream = new FileOutputStream("heapuri.out");
PrintWriter writer = new PrintWriter(outputStream);
Map<Integer, Integer> positions = new HashMap<Integer, Integer>();
List<Integer> order = new ArrayList<Integer>();
MinHeap<Integer> heap = new MinHeap<Integer>(positions);
int N = scanner.nextInt();
while (N-- > 0) {
int type = scanner.nextInt();
switch (type) {
case 1:
int value1 = scanner.nextInt();
order.add(value1);
heap.insertValue(value1);
break;
case 2:
int time = scanner.nextInt() - 1;
int value2 = order.get(time);
int position = positions.get(value2);
heap.deleteValueFromPosition(position);
break;
case 3:
int minValue = heap.getMinValue();
writer.println(String.valueOf(minValue));
break;
}
}
writer.flush();
inputStream.close();
scanner.close();
outputStream.close();
writer.close();
}
}