Estimation

时间限制(普通/Java):5000MS/15000MS     运行内存限制:65536KByte
总提交: 6            测试通过: 1

描述

“There are too many numbers here!” your boss bellows. “How am I supposed to make sense of all of this? Pare it down! Estimate!”
You are disappointed. It took a lot of work to generate those numbers. But, you’ll do what your boss asks.
You decide to estimate in the following way: You have an array A of numbers. You will partition it into k contiguous sections, which won’t necessarily be of the same size. Then, you’ll use a single number to estimate an entire section. In other words, for your array A of size n, you want to create another array B of size n, which has k contiguous sections. If i and j are in the same section, then B[i]=B[j]. You want to minimize the error, expressed as the sum of the absolute values of the differences (Σ|A[i]-B[i]|).

输入

There will be several test cases in the input. Each test case will begin with two integers on a line, n (1≤n≤2,000) and k (1≤k≤25, kn), where nis the size of the array, and k is the number of contiguous sections to use in estimation. The array A will be on the next n lines, one integer per line. Each integer element of A will be in the range from -10,000 to 10,000, inclusive. The input will end with a line with two 0s.

输出

For each test case, output a single integer on its own line, which is the minimum error you can achieve. Output no extra spaces, and do not separate answers with blank lines. All possible inputs yield answers which will fit in a signed 64-bit integer.

样例输入

7 2
6
5
4
3
2
1
7
0 0

样例输出

9

题目来源

The University of Chicago Invitational Programming Contest 2012

题解:

勉强卡过。。应该不是正解。。

以前做过一道题。。在一个线段上给定几个点。。求一个点使得到各点的距离和最短。。。从小到大排序。。该点就是中间大的点。。偶数个的话。。中间两个点都可以

dp[i][j]记录i到j最小的绝对值。。。

树状数组t[i]用来记录大小为i的点的个数

树状数组e[i]用来记录<=i的所有点的和

求i到j这一段的中间值就是找t[mid] == (j - i + 1 + 1) / 2 偶数个的话。。取后一个。。

之后找比 mid 小的所有点和sum1。。还有比 mid 大的所有点和sum2

求比 mid 小的所有点的个数num1。。比 mid 大的点个数num2

dp[i][j] = sum2 - sum1 + mid * (num1 - num2)

 #include <stdio.h>
#include <string.h>
const int N = ;
const int Max = ;
int a[N];
int t[Max+];
int e[Max+];
int dp[N][N];
int f[N][];
int Min(int a, int b){
return a < b ? a : b;
} int lowbit(int x){
return x & (-x);
} void add(int i, int val, int c[]){ while(i <= Max){
c[i] += val;
i += lowbit(i);
}
}
int sum(int i, int c[]){ int ret = ;
while(i > ){
ret += c[i];
i -= lowbit(i);
}
return ret;
}
int Bin_search(int v){ int l = ;
int r = Max;
while(l < r){
int m = (l + r) >> ;
int p = sum(m, t);
if(p >= v){
r = m;
}
else{
l = m + ;
}
}
return l;
}
int main()
{
int n, k;
while(scanf("%d %d", &n, &k), n || k){
int i, j, g;
for(i = ; i <= n; i++){
scanf("%d", a+i);
a[i] += ;
}
for(i = ; i <= n; i++){ memset(t, , sizeof(t));
memset(e, , sizeof(e));
for(j = i; j <= n; j++){
add(a[j], , t);
add(a[j], a[j], e);
int num = (j - i + ) >> ;
int mid = Bin_search(num);
// printf("mid = %d\n", mid);
int sum1 = sum(mid - , e);
// printf("sum1 = %d\n", sum1);
int sum2 = sum(Max, e) - sum(mid, e);
// printf("sum2 = %d\n", sum2);
int num1 = sum(mid - , t);
// printf("num1 = %d\n", num1);
int num2 = sum(Max, t) - sum(mid, t);
// printf("num2 = %d\n", num2);
dp[i][j] = sum2 - sum1 + mid * (num1 - num2);
// printf("%d ", dp[i][j]);
}
}
for(i = ; i <= k; i++){
for(j = ; j <= n; j++){
f[j][i] = dp[][j];
if(i == ){
continue;
}
for(g = ; g <= j; g++){
f[j][i] = Min(f[j][i], f[g-][i-] + dp[g][j]);
}
//printf("%d ", f[j][i]);
}
//puts("");
}
printf("%d\n", f[n][k]);
}
return ;
}

