The 2022 ICPC Asia Regionals Online Contest (II) B

B : Non-decreasing Array

You are given a non-decreasing array of integers a1​,a2​,…,an​. In one operation, when the current length of the array is m:

  • Firstly you can choose an index i(1<i<m) and delete ai​ (m decrease 1) or you can do nothing,
  • Secondly you can choose an index i(1<i<m) and change ai​ to any integer.

You should ensure that the array is non-decreasing after every delete or change.

Now you want to know that after operating k(1≤k≤n) times, when the current length of the array is m, what is the maximum value of ∑(i=2-m)​(ai​−ai_1​)^2.

You need to answer for each k(1≤k≤n), different queries are independent of each other.

输入格式:

The first line contains one integer n(3≤n≤100).

The second line contains n integers a1​,a2​,...,an​(−10^9≤ai​≤10^9).

输出格式:

Output n lines, each of which contains a single integer—the i-th number is for the answer of k=i.

输入样例:

5
1 2 3 4 5

输出样例:

10
16
16
16
16

代码长度限制

16 KB

时间限制

400 ms

内存限制

dp[i][k] 为前[1-i]中删除k个数,且a[1],a[i]不删除的最大权值

1次操作对应两次删除

#include<bits/stdc++.h>
using namespace std;
typedef double db;
#define int long long
const int N=110;
int a[N],n;
int dp[N][N];
int dfs(int id,int k)
{
    if (dp[id][k]!=-1)return dp[id][k];
    int mx=0;
    for(int i=1;i<id;i++)
    {
        //del[i+1,id-1]
        int cnt=(id-1)-(i+1)+1;//删除的个数
        if (cnt>k)continue;
        mx=max(mx,dfs(i,k-cnt)+(a[id]-a[i])*(a[id]-a[i]));
    }
    return dp[id][k]=mx;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
    }
    memset(dp,-1,sizeof dp);
    for(int i=1; i<=n; i++)
    {
        cout<<dfs(n,min(n-2,i*2))<<"\n";
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值