//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; struct Info { uint_fast32_t decrease, increase; uint_fast32_t id; }; inline static void solution() { using std::swap; uint_fast32_t n; int_fast64_t z; in >> n >> z; std::vector<Info> stage1; std::vector<Info> stage2; for(uint_fast32_t i = 1; i <= n; ++i) { uint_fast32_t dec, inc; in >> dec >> inc; int_fast32_t diff = inc - dec; if(diff >= 0) stage1.push_back( {dec, inc, i}); else stage2.push_back( {dec, inc, i}); } std::sort(stage1.begin(), stage1.end(), [](const Info & lhs, const Info & rhs) { return lhs.decrease<rhs.decrease; }); for(const Info& m:stage1) { z-=m.decrease; if(z<=0) break; z+=m.increase; } if(z<=0) { out<<"NIE\n"; return; } // z = stage1 result (top) std::sort(stage2.begin(),stage2.end(),[](const Info & lhs, const Info & rhs){ return lhs.increase<rhs.increase; }); uint_fast64_t stage2result=0; for(const Info& m:stage2) { stage2result=std::max<uint_fast64_t>(stage2result,m.increase); stage2result-=m.increase; stage2result+=m.decrease; } stage2result+=1; if(uint_fast64_t(z)>=stage2result) { out<<"TAK\n"; for(const Info& m : stage1) out<<m.id<<' '; for(auto i=stage2.crbegin(),e=stage2.crend();i!=e;++i) out<<i->id<<' '; out<<'\n'; } else { out<<"NIE\n"; } } } // 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 | //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; struct Info { uint_fast32_t decrease, increase; uint_fast32_t id; }; inline static void solution() { using std::swap; uint_fast32_t n; int_fast64_t z; in >> n >> z; std::vector<Info> stage1; std::vector<Info> stage2; for(uint_fast32_t i = 1; i <= n; ++i) { uint_fast32_t dec, inc; in >> dec >> inc; int_fast32_t diff = inc - dec; if(diff >= 0) stage1.push_back( {dec, inc, i}); else stage2.push_back( {dec, inc, i}); } std::sort(stage1.begin(), stage1.end(), [](const Info & lhs, const Info & rhs) { return lhs.decrease<rhs.decrease; }); for(const Info& m:stage1) { z-=m.decrease; if(z<=0) break; z+=m.increase; } if(z<=0) { out<<"NIE\n"; return; } // z = stage1 result (top) std::sort(stage2.begin(),stage2.end(),[](const Info & lhs, const Info & rhs){ return lhs.increase<rhs.increase; }); uint_fast64_t stage2result=0; for(const Info& m:stage2) { stage2result=std::max<uint_fast64_t>(stage2result,m.increase); stage2result-=m.increase; stage2result+=m.decrease; } stage2result+=1; if(uint_fast64_t(z)>=stage2result) { out<<"TAK\n"; for(const Info& m : stage1) out<<m.id<<' '; for(auto i=stage2.crbegin(),e=stage2.crend();i!=e;++i) out<<i->id<<' '; out<<'\n'; } else { out<<"NIE\n"; } } } // 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; } |