Submission #501050

#TimeUsernameProblemLanguageResultExecution timeMemory
501050InternetPerson10Building Skyscrapers (CEOI19_skyscrapers)C++17
0 / 100
3571 ms41092 KiB
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> v; int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; int dxe[4] = {-1, -1, 1, 1}; int dye[4] = {-1, 1, -1, 1}; map<pair<int, int>, int> m; set<pair<int, int>> black; struct dsu { vector<int> par, siz; void init(int n) { vector<int>().swap(par); vector<int>().swap(siz); par.resize(n); siz.resize(n, 1); for(int i = 0; i < n; i++) par[i] = i; } int get(int x) { if(x == par[x]) return x; return par[x] = get(par[x]); } bool unite(int x, int y) { x = get(x); y = get(y); if(x == y) return false; if(x < y) swap(x, y); // y is the lesser one now par[x] = y; siz[y] += siz[x]; return true; } }; vector<int> find_order(int n, vector<int> x, vector<int> y) { vector<int> ans; return ans; } vector<int> find_best_order(int n, vector<int> x, vector<int> y) { vector<int> ans; int mix, miy; mix = miy = 1234567890; for(int i = 0; i < n; i++) { mix = min(mix, x[i]); miy = min(miy, y[i]); } mix--; miy--; for(int i = 0; i < n; i++) { x[i] -= mix; y[i] -= miy; v.push_back({x[i], y[i]}); black.insert({x[i], y[i]}); for(int j = 0; j < 8; j++) { v.push_back({x[i]+dx[j], y[i]+dy[j]}); } } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); // Vertex labels are stored as an unordered_map // I should constopt this if it's too slow for(int i = 0; i < v.size(); i++) { m[v[i]] = i; } dsu uf; uf.init(v.size()+1); // First check if the peple are connected bool ok = false; for(int i = 0; i < v.size(); i++) { if(black.count(v[i])) { for(int j = 0; j < 8; j++) { pair<int, int> oth_pair = {v[i].first+dx[j], v[i].second+dy[j]}; if(black.count(oth_pair)) { uf.unite(m[v[i]], m[oth_pair]); } } if(uf.siz[uf.get(m[v[i]])] == n) ok = true; } } if(!ok) return ans; // Now find an order, unite all white regions uf.init(v.size()+1); for(int i = 0; i < v.size(); i++) { if(black.count(v[i])) continue; for(int j = 0; j < 8; j++) { pair<int, int> oth_pair = {v[i].first+dx[j], v[i].second+dy[j]}; if(black.count(oth_pair)) continue; uf.unite(m[v[i]], m[oth_pair]); } } ans.reserve(n); for(int z = 0; z < n; z++) { for(int i = 0; i < n; i++) { // Checks if the regions around the cell are "good" pair<int, int> this_pair = {x[i], y[i]}; // cout << x[i] << ' ' << y[i] << '\n'; if(!black.count(this_pair)) continue; vector<int> regs; for(int j = 0; j < 8; j++) { pair<int, int> oth_pair = {x[i]+dx[j], y[i]+dy[j]}; if(black.count(oth_pair)) continue; if(!m.count(oth_pair)) continue; regs.push_back(uf.get(m[oth_pair])); } if(regs.size() == 0) continue; regs.erase(unique(regs.begin(), regs.end()), regs.end()); if(regs.size() == 1 && regs[0] == 0) { black.erase(this_pair); ans.push_back(i+1); uf.unite(m[this_pair], 0); break; } if(regs.size() == 1) continue; if(regs[0] == regs.back()) regs.pop_back(); sort(regs.begin(), regs.end()); if(regs[0] != 0) continue; bool ok = true; for(int i = 1; i < regs.size(); i++) { if(regs[i] == regs[i-1]) ok = false; } if(!ok) continue; black.erase(this_pair); ans.push_back(i+1); uf.unite(m[this_pair], 0); for(int i = 1; i < regs.size(); i++) { uf.unite(regs[i-1], regs[i]); } break; } if(ans.size() == z-1) { vector<int>().swap(ans); return ans; } } reverse(ans.begin(), ans.end()); return ans; } int main() { int n, s; cin >> n >> s; vector<int> x(n), y(n); for(int i = 0; i < n; i++) { cin >> x[i] >> y[i]; } vector<int> ans; if(s == 1) ans = find_best_order(n, x, y); if(s == 2) ans = find_best_order(n, x, y); if(ans.size() == 0) { cout << "NO\n"; } else { cout << "YES\n"; for(int i = 0; i < ans.size(); i++) cout << ans[i] << '\n'; } }

Compilation message (stderr)

skyscrapers.cpp: In function 'std::vector<int> find_best_order(int, std::vector<int>, std::vector<int>)':
skyscrapers.cpp:68:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |     for(int i = 0; i < v.size(); i++) {
      |                    ~~^~~~~~~~~~
skyscrapers.cpp:75:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |     for(int i = 0; i < v.size(); i++) {
      |                    ~~^~~~~~~~~~
skyscrapers.cpp:89:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |     for(int i = 0; i < v.size(); i++) {
      |                    ~~^~~~~~~~~~
skyscrapers.cpp:123:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |             for(int i = 1; i < regs.size(); i++) {
      |                            ~~^~~~~~~~~~~~~
skyscrapers.cpp:130:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |             for(int i = 1; i < regs.size(); i++) {
      |                            ~~^~~~~~~~~~~~~
skyscrapers.cpp:135:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  135 |         if(ans.size() == z-1) {
      |            ~~~~~~~~~~~^~~~~~
skyscrapers.cpp: In function 'int main()':
skyscrapers.cpp:159:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  159 |         for(int i = 0; i < ans.size(); i++) cout << ans[i] << '\n';
      |                        ~~^~~~~~~~~~~~
#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...