原题链接

题目大意是有N个数,分成K段,每一段的花费是这个数里相同的数的数对个数,要求花费最小

如果只是区间里相同数对个数的话,莫队就够了

而这里是!边单调性优化边莫队(只是类似莫队)!而移动的次数和分治的复杂度是一样的!

这个时候就不能用单调栈+二分了,得用分治

分治的方法就是\(Solve(l,r,ql,qr)\)表示我想计算区间\([l,r]\)的答案,然后转移过来的区间在\([ql,qr]\)

暴力计算出\(f[mid]\)的值,找到最优转移点是\(k\),然后分成\(Solve(l,mid,ql,k)\)和\(Solve(mid +1,r,k,qr)\)做下去

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <bitset>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
int N,K,a[MAXN],cnt[MAXN];
int64 f[22][MAXN],w = 0;
int nw,p,q;
int64 work(int l,int r) {
while(q < r) w += cnt[a[++q]]++;
while(p > l) w += cnt[a[--p]]++;
while(p < l) w -= --cnt[a[p++]];
while(q > r) w -= --cnt[a[q--]];
return w;
}
int64 Calc(int a,int b) {
return f[nw - 1][a] + work(a + 1,b);
}
void Solve(int l,int r,int ql,int qr) {
if(l > r) return;
int mid = (l + r) >> 1,p = min(qr,mid - 1);
int k;
f[nw][mid] = Calc(ql,mid);k = ql;
for(int i = ql + 1 ; i <= p ; ++i) {
int64 x = Calc(i,mid);
if(x <= f[nw][mid]) {
f[nw][mid] = x;k = i;
}
}
Solve(l,mid - 1,ql,k);
Solve(mid + 1,r,k,qr);
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
read(N);read(K);
for(int i = 1 ; i <= N ; ++i) {read(a[i]);f[1][i] = f[1][i - 1] + cnt[a[i]]++;} for(int i = 2 ; i <= K ; ++i) {
memset(cnt,0,sizeof(cnt));
p = 1;q = 0;w = 0;
nw = i;
Solve(0,N,0,N);
}
out(f[K][N]);enter;
}

【CodeForces】868F. Yet Another Minimization Problem的更多相关文章

  1. 【CodeForces】713 C. Sonya and Problem Wihtout a Legend

    [题目]C. Sonya and Problem Wihtout a Legend [题意]给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增.n<=10^5. [算法]动态规划 ...

  2. 【BZOJ2318】Spoj4060 game with probability Problem 概率

    [BZOJ2318]Spoj4060 game with probability Problem Description Alice和Bob在玩一个游戏.有n个石子在这里,Alice和Bob轮流投掷硬 ...

  3. 【CF954I】Yet Another String Matching Problem(FFT)

    [CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...

  4. 【Codeforces】Round #491 (Div. 2) 总结

    [Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...

  5. 【Codeforces】Round #488 (Div. 2) 总结

    [Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...

  6. Codeforces 868F Yet Another Minimization Problem 决策单调性 (看题解)

    Yet Another Minimization Problem dp方程我们很容易能得出, f[ i ] = min(g[ j ] + w( j + 1, i )). 然后感觉就根本不能优化. 然后 ...

  7. Codeforces 868F Yet Another Minimization Problem(分治+莫队优化DP)

    题目链接  Yet Another Minimization Problem 题意  给定一个序列,现在要把这个序列分成k个连续的连续子序列.求每个连续子序列价值和的最小值. 设$f[i][j]$为前 ...

  8. 【Codeforces】849D. Rooter's Song

    [算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...

  9. 【CodeForces】601 D. Acyclic Organic Compounds

    [题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...

随机推荐

  1. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  2. RK3399 4G模块移远EC20移植调试

    转载请注明出处:https://www.cnblogs.com/lialong1st/p/11266330.html CPU:RK3399 系统:Android 7.1 1.通过串口打印或者adb获取 ...

  3. idea JRebel

    JRebel 链接:https://pan.baidu.com/s/11LI0RkPtrfEWQENns6cWAA 提取码:ndsu settings -> plugins -> inst ...

  4. 上下文对象-请求对象-响应对象(ServletContext - response - request)

    目录 上下文对象-请求对象-响应对象 ServletContext response request 上下文对象-请求对象-响应对象 ServletContext 什么是ServletContext ...

  5. ArcGIS10.3_解决属性表中文乱码问题

    借鉴前辈们解决ArcMap低版本属性表乱码的问题解决方法,勇敢的尝试了一下Pro中的解决方法,其实道理都一样.先来看看第一种方法:打开CMD,如果是ArcMap,输入如下命令: reg add HKE ...

  6. 蜜汁头文件&&slow slow read

    slow slow read 板子 inline int read() { ; char last=' ',ch=getchar(); ') last=ch,ch=getchar(); +ch-',c ...

  7. python @abstractmethod

    1.写在前面 由于python 没有抽象类.接口的概念,所以要实现这种功能得abc.py 这个类库 2.@abstractmethod特点 @abstractmethod:抽象方法,含abstract ...

  8. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_19-认证接口开发-接口开发-controller

    补充controller方法 判断是否有值传过来. 私有方法存储cookie httpOnly设置为false的话 浏览器就拿到这个cookie 拿到Response cookie在配置文件内的配置 ...

  9. 一百四十七:CMS系统之celery实现邮件和短信异步发送

    celery工作原理 celery官方文档:https://docs.celeryproject.org/en/latest/ 安装:pip install celery windows下还需安装ev ...

  10. 关于Jmeter+Ant+Jenkins作为接口、性能自动化框架的误区

    说明:Apach-Jmeter有完善的桌面客户端,关联数据的处方方式,各种参数化的方式,各种Jar包的扩展,也可以用作抓包工具使用,当然最重要的是它是[开源!开源!开源!],重要的事说三遍,目前也有基 ...