Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import lib.InputReader;
- import java.io.PrintWriter;
- import java.util.*;
- public class TaskB {
- static List<Point> all = new ArrayList<>();
- static class Point {
- int x;
- int y;
- char dir = '?';
- Point prevX;
- Point prevY;
- Point nextX;
- Point nextY;
- Point savePrevX;
- Point savePrevY;
- Point saveNextX;
- Point saveNextY;
- public Point() {
- all.add(this);
- }
- }
- public void solve(int testNumber, InputReader in, PrintWriter out) {
- int n = in.nextInt();
- Point[] p = new Point[n];
- for (int i = 0; i < n; ++i) {
- p[i] = new Point();
- p[i].x = in.nextInt();
- p[i].y = in.nextInt();
- p[i].dir = in.next().charAt(0);
- }
- Map<Integer, Point> sentinelX = new HashMap<>();
- Map<Integer, Point> sentinelY = new HashMap<>();
- Arrays.sort(p, new Comparator<Point>() {
- @Override
- public int compare(Point o1, Point o2) {
- return o1.x - o2.x;
- }
- });
- for (Point pp : p) {
- Point s = sentinelX.get(pp.y);
- if (s == null) {
- s = new Point();
- s.nextX = s;
- s.prevX = s;
- sentinelX.put(pp.y, s);
- }
- pp.nextX = s;
- pp.prevX = s.prevX;
- pp.nextX.prevX = pp;
- pp.prevX.nextX = pp;
- }
- Arrays.sort(p, new Comparator<Point>() {
- @Override
- public int compare(Point o1, Point o2) {
- return o1.y - o2.y;
- }
- });
- for (Point pp : p) {
- Point s = sentinelY.get(pp.x);
- if (s == null) {
- s = new Point();
- s.nextY = s;
- s.prevY = s;
- sentinelY.put(pp.x, s);
- }
- pp.nextY = s;
- pp.prevY = s.prevY;
- pp.nextY.prevY = pp;
- pp.prevY.nextY = pp;
- }
- for (Point pp : all) {
- pp.savePrevX = pp.prevX;
- pp.saveNextX = pp.nextX;
- pp.savePrevY = pp.prevY;
- pp.saveNextY = pp.nextY;
- }
- int res = 0;
- for (int i = 0; i < n; ++i) {
- for (Point pp : all) {
- pp.prevX = pp.savePrevX;
- pp.nextX = pp.saveNextX;
- pp.prevY = pp.savePrevY;
- pp.nextY = pp.saveNextY;
- }
- int cnt = 0;
- Point at = p[i];
- while (at.dir != '?') {
- ++cnt;
- at.nextX.prevX = at.prevX;
- at.prevX.nextX = at.nextX;
- at.nextY.prevY = at.prevY;
- at.prevY.nextY = at.nextY;
- switch (at.dir) {
- case '>':
- at = at.nextX;
- break;
- case '<':
- at = at.prevX;
- break;
- case '^':
- at = at.prevY;
- break;
- case 'v':
- at = at.nextY;
- break;
- default:
- throw new RuntimeException("" + at.dir);
- }
- }
- res = Math.max(res, cnt);
- }
- out.println(res);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement