# bzoj 1049: 数字序列 dp

## 题解:

(貌似以前考试考过这道题)

(均表示1~i的答案)

(很抱歉我连怎么用都不会,%了一发hzwer的代码)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline ll cat_max(const ll &a,const ll &b){return a>b ? a:b;}
inline ll cat_min(const ll &a,const ll &b){return a<b ? a:b;}
const ll maxn = 35010;
const ll inf1 = 1<<30;
const ll inf2 = 1LL<<60;
// inline ll abs(ll x){
// 	return x < 0 ? -x : x;
// }
struct Edge{
ll to,next;
}G[maxn];
G[++cnt].to = v;
}
ll a[maxn],f[maxn],m[maxn],g[maxn];
ll lim;
inline ll find(ll x){
ll l = 1,r = lim,ret = 0;
while(l <= r){
ll mid = (l+r) >> 1;
if(m[mid] <= x) ret = mid,l = mid+1;
else r = mid - 1;
}return ret;
}
ll suma[maxn],sumb[maxn];
int main(){
memset(m,0x3f,sizeof m);
a[++n] = inf1;m[0] = -inf1;
for(ll i=1;i<=n;++i){
f[i] = find(a[i]) + 1;
//printf("f[%d] = %d\n",i,f[i]);
lim = max(lim,f[i]);
m[f[i]] = min(a[i],m[f[i]]);
}
for(ll i = n;i>=0;--i) add(f[i],i),g[i] = 1LL<<60;
a[0] = -inf1;g[0] = 0;
#define v G[p].to
for(ll u = 1;u<=n;++u){
if(v > u) break;
if(a[v] > a[u]) continue;
suma[v-1] = sumb[v-1] = 0;
for(ll i=v;i<=u;++i){
suma[i] = suma[i-1] + abs(a[v] - a[i]);
sumb[i] = sumb[i-1] + abs(a[u] - a[i]);
}
for(ll i=v;i<=u;++i){
//	printf("%d <- %d\n",g[u],g[v] + suma[i] - suma[v] + sumb[u] - sumb[i]);
g[u] = min(g[u],g[v] + suma[i] - suma[v] + sumb[u] - sumb[i]);
}
}
}
#undef v
printf("%lld\n%lld\n",n-f[n],g[n]);
getchar();getchar();
return 0;
}


posted @ 2017-02-20 19:31  Sky_miner  阅读(166)  评论(0编辑  收藏  举报