**Problem**

A company is located in two very tall buildings. The company intranet connecting the buildings consists of many wires, each connecting a window on the first building to a window on the second building.

You are looking at those buildings from the side, so that one of the buildings is to the left and one is to the right. The windows on the left building are seen as points on its right wall, and the windows on the right building are seen as points on its left wall. Wires are straight segments connecting a window on the left building to a window on the right building.

You've noticed that no two wires share an endpoint (in other words, there's at most one wire going out of each window). However, from your viewpoint, some of the wires intersect midway. You've also noticed that exactly two wires meet at each intersection point.

On the above picture, the intersection points are the black circles, while the windows are the white circles.

How many intersection points do you see?

### Input

The first line of the input gives the number of test cases,**T**.

**T**test cases follow. Each case begins with a line containing an integer

**N**, denoting the number of wires you see.

The next

**N**lines each describe one wire with two integers

**A**and

_{i}**B**. These describe the windows that this wire connects:

_{i}**A**is the height of the window on the left building, and

_{i}**B**is the height of the window on the right building.

_{i}### Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of intersection points you see.### Limits

1 ≤**T**≤ 15.

1 ≤

**A**≤ 10

_{i}^{4}.

1 ≤

**B**≤ 10

_{i}^{4}.

Within each test case, all

**A**are different.

_{i}Within each test case, all

**B**are different.

_{i}No three wires intersect at the same point.

#### Small dataset

1 ≤**N**≤ 2.

#### Large dataset

1 ≤**N**≤ 1000.

### Sample

**Solution**

This is one of the simpler problems that I was able to solve in the Codejam round. Following is the source code.

private long solve(int[] a, int[] b) { PointMesh p = new PointMesh(); for(int i=0;i<a.length;i++){ p.add(a[i], b[i]); } return p.intersects; } class PointMesh{ long intersects; List<Line2D> lineList = new ArrayList<Line2D>(); Line2D line1 = new Line2D.Double(); Line2D r = new Line2D.Double(); void add(int a,int b){ Point2D p1 = new Point2D.Double(0, a); Point2D p2 = new Point2D.Double(10, b); Line2D newLine = new Line2D.Double(p1,p2); for(Line2D eachLine:lineList){ if(eachLine.intersectsLine(newLine)) intersects++; lineList.add(newLine); } }

Complete source code : here

Practice inputs : here

Cheers!!

Bragaadeesh.

## No comments:

Post a Comment