SDOI2016 R1 day2 T3 征途 斜率优化DP

我们写出式子 fi,j=fk,j-1+(si-sk)^2,然后固定一个j,哗哗就出来了

又是今天的T3,然而傻逼到不会写写了个错的70分滚粗

那么傻逼的题都只有230,这是滚大粗的节奏啊

/* ***********************************************
Author        :BPM136
Created Time  :2016-4-25 10:20:53
File Name     :C.cpp
************************************************ */

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<bitset>
#include<queue>
#include<ctime>
#include<set>
#include<utility>
#include<vector>
#include<functional>
#include<numeric>
#include<memory>
#include<iterator>
#define LL long long
#define DB double
#define LB long double
#define UL unsigned long
#define ULL unsigned long long
#define pb push_back
#define popb pop_back
#define get(a,i) a&(1<<(i-1))
#define PAU putchar(32)
#define ENT putchar(10)
#define clr(a,b) memset(a,b,sizeof(a))
#define fo(_i,_a,_b) for(int _i=_a;_i<=_b;_i++)
#define fd(_i,_a,_b) for(int _i=_a;_i>=_b;_i--)
#define efo(_i,_a) for(int _i=last[_a];_i!=0;_i=e[_i].next)
#define file(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
#define filein(x) freopen(#x".in","r",stdin)
#define fileout(x) freopen(#x".out","w",stdout)
#define mkd(x) freopen(#x".in","w",stdout);
#define setlargestack(x) int size=x<<20;char *p=(char*)malloc(size)+size;__asm__("movl %0, %%esp\n" :: "r"(p));
#define end system("pause")
using namespace std;
LL read()
{
         LL f=1,d=0;char s=getchar();
         while (s<48||s>57){if (s==45) f=-1;s=getchar();}
         while (s>=48&&s<=57){d=d*10+s-48;s=getchar();}
         return f*d;
}
LL readln()
{
       LL f=1,d=0;char s=getchar();
       while (s<48||s>57){if (s==45) f=-1;s=getchar();}
       while (s>=48&&s<=57){d=d*10+s-48;s=getchar();}
       while (s!=10) s=getchar();
       return f*d;
}
inline void write(LL x)
{
    if(x==0){putchar(48);return;}if(x<0)putchar(45),x=-x;
    int len=0,buf[20];while(x)buf[len++]=x%10,x/=10;
    for(int i=len-1;i>=0;i--)putchar(buf[i]+48);return;
}
inline void writeln(LL x){write(x);ENT;}

const int N = 4005;
const int MAX = 30005;

LL f[N];
LL a[N], s[N];
int n,m;

LL sqr(LL x) {
	return x * x;
}

void init() {
	n=read(), m=read();
	fo(i, 1, n) a[i] = read();
	s[0] = 0;
	fo(i, 1, n) s[i] = s[i - 1] + a[i];
}

LL g[N];

DB calc(int x, int y) {
	return (DB) ((DB) ((LL) g[x] + sqr(s[x]) - g[y] - sqr(s[y])) ) * 1.0 / (DB) (s[x] - s[y]);
}

int st[N];
void work() {
	fo(i, 1, n) {
		f[i] = sqr(s[i]) ;
	}

	fo(j, 2, m) {
		int head = 0, tail = 0;
		fo(i, 1, n) g[i] = f[i];
		memset(st,0,sizeof(st)); st[1] = 0;
		memset(f,0,sizeof(f));
		fo(i, 1, n) {
			while(head < tail && (DB) calc(st[ head ], st[head + 1]) < (DB) 2 * s[i]) head++;
			int now = st[ head ];
			f[i] = g[now] + sqr(s[i] - s[now]);
			while(head < tail && (DB) calc(st[ tail ], st[ tail - 1 ]) > calc(st[ tail ], i) ) tail--;
			st[++tail] = i;
		}
	}

	cout << f[n] * m - sqr(s[n]) <<endl;
}

const LL inf = 1LL << 43;
LL ff[605][605];
void workBL() {
	fo(i, 1, n) ff[i][1] = sqr(s[i]);
	fo(j, 2, m) {
		fo(i, 1, n) {
			LL mi = inf;
			fo(k, 1, n) {
				mi = min(mi, ff[k][j - 1] + sqr(s[i] - s[k]));
			}
			ff[i][j] = mi;
		}
	}
	cout << ff[n][m] * m - sqr(s[n]) << endl;
}


int main() {
	file(journey);
	init();
	if(n > 500) work();
	else workBL();
	return 0;
}


你可能感兴趣的:(动态规划,斜率优化)