#include <stdio.h> //#include <cstdlib> struct enemy { long int damage; long int cure; long int health_income; bool defeated; }; void quickSort(enemy arr[], int left, int right) { long int i = left, j = right; enemy tmp; long int pivot = arr[(left + right) / 2].damage; /* partition */ while (i <= j) { while (arr[i].damage < pivot) i++; while (arr[j].damage > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } } if (left < j) quickSort(arr, left, j); if (i < right) quickSort(arr, i, right); } int main() { long int enemies_amount = 0; long int life_amount = 0; //FILE * pFile; //pFile = fopen ("invalid.txt","r"); //fscanf (pFile, "%d", &how_many_times); scanf ("%ld",&enemies_amount); scanf ("%ld",&life_amount); //fscanf (pFile,"%ld",&enemies_amount); //fscanf (pFile,"%ld",&life_amount); enemy *enemies = new enemy[enemies_amount]; long int *enemies_order = new long int[enemies_amount]; long int enemies_iterator = 0; for(int how_many_iterator = 0; how_many_iterator < enemies_amount; how_many_iterator++) { scanf ("%ld",&(enemies[how_many_iterator].damage)); scanf ("%ld",&(enemies[how_many_iterator].cure)); //fscanf (pFile,"%ld",&(enemies[how_many_iterator].damage)); //fscanf (pFile,"%ld",&(enemies[how_many_iterator].cure)); enemies[how_many_iterator].health_income = enemies[how_many_iterator].cure - enemies[how_many_iterator].damage; enemies[how_many_iterator].defeated = false; } //bubble sort by damage // szybsze sortowanie? quickSort(enemies, 0, enemies_amount - 1); /* for(int i = 0; i < enemies_amount; i++) { for (int j = 0; j < enemies_amount-1; j++) { if (enemies[j].damage > enemies[j+1].damage) { enemy temp = enemies[j]; enemies[j] = enemies[j+1]; enemies[j+1] = temp; } } } for(int i = 0; i < enemies_amount; i++) { printf("%d ", enemies[i].damage); } */ for(int how_many_iterator = 0; how_many_iterator < enemies_amount; how_many_iterator++) { if(enemies[how_many_iterator].health_income >= 0) { if(life_amount <= enemies[how_many_iterator].damage) { printf("NIE"); return 0; } else { life_amount += enemies[how_many_iterator].health_income; enemies[how_many_iterator].defeated = true; enemies_order[enemies_iterator] = how_many_iterator + 1; enemies_iterator++; } } } for(int how_many_iterator = enemies_amount - 1; how_many_iterator >= 0; how_many_iterator--) { if(!enemies[how_many_iterator].defeated) { if(life_amount <= enemies[how_many_iterator].damage) { printf("NIE"); //system("pause"); return 0; } else { life_amount += enemies[how_many_iterator].health_income; enemies[how_many_iterator].defeated = true; enemies_order[enemies_iterator] = how_many_iterator + 1; enemies_iterator++; } } } printf("TAK\n"); for(int how_many_iterator = 0; how_many_iterator < enemies_amount; how_many_iterator++) { printf("%d ", enemies_order[how_many_iterator]); } //fclose (pFile); delete[] enemies; delete[] enemies_order; //system("pause"); 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 | #include <stdio.h> //#include <cstdlib> struct enemy { long int damage; long int cure; long int health_income; bool defeated; }; void quickSort(enemy arr[], int left, int right) { long int i = left, j = right; enemy tmp; long int pivot = arr[(left + right) / 2].damage; /* partition */ while (i <= j) { while (arr[i].damage < pivot) i++; while (arr[j].damage > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } } if (left < j) quickSort(arr, left, j); if (i < right) quickSort(arr, i, right); } int main() { long int enemies_amount = 0; long int life_amount = 0; //FILE * pFile; //pFile = fopen ("invalid.txt","r"); //fscanf (pFile, "%d", &how_many_times); scanf ("%ld",&enemies_amount); scanf ("%ld",&life_amount); //fscanf (pFile,"%ld",&enemies_amount); //fscanf (pFile,"%ld",&life_amount); enemy *enemies = new enemy[enemies_amount]; long int *enemies_order = new long int[enemies_amount]; long int enemies_iterator = 0; for(int how_many_iterator = 0; how_many_iterator < enemies_amount; how_many_iterator++) { scanf ("%ld",&(enemies[how_many_iterator].damage)); scanf ("%ld",&(enemies[how_many_iterator].cure)); //fscanf (pFile,"%ld",&(enemies[how_many_iterator].damage)); //fscanf (pFile,"%ld",&(enemies[how_many_iterator].cure)); enemies[how_many_iterator].health_income = enemies[how_many_iterator].cure - enemies[how_many_iterator].damage; enemies[how_many_iterator].defeated = false; } //bubble sort by damage // szybsze sortowanie? quickSort(enemies, 0, enemies_amount - 1); /* for(int i = 0; i < enemies_amount; i++) { for (int j = 0; j < enemies_amount-1; j++) { if (enemies[j].damage > enemies[j+1].damage) { enemy temp = enemies[j]; enemies[j] = enemies[j+1]; enemies[j+1] = temp; } } } for(int i = 0; i < enemies_amount; i++) { printf("%d ", enemies[i].damage); } */ for(int how_many_iterator = 0; how_many_iterator < enemies_amount; how_many_iterator++) { if(enemies[how_many_iterator].health_income >= 0) { if(life_amount <= enemies[how_many_iterator].damage) { printf("NIE"); return 0; } else { life_amount += enemies[how_many_iterator].health_income; enemies[how_many_iterator].defeated = true; enemies_order[enemies_iterator] = how_many_iterator + 1; enemies_iterator++; } } } for(int how_many_iterator = enemies_amount - 1; how_many_iterator >= 0; how_many_iterator--) { if(!enemies[how_many_iterator].defeated) { if(life_amount <= enemies[how_many_iterator].damage) { printf("NIE"); //system("pause"); return 0; } else { life_amount += enemies[how_many_iterator].health_income; enemies[how_many_iterator].defeated = true; enemies_order[enemies_iterator] = how_many_iterator + 1; enemies_iterator++; } } } printf("TAK\n"); for(int how_many_iterator = 0; how_many_iterator < enemies_amount; how_many_iterator++) { printf("%d ", enemies_order[how_many_iterator]); } //fclose (pFile); delete[] enemies; delete[] enemies_order; //system("pause"); return 0; } |