#include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, a, b, c, z, m, d, t; vi va,vk; string w; priority_queue<vi> pq; int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\t1.in.txt", "rt", stdin); cin >> t; for (int cas = 1; cas <= t; cas++) { cin >> n; cin >> w; va.clear(); vk.assign(2, 0); while (!pq.empty()) pq.pop(); d = 0; k = 0; c = w[0]-'0'; if (c == 0) d = 1; for (int i = 1; i < n; i++) { if (w[i] == '0') { if (c == 1) { c = 0; k = 1; } d++; } else { if (c == 0) { c = 1; k++; vk[0] = d*(3-k); vk[1] = k; pq.push(vk); d = 0; k = 0; } } } if (d > 0) { vk[0] = 2*d; vk[1] = 1; pq.push(vk); } d = 0; z = 0; while (!pq.empty()) { vk = pq.top(); pq.pop(); if (vk[1] > 1) { vk[0] -= 2 * d; if (vk[0] > 0) { vk[1]--; z++; vk[0]--; vk[0] += d; vk[0] *= 2; pq.push(vk); } else break; } else { if (vk[0]/2 > d) z += vk[0]/2 - d; else break; } d++; } cout << n - z << endl; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, a, b, c, z, m, d, t; vi va,vk; string w; priority_queue<vi> pq; int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\t1.in.txt", "rt", stdin); cin >> t; for (int cas = 1; cas <= t; cas++) { cin >> n; cin >> w; va.clear(); vk.assign(2, 0); while (!pq.empty()) pq.pop(); d = 0; k = 0; c = w[0]-'0'; if (c == 0) d = 1; for (int i = 1; i < n; i++) { if (w[i] == '0') { if (c == 1) { c = 0; k = 1; } d++; } else { if (c == 0) { c = 1; k++; vk[0] = d*(3-k); vk[1] = k; pq.push(vk); d = 0; k = 0; } } } if (d > 0) { vk[0] = 2*d; vk[1] = 1; pq.push(vk); } d = 0; z = 0; while (!pq.empty()) { vk = pq.top(); pq.pop(); if (vk[1] > 1) { vk[0] -= 2 * d; if (vk[0] > 0) { vk[1]--; z++; vk[0]--; vk[0] += d; vk[0] *= 2; pq.push(vk); } else break; } else { if (vk[0]/2 > d) z += vk[0]/2 - d; else break; } d++; } cout << n - z << endl; } return 0; } |