Advertisement
Guest User

PE472

a guest
Jun 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <cstdio>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int f(int n) {
  8.     bool *seats = new bool[n];
  9.     int bestOutcome = 0;
  10.     int bestCount = 0;
  11.  
  12.     for (int i=0;i<n;i++) {
  13.         memset(seats,0,n);
  14.  
  15.         seats[i] = true;
  16.         int seated = 1;
  17.         while (1) {
  18.             int bestDistance = 0;
  19.             int bestSeat = -1;
  20.             for (int p=n-1;p>=0;p--) {
  21.                 if (seats[p]) continue;
  22.                 if (p > 0 && seats[p-1]) continue;
  23.                 if (p < n-1 && seats[p+1]) continue;
  24.  
  25.                 int distance = n+1;
  26.                 for (int q=0;q<n;q++) {
  27.                     if (seats[q]) distance = min(distance,abs(q-p));
  28.                 }
  29.  
  30.                 if (distance >= bestDistance) {
  31.                     bestDistance = distance;
  32.                     bestSeat = p;
  33.                 }
  34.  
  35.             }
  36.             if (!bestDistance) break;
  37.  
  38.             seated++;
  39.             seats[bestSeat] = true;
  40.         }
  41.  
  42.         if (seated > bestOutcome) {
  43.             bestOutcome = seated;
  44.             bestCount = 1;
  45.         } else if (seated == bestOutcome) {
  46.             bestCount++;
  47.         }
  48.     }
  49.  
  50.     delete[] seats;
  51.     return bestCount;
  52. }
  53.  
  54. int main() {
  55.     for (int n=1;n<1000;n++) printf("f(%d) = %d\n",n,f(n));
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement