Codeforces 1098B. Nice table 构造
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1098B.html
题解
首先,我们来证明一个结论:
合法的矩阵要么满足每列只有两种字符,要么满足每行只有两种字符。
然后直接枚举就好了。
代码并不是那么好写。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL read(){
LL x=0;
char ch=getchar();
while (!isdigit(ch))
ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x;
}
const int N=300005;
const LL INF=1LL<<60;
int n,m;
char ch[4]={'A','C','G','T'};
int v[300],a[N],b[N],t[N],ans;
char s[N];
int &A(int i,int j){
return a[(i-1)*m+j];
}
int &B(int i,int j){
return b[(i-1)*m+j];
}
int &T(int i,int j){
return t[(i-1)*m+j];
}
int get(int c,int v1,int v2){
int c1=0,c2=0;
for (int i=1;i<=n;i++){
if (i&1){
c1+=A(i,c)!=v1;
c2+=A(i,c)!=v2;
}
else {
c1+=A(i,c)!=v2;
c2+=A(i,c)!=v1;
}
}
if (c1<=c2){
for (int i=1;i<=n;i++)
if (i&1)
T(i,c)=v1;
else
T(i,c)=v2;
return c1;
}
else {
for (int i=1;i<=n;i++)
if (i&1)
T(i,c)=v2;
else
T(i,c)=v1;
return c2;
}
}
int get2(int c,int v1,int v2){
int c1=0,c2=0;
for (int i=1;i<=m;i++){
if (i&1){
c1+=A(c,i)!=v1;
c2+=A(c,i)!=v2;
}
else {
c1+=A(c,i)!=v2;
c2+=A(c,i)!=v1;
}
}
if (c1<=c2){
for (int i=1;i<=m;i++)
if (i&1)
T(c,i)=v1;
else
T(c,i)=v2;
return c1;
}
else {
for (int i=1;i<=m;i++)
if (i&1)
T(c,i)=v2;
else
T(c,i)=v1;
return c2;
}
}
int main(){
v['A']=0,v['C']=1,v['G']=2,v['T']=3;
n=read(),m=read();
for (int i=1;i<=n;i++){
scanf("%s",s+1);
for (int j=1;j<=m;j++)
A(i,j)=v[s[j]];
}
ans=n*m;
for (int i=0;i<4;i++)
for (int j=i+1;j<4;j++){
int now=0;
int ii=-1,jj;
for (int k=0;k<4;k++)
if (k!=i&&k!=j)
if (!~ii)
ii=k;
else
jj=k;
for (int k=1;k<=m;k++)
if (k&1)
now+=get(k,i,j);
else
now+=get(k,ii,jj);
if (now<ans){
ans=now;
for (int i=1;i<=n*m;i++)
b[i]=t[i];
}
}
for (int i=0;i<4;i++)
for (int j=i+1;j<4;j++){
int now=0;
int ii=-1,jj;
for (int k=0;k<4;k++)
if (k!=i&&k!=j)
if (!~ii)
ii=k;
else
jj=k;
for (int k=1;k<=n;k++)
if (k&1)
now+=get2(k,i,j);
else
now+=get2(k,ii,jj);
if (now<ans){
ans=now;
for (int i=1;i<=n*m;i++)
b[i]=t[i];
}
}
for (int i=1;i<=n;i++,puts(""))
for (int j=1;j<=m;j++)
putchar(ch[B(i,j)]);
return 0;
}
Codeforces 1098B. Nice table 构造的更多相关文章
- Codeforces.226D.The table(构造)
题目链接 \(Description\) 给定一个\(n\times m\)的矩阵\(A_{i,j}\),每次可以将一列或一行取负.求一个方案使得若干次操作后,每行每列的和都非负. \(n,m\leq ...
- Codeforces 417E Square Table(随机算法)
题目链接:Codeforces 417E Square Table 题目大意:给出n和m.要求给出一个矩阵,要求每一列每一行的元素的平方总和是一个平方数. 解题思路:构造.依照 a a a b a a ...
- Codeforces Round #140 (Div. 1) D. The table 构造
D. The table 题目连接: http://www.codeforces.com/contest/226/problem/D Description Harry Potter has a di ...
- CodeForces 1099E - Nice table - [好题]
题目链接:https://codeforces.com/problemset/problem/1099/E You are given an $n×m$ table, consisting of ch ...
- Lua中的table构造式(table constructor)
最简单的构造式就是一个空构造式{},用于创建一个空table. 构造式还可以用于初始化数组.例如,以下语句:days = {"Sunday", "Monday" ...
- Codeforces 1383D - Rearrange(构造)
Codeforces 题面传送门 & 洛谷题面传送门 一道不算困难的构造,花了一节英语课把它搞出来了,题解简单写写吧( 考虑从大往小加数,显然第三个条件可以被翻译为,每次加入一个元素,如果它所 ...
- Codeforces 549B. Looksery Party[构造]
B. Looksery Party time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- codeforces 582A. GCD Table 解题报告
题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...
- codeforces 323A. Black-and-White Cube 构造
输入n 1 <= n <= 100 有一个n * n * n 的立方体,由n ^ 3 个1 * 1 * 1 的单位立方体构成 要用white 和 black 2种颜色来染这n ^ 3个立方 ...
随机推荐
- mpvue——引入vant_weapp组件
克隆仓库 克隆后,将dist目录下的所有文件复制到项目中的/static/vant/目录下,vant目录是我自己创建为了区分的 git clone https://github.com/youzan/ ...
- 爬虫 requests 模块
requests 模块 介绍 使用requests可以模拟浏览器的请求, 比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) ps: requests库发 ...
- NowCoder -- 牛客小白月赛10
A--勘测 推下公式 a[i] = a[i-1] + a[i-2] +2 #include<stdio.h> #include<string.h> ]; int main() ...
- vue实战记录(二)- vue实现购物车功能之创建vue实例
vue实战,一步步实现vue购物车功能的过程记录,课程与素材来自慕课网,自己搭建了express本地服务器来请求数据 作者:狐狸家的鱼 本文链接:vue实战-实现购物车功能(二) GitHub:sue ...
- phpmyadmin低权限getshell
账号:‘localhost’@'@” 密码:为空 可获得一个低权限账号 利用方法: Mysql可以把指定的文件写进表 CREATE TABLE `test`.`a` (`a1` TEXT NOT NU ...
- C#中的Finalize,Dispose,SuppressFinalize的实现和使用介绍
原文地址:http://www.csharpwin.com/csharpspace/8927r1397.shtml MSDN建议按照下面的模式实现IDisposable接口: public class ...
- SpringBoot系列: url重定向和转发
Web UI项目中, 很多 Spring controller 视图函数直接返回 html 页面, 还有一些视图函数是要重定向或转发到其他的 url 上. redirect 和 forward的区别: ...
- Coursera, Big Data 3, Integration and Processing (week 4)
Week 4 Big Data Precessing Pipeline 上图可以generalize 成下图,也就是Big data pipeline some high level processi ...
- L2-001 紧急救援 (25 分) (最短路+路径打印)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 题目: 作为一个城市的应急救援队伍的负 ...
- Web从入门到放弃<5>
<1> CSS_DOM 1,structural layer 2,presentation layer 3,behavior layer style也是一个属性 <!DOCTYPE ...