Advertisement
agul

20121030_problem1

Oct 30th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. #define ATHLETES_MAX_NUMBER 200000
  6.  
  7. struct athlete {
  8.     int weight, strength, number;
  9. };
  10.  
  11. void swap(athlete * a, athlete * b) {
  12.     athlete t = *a;
  13.     *a = *b;
  14.     *b = t;
  15. }
  16.  
  17. int n, curWeight, curStrength, ans, result[ATHLETES_MAX_NUMBER], weightsSum;
  18. athlete athletes[ATHLETES_MAX_NUMBER];
  19.  
  20. void sort(int l, int r) {
  21.     int i = l, j = r, mediane = l + rand() % (r - l + 1), weightMediane = athletes[mediane].weight, strengthMediane = athletes[mediane].strength;
  22.     do {
  23.         while (athletes[i].weight < weightMediane || athletes[i].weight == weightMediane && athletes[i].strength < strengthMediane) ++i;
  24.         while (athletes[j].weight > weightMediane || athletes[j].weight == weightMediane && athletes[j].strength > strengthMediane) --j;
  25.         if (i <= j) {
  26.             swap(athletes + i, athletes + j);
  27.             ++i;
  28.             --j;
  29.         }
  30.     } while (i <= j);
  31.     if (l < j) sort(l, j);
  32.     if (i < r) sort(i, r);
  33. }
  34.  
  35. int main() {
  36.     srand(time(NULL));
  37.     scanf("%d", &n);
  38.     for (int i = 0; i < n; ++i) {
  39.         scanf("%d%d", &curWeight, &curStrength);
  40.         athletes[i].weight = curWeight;
  41.         athletes[i].strength = curStrength;
  42.         athletes[i].number = i + 1;
  43.     }
  44.     sort(0, n - 1);
  45.     weightsSum = 0;
  46.     for (int i = 0; i < n; ++i)
  47.         if (athletes[i].strength >= weightsSum) {
  48.             result[ans++] = athletes[i].number;
  49.             weightsSum += athletes[i].weight;
  50.         }
  51.     printf("%d\n", ans);
  52.     for (int i = ans - 1; i >= 0; --i)
  53.         printf("%d ", result[i]);
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement