//Maciej Poleski #ifdef DEBUG #define _GLIBCXX_CONCEPT_CHECKS #include <iostream> #include <fstream> #include <cstdlib> #include <cassert> namespace { namespace Wrapper { std::ifstream in; std::ofstream out; } void init(int argc, char **argv) { if(argc != 3) { std::cerr << "Potrzeba dokładnie dwóch argumentów\n"; std::abort(); } Wrapper::in.open(argv[1]); Wrapper::out.open(argv[2]); } } #define check(x) assert(x) #else #ifndef NDEBUG #define NDEBUG #endif #define check(x) #include <iostream> namespace { namespace Wrapper { std::istream &in = std::cin; std::ostream &out = std::cout; } } #endif #include <cstdint> namespace { namespace Wrapper { typedef std::uint_fast64_t uint_fast64_t; typedef std::uint_fast32_t uint_fast32_t; typedef std::uint_fast16_t uint_fast16_t; typedef std::uint_fast8_t uint_fast8_t; typedef std::uint64_t uint64_t; typedef std::uint32_t uint32_t; typedef std::uint16_t uint16_t; typedef std::uint8_t uint8_t; typedef std::int_fast64_t int_fast64_t; typedef std::int_fast32_t int_fast32_t; typedef std::int_fast16_t int_fast16_t; typedef std::int_fast8_t int_fast8_t; typedef std::int64_t int64_t; typedef std::int32_t int32_t; typedef std::int16_t int16_t; typedef std::int8_t int8_t; typedef std::size_t size_t; } } #include <string> #include <algorithm> #include <limits> #include <locale> #include <cstring> #include <utility> #include <cstdlib> #include <random> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <list> #include <iomanip> #include <set> #include <map> #include <memory> #include <functional> #include <unordered_map> #include <unordered_set> #include <complex> namespace { using namespace Wrapper; static uint_fast32_t n, m; static uint_fast32_t *items; static uint_fast32_t *bagsEnd; static uint_fast32_t *bagsStart; enum class Greedy1result { GreedyFound, // Znaleziono ograniczenie dolne BestFound, // Znaleziono rozwiązanie Failed, // Nie ma rozwiązania }; static std::pair<Greedy1result, uint_fast32_t> findBest() { bool found = true; uint_fast32_t bestMaybePossible = 1; std::vector<int_fast32_t> bags(bagsStart, bagsEnd); // detached copy auto bagsIterator = bags.begin(); for(auto item = items; item < items + n; ++item) { if(*bagsIterator <= 0) { if(*bagsIterator < 0) { found = false; } bestMaybePossible += 1; ++bagsIterator; } if(bagsIterator == bags.end()) { return std::make_pair(Greedy1result::Failed, bestMaybePossible); } *bagsIterator -= int_fast32_t(*item); } if(!found || *bagsIterator < 0) { return std::make_pair(Greedy1result::GreedyFound, bestMaybePossible+1); } else { return std::make_pair(Greedy1result::BestFound, bestMaybePossible); } } static uint_fast32_t currentBestSolution; static uint_fast32_t findSolution(const uint_fast32_t* bags,uint_fast32_t* restItems, uint_fast32_t usedBags) { check(restItems!=items+n); if(usedBags+1==currentBestSolution) { std::sort(restItems,items+n); // Z sufiksem już nic nie da się zrobić return n+1; // fail } if(bags==bagsEnd) { std::sort(restItems,items+n); // Z sufiksem już nic nie da się zrobić return n+1; // fail } uint_fast32_t currentBag=*bags; uint_fast32_t *itemPtr=restItems; while(itemPtr!=items+n) { if(currentBag<*itemPtr) break; currentBag-=*itemPtr; itemPtr+=1; } if(itemPtr==items+n) // Skończone { std::sort(restItems,itemPtr); return 1; } else if(itemPtr==restItems) // Plecak pozostał pusty - błąd { std::sort(itemPtr,items+n); return n+1; // Nie ma rozwiązania w permutacjach sufiksu [restItems,...) } else { std::sort(restItems,itemPtr); // Kolejność w plecaku nie ma znaczenia uint_fast32_t bestSolution=n; //do { const uint_fast32_t proposition=findSolution(bags+1,itemPtr,usedBags+1); bestSolution=std::min(bestSolution,proposition); } //while(std::prev_permutation(itemPtr,items+n)); //std::next_permutation(itemPtr,items+n); // Anuluj ostatnią operacje return bestSolution+1; } } static uint_fast32_t findSolutionWithoutSort(const uint_fast32_t* bags,uint_fast32_t* restItems, uint_fast32_t usedBags) { check(restItems!=items+n); if(usedBags+1==currentBestSolution) { //std::sort(restItems,items+n); // Z sufiksem już nic nie da się zrobić return n+1; // fail } if(bags==bagsEnd) { //std::sort(restItems,items+n); // Z sufiksem już nic nie da się zrobić return n+1; // fail } uint_fast32_t currentBag=*bags; uint_fast32_t *itemPtr=restItems; while(itemPtr!=items+n) { if(currentBag<*itemPtr) break; currentBag-=*itemPtr; itemPtr+=1; } if(itemPtr==items+n) // Skończone { //std::sort(restItems,itemPtr); return 1; } else if(itemPtr==restItems) // Plecak pozostał pusty - błąd { //std::sort(itemPtr,items+n); return n+1; // Nie ma rozwiązania w permutacjach sufiksu [restItems,...) } else { //std::sort(restItems,itemPtr); // Kolejność w plecaku nie ma znaczenia uint_fast32_t bestSolution=n; //do { const uint_fast32_t proposition=findSolutionWithoutSort(bags+1,itemPtr,usedBags+1); bestSolution=std::min(bestSolution,proposition); } //while(std::prev_permutation(itemPtr,items+n)); //std::next_permutation(itemPtr,items+n); // Anuluj ostatnią operacje return bestSolution+1; } } static std::mt19937 engine(404); static uint_fast32_t tryGreedy(uint_fast32_t* bags,uint_fast32_t* restItems) { std::vector<uint_fast32_t> bagsDetached(bags,bagsEnd); const auto bagsSize=bagsDetached.size(); uint_fast32_t result=0; for(auto ptr=restItems;ptr!=items+n;++ptr) { bool ok=false; for(uint_fast32_t i=0;i<bagsSize;++i) { if(bagsDetached[i]>=*ptr) { bagsDetached[i]-=*ptr; result=std::max(result,i+1); ok=true; break; } } if(!ok) return n+1; } return result; } inline static void solution() { using std::swap; in >> n >> m; items = new uint_fast32_t[n]; uint_fast32_t *bags = new uint_fast32_t[m]; uint_fast64_t totalItemsWeight = 0; uint_fast32_t maxItemWeight = 0; for(uint_fast32_t i = 0; i < n; ++i) { in >> items[i]; maxItemWeight = std::max(maxItemWeight, items[i]); totalItemsWeight += items[i]; } for(uint_fast32_t i = 0; i < m; ++i) { in >> bags[i]; } std::sort(items, items + n); std::sort(bags, bags + m); bagsEnd = bags + m; bagsStart = bagsEnd - std::min(n,m); uint_fast64_t totalBagsCapacity = 0; uint_fast32_t maxBagCapacity = 0; for(auto ptr = bagsStart; ptr < bagsEnd; ++ptr) { maxBagCapacity = std::max(maxBagCapacity, *ptr); totalBagsCapacity += *ptr; } if(maxItemWeight > maxBagCapacity) { out << "NIE\n"; } else if(totalItemsWeight > totalBagsCapacity) { out << "NIE\n"; } else { const auto firstGreedy = findBest(); if(firstGreedy.first == Greedy1result::BestFound) { out << firstGreedy.second << '\n'; } else if(firstGreedy.first == Greedy1result::Failed) { out << "NIE\n"; } else { const uint_fast32_t bestPossible=firstGreedy.second; // std::cerr<<"Best possible: "<<bestPossible<<'\n'; std::reverse(items,items+n); std::reverse(bagsStart,bagsEnd); // for(auto ptr=bagsStart;ptr!=bagsEnd;++ptr) // std::cerr<<*ptr<<' '; // std::cerr<<'\n'; bool succeed=false; currentBestSolution=n+1; uint_fast32_t itcount=0; constexpr uint_fast32_t itcountmax=2000000; constexpr uint_fast32_t totalitcontmax=3000000; bool breakit=false; do { // for(int i=0;i<n;++i) // std::cerr<<items[i]<<' '; // std::cerr<<'\n'; const uint_fast32_t proposition=findSolution(bagsStart,items,0); if(proposition==bestPossible) { out<<proposition<<'\n'; succeed=true; break; } currentBestSolution=std::min(currentBestSolution,proposition); itcount+=1; if(itcount==itcountmax) { breakit=true; break; } } while(std::prev_permutation(items,items+n)); if(breakit) { auto rand= [](std::ptrdiff_t n) { return (std::uniform_int_distribution<std::ptrdiff_t>(0,n-1))(engine); }; for(;itcount<totalitcontmax;++itcount) { std::random_shuffle(items,items+n,rand); //std::random_shuffle(bagsStart,bagsEnd,rand); const uint_fast32_t proposition=std::min(findSolutionWithoutSort(bagsStart,items,0),tryGreedy(bagsStart,items)); if(proposition==bestPossible) { out<<proposition<<'\n'; succeed=true; break; } currentBestSolution=std::min(currentBestSolution,proposition); } } // for(int i=0;i<n;++i) // std::cerr<<items[i]<<' '; // std::cerr<<'\n'; if(!succeed && currentBestSolution<=std::min(n,m)) out<<currentBestSolution<<'\n'; else if(!succeed) out<<"NIE\n"; } } delete [] bags; delete [] items; } } // namespace int main(int argc, char **argv) { std::ios_base::sync_with_stdio(false); #ifdef DEBUG init(argc, argv); #else (void)argc; (void)argv; #endif solution(); 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 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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | //Maciej Poleski #ifdef DEBUG #define _GLIBCXX_CONCEPT_CHECKS #include <iostream> #include <fstream> #include <cstdlib> #include <cassert> namespace { namespace Wrapper { std::ifstream in; std::ofstream out; } void init(int argc, char **argv) { if(argc != 3) { std::cerr << "Potrzeba dokładnie dwóch argumentów\n"; std::abort(); } Wrapper::in.open(argv[1]); Wrapper::out.open(argv[2]); } } #define check(x) assert(x) #else #ifndef NDEBUG #define NDEBUG #endif #define check(x) #include <iostream> namespace { namespace Wrapper { std::istream &in = std::cin; std::ostream &out = std::cout; } } #endif #include <cstdint> namespace { namespace Wrapper { typedef std::uint_fast64_t uint_fast64_t; typedef std::uint_fast32_t uint_fast32_t; typedef std::uint_fast16_t uint_fast16_t; typedef std::uint_fast8_t uint_fast8_t; typedef std::uint64_t uint64_t; typedef std::uint32_t uint32_t; typedef std::uint16_t uint16_t; typedef std::uint8_t uint8_t; typedef std::int_fast64_t int_fast64_t; typedef std::int_fast32_t int_fast32_t; typedef std::int_fast16_t int_fast16_t; typedef std::int_fast8_t int_fast8_t; typedef std::int64_t int64_t; typedef std::int32_t int32_t; typedef std::int16_t int16_t; typedef std::int8_t int8_t; typedef std::size_t size_t; } } #include <string> #include <algorithm> #include <limits> #include <locale> #include <cstring> #include <utility> #include <cstdlib> #include <random> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <list> #include <iomanip> #include <set> #include <map> #include <memory> #include <functional> #include <unordered_map> #include <unordered_set> #include <complex> namespace { using namespace Wrapper; static uint_fast32_t n, m; static uint_fast32_t *items; static uint_fast32_t *bagsEnd; static uint_fast32_t *bagsStart; enum class Greedy1result { GreedyFound, // Znaleziono ograniczenie dolne BestFound, // Znaleziono rozwiązanie Failed, // Nie ma rozwiązania }; static std::pair<Greedy1result, uint_fast32_t> findBest() { bool found = true; uint_fast32_t bestMaybePossible = 1; std::vector<int_fast32_t> bags(bagsStart, bagsEnd); // detached copy auto bagsIterator = bags.begin(); for(auto item = items; item < items + n; ++item) { if(*bagsIterator <= 0) { if(*bagsIterator < 0) { found = false; } bestMaybePossible += 1; ++bagsIterator; } if(bagsIterator == bags.end()) { return std::make_pair(Greedy1result::Failed, bestMaybePossible); } *bagsIterator -= int_fast32_t(*item); } if(!found || *bagsIterator < 0) { return std::make_pair(Greedy1result::GreedyFound, bestMaybePossible+1); } else { return std::make_pair(Greedy1result::BestFound, bestMaybePossible); } } static uint_fast32_t currentBestSolution; static uint_fast32_t findSolution(const uint_fast32_t* bags,uint_fast32_t* restItems, uint_fast32_t usedBags) { check(restItems!=items+n); if(usedBags+1==currentBestSolution) { std::sort(restItems,items+n); // Z sufiksem już nic nie da się zrobić return n+1; // fail } if(bags==bagsEnd) { std::sort(restItems,items+n); // Z sufiksem już nic nie da się zrobić return n+1; // fail } uint_fast32_t currentBag=*bags; uint_fast32_t *itemPtr=restItems; while(itemPtr!=items+n) { if(currentBag<*itemPtr) break; currentBag-=*itemPtr; itemPtr+=1; } if(itemPtr==items+n) // Skończone { std::sort(restItems,itemPtr); return 1; } else if(itemPtr==restItems) // Plecak pozostał pusty - błąd { std::sort(itemPtr,items+n); return n+1; // Nie ma rozwiązania w permutacjach sufiksu [restItems,...) } else { std::sort(restItems,itemPtr); // Kolejność w plecaku nie ma znaczenia uint_fast32_t bestSolution=n; //do { const uint_fast32_t proposition=findSolution(bags+1,itemPtr,usedBags+1); bestSolution=std::min(bestSolution,proposition); } //while(std::prev_permutation(itemPtr,items+n)); //std::next_permutation(itemPtr,items+n); // Anuluj ostatnią operacje return bestSolution+1; } } static uint_fast32_t findSolutionWithoutSort(const uint_fast32_t* bags,uint_fast32_t* restItems, uint_fast32_t usedBags) { check(restItems!=items+n); if(usedBags+1==currentBestSolution) { //std::sort(restItems,items+n); // Z sufiksem już nic nie da się zrobić return n+1; // fail } if(bags==bagsEnd) { //std::sort(restItems,items+n); // Z sufiksem już nic nie da się zrobić return n+1; // fail } uint_fast32_t currentBag=*bags; uint_fast32_t *itemPtr=restItems; while(itemPtr!=items+n) { if(currentBag<*itemPtr) break; currentBag-=*itemPtr; itemPtr+=1; } if(itemPtr==items+n) // Skończone { //std::sort(restItems,itemPtr); return 1; } else if(itemPtr==restItems) // Plecak pozostał pusty - błąd { //std::sort(itemPtr,items+n); return n+1; // Nie ma rozwiązania w permutacjach sufiksu [restItems,...) } else { //std::sort(restItems,itemPtr); // Kolejność w plecaku nie ma znaczenia uint_fast32_t bestSolution=n; //do { const uint_fast32_t proposition=findSolutionWithoutSort(bags+1,itemPtr,usedBags+1); bestSolution=std::min(bestSolution,proposition); } //while(std::prev_permutation(itemPtr,items+n)); //std::next_permutation(itemPtr,items+n); // Anuluj ostatnią operacje return bestSolution+1; } } static std::mt19937 engine(404); static uint_fast32_t tryGreedy(uint_fast32_t* bags,uint_fast32_t* restItems) { std::vector<uint_fast32_t> bagsDetached(bags,bagsEnd); const auto bagsSize=bagsDetached.size(); uint_fast32_t result=0; for(auto ptr=restItems;ptr!=items+n;++ptr) { bool ok=false; for(uint_fast32_t i=0;i<bagsSize;++i) { if(bagsDetached[i]>=*ptr) { bagsDetached[i]-=*ptr; result=std::max(result,i+1); ok=true; break; } } if(!ok) return n+1; } return result; } inline static void solution() { using std::swap; in >> n >> m; items = new uint_fast32_t[n]; uint_fast32_t *bags = new uint_fast32_t[m]; uint_fast64_t totalItemsWeight = 0; uint_fast32_t maxItemWeight = 0; for(uint_fast32_t i = 0; i < n; ++i) { in >> items[i]; maxItemWeight = std::max(maxItemWeight, items[i]); totalItemsWeight += items[i]; } for(uint_fast32_t i = 0; i < m; ++i) { in >> bags[i]; } std::sort(items, items + n); std::sort(bags, bags + m); bagsEnd = bags + m; bagsStart = bagsEnd - std::min(n,m); uint_fast64_t totalBagsCapacity = 0; uint_fast32_t maxBagCapacity = 0; for(auto ptr = bagsStart; ptr < bagsEnd; ++ptr) { maxBagCapacity = std::max(maxBagCapacity, *ptr); totalBagsCapacity += *ptr; } if(maxItemWeight > maxBagCapacity) { out << "NIE\n"; } else if(totalItemsWeight > totalBagsCapacity) { out << "NIE\n"; } else { const auto firstGreedy = findBest(); if(firstGreedy.first == Greedy1result::BestFound) { out << firstGreedy.second << '\n'; } else if(firstGreedy.first == Greedy1result::Failed) { out << "NIE\n"; } else { const uint_fast32_t bestPossible=firstGreedy.second; // std::cerr<<"Best possible: "<<bestPossible<<'\n'; std::reverse(items,items+n); std::reverse(bagsStart,bagsEnd); // for(auto ptr=bagsStart;ptr!=bagsEnd;++ptr) // std::cerr<<*ptr<<' '; // std::cerr<<'\n'; bool succeed=false; currentBestSolution=n+1; uint_fast32_t itcount=0; constexpr uint_fast32_t itcountmax=2000000; constexpr uint_fast32_t totalitcontmax=3000000; bool breakit=false; do { // for(int i=0;i<n;++i) // std::cerr<<items[i]<<' '; // std::cerr<<'\n'; const uint_fast32_t proposition=findSolution(bagsStart,items,0); if(proposition==bestPossible) { out<<proposition<<'\n'; succeed=true; break; } currentBestSolution=std::min(currentBestSolution,proposition); itcount+=1; if(itcount==itcountmax) { breakit=true; break; } } while(std::prev_permutation(items,items+n)); if(breakit) { auto rand= [](std::ptrdiff_t n) { return (std::uniform_int_distribution<std::ptrdiff_t>(0,n-1))(engine); }; for(;itcount<totalitcontmax;++itcount) { std::random_shuffle(items,items+n,rand); //std::random_shuffle(bagsStart,bagsEnd,rand); const uint_fast32_t proposition=std::min(findSolutionWithoutSort(bagsStart,items,0),tryGreedy(bagsStart,items)); if(proposition==bestPossible) { out<<proposition<<'\n'; succeed=true; break; } currentBestSolution=std::min(currentBestSolution,proposition); } } // for(int i=0;i<n;++i) // std::cerr<<items[i]<<' '; // std::cerr<<'\n'; if(!succeed && currentBestSolution<=std::min(n,m)) out<<currentBestSolution<<'\n'; else if(!succeed) out<<"NIE\n"; } } delete [] bags; delete [] items; } } // namespace int main(int argc, char **argv) { std::ios_base::sync_with_stdio(false); #ifdef DEBUG init(argc, argv); #else (void)argc; (void)argv; #endif solution(); return 0; } |