Problem - 1678B2 - Codeforces Tokitsukaze and Good 01-String (hard version)

Problem - 1678B2 - Codeforces
因为所有字段都必须是偶数,所以很自然的将i与i+1(i=1,3,5,...)组合到一起,如果si != si+1 则两个字符中一定有一个要改变,也就是这个cell不能确定是0还是1。

不能确定的cell数即是需要op的数量,能确定的cell相邻不同的数量即是最后最小的数量

很好想,最难想的地方是每两个一组的构造

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

//#define int ll
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair PII;
typedef pair PIII;
typedef pair PCI;
typedef pair PIC;
typedef pair PDD;
typedef pair PLL;
const int N = 200010, M = 2 * N, B = N;
const int INF = 0x3f3f3f3f;
const ll LLINF = 1e18;

int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int n, m, k;
char s[N];

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll x, ll n, ll mod)
{
    ll res = 1;
    while (n)
    {
        if (n & 1)res = (1ll * res * x) % mod;
        x = (1ll * x * x) % mod;
        n >>= 1;
    }
    return res;
}

void init() {}

void solve()
{
    si(n);
    ss(s + 1);
    int opcnt = 0;
    int ccnt = 0;
    char last = ' ';
    for(int i=1;i<=n;i+=2)
    {
        if (s[i + 1] != s[i]) opcnt++;
        else if (s[i] != last) ccnt++, last = s[i];
    }
    cout << opcnt << ' ' << max(1,ccnt) << endl;
}

signed main()
{
    int _;
    cin >> _;
    //_ = 1;
    init();
    while (_--)
    {
        solve();
    }
    return 0;
}

你可能感兴趣的:(codeforces,c++,蓝桥杯,算法)