[hdu3045] Picnic Cows DP斜率优化
来源:互联网 发布:java流式读取文件 编辑:程序博客网 时间:2024/06/05 16:04
Picnic Cows
Problem Description
It’s summer vocation now. After tedious milking, cows are tired and wish to take a holiday. So Farmer Carolina considers having a picnic beside the river. But there is a problem, not all the cows consider it’s a good idea! Some cows like to swim in West Lake, some prefer to have a dinner in Shangri-la ,and others want to do something different. But in order to manage expediently, Carolina coerces all cows to have a picnic!
Farmer Carolina takes her N (1 < N≤400000) cows to the destination, but she finds every cow’s degree of interest in this activity is so different that they all loss their interests. So she has to group them to different teams to make sure that every cow can go to a satisfied team. Considering about the security, she demands that there must be no less than T(1< T≤N)cows in every team. As every cow has its own interest degree of this picnic, we measure this interest degree’s unit as “Moo~”. Cows in the same team should reduce their Moo~ to the one who has the lowest Moo~ in this team——It’s not a democratical action! So Carolina wishes to minimize the TOTAL reduced Moo~s and groups N cows into several teams.
For example, Carolina has 7 cows to picnic and their Moo~ are ‘8 5 6 2 1 7 6’ and at least 3 cows in every team. So the best solution is that cow No.2,4,5 in a team (reduce (2-1)+(5-1) Moo~)and cow No.1,3,6,7 in a team (reduce ((7-6)+(8-6)) Moo~),the answer is 8.
Input
The input contains multiple cases.
For each test case, the first line has two integer N, T indicates the number of cows and amount of Safe-base line.
Following n numbers, describe the Moo~ of N cows , 1st is cow 1 , 2nd is cow 2, and so on.
Output
One line for each test case, containing one integer means the minimum of the TOTAL reduced Moo~s to group N cows to several teams.
Sample Input
7 3
8 5 6 2 1 7 6
Sample Output
8
数据范围竟然在题面上
f[i] = min{f[j]+sum[i]-sum[j]-(i-j)*a[j+1]}
化简得:
分母:f[J]-f[k]+sum[k]-sum[j]+j*a[j+1]-k*a[k+1]
分子:a[j+1]-a[k+1]
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int N = 400000 + 5;typedef long long ll;ll a[N],sum[N],f[N]; int q[N];ll n,t;ll up( int j, int k ){ return f[j]-f[k]+sum[k]-sum[j]+j*a[j+1]-k*a[k+1]; }ll down( int j, int k ){ return a[j+1]-a[k+1]; }int main(){ while( ~scanf("%d%d", &n, &t )) { memset(sum,0,sizeof(sum)); f[0] = sum[0] = a[0] = 0; for( int i = 1; i <= n; i++ ) scanf("%I64d", &a[i]); std::sort(a+1,a+1+n); for( int i = 1; i <= n; i++ ) sum[i] = sum[i-1]+a[i]; int head = 0, tail = 0; q[tail++] = 0; for( int i = 1; i <= n ; i++ ){ while( head+1 < tail && up(q[head+1], q[head]) <= i * down( q[head+1], q[head] )) head++; f[i] = f[q[head]] + sum[i] - sum[q[head]] - a[q[head]+1]*(i-q[head]); int j = i-t+1; if( j < t ) continue; while( head+1 < tail && up(j,q[tail-1])*down(q[tail-1],q[tail-2]) <= down(j,q[tail-1])*up(q[tail-1],q[tail-2])) tail--; q[tail++] = j; } printf("%I64d\n", f[n]); } return 0;}
- hdu3045 Picnic Cows(斜率优化DP)
- [hdu3045] Picnic Cows DP斜率优化
- hdu3045 Picnic Cows 斜率优化dp
- 【HDU3045】【斜率优化DP】Picnic Cows题解
- HDU3045——Picnic Cows(斜率优化DP)
- HDU 3045 Picnic Cows【dp斜率优化】
- HDU 3045 Picnic Cows 斜率优化DP
- HDU 3045 Picnic Cows (斜率优化DP)
- hdu 3045 Picnic Cows(斜率优化dp)
- hdu 3045 Picnic Cows 斜率优化dp
- HDU-3045 Picnic Cows 【DP+斜率优化】
- HDU-3045 Picnic Cows 斜率优化DP
- [HDU3045] Picnic Cows
- hdu3045 Picnic Cows
- HDU3045 Picnic Cows
- HDU 3045 Picnic Cows(斜率优化DP)
- HDOJ 3045 Picnic Cows(斜率优化DP)
- dp斜率优化 Hdu 3045(Picnic Cows)题解
- codeforces 514B
- 自己做了一个网站,springmvc+mybatis+mysql
- 新电脑从另外一台电脑完整拷贝环境,不需要安装环境
- android布局与组件
- mysql创建定时器(每天凌晨执行)
- [hdu3045] Picnic Cows DP斜率优化
- Quartz 2D编程指南(2) - 图形上下文(Graphics Contexts)
- 关于"net start mysql"提示"服务名无效"的启动mysql方法
- Android调试的Log.d()没有输出
- 网站
- c/c++ 的 指针的解引用
- Webpack打包font-awesome
- 【Unity&JSON】Console.WriteLine的问题
- jxl导出数据到excel文件