题目描述

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 \ldots n1…n around the perimeter of the barn ( 3 \leq n \leq 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 r_iri​ cows to end up in room ii ( 1 \leq r_i \leq 1,000,0001≤ri​≤1,000,000 ). To herd the cows into the barn in an orderly fashion, he plans to unlock kk exterior doors ( 1 \leq k \leq 71≤k≤7 ), allowing the cows to enter through only those doors. Each cow then walks clockwise through the rooms until she reaches a suitable destination. Farmer John wants to unlock the exterior doors that will cause his cows to collectively walk a minimum total amount of distance after entering the barn (they can initially line up however they like outside the kk unlocked doors; this does not contribute to the total distance in question). Please determine the minimum total distance his cows will need to walk, if he chooses the best kk such doors to unlock.

还是这个谷仓,有n(3<=n<=100)个房间。当然,奶牛可能不止n头了。奶牛都在谷仓外面。现在约翰想要让第i个房间关ri(1<=ri<=1000000)头奶牛按顺时针方向走,直到到达合适的房间。这k(1<=k<=7)个门开在哪里,才能使得奶牛们走的路程最少。奶牛在谷仓外可以随意移动,可以随意选择k个门中的任意一个排队,这不计入最终的路程。

输入输出格式

输入格式:

The first line of input contains nn and kk . Each of the remaining nn lines

contain r_1 \ldots r_nr1​…rn​ .

输出格式:

Please write out the minimum amount of distance the cows need to travel.

输入输出样例

输入样例#1: 复制

6 2
2
5
4
2
6
2

输出样例#1: 复制

14

说明

Farmer John can unlock doors 2 and 5. 11 cows enter at door 2 and walk a total

distance of 8 to get to rooms 2, 3, and 4. 10 cows enter at door 5 and walk a

total distance of 6 to get to rooms 5, 6 and 1.

思路:破环为链,dp[i][j][k]表示i房必须开门,i到j房共开k扇门,牛充满i到j房走过的距离。预处理O(k*n*n*n)暴力dp就行。

# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL num[203][203], a[203], dp[203][203][8];
int main()
{int n, K;scanf("%d%d",&n,&K);memset(dp, 0x3f, sizeof(dp));K = min(n, K);for(int i=1; i<=n; ++i) scanf("%lld",a+i), a[i+n] = a[i];for(int i=1; i<=2*n; ++i){for(int j=i; j<=2*n&&j-i+1<=n; ++j){num[i][j] = num[i][j-1] + a[(j<=n)?j:j-n]*(j-i);dp[i][j][1] = num[i][j];}}for(int k=2; k<=K; ++k)for(int i=1; i<=n&&i+k-1<=2*n; ++i)for(int j=i+k-1; j-i+1<=n; ++j)for(int t=i+k-1; t<=j; ++t)dp[i][j][k] = min(dp[i][j][k], dp[i][t-1][k-1]+num[t][j]);LL ans = 0x3f3f3f3f3f3f3f3f;for(int i=1; i+n-1<=2*n; ++i)ans = min(ans, dp[i][i+n-1][K]);printf("%lld\n",ans);return 0;
}

USACO16FEB:再探圆形谷仓(dp)相关推荐

  1. [四连测(三)]圆形谷仓

    我复制了图片,样例什么的就不要在意了. 解题思路 也许有人很迷惑,这个样例怎么算出来的,至少都是上百啊,我身边是有人跟我这样说过的,确实,题目描述有一点问题.它说的是"每头奶牛都待在一个房间 ...

  2. 再探C#类与结构体究竟谁快——考虑栈变量、栈分配、64位整数、密封类

    上次我对C#类与结构体做了一次速度评测(http://blog.csdn.net/zyl910/article/details/6788417).经过一段时间思索,发现还可以进一步探讨-- 第一.栈变 ...

  3. SAP MM 工序委外场景再探 – 相关采购申请单据中的总账科目确定

    SAP MM 工序委外场景再探 – 相关采购申请单据中的总账科目确定 如下的采购申请,是工序委外场景的采购申请, 它在工单1000030 Release的时候被自动触发. 问题是:这个PR上的总账科目 ...

  4. 【Vue.js 牛刀小试】:第九章 - 组件基础再探(data、props)

    系列目录地址 一.基础知识概览 第一章 - 一些基础概念(posted at 2018-10-31) 第二章 - 常见的指令的使用(posted at 2018-11-01) 第三章 - 事件修饰符的 ...

  5. RxSwift学习--核心逻辑再探

    前言 通过上一篇内容RxSwift学习--核心逻辑初探,对RxSwift有了些初步的认知,下面通过源码来看一下RxSwift到底有多骚 RxSwift核心逻辑再探 先把上篇中的例子代码搬过来: //第 ...

  6. 【CTF大赛】2021 DASCTF July cybercms 一探再探

    引言 在前不久结束的 2021 DASCTF July X CBCTF 4th 比赛中,有一道名为 cybercms 的 web 题目. 预期解是从后台登录处进行 SQL 注入写入一句话木马,然而咱在 ...

  7. 联发科mtk手机处理器怎么样_5G手机价格将再探新低!联发科天玑700芯片发布:入门级5G处理器...

    ↑↑↑点击上方蓝字订阅每日最新国产手机资讯 今年5G手机的普及之所以能进展的如此迅速,联发科在5G芯片领域的的活跃功不可没.今天一早,联发科发布的全新的天玑700移动平台,标志着5G手机的价格将进一步 ...

  8. [BZOJ2125]最短路(圆方树DP)

    题意:仙人掌图最短路. 算法:圆方树DP,$O(n\log n+Q\log n)$ 首先建出仙人掌圆方树(与点双圆方树的区别在于直接连割边,也就是存在圆圆边),然后考虑点u-v的最短路径,显然就是:在 ...

  9. C++ 类的知识 | 构造函数再探、匿名对象、友元函数、内部类、类的const成员、类的static成员

    文章目录 构造函数再探 以下代码共调用多少次拷贝构造函数 委托构造函数 概念 形式 匿名对象 友元 友元的声明 友元类 令成员函数作为友元 函数重载和友元 注意 内部类 特性 类的const成员 可变 ...

  10. 函数重载、引用再探、内联函数

    文章目录 函数重载 为什么C++支持重载,C语言不支持呢? extern "C" 引用再探 引用的特性 引用的使用场景 引用和指针 引用和指针的不同点: 内联函数 什么是内联函数? ...

最新文章

  1. Java多线程闲聊(一):概论
  2. android、ios、php之间AES加密解密
  3. 双十一购物节,Nacos 1.4.0 + Go SDK 1.0.1发布
  4. TCP/IP协议详解---概述
  5. 数据结构(七)---循环队列的实现---java版
  6. cocos android-1,cocos2dx在windows下开发,编译到android上(1)
  7. 玩转Android之加速度传感器的使用,模仿微信摇一摇
  8. 2017.3.25 矩阵快速幂 求斐波那契数列第n项
  9. 一篇文章搞定java序列化机制
  10. 一致性哈希算法原理、避免数据热点方法及Java实现
  11. 题解 P3367 【【模板】并查集】
  12. 使用虚拟机备份软件备份云宏Winghong CNware虚拟机
  13. CSUOJ 1009 抛硬币
  14. 在线社交网络的影响力最大化算法
  15. 华为手机传感器测试软件,华为P8拍摄能力测试 IMX278传感器到底魅力何在
  16. 面试送命题,你为什么从上家公司离职?
  17. 疯狂考证的两年:通过CFA、FRM、CMA经验与收获
  18. KVM是什么 机柜 机架服务器 怎么用
  19. 用户权限影响瑞星安装
  20. 【2021】网络协议从入门到底层原理-MJ【新】附上下载链接

热门文章

  1. Java期末考试题(附答案)
  2. php修改服务器ip地址,php修改服务器ip地址
  3. Android自定义View(CustomCalendar-定制日历控件)
  4. 全国各地土特产一览表1
  5. php beanstalk 客户端,Pheanstalk(beanstalk的PHP客户端)-连接如何工作?
  6. 录音文件的比特率的计算方法
  7. 智能家居的应用研究现状
  8. 插件化框架集成-360插件框架DroidPlug
  9. QT开发代码格式化设置
  10. opencv-11-中值滤波及自适应中值滤波