A.
思路很简单,代码量有点大(可能是我方法不大好)
写了18分钟
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long,long long> pll;
template<class T>inline void rd(T &x){x=0;char o,f=1;while(o=getchar(),o<48)if(o==45)f=-f;do x=(x<<3)+(x<<1)+(o^48);while(o=getchar(),o>47);x*=f;}
const int inf=~0u>>2; //1073741823
const ll INF=~0ull>>2;//4611686018427387903
const int maxn=1e3+10;
int n,m;
void solve()
{
rd(n),rd(m);
if((n*m)%2==1)
{
int flag=0,flag2=0;
for(int i=1;i<=n;i++)
{
if(flag2==0) flag=0,flag2=1;
else flag=1,flag2=0;
for(int j=1;j<=m;j++)
{
if(flag==0)
{
cout<<'B';
flag=1;
}
else
{
cout<<'W';
flag=0;
}
}
cout<<endl;
}
return;
}
else
{
int flag=0,flag2=0,flag3=0;
for(int i=1;i<=n;i++)
{
if(flag2==0) flag=0,flag2=1;
else flag=1,flag2=0;
for(int j=1;j<=m;j++)
{
if(flag==0)
{
cout<<'B';
flag=1;
}
else
{
if(flag3==0)
{
flag3=1;
flag=0;
cout<<'B';
}
else
{
cout<<'W';
flag=0;
}
}
}
cout<<endl;
}
return;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("hzh.in","r",stdin);
//freopen("hzh.out","w",stdout);
#endif
ll T;
rd(T);
while(T--)
solve();
return 0;
}
B.
9min秒杀了
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long,long long> pll;
template<class T>inline void rd(T &x){x=0;char o,f=1;while(o=getchar(),o<48)if(o==45)f=-f;do x=(x<<3)+(x<<1)+(o^48);while(o=getchar(),o>47);x*=f;}
const int inf=~0u>>2; //1073741823
const ll INF=~0ull>>2;//4611686018427387903
const int maxn=1e5+10;
ll n,m,a[maxn],b[maxn];
void solve()
{
ll flag1=0,flag2=0;
rd(n);
for(ll i=1;i<=n;i++) rd(a[i]);
for(ll i=1;i<=n;i++) rd(b[i]);
for(ll i=1;i<=n;i++)
{
if(flag1==0&&a[i]<b[i])
{
puts("NO");
return;
}
if(flag2==0&&a[i]>b[i])
{
puts("NO");
return;
}
if(a[i]==1) flag1=1;
if(a[i]==-1) flag2=1;
if(flag1==1&&flag2==1)
{
puts("YES");
return;
}
}
puts("YES");
return;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("hzh.in","r",stdin);
//freopen("hzh.out","w",stdout);
#endif
ll T;
rd(T);
while(T--)
solve();
return 0;
}
C.
被卡了好久
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long,long long> pll;
template<class T>inline void rd(T &x){x=0;char o,f=1;while(o=getchar(),o<48)if(o==45)f=-f;do x=(x<<3)+(x<<1)+(o^48);while(o=getchar(),o>47);x*=f;}
const int inf=~0u>>2; //1073741823
const ll INF=~0ull>>2;//4611686018427387903
const int maxn=2e5+10;
ll n,a[maxn],b[maxn];
void solve()
{
ll ans=0,rec=0;
map<ll,ll>m1,m2;
rd(n);
for(ll i=1;i<=n;i++) rd(a[i]),b[i]=b[i-1]+a[i];
m1[0]=0;
m2[0]=1;
for(ll i=1;i<=n;i++)
{
if(m1[b[i]]!=0)
{
while(rec<=m1[b[i]])
{
--m2[b[rec]];
++rec;
}
}
ans+=i-rec-m2[b[i]];
m1[b[i]]=i;
m2[b[i]]++;
}
cout<<ans<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("hzh.in","r",stdin);
//freopen("hzh.out","w",stdout);
#endif
/*
ll T;
rd(T);
while(T--)
*/
solve();
return 0;
}
D.
用cout会超时
思路:第一步存最少次数的答案,第二步拆分次数为k
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long,long long> pll;
template<class T>inline void rd(T &x){x=0;char o,f=1;while(o=getchar(),o<48)if(o==45)f=-f;do x=(x<<3)+(x<<1)+(o^48);while(o=getchar(),o>47);x*=f;}
const int inf=~0u>>2; //1073741823
const ll INF=~0ull>>2;//4611686018427387903
const int maxn=3000000+10;
string s;
ll n,k;
vector<ll>ans[maxn];
void solve()
{
rd(n),rd(k);
getline(cin,s);
ll sum=0,ed=0;
for(ll ii=1;;ii++)
{
ll flag=0;
for(ll i=1;i<n;i++)
{
if(s[i-1]=='R'&&s[i]=='L')
{
swap(s[i-1],s[i]);
flag=1;
ans[ii].push_back(i);
i++;
sum++;
ed=ii;
}
}
if(flag==0) break;
}
if(ed>k||sum<k)
{
puts("-1");
return;
}
int cur=0,ff=0;
if(ed==k) ff=1;
for(ll i=1;i<=ed;i++)
{
ll len=ans[i].size();
if(ff==1)
{
printf("%lld ",len);
for(ll j=0;j<len;j++) printf("%lld ",ans[i][j]);
puts("");
continue;
}
else
{
for(ll j=0;j<len;j++)
{
if(cur+ed-i+1==k)
{
ff=1;
printf("%lld ",len-j);
for(ll j2=j;j2<len;j2++) printf("%lld ",ans[i][j2]);
puts("");
break;
}
printf("1 %lld\n",ans[i][j]);
++cur;
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("hzh.in","r",stdin);
//freopen("hzh.out","w",stdout);
#endif
/*
ll T;
rd(T);
while(T--)
*/
solve();
return 0;
}
四道编程题解析
本文解析了四道编程挑战题目,包括棋盘颜色分配、序列比较、子序列计数及字符串操作,涵盖算法设计、数据结构应用及逻辑判断等关键技能。
237

被折叠的 条评论
为什么被折叠?