toj 4353 Estimation(树状数组+二分查找)的更多相关文章

  1. POJ 2182 Lost Cows (树状数组 && 二分查找)

    题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...

  2. PAT-1057 Stack (树状数组 + 二分查找)

    1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...

  3. 树状数组+二分||线段树 HDOJ 5493 Queue

    题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...

  4. poj2182Lost Cows——树状数组快速查找

    题目:http://poj.org/problem?id=2182 从后往前确定,自己位置之前没有被确定的且比自己编号小的个数+1即为自己的编号: 利用树状数组快速查找,可另外开一个b数组,角标为编号 ...

  5. P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]

    题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...

  6. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  7. TZOJ 4602 高桥和低桥(二分或树状数组+二分)

    描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...

  8. POJ 2182 Lost Cows 【树状数组+二分】

    题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  9. The Stream of Corning 2( 权值线段树/(树状数组+二分) )

    题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...

随机推荐

  1. [bzoj4824][洛谷P3757][Cqoi2017]老C的键盘

    Description 老 C 是个程序员. 作为一个优秀的程序员,老 C 拥有一个别具一格的键盘,据说这样可以大幅提升写程序的速度,还能让写出来的程序 在某种神奇力量的驱使之下跑得非常快.小 Q 也 ...

  2. noip2017考前基础复习——数论数学

    ·最大公约数 gcd 辗转相除法  gcd(a,b)=gcd(b,a%b) int gcd(int x,int y){ ) return x; return gcd(y,x%y); } 效率O(log ...

  3. .NetCore自定义WebAPI返回Json的格式大小写的三种方式

    .NetCore的Controller/WebAPI可以帮我们将返回结果自动转换为Json格式给前台,而且可以自由设定格式(大写.小写.首字母大写等),我总结了三种方法,对应三种灵活度,供大家参考 ( ...

  4. 优课堂考勤系统——Alpha发布1

    一.作业描述 这个作业属于哪个课程 这个作业要求在哪里 团队名称 CTRL-IKun 这个作业的目标 在这个星期内完成团队项目α版本的第一次测试和发布,为发布下一次的 α版本做一个准备和前期检验 项目 ...

  5. CF572_Div2_F

    题意 http://codeforces.com/contest/1189/problem/F 思考 由于是子序列,答案只跟选法有关,与顺序无关,先排序. 直接计算答案比较困难.联想到期望的无穷级数计 ...

  6. cesium纽约3dtiles数据下载

    cesium示例有纽约的3dtiles数据,下载官方有下载链接,但是下载后为乱码. 因此研究了下,写了个爬虫解码下载,使用办法,安装Python直接运行即可,代码如下: #coding=utf-8 f ...

  7. Shrio | java.io.IOException: Resource [classpath:shiro.ini] could not be found

    案例 今天项目启动时一直报异常,看了错误日志发现是shrio文件找不到引起的,异常: java.io.IOException: Resource [classpath:shiro.ini] could ...

  8. vue报错Error in v-on handler: "RangeError: Maximum call stack size exceeded"

    看下面的报错 错误 看到这个错误一脸懵逼.后面了解到,是因为程序进入了死循环,后面检查了我的代码,原来在这里自己调用自己

  9. ros之发布者和订阅者协同工作

    前面的例子显示了单个发布者/单个订阅者的情况,但是一个节点也可以同时是一个发布者和订阅者,或者拥有多个订阅和发布. 实际上,ROS节点最常做的事情是传递消息,并在消息上进行运算. 例doubler.p ...

  10. POJ_1564_dfs

    题目描述: 每组数据给定一个大的数,和一系列降序的数值,要求列出不重复的数值中挑选的数的和为大数的方案,每一种方案里,每个数值最多只能使用一次. 思路: dfs基础题,每次记录大数和当前总和的差值,当 ...