Loading...
Submission
# When Author Problem Language CPU Memory
20816 2024-06-08 23:00:01 AHAMMED_99 Warrior of Exoland C 2 ms 2520 kb Wrong Answer - 1
Test Cases
# CPU Memory Points
1 2 ms 2520 kb 0 Wrong Answer
2 0 ms 0 kb 0 Skipped
3 0 ms 0 kb 0 Skipped
4 0 ms 0 kb 0 Skipped
5 0 ms 0 kb 0 Skipped
6 0 ms 0 kb 0 Skipped
7 0 ms 0 kb 0 Skipped
8 0 ms 0 kb 0 Skipped
9 0 ms 0 kb 0 Skipped
10 0 ms 0 kb 0 Skipped
11 0 ms 0 kb 0 Skipped
Source Code
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // Structure to represent a warrior
  5. typedef struct {
  6. int x;
  7. int y;
  8. } Warrior;
  9.  
  10. // Function to compare two elements for sorting
  11. int compare(const void *a, const void *b) {
  12. return (*(int *)a - *(int *)b);
  13. }
  14.  
  15. // Function to count the frequency of numbers and return the sorted list of unique numbers
  16. int countAndSortFrequencies(Warrior *warriors, int N, int *frequency, int *uniqueNumbers) {
  17. int maxVal = 400000;
  18. for (int i = 0; i < N; i++) {
  19. frequency[warriors[i].x]++;
  20. frequency[warriors[i].y]++;
  21. }
  22.  
  23. int uniqueCount = 0;
  24. for (int i = 1; i <= maxVal; i++) {
  25. if (frequency[i] > 0) {
  26. uniqueNumbers[uniqueCount++] = i;
  27. }
  28. }
  29.  
  30. qsort(uniqueNumbers, uniqueCount, sizeof(int), compare);
  31. return uniqueCount;
  32. }
  33.  
  34. // Function to find the maximum number of warriors that can be selected
  35. int maxWarriors(int N, Warrior *warriors) {
  36. int maxVal = 400000;
  37. int *frequency = (int *)calloc(maxVal + 1, sizeof(int));
  38. int *uniqueNumbers = (int *)malloc((maxVal + 1) * sizeof(int));
  39. int uniqueCount = countAndSortFrequencies(warriors, N, frequency, uniqueNumbers);
  40.  
  41. int *selectedIdentifiers = (int *)calloc(maxVal + 1, sizeof(int));
  42. int selectedWarriorsCount = 0;
  43.  
  44. for (int i = 0; i < uniqueCount; i++) {
  45. int number = uniqueNumbers[i];
  46. if (!selectedIdentifiers[number]) {
  47. for (int j = 0; j < N; j++) {
  48. if ((warriors[j].x == number || warriors[j].y == number) &&
  49. !selectedIdentifiers[warriors[j].x] && !selectedIdentifiers[warriors[j].y]) {
  50. selectedIdentifiers[warriors[j].x] = 1;
  51. selectedIdentifiers[warriors[j].y] = 1;
  52. selectedWarriorsCount++;
  53. if (selectedWarriorsCount == N) {
  54. free(frequency);
  55. free(uniqueNumbers);
  56. free(selectedIdentifiers);
  57. return selectedWarriorsCount;
  58. }
  59. }
  60. }
  61. }
  62. }
  63.  
  64. free(frequency);
  65. free(uniqueNumbers);
  66. free(selectedIdentifiers);
  67. return selectedWarriorsCount;
  68. }
  69.  
  70. int main() {
  71. int N = 20;
  72. Warrior warriors[] = {
  73. {6, 19}, {8, 20}, {16, 8}, {3, 20}, {9, 13}, {10, 11},
  74. {8, 15}, {20, 11}, {18, 17}, {8, 14}, {15, 16}, {20, 1},
  75. {5, 2}, {7, 11}, {14, 7}, {4, 11}, {14, 15}, {20, 5},
  76. {15, 1}, {12, 1}
  77. };
  78. printf("%d\n", maxWarriors(N, warriors)); // Output: 17
  79. return 0;
  80. }
  81.