BZOJ4953 Wf2017Posterize(动态规划)

  设f[i][j]为前i种强度选了j种且其中第i种选时前i个的最小误差。转移枚举上个选啥前缀和优化即可。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 256
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
int n,m,a[N];
ll s[N][N],f[N][N];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("bzoj4953.in","r",stdin);
    freopen("bzoj4953.out","w",stdout);
    const char LL[]="%I64d\n";
#else
    const char LL[]="%lld\n";
#endif
    n=read(),m=read();
    for (int i=1;i<=n;i++)
    {
        int x=read(),y=read();
        a[x]=y;
    }
    n=255;
    for (int i=0;i<=n;i++)
    {
        s[i][0]=1ll*a[0]*i*i;
        for (int j=1;j<=n;j++)
        s[i][j]=s[i][j-1]+1ll*a[j]*(i-j)*(i-j);
    }
    memset(f,42,sizeof(f));
    for (int i=0;i<=n;i++) f[i][1]=s[i][i];
    for (int i=2;i<=m;i++)
        for (int x=i-1;x<=n;x++)
            for (int y=i-2;y<x;y++)
            f[x][i]=min(f[x][i],f[y][i-1]+s[y][x+y>>1]-s[y][y]+s[x][x]-s[x][(x+y>>1)]);
    for (int i=0;i<n;i++) f[n][m]=min(f[n][m],f[i][m]+s[i][n]-s[i][i]);
    cout<<f[n][m];
    return 0;
}

 

posted @ 2018-12-02 13:56  Gloid  阅读(205)  评论(0编辑  收藏  举报