月赛A题求调
  • 板块学术版
  • 楼主梦回江南
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/10/3 22:20
  • 上次更新2023/10/27 08:56:47
查看原帖
月赛A题求调
492676
梦回江南楼主2022/10/3 22:20

题目link

算法:区间dp

#include <memory.h>
#include <iomanip>
#include <iostream>
#include <algorithm>

#define LL long long
#define endl '\n'
#define ref(i, a, b, p) for (int i = a; i <= b; i += p)
#define gef(i, a, b, p) for (int i = a; i >= b; i -= p)
using namespace std;

int n, q;
long long a[200005], f[200][200];
const long long maximum = 1 << 30;

void dp(int l, int r)
{
    memset(f, 0, sizeof(f));
    int len = r - l + 1;
    if (l > r)
    {
        cout << 1 << endl;
        return ;
    }
    if (l == r)
    {
        if (a[l] > maximum)
            cout << "Too large" << endl;
        else if (a[l] < 1)
            cout << '1' << endl;
        else  
            cout << a[l] << endl;
        return ;
    }
    int cnt = 0;
    ref (i, l, r, 1)
        ++cnt, f[cnt][cnt] = a[i];
    LL ans = 0ll;
    ref (p, 1, (cnt - 1), 1)
    {
        ref (i, 1, (cnt - p), 1)
        {
            int j = i + p;
            ref (k, i, (j - 1), 1)
            {
                f[i][j] = max(f[i][j], f[i][k] * f[k + 1][j]);
                ans = max(ans, f[i][j]);
                // ans = max(ans, f[i][k] * f[k + 1][j]);
                if (ans > maximum)
                {
                    cout << "Too large" << endl;
                    return ;
                }
            }
        }
    }
    if (ans < 1)
        cout << '1' << endl;
    else  
        cout << ans << endl;
    return ;
}

void op(int opr, int l, int r)
{
    if (opr == 1)
        a[l] = r;
    else 
        dp(l, r);
    return ;
}

void work()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    // freopen("yl.in", "r", stdin);
    // freopen("yl.out", "w", stdout);
    cin >> n >> q;
    ref (i, 1, n, 1)
        cin >> a[i];
    ref (i, 1, q, 1)
    {
        int opr = 0, l = 0, r = 0;
        memset(f, 0, sizeof(f));
        cin >> opr >> l >> r;
        op(opr, l, r);
    }

    return ;
}

int main()
{
    work();

    return 0;
}

/*
30 2
2 2 2 2 -2 2 2 2 4 2 2 -4 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 6 25
1 17 2
2 1 20

*/
2022/10/3 22:20
加载中...