ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1003. Чётность

#1 TLE whats wrong? Java code
Послано Sammael 9 мар 2012 05:19
My program passed tests posted below but i got TLE in #1 test. I really don't know what is wrong with it. Could someone help me by telling me what is wrong or giving me some test? I would be very grateful.

TESTS:
100
5
5 6 odd
7 8 odd
1 6 even
1 4 odd
7 8 even
3
3
1 1 odd
3 3 odd
1 3 odd
5
4
1 2 even
4 5 even
1 5 odd
3 3 even
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
20
8
1 9 odd
10 17 odd
18 26 odd
4 5 odd
27 40 even
21 40 odd
1 20 odd
10 20 odd
200
8
1 9 odd
10 17 odd
18 26 odd
4 5 odd
27 40 even
21 40 odd
1 20 odd
10 20 odd
-1

ANSWERS:
4
3
3
3
2
6

MY CODE:
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Main test = new Main();
        List<Integer> results = new ArrayList<Integer>();

        // LOADING DATA
        while (true) {
            int length = input.nextInt();
            input.nextLine();
            if (length == -1) {
                break;
            } else {
                int hintCounter = input.nextInt();
                input.nextLine();

                // IF THERE IS NO ANSWER - CONTINUE
                if (hintCounter == 0) {
                    results.add(0);
                    continue;
                }

                List<Row> testData = new ArrayList<Row>();

                // READ ANSWERS
                for (int i = 0; i < hintCounter; i++) {
                    int start = input.nextInt();
                    int end = input.nextInt();
                    String parity = input.nextLine().trim();
                    Row newRow = test.new Row(start, end, (parity.equals("odd")) ? true : false);
                    testData.add(newRow);
                }

                Database db = test.new Database(length);
                int correctHints = 0;

                // ADD ANSWERS TO DATABASE AND CHECK AUTHENTICITY
                for (int i = 0; i < testData.size(); i++) {
                    Row r = testData.get(i);
                    db.addElement(r.start, r.end, r.odd);
                    if (db.isConsistent()) {
                        correctHints++;
                    } else {
                        break;
                    }
                }

                results.add(correctHints);
            }
        }

        // PRINT RESULTS
        for (int i = 0; i < results.size(); i++) {
            System.out.println(results.get(i));
        }

    }

    private class Row {
        int start;
        int end;
        Boolean odd;

        public Row(int pStart, int pEnd, Boolean pOdd) {
            start = pStart;
            end = pEnd;
            odd = pOdd;
        }
    }

    private class Database {
        HashMap<Integer, HashMap<Integer, Boolean>> data;
        Boolean consistent;
        int maxLength;
        int maxX;
        int maxY;
        int minX;
        int minY;

        public Database(int pMaxLength) {
            data = new HashMap<Integer, HashMap<Integer, Boolean>>();
            consistent = true;
            maxX = 0;
            maxY = 0;
            minX = 2000000000;
            minY = 2000000000;
            maxLength = pMaxLength;
        }

        public Boolean isConsistent() {
            return consistent;
        }

        public void addElement(int x, int y, Boolean odd) {

            consistent = ((x < 1 || y > maxLength) ? false : true);

            if (consistent) {
                if (data.containsKey(x)) {
                    // X ALREADY EXISTS
                    if (data.get(x).containsKey(y)) {
                        // Y ALSO EXISTS
                        consistent = (data.get(x).get(y).equals(odd) ? true : false);
                        return;
                    } else {
                        // Y NOT EXISTS
                        maxY = (y > maxY ? y : maxY);
                        minY = (y < minY ? y : minY);
                        data.get(x).put(y, odd);
                    }
                } else {
                    // X NOT EXISTS
                    maxX = (x > maxX ? x : maxX);
                    minX = (x < minX ? x : minX);
                    data.put(x, new HashMap<Integer, Boolean>());
                    data.get(x).put(y, odd);
                }

                // UPDATING THE DATA
                updatePefixes(x, y, odd);
                updateSuffixes(x, y, odd);
            } else {
                // DO NOTHING
            }

        }

        public void updatePefixes(int x, int y, Boolean odd) {
            // UPDATING PREFIXES
            for (int i = minX; i < x; i++) {

                if (data.containsKey(i) && data.get(i).containsKey(x - 1)) {
                    Boolean leftOdd = data.get(i).get(x - 1) ^ odd;
                    addElement(i, y, leftOdd);

                    for (int j = y + 1; j <= maxY; j++) {
                        if (data.containsKey(y + 1) && data.get(y + 1).containsKey(j)) {
                            addElement(i, j, leftOdd ^ data.get(y + 1).get(j));
                        } else {
                            // DO NOTHING
                        }
                    }
                } else {
                    // DO NOTHING
                }
            }
        }

        public void updateSuffixes(int x, int y, Boolean odd) {
            // UPDATING SUFFIXES
            for (int i = y + 1; i <= maxY; i++) {
                if (data.containsKey(y + 1) && data.get(y + 1).containsKey(i)) {
                    addElement(x, i, odd ^ data.get(y + 1).get(i));
                } else {
                    // DO NOTHING
                }
            }
        }

    }
}