A. Almost Rectangle 源自cf #713 div3
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There is a square field of size n×nn×n in which two cells are marked. These cells can be in the same row or column.
You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes.
For example, if n=4n=4 and a rectangular field looks like this (there are asterisks in the marked cells):
…∗…∗…∗…∗…
Then you can mark two more cells as follows
∗.∗…∗.∗…∗.∗…∗.∗…
If there are several possible solutions, then print any of them.
Input
The first line contains a single integer tt (1≤t≤4001≤t≤400). Then tt test cases follow.
The first row of each test case contains a single integer nn (2≤n≤4002≤n≤400) — the number of rows and columns in the table.
The following nn lines each contain nn characters ‘.’ or ‘*’ denoting empty and marked cells, respectively.
It is guaranteed that the sums of nn for all test cases do not exceed 400400.
It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column.
It is guaranteed that the solution exists.
Output
For each test case, output nn rows of nn characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them.
Example
input
Copy
6
4
..*.
....
*...
....
2
*.
.*
2
.*
.*
3
*.*
...
...
5
.....
..*..
.....
.*...
.....
4
....
....
*...
*...
output
Copy
*.*.
....
*.*.
....
**
**
**
**
*.*
*.*
...
.....
.**..
.....
.**..
.....
....
....
**..
**..
#include<bits/stdc++.h>
using namespace std;
const int maxn=410;
char a[maxn][maxn];
int main()
{
int t,n;
cin>>t;
while(t--){
memset(a,0,sizeof(a));
cin>>n;
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
cin>>a[i][j];
}
}
int h1=0,l1=0,h2=0,l2=0;
int cnt=0;
bool flag=false;
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(a[i][j]=='*'&&cnt==0){
cnt++;
h1=i;
l1=j;
}
else if(a[i][j]=='*'&&cnt==1){
cnt++;
h2=i;
l2=j;
flag=true;
}
if(flag){
break;
}
}
if(flag){
break;
}
}
if(l1==l2){
if(l1+1<n){
a[h1][l1+1]='*';
a[h2][l2+1]='*';
}
else if(l1-1>=0){
a[h1][l1-1]='*';
a[h2][l2-1]='*';
}
}
else{
if(h1==h2){
if(h1+1<n){
a[h1+1][l1]='*';
a[h2+1][l2]='*';
}
else if(h1-1>=0){
a[h1-1][l1]='*';
a[h2-1][l2]='*';
}
}
else{
a[h1][l2]='*';
a[h2][l1]='*';
}
}
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
cout<<a[i][j];
}
cout<<endl;
}
}
return 0;
}
B. A-B Palindromecf #713 div3
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a string ss consisting of the characters ‘0’, ‘1’, and ‘?’. You need to replace all the characters with ‘?’ in the string ss by ‘0’ or ‘1’ so that the string becomes a palindrome and has exactly aa characters ‘0’ and exactly bb characters ‘1’. Note that each of the characters ‘?’ is replaced independently from the others.
A string tt of length nn is called a palindrome if the equality t[i]=t[n−i+1]t[i]=t[n−i+1] is true for all ii (1≤i≤n1≤i≤n).
For example, if s=s=“01???0”, a=4a=4 and b=4b=4, then you can replace the characters ‘?’ in the following ways:
- “01011010”;
- “01100110”.
For the given string ss and the numbers aa and bb, replace all the characters with ‘?’ in the string ss by ‘0’ or ‘1’ so that the string becomes a palindrome and has exactly aa characters ‘0’ and exactly bb characters ‘1’.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104). Then tt test cases follow.
The first line of each test case contains two integers aa and bb (0≤a,b≤2⋅1050≤a,b≤2⋅105, a+b≥1a+b≥1).
The second line of each test case contains the string ss of length a+ba+b, consisting of the characters ‘0’, ‘1’, and ‘?’.
It is guaranteed that the sum of the string lengths of ss over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, output:
- “-1”, if you can’t replace all the characters ‘?’ in the string ss by ‘0’ or ‘1’ so that the string becomes a palindrome and that it contains exactly aa characters ‘0’ and exactly bb characters ‘1’;
- the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example
input
Copy
9
4 4
01?????0
3 3
??????
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
output
Copy
01011010
-1
0
-1
0110
-1
111
1001
0101010
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
char z[maxn],f[maxn];
int main()
{
int t;
cin>>t;
while(t--){
bool flag=false;
int cnt0,cnt1;
string s;
cin>>cnt0>>cnt1>>s;
for(int i=0;i<s.size();++i){
z[i]=s[i];
f[i]=s[s.size()-i-1];
}
if(s.size()==1){
if(z[0]=='0'){
if(cnt0>=1){
cout<<"0"<<endl;
flag=true;
}
else{
cout<<"-1"<<endl;
flag=true;
}
}
else if(z[0]=='1'){
if(cnt1>=1){
cout<<"1"<<endl;
flag=true;
}
else{
cout<<"-1"<<endl;
flag=true;
}
}
else{
if(cnt0){
cout<<"0"<<endl;
flag=true;
}
else if(cnt1){
cout<<"1"<<endl;
flag=true;
}
else{
cout<<"-1"<<endl;
flag=true;
}
}
}
if(flag) continue;
//for(int i=0;i<s.size();++i) cout<<z[i]; puts("");
//for(int i=0;i<s.size();++i) cout<<f[i]; puts("");
for(int i=0;i<s.size()/2;++i){
if(z[i]=='?'&&f[i]=='?') continue;
else if(z[i]=='1'&&f[i]=='0'){
cout<<"-1"<<endl;
flag=true;
break;
}
else if(z[i]=='0'&&f[i]=='1'){
cout<<"-1"<<endl;
flag=true;
break;
}
else if(z[i]=='0'&&f[i]=='0'){
if(cnt0>=2) cnt0-=2;
else{
cout<<"-1"<<endl;
flag=true;
break;
}
}
else if(z[i]=='1'&&f[i]=='1'){
if(cnt1>=2) cnt1-=2;
else{
cout<<"-1"<<endl;
flag=true;
break;
}
}
else if(z[i]=='?'&&f[i]=='1'){
if(cnt1>=2){
cnt1-=2;
z[i]='1';
}
else{
cout<<"-1"<<endl;
flag=true;
break;
}
}
else if(z[i]=='?'&&f[i]=='0'){
if(cnt0>=2){
cnt0-=2;
z[i]='0';
}
else{
cout<<"-1"<<endl;
flag=true;
break;
}
}
else if(z[i]=='1'&&f[i]=='?'){
if(cnt1>=2){
cnt1-=2;
f[i]='1';
}
else{
cout<<"-1"<<endl;
flag=true;
break;
}
}
else if(z[i]=='0'&&f[i]=='?'){
if(cnt0>=2){
cnt0-=2;
f[i]='0';
}
else{
cout<<"-1"<<endl;
flag=true;
break;
}
}
}
if(flag) continue;
else{
for(int i=0;i<s.size()/2;++i){
if(z[i]=='?'&&f[i]=='?'){
if(cnt0>=2){
z[i]='0',f[i]='0';
cnt0-=2;
}
else if(cnt1>=2){
z[i]='1',f[i]='1';
cnt1-=2;
}
else{
cout<<"-1"<<endl;
flag=true;
break;
}
}
}
}
if(flag) continue;
else if(!flag&&s.size()%2==0){
for(int i=0;i<s.size()/2;++i) cout<<z[i];
for(int i=s.size()/2-1;i>=0;--i) cout<<f[i];
puts("");
continue;
}
else if(!flag&&s.size()%2){
if(z[s.size()/2]=='0'){
if(cnt0>=1){
cnt0--;
}
else{
cout<<"-1"<<endl;
flag=true;
}
}
else if(z[s.size()/2]=='1'){
if(cnt1>=1) cnt1--;
else{
cout<<"-1"<<endl;
flag=true;
}
}
else{
if(cnt0>=1){
z[s.size()/2]=f[s.size()/2]='0';
cnt0--;
}
else if(cnt1>=1){
z[s.size()/2]=f[s.size()/2]='1';
cnt1--;
}
else{
cout<<"-1"<<endl;
flag=true;
}
}
}
if(flag) continue;
for(int i=0;i<s.size()/2;++i) cout<<z[i];
cout<<z[s.size()/2];
for(int i=s.size()/2-1;i>=0;--i) cout<<f[i];
puts("");
}
return 0;
}
G - A Computer Graphics Problem(水题)
In this problem we talk about the study of Computer Graphics. Of course, this is very, very hard.
We have designed a new mobile phone, your task is to write a interface to display battery powers.
Here we use ‘.’ as empty grids.
When the battery is empty, the interface will look like this:
------------
|…|
|…|
|…|
|…|
|…|
|…|
|…|
|…|
|…|
|…|
------------
When the battery is 60% full, the interface will look like this:
------------
|…|
|…|
|…|
… |
---|
------------ |
------------ |
------------ |
------------ |
------------ |
------------
Each line there are 14 characters.
Given the battery power the mobile phone left, say x%, your task is to output the corresponding interface. Here x will always be a multiple of 10, and never exceeds 100.
Input
The first line has a number T (T < 10) , indicating the number of test cases.
For each test case there is a single line with a number x. (0 < x < 100, x is a multiple of 10)
Output
For test case X, output “Case #X:” at the first line. Then output the corresponding interface.
See sample output for more details.
Sample Input
2
0
60
Sample Output
Case #1:
------------
|…|
|…|
|…|
|…|
|…|
|…|
|…|
|…|
|…|
|…|
------------
Case #2:
------------
|…|
|…|
|…|
… |
---|
------------ |
------------ |
------------ |
------------ |
------------ |
------------
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
for(int i=1;i<=t;++i){
int rem;
cin>>rem;
cout<<"Case #"<<i<<":"<<endl;
for(int j=1;j<=14;++j){
if(j==1) cout<<"*";
else if(j==14) cout<<"*"<<endl;
else cout<<'-';
}
for(int j=1;j<=10-rem/10;++j){
for(int k=1;k<=14;++k){
if(k==1) cout<<"|";
else if(k==14) cout<<"|"<<endl;
else cout<<".";
}
}
for(int j=10-rem/10+1;j<=10;++j){
for(int k=1;k<=14;++k){
if(k==1) cout<<"|";
else if(k==14) cout<<"|"<<endl;
else cout<<"-";
}
}
for(int j=1;j<=14;++j){
if(j==1) cout<<"*";
else if(j==14) cout<<"*"<<endl;
else cout<<"-";
}
}
return 0;
}