/ SeriousOJ /

Record Detail

Memory Exceeded


  
# Status Time Cost Memory Cost
#1 Memory Exceeded ≥84ms ≥256.016 MiB
#2 Memory Exceeded ≥115ms ≥256.016 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using minheap = priority_queue<long long, vector<long long>, greater<long long>>;
typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key

#define ll long long
#define ld long double
#define MOD 1000000007
#define pie 2*(acos(0.0))
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define pb push_back
#define endl '\n'
#define lcm(a,b) (a*b)/(__gcd<ll>(a,b))
#define print(v) for(auto e:v) cout<<e<<" "; cout<<endl;
#define printp(v) for(auto e:v) cout<<e.first<<" "<<e.second<<endl;
#define srt(v) sort(v.begin(),v.end())
#define rsrt(v) sort(v.rbegin(),v.rend())
#define life_is_a_race ios::sync_with_stdio(false); cin.tie(nullptr);

const int N = 2e5 + 2, inf = 1000;
vector<int> sq;
int dp[450][N];

int rec(int i, int j) {
    if(j == 0) return 0;
    if(j < 0) return inf;
    if(i == sq.size()) return inf;
    if(sq[i] > j) return inf;

    auto& ok = dp[i][j];
    if(ok != -1) return ok;
    ok = rec(i + 1, j); // not take
    if(j - sq[i] >= 0) ok = min(ok, 1 + rec(i + 1, j - sq[i])); // take
    if(ok > inf) ok = inf;
    return ok;
}

void solve(int tc)
{
    //cout<<"Case "<<tc<<": ";
    int x;
    cin >> x;
    int ans = rec(0, x);
    if(ans < inf) cout << ans << endl;
    else cout << -1 << endl;
}

int main()
{
    life_is_a_race
    for(int i = 1; i < 450; i++) {
        sq.pb(i * i);
    }
    memset(dp, -1, sizeof dp);
    rec(0, 1e5);
    rec(0, 1e4);
    rec(0, 1e3);
    rec(0, 1e2);
    int t=1; 
    cin>>t;
    for(int i=1;i<=t;i++) solve(i);
}

Information

Submit By
Type
Submission
Problem
P1051 Square Sum
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-24 15:08:26
Judged At
2024-11-24 15:08:26
Judged By
Score
0
Total Time
≥115ms
Peak Memory
≥256.016 MiB