D-牛妹爱数列
考虑仅进行 的翻转操作,那么对于任意一个 串,都可以通过 次翻转得到
那么这个 串长度超过 时,采用整串翻转,否则采用单个翻转
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<pair<int, int>> data; int tmp; cin >> tmp; data.emplace_back(tmp, 1); for (int i = 0; i < n - 1; ++i) { cin >> tmp; if (data.back().first == tmp) data.back().second++; else data.emplace_back(tmp, 1); } int re = 1; int ans = 0; for (int i = (int) data.size() - 1; i >= 0; --i) { if (data[i].first == re) { if (data[i].second >= 2) { re ^= 1; } ans += 1; } } cout << ans << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifdef ACM_LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); signed localTestCount = 1, localReadPos = cin.tellg(); char localTryReadChar; do { if (localTestCount > 20) throw runtime_error("Check the stdin!!!"); auto startClockForDebug = clock(); solve(); auto endClockForDebug = clock(); cout << "Test " << localTestCount << " successful" << endl; cerr << "Test " << localTestCount++ << " Run Time: " << double(endClockForDebug - startClockForDebug) / CLOCKS_PER_SEC << "s" << endl; cout << "--------------------------------------------------" << endl; } while (localReadPos != cin.tellg() && cin >> localTryReadChar && localTryReadChar != '$' && cin.putback(localTryReadChar)); #else solve(); #endif return 0; }