[USACO09MAR]Cleaning Up
题目大意:
给你一个长度为n的序列a,你可以将其分为若干段,最终的答案为每一段不同数个数的平方和。
思路:
不难想到一个O(n^2)的DP:
f[i]=min{f[j]+cnt(j,i)^2}
考虑一些优化。
首先不难发现,答案最坏不会超过n。(一个数一段)
要让答案更优,一段内不同数的个数不会超过sqrt(n)。(不然平方之后就超过n了)。
我们把到i有j个不同数的最后位置记作pos[j]。
考虑如何维护这个pos[j]。
我们可以先将每个数字出现的上一个位置记作last[i],一段中出现的不同数字个数记作cnt[i]。
首先,如果last[a[i]]<=pos[j],则cnt[j]++。
当cnt[j]>j时,把左端点往右缩,如果这时last[a[pos[j]]]==pos[j],cnt[j]--。
#include<cmath>
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int inf=0x7fffffff;
const int N=,M=;
int a[N],f[N],pos[M],last[M],cnt[M];
int main() {
int n=getint(),m=getint();
for(register int i=;i<=n;i++) {
const int x=getint();
if(x!=a[a[]]) a[++a[]]=x;
}
n=a[],m=sqrt(a[]);
for(register int i=;i<=n;i++) {
f[i]=inf;
for(register int j=;j<=m;j++) {
if(last[a[i]]<=pos[j]) cnt[j]++;
}
last[a[i]]=i;
for(register int j=;j<=m;j++) {
while(cnt[j]>j) {
pos[j]++;
if(last[a[pos[j]]]==pos[j]) cnt[j]--;
}
}
for(register int j=;j<=m;j++) {
f[i]=std::min(f[i],f[pos[j]]+j*j);
}
}
printf("%d\n",f[n]);
return ;
}
[USACO09MAR]Cleaning Up的更多相关文章
- P2943 [USACO09MAR]Cleaning Up G
一句话题意:将一个数列分成若干段,每段的不和谐度为该段内不同数字数量的平方,求不和谐度之和的最小值. 令 \(f_i\) 表示前 \(i\) 个数的最小答案,很容易就能写出暴力转移方程:\(f_i=\ ...
- 洛谷2943 [USACO09MAR]清理Cleaning Up——转变枚举内容的dp
题目:https://www.luogu.org/problemnew/show/P2943 一下想到n^2.然后不会了. 看过TJ之后似乎有了新的认识. n^2的冗余部分在于当后面那部分的种类数一样 ...
- luogu 2943 [USACO09MAR]清理Cleaning Up 动态规划
非常巧妙的动态规划. 你会发现每一个区间地颜色种类不能超过 $\sqrt n$, 所以可以直接枚举区间颜色种类. 令这个为 $pos[j],$ 然后考虑如何去更新这个东西就行了. Code: #inc ...
- 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚
题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...
- Coursera-Getting and Cleaning Data-week1-课程笔记
博客总目录,记录学习R与数据分析的一切:http://www.cnblogs.com/weibaar/p/4507801.html -- Sunday, January 11, 2015 课程概述 G ...
- Coursera-Getting and Cleaning Data-Week2-课程笔记
Coursera-Getting and Cleaning Data-Week2 Saturday, January 17, 2015 课程概述 week2主要是介绍从各个来源读取数据.包括MySql ...
- Coursera-Getting and Cleaning Data-Week3-dplyr+tidyr+lubridate的组合拳
Coursera-Getting and Cleaning Data-Week3 Wednesday, February 04, 2015 好久不写笔记了,年底略忙.. Getting and Cle ...
- Coursera-Getting and Cleaning Data-week4-R语言中的正则表达式以及文本处理
博客总目录:http://www.cnblogs.com/weibaar/p/4507801.html Thursday, January 29, 2015 补上第四周笔记,以及本次课程总结. 第四周 ...
- 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划
[BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...
随机推荐
- python 学习分享-select等
首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组(在linux中 ...
- 剖析epool
[01]什么是epool: 当互联网的用户越来越多的时候,人们发现传统的网络io模型,扛不住用户的高并发请求的时候.各个操作系统给出了自己对应的答案, 而linux给出的答案是epool.epool是 ...
- a链接点击下载图片到本地(php)
$url="http://pan.baidu.com/share/qrcode?w=150&h=150&url=http://gandao.my".U('Quest ...
- 孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块
孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.datetime模块 dateti ...
- ocrosoft Contest1316 - 信奥编程之路~~~~~第三关 问题 M: 当总统
http://acm.ocrosoft.com/problem.php?cid=1316&pid=12 题目描述 小明想当丑国的总统,丑国大选是按各州的投票结果来确定最终的结果的,如果得到超过 ...
- Centos安装后的一些必要处理工作
1永久关闭selinux,修改成permissive或者disabled(建议),修改完需重启 2配置network 3.禁止ping(可选,一般不需要禁止)(默认为0位启用ICMP协议,1为禁止), ...
- ES6 Destructuring Assignment All In One
ES6 Destructuring Assignment All In One ES6 & Destructuring Assignment Axios, vue https://develo ...
- 【bzoj4419】[Shoi2013]发微博 STL-set
题目描述 刚开通的SH微博共有n个用户(1..n标号),在短短一个月的时间内,用户们活动频繁,共有m条按时间顺序的记录: ! x 表示用户x发了一条微博: + x y 表示用户x和用户y成为了好友 ...
- freebsd网卡驱动程序详解
freebsd网卡驱动程序详解 来源 https://blog.csdn.net/h_cszc/article/details/7776116 /* 注释:xie_minix */ /*此处为BSD申 ...
- 股神小L [贪心]
题面 思路 股票题肯定是贪心或者$dp$啊 这个题比较$naive$,可以看出来你这里买股票的过程一定是能不买就不买,能卖就拣最贵的日子卖,而且时间不能倒流(废话= =||) 所以我们按照时间从前往后 ...