Advertisement
Asif_Anwar

Timus-1073. Square Country

Apr 9th, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define pb push_back
  5. #define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  6. #define F first
  7. #define S second
  8. typedef long long ll;
  9. typedef vector< int > vi;
  10. typedef vector< ll > V;
  11. typedef map<int, int > mp;
  12. #define debug cout << -1 << endl;
  13. #define REP(i, a, b) for(int i=a; i<b; i++)
  14. #define pop pop_back
  15. #define sz(a) (int)a.size()
  16. const ll MOD = 1000000007;
  17. const int maxN = 2001;
  18.  
  19. int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
  20. int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
  21.  
  22. void solve()
  23. {
  24.     int x;
  25.     cin >> x;
  26.     vector< int > v;
  27.     int i = 1;
  28.     while(i*i<=x) {
  29.         v.pb(i*i);
  30.         //cout << i*i << endl;
  31.         i++;
  32.     }
  33.     int n = sz(v);
  34.     int dp[n+1][x+1];
  35.     for(int i=0; i<=n; i++) {
  36.         dp[i][0] = 0;
  37.         for(int j=1; j<=x; j++) {
  38.             dp[i][j] = INT_MAX;
  39.         }
  40.     }
  41.     for(int i=1; i<=n; i++) {
  42.         for(int j=1; j<=x; j++) {
  43.             if(v[i-1]<=j) {
  44.                 dp[i][j] = min(1+dp[i][j-v[i-1]],dp[i-1][j]);
  45.             }
  46.             else dp[i][j] = dp[i-1][j];
  47.         }
  48.     }
  49.     cout << dp[n][x] << endl;
  50.     return;
  51. }
  52.  
  53. int main()
  54. {
  55.     FastIO;
  56.     //freopen("tttt.in","r",stdin);
  57.     //freopen("tttt.out","w",stdout);
  58.     int t;
  59.     t = 1;
  60.     //cin >> t;
  61.     while(t--){
  62.         solve();
  63.     }
  64.     return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement