Submission #1072820

#TimeUsernameProblemLanguageResultExecution timeMemory
1072820PenguinsAreCuteBuilding Skyscrapers (CEOI19_skyscrapers)C++17
22 / 100
3532 ms113552 KiB
#include <bits/stdc++.h> using namespace std; using i2 = pair<int,int>; #define fi first #define se second #define lb lower_bound #define ub upper_bound #define all(v) begin(v),end(v) #define sz(v) int(v.size()) namespace ufds { int par[1200005], sz[1200005], spec; vector<int> child[1200005]; // "literally a child" vector<int> toUp; void init(int _spec) { iota(par,par+1200005,0); fill(sz,sz+1200005,0); for(int i=0;i<1200005;i++) child[i]={i}; spec = _spec; toUp = {spec}; } int onion(int x) {return par[x]=(par[x]==x?x:onion(par[x]));} void unite(int x, int y) { x = onion(x); y = onion(y); if(sz[x]>sz[y]) swap(x,y); if(x==y) return; if(x==onion(spec)) for(auto i: child[y]) toUp.push_back(i); if(y==onion(spec)) for(auto i: child[x]) toUp.push_back(i); for(auto i: child[x]) child[y].push_back(i); sz[y]+=sz[x]; par[x]=y; } } vector<int> vadj[150005]; int dist[150005]; void bfs(int s) { queue<int> q; dist[s]=0; for(q.push(s);q.size();) { int x = q.front(); q.pop(); for(auto i: vadj[x]) if(dist[i]==-1) { dist[i]=dist[x]+1; q.push(i); } } } struct block {int x, y, i;}; auto lamb = [](block a, block b){return i2(a.x,a.y)<i2(b.x,b.y);}; block lst[150005]; int val(int n, int x, int y) { auto it = lower_bound(lst,lst+n,block{x,y,0},lamb); if(it==lst+n||(*it).x!=x||(*it).y!=y) return -1; return (*it).i; } int dx[8]={1,0,-1,0,1,1,-1,-1}; int dy[8]={0,1,0,-1,-1,1,-1,1}; void dfs(int x, vector<vector<int>> &vac) { vac[x].push_back(-1); for(int i=0;i<4;i++) { auto it = lower_bound(vac.begin(),vac.end(),vector<int>{vac[x][0]+dx[i],vac[x][1]+dy[i]}); if(it==vac.end()) continue; if((*it)!=vector<int>{vac[x][0]+dx[i],vac[x][1]+dy[i]}) continue; dfs(it-vac.begin(),vac); } } vector<int> build_skyscrapers(int t, vector<int> r, vector<int> c) { int n = r.size(); for(int i=0;i<n;i++) lst[i]={r[i],c[i],i}; sort(lst,lst+n,lamb); vector<int> eadj[n]; vector<vector<int>> vacant; for(int i=0;i<n;i++) for(int j=0;j<8;j++) { int x = val(n,r[i]+dx[j],c[i]+dy[j]); vacant.push_back({r[i]+dx[j],c[i]+dy[j]}); vadj[i].push_back(x); if(j<4) eadj[i].push_back(x); } sort(all(vacant)); vacant.resize(unique(all(vacant))-begin(vacant)); int lft = min_element(all(r))-begin(r); int st = find(all(vacant),vector<int>{r[lft]-1,c[lft]})-begin(vacant); ufds::init(st); vector<bool> on(sz(vacant),0); on[st]=1; auto turnOn = [&vacant,&on](int x) { on[x]=1; for(int i=0;i<4;i++) { auto it = find(all(vacant),vector<int>{vacant[x][0]+dx[i],vacant[x][1]+dy[i]}); if(it==end(vacant)) continue; if(!on[it-begin(vacant)]) continue; ufds::unite(it-begin(vacant),x); } }; for(int i=0;i<sz(vacant);i++) if(val(n,vacant[i][0],vacant[i][1])==-1) turnOn(i); fill(dist,dist+n,-1); bfs(0); if(*min_element(dist,dist+n)==-1) return {-1}; vector<bool> used(n); priority_queue<i2> pq; auto pushOne = [&pq,&used,n](int x, int y) { for(int i=0;i<4;i++) { int z = val(n,x+dx[i],y+dy[i]); if(z==-1) continue; if(used[z]) continue; pq.push({dist[z],z}); used[z]=1; } }; auto push = [&pq,&pushOne,&vacant]() { for(auto i: ufds::toUp) pushOne(vacant[i][0],vacant[i][1]); ufds::toUp.clear(); }; push(); vector<int> perm; while(pq.size()) { int x = pq.top().second; pq.pop(); perm.push_back(x); auto it = find(all(vacant),vector<int>{r[x],c[x]}); if(it!=end(vacant)) turnOn(it-begin(vacant)); push(); } reverse(perm.begin(),perm.end()); if(perm.size()<n) {cout<<"F*CK\n"; exit(0);} return perm; } // grader main() { int n, t; assert(2==scanf("%d %d",&n,&t)); vector<int> r(n), c(n); for(int i=0;i<n;i++) assert(2==scanf("%d %d",&r[i],&c[i])); vector<int> v = build_skyscrapers(t,r,c); if(v[0]==-1) printf("NO"); else { printf("YES\n"); for(auto i: v) printf("%d\n",i+1); } } /* g.kd. .#.m. f.0.i j#..b helca */

Compilation message (stderr)

skyscrapers.cpp: In function 'std::vector<int> build_skyscrapers(int, std::vector<int>, std::vector<int>)':
skyscrapers.cpp:126:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  126 |   if(perm.size()<n) {cout<<"F*CK\n"; exit(0);}
      |      ~~~~~~~~~~~^~
skyscrapers.cpp: At global scope:
skyscrapers.cpp:131:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  131 | main() {
      | ^~~~
#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...