Submission #1351011

#TimeUsernameProblemLanguageResultExecution timeMemory
1351011dex111222333444555Money (IZhO17_money)C++20
0 / 100
0 ms1096 KiB
#include <bits/stdc++.h>
#define all(v) begin(v), end(v)
#define dbg(x) "[" #x " = " << x << "]"
#define ii pair<int, long long>
#define fi first
#define se second
#define siz(v) (int)(v).size()
#define lli pair<long long, int>
#define BIT(x, i) (((x) >> (i)) & 1)

bool M1;

using namespace std;

const int infINT = 1e9 + 1323, mod = 1e18 + 7;
const long long inf = 1e18 + 123123;

template<class X, class Y> bool minimize(X &x, const Y &y){return x > y ? x = y, 1: 0;}
template<class X, class Y> bool maximize(X &x, const Y &y){return x < y ? x = y, 1: 0;}

void add(long long &a, const long long &b){
    a += b;
    if (a >= mod) a -= mod;
}

void sub(long long &a, const long long &b){
    a -= b;
    if (a < 0) a += mod;
}

long long mul(const long long &a, const long long &b){
    return 1LL * a * 1LL * b % mod;
}

const int MAXN = 1e6 + 6;

int numVal, val[MAXN], pos[MAXN], sum;
long long f[305][305];

bool cmp(const int &a, const int &b){
    if (val[a] != val[b]) return val[a] < val[b];
    return a < b;
}

void input(){
    cin >> numVal;
    for(int i = 1; i <= numVal; i++) cin >> val[i], pos[i] = i;
    sort(pos + 1, pos + 1 + numVal, cmp);

}

namespace sub3{
    bool checkSub(){
        return 1;
    }

    void solve(){
        memset(f, 0x3f, sizeof f);
        for(int i = 1; i <= numVal + 1; i++){
            f[i][i - 1] = f[i][i] = 1;
        }


        for(int len = 2; len <= numVal; len++){
            for(int i = 1; i + len - 1 <= numVal; i++){
                int j = i + len - 1;
                for(int k = i; k < j; k++){
                    minimize(f[i][j], f[i][k] + f[k + 1][j]);
                }
                int l = i;
                while(l < j && pos[l + 1] == pos[l] + 1) l++;
                int r = j;
                while(r > i && pos[r - 1] == pos[r] - 1) r--;
                if (l < r) minimize(f[i][j], f[l + 1][r - 1] + 1);
                else f[i][j] = 1;
            }
        }
        cout << f[1][numVal] << '\n';
    }
}

bool M2;
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define task "MONEY"
    if (fopen(task".in", "r")){
        freopen(task".in", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    input();
    if (sub3::checkSub()) sub3::solve();
    cerr << (&M2 - &M1) / 1048576 << " mb\n";
    cerr << (1.0 * clock()) / CLOCKS_PER_SEC << ".s\n";
}

Compilation message (stderr)

money.cpp:15:43: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   15 | const int infINT = 1e9 + 1323, mod = 1e18 + 7;
      |                                      ~~~~~^~~
money.cpp: In function 'int main()':
money.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |         freopen(task".in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
money.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...