【动态规划】[USACO2016 金组]Circular Barn Revisited
来源:互联网 发布:数据安全保护策略 编辑:程序博客网 时间:2024/05/16 16:05
题目描述
After the last debacle involving Farmer John’s circular barn, one would think he had learned his lesson about non-traditional architecture. However, he thinks he can still make his circular barn (from the preceding problem) function properly by allowing multiple cows into each room. To recap, the barn consists of a ring of nn rooms, numbered clockwise from 1…n1…n around the perimeter of the barn (3≤n≤1003≤n≤100). Each room has doors to its two neighboring rooms, and also a door opening to the exterior of the barn.
Farmer John wants exactly riri cows to end up in room ii (1≤
INPUT FORMAT (file cbarn2.in):
The first line of input contains nn and kk. Each of the remaining nn lines contain r1…rnr1…rn.
OUTPUT FORMAT (file cbarn2.out):
Please write out the minimum amount of distance the cows need to travel.
SAMPLE INPUT:
6 2
2
5
4
2
6
2
SAMPLE OUTPUT:
14
题目分析
我们令
代码
#include <cstdio>#include <iostream>#include <cstring>using namespace std;const int MAXN = 100;const long long INF = 1e18+7;long long dp[MAXN+10][10], sum[MAXN+10];int n, k;inline long long val(int l, int r, long long fix){ long long ret = 0; for(int i=l;i<r;i++) ret += sum[i] * (i - l + fix); return ret;}long long solve(int beg){ for(int i=1;i<k;i++){ for(int j=1;j<=n;j++) if(~dp[j][i]){ for(int t=j+1;t<=n;t++){ if(dp[t][i+1] == -1) dp[t][i+1] = INF; dp[t][i+1] = min(dp[t][i+1], dp[j][i]+val(j, t, 0)); } } } long long ret = INF; for(int i=1;i<=n;i++) if(~dp[i][k]) ret = min(ret, dp[i][k]+val(i, n+1, 0)+val(1, beg, n-i+1)); return ret;}int main(){ scanf("%d%d", &n, &k); for(int i=1;i<=n;i++)scanf("%lld", &sum[i]); long long ans = INF; for(int i=1;i<=n;i++){ memset(dp, -1, sizeof dp); dp[i][1] = 0; ans = min(ans, solve(i)); } printf("%lld\n", ans); return 0;}
- 【动态规划】[USACO2016 金组]Circular Barn Revisited
- 【贪心】[USACO2016 金组]Circular Barn
- BZOJ4409: [Usaco2016 Feb]Circular barn
- BZOJ 4412/Usaco2016 Feb Circular Barn(构造)
- [BZOJ 4412] [Usaco2016 Feb]Circular Barn
- [BZOJ 4409] [Usaco2016 Feb]Circular barn
- 【DP】[USACO 2016 February Contest, Gold]Circular Barn Revisited
- bzoj4412 Circular Barn 构造
- [动态规划]5.3.4 Big Barn
- USACO|Barn Repair|堆|贪心|动态规划
- BZOJ 4742 [Usaco2016 Dec]Team Building 动态规划
- 【动态规划】Barn Repair 修理牛棚 (Usaco_Training 1.3)
- USACO 1.3 Barn Repair (动态规划再写次)
- 【动态规划】[luoguP1209 USACO1.3]修理牛棚 Barn Repair
- 洛谷 P3137 [USACO16FEB]圆形谷仓Circular Barn
- [USACO5.3]巨大的牛棚Big Barn && 洛谷【p1387】最大正方形(动态规划)
- 【贪心】[USACO 2015 February Contest, Gold]Circular Barn
- USACO 2016 February Contest, Bronze Problem 2. Circular Barn
- react.js-06-propTypes类型校验
- 【数据结构与算法】(三) c 语言栈的简单实现
- Android双向滑动冲突解决方案
- VTK 7.0 安装中内存读取冲突 no override found for “.." 解决记录
- 剑指offer 调整数组顺序使奇数位于偶数前面
- 【动态规划】[USACO2016 金组]Circular Barn Revisited
- 【Java数据库连接类】封装数据库操作的连接类DBConnection
- lua-队列
- 【转载】Contiki 的protothreads
- CodeForces - 633D Fibonacci-ish (map&暴力)
- 第二次上机实践项目-项目3-输出星号图
- poj 3233 Matrix Power Series 矩阵快速幂
- 杭电2571
- C 语言基础(一)