Alex_tz307

Fundamente XI - 9 / 219

Sep 21st, 2020 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     ios_base::sync_with_stdio(false);
  8.     cin.tie(nullptr);
  9.     cout.tie(nullptr);
  10.     int N, Z, K;
  11.     cin >> N >> Z >> K;
  12.     vector < vector < int > > dp(N + 1, vector < int >(K + 1, INF)), t(N + 1, vector < int >(K + 1));
  13.     /* dp[i][j] = nr. minim de zile necesare pentru a avea i persoane
  14.                   si j perechi de persoane nascute in aceeasi zi */
  15.     // Exista sol <=> dp[N][K] <= Z
  16.     dp[0][0] = 0;
  17.     dp[1][0] = t[1][0] = 1;
  18.     for(int i = 2; i <= N; ++i)
  19.         for(int j = 1; j <= K; ++j)
  20.             for(int z = 1; z <= i; ++z)
  21.                 if(j - z * (z - 1) / 2 >= 0 && dp[i][j] > dp[i - z][j - z * (z - 1) / 2] + 1) {
  22.                     dp[i][j] = dp[i - z][j - z * (z - 1) / 2] + 1;
  23.                     t[i][j] = z;
  24.                 }
  25.     if(dp[N][K] > Z)
  26.         cout << "Nu exista solutie\n";
  27.     else {
  28.         vector < int > sol;
  29.         while(N > 0) {
  30.             int i = t[N][K];
  31.             for(int j = 1; j <= i; ++j)
  32.                 sol.emplace_back(dp[N][K]);
  33.             N -= i;
  34.             K -= i * (i - 1) / 2;
  35.         }
  36.         reverse(sol.begin(), sol.end());
  37.         for(int x : sol)
  38.             cout << x << ' ';
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment