Codeforces打卡(六)

本文分别解析了两个编程竞赛题目:EducationalCodeforcesRound97的B题和1433D题。B题中,程序通过计算字符串中连续0和1的最长子串来求解;D题则涉及寻找数组中不同元素的分隔符,并输出分隔位置。两题均使用C++语言实现,涉及字符串处理和数组操作技巧。
摘要由CSDN通过智能技术生成

#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 2e5 + 10;
char s[N];

void solve()
{
     int n;
     cin >> n >> s + 1;
     int sum0 = 0, sum1 = 0;
     for (int i = 1; i <= n; i ++ )
     {
          if (s[i] == '1' && s[i + 1] == '1')
               sum0++;
          if (s[i] == '0' && s[i + 1] == '0')
               sum1++;
     }
     cout << max(sum1, sum0) << endl;
}

int main()
{
     ios::sync_with_stdio(false);
     cin.tie(0);
     cout.tie(0);
     int T;
     cin >> T;
     while (T--)
     {
          solve();
     }
     return 0;
}

https://codeforces.com/problemset/problem/1433/D题

#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;

const int N = 1e5 + 10;
int a[N];
map<int, int> mp;
vector<pair<int, int>> v;

void solve()
{
     mp.clear();
     int n;
     cin >> n;
     int b = -1, c = -1;
     int id;
     for (int i = 1; i <= n; i++)
     {
          cin >> a[i];
          mp[i] = a[i];
          if (i == 1)
               b = a[i];
          else if (i >= 2 && a[i] != b)
          {
               c = a[i];
               id = i;
          }
     }
     if (c == -1)
     {
          cout << "NO" << endl;
          return;
     }
     else
     {
          cout << "YES" << endl;
          for (int i = 2; i <= n; i++)
          {
               if (mp[i] != b)
                    v.push_back({1, i});
               else if (id != i && mp[i] == b)
                    v.push_back({id, i});
          }
          for (int i = 0; i < v.size(); i++)
          {
               cout << v[i].first << ' ' << v[i].second << endl;
          }
          v.clear();
     }
}

int main()
{
     ios::sync_with_stdio(false);
     cin.tie(0);
     cout.tie(0);
     int T;
     cin >> T;
     while (T--)
     {
          solve();
     }
     return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值