HashMap and HashSet in Java
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84400
Problem D from Ecust match
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public int MAX_K = 1002;
public int n;
public int[] a = new int[2005];
public int[] b = new int[2005];
// The position of each point
private HashMap<String, HashSet<String>> datas;
public static void main(String[] s) {
Main context = new Main();
context.fMain();
}
private void fMain() {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
a[0] = sc.nextInt();
b[0] = sc.nextInt();
datas = new HashMap<String, HashSet<String>>();
// HashSet<Point> points = new HashSet<Point>();
// HashSet will never allow its child to be the same.
// Map will ensure all the keys will be the unique.
for (int i = 1; i < n; i++) {
a[i] = sc.nextInt();
b[i] = sc.nextInt();
fAdd(a[i], b[i], i);
}
long put = n * (n - 1) * (n - 2) / 6;
for(String line: datas.keySet()){
}
for (String line : datas.keySet()) {
int length = datas.get(line).size();
if (length <= 2) {
} else {
put -= length * (length - 1) * (length - 2) / 6;
}
}
System.out.print(put + "");
}
private void fAdd(int x, int y, int flag) {
double K, B;
for (int i = 0; i < flag; i++) {
// Find out B = ? & K = ?
B = x - a[i];
if (B == 0) {
K = MAX_K;
// B = x;
} else {
K = (y - b[i]) / B;
B = y - K * x;
}
// Add New Line or Add New Point
String id=K+"-"+B;
if (datas.containsKey(id)) {
HashSet<String> points = datas.get(id);
points.add(x+"-"+y);
} else {
HashSet<String> points = new HashSet<String>();
points.add(a[i]+"-"+b[i]);
points.add(x+"-"+y);
datas.put(id, points);
}
}
}
}
No matter what, to use object as a key is totally wrong ! !
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public int MAX_K = 1002;
// The value k that stands for the max number
public int n;
public int[] a = new int[2005];
public int[] b = new int[2005];
// The position of each point
private HashMap<Line, HashSet<Point>> datas;
// The whole data that contains all lines and the corresponded points
public static void main(String[] s) {
Main context = new Main();
context.fMain();
}
private void fMain() {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
a[0] = sc.nextInt();
b[0] = sc.nextInt();
datas = new HashMap<Line, HashSet<Point>>();
// HashSet<Point> points = new HashSet<Point>();
// HashSet will never allow its child to be the same.
// Map will ensure all the keys will be the unique.
for (int i = 1; i < n; i++) {
a[i] = sc.nextInt();
b[i] = sc.nextInt();
fAdd(a[i], b[i], i);
}
long put = n * (n - 1) * (n - 2) / 6;
for (Line line : datas.keySet()) {
int length = datas.get(line).size();
if (length <= 2) {
} else {
put -= length * (length - 1) * (length - 2) / 6;
}
}
System.out.print(put + "");
}
private void fAdd(int x, int y, int flag) {
double K, B;
for (int i = 0; i < flag; i++) {
// B = ? & K = ?
B = x - a[i];
if (B == 0) {
K = MAX_K;
// B = x;
} else {
K = (y - b[i]) / B;
B = y - K * x;
}
// Add New Line or Add New Point
Line newLine = new Line(K, B);
if (datas.containsKey(newLine)) {
HashSet<Point> points = datas.get(newLine);
points.add(new Point(x, y));
} else {
HashSet<Point> points = new HashSet<Point>();
points.add(new Point(a[i], b[i]));
points.add(new Point(x, y));
datas.put(newLine, points);
}
}
}
private class Line {
public double k;
public double b;
public Line(double k, double b) {
this.k = k;
this.b = b;
}
}
private class Point {
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
}
And C++ is better in this way
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
struct node
{
int x, y;
};
int n;
//node a[100005];
node a[2005];
int main()
{
long long ans;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%d%d", &a[i].x, &a[i].y);
}
ans = 0;
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
int x = a[i].x - a[j].x, y = a[i].y - a[j].y;
for(int k = j + 1; k < n; k++)
if(x * (a[k].y - a[i].y != y * (a[k].x - a[i].x))) ans++;
}
}
printf("%I64d\n", ans);
return 0;
}