#include <algorithm> #include <cstdio> using namespace std; #define MAX 100009 #define MP make_pair #define F first #define S second typedef long long typ; struct wal{ typ dol, gora, index; }; wal tab[MAX]; typ wartosc(wal a){ return a.gora - a.dol; } bool fun(wal a, wal b){ typ pa, pb; pa = wartosc(a); pb = wartosc(b); if(pa * pb <= 0)return pa > pb; if(pa > 0)return a.dol < b.dol; if(pa < 0)return a.gora > b.gora; } int main (){ long long n, z, koniec; scanf("%lld%lld", &n, &z); koniec = z; for(int i=1; i<=n; i++){ scanf("%lld%lld", &tab[i].dol, &tab[i].gora); koniec += tab[i].gora - tab[i].dol; tab[i].index = i; } if(koniec < 0){ printf("NIE"); return 0; } sort(tab + 1, tab + n + 1, fun); //for(int i=1; i<=n; i++)printf("%lld %lld\n", tab[i].dol, tab[i].gora); int poz = 1; for(poz; poz <=n; poz++){ if(wartosc(tab[poz]) < 0)break; z -= tab[poz].dol; if(z <= 0){ printf("NIE"); return 0; } z += tab[poz].gora; } for(int i=n; i>=poz; i--){ koniec -= tab[i].gora; if(koniec <= 0){ printf("NIE"); return 0; } koniec += tab[i].dol; } printf("TAK\n"); for(int i=1; i<=n; i++)printf("%d ", tab[i].index); }
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 | #include <algorithm> #include <cstdio> using namespace std; #define MAX 100009 #define MP make_pair #define F first #define S second typedef long long typ; struct wal{ typ dol, gora, index; }; wal tab[MAX]; typ wartosc(wal a){ return a.gora - a.dol; } bool fun(wal a, wal b){ typ pa, pb; pa = wartosc(a); pb = wartosc(b); if(pa * pb <= 0)return pa > pb; if(pa > 0)return a.dol < b.dol; if(pa < 0)return a.gora > b.gora; } int main (){ long long n, z, koniec; scanf("%lld%lld", &n, &z); koniec = z; for(int i=1; i<=n; i++){ scanf("%lld%lld", &tab[i].dol, &tab[i].gora); koniec += tab[i].gora - tab[i].dol; tab[i].index = i; } if(koniec < 0){ printf("NIE"); return 0; } sort(tab + 1, tab + n + 1, fun); //for(int i=1; i<=n; i++)printf("%lld %lld\n", tab[i].dol, tab[i].gora); int poz = 1; for(poz; poz <=n; poz++){ if(wartosc(tab[poz]) < 0)break; z -= tab[poz].dol; if(z <= 0){ printf("NIE"); return 0; } z += tab[poz].gora; } for(int i=n; i>=poz; i--){ koniec -= tab[i].gora; if(koniec <= 0){ printf("NIE"); return 0; } koniec += tab[i].dol; } printf("TAK\n"); for(int i=1; i<=n; i++)printf("%d ", tab[i].index); } |