Submission #66812298


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
constexpr int MAXN = 1'000'007;
constexpr int INF = 2'000'000'000;
constexpr ll INFF = 1'000'000'000'000'000'000LL;
constexpr ll MOD = 998'244'353;
#define mkp make_pair
#define F first
#define S second
#define pb emplace_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> l(n), r(n);
    for (int i = 0; i < n; i++)
      cin >> l[i] >> r[i];

    vector<vector<int>> contain(n, vector<int>(n));
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (l[i] <= l[j] && r[i] >= r[j])
          contain[i][j] = 1;

    vector<int> used(n);
    vector<int> ans(n);
    vector<int> li(n), ri(n), lli, rri;
    for (int i = 0; i < n; i++)
      li[i] = -INF, ri[i] = INF;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        // cout << i << ' ' << j << ' ' << used[j] << '\n';
        if (used[j])
          continue;

        bool ok = 1;
        lli = li, rri = ri;
        vector<pii> ord;
        for (int k = i + 1; k < n; k++) {
          if (contain[i][k])
            rri[k] = min(rri[k], j - 1);
          else if (contain[k][i])
            lli[k] = max(lli[k], j + 1);
          ord.pb(mkp(lli[k], rri[k]));
        }
        sort(all(ord));
        int idx = 0;
        priority_queue<int, vector<int>, greater<int>> pq;
        for (int k = 0; k < n; k++) {
          if (used[k] || k == j)
            continue;
          while (idx < sz(ord) && ord[idx].F <= k) {
            pq.push(ord[idx].S);
            idx++;
          }
          if (pq.empty() || k > pq.top()) {
            ok = 0;
            break;
          }
          pq.pop();
        }

        if (ok) {
          ans[i] = j;
          used[j] = 1;
          for (int k = i + 1; k < n; k++) {
            if (contain[i][k])
              ri[k] = min(ri[k], j - 1);
            else if (contain[k][i])
              li[k] = max(li[k], j + 1);
          }
          break;
        }
      }
    }
    for (int i : ans)
      cout << i + 1 << ' ';
    cout << '\n';
  }
}
/*
- How to come up with solutions? (https://hackmd.io/-3cIVAFSQMC3iJTh9EpszA)
  - Play with some small examples.
  - Make observations (or random guesses) by intuition.
    - Try to find counterexamples of the "observations."
  - Find the characteristics of an optimal solution.
  - Try to solve simple cases.
  - Brute force and print out the results.
  - Pick a method (greedy, dp, d&c, ...)
    - But DO NOT force algos on problems!
  - IF YOU'RE STUCK, TRY SOMETHING ELSE! Make new observations!

- Before writing the solution:
  - check TL/ML/correctness of samples/implementation details!

- Pre-submit:
  - Did you make a typo when copying a template?
  - Test more cases if unsure.
    - Write a naive solution and check small cases.
  - Submit the correct file.

- Debugging:
  - General Debugging:
    - Read the whole problem again.
    - Think over the algorithm again.
    - Go to the toilet.

  - Wrong Answer:
    - Any possible overflows?
      - > `__int128` ?
      - Try `-ftrapv` or `#pragma GCC optimize("trapv")`
    - Floating point errors?
      - > `long double` ?
      - turn off math optimizations
      - check for `==`, `>=`, `acos(1.000000001)`, etc.
    - Did you forget to sort or unique?
    - Generate large and worst "corner" cases.
    - Check your `m` / `n`, `i` / `j`,  `x` / `y` and `<` / `>`.
    - Are everything initialized or reset properly?
    - Are you sure about the STL thing you are using?
      - Read cppreference.
    - Print everything and run it on pen and paper.

  - Time Limit Exceeded:
    - Calculate your time complexity again.
    - Does the program actually end?
      - Check for `while(q.size())` etc.
    - Test the largest cases locally.
    - Did you do unnecessary stuff?
      - e.g. pass vectors by value
      - e.g. `memset` for every test case
    - Is your constant factor reasonable?

  - Runtime Error:
    - Check memory usage.
      - Forget to clear or destroy stuff?
      - > `vector::shrink_to_fit()`
    - Stack overflow?
    - Bad pointer / array access?
      - Try `-fsanitize=address`
    - Division by zero? NaN's?
*/

Submission Info

Submission Time
Task C - Movie Theater
User henotrix
Language C++ 20 (gcc 12.2)
Score 700
Code Size 4512 Byte
Status AC
Exec Time 551 ms
Memory 4640 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 1
AC × 27
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 02_handmade_00.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 03_random_00.txt, 03_random_01.txt, 03_random_02.txt, 03_random_03.txt, 03_random_04.txt, 03_random_05.txt, 03_random_06.txt, 03_random_07.txt, 03_random_08.txt, 03_random_09.txt, 03_random_10.txt, 03_random_11.txt, 03_random_12.txt, 03_random_13.txt, 03_random_14.txt, 03_random_15.txt, 03_random_16.txt, 03_random_17.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3396 KiB
01_small_00.txt AC 1 ms 3624 KiB
01_small_01.txt AC 1 ms 3408 KiB
01_small_02.txt AC 1 ms 3460 KiB
02_handmade_00.txt AC 4 ms 4552 KiB
02_handmade_01.txt AC 4 ms 4448 KiB
02_handmade_02.txt AC 551 ms 4492 KiB
02_handmade_03.txt AC 4 ms 4504 KiB
02_handmade_04.txt AC 294 ms 4508 KiB
03_random_00.txt AC 1 ms 3488 KiB
03_random_01.txt AC 1 ms 3440 KiB
03_random_02.txt AC 2 ms 3544 KiB
03_random_03.txt AC 2 ms 3448 KiB
03_random_04.txt AC 7 ms 3588 KiB
03_random_05.txt AC 7 ms 3476 KiB
03_random_06.txt AC 33 ms 3732 KiB
03_random_07.txt AC 24 ms 3480 KiB
03_random_08.txt AC 365 ms 4356 KiB
03_random_09.txt AC 385 ms 4516 KiB
03_random_10.txt AC 364 ms 4500 KiB
03_random_11.txt AC 385 ms 4416 KiB
03_random_12.txt AC 387 ms 4360 KiB
03_random_13.txt AC 360 ms 4548 KiB
03_random_14.txt AC 402 ms 4640 KiB
03_random_15.txt AC 369 ms 4448 KiB
03_random_16.txt AC 380 ms 4316 KiB
03_random_17.txt AC 358 ms 4448 KiB