Be cautious when using Map and Set in Java

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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值