【CodeForces】868F. Yet Another Minimization Problem
题目大意是有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的更多相关文章
- 【CodeForces】713 C. Sonya and Problem Wihtout a Legend
[题目]C. Sonya and Problem Wihtout a Legend [题意]给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增.n<=10^5. [算法]动态规划 ...
- 【BZOJ2318】Spoj4060 game with probability Problem 概率
[BZOJ2318]Spoj4060 game with probability Problem Description Alice和Bob在玩一个游戏.有n个石子在这里,Alice和Bob轮流投掷硬 ...
- 【CF954I】Yet Another String Matching Problem(FFT)
[CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...
- 【Codeforces】Round #491 (Div. 2) 总结
[Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...
- 【Codeforces】Round #488 (Div. 2) 总结
[Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...
- Codeforces 868F Yet Another Minimization Problem 决策单调性 (看题解)
Yet Another Minimization Problem dp方程我们很容易能得出, f[ i ] = min(g[ j ] + w( j + 1, i )). 然后感觉就根本不能优化. 然后 ...
- Codeforces 868F Yet Another Minimization Problem(分治+莫队优化DP)
题目链接 Yet Another Minimization Problem 题意 给定一个序列,现在要把这个序列分成k个连续的连续子序列.求每个连续子序列价值和的最小值. 设$f[i][j]$为前 ...
- 【Codeforces】849D. Rooter's Song
[算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...
- 【CodeForces】601 D. Acyclic Organic Compounds
[题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...
随机推荐
- [bzoj 4887] [Tjoi2017]可乐
传送门 Description 加里敦星球的人们特别喜欢喝可乐.因而,他们的敌对星球研发出了一个可乐机器人,并且 放在了加里敦星球的1号城市上.这个可乐机器人有三种行为:停在原地,去下一个相邻的 城市 ...
- CV_Learn
CV学习进度条记录,也是SRTP的进度记录. 阶段一(2019.4-2019.5): 1.opencv简单操作学习.(实现了一些基础操作,从颜色通道到边缘轮廓,2019.4.22完成) 2.linux ...
- react组件中返回并列元素的方法
我们在写react组件的时候,经常会遇到这种问题,在render中return元素只能有一个顶级元素,比如div,假如写成这样就会报错: render(){ return( <div>12 ...
- JVM | JVM体系结构认知
虚拟机 何为虚拟机呢?虚拟机是模拟执行某种指令集体系结构(ISA)的软件,是对操作系统和硬件的一种抽象.其软件模型如下图所示: 计算机系统的这种抽象类似于面向对象编程(OOP)中的针对接口编程泛型(或 ...
- Android 使用Intent
使用intent可以吊起其他应用 例如发送电子邮件 public void sendEmail(View view){ Intent intent = new Intent(Intent.ACTION ...
- Swift_IOS之提示框UIAlertController
import UIKit class ViewController: UIViewController ,UIActionSheetDelegate{ @IBAction func btn1(_ se ...
- Java语言发展史
Java语言发展史 詹姆斯·高斯林(James Gosling)1977年获得了加拿大卡尔加里大学计算机科学学士学位,1983年获得了美国卡内基梅隆大学计算机科学博士学位,毕业后到IBM工作,设计IB ...
- Harbor 1.8.0 仓库的安装和使用
安装的先决条件 硬件环境 1.CPU 至少2G,最好4G 2.内存 至少4G,最好8G 3.磁盘 至少40G,最好160G 软件环境 1.docker版本 17.03.0-ce ...
- Day9作业:socket之FTP工具
代码传的太累,直接发个github的链接吧! https://github.com/ccorzorz/Socketserver_FTP 上两张图给抛砖引玉下吧: 后台管理: FTP程序,包括客户端和s ...
- matlab学习——05插值和拟合(一维二维插值,拟合)
05插值和拟合 1.一维插值 (1) 机床加工零件,试用分段线性和三次样条两种插值方法计算.并求x=0处的曲线斜率和13<=x<=15范围内y的最小值. x0=[0 3 5 7 9 11 ...