//Autor: Mateusz Wasylkiewicz
//Zawody: Potyczki Algorytmiczne 2019
//Strona: http://potyczki.mimuw.edu.pl/
//Zadanie: Herbata, runda 2B
//Czas: O(t*n*log(n))
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<LL> VI;
typedef pair<LL, LL> PII;
#define FOR(x, a, b) for (LL x = (a); x <= (b); x++)
#define FORD(x, a, b) for (LL x = (a); x >= (b); x--)
#define REP(x, n) for (LL x = 0; x < (n); x++)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) (LL((x).size()))
#define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); i++)
#define PB push_back
#define ST first
#define ND second
#define POKAZ(x) cerr << #x << " = " << (x) << '\n'
const LL MAX = 100100;
LL n, srednia[2];
PII pary[2][MAX];
void wyczysc_wszystko()
{
REP(q, 2)
srednia[q] = 0;
}
void wczytaj_dane()
{
cin >> n;
REP(i, n)
{
LL l, c[2];
cin >> l;
REP(q, 2)
{
cin >> c[q];
pary[q][i] = PII(c[q], l);
srednia[q] += l * c[q];
}
}
}
inline bool majoryzuje()
{
LL i[2] = {0, 0}, s[2] = {0, 0};
while (i[0] < n)
{
LL dl = min(pary[0][i[0]].ND, pary[1][i[1]].ND);
REP(q, 2)
{
s[q] += dl * pary[q][i[q]].ST;
pary[q][i[q]].ND -= dl;
if (pary[q][i[q]].ND == 0)
i[q]++;
}
if (s[0] < s[1])
return false;
}
assert(i[0] == n);
assert(i[1] == n);
return true;
}
bool rozwiaz()
{
if (srednia[0] != srednia[1])
return false;
REP(q, 2)
sort(pary[q], pary[q] + n, greater<PII>());
return majoryzuje();
}
void zrob_test()
{
wyczysc_wszystko();
wczytaj_dane();
cout << (rozwiaz() ? "TAK" : "NIE") << '\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
LL t;
cin >> t;
while (t--)
zrob_test();
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 | //Autor: Mateusz Wasylkiewicz //Zawody: Potyczki Algorytmiczne 2019 //Strona: http://potyczki.mimuw.edu.pl/ //Zadanie: Herbata, runda 2B //Czas: O(t*n*log(n)) #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef vector<LL> VI; typedef pair<LL, LL> PII; #define FOR(x, a, b) for (LL x = (a); x <= (b); x++) #define FORD(x, a, b) for (LL x = (a); x >= (b); x--) #define REP(x, n) for (LL x = 0; x < (n); x++) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) (LL((x).size())) #define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); i++) #define PB push_back #define ST first #define ND second #define POKAZ(x) cerr << #x << " = " << (x) << '\n' const LL MAX = 100100; LL n, srednia[2]; PII pary[2][MAX]; void wyczysc_wszystko() { REP(q, 2) srednia[q] = 0; } void wczytaj_dane() { cin >> n; REP(i, n) { LL l, c[2]; cin >> l; REP(q, 2) { cin >> c[q]; pary[q][i] = PII(c[q], l); srednia[q] += l * c[q]; } } } inline bool majoryzuje() { LL i[2] = {0, 0}, s[2] = {0, 0}; while (i[0] < n) { LL dl = min(pary[0][i[0]].ND, pary[1][i[1]].ND); REP(q, 2) { s[q] += dl * pary[q][i[q]].ST; pary[q][i[q]].ND -= dl; if (pary[q][i[q]].ND == 0) i[q]++; } if (s[0] < s[1]) return false; } assert(i[0] == n); assert(i[1] == n); return true; } bool rozwiaz() { if (srednia[0] != srednia[1]) return false; REP(q, 2) sort(pary[q], pary[q] + n, greater<PII>()); return majoryzuje(); } void zrob_test() { wyczysc_wszystko(); wczytaj_dane(); cout << (rozwiaz() ? "TAK" : "NIE") << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); LL t; cin >> t; while (t--) zrob_test(); return 0; } |
English