#include <cstdio> #include <algorithm> #include <list> struct Dragon { int index; int damage, energy; inline int energyCost() const { return damage - energy; } }; typedef std::list<int> IndexList; typedef std::list<Dragon> DragonList; struct MostDamagingDragon; struct LeastEnergyCostDragon; struct PositiveDragon; typedef std::list<MostDamagingDragon> MostDamagingDragonList; typedef std::list<LeastEnergyCostDragon> LeastEnergyCostDragonList; typedef std::list<PositiveDragon> PositiveDragonList; struct MostDamagingDragon { DragonList::iterator dragon; LeastEnergyCostDragonList::iterator linked; bool operator<(const MostDamagingDragon &other) const { if (dragon->damage != other.dragon->damage) return dragon->damage > other.dragon->damage; else return dragon->index > other.dragon->index; } }; struct LeastEnergyCostDragon { DragonList::iterator dragon; MostDamagingDragonList::iterator linked; bool operator<(const LeastEnergyCostDragon &other) const { if (dragon->energyCost() != other.dragon->energyCost()) return dragon->energyCost() < other.dragon->energyCost(); else return dragon->index < other.dragon->index; } }; struct PositiveDragon { DragonList::iterator dragon; bool operator<(const PositiveDragon &other) const { if (dragon->damage != other.dragon->damage) return dragon->damage < other.dragon->damage; else return dragon->index < other.dragon->index; } }; bool solve(int n, Dragon *dragons, int z, IndexList &outIndexes) { long long energy = z; // raw DragonList rawPositiveDragons; DragonList rawNegativeDragons; for (int i = 0; i < n; ++i) { int delta = dragons[i].energy - dragons[i].damage; if (delta >= 0) rawPositiveDragons.push_back(dragons[i]); else rawNegativeDragons.push_back(dragons[i]); } // positive PositiveDragonList positiveDragons; for (DragonList::iterator iterator = rawPositiveDragons.begin(); iterator != rawPositiveDragons.end(); ++iterator) { positiveDragons.push_back(PositiveDragon()); PositiveDragonList::iterator iteratorPositive = --positiveDragons.end(); iteratorPositive->dragon = iterator; } positiveDragons.sort(); // negative MostDamagingDragonList mostDamagingDragons; LeastEnergyCostDragonList leastEnergyCostDragons; for (DragonList::iterator iterator = rawNegativeDragons.begin(); iterator != rawNegativeDragons.end(); ++iterator) { mostDamagingDragons.push_back(MostDamagingDragon()); leastEnergyCostDragons.push_back(LeastEnergyCostDragon()); MostDamagingDragonList::iterator iteratorMost = --mostDamagingDragons.end(); LeastEnergyCostDragonList::iterator iteratorLeast = --leastEnergyCostDragons.end(); iteratorMost->dragon = iterator; iteratorLeast->dragon = iterator; iteratorMost->linked = iteratorLeast; iteratorLeast->linked = iteratorMost; } mostDamagingDragons.sort(); leastEnergyCostDragons.sort(); // iterate while (!positiveDragons.empty() || !mostDamagingDragons.empty()) { // try all positive while (!positiveDragons.empty() && energy > positiveDragons.front().dragon->damage) { energy -= positiveDragons.front().dragon->damage; energy += positiveDragons.front().dragon->energy; outIndexes.push_back(positiveDragons.front().dragon->index); positiveDragons.pop_front(); } // try one most damaging which does not block if (!mostDamagingDragons.empty() && energy > mostDamagingDragons.front().dragon->damage && energy > leastEnergyCostDragons.front().dragon->damage + mostDamagingDragons.front().dragon->energyCost()) { energy -= mostDamagingDragons.front().dragon->damage; energy += mostDamagingDragons.front().dragon->energy; outIndexes.push_back(mostDamagingDragons.front().dragon->index); leastEnergyCostDragons.erase(mostDamagingDragons.front().linked); mostDamagingDragons.pop_front(); // repeat continue; } // try one least energy which does not block if (!leastEnergyCostDragons.empty() && energy > leastEnergyCostDragons.front().dragon->damage && energy > mostDamagingDragons.front().dragon->damage + leastEnergyCostDragons.front().dragon->energyCost()) { energy -= leastEnergyCostDragons.front().dragon->damage; energy += leastEnergyCostDragons.front().dragon->energy; outIndexes.push_back(leastEnergyCostDragons.front().dragon->index); mostDamagingDragons.erase(leastEnergyCostDragons.front().linked); leastEnergyCostDragons.pop_front(); // repeat continue; } // try one most damaging if (!mostDamagingDragons.empty() && energy > mostDamagingDragons.front().dragon->damage) { energy -= mostDamagingDragons.front().dragon->damage; energy += mostDamagingDragons.front().dragon->energy; outIndexes.push_back(mostDamagingDragons.front().dragon->index); leastEnergyCostDragons.erase(mostDamagingDragons.front().linked); mostDamagingDragons.pop_front(); // repeat continue; } // finish return positiveDragons.empty() && mostDamagingDragons.empty(); } return true; } int main() { int z; int n; scanf("%i%i", &n, &z); Dragon *dragons = new Dragon[n]; for (int i = 0; i < n; ++i) { dragons[i].index = i + 1; scanf("%i%i", &dragons[i].damage, &dragons[i].energy); } IndexList sequence; if (solve(n, dragons, z, sequence)) { printf("TAK\n"); for (IndexList::const_iterator iterator = sequence.begin(); iterator != sequence.end(); ++iterator) printf("%i ", *iterator); printf("\n"); } else printf("NIE\n"); 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 | #include <cstdio> #include <algorithm> #include <list> struct Dragon { int index; int damage, energy; inline int energyCost() const { return damage - energy; } }; typedef std::list<int> IndexList; typedef std::list<Dragon> DragonList; struct MostDamagingDragon; struct LeastEnergyCostDragon; struct PositiveDragon; typedef std::list<MostDamagingDragon> MostDamagingDragonList; typedef std::list<LeastEnergyCostDragon> LeastEnergyCostDragonList; typedef std::list<PositiveDragon> PositiveDragonList; struct MostDamagingDragon { DragonList::iterator dragon; LeastEnergyCostDragonList::iterator linked; bool operator<(const MostDamagingDragon &other) const { if (dragon->damage != other.dragon->damage) return dragon->damage > other.dragon->damage; else return dragon->index > other.dragon->index; } }; struct LeastEnergyCostDragon { DragonList::iterator dragon; MostDamagingDragonList::iterator linked; bool operator<(const LeastEnergyCostDragon &other) const { if (dragon->energyCost() != other.dragon->energyCost()) return dragon->energyCost() < other.dragon->energyCost(); else return dragon->index < other.dragon->index; } }; struct PositiveDragon { DragonList::iterator dragon; bool operator<(const PositiveDragon &other) const { if (dragon->damage != other.dragon->damage) return dragon->damage < other.dragon->damage; else return dragon->index < other.dragon->index; } }; bool solve(int n, Dragon *dragons, int z, IndexList &outIndexes) { long long energy = z; // raw DragonList rawPositiveDragons; DragonList rawNegativeDragons; for (int i = 0; i < n; ++i) { int delta = dragons[i].energy - dragons[i].damage; if (delta >= 0) rawPositiveDragons.push_back(dragons[i]); else rawNegativeDragons.push_back(dragons[i]); } // positive PositiveDragonList positiveDragons; for (DragonList::iterator iterator = rawPositiveDragons.begin(); iterator != rawPositiveDragons.end(); ++iterator) { positiveDragons.push_back(PositiveDragon()); PositiveDragonList::iterator iteratorPositive = --positiveDragons.end(); iteratorPositive->dragon = iterator; } positiveDragons.sort(); // negative MostDamagingDragonList mostDamagingDragons; LeastEnergyCostDragonList leastEnergyCostDragons; for (DragonList::iterator iterator = rawNegativeDragons.begin(); iterator != rawNegativeDragons.end(); ++iterator) { mostDamagingDragons.push_back(MostDamagingDragon()); leastEnergyCostDragons.push_back(LeastEnergyCostDragon()); MostDamagingDragonList::iterator iteratorMost = --mostDamagingDragons.end(); LeastEnergyCostDragonList::iterator iteratorLeast = --leastEnergyCostDragons.end(); iteratorMost->dragon = iterator; iteratorLeast->dragon = iterator; iteratorMost->linked = iteratorLeast; iteratorLeast->linked = iteratorMost; } mostDamagingDragons.sort(); leastEnergyCostDragons.sort(); // iterate while (!positiveDragons.empty() || !mostDamagingDragons.empty()) { // try all positive while (!positiveDragons.empty() && energy > positiveDragons.front().dragon->damage) { energy -= positiveDragons.front().dragon->damage; energy += positiveDragons.front().dragon->energy; outIndexes.push_back(positiveDragons.front().dragon->index); positiveDragons.pop_front(); } // try one most damaging which does not block if (!mostDamagingDragons.empty() && energy > mostDamagingDragons.front().dragon->damage && energy > leastEnergyCostDragons.front().dragon->damage + mostDamagingDragons.front().dragon->energyCost()) { energy -= mostDamagingDragons.front().dragon->damage; energy += mostDamagingDragons.front().dragon->energy; outIndexes.push_back(mostDamagingDragons.front().dragon->index); leastEnergyCostDragons.erase(mostDamagingDragons.front().linked); mostDamagingDragons.pop_front(); // repeat continue; } // try one least energy which does not block if (!leastEnergyCostDragons.empty() && energy > leastEnergyCostDragons.front().dragon->damage && energy > mostDamagingDragons.front().dragon->damage + leastEnergyCostDragons.front().dragon->energyCost()) { energy -= leastEnergyCostDragons.front().dragon->damage; energy += leastEnergyCostDragons.front().dragon->energy; outIndexes.push_back(leastEnergyCostDragons.front().dragon->index); mostDamagingDragons.erase(leastEnergyCostDragons.front().linked); leastEnergyCostDragons.pop_front(); // repeat continue; } // try one most damaging if (!mostDamagingDragons.empty() && energy > mostDamagingDragons.front().dragon->damage) { energy -= mostDamagingDragons.front().dragon->damage; energy += mostDamagingDragons.front().dragon->energy; outIndexes.push_back(mostDamagingDragons.front().dragon->index); leastEnergyCostDragons.erase(mostDamagingDragons.front().linked); mostDamagingDragons.pop_front(); // repeat continue; } // finish return positiveDragons.empty() && mostDamagingDragons.empty(); } return true; } int main() { int z; int n; scanf("%i%i", &n, &z); Dragon *dragons = new Dragon[n]; for (int i = 0; i < n; ++i) { dragons[i].index = i + 1; scanf("%i%i", &dragons[i].damage, &dragons[i].energy); } IndexList sequence; if (solve(n, dragons, z, sequence)) { printf("TAK\n"); for (IndexList::const_iterator iterator = sequence.begin(); iterator != sequence.end(); ++iterator) printf("%i ", *iterator); printf("\n"); } else printf("NIE\n"); return 0; } |