Advertisement
STANAANDREY

job greedy

Dec 15th, 2020
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6.  
  7. struct lucrare {
  8.   unsigned int h, p, cod;
  9. } v[1000];
  10.  
  11. void ord() {
  12.   for (int i = 0; i < n - 1; i++)
  13.     for (int j = i + 1; j < n; j++)
  14.       if (v[i].h > v[j].h)
  15.         swap(v[i], v[j]);
  16.       else if (v[i].h == v[j].h)
  17.         if (v[i].p < v[j].p)
  18.           swap(v[i], v[j]);
  19. }
  20.  
  21. void greedy() {
  22.   cout << v[0].cod << " ";
  23.   int s = 0;
  24.   for (int i = 1; i < n; i++)
  25.     if (v[i].h == v[i - 1].h)
  26.       s += v[i].p;
  27.     else
  28.       cout << v[i].cod << " ";
  29.   cout << endl << "suma penalizarilor " << s;
  30. }
  31.  
  32. void citire() {
  33.   cout << "n=";
  34.   cin >> n;
  35.   for (int i = 0; i < n; i++) {
  36.     cout << "Ora ";
  37.     cin >> v[i].h;
  38.     cout << "Penalizare ";
  39.     cin >> v[i].p;
  40.     v[i].cod = i + 1;
  41.   }
  42. }
  43.  
  44. int main() {
  45.   citire();
  46.   ord();
  47.   greedy();
  48.   return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement