Linkin_Park

BruteForce Task

Mar 19th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. /*
  2. B. Shower Line
  3. time limit per test1 second
  4. memory limit per test256 megabytes
  5. inputstandard input
  6. outputstandard output
  7. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks.
  8.  
  9. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower.
  10.  
  11. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one.
  12.  
  13. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower.
  14.  
  15. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower.
  16.  
  17. Input
  18. The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i.
  19.  
  20. Assume that the students are numbered from 1 to 5.
  21.  
  22. Output
  23. Print a single integer — the maximum possible total happiness of the students.
  24. */
  25.  
  26. #include <iostream>
  27. #include <cstdio>
  28. #include <set>
  29. #include <map>
  30. #include <iterator>
  31. #include <vector>
  32. #include <iomanip>
  33. #include <algorithm>
  34. #include <string>
  35. #include <cmath>
  36.  
  37. using namespace std;
  38.  
  39. typedef long long int64;
  40.  
  41. const int64 N = 5;
  42. vector <int64> st(0);
  43. vector <bool> use(N);
  44. vector <vector<int64>> students(N, vector<int64>(N));
  45. int64 maxSum;
  46.  
  47. bool check(int64 currentSum, int64 maxSum)
  48. {
  49.     if (currentSum > maxSum)
  50.     {
  51.         return true;
  52.     }
  53.    
  54.     return false;
  55. }
  56.  
  57. int64 calculateCurrentSum()
  58. {
  59.     int64 currentSum = (students[st[2]][st[3]] + students[st[3]][st[2]] +
  60.         students[st[3]][st[4]] + students[st[4]][st[3]]) * 2 +
  61.         students[st[0]][st[1]] + students[st[1]][st[0]] +
  62.         students[st[1]][st[2]] + students[st[2]][st[1]];
  63.     return currentSum;
  64. }
  65.  
  66. void bruteForce()
  67. {
  68.     if (st.size() == N)
  69.     {
  70.         int64 currentSum = calculateCurrentSum();
  71.  
  72.         if (currentSum > maxSum)
  73.         {
  74.             maxSum = currentSum;
  75.         }
  76.  
  77.         return;
  78.     }
  79.  
  80.     for (int i = 0; i < N; i++)
  81.     {
  82.         if (!use[i])
  83.         {
  84.             use[i] = true;
  85.             st.push_back(i);
  86.             bruteForce();
  87.             st.pop_back();
  88.             use[i] = false;
  89.         }
  90.     }
  91.  
  92. }
  93.  
  94. int main()
  95. {
  96.     freopen("input.txt", "r", stdin);
  97.  
  98.     for (int i = 0; i < N; i++)
  99.     {
  100.         for (int j = 0; j < N; j++)
  101.         {
  102.             cin >> students[i][j];
  103.         }
  104.     }
  105.  
  106.     maxSum = 0;
  107.  
  108.     bruteForce();
  109.  
  110.     cout << maxSum << endl;
  111.  
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment