제출 #207904

#제출 시각아이디문제언어결과실행 시간메모리
207904E869120Building Skyscrapers (CEOI19_skyscrapers)C++14
71 / 100
933 ms91512 KiB
#include <iostream> #include <map> #include <queue> #include <vector> #include <algorithm> #include <functional> using namespace std; #pragma warning (disable: 4996) int N, T, X[1 << 18], Y[1 << 18]; map<pair<int, int>, int> Map; vector<int> G[1 << 18]; bool used[1 << 18]; void solve_subtask1() { for (int i = 1; i <= N; i++) Map[make_pair(X[i], Y[i])] = i; for (int i = 1; i <= N; i++) { int dx[8] = { 1, 1, 1, 0, -1, -1, -1, 0 }; int dy[8] = { -1, 0, 1, 1, 1, 0, -1, -1 }; for (int j = 0; j < 8; j++) { int ex = X[i] + dx[j], ey = Y[i] + dy[j]; if (Map[make_pair(ex, ey)] >= 1) { G[i].push_back(Map[make_pair(ex, ey)]); } } } priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q; Q.push(make_pair(X[1], Y[1])); vector<int> I; while (!Q.empty()) { pair<int, int> pos = Q.top(); Q.pop(); int idx = Map[pos]; if (used[idx] == true) continue; used[idx] = true; I.push_back(idx); for (int i : G[idx]) { if (used[i] == true) continue; Q.push(make_pair(X[i], Y[i])); } } if ((int)I.size() != N) { printf("NO\n"); } else { printf("YES\n"); for (int i = 0; i < I.size(); i++) printf("%d\n", I[i]); } } bool reached[2009][2009]; bool colored[2009][2009]; int nums[2009][2009]; queue<pair<int, int>> Que; void calc() { while (!Que.empty()) { pair<int, int> pos = Que.front(); Que.pop(); int dx[4] = { -1, 0, 1, 0 }; int dy[4] = { 0, 1, 0, -1 }; for (int i = 0; i < 4; i++) { int ex = pos.first + dx[i], ey = pos.second + dy[i]; if (ex < 0 || ey < 0 || ex > N + 1 || ey > N + 1) continue; if (reached[ex][ey] == true || colored[ex][ey] == true) continue; reached[ex][ey] = true; Que.push(make_pair(ex, ey)); } } } bool cond1[2009], cond2[2009]; int low[2009], ord[2009], cnts; void dfs2(int pos, int par) { cnts++; ord[pos] = cnts; low[pos] = cnts; int deg = 0; for (int i : G[pos]) { if (ord[i] == -1) { dfs2(i, pos); deg++; low[pos] = min(low[pos], low[i]); if (ord[pos] <= low[i]) cond2[pos] = false; } else { if (i != par) low[pos] = min(low[pos], ord[i]); } } if (pos == 1 && deg >= 2) cond2[pos] = false; } int solve() { cnts = 0; for (int i = 1; i <= N; i++) { G[i].clear(); cond1[i] = false; cond2[i] = true; low[i] = -1; ord[i] = -1; } for (int i = 1; i <= N; i++) { int dx[4] = { -1, 0, 1, 0 }; int dy[4] = { 0, 1, 0, -1 }; for (int j = 0; j < 4; j++) { int ex = X[i] + dx[j], ey = Y[i] + dy[j]; if (reached[ex][ey] == true) { cond1[i] = true; break; } } } for (int i = 1; i <= N; i++) { if (colored[X[i]][Y[i]] == false) continue; int dx[8] = { 1, 1, 1, 0, -1, -1, -1, 0 }; int dy[8] = { -1, 0, 1, 1, 1, 0, -1, -1 }; for (int j = 0; j < 8; j++) { int ex = X[i] + dx[j], ey = Y[i] + dy[j]; if (ex < 0 || ey < 0 || ex > N + 1 || ey > N + 1) continue; if (nums[ex][ey] >= 1 && colored[ex][ey] == true) G[i].push_back(nums[ex][ey]); } } dfs2(1, -1); for (int i = 1; i <= N; i++) { if (colored[X[i]][Y[i]] == true && ord[i] == -1) return -1; } for (int i = N; i >= 1; i--) { if (colored[X[i]][Y[i]] == true && cond1[i] == true && cond2[i] == true) return i; } return -2; } void solve_subtask2() { int lx = (1 << 30), ly = (1 << 30); for (int i = 1; i <= N; i++) { lx = min(lx, X[i]); ly = min(ly, Y[i]); } for (int i = 1; i <= N; i++) { X[i] -= lx; X[i]++; Y[i] -= ly; Y[i]++; if (X[i] >= N + 1 || Y[i] >= N + 1) { printf("NO\n"); return; } colored[X[i]][Y[i]] = true; nums[X[i]][Y[i]] = i; } for (int i = 1; i <= N; i++) { Que.push(make_pair(0, i)); reached[0][i] = true; } for (int i = 1; i <= N; i++) { Que.push(make_pair(N + 1, i)); reached[N + 1][i] = true; } for (int i = 1; i <= N; i++) { Que.push(make_pair(i, 0)); reached[i][0] = true; } for (int i = 1; i <= N; i++) { Que.push(make_pair(i, N + 1)); reached[i][N + 1] = true; } calc(); vector<int> I; for (int i = 1; i <= N; i++) { int idx = solve(); if (idx == -1) break; I.push_back(idx); colored[X[idx]][Y[idx]] = false; reached[X[idx]][Y[idx]] = true; Que.push(make_pair(X[idx], Y[idx])); calc(); } reverse(I.begin(), I.end()); if ((int)I.size() != N) { printf("NO\n"); } else { printf("YES\n"); for (int i = 0; i < I.size(); i++) printf("%d\n", I[i]); } } int main() { scanf("%d%d", &N, &T); for (int i = 1; i <= N; i++) scanf("%d%d", &X[i], &Y[i]); if (T == 1) { solve_subtask1(); } else { solve_subtask2(); } return 0; }

컴파일 시 표준 에러 (stderr) 메시지

skyscrapers.cpp:8:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning (disable: 4996)
 
skyscrapers.cpp: In function 'void solve_subtask1()':
skyscrapers.cpp:45:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < I.size(); i++) printf("%d\n", I[i]);
                   ~~^~~~~~~~~~
skyscrapers.cpp: In function 'void solve_subtask2()':
skyscrapers.cpp:165:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < I.size(); i++) printf("%d\n", I[i]);
                   ~~^~~~~~~~~~
skyscrapers.cpp: In function 'int main()':
skyscrapers.cpp:170:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &T);
  ~~~~~^~~~~~~~~~~~~~~~
skyscrapers.cpp:171:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 1; i <= N; i++) scanf("%d%d", &X[i], &Y[i]);
                               ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...