#include <bits/stdc++.h> using namespace std; const int MAX_N = 18; const int MAX_L = 100000; int powers[MAX_N + 1]; int next_free[MAX_N][MAX_L + 1]; int next_available[MAX_N][MAX_L + 1]; int dp_whole[1<<MAX_N]; int dp_fractional[1<<MAX_N]; string chores[MAX_N]; int L; int N; int whole_part; int frac_a; int frac_b; int ceiling; vector<pair<int, int> > fractions; void make_next_free(){ for(int i = 0; i < N; i++){ next_free[i][L] = L; for(int j = L - 1; j >= 0; j--){ if(chores[i][j] == '.'){ next_free[i][j] = next_free[i][j + 1]; } else { next_free[i][j] = j; } } } } int gcd(int a, int b){ if(a == 0){ return b; } else if(b == 0){ return a; } if(a < b) swap(a, b); return gcd(a%b, b); } void create_fractions(){ for(int i = 1; i <= N; i++){ for(int j = 0; j < i; j++){ int a = j; int b = i; int d = gcd(a, b); a /= d; b /= d; fractions.push_back(make_pair(a, b)); } } sort(fractions.begin(), fractions.end(), [](pair<int, int> P, pair<int, int> Q){return P.first * Q.second < P.second * Q.first;}); fractions.erase(unique(fractions.begin(), fractions.end()), fractions.end()); } void find_next_available(){ for(int i = 0; i < N; i++){ next_available[i][L] = L + 1; for(int j = L - 1; j >= 0; j--){ if(next_free[i][j] >= j + ceiling){ next_available[i][j] = j; } else { next_available[i][j] = next_available[i][j + 1]; } } } } // we always check a non-empty sleeping schedule. bool current_works(){ find_next_available(); /* cout << "NEXT FREE" << endl; for(int i = 0; i < N; i++){ for(int j = 0; j <= L; j++){ cout << next_free[i][j] << ' '; } cout << endl; } cout << "NEXT AVAILABLE" << endl; for(int i = 0; i < N; i++){ for(int j = 0; j <= L; j++){ cout << next_available[i][j] << ' '; } cout << endl; } cout << "DP START" << endl; */ // for dp, we keep dp_fractional in range [0, frac_b - 1]. dp_whole[0] = 0; dp_fractional[0] = 0; bitset<MAX_N> it; uint new_index; int new_whole, new_fractional; for(uint index = 1; index < powers[N]; index++){ it = bitset<MAX_N>(index); dp_whole[index] = L; dp_fractional[index] = 1; for(int i = 0; i < N; i++){ if(it[i]){ new_index = index - powers[i]; if(dp_whole[new_index] >= L) continue; // new_fractional must lie in interval [1, frac_b]. new_whole = dp_whole[new_index] + whole_part; new_fractional = dp_fractional[new_index] + frac_a; if(new_fractional > frac_b){ new_whole++; new_fractional -= frac_b; } else if(new_fractional == 0){ new_whole--; new_fractional = frac_b; } if(new_whole >= L) continue; if(next_free[i][dp_whole[new_index]] <= new_whole){ // in this case, we can not place sleeping time right in this moment // so we update with: (next_available[i][dp_whole[new_index]], 0) if(next_available[i][dp_whole[new_index]] <= dp_whole[index]){ dp_whole[index] = next_available[i][dp_whole[new_index]]; dp_fractional[index] = 0; } } else { // otherwise, we find that everything works! if(new_fractional == frac_b){ new_fractional = 0; new_whole++; } // so we update with: (new_whole, new_fractional) if(new_whole < dp_whole[index]){ dp_whole[index] = new_whole; dp_fractional[index] = new_fractional; } else if(new_whole == dp_whole[index]){ dp_fractional[index] = min(new_fractional, dp_fractional[index]); } } } } // cout << index << ": " << dp_whole[index] << " + " << dp_fractional[index] << "/" << frac_b << endl; } if(dp_whole[powers[N] - 1] < L) return true; if(dp_whole[powers[N] - 1] == L && dp_fractional[powers[N] - 1] == 0) return true; return false; } void get_fraction_from_order(int ord){ whole_part = ord / fractions.size(); frac_a = fractions[ord - whole_part*fractions.size()].first; frac_b = fractions[ord - whole_part*fractions.size()].second; if(frac_a > 0){ ceiling = whole_part + 1; } else { ceiling = whole_part; } } bool preprocess(){ // basically, if there exists a column that is fully // occupied, then output is -1. Furthermore, if a // column has only 1 person available, then this person // must take care of the baby, hence is occupied. // Otherwise, if at least 2 people are free at a given // slot, then there will always be someone to take // care of the baby! for(int j = 0; j < L; j++){ int count = 0; for(int i = 0; i < N; i++){ if(chores[i][j] == '.'){ count++; } } if(count == 0){ // baby is left alone return false; } else if(count == 1){ for(int i = 0; i < N; i++){ chores[i][j] = 'X'; } } } return true; } void process(){ int order_a = 0; int order_b = (L + N - 1) / N; order_b *= fractions.size(); while(order_a < order_b){ int h = (order_a + order_b + 1)/2; get_fraction_from_order(h); // cout << "checking: " << whole_part << "+" << frac_a << "/" << frac_b << endl; if(current_works()){ order_a = h; } else { order_b = h - 1; } } get_fraction_from_order(order_a); // and here is the output. cout << (whole_part * frac_b + frac_a) << "/" << frac_b << endl; } int main(){ cin.tie(NULL); ios_base::sync_with_stdio(0); cin >> N >> L; for(int i = 0; i < N; i++){ cin >> chores[i]; } powers[0] = 1; for(int i = 1; i <= N; i++){ powers[i] = 2*powers[i - 1]; } if(preprocess()){ make_next_free(); create_fractions(); process(); } else { cout << "-1" << endl; } }
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | #include <bits/stdc++.h> using namespace std; const int MAX_N = 18; const int MAX_L = 100000; int powers[MAX_N + 1]; int next_free[MAX_N][MAX_L + 1]; int next_available[MAX_N][MAX_L + 1]; int dp_whole[1<<MAX_N]; int dp_fractional[1<<MAX_N]; string chores[MAX_N]; int L; int N; int whole_part; int frac_a; int frac_b; int ceiling; vector<pair<int, int> > fractions; void make_next_free(){ for(int i = 0; i < N; i++){ next_free[i][L] = L; for(int j = L - 1; j >= 0; j--){ if(chores[i][j] == '.'){ next_free[i][j] = next_free[i][j + 1]; } else { next_free[i][j] = j; } } } } int gcd(int a, int b){ if(a == 0){ return b; } else if(b == 0){ return a; } if(a < b) swap(a, b); return gcd(a%b, b); } void create_fractions(){ for(int i = 1; i <= N; i++){ for(int j = 0; j < i; j++){ int a = j; int b = i; int d = gcd(a, b); a /= d; b /= d; fractions.push_back(make_pair(a, b)); } } sort(fractions.begin(), fractions.end(), [](pair<int, int> P, pair<int, int> Q){return P.first * Q.second < P.second * Q.first;}); fractions.erase(unique(fractions.begin(), fractions.end()), fractions.end()); } void find_next_available(){ for(int i = 0; i < N; i++){ next_available[i][L] = L + 1; for(int j = L - 1; j >= 0; j--){ if(next_free[i][j] >= j + ceiling){ next_available[i][j] = j; } else { next_available[i][j] = next_available[i][j + 1]; } } } } // we always check a non-empty sleeping schedule. bool current_works(){ find_next_available(); /* cout << "NEXT FREE" << endl; for(int i = 0; i < N; i++){ for(int j = 0; j <= L; j++){ cout << next_free[i][j] << ' '; } cout << endl; } cout << "NEXT AVAILABLE" << endl; for(int i = 0; i < N; i++){ for(int j = 0; j <= L; j++){ cout << next_available[i][j] << ' '; } cout << endl; } cout << "DP START" << endl; */ // for dp, we keep dp_fractional in range [0, frac_b - 1]. dp_whole[0] = 0; dp_fractional[0] = 0; bitset<MAX_N> it; uint new_index; int new_whole, new_fractional; for(uint index = 1; index < powers[N]; index++){ it = bitset<MAX_N>(index); dp_whole[index] = L; dp_fractional[index] = 1; for(int i = 0; i < N; i++){ if(it[i]){ new_index = index - powers[i]; if(dp_whole[new_index] >= L) continue; // new_fractional must lie in interval [1, frac_b]. new_whole = dp_whole[new_index] + whole_part; new_fractional = dp_fractional[new_index] + frac_a; if(new_fractional > frac_b){ new_whole++; new_fractional -= frac_b; } else if(new_fractional == 0){ new_whole--; new_fractional = frac_b; } if(new_whole >= L) continue; if(next_free[i][dp_whole[new_index]] <= new_whole){ // in this case, we can not place sleeping time right in this moment // so we update with: (next_available[i][dp_whole[new_index]], 0) if(next_available[i][dp_whole[new_index]] <= dp_whole[index]){ dp_whole[index] = next_available[i][dp_whole[new_index]]; dp_fractional[index] = 0; } } else { // otherwise, we find that everything works! if(new_fractional == frac_b){ new_fractional = 0; new_whole++; } // so we update with: (new_whole, new_fractional) if(new_whole < dp_whole[index]){ dp_whole[index] = new_whole; dp_fractional[index] = new_fractional; } else if(new_whole == dp_whole[index]){ dp_fractional[index] = min(new_fractional, dp_fractional[index]); } } } } // cout << index << ": " << dp_whole[index] << " + " << dp_fractional[index] << "/" << frac_b << endl; } if(dp_whole[powers[N] - 1] < L) return true; if(dp_whole[powers[N] - 1] == L && dp_fractional[powers[N] - 1] == 0) return true; return false; } void get_fraction_from_order(int ord){ whole_part = ord / fractions.size(); frac_a = fractions[ord - whole_part*fractions.size()].first; frac_b = fractions[ord - whole_part*fractions.size()].second; if(frac_a > 0){ ceiling = whole_part + 1; } else { ceiling = whole_part; } } bool preprocess(){ // basically, if there exists a column that is fully // occupied, then output is -1. Furthermore, if a // column has only 1 person available, then this person // must take care of the baby, hence is occupied. // Otherwise, if at least 2 people are free at a given // slot, then there will always be someone to take // care of the baby! for(int j = 0; j < L; j++){ int count = 0; for(int i = 0; i < N; i++){ if(chores[i][j] == '.'){ count++; } } if(count == 0){ // baby is left alone return false; } else if(count == 1){ for(int i = 0; i < N; i++){ chores[i][j] = 'X'; } } } return true; } void process(){ int order_a = 0; int order_b = (L + N - 1) / N; order_b *= fractions.size(); while(order_a < order_b){ int h = (order_a + order_b + 1)/2; get_fraction_from_order(h); // cout << "checking: " << whole_part << "+" << frac_a << "/" << frac_b << endl; if(current_works()){ order_a = h; } else { order_b = h - 1; } } get_fraction_from_order(order_a); // and here is the output. cout << (whole_part * frac_b + frac_a) << "/" << frac_b << endl; } int main(){ cin.tie(NULL); ios_base::sync_with_stdio(0); cin >> N >> L; for(int i = 0; i < N; i++){ cin >> chores[i]; } powers[0] = 1; for(int i = 1; i <= N; i++){ powers[i] = 2*powers[i - 1]; } if(preprocess()){ make_next_free(); create_fractions(); process(); } else { cout << "-1" << endl; } } |