BZOJ2933 [Poi1999]地图【区间DP】
Description
一个人口统计办公室要绘制一张地图。由于技术的原因只能使用少量的颜色。两个有相同或相近人口的区域在地图应用相同的颜色。例如一种颜色k,则A(k) 是相应的数,则有:
在用颜色k的区域中至少有一半的区域的人口不大于A(k)
在用颜色k的区域中至少有一半的区域的人口不小于A(k)
区域颜色误差是该区域的人口与A(k)差的绝对值。累计误差是所有区域颜色误差的总和。我们要求出一种最佳的染色方案(累计误差最小)。
任务
写一个程序:
读入每个区域的人口数
计算最小的累计误差
将结果输出
Input
第一行有一个整数n,表示区域数,10< n <3000。在第二行中的数m表示颜色数,2 <= m <= 10。在接下来的n中每行有一个非负整数,表示一个区域的人口。人口都不超过2^30。
Output
输出一个整数,表示最小的累计误差
Sample Input
11
3
21
14
6
18
10
2
15
12
3
2
2
Sample Output
15
思路
有贪心的思想,可以先排序,从小到大进行分块
\(dp_{i,j}\)表示前i个数分j个颜色
然后\(dp_{i,j}=\min(dp_{k-1,j-1}+calc(k,i))\)
\(calc(k,i)=\sum_{p=k}^i |a[mid]-a[p]|\)
然后考虑怎么快速算calc
发现每次在端点加上一个数,中位数会向右平移一位,然而平移前后原来和是不变的
所以只需要统计当前加上这个数之后的贡献
//Author: dream_maker
#include<bits/stdc++.h>
using namespace std;
//----------------------------------------------
//typename
typedef long long ll;
//convenient for
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
//inf of different typename
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
//fast read and write
template <typename T>
void Read(T &x) {
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 3010;
const int M = 20;
int n, m, a[N];
ll cal[N][N], dp[N][M];
int main() {
Read(n), Read(m);
fu(i, 1, n) Read(a[i]);
sort(a + 1, a + n + 1);
fu(j, 2, n)
fd(i, j - 1, 1)
cal[i][j] = cal[i + 1][j] + a[(i + j + 1) >> 1] - a[i];
fu(i, 0, n)
fu(j, 0, m) dp[i][j] = INF_of_ll;
dp[0][0] = 0;
fu(i, 1, n)
fu(j, 1, m)
fu(k, 1, i)
dp[i][j] = min(dp[i][j], dp[k - 1][j - 1] + cal[k][i]);
Write(dp[n][m]);
return 0;
}
BZOJ2933 [Poi1999]地图【区间DP】的更多相关文章
- BZOJ 2933([Poi1999]地图-区间Dp)
2933: [Poi1999]地图 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 7 Solved: 7 [ Submit][ Status] ...
- BZOJ2933: [Poi1999]地图
Description 一个人口统计办公室要绘制一张地图.由于技术的原因只能使用少量的颜色.两个有相同或相近人口的区域在地图应用相同的颜色.例如一种颜色k,则A(k) 是相应的数,则有: 在用颜色 ...
- UESTC 2015dp专题 A 男神的礼物 区间dp
男神的礼物 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/65 Descri ...
- 【区间DP】【lgP3146】248
传送门 Description 给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-40),问最大能合出多少.注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3. In ...
- 「USACO16OPEN」「LuoguP3146」248(区间dp
题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small to ...
- 「区间DP」「洛谷PP3146 」[USACO16OPEN]248 G
[USACO16OPEN]248 G 题目: 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
随机推荐
- Codeforces Round #365 (Div. 2) E - Mishka and Divisors(转化成01-背包)
http://codeforces.com/contest/703/problem/E 题意: 给出n个数和一个k,计算出至少要多少个数相乘才是k的倍数. 思路:这道题目参考了杭电大神的代码http: ...
- Gym - 100712B Rock-Paper-Scissors
https://vjudge.net/problem/Gym-100712B 题意: 石头剪刀布游戏. 给出一个玩家n局的出的顺序,现在另一个是这样出的,X+Y+Z=n,在前X轮出石头,中间Y轮出布, ...
- springboot Actuator健康检查
通过情况下,如我们想在系统中添加一个健康检查的接口,我们怎么做呢? 我们会新建一个类,或在已存在类的基础上添加检测接口. package com.crhms.medicareopinion; impo ...
- 关于Vue的component制作dialog组件
其实原理很简单,兴个粟子, 点击按钮出现 dialog 弹出杠, 将dialog做成一个组件,components/dialog.vue 就是在components里面新建一个vue.将这个vue做为 ...
- Java字符串替换忽略大小写
今天在做字符串替换时忽略大小写,网上找到了一个非常简便的方法,使用String的replaceAll方法. java.lang.String.replaceAll(String regex, Stri ...
- mvn deploy返回400错误的几种可能
user credentials are wrong url to server is wrong user does not have access to the deployment reposi ...
- 【Python】深入浅出学习Python的yield和generator
背景 之前走马观花接触过Python协程的概念,这两天和一个同事聊到了协程,死活想不起来曾经看过的东西,就记得一个yield,概念不清: 所以想捋一捋相关的东西,此篇作为学习的记录. Generato ...
- Shell date 命令详解
格式: date [选项] ... [+格式] 选项说明: -d ,--date=字符串 显示指定字符串所描述的时间 格式说明: 例子1: #!/bin/bash ##. 获取当前系统时间 YYYY- ...
- SSM框架WebSocket配置
1.StartFilter.java package cn.xydata.pharmacy.websocket; import java.io.IOException; import javax.se ...
- CentOS 7.2下编译安装PHP7.0.10+MySQL5.7.14+Nginx1.10.1
一.安装前的准备工作 1.yum update #更新系统 2.yum install gcc gcc-c++ autoconf automake cmake bison m4 libxml2 ...