// Problem: Pandemia [B] // https://sio2.mimuw.edu.pl/c/pa-2021-1/p/pan/ #include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <string> #include <cctype> #include <vector> #include <set> #include <map> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define FOREACH(e, v) for(auto const &e: v) #define IN(e, s) (s).find((e)) != (s).end() #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; #define PB push_back #define ST first #define ND second #define ALL(c) (c).begin(), (c).end() #define SZ(x) ((int)(x).size()) typedef long long LL; typedef unsigned long long ULL; typedef unsigned short UST; typedef unsigned int UINT; typedef vector<int> VI; inline void fix_io() { cin.tie(nullptr); ios::sync_with_stdio(false); } bool is_safe(int start, int end, vector<int>& v) { if (start == 0) { if (end == v.size()-1) { return true; } else { if (v[end+1] == -1) { return true; } } } else { if (end == v.size()-1) { if (v[start-1] == -1) return true; } else { if (v[start-1] == -1 && v[end+1] == -1) return true; } } return false; } int infect(vector<int>& v) { int infected = 0; FOR(i,0,v.size()-1) { if (v[i]==1) { if (i != 0) { if (v[i-1] == 0) { v[i-1] = 2; infected++; } } if (i != v.size()-1) { if (v[i+1] == 0) { v[i+1] = 2; infected++; } } } } // How can we achive it without additional loop? FOR(i,0,v.size()-1) if (v[i]==2) v[i] = 1; return infected; } int count_infected(vector<int>& v) { int count = 0; FOR(i,0,v.size()-1) if (v[i] == 1) count++; return count; } int vaccination_index(vector<int>& v) { bool in = false; int max_size = -1; int tstart = -1; int tend = -1; int start_max = -1; int end_max = -1; FOR(i,0,v.size()-1) { if (in) { if (v[i] == 0) { if (i == v.size() - 1) { // last element tend = i; int size = tend-tstart+1; if (!is_safe(tstart, tend, v)) if (size > max_size) { max_size = size; start_max = tstart; end_max = tend; } } } else { tend = i-1; in = false; int size = tend-tstart+1; if (!is_safe(tstart, tend, v)) if (size > max_size) { max_size = size; start_max = tstart; end_max = tend; } } } else { if (v[i] == 0) { in = true; tstart = i; } if (i == v.size() - 1) { // last element tend = i; int size = tend-tstart+1; if (!is_safe(tstart, tend, v)) if (size > max_size) { max_size = size; start_max = tstart; end_max = tend; } } } } if (start_max == 0) { return end_max; } else if (end_max == v.size()-1) { return start_max; } else if (v[start_max-1] == -1) { return end_max; } else { return start_max; } } void solve() { int n; int zeros=0; string s; vector<int> cities; cin >> n; cin >> s; FOR(i,0,s.length()-1) { if (s[i] == '1') { cities.PB(1); } else { cities.PB(0); zeros++; } } if (zeros == n) { cout << "0\n"; return; } while(zeros > 0) { // FOR(i,0,cities.size()-1) cout << cities[i] << " ";cout << '\n'; int vidx = vaccination_index(cities); if (vidx != -1) { cities[vidx] = -1; zeros--; } else { break; } zeros -= infect(cities); } cout << count_infected(cities) << '\n'; } int main() { fix_io(); UINT t; cin >> t; while(t--) solve(); }
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | // Problem: Pandemia [B] // https://sio2.mimuw.edu.pl/c/pa-2021-1/p/pan/ #include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <string> #include <cctype> #include <vector> #include <set> #include <map> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define FOREACH(e, v) for(auto const &e: v) #define IN(e, s) (s).find((e)) != (s).end() #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; #define PB push_back #define ST first #define ND second #define ALL(c) (c).begin(), (c).end() #define SZ(x) ((int)(x).size()) typedef long long LL; typedef unsigned long long ULL; typedef unsigned short UST; typedef unsigned int UINT; typedef vector<int> VI; inline void fix_io() { cin.tie(nullptr); ios::sync_with_stdio(false); } bool is_safe(int start, int end, vector<int>& v) { if (start == 0) { if (end == v.size()-1) { return true; } else { if (v[end+1] == -1) { return true; } } } else { if (end == v.size()-1) { if (v[start-1] == -1) return true; } else { if (v[start-1] == -1 && v[end+1] == -1) return true; } } return false; } int infect(vector<int>& v) { int infected = 0; FOR(i,0,v.size()-1) { if (v[i]==1) { if (i != 0) { if (v[i-1] == 0) { v[i-1] = 2; infected++; } } if (i != v.size()-1) { if (v[i+1] == 0) { v[i+1] = 2; infected++; } } } } // How can we achive it without additional loop? FOR(i,0,v.size()-1) if (v[i]==2) v[i] = 1; return infected; } int count_infected(vector<int>& v) { int count = 0; FOR(i,0,v.size()-1) if (v[i] == 1) count++; return count; } int vaccination_index(vector<int>& v) { bool in = false; int max_size = -1; int tstart = -1; int tend = -1; int start_max = -1; int end_max = -1; FOR(i,0,v.size()-1) { if (in) { if (v[i] == 0) { if (i == v.size() - 1) { // last element tend = i; int size = tend-tstart+1; if (!is_safe(tstart, tend, v)) if (size > max_size) { max_size = size; start_max = tstart; end_max = tend; } } } else { tend = i-1; in = false; int size = tend-tstart+1; if (!is_safe(tstart, tend, v)) if (size > max_size) { max_size = size; start_max = tstart; end_max = tend; } } } else { if (v[i] == 0) { in = true; tstart = i; } if (i == v.size() - 1) { // last element tend = i; int size = tend-tstart+1; if (!is_safe(tstart, tend, v)) if (size > max_size) { max_size = size; start_max = tstart; end_max = tend; } } } } if (start_max == 0) { return end_max; } else if (end_max == v.size()-1) { return start_max; } else if (v[start_max-1] == -1) { return end_max; } else { return start_max; } } void solve() { int n; int zeros=0; string s; vector<int> cities; cin >> n; cin >> s; FOR(i,0,s.length()-1) { if (s[i] == '1') { cities.PB(1); } else { cities.PB(0); zeros++; } } if (zeros == n) { cout << "0\n"; return; } while(zeros > 0) { // FOR(i,0,cities.size()-1) cout << cities[i] << " ";cout << '\n'; int vidx = vaccination_index(cities); if (vidx != -1) { cities[vidx] = -1; zeros--; } else { break; } zeros -= infect(cities); } cout << count_infected(cities) << '\n'; } int main() { fix_io(); UINT t; cin >> t; while(t--) solve(); } |