Submission #66812421


Source Code Expand

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

#define INF 1234567890
#define ll long long
#define MOD 998244353

int T;
int N;
int L[505], R[505], P[505];
vector<int> g[505], v[505], res;

bool vis[505];
void DFS(int n, int st)
{
	vis[n] = true;
	if (st != n) v[st].push_back(n); // st는 n들에 의존성을 가진다.
	for(int next : g[n])
		if (!vis[next])
			DFS(next, st);
}

void Solve(int i)
{
	while(1)
	{
		int mn = INF;
		for(int j : v[i])
			if (!vis[j])
				mn = min(mn, j);
		if (mn == INF) // 의존성이 전부 해결된 경우
		{
			vis[i] = true;
			res.push_back(i);
			return;
		}
		Solve(mn);
	}
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);
	cin >> T;
	while(T--)
	{
		cin >> N;
		for(int i=1; i<=N; i++)
			cin >> L[i] >> R[i];

		// clear
		for(int i=1; i<=N; i++)
		{
			g[i].clear();
			v[i].clear();
		}
		res.clear();

		for(int i=1; i<=N; i++)
			for(int j=1; j<=N; j++)
				if (L[i] < L[j] && R[j] < R[i])
					g[i].push_back(j); // 위상정렬 역관계, P[j] < P[i]이어야 한다.
		
		for(int i=1; i<=N; i++)
		{
			memset(vis, 0, sizeof(vis));
			DFS(i, i);
		}

		// 1번 사람, 2번 사람, ...을 차례대로 뽑아야 한다.
		memset(vis, 0, sizeof(vis));
		for(int i=1; i<=N; i++)
			if (!vis[i])
				Solve(i);
		
		// res는 seat를 뽑는 순서이다. 사람을 뽑는 순서는 inv perm임
		int k = 1;
		for(int i : res)
			P[i] = k++;
		
		for(int i=1; i<=N; i++)
			cout << P[i] << " ";
		cout << "\n";
	}
	return 0;
}

Submission Info

Submission Time
Task C - Movie Theater
User edenooo
Language C++ 20 (gcc 12.2)
Score 700
Code Size 1611 Byte
Status AC
Exec Time 14 ms
Memory 4928 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 3432 KiB
01_small_00.txt AC 1 ms 3468 KiB
01_small_01.txt AC 1 ms 3472 KiB
01_small_02.txt AC 1 ms 3548 KiB
02_handmade_00.txt AC 1 ms 3616 KiB
02_handmade_01.txt AC 1 ms 3344 KiB
02_handmade_02.txt AC 14 ms 4928 KiB
02_handmade_03.txt AC 13 ms 4584 KiB
02_handmade_04.txt AC 14 ms 4760 KiB
03_random_00.txt AC 1 ms 3492 KiB
03_random_01.txt AC 1 ms 3348 KiB
03_random_02.txt AC 1 ms 3624 KiB
03_random_03.txt AC 1 ms 3468 KiB
03_random_04.txt AC 1 ms 3368 KiB
03_random_05.txt AC 1 ms 3448 KiB
03_random_06.txt AC 2 ms 3592 KiB
03_random_07.txt AC 1 ms 3572 KiB
03_random_08.txt AC 3 ms 3920 KiB
03_random_09.txt AC 4 ms 3928 KiB
03_random_10.txt AC 4 ms 3928 KiB
03_random_11.txt AC 4 ms 3960 KiB
03_random_12.txt AC 4 ms 4060 KiB
03_random_13.txt AC 3 ms 3928 KiB
03_random_14.txt AC 4 ms 3936 KiB
03_random_15.txt AC 3 ms 3968 KiB
03_random_16.txt AC 3 ms 4124 KiB
03_random_17.txt AC 3 ms 3804 KiB