• 2019-2020 ICPC, NERC, Southern and Volga Russian Regional Contest (Online Mirror, ICPC Rules, Teams Preferred) M. SmartGarden 题解


    cf1250 M. SmartGarden

    完全不会做 orz,在 cf 上看到了有趣的做法。

    通读题意后可以发现是对于每一次操作,要求选出的行集合 (R) 和列集合 (C) 要满足如下条件:

    [(forall r)(forall c)(r in R wedge c in C wedge r eq c wedge r eq c+1) ]

    接下来考虑二进制下对每一次操作如何取行集合和列集合。枚举二进制下每一位 (i),把所有当前位 (i=0) 的行 (r) 加入行集合 (R) 中,同时将所有当前位 (i=1) 以及 (c+1) 的当前位也为 (i=1) 的列 (c) 加入列集合 (C) 中,这样就是一次操作。反过来可以把所有当前位 (i=1) 的行 (r) 加入行集合 (R) 中,同时将所有当前位 (i = 0) 以及 (c+1) 的当前位也为 $i = 0 $ 的列 (c) 加入列集合 (C) 中,这样又是一次操作。这样共计 (2 cdot lceil log_2 5000 ceil = 2 cdot 13 = 26) 次操作。这样的操作必定满足上面的条件。问题变为了如何填满漏选的地方。

    观察一下列 (c) 的格式,可以发现其形如 (XX...X011...1) ,前面的 (X) 为固定位,后面的 (011...1) 为翻转位,(c+1)(c) 的固定位相同,而翻转位每一位都不同。若 ((r,c)) 漏选,可以得出 (r)(c) 的固定位相同的结论。这是一个充要条件。

    那么,枚举后面的翻转位的长度,并将所有翻转位都为该长度的列 (c) 加入本次的列集合 (C) 中。而关于行 (r) ,只要其二进制下与列 (c) 翻转位对应的位不为 (011...1)(100...0) 即可满足条件。这样也会有 (lceil log_2 5000 ceil = 13) 次操作。故最大操作数为 (39)

    #include<bits/stdc++.h>
    using namespace std;
    
    typedef long long ll;
    typedef double db;
    typedef long double ld;
    
    #define IL inline
    #define fi first
    #define se second
    #define mk make_pair
    #define pb push_back
    #define SZ(x) (int)(x).size()
    #define ALL(x) (x).begin(), (x).end()
    #define dbg1(x) cout << #x << " = " << x << ", "
    #define dbg2(x) cout << #x << " = " << x << endl
    
    template<typename Tp> IL void read(Tp &x) {
        x=0; int f=1; char ch=getchar();
        while(!isdigit(ch)) {if(ch == '-') f=-1; ch=getchar();}
        while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
        x *= f;
    }
    int buf[42];
    template<typename Tp> IL void write(Tp x) {
        int p = 0;
        if(x < 0) { putchar('-'); x=-x;}
        if(x == 0) { putchar('0'); return;}
        while(x) {
            buf[++p] = x % 10;
            x /= 10;
        }
        for(int i=p;i;i--) putchar('0' + buf[i]);
    }
    
    const int N = 5000 + 5;
    
    int n;
    vector<vector<int> > rows, cols;
    
    IL bool chkbit(int x, int p) { return (x & (1 << p)) > 0;}
    
    int main() {
    #ifdef LOCAL
        freopen("M.in", "r", stdin);
    #endif
        scanf("%d", &n);
        for(int bt=0;bt<=1;bt++) {
            for(int i=0; (1<<i) <= n; i++) {
                vector<int> row, col;
                for(int k=1;k<=n;k++) {
                    if(chkbit(k, i) == bt) row.push_back(k);
                    else if(k == n || chkbit(k+1, i) != bt) col.push_back(k);
                }
    
                if(row.empty() || col.empty()) continue;
                rows.push_back(row);
                cols.push_back(col);
            }
        }
    
        for(int i=1; (1<<i)-1 <= n; i++) {
            int flip = (1 << i) - 1;
            int flip2 = (flip << 1) | 1;
            int isrow = (1 << i);
            vector<int> row, col;
            for(int j=1;j<=n;j++) {
                if((j & flip) == flip && (j & flip2) != flip2) col.push_back(j); // XX...X011...1
                else if((j & flip2) != isrow) row.push_back(j);
            }
            if(col.empty() || row.empty()) continue;
    
            rows.push_back(row);
            cols.push_back(col);
        }
    
        printf("%d
    ", SZ(rows));
        for(int i=0;i<SZ(rows);i++) {
            printf("%d ", SZ(rows[i]));
            for(int j=0;j<SZ(rows[i]);j++) {
                printf("%d%c", rows[i][j], " 
    "[j == SZ(rows[i]) - 1]);
            }
            printf("%d ", SZ(cols[i]));
            for(int j=0;j<SZ(cols[i]);j++) {
                printf("%d%c", cols[i][j], " 
    "[j == SZ(cols[i]) - 1]);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    ping
    android Handler总结
    配置网络测试环境的批处理
    shell 的选项解析
    [转] Linux 中共享库的搜索
    SecureCRT 脚本一则(0720.Rev.1)
    使用 Wget 完成自动 Web 认证(推 portal)
    shell 选项解析之需求一:多路径自动补全
    getopts 的简单模拟(09.12 Rev)
    ThinkPHP框架使用心得三 RBAC权限控制(2)简要原理
  • 原文地址:https://www.cnblogs.com/bringlu/p/14994180.html
Copyright © 2020-2023  润新知