cf1250 M. SmartGarden

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

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

\[(\forall r)(\forall c)(r \in R \wedge c \in C \wedge r \neq c \wedge r \neq 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 \rceil = 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 \rceil = 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\n", 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], " \n"[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], " \n"[j == SZ(cols[i]) - 1]);
}
}
return 0;
}

2019-2020 ICPC, NERC, Southern and Volga Russian Regional Contest (Online Mirror, ICPC Rules, Teams Preferred) M. SmartGarden 题解的更多相关文章

  1. 2019-2020 ICPC, NERC, Southern and Volga Russian Regional Contest (Online Mirror, ICPC Rules, Teams Preferred)【A题 类型好题】

    A. Berstagram Polycarp recently signed up to a new social network Berstagram. He immediately publish ...

  2. 2020-2021 ICPC, NERC, Southern and Volga Russian Regional Contest (Online Mirror, ICPC Rules) D. Firecrackers (贪心,二分)

    题意:有个长度为\(n\)的监狱,犯人在位置\(a\),cop在位置\(b\),你每次可以向左或者向右移动一个单位,或者选择不动并在原地放一个爆竹\(i\),爆竹\(i\)在\(s[i]\)秒后爆炸, ...

  3. 2020-2021 ICPC, NERC, Southern and Volga Russian Regional Contest (Online Mirror, ICPC Rules) C. Berpizza (STL)

    题意:酒吧里有两个服务员,每个人每次都只能服务一名客人,服务员2按照客人进酒吧的顺序服务,服务员3按照客人的钱来服务,询问\(q\),\(1\)表示有客人进入酒吧,带着\(m\)块钱,\(2\)表示询 ...

  4. 2019-2020 ICPC, NERC, Southern and Volga Russian Regional Contest

    目录 Contest Info Solutions A. Berstagram B. The Feast and the Bus C. Trip to Saint Petersburg E. The ...

  5. 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)

    A. Find a Number 找到一个树,可以被d整除,且数字和为s 记忆化搜索 static class S{ int mod,s; String str; public S(int mod, ...

  6. 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) Solution

    A. Find a Number Solved By 2017212212083 题意:$找一个最小的n使得n % d == 0 并且 n 的每一位数字加起来之和为s$ 思路: 定义一个二元组$< ...

  7. Codeforces1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)总结

    第一次打ACM比赛,和yyf两个人一起搞事情 感觉被两个学长队暴打的好惨啊 然后我一直做傻子题,yyf一直在切神仙题 然后放一波题解(部分) A. Find a Number LINK 题目大意 给你 ...

  8. codeforce1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) 题解

    秉承ACM团队合作的思想懒,这篇blog只有部分题解,剩余的请前往星感大神Star_Feel的blog食用(表示男神汉克斯更懒不屑于写我们分别代写了下...) C. Cloud Computing 扫 ...

  9. 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)

    A 思路: 贪心,每次要么选两个最大的,要么选三个,因为一个数(除了1)都可以拆成2和3相加,直到所有的数都相同就停止,这时就可以得到答案了; C: 二分+bfs,二分答案,然后bfs找出距离小于等于 ...

  10. 2014-2015 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)

    I. Sale in GameStore(贪心) time limit per test 2 seconds memory limit per test 512 megabytes input sta ...

随机推荐

  1. 2019-8-31-C#-大端小端转换

    title author date CreateTime categories C# 大端小端转换 lindexi 2019-08-31 16:55:58 +0800 2018-05-28 10:21 ...

  2. vue中使用vue-b2wordcloud创建词云

    安装使用 安装:使用npm install vue-b2wordcloud --save或者直接在vue ui中添加vue-b2wordcloud运行依赖 使用:在main.js中导入使用 impor ...

  3. C++ 多级继承与多重继承:代码组织与灵活性的平衡

    C++ 多级继承 多级继承是一种面向对象编程(OOP)特性,允许一个类从多个基类继承属性和方法.它使代码更易于组织和维护,并促进代码重用. 多级继承的语法 在 C++ 中,使用 : 符号来指定继承关系 ...

  4. 原生js写悬浮广告效果

    网上抄的,改成vue写法失败,下一篇是自己写的vue版本的 <html> <head> <meta http-equiv="Content-Type" ...

  5. ide构建SpringMVC框架

    框架原理图如下: 1. 创建如图项目 2. 在lib中所需导入jar包 3. 配置变量 (1) (2)add library (3)选择web app libraries 4. 配置web.xml文件 ...

  6. VMware最小化安装Centos7.6-无桌面

    目录 安装包工具 新建虚拟机 安装 centos 7.6 系统 终端登陆系统 设置ip地址 关闭防火墙 关闭 SELINUX SELINUX=enforcing 硬盘挂载 桥接上网方式 安装包工具 V ...

  7. NASM中的Preprocessor

    NASM中的Preprocessor都以%开头. 单行macro %define %define与C语言中的#define类似: %define a(x) 1+b(x) %define b(x) 2* ...

  8. 前端使用 Konva 实现可视化设计器(10)- 对齐线

    请大家动动小手,给我一个免费的 Star 吧~ 大家如果发现了 Bug,欢迎来提 Issue 哟~ github源码 gitee源码 示例地址 不知不觉来到第 10 章了,感觉接近尾声了... 对齐线 ...

  9. Unity Visual Scripting 使用随记

    1.Wait Until并不会再执行前面的代码,而是反复执行获取bool变量的代码:需自己拆出来写. 2.yield return null对应Wait For Next Frame,多用这个避免协程 ...

  10. .NET 9 预览版:打造云原生及人工智能 AI 平台

    前言 微软发布了 .NET 9 首个预览版,分享.NET团队对 .NET 9 的初步愿景,该愿景将于今年年底在 .NET Conf 2024 上发布. 重点关注针对云原生和人工智能领域的应用程序开发以 ...