A. Madoka and Math Dad
分析
给一个n,构造一个数字,使这个数字每位和为n且这个数字相邻各位不能相等,且这个数是所有数中最大的。
要使数字最大,肯定要使位数尽可能的多,而且相邻位不能相等,所以用1 2这两个数字就可以构造出所有的数字,使2尽可能放在高位,使数字最大。
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn=2e5+10;
const ll mod=-1e9+7;
const double pi=acos(-1);
int a[maxn],b[maxn];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int cnt=0;
int f=1;
if(n%3==1)
{
while(cnt<n)
{
if(f==1)
{
cout<<1;
cnt++;
f=0;
}
else {
cout<<2;
cnt+=2;
f=1;
}
}
}
else {
while(cnt<n)
{
if(f==1)
{
cout<<2;
cnt+=2;
f=0;
}
else {
cout<<1;
cnt++;
f=1;
}
}
}
cout<<endl;
}
return 0;
}
B. Madoka and the Elegant Gift
分析
就是一个暴力的遍历求解,就是不能让黑色格子组成的相交,就构不成好的矩阵。
那就用排除法,把n*m化小,化为2*2的矩阵。在2*2的矩阵中找无法构成的情况,然后在n*m中找所有的2*2。
注意,数组不能用int型,每一行中每个格输入时没有空格。
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn=2e5+10;
const ll mod=-1e9+7;
const double pi=acos(-1);
int a[maxn],b[maxn];
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
char mp[105][105];
int f=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>mp[i][j];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mp[i][j]=='1')
{
if(i+1<=n&&mp[i+1][j]=='1'&&j+1<=m&&mp[i][j+1]=='1'&&mp[i+1][j+1]=='0')
{
f=0;
}
if(i+1<=n&&mp[i+1][j]=='1'&&j-1>=1&&mp[i][j-1]=='1'&&mp[i+1][j-1]=='0')
{
f=0;
}
if(i-1>=1&&mp[i-1][j]=='1'&&j+1<=m&&mp[i][j+1]=='1'&&mp[i-1][j+1]=='0')
{
f=0;
}
if(i-1>=1&&mp[i-1][j]=='1'&&j-1>=1&&mp[i][j-1]=='1'&&mp[i-1][j-1]=='0')
{
f=0;
}
}
}
}
if(f)
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
C. Madoka and Childish Pranks
分析
给一个01矩阵,在n*m步内将全0矩阵转换为所给的01矩阵。每一步的左上角必须是0.
因为后面的每一步可以将前一步所覆盖,所以从矩阵的右下角开始修改。因为每一步的左上角必须是0所以如果所给矩阵的11位置是1的话,直接输出-1,这种无可能操作为所给矩阵。因为是在n*m步内,所以把每一步修改化小,每次修改都为1*2或2*1(这样就可以每次只涂一个格),用一个队列存储答案,最后按顺序输出即可。
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn=2e5+10;
const ll mod=-1e9+7;
const double pi=acos(-1);
int a[maxn],b[maxn];
struct node{
int x1,y1,x2,y2;
};
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
char mp[105][105];
int f=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>mp[i][j];
}
}
queue<node> q;
if(mp[1][1]=='1')
{
cout<<-1<<endl;
continue;
}
else {
for(int i=n;i>=1;i--)
{
for(int j=m;j>=1;j--)
{
if(mp[i][j]=='1')
{
if(j==1)
{
q.push({i-1,j,i,j});
}
else q.push({i,j-1,i,j});
}
}
}
cout<<q.size()<<endl;
while(q.size())
{
node t=q.front();
cout<<t.x1<<" "<<t.y1<<" "<<t.x2<<" "<<t.y2<<endl;
q.pop();
}
}
}
return 0;
}