#include <iostream> #include <vector> #include <algorithm> using namespace std; /* 3 8 00110100 10 1001000010 4 0000 1 19 1000001000001000001 1 20 00100000000100000010 1 20 00011000010100001000 1 20 00001111001001111000 */ void solve(const int t, const string& s) { vector<int> areas; vector<int> edgeAreas; int i=0; while(i<s.length()) { int j=i+1; while(j<s.length()&&s[i]==s[j])++j; if(s[i]=='0') { bool isEdgeArea=i==0||j==s.length(); (isEdgeArea?edgeAreas:areas).push_back(j-i); } i=j; } if(edgeAreas.size()==1&&edgeAreas[0]==s.length()&&areas.size()==0) { cout << 0 << endl; return; } int safeCityCount=0; int currentArea=0; sort(areas.begin(),areas.end(),greater<int>()); int currentEdgeArea=0; sort(edgeAreas.begin(),edgeAreas.end(),greater<int>()); int virusReach=0; while (true) { int edgeAreaSize=currentEdgeArea<edgeAreas.size()?(edgeAreas[currentEdgeArea]-virusReach):0; int areaSize=currentArea<areas.size()?(areas[currentArea]-virusReach*2):0; if (edgeAreaSize*2>areaSize&&edgeAreaSize>0) { int savedCityCount = edgeAreas[currentEdgeArea++]-virusReach; safeCityCount += savedCityCount; ++virusReach; } else if(areaSize>0) { int savedCityCount=areas[currentArea++]-virusReach*2; safeCityCount+=savedCityCount; ++virusReach; if(savedCityCount>=2) { --safeCityCount; ++virusReach; } } else break; } cout<<(s.length()-safeCityCount)<<endl; } int main() { int n; cin >> n; for(int i=0;i<n;++i) { int t; string s; cin >> t >> s; solve(t,s); } 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; /* 3 8 00110100 10 1001000010 4 0000 1 19 1000001000001000001 1 20 00100000000100000010 1 20 00011000010100001000 1 20 00001111001001111000 */ void solve(const int t, const string& s) { vector<int> areas; vector<int> edgeAreas; int i=0; while(i<s.length()) { int j=i+1; while(j<s.length()&&s[i]==s[j])++j; if(s[i]=='0') { bool isEdgeArea=i==0||j==s.length(); (isEdgeArea?edgeAreas:areas).push_back(j-i); } i=j; } if(edgeAreas.size()==1&&edgeAreas[0]==s.length()&&areas.size()==0) { cout << 0 << endl; return; } int safeCityCount=0; int currentArea=0; sort(areas.begin(),areas.end(),greater<int>()); int currentEdgeArea=0; sort(edgeAreas.begin(),edgeAreas.end(),greater<int>()); int virusReach=0; while (true) { int edgeAreaSize=currentEdgeArea<edgeAreas.size()?(edgeAreas[currentEdgeArea]-virusReach):0; int areaSize=currentArea<areas.size()?(areas[currentArea]-virusReach*2):0; if (edgeAreaSize*2>areaSize&&edgeAreaSize>0) { int savedCityCount = edgeAreas[currentEdgeArea++]-virusReach; safeCityCount += savedCityCount; ++virusReach; } else if(areaSize>0) { int savedCityCount=areas[currentArea++]-virusReach*2; safeCityCount+=savedCityCount; ++virusReach; if(savedCityCount>=2) { --safeCityCount; ++virusReach; } } else break; } cout<<(s.length()-safeCityCount)<<endl; } int main() { int n; cin >> n; for(int i=0;i<n;++i) { int t; string s; cin >> t >> s; solve(t,s); } return 0; } |