Advertisement
Soupborsh

3_problem

Jan 16th, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | Source Code | 0 0
  1. #include <algorithm>
  2. #include <climits>
  3. #include <cstdio>
  4. #include <vector>
  5.  
  6. #define N_PROBLEMS 6U
  7. #define FIND_N_PROBLEMS ((unsigned char)exer.back().size() - 1)
  8. #define I_PER 0U
  9.  
  10. struct problem {
  11.   unsigned int a = 0; // sum of scores or score
  12.   unsigned int b = 0; // id or number of submissions
  13. };
  14.  
  15. std::vector<std::vector<problem>> exer;
  16.  
  17. unsigned int my_sum;
  18.  
  19. inline bool comp_exer(std::vector<problem>& i, std::vector<problem>& j) {
  20.   return (i[I_PER].a < j[I_PER].a);
  21. }
  22.  
  23. inline bool comp_exer1(const problem& i, const problem& j) {
  24.   //if (i.a != j.a) {
  25.   //  return (i.a > j.a);
  26.   //} else {
  27.   //  return (i.b < j.b);
  28.   //}
  29.   return (i.a > j.a);
  30. }
  31.  
  32. inline unsigned int min_count_sum(void) {
  33.   unsigned int min_count = UINT_MAX;
  34.   problem sum;
  35.   for (unsigned char enabled = 1;
  36.        enabled < (1 << FIND_N_PROBLEMS); enabled++) {
  37.     for (unsigned int i = 0; i < N_PROBLEMS; i++) {
  38.       if ((enabled & (1 << i)) != 0) {
  39.         sum.a += exer.back()[i + 1].a;
  40.         sum.b += exer.back()[i + 1].b;
  41.       }
  42.     }
  43.     if ((exer.back()[I_PER].a - sum.a) <= my_sum) {
  44.       if (sum.b < min_count) {
  45.         min_count = sum.b;
  46.       }
  47.     }
  48.     sum.a = 0;
  49.     sum.b = 0;
  50.   }
  51.  
  52.   return min_count;
  53. }
  54.  
  55. int main(void) {
  56.   unsigned int n, q;
  57.   scanf("%u", &n);
  58.   scanf("%u", &q);
  59.  
  60.   exer.resize(n);
  61.  
  62.   for (unsigned int i = 0; i < n; i++) {
  63.     exer[i].resize(1 + N_PROBLEMS);
  64.     //exer[i][I_PER].a = 0; // score sum
  65.     //exer[i][I_PER].b = i; // id
  66.   }
  67.  
  68.   //  get input data
  69.   unsigned int id, score, ex;
  70.   char ex_c;
  71.   for (unsigned int i = 0; i < q; i++) {
  72.     scanf("%u", &id);
  73.     id--;
  74.     scanf(" %c", &ex_c);
  75.     ex = (ex_c - 'A') + 1;
  76.     scanf("%u", &score);
  77.  
  78.     if (exer[id][ex].a < score) {
  79.       exer[id][ex].a = score;
  80.     }
  81.     exer[id][ex].b++;
  82.   }
  83.  
  84.   for (unsigned int i = 0; i < n; i++) {
  85.     for (unsigned int j = 1; j <= N_PROBLEMS; j++) {
  86.       exer[i][I_PER].a += exer[i][j].a;
  87.     }
  88.   }
  89.  
  90.   my_sum = exer[0][I_PER].a;
  91.   unsigned int deleted = 0;
  92.  
  93.   // HACK: make first person's score to be zero to not influence the result
  94.   exer[0][I_PER].a = 0;
  95.   // better would be to pop it but popping in front is inefficient in vector.
  96.  
  97.   // ascending sort of persons based on score
  98.   for (unsigned int i = 0; i < n; i++) {
  99.     std::sort(exer[i].begin() + 1, exer[i].end(), comp_exer1);
  100.   }
  101.  
  102.   // descending sort of problems for each person
  103.   std::sort(exer.begin(), exer.end(), comp_exer);
  104.  
  105.   // remove problems with 0 score
  106.   for (unsigned int i = 0; i < n; i++) {
  107.     while (exer[i].back().a == 0) {
  108.       exer[i].pop_back();
  109.     }
  110.   }
  111.  
  112.   while (exer.back()[I_PER].a > my_sum) {
  113.     deleted += min_count_sum();
  114.     exer.pop_back();
  115.   }
  116.  
  117.   printf("%u\n", deleted);
  118.   return 0;
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement