#include <algorithm> #include <cstdio> #include <utility> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(b)-1;i>=(a);--i) #define REP(i,n) FOR(i,0,n) #define REPD(i,n) FORD(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define PB push_back #define ALL(c) (c).begin(),(c).end() #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef pair<int,int> PII; typedef vector<PII> VII; int end(const PII& x) { return (x.FT << 1) / x.SD; } bool comp(const PII& x, const PII& y) { return end(x) > end(y); } int main() { INT(t); REP(tt,t) { INT(n); scanf("\n"); VII v; bool f = 1; int x = 0; REP(i,n) { if (getchar() == '1') { if (x) v.PB(MP(x, 2 - f)); x = 0; f = 0; } else ++x; } if (f) { printf("0\n"); continue; } if (x) v.PB(MP(x, 1)); sort(ALL(v), comp); int d = 0, r = n; FORE(it,v) { int l = it->FT - d * it->SD; if (l <= 0) continue; if (it->SD == 2 && l >= 2) { ++r; if (l > 2) ++d; } r -= l; ++d; } printf("%d\n", r); } }
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 | #include <algorithm> #include <cstdio> #include <utility> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(b)-1;i>=(a);--i) #define REP(i,n) FOR(i,0,n) #define REPD(i,n) FORD(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define PB push_back #define ALL(c) (c).begin(),(c).end() #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef pair<int,int> PII; typedef vector<PII> VII; int end(const PII& x) { return (x.FT << 1) / x.SD; } bool comp(const PII& x, const PII& y) { return end(x) > end(y); } int main() { INT(t); REP(tt,t) { INT(n); scanf("\n"); VII v; bool f = 1; int x = 0; REP(i,n) { if (getchar() == '1') { if (x) v.PB(MP(x, 2 - f)); x = 0; f = 0; } else ++x; } if (f) { printf("0\n"); continue; } if (x) v.PB(MP(x, 1)); sort(ALL(v), comp); int d = 0, r = n; FORE(it,v) { int l = it->FT - d * it->SD; if (l <= 0) continue; if (it->SD == 2 && l >= 2) { ++r; if (l > 2) ++d; } r -= l; ++d; } printf("%d\n", r); } } |