sdut2169:Sequence(dp)

题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2169

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
ll n,m;
ll sum;
ll w[1010],num[1010],dp[1010];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        sum=0;
        scanf("%lld%lld",&n,&m);
        memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++)
        {
           scanf("%d",&w[i]);
           num[i]=num[i-1]+w[i];
           dp[i]=num[i]*num[i];
           sum+=w[i];
        }
        if(m==1)
        {
            sum=sum*sum;
            printf("%lld\n",sum);
            continue;
        }
        bool F=false;
        for(int j=2;j<=m;j++)
        {
            for(int i=n-m+j;i>=j;i--)
            {
                for(int k=j-1;k<i;k++)
                {
                    dp[i]=min(dp[i],(dp[k]+(num[i]-num[k])*(num[i]-num[k])));
                     if(j==m&&k==i-1)
                     {
                            F=true;
                            break;
                     }
                }
                if(F) break;
            }
        }
        printf("%lld\n",dp[n]);
    }
    return 0;
}
 
时间: 2024-10-31 20:31:11

sdut2169:Sequence(dp)的相关文章

poj 2081 Recaman&#39;s Sequence (dp)

Recaman's Sequence Time Limit: 3000MS   Memory Limit: 60000K Total Submissions: 22566   Accepted: 9697 Description The Recaman's sequence is defined by a0 = 0 ; for m > 0, am = am−1 − m if the rsulting am is positive and not already in the sequence,

Codeforces 13C Sequence --DP+离散化

题意:给出一个 n (1 <= n <= 5000)个数的序列 .每个操作可以把 n 个数中的某一个加1 或 减 1.问使这个序列变成非递减的操作数最少是多少 解法:定义dp[i][j]为将前i个数变为以j为结尾的非递减序列的最少操作次数. 则有: dp[i][j] = min(dp[i][j], min(dp[i][k]) + Cost(原来第i个位置上的数转换到j))  (1 <= k <= j) 即前i个数以j结尾的状态可以由前i-1个数以小于等于j的k结尾的状态转移过来,取

hdu6078 Wavel Sequence dp+二维树状数组

//#pragma comment(linker, "/STACK:102400000,102400000") /** 题目:hdu6078 Wavel Sequence 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6078 题意:给定a序列和b序列.从a中取一个子序列x(数的位置顺序保持不变),子序列每个数满足a1<a2>a3<a4>a5<a6... 波浪形 从b中取相同长度的子序列y,也满足波浪形. 如果x

POJ1699:Best Sequence(DP)

Description The twenty-first century is a biology-technology developing century. One of the most attractive and challenging tasks is on the gene project, especially on gene sorting program. Recently we know that a gene is made of DNA. The nucleotide

poj-2478 Farey Sequence(dp,欧拉函数)

题目链接: Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14230   Accepted: 5624 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd

poj 1141 Brackets Sequence dp

 Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, then AB is a regu

UVa 10534 Wavio Sequence ( DP 二分 最长递增子序列 )

题意  求一个序列a某一位的最长递增序列(lis)和最长递减序列(lds)中最小值的最大值 开始直接用DP写了   然后就超时了  后来看到别人说要用二分把时间复杂度优化到O(n*logn)   果然如此   用一个栈s保存长度为i的LIS的最小尾部s[i]  top为栈顶即当前LIS的长度  初始s[1]=a[1]  top=1 遍历整个序列  当a[i]>s[top]时  a[i]入栈 in[i]=top   否则 在栈中查找(二分)第一个大于等于a[i]的下标pos  并替换  这样就增加

UVA 348 &amp; ZOJ 1276 Optimal Array Multiplication Sequence(dp , 矩阵链相乘问题)

Optimal Array Multiplication Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Given two arrays A and B, we can determine the array C = AB using the standard definition of matrix multiplication: The number of

2017多校第4场 HDU 6078 Wavel Sequence DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6078 题意:求两个序列的公共波形子序列的个数. 解法: 类似于最长公共上升子序列,对于每个i,只考虑存在j使得a[i]==b[j]的情况. dp[i][j][0]表示以a[i]和b[j]为公共序列结尾且为波谷的情况总和. dp[i][j][1]则表示波峰的情况总和. S[i][j][0]表示sum(dp[k][j][0] | 1<=k<=j-1). S[i][j][1]则表示sum(dp[k][j