C++:2024/2/21 (Codeforces)

文章介绍了三个Codeforces编程问题,涉及字符串回文判断、通过数组操作使数组非递减以及稳定放置棋子的算法。第一题讨论了如何通过检查字符串是否能形成无长度大于1的回文子串来确定答案;第二题涉及将数组中的1向后移动以达到最优非递减状态;第三题探讨了在有限棋盘上放置棋子以实现稳定的攻击范围。
摘要由CSDN通过智能技术生成

A. ABC

原题链接:Problem - A - Codeforces

题目大意:

给一个由1,0组成的字符串,问你这个字符串重新排列后能不能满足:

无长度大于1的substr是回文,若不可则输出NO,反之YES。

substr的定义是:

A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

也即从一个字符串中截取出的一段连续字符序列。

题目做法:

如果长度大于等于3的话。

你可以想,10后无论接1还是接0都会使得条件不满足。

如果是11或00,那都不需要等到长度大于等于3就不可以了。

所以可以得出一个结论长度大于等于3一定会使得条件不满足。

AC代码:

#include<bits/stdc++.h>
#define pb(element) push_back(element)
#define fast ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
//#define int long long
using namespace std;
const int mode = 1e9 + 7;
const int maxn = 2e5 + 10;
void solve(){
    int n;
    string s;
    cin >> n >> s;
    if(s.length() >= 3){
        cout << "NO" << '\n';
        return ;
    }
    if(s.length() == 1){
        cout << "YES" << '\n';
        return ;
    }
    if(s.length() == 2 && s[0] != s[1]){
        cout << "YES" << '\n';
        return ;
    }
    cout << "NO" << '\n';
}
signed main(){
    fast int casen = 1;
    cin >> casen;
    while(casen--) solve();
}

B. Rebellion

原题链接:Problem - B - Codeforces

题目大意:

给你一个数组,该数组由0或1组成,你可以进行操作,每次操作选取两个点,吧其中一个点的值加到另外一个点上,然后把加完的点从数组中移除。

问你最小进行多少次操作后能让数组变得非递减。

题目做法:

把1都往后放,形成的数组最优,从后往前扫,0的位置放1。

AC代码:

#include<bits/stdc++.h>
#define pb(element) push_back(element)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//#define int long long
#define ll long long
//Cara's templet version 1.18
//2024/2/20 20:32 UTC+8
//Tell u what when the time I see someone
//Even that I can't see the clear of one's face
//I still can feel sth different
using namespace std;
const int maxn=2e5+10;
void solve()
{
    int n;
    cin>>n;
    int ar[n];
    int c1=0,op=0;
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
        if(ar[i]) c1++;
    }
    for(int i=n-1;i>=0&&c1;i--)
    {
        if(ar[i]==0) op++,c1--;
        else c1--;
        //cout<<i<<" "<<c1<<" "<<op<<'\n';
    }
    cout<<op<<'\n';
}
signed main()
{
	fast int casen=1;
	cin>>casen;
	while(casen--) solve();
}

A. Stable Arrangement of Rooks

 原题链接:Problem - A - Codeforces

题目大意:

他定义了两种放置,棋子间互相攻击不到的是good,任意一棋子移动一格也互相攻击不到的称为stable,棋子攻击范围是同行同列。

给你k个棋子,n * n的棋盘,问你有无stable的放置,没有输出 -1,有的话输出棋盘放置。

题目做法:

对角线,贪心放,放的了就可以,放一个空隔一个。

AC代码:

#include<bits/stdc++.h>
#define pb(element) push_back(element)
#define fast ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
//#define int long long
using namespace std;
const int mode = 1e9 + 7;
const int maxn = 2e5 + 10;
void solve(){
    int n, k, ppx = 0, ppy = 0;
    cin >> n >> k;
    // cout << ceil(double(n) / 2) <<'\n';
    if(ceil(double(n) / 2) < k){
        cout << "-1" << '\n';
        return ;
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(i == ppx && j == ppy && k > 0){
                cout << "R";
                k--;
                ppx += 2;
                ppy += 2;
            }
            else cout << ".";
        }
        cout << '\n';
    }
}
signed main(){
    fast int casen = 1;
    cin >> casen;
    while(casen--) solve();
}

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值