C++ Const
- 写在前面
- 1.const 修饰成员变量
- 2.const修饰函数参数
- 3.const修饰成员函数
- 4.const修饰函数返回值
- (1)指针传递
- (2)值传递
- 读一首诗
[TOCM]
Consider an n \times mn×m matrix of ones and zeros. For example, this 4 × 4:
1111
0111
0111
0110
We can compute even parity for each row, and each column. In this case, the row parities are [0, 1, 1, 0][0,1,1,0] and the column parities are [1, 0, 0, 1][1,0,0,1] (the parity is 11 if there is an odd number of 11s in the row or column, 00 if the number of 11s is even). Note that the top row is row 11, the bottom row is row nn, the leftmost column is column 11, and the rightmost column is column mm.
Suppose we lost the original matrix, and only have the row and column parities. Can we recover the original matrix? Unfortunately, we cannot uniquely recover the original matrix, but with some constraints, we can uniquely recover a matrix that fits the bill. Firstly, the recovered matrix must contain as many 11’s as possible. Secondly, of all possible recovered matrices with the most 11’s, use the one which has the smallest binary value when you start with row 11, concatenate row 22 to the end of row 11, then append row 33, row 44, and so on.
Input Format
Each input will consist of a single test case.
Note that your program may be run multiple times on different inputs.
Each test case will consist of exactly two lines.
The first line will contain a string R (1 \le |R| \le 50)R(1≤∣R∣≤50), consisting only of the characters 00 and 11. These are the row parities, in order.
The second line will contain a string C (1 \le |C| \le 50)C(1≤∣C∣≤50), consisting only of the characters 00 and 11. These are the column parities, in order.
Output Format
If it is possible to recover the original matrix with the given constraints, then output the matrix as |R|∣R∣ lines of exactly |C|∣C∣ characters, consisting only of 00’s and 11’s. If it is not possible to recover the original matrix, output -1−1.
样例输入1
0110
1001
样例输出1
1111
0111
1110
1111
样例输入2
0
1
样例输出2
-1
样例输入3
11
0110
样例输出3
1011
1101
题目来源
The North American Invitational Programming Contest 2018
题目的意思是,一开始有一个矩阵,每一行的 1
的个数如果是偶数个,记为0
,奇数个记为1
,所以这样会根据矩阵行列中的 1
的个数 来形成两个10
字符串。现在已知这个最终的字符串,要我们还原出原先的矩阵,两点额外的要求:
+ 使矩阵中 1
的数目尽可能的多
+ 最后的矩阵的每一行连接起来
形成的二进制数最小
这样一来我们可以分析得到,我们要在满足尽可能多的 1
的数目的前提下,把 1
往矩阵的右下角扔,这样可以保证,得到的二进制数最小。然后就是另一个问题,我们如何保证当前是尽可能多的 1
。
一开始想把 1
先往右下角放,然后遍历上去,最后检查,但是后来发现这个做法不可取,因为最后一行有可能并不一定都能通过改动变化成合法的,所以后期就把策略改成了
1
的奇偶数,来判断需要置零的坐标这个时候要注意 ,对于下面这一种情况的特判,当时找了好久的BUG
00
11
答案应该是
00
11
如果根据我们的算法,x 和 y 的坐标数不对等的话,还要根据二者的差值是否是偶数,来判断能否更改成合法矩阵,二者差值为奇数的情况始终不可能修改成合法的矩阵,应该输出 -1
.
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = (int)50 + 1;
int dic[maxn][maxn];
int main(int argc, char const *argv[])
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
string a, b;
while(cin >> a >> b){
memset(dic,0,sizeof(dic));
vector<int> cmda,cmdb;
bool have = false;
int lena = a.size(), lenb = b.size();
for(int i = 0;i < lena; i++){
if(lenb & 1 && a[i] == '0'){
cmda.push_back(i);
}
else if(a[i] == '1' && !(lenb&1)){
cmda.push_back(i);
}
}
for(int i = 0;i < lenb; i++){
if(lena & 1 && b[i] == '0'){
cmdb.push_back(i);
}
else if(b[i] == '1' && !(lena&1)){
cmdb.push_back(i);
}
}
if((cmda.size()-cmdb.size()) & 1){
cout << -1 << endl;
have = true;
}
else if(cmda.size() == cmdb.size()){
int sl = cmda.size();
for(int i = 0;i < sl; i++){
dic[cmda[i]][cmdb[i]]++;
}
}
else{
if(cmda.size() < cmdb.size()){
int i = 0,j = 0;
for(i = 0;i < (int)cmdb.size() - (int)cmda.size(); i++){
dic[0][cmdb[i]]++;
}
for(;j < (int)cmda.size();){
dic[cmda[j++]][cmdb[i++]]++;
}
}
else{
int i = 0,j = 0;
for(i = 0;i < (int)cmda.size() - (int)cmdb.size(); i++){
dic[cmda[i]][0]++;
}
for(;j < (int)cmdb.size();){
dic[cmda[i++]][cmdb[j++]]++;
}
}
}
for(int i = 0;i < lena && !have; i++){
for(int j = 0;j < lenb; j++){
cout << 1 - dic[i][j];
}
// cout << " " << i+1;
cout << endl;
}
}
return 0;
}
代码应该很容易懂,也没加上啥巧办法,就是生写的。
-- 2018.08.12
题目中的意思是,给出一个由 0 和 1 组成的矩阵块,每次按压之后,按压的那一块,其上下左右四面的块的状态都会改变状态,问哪一种按压方法,可以在按压的次数最少的情况下使得矩阵全部为 0,最后输出矩阵。
这个在操作的时候,每一个块一定要尽可能的少操作,因为操作偶数次会恢复初始状态,并且由题意知道,操作的顺序对解结果没有影响,所以我们可以先给定第一行的状态,再由第一行去逐一判断下一行,直到最后看一看是不是合法就好了。
这里的第一行的操作的枚举,可以使用二进制来进行,快速方便,简记做如下代码:
for(int i = 0;i < (1<<n); ++i){
memset(jge,0,sizeof(jge));
for(int j = 0;j < n; ++j){
jge[0][n-j-1] = i>>j&1;
}
}
有这几行代码给出的数组的第一行,就是不同的操作状态,也就是每一种我们都去模拟一下,最后看看哪一个最小。
IMPOSSIBLE
,否则累加当前状态的操作数,看看是不是当前最优操作。#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>
#include <functional>
#include <utility>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <climits>
typedef long long ll;
using namespace std;
const int maxn = 100;
const int dx[5] = {-1,0,0,0,1};
const int dy[5] = {0,-1,0,1,0};
int org[maxn][maxn], jge[maxn][maxn], ans[maxn][maxn];
//origin / judge / answer
int n, m;
inline int find(int x, int y){
int res = org[x][y];
for(int i = 0;i < 5; ++i){
int nx = x + dx[i],ny = y + dy[i];
if(nx >= 0 && nx < m && ny >= 0 && ny < n){
res += jge[nx][ny];
}
}
return (res&1);
}
inline int js(){
for(int i = 1;i < m; ++i){
for(int j = 0;j < n; ++j){
if(find(i-1, j)){
jge[i][j] = 1;
}
}
}
for(int i = 0;i < n; ++ i){
if(find(m-1,i))
return -1;
}
int rs = 0;
for(int i = 0;i < m; ++i){
for(int j = 0;j < n; ++j){
rs += jge[i][j];
}
}
return rs;
}
inline int flip(){
int res = -1;
for(int i = 0;i < (1<<n); ++i){
memset(jge,0,sizeof(jge));
for(int j = 0;j < n; ++j){
jge[0][n-j-1] = i>>j&1;
}
cout << endl;
int num = js();
if(num >= 0 && (res < 0 || res > num)){
res = num;
memcpy(ans,jge,sizeof(jge));
}
}
if (res < 0){
cout << "IMPOSSIBLE" << endl;
}
else {
for(int i = 0;i < m; ++i){
cout << ans[i][0];
for(int j = 1;j < n; ++j){
cout << " " << ans[i][j];
}
cout << endl;
}
}
return 0;
}
int main(int argc, char const *argv[])
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
while(cin >> m >> n){
memset(org,0,sizeof(org));
for(int i = 0;i < m; ++i){
for(int j = 0;j < n; ++j){
cin >> org[i][j];
}
}
flip();
}
return 0;
}