Advertisement
ec1117

Untitled

Dec 7th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1.  
  2. int dx[]={1,1,1,0,0,-1,-1,-1}, dy[]={0,-1,1,1,-1,0,-1,1};
  3. int dx2[]={1,0,-1,0}, dy2[]={0,1,0,-1};
  4. int n, g[MX][MX], m[MX*MX];
  5. bool c[MX][MX];
  6. vector<pair<int, pi>> ev;
  7. ll ret=0;
  8. /*
  9. CHANGES MY MAP TO INT FOR SPEEDUP
  10. 10/21 wo
  11.  */
  12.  
  13. bool side(int x, int y){
  14.     return (x==0 || y==0 || x==n-1 || y==n-1);
  15. }
  16. bool leg(int x, int y){
  17.     return (x>=0 && x<=n-1 && y>=0 && y<=n-1);
  18. }
  19.  
  20. int tr(int x, int y){
  21.     return x*n+y;
  22. }
  23. pi r(int x){
  24.     return mp(x/n,x%n);
  25. }
  26. struct DSU{
  27.     int n;
  28.     vi p, mx;
  29.     void init(int N){
  30.         n=N;
  31.         p=vi(n,-1);
  32.         mx=vi(n,0);
  33.     }
  34.     int get(int x){
  35.         return (p[x]<0)?x:p[x]=get(p[x]);
  36.     }
  37.     bool same(int x, int y){
  38.         return get(x)==get(y);
  39.     }
  40.     bool unite(int x, int y){
  41.         x=get(x), y=get(y);
  42.         if(x==y)return false;
  43.         if(y==0 || (x!=0 && p[y]<p[x]))swap(x,y);
  44.         p[x]+=p[y];
  45.         p[y]=x;
  46.         ckmax(mx[x],mx[y]);
  47.         return true;
  48.     }
  49. };
  50.  
  51. DSU dsu;
  52. int main() {
  53.     setIO("valleys");
  54.     cin >> n;
  55.     dsu.init(MX*MX);
  56.     For(i,n){
  57.         For(j,n){
  58.             cin>> g[i][j];
  59.             ev.pb(mp(g[i][j],mp(i,j)));
  60.         }
  61.     }
  62.     sort(all(ev));
  63.     reverse(all(ev));
  64.     trav(x,ev){//FIND MAX HOLE wrong here?
  65.         queue<pi> q;
  66.         if(side(x.s.f,x.s.s)){
  67.             q.push(x.s);
  68.         }
  69.         else{
  70.             For(k,8){
  71.                 int nx=x.s.f+dx[k], ny=x.s.s+dy[k];//DECR, which ones need me
  72.                 if(leg(nx,ny) && c[nx][ny]){
  73.                     q.push(x.s);
  74.                     break;
  75.                 }
  76.             }
  77.         }
  78.         while(sz(q)){
  79.             pi y=q.front(); q.pop();
  80.             int tx=y.f, ty=y.s;
  81.             if(!c[tx][ty]){
  82.                 ckmax(m[tr(x.s.f,x.s.s)],g[tx][ty]);//here?
  83.                 c[tx][ty]=true;
  84.                 For(k,8){
  85.                     int nx=tx+dx[k], ny=ty+dy[k];//DECR, which ones need me
  86.                     if(leg(nx,ny) && g[nx][ny]>g[x.s.f][x.s.s] && !c[nx][ny])
  87.                         q.push(mp(nx,ny));
  88.                 }
  89.             }
  90.         }
  91.     }
  92.     reverse(all(ev));
  93.     trav(x,ev){
  94.         int cx=x.s.f,cy=x.s.s;
  95.         For(k,4){
  96.             int tx=cx+dx2[k], ty=cy+dy2[k];
  97.             if(leg(tx,ty) && g[tx][ty]<g[cx][cy])//already processed
  98.                 dsu.unite(tr(tx,ty),tr(cx,cy));
  99.         }
  100.         ckmax(dsu.mx[dsu.get(tr(cx,cy))],m[tr(cx,cy)]);
  101.  
  102.         if(g[cx][cy]>=dsu.mx[dsu.get(tr(cx,cy))])//no hole to worry abt
  103.             dsu.mx[dsu.get(tr(cx,cy))]=-1;
  104.  
  105.         if(dsu.mx[dsu.get(tr(cx,cy))]<=0){
  106.             ret+=-dsu.p[dsu.get(tr(cx,cy))];
  107.         }
  108.         // else dbg("HI",dsu.mx[dsu.get(tr(cx,cy))],cx,cy);
  109.     }
  110.     cout << ret << nl;
  111.     dbg(ret);
  112. }
  113.             // c[x.s.f][x.s.s]=true;
  114.             // For(k,8){
  115.             //  int tx=x.s.f+dx[k], ty=x.s.s+dy[k];
  116.             //  if(leg(tx, ty) && g[tx][ty]>g[x.s.f][x.s.s] && !c[tx][ty])
  117.             //      q.push(mp(tx,ty));
  118.             // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement