Advertisement
CR7CR7

GreedyApproach

Jun 7th, 2023
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int calculateMinimumLectures(int n, int L, int topics[])
  4. {
  5.     int lectureCount = 1;
  6.     int totalDuration = 0;
  7.  
  8.     for (int i = 0; i < n; i++)
  9.     {
  10.         if (totalDuration + topics[i] > L)
  11.         {
  12.             lectureCount++;
  13.             totalDuration = 0;
  14.         }
  15.  
  16.         totalDuration += topics[i];
  17.     }
  18.  
  19.     return lectureCount;
  20. }
  21.  
  22. int calculateTotalDI(int lectureCount, int topics[], int C)
  23. {
  24.     int totalDI = 0;
  25.  
  26.     for (int i = 0; i < lectureCount; i++)
  27.     {
  28.         int freeTime = topics[i] > 10 ? topics[i] - 10 : 0;
  29.         int DI = freeTime > 0 ? (freeTime - 10) * (freeTime - 10) : 0;
  30.         totalDI += DI;
  31.     }
  32.  
  33.     if (lectureCount > 1)
  34.         totalDI += C;
  35.  
  36.     return totalDI;
  37. }
  38.  
  39. int main()
  40. {
  41.     int caseNumber = 1;
  42.  
  43.     while (1)
  44.     {
  45.         int n, L, C;
  46.         scanf("%d", &n);
  47.  
  48.         if (n == 0)
  49.             break;
  50.  
  51.         printf("Case %d:\n", caseNumber);
  52.  
  53.         scanf("%d %d", &L, &C);
  54.  
  55.         int topics[n];
  56.         for (int i = 0; i < n; i++)
  57.         {
  58.             scanf("%d", &topics[i]);
  59.         }
  60.  
  61.         int lectureCount = calculateMinimumLectures(n, L, topics);
  62.         int totalDI = calculateTotalDI(lectureCount, topics, C);
  63.  
  64.         printf("Minimum number of lectures: %d\n", lectureCount);
  65.         printf("Total dissatisfaction index: %d\n\n", totalDI);
  66.  
  67.         caseNumber++;
  68.     }
  69.  
  70.     return 0;
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement