2021.5.30
A
题意:输入一个长度为n的01字符串,每次操作可以将字符串中有且只有一个邻居为1的0变为1,问m次操作变化后的字符串。
题解:每次操作遍历字符串,如果0字符只有一个邻居为1就变为1,如果遍历完发现整个字符串未改变,那么直接退出操作,因为字符串已经不会再变化,这样可以避免超时(即使每次改变一个字符,因为n<10^3,所以只要退出不改变的就不会超时),若有改变再进入下一个操作。
#include <iostream>
#include <algorithm>
using namespace std;
const int N=1005;
char s[N];
int main()
{
ios_base::sync_with_stdio(false);
int t,n,m;
cin>>t;
while(t--)
{
cin>>n>>m>>(s+1);
while(m--)
{
int num=0,k=-1;
for(int i=1;i<=n;i++)
{
if(s[i]=='0')
{
if((s[i-1]=='1'&&k!=i-1&&s[i+1]!='1')||((s[i-1]!='1'||(s[i-1]=='1'&&k==i-1))&&s[i+1]=='1'))
{
s[i]='1';
k=i;
num++;
}
}
}
//cout<<(s+1)<<endl;
if(!num)
break;
}
cout<<(s+1)<<endl;
}
return 0;
}
B
题意:一个长度为偶数n的正整数序列a,有下列两种操作,要求利用下列两种操作使得每个序列的元素都是原序列对应位置元素的相反值,输出每次操作的类型和操作的两个元素下标。
前提i<j
1.
2.
题解:
每次对两个元素a,b进行操作,其中分为6步操作:
1.type 1 a+b b
2.type 2 a+b -a
3.type 1 b -a
4.type 1 b-a -a
5.type 2 b-a -b
6.type 1 -a -b
这样就可以将两两元素变为相反值,因为题目也刚好是偶数
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
const int N=1005;
ll a[N];
void solve(int a,int b)
{
cout<<"1 "<<a<<' '<<b<<endl;
cout<<"2 "<<a<<' '<<b<<endl;
cout<<"1 "<<a<<' '<<b<<endl;
cout<<"1 "<<a<<' '<<b<<endl;
cout<<"2 "<<a<<' '<<b<<endl;
cout<<"1 "<<a<<' '<<b<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
ll n,t;
cin>>t;
while(t--){
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<6*n/2<<endl;
for(int i=1;i<=n;i+=2)
{
solve(i,i+1);
}
}
return 0;
}
C
题意:说实在没题目看懂,看样例猜题意的。应该是像目录那种的,比如1.1之后只能是1.2或者1.1.1,如果在1.1后加入2不能直接1.1.2,因为没有1.1.1这个目录,只能变为1.2。
题解:初始序列保存一个1,然后每次输入x找前面的等于x-1的那个数,因为必须保证有答案,所以肯定可以找得到,然后对x-1这个数+1变为x,这个位置就是x要放的。
#include <iostream>
#include <algorithm>
using namespace std;
const int N=100005;
int a[N];
int main()
{
ios_base::sync_with_stdio(false);
int t,n,x;
cin>>t;
while(t--)
{
int l=0;
cin>>n;
while(n--)
{
cin>>x;
if(x==1)
a[++l]=1;
else
{
while(a[l]!=x-1&&l>=0)
l--;
a[l]++;
}
cout<<a[1];
for(int i=2;i<=l;i++)
cout<<'.'<<a[i];
cout<<endl;
}
}
return 0;
}