#include<iostream> using namespace std; void quicksort(int* d_up, int* a_up, int* i_up, int first, int last) { int i = first-1; int temp, j; int pivot = d_up[last]; for(j=first; j<last; ++j) { if(d_up[j]<pivot) { ++i; temp = d_up[i]; d_up[i] = d_up[j]; d_up[j] = temp; temp = a_up[i]; a_up[i] = a_up[j]; a_up[j] = temp; temp = i_up[i]; i_up[i] = i_up[j]; i_up[j] = temp; } } if(i<first) { temp = d_up[first]; d_up[first]=d_up[last]; d_up[last]=temp; temp = a_up[first]; a_up[first]=a_up[last]; a_up[last]=temp; temp = i_up[first]; i_up[first]=i_up[last]; i_up[last]=temp; ++i; } if (first<i) quicksort(d_up, a_up, i_up, first, i); if (last>i+1) quicksort(d_up, a_up, i_up, i+1, last); } int main(int argc, char** argv) { int n; long long z; int d, a; int * d_up; int * a_up; int * i_up; int * d_down; int * a_down; int * i_down; int up; int down; long long up_sum = 0; long long down_sum = 0; int * order; int i_order; bool yes = true; cin >> n; cin >> z; d_up = new int[n]; a_up = new int[n]; i_up = new int[n]; d_down = new int[n]; a_down = new int[n]; i_down = new int[n]; order = new int[n]; up=0; down=0; i_order = 0; for(int i=1; i<=n; ++i) { cin >> d; cin >> a; if(a >= d) { if(d < z) { order[i_order] = i; i_order ++; z += a-d; } else { up_sum += a-d; d_up[up] = d; a_up[up] = a; i_up[up] = i; up++; } } else { down_sum += a-d; d_down[down]=d; a_down[down]=a; i_down[down]=i; down++; } } if(z+up_sum+down_sum <= 0) { yes = false; } else { if(up>1) quicksort(d_up,a_up,i_up,0,up-1); for(int i=0;i<up;++i) { if(d_up[i] < z) { order[i_order] = i_up[i]; i_order++; z += a_up[i] - d_up[i]; } else { yes = false; break; } } if(yes) { if(down>1) quicksort(d_down,a_down,i_down,0,down-1); for(int i=down-1;i>=0;--i) { if(d_down[i] < z) { order[i_order] = i_down[i]; i_order++; z += a_down[i] - d_down[i]; } else { yes = false; break; } } } } if(yes) { cout << "TAK" << endl; for(int i=0; i<i_order; ++i) { cout << order[i] << " "; } } else { cout << "NIE"; } delete [] d_up; delete [] a_up; delete [] i_up; delete [] d_down; delete [] a_down; delete [] i_down; delete [] order; 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 | #include<iostream> using namespace std; void quicksort(int* d_up, int* a_up, int* i_up, int first, int last) { int i = first-1; int temp, j; int pivot = d_up[last]; for(j=first; j<last; ++j) { if(d_up[j]<pivot) { ++i; temp = d_up[i]; d_up[i] = d_up[j]; d_up[j] = temp; temp = a_up[i]; a_up[i] = a_up[j]; a_up[j] = temp; temp = i_up[i]; i_up[i] = i_up[j]; i_up[j] = temp; } } if(i<first) { temp = d_up[first]; d_up[first]=d_up[last]; d_up[last]=temp; temp = a_up[first]; a_up[first]=a_up[last]; a_up[last]=temp; temp = i_up[first]; i_up[first]=i_up[last]; i_up[last]=temp; ++i; } if (first<i) quicksort(d_up, a_up, i_up, first, i); if (last>i+1) quicksort(d_up, a_up, i_up, i+1, last); } int main(int argc, char** argv) { int n; long long z; int d, a; int * d_up; int * a_up; int * i_up; int * d_down; int * a_down; int * i_down; int up; int down; long long up_sum = 0; long long down_sum = 0; int * order; int i_order; bool yes = true; cin >> n; cin >> z; d_up = new int[n]; a_up = new int[n]; i_up = new int[n]; d_down = new int[n]; a_down = new int[n]; i_down = new int[n]; order = new int[n]; up=0; down=0; i_order = 0; for(int i=1; i<=n; ++i) { cin >> d; cin >> a; if(a >= d) { if(d < z) { order[i_order] = i; i_order ++; z += a-d; } else { up_sum += a-d; d_up[up] = d; a_up[up] = a; i_up[up] = i; up++; } } else { down_sum += a-d; d_down[down]=d; a_down[down]=a; i_down[down]=i; down++; } } if(z+up_sum+down_sum <= 0) { yes = false; } else { if(up>1) quicksort(d_up,a_up,i_up,0,up-1); for(int i=0;i<up;++i) { if(d_up[i] < z) { order[i_order] = i_up[i]; i_order++; z += a_up[i] - d_up[i]; } else { yes = false; break; } } if(yes) { if(down>1) quicksort(d_down,a_down,i_down,0,down-1); for(int i=down-1;i>=0;--i) { if(d_down[i] < z) { order[i_order] = i_down[i]; i_order++; z += a_down[i] - d_down[i]; } else { yes = false; break; } } } } if(yes) { cout << "TAK" << endl; for(int i=0; i<i_order; ++i) { cout << order[i] << " "; } } else { cout << "NIE"; } delete [] d_up; delete [] a_up; delete [] i_up; delete [] d_down; delete [] a_down; delete [] i_down; delete [] order; return 0; } |