Skip to content

Instantly share code, notes, and snippets.

@frostiq
Created December 22, 2013 19:06
Show Gist options
  • Save frostiq/8086911 to your computer and use it in GitHub Desktop.
Save frostiq/8086911 to your computer and use it in GitHub Desktop.
/**
* Created with IntelliJ IDEA.
* User: Аляксандр
* Date: 21.12.13
* Time: 8.27
* To change this template use File | Settings | File Templates.
*/
interface IInterval extends Cloneable, Comparable<IInterval>{
Number getBegin();
Number getEnd();
public IInterval clone() throws CloneNotSupportedException;
}
abstract class Interval implements IInterval{
@Override
public Number getBegin() {
return begin; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public Number getEnd() {
return end; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public int compareTo(IInterval obj) {
int res;
if((begin.doubleValue() <= obj.getBegin().doubleValue()) && (end.doubleValue() >= obj.getEnd().doubleValue()))
res = 1;
else if((obj.getBegin().doubleValue() <= begin.doubleValue()) && (obj.getEnd().doubleValue() >= end.doubleValue()))
res = -1;
else
res = 0;
return res;
}
@Override
public Interval clone() throws CloneNotSupportedException {
return (Interval)super.clone();
}
@Override
public String toString() {
return "["+begin+"; "+end+"]";
}
Number begin;
Number end;
}
class IntervalInteger extends Interval{
public IntervalInteger(Integer begin, Integer end){
this.begin = begin;
this.end = end;
}
}
class IntervalDouble extends Interval{
public IntervalDouble(Double begin, Double end){
this.begin = begin;
this.end = end;
}
}
/**
* Created with IntelliJ IDEA.
* User: Аляксандр
* Date: 21.12.13
* Time: 8.35
* To change this template use File | Settings | File Templates.
*/
import java.lang.reflect.*;
import java.util.*;
public class IntervalArray<T extends IInterval> implements Cloneable, Iterable{
public IntervalArray(){}
public IntervalArray(int size, Class<T> elementType) {
this.elementType = elementType;
arr = (T[]) Array.newInstance(elementType, size);
//arr = (T[]) new Object[size];
}
public T getElem(int i) {
if(0<=i && i<arr.length)
return arr[i];
else
throw new NoSuchElementException();
}
public void setElem(T elem, int i) {
if(0<=i && i<arr.length)
this.arr[i] = elem;
else
throw new NoSuchElementException();
}
public T longestInterval(){
Number min = arr[0].getBegin();
Number max = arr[0].getEnd();
for(int i=1; i<arr.length; ++i){
if (min.doubleValue() > arr[i].getBegin().doubleValue()) min = arr[i].getBegin();
if (max.doubleValue() < arr[i].getEnd().doubleValue()) max = arr[i].getEnd();
}
T res;
try{
Constructor c[] = elementType.getConstructors();
res = (T) c[0].newInstance(min,max);
return res;
}
catch (InvocationTargetException e){
System.out.println();
}
catch (InstantiationException e){
System.out.println(e);
}
catch (IllegalAccessException e){
System.out.println(e);
}
return null;
}
int compareLongestInterval(IntervalArray<?> other){
return longestInterval().compareTo(other.longestInterval());
}
void setFirstLongestInterval(IntervalArray<? extends IInterval> other, MyPair<? super IInterval> pair){
if (compareLongestInterval(other)<0){
pair.setFirst(getElem(0));
pair.setSecond(longestInterval());
}
else {
pair.setFirst(other.getElem(0));
pair.setSecond(other.longestInterval());
}
}
@Override
public IntervalArray clone() throws CloneNotSupportedException {
IntervalArray copy = (IntervalArray)super.clone();
for (int i = 0; i < arr.length; i++) {
copy.arr[i] = arr[i].clone();
}
return copy;
}
@Override
public Iterator<T> iterator() {
return new IntervalArrayIterator<T>(arr);
}
protected T[] arr;
private Class<T> elementType;
}
class IntervalArrayIterator<T extends IInterval> implements Iterator<T> {
private T array[];
private int pos = 0;
public IntervalArrayIterator(T anArray[]) {
array = anArray;
}
@Override
public boolean hasNext() {
return pos < array.length;
}
@Override
public T next() throws NoSuchElementException {
if (hasNext())
return array[pos++];
else
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
/**
* Created with IntelliJ IDEA.
* User: Аляксандр
* Date: 21.12.13
* Time: 8.58
* To change this template use File | Settings | File Templates.
*/
public class MyPair<T> {
MyPair(T first, T second){
this.first = first;
this.second = second;
}
MyPair(){};
public T getFirst() {
return first;
}
public void setFirst(T first) {
this.first = first;
}
public T getSecond() {
return second;
}
public void setSecond(T second) {
this.second = second;
}
T first;
T second;
}
/**
* Created with IntelliJ IDEA.
* User: Аляксандр
* Date: 21.12.13
* Time: 8.36
* To change this template use File | Settings | File Templates.
*/
import java.io.*;
import java.util.*;
public class Runner {
public static void main(String[] args){
try {
BufferedReader f = new BufferedReader(new FileReader(new File("in.txt")));
Scanner sc = new Scanner(f);
int size = sc.nextInt();
IntervalArray<IntervalInteger> intervInt = new IntervalArray<IntervalInteger>(size, IntervalInteger.class);
for (int i = 0; i < size; i++) {
intervInt.setElem(new IntervalInteger(sc.nextInt(),sc.nextInt()),i);
}
size = sc.nextInt();
IntervalArray<IntervalDouble> intervDouble = new IntervalArray<IntervalDouble>(size, IntervalDouble.class);
for (int i = 0; i < size; i++) {
intervDouble.setElem(new IntervalDouble(sc.nextDouble(),sc.nextDouble()),i);
}
MyPair<IInterval> pair = new MyPair<IInterval>();
intervInt.setFirstLongestInterval(intervDouble,pair);
System.out.println(pair.getFirst()+" "+pair.getSecond());
sc.close();
}
catch (Exception e){
System.out.println(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment