CodeForces 1099E - Nice table - [好题]
题目链接:https://codeforces.com/problemset/problem/1099/E
You are given an $n×m$ table, consisting of characters «A», «G», «C», «T». Let's call a table nice, if every $2×2$ square contains all four distinct characters. Your task is to find a nice table (also consisting of «A», «G», «C», «T»), that differs from the given table in the minimum number of characters.
Input
First line contains two positive integers $n$ and $m$ — number of rows and columns in the table you are given $(2≤n,m,n×m≤300000)$. Then, $n$ lines describing the table follow. Each line contains exactly $m$ characters «A», «G», «C», «T».
Output
Output $n$ lines, $m$ characters each. This table must be nice and differ from the input table in the minimum number of characters.
Examples
Input
2 2
AG
CT
Output
AG
CT
Input
3 5
AGCAG
AGCAG
AGCAG
Output
TGCAT
CATGC
TGCAT
Note
In the first sample, the table is already nice. In the second sample, you can change $9$ elements to make the table nice.
题解:
一个 nice table 必然属于以下两种情况:
1、每行都由两个字符交替组成,相邻两行的字符集合交集为空;
2、每列都由两个字符交替组成,相邻两列的字符集合交集为空。
对于上述两种情况中的一种,不妨先对于每行,枚举其可能的字符集,而对于一个字符集,又有正序和逆序两种情况。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+;
const char choice[][]={{'A','C'},{'A','G'},{'A','T'},{'C','G'},{'C','T'},{'G','T'}}; int n,m;
string str[maxn]; int ord[][maxn][],cnt[][maxn]; string out[maxn];
void print(int rc,int k)
{
for(int i=;i<n;i++) out[i]="";
if(rc==) //R
{
for(int i=;i<n;i++)
for(int j=;j<m;j++)
out[i]+=choice[(i&)?-k:k][(j&)^ord[][i][k]];
}
else //C
{
for(int j=;j<m;j++)
for(int i=;i<n;i++)
out[i]+=choice[(j&)?-k:k][(i&)^ord[][j][k]];
}
for(int i=;i<n;i++) cout<<out[i]<<endl;
}
int main()
{
cin>>n>>m;
for(int i=;i<n;i++) cin>>str[i]; memset(cnt,,sizeof(cnt));
for(int i=;i<n;i++)
{
for(int k=;k<;k++)
{
int now1=, now2=;
for(int j=;j<m;j++)
{
now1+=(str[i][j]!=choice[(i&)?-k:k][j&]);
now2+=(str[i][j]!=choice[(i&)?-k:k][(j&)^]);
}
ord[][i][k]=now1<now2?:; //0正序,1逆序
cnt[][k]+=min(now1,now2);
}
}
for(int j=;j<m;j++)
{
for(int k=;k<;k++)
{
int now1=, now2=;
for(int i=;i<n;i++)
{
now1+=(str[i][j]!=choice[(j&)?-k:k][i&]);
now2+=(str[i][j]!=choice[(j&)?-k:k][(i&)^]);
}
ord[][j][k]=now1<now2?:; //0正序,1逆序
cnt[][k]+=min(now1,now2);
}
} int ans=0x3f3f3f3f,RC,K;
for(int rc=;rc<=;rc++)
{
for(int k=;k<;k++)
{
if(cnt[rc][k]<ans) ans=cnt[rc][k], RC=rc, K=k;
}
}
//cout<<ans<<endl;
print(RC,K);
}
(注:借鉴了https://www.luogu.org/blog/xht37/cf1098bcf1099e-nice-table的代码,到了这个点心态真的要放平……心态不稳代码基本不可能AC……)
CodeForces 1099E - Nice table - [好题]的更多相关文章
- Codeforces 417E Square Table(随机算法)
题目链接:Codeforces 417E Square Table 题目大意:给出n和m.要求给出一个矩阵,要求每一列每一行的元素的平方总和是一个平方数. 解题思路:构造.依照 a a a b a a ...
- Codeforces#441 Div.2 四小题
Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...
- You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]
补一下codeforces前天教育场的题.当时只A了一道题. 大致题意: 定义一个x - y - counter :是一个加法计数器.初始值为0,之后可以任意选择+x或者+y而我们由每次累加结果的最后 ...
- codeforces 1165F1/F2 二分好题
Codeforces 1165F1/F2 二分好题 传送门:https://codeforces.com/contest/1165/problem/F2 题意: 有n种物品,你对于第i个物品,你需要买 ...
- Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题
A. Multiplication Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/57 ...
- Codeforces 675C Money Transfers 思维题
原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...
- Codeforces Gym 100531G Grave 水题
Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...
- Codeforces #662C Binary Table
听说这是一道$ Tourist$现场没出的题 Codeforces #662C 题意: 给定$n*m的 01$矩阵,可以任意反转一行/列($0$变$1$,$1$变$0$),求最少$ 1$的数量 $ n ...
- Codeforces 1137D - Cooperative Game - [交互题+思维题]
题目链接:https://codeforces.com/contest/1137/problem/D 题意: 交互题. 给定如下一个有向图: 现在十个人各有一枚棋子(编号 $0 \sim 9$),在不 ...
随机推荐
- ios NSURLSession后台传输
http://www.appcoda.com/background-transfer-service-ios7/ http://www.raywenderlich.com/51127/nsurlses ...
- Effective Java 第三版——36. 使用EnumSet替代位属性
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- Spring Boot 2.0 返回JSP页面实战
1. 模板引擎JSP的限制 在开始之前呢,我觉得我们有必要先去了解下 Spring Boot 2.0 官方文档中提到的如下内容: 模板引擎 除了REST Web服务之外,还可以使用Spring MVC ...
- curl 伪装来路(referer)
curl -e http://vip.hihi.com http://test.image.ok.com/parcel/201704/1491035345125_971038.jpg 1xx.8x.3 ...
- Source Insight 中文注释为乱码解决办法(完美解决,一键搞定)
我从网上查了一堆解决办法,但是都是2017年以前的解决方案,并且都是针对于source insight 3.5及以下版本的,目前SI软件版本都到4.0了,应该有新方法出现了. ------------ ...
- 【Linux高级驱动】input子系统框架
[1.input子系统框架(drivers\input)] 如何得出某个驱动所遵循的框架? 1) 通过网络搜索 2) 自己想办法跟内核代码! 2.1 定位此驱动是属于哪种类 ...
- Asp.Net T4模板生成三层架构
1.T4 Editor安装 T4:根据模板生成文件,例如model等 vs中默认t4模板编码是没有提示和高亮的,需使用以下插件,免费的 https://t4-editor.tangible-engin ...
- 推荐几个Windows工具软件: HDDB - Everything的替代品
主页: http://hddb.xp-zed.com/index.html 简单地说,HDDB 是NTFS磁盘的文件搜索工具,它最开始是模仿 Everything 的,但解决了 Everything ...
- 聊聊Python中的闭包和装饰器
1. 闭包 首先我们明确一下函数的引用,如下所示: def test1(): print("--- in test1 func----") # 调用函数 test1() # 引用函 ...
- Python3运算符
一.算术运算符 二.比较运算符 三 .赋值运算符 四 .位运算符 按位运算符是把数字看作二进制来进行计算的.Python中的按位运算法则如下: 下表中变量 a 为 60,b 为 13二进制格式如下: ...