#include <cstdio> struct Kandydat { int idx = -1; int max_zdrowi_sasiedzi = -1; Kandydat() {}; Kandydat(int i, int n) { idx = i; max_zdrowi_sasiedzi = n; }; }; int liczba_testow; int main() { scanf("%d", &liczba_testow); for (int t = 0; t < liczba_testow; t++) { int miasta[5000]; int miasta_temp[5000]; int chorzy = 0; int zaszczepieni = 0; int n_miast; Kandydat kandydaci[5000]; scanf("%d", &n_miast); int temp; scanf("%d", &temp); for (int i = n_miast - 1; i >= 0; i--) { miasta[i] = temp % 10; temp /= 10; } // Liczba chorych na poczatku for (int i = 0; i < n_miast; i++) { //printf("%d", miasta[i]); if (miasta[i] == 1) chorzy++; } //printf("\n"); while (true) { int choice = -1; int choice_zdrowi_sasiedzi = 0; int any_with_sick_possibility = -1; int any_healthy = -1; int kandydaci_size = 0; for (int i = 0; i < n_miast; i++) { /*printf("%d", miasta[i]); if (i == n_miast - 1) { printf("\n"); }*/ int sasiedzi_zarazeni = 0; int sasiedzi_zdrowi = 0; int szczepimy = false; if (miasta[i] == 1 || miasta[i] == 2) { continue; } any_healthy = i; if (i - 1 >= 0 && miasta[i - 1] == 1) { sasiedzi_zarazeni++; any_with_sick_possibility = i; } if (i + 1 < n_miast && miasta[i + 1] == 1) { sasiedzi_zarazeni++; any_with_sick_possibility = i; } if (i - 1 >= 0 && miasta[i - 1] == 0) { sasiedzi_zdrowi++; } if (i + 1 < n_miast && miasta[i + 1] == 0) { sasiedzi_zdrowi++; } // Znajdz miasta ktore moga byc zakazone i zakazac dalej if (sasiedzi_zarazeni > 0 && sasiedzi_zdrowi > 0) { int max = 0; // ilosc zdrowych sasiadow po lewej stronie int j = i - 1; int sasiedzi = 0; while (miasta[j] == 0) { sasiedzi++; j--; } max = sasiedzi; // ilosc zdrowych sasiadow po prawej stronie j = i + 1; sasiedzi = 0; while (miasta[j] == 0) { sasiedzi++; j++; } // Wybierz miasto z najwiekszym potencjalem na dalsze zarazanie if (sasiedzi > max) { max = sasiedzi; } kandydaci[kandydaci_size++] = Kandydat(i, max); } } // Choose with many neighbours if (kandydaci_size > 0) { for (int i = 0; i < kandydaci_size; i++) { Kandydat k = kandydaci[i]; if (k.max_zdrowi_sasiedzi > choice_zdrowi_sasiedzi) { choice = k.idx; } } } // choose with sick possibility else if (any_with_sick_possibility > -1) { choice = any_with_sick_possibility; } // choose any else if (any_healthy > -1) { choice = any_healthy; } // koniec else { printf("%d\n", chorzy); break; } /*printf("%d\n", kandydaci_size); printf("%d\n", any_with_sick_possibility); printf("%d\n", any_healthy);*/ miasta[choice] = 2; // Szczepienie zaszczepieni++; // PO SZCZEPIENIU for (int i = 0; i < n_miast; i++) { miasta_temp[i] = miasta[i]; } for (int i = 0; i < n_miast; i++) { if (miasta[i] == 0 || miasta[i] == 2) { continue; } // Sasiad po lewo zachoruje if (i - 1 >= 0 && miasta[i - 1] == 0) { if (miasta_temp[i - 1] == 0) { chorzy++; } miasta_temp[i - 1] = 1; } // Sasiad po prawo zachoruje if (i + 1 < n_miast && miasta[i + 1] == 0) { if (miasta_temp[i + 1] == 0) { chorzy++; } miasta_temp[i + 1] = 1; } } for (int i = 0; i < n_miast; i++) { miasta[i] = miasta_temp[i]; //printf("%d ", miasta_temp[i]); } } } 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 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 | #include <cstdio> struct Kandydat { int idx = -1; int max_zdrowi_sasiedzi = -1; Kandydat() {}; Kandydat(int i, int n) { idx = i; max_zdrowi_sasiedzi = n; }; }; int liczba_testow; int main() { scanf("%d", &liczba_testow); for (int t = 0; t < liczba_testow; t++) { int miasta[5000]; int miasta_temp[5000]; int chorzy = 0; int zaszczepieni = 0; int n_miast; Kandydat kandydaci[5000]; scanf("%d", &n_miast); int temp; scanf("%d", &temp); for (int i = n_miast - 1; i >= 0; i--) { miasta[i] = temp % 10; temp /= 10; } // Liczba chorych na poczatku for (int i = 0; i < n_miast; i++) { //printf("%d", miasta[i]); if (miasta[i] == 1) chorzy++; } //printf("\n"); while (true) { int choice = -1; int choice_zdrowi_sasiedzi = 0; int any_with_sick_possibility = -1; int any_healthy = -1; int kandydaci_size = 0; for (int i = 0; i < n_miast; i++) { /*printf("%d", miasta[i]); if (i == n_miast - 1) { printf("\n"); }*/ int sasiedzi_zarazeni = 0; int sasiedzi_zdrowi = 0; int szczepimy = false; if (miasta[i] == 1 || miasta[i] == 2) { continue; } any_healthy = i; if (i - 1 >= 0 && miasta[i - 1] == 1) { sasiedzi_zarazeni++; any_with_sick_possibility = i; } if (i + 1 < n_miast && miasta[i + 1] == 1) { sasiedzi_zarazeni++; any_with_sick_possibility = i; } if (i - 1 >= 0 && miasta[i - 1] == 0) { sasiedzi_zdrowi++; } if (i + 1 < n_miast && miasta[i + 1] == 0) { sasiedzi_zdrowi++; } // Znajdz miasta ktore moga byc zakazone i zakazac dalej if (sasiedzi_zarazeni > 0 && sasiedzi_zdrowi > 0) { int max = 0; // ilosc zdrowych sasiadow po lewej stronie int j = i - 1; int sasiedzi = 0; while (miasta[j] == 0) { sasiedzi++; j--; } max = sasiedzi; // ilosc zdrowych sasiadow po prawej stronie j = i + 1; sasiedzi = 0; while (miasta[j] == 0) { sasiedzi++; j++; } // Wybierz miasto z najwiekszym potencjalem na dalsze zarazanie if (sasiedzi > max) { max = sasiedzi; } kandydaci[kandydaci_size++] = Kandydat(i, max); } } // Choose with many neighbours if (kandydaci_size > 0) { for (int i = 0; i < kandydaci_size; i++) { Kandydat k = kandydaci[i]; if (k.max_zdrowi_sasiedzi > choice_zdrowi_sasiedzi) { choice = k.idx; } } } // choose with sick possibility else if (any_with_sick_possibility > -1) { choice = any_with_sick_possibility; } // choose any else if (any_healthy > -1) { choice = any_healthy; } // koniec else { printf("%d\n", chorzy); break; } /*printf("%d\n", kandydaci_size); printf("%d\n", any_with_sick_possibility); printf("%d\n", any_healthy);*/ miasta[choice] = 2; // Szczepienie zaszczepieni++; // PO SZCZEPIENIU for (int i = 0; i < n_miast; i++) { miasta_temp[i] = miasta[i]; } for (int i = 0; i < n_miast; i++) { if (miasta[i] == 0 || miasta[i] == 2) { continue; } // Sasiad po lewo zachoruje if (i - 1 >= 0 && miasta[i - 1] == 0) { if (miasta_temp[i - 1] == 0) { chorzy++; } miasta_temp[i - 1] = 1; } // Sasiad po prawo zachoruje if (i + 1 < n_miast && miasta[i + 1] == 0) { if (miasta_temp[i + 1] == 0) { chorzy++; } miasta_temp[i + 1] = 1; } } for (int i = 0; i < n_miast; i++) { miasta[i] = miasta_temp[i]; //printf("%d ", miasta_temp[i]); } } } return 0; } |