zoukankan      html  css  js  c++  java
  • Codeforces 650C Table Compression

    time limit per test  4 seconds
    memory limit per test  256 megabytes
    input  standard input
    output  standard output

    Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name dis.

    Petya decided to compress tables. He is given a table a consisting of n rows and m columns that is filled with positive integers. He wants to build the table a' consisting of positive integers such that the relative order of the elements in each row and each column remains the same. That is, if in some row i of the initial table ai, j < ai, k, then in the resulting table a'i, j < a'i, k, and if ai, j = ai, k then a'i, j = a'i, k. Similarly, if in some column j of the initial table ai, j < ap, j then in compressed table a'i, j < a'p, j and if ai, j = ap, j then a'i, j = a'p, j.

    Because large values require more space to store them, the maximum value in a' should be as small as possible.

    Petya is good in theory, however, he needs your help to implement the algorithm.

    Input

    The first line of the input contains two integers n and m (, the number of rows and the number of columns of the table respectively.

    Each of the following n rows contain m integers ai, j (1 ≤ ai, j ≤ 109) that are the values in the table.

    Output

    Output the compressed table in form of n lines each containing m integers.

    If there exist several answers such that the maximum number in the compressed table is minimum possible, you are allowed to output any of them.

    Examples
    Input
    2 2
    1 2
    3 4
    Output
    1 2
    2 3
    Input
    4 3
    20 10 30
    50 40 30
    50 60 70
    90 80 70
    Output
    2 1 3
    5 4 3
    5 6 7
    9 8 7
    Note

    In the first sample test, despite the fact a1, 2 ≠ a21, they are not located in the same row or column so they may become equal after the compression.

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Solution:

    这道题真心好,妙极。

    Tutorial上给出的是图论向的解法,但我压根就没往那方向想过,never ever!

    这里讨论一个正常人的解法。

    将所有数从小到大排序,再往里填。我相信这是最自然的想法了。

    紧接着而来的问题(trouble)就是按什么顺序填充那些相同的数。

    我最开始的想法是很朴素也很naive的:

    给排序加若干辅助的优先级。

    这些辅助优先级是观察样例得来的猜想,当然不大可能靠得住。

    -----------------------------------------------------------------------------------------

    后来参考了某篇题解,解决了上面提到的trouble。

    注意到题目中要求:such that the relative order of the elements in each row and each column remains the same.这句话其实是在表格元素上定义了一个等价关系

      a~b: a=b 且存在一条由a到b的曼哈顿路径且该路径上的所有点(元素)都属于集合{x | x=a}.

    (这样描述看起来还是不够形式化 :D)

    按这个等价关系可将表格(全集)分成若干等价类。

    而题目正是要求压缩(映射)之后维持这个等价关系。

    因此,实际上要在排序的基础上(对相同元素)进一步维护出等价类(equivelance classes)。

    说到等价关系自然就想到并查集

    ---------------------------------------------------------------------------------------------------------------------------------------------

    细节就不多说了,coding时自能体会(也许读者才不会像LZ,犯那样SB的错误呢)。

    Implementation:

    /*
    In mathematics, an equivalence relation is a binary relation that is at the same time a
    reflexive relation, a symmetric relation and a transitive relation. As a consequence of
    these properties relation provides a partition of a set onto equivalence classes.
    */
    #include <bits/stdc++.h>
    using namespace std;
    
    const int N(1e6+5);
    
    struct node{
        int x, y, v;
        bool operator <(const node &a)const{
            return v<a.v;
        }
    }a[N], mx[N], my[N];
    
    int par[N], ma[N], ans[N];
    
    
    int m, n;
    
    int ID(node &a){
        return a.x*m+a.y;
    }
    
    int find(int x){
        return x==par[x]?x:par[x]=find(par[x]);
    }
    
    void unite(int x, int y){
        x=find(x), y=find(y);
        par[x]=y;
        ma[y]=max(ma[y], ma[x]);
    }
    
    int main(){
        cin>>n>>m;
    
        for(int i=0; i<n; i++){
            for(int j=1; j<=m; j++){
                int x;
                cin>>x;
                a[i*m+j]={i, j, x};
            }
        }
    
        for(int i=1; i<=m*n; i++)
            par[i]=i;
    
        sort(a+1, a+m*n+1);
    
        //two-pointers
        for(int i=1, j; i<=m*n; ){
            // cout<<a[i].v<<endl;
            for(j=i; j<=m*n && a[j].v==a[i].v; j++){
    
                ma[ID(a[j])]=max(ans[ID(mx[a[j].x])], ans[ID(my[a[j].y])]);    //error-prone
    
                if(a[j].v==mx[a[j].x].v){
                    unite(ID(a[j]), ID(mx[a[j].x]));
                }
                else{
                    mx[a[j].x]=a[j];
                }
    
                if(a[j].v==my[a[j].y].v){
                    unite(ID(a[j]), ID(my[a[j].y]));
                }
                else{
                    my[a[j].y]=a[j];
                }
            }
    
            for(; i!=j; i++){
                int id=ID(a[i]);
                // cout<<i<<' '<<find(ID(a[i]))<<endl;
                // ma[ID(a[i])]=ma[find(ID(a[i]))]+1 //OMG!
                ans[id]=ma[find(id)]+1;
            }
        }
    
    
         for(int i=0; i<n; i++){
             for(int j=1; j<=m; j++)
                cout<<ans[i*m+j]<<' ';
            cout<<'
    ';
        }
    
        return 0;
    }

    ---------------------------------------

    其实还有许多可总结的,累了,坑留着往后再填吧。

  • 相关阅读:
    Cryptography中的对称密钥加解密:fernet算法探究
    HTTPS的工作原理
    最近要写的博客
    浅谈路由器软硬件架构
    组管理、权限管理、定时任务调度、磁盘分区
    matplotlib数据可视化
    tensorflow实现简单的卷积神经网络
    tensorflow实现简单的感知机
    tensorflow实现简单的自编码器
    区域生长算法(手动选取种子点)MATLAB
  • 原文地址:https://www.cnblogs.com/Patt/p/5281311.html
Copyright © 2011-2022 走看看