您的位置:首页 > 编程语言 > Java开发

Google Code Jam Notes - Meet And Party - Java

2013-12-31 04:18 676 查看
Problem:

Retrieved from: https://code.google.com/codejam/contest/2929486/dashboard#s=p1

Little Sin lives in a Manhattan-grid city, a 2D plane where people can only go north, west, south or east along the grid. The distance from (x1, y1) to (x2, y2) is |x1 - x2| + |y1 - y2|.

Little Sin really likes to party and is hoping to host a house party in Manhattan this Sunday. Little Sin has collected a list of people who will attend, and now needs to decide at whose home she will host the party.
Little Sin invited all of the people in several rectangular areas, and all of those people have said yes. A rectangular area is denoted as (x1, y1, x2, y2), where x1 ≤ x2, y1 ≤ y2. People who live in a
rectangular area fill all integral points inside it. So there are a total of (x2 - x1 + 1) * (y2 - y1 + 1) people in the rectangular area (x1, y1, x2, y2).
Little Sin knows the coordinates of those rectangular areas. She wants the party to be hosted at the home of one of the people who is attending, but she also doesn't want everyone else to have to travel
very far: she wants to minimize the sum of all distances from all attendees' houses to the party. Can you help her?

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing a single integer: the number of rectangular
areas, B. Blines follow. Each line contains 4 integers: x1, y1, x2, y2, denoting the coordinates of a rectangular area of people Little Sin has invited to her party.

Output

For each test case, output one line containing "Case #t: x y d", where t is the case number (starting from 1) and (x, y) is the coordinates of the person whose home the party should be hosted. If there
are multiple positions with the same minimum total distance, choose the one with the smallest x. If there are still multiple positions, choose the one with the smallest y. The value d is the sum of the distances from all attendees' houses to the point (x,
y).

Limits

1 ≤ T ≤ 10.

|x1|, |y1|, |x2|, |y2| ≤ 109.

x1 ≤ x2, y1 ≤ y2.

The rectangular areas within a test case don't intersect.

Small dataset

1 ≤ B ≤ 100.

1 ≤ Total number of people in each test case ≤ 1000.

Large dataset

1 ≤ B ≤ 1000.

1 ≤ Total number of people in each test case ≤ 1000000.

Sample

Input

Output

2
1
0 0 2 2
3
-1 2 -1 2
0 0 0 0
1 3 1 3

Case #1: 1 1 12
Case #2: -1 2 6

Analysis:

Calculating the total distance can be divided to calculate the total horizontal distance and total vertical distance.

A new class Pair is created to record(x,y), and it also sort all the elements by x first, if there are multiple equal x, sort the elements by y. This pre-sort is in order to meet the problem requirement: If there are multiple positions with the same minimum
total distance, choose the one with the smallest x. If there are still multiple positions, choose the one with the smallest y.

Time Complexity O(N longN), N is the total number of people.

My solution: (Your opinion is highly appreciated)

package codeJam.google.com;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
* @author Zhenyi 2013 Dec 26, 2013 2:17:53 PM
*/
class Pair implements Comparable<Pair> {
Integer first;
Integer second;

@Override
public int compareTo(Pair o) {
int compare = this.first.compareTo(o.first);
return compare != 0 ? compare : this.second.compareTo(o.second);
}

public Pair(int x, int y) {
this.first = x;
this.second = y;
}

}

public class MeetAndParty {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new File(
"C:/Users/Zhenyi/Downloads/B-small-practice.in"));
FileWriter out = new FileWriter(
"C:/Users/Zhenyi/Downloads/B-small-practice.out");
// Scanner in = new Scanner(new
// File("C:/Users/Zhenyi/Downloads/B-large-practice.in"));
// FileWriter out = new
// FileWriter("C:/Users/Zhenyi/Downloads/B-large-practice.out");

int T = in.nextInt();

for (int cases = 1; cases <= T; cases++) {
int B = in.nextInt();
ArrayList<Integer> x = new ArrayList<Integer>();
ArrayList<Integer> y = new ArrayList<Integer>();
ArrayList<Pair> pair = new ArrayList<Pair>();
for (int i = 0; i < B; i++) {
int x1 = in.nextInt();
int y1 = in.nextInt();
int x2 = in.nextInt();
int y2 = in.nextInt();
for (int j = x1; j <= x2; j++) {
for (int k = y1; k <= y2; k++) {
x.add(j);
y.add(k);
pair.add(new Pair(j, k));
}
}
}
Collections.sort(x);
Collections.sort(y);
Collections.sort(pair);

ArrayList<Long> sumx = new ArrayList<Long>();
ArrayList<Long> sumy = new ArrayList<Long>();
sumx.add((long) 0);
sumy.add((long) 0);
for (int i = 0; i < x.size(); i++) {
Long tmp = sumx.get(sumx.size() - 1);
sumx.add(tmp + x.get(i));
}
for (int i = 0; i < y.size(); i++) {
Long tmp = sumy.get(sumy.size() - 1);
sumy.add(tmp + y.get(i));
}
long bestCost = Long.MAX_VALUE;
int resultx = 0;
int resulty = 0;

for (int i = 0; i < pair.size(); i++) {
int firstx = Collections.binarySearch(x, pair.get(i).first);
int firsty = Collections.binarySearch(y, pair.get(i).second);
long cost = (firstx + 1) * (long) pair.get(i).first
- sumx.get(firstx + 1) + sumx.get(sumx.size() - 1)
- sumx.get(firstx + 1) - (pair.size() - firstx - 1)
* (long) pair.get(i).first + (firsty + 1)
* (long) pair.get(i).second - sumy.get(firsty + 1)
+ sumy.get(sumy.size() - 1) - sumy.get(firsty + 1)
- (pair.size() - firsty - 1)
* (long) pair.get(i).second;
if (cost < bestCost) {
bestCost = cost;
resultx = pair.get(i).first;
resulty = pair.get(i).second;
}
}
out.write("Case #" + cases + ": " + resultx + " " + resulty + " "
+ bestCost + "\n");
System.out.print("Case #" + cases + ": " + resultx + " " + resulty
+ " " + bestCost + "\n");

}
in.close();
out.flush();
out.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: