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 #include #include #include #include #include #define LL long long #define BIGMOD 1000012177LL #define DNUM 31LL #define K3DBG(X) using namespace std; bool secondComparison(const std::pair &a,const std::pair &b) { return a.second < b.second; } void invertVec(std::vector > &A) { for (int i=0; i < A.size(); i++) { A[i].second = -A[i].second; } } void printVec(std::vector > &A, bool newline) { for (int i=0; i < A.size(); i++) { printf("(%lld,%lld) ", A[i].first, A[i].second); } if (newline) printf("\n"); } bool solve(std::vector > &A, std::vector > &B) { if (A.size() != B.size()) { //printf("error reading data %ull %ull", A.size(), B.size()); } int n = A.size(); // n == B.size() as well LL sumaLitrow = 0; LL productA=0, productB = 0; for (int i=0 ; i < n; i++) { sumaLitrow += A[i].first; productA += A[i].first * A[i].second; productB += B[i].first * B[i].second; } K3DBG(printf("product: A=%lld, B=%lld\n", productA, productB)); if (productA != productB) { return false; } std::sort(A.begin(), A.end(), secondComparison); std::sort(B.begin(), B.end(), secondComparison); K3DBG(printVec(A, true)); K3DBG(printVec(B, true)); int i=0, j=0; LL subproductA=0,subproductB=0; LL subsumA=0, subsumB = 0; for (;j < n; j++) { K3DBG(printf("j=%d\n", j)); subproductB += B[j].first * B[j].second; subsumB += B[j].first; bool OK = false; while (i < n) { K3DBG(printf("j=%d, i=%d, subsumA=%lld, subsumB=%lld\n", j, i, subsumA, subsumB)); if (subsumA + A[i].first >= subsumB) { LL diff = subsumB - subsumA; LL energyA = subproductA + diff * A[i].second; LL energyB = subproductB; K3DBG(printf("j=%d energyA=%lld, energyB=%lld\n", j, energyA, energyB)); OK = energyA <= energyB; break; } subproductA += A[i].first * A[i].second; subsumA += A[i].first; i++; } if (!OK) { return false; } } return true; } void selftest() { std::vector > A = {{2,6}, {1,2}, {3,4}}; std::vector > B = {{2,4}, {1,3}, {3,5}}; //invertVec(A); invertVec(B); printf("result=%d\n", solve(A, B)); } int main() { //selftest(); int testsCount; scanf("%d", &testsCount); for (int i=0; i < testsCount; i++) { int mugsCount; scanf("%d", &mugsCount); std::vector > mugsCurrent; std::vector > mugsDesired; for (int j=0; j < mugsCount; j++) { LL L, a, b; scanf("%lld%lld%lld", &L, &a, &b); mugsCurrent.push_back(std::make_pair(L, a)); mugsDesired.push_back(std::make_pair(L, b)); } bool r1 = solve(mugsCurrent, mugsDesired); bool r2 = r1; if (r1) { invertVec(mugsCurrent); invertVec(mugsDesired); r2 = solve(mugsCurrent, mugsDesired); } printf((r1 && r2) ? "TAK\n" : "NIE\n"); } }