Skip to content

Instantly share code, notes, and snippets.

@chermehdi
Created December 22, 2017 22:41
Show Gist options
  • Save chermehdi/102dc2d4c4edc6cddaa479e41748027c to your computer and use it in GitHub Desktop.
Save chermehdi/102dc2d4c4edc6cddaa479e41748027c to your computer and use it in GitHub Desktop.
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.util.Set;
import java.util.HashMap;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.UnsupportedEncodingException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author MaxHeap
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
Anonymous solver = new Anonymous();
solver.solve(1, in, out);
out.close();
}
static class Anonymous {
public void solve(int testNumber, FastReader in, OutputWriter out) {
int t = in.nextInt();
int num = 1;
for (int tc = 1; tc <= t; tc++) {
int n = in.nextInt();
User[] users = new User[n];
HashMap<String, List<String>> map = new HashMap<>();
for (int i = 0; i < n; i++) {
String name = in.next();
int x = in.nextInt();
int y = in.nextInt();
int d = in.nextInt();
users[i] = new User(name, x, y, d);
map.put(name, new ArrayList<>());
String[] line = in.nextLine().split(" ");
for (String s : line) {
users[i].has.add(Integer.parseInt(s));
}
line = in.nextLine().split(" ");
for (String s : line) {
users[i].looking.add(Integer.parseInt(s));
}
}
boolean found = false;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (users[i].isCompatible(users[j])) {
map.get(users[i].name).add(users[j].name);
found = true;
}
}
}
out.printf("Case %d :\n", num);
for (int i = 0; i < n; i++) {
out.print(users[i].name + " :");
if (map.get(users[i].name).isEmpty()) {
out.println(" No profile found!");
} else {
for (String name : map.get(users[i].name)) {
out.print(" " + name);
}
if (tc != t - 1)
out.println();
}
}
num++;
}
}
class User {
String name;
int x;
int y;
int d;
Set<Integer> has;
Set<Integer> looking;
public User(String name, int x, int y, int d) {
this.name = name;
this.x = x;
this.y = y;
this.d = d;
has = new HashSet<>();
looking = new HashSet<>();
}
int distance(User u) {
return Math.abs(x - u.x) + Math.abs(y - u.y);
}
boolean isCompatible(User user) {
if (d != 0) {
if (distance(user) <= d) {
for (int i : looking) {
if (!user.has.contains(i)) return false;
}
return true;
}
return false;
}
for (int i : looking) {
if (!user.has.contains(i)) return false;
}
return true;
}
}
}
static class OutputWriter extends PrintWriter {
public OutputWriter(OutputStream os, boolean autoFlush) {
super(os, autoFlush);
}
public OutputWriter(Writer out) {
super(out);
}
public OutputWriter(Writer out, boolean autoFlush) {
super(out, autoFlush);
}
public OutputWriter(String fileName) throws FileNotFoundException {
super(fileName);
}
public OutputWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
super(fileName, csn);
}
public OutputWriter(File file) throws FileNotFoundException {
super(file);
}
public OutputWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
super(file, csn);
}
public OutputWriter(OutputStream out) {
super(out);
}
public void flush() {
super.flush();
}
public void close() {
super.close();
}
}
static class FastReader {
BufferedReader reader;
StringTokenizer st;
public FastReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public String nextLine() {
String s = null;
try {
s = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment