P3137 [USACO16FEB]圆形谷仓Circular Barn_Silver

题目描述

Being a fan of contemporary architecture, Farmer John has built a new barn in the shape of a perfect circle. Inside, 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 10003≤n≤1000). Each room has doors to its two neighboring rooms, and also a door opening to the exterior of the barn.

Farmer John owns nn cows, and he wants exactly one cow to end up in each room in the barn. However, the cows, being slightly confused, line up at haphazard doors, with possibly multiple cows lining up at the same door. Precisely c_ici​ cows line up outside the door to room ii, so \sum c_i = n∑ci​=n.

To manage the process of herding the cows so that one cow ends up in each room, Farmer John wants to use the following approach: each cow enters at the door at which she initially lined up, then walks clockwise through the rooms until she reaches a suitable destination. Given that a cow walking through dd doors

consumes d^2d2 energy, please determine the minimum amount of energy needed to distribute the cows so one ends up in each room.

给定一个环长为n的环上每个点上有aia_i只牛,每只牛可以在环上顺时针移动,代价为移动距离的平方,问将环上每个节点上都有一只牛的代价是多少

输入输出格式

输入格式:

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

c_1 \ldots c_nc1​…cn​.

输出格式:

Please write out the minimum amount of energy consumed by the cows.

输入输出样例

输入样例#1: 复制

10
1
0
0
2
0
0
1
2
2
2

输出样例#1: 复制

33

思路:贪心。a^2+b^2<(a+b)^2

奶牛不能越过位置行走,只能走到下一位,在走到下一位。上图中红色路线的代价为2小于橙色路线的代价。
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
priority_queue<int,vector<int>,greater<int> >que;
int a[2010];
int n,ans=1e9;
int main(){scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]);a[n+i]=a[i];}for(int i=1;i<=n;i++){int flag=0,cnt=0;for(int j=i;j<=i+n-1;j++){if(a[j]==0&&que.size()==0){ flag=1; break; }int x=a[j];while(x){ que.push(j);x--; }int now=que.top();que.pop();cnt+=(j-now)*(j-now);}if(flag==1)    continue;ans=min(ans,cnt);}cout<<ans;
}

 

转载于:https://www.cnblogs.com/cangT-Tlan/p/9609611.html

洛谷 P3137 [USACO16FEB]圆形谷仓Circular Barn_Silver相关推荐

  1. ybtoj洛谷P3268:圆的异或并(扫描线)

    解析 很神奇的一道题. 关键条件:任意两个圆无交. 把一个圆分成上下两个圆弧,那么所有圆弧的高度关系不会发生变化. 所以可以开一个 set,维护一个从左往右扫的扫描线,按照当前扫描线的横坐标定义比较符 ...

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

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

  3. 洛谷P4630 [APIO2018] Duathlon 铁人两项 【圆方树】

    题目链接 洛谷P4630 题解 看了一下部分分,觉得树的部分很可做,就相当于求一个点对路径长之和的东西,考虑一下能不能转化到一般图来? 一般图要转为树,就使用圆方树呗 思考一下发现,两点之间经过的点双 ...

  4. Hash表——省市(洛谷 P3405)

    题目选自洛谷P3405 因为值和前两个字母有关系,所以对每个字符串只保留前两个字母,相当于有N个二元组 <ai,bi>,问<ai,bi> = <bi,ai> 的(i ...

  5. 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver

    洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver 题目描述 FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱. ...

  6. 洛谷——P2433 【深基1-2】小学数学 N 合一

    P2433 [深基1-2]小学数学 N 合一 题目描述 问题 1 请输出 I love Luogu! 问题 2 这里有 10 个苹果,小A 拿走了 2 个,Uim 拿走了 4 个,八尾勇拿走剩下的所有 ...

  7. 【蓝桥杯专题】 贪心(C++ | 洛谷 | acwing | 蓝桥)

    菜狗现在才开始备战蓝桥杯QAQ 文章目录 [蓝桥杯专题] (C++ | 洛谷 | acwing | 蓝桥) 1055. 股票买卖 II AcWing 104. 货仓选址 传递糖果 AcWing 112 ...

  8. 洛谷日报 2020年3月前索引

    2020 2019 2018 感觉洛谷日报全是干货!!!先记下来再说 2020 年洛谷日报索引 3 月 #260[dove]Church 编码(和 Lambda 演算) https://www.luo ...

  9. 洛谷日报索引(2020、2019、2018)

    历年洛谷日报索引 2020 2019 2018 感觉洛谷日报全是干货!!!先记下来再说 2020 年洛谷日报索引 3 月 #260[dove]Church 编码(和 Lambda 演算) https: ...

  10. [转载]洛谷日报索引

    2019年 6月 #183[朝田诗乃]你以为莫队只能离线?莫队的在线化改造 https://shoko.blog.luogu.org/moqueue #182[Heartlessly ]常用距离算法详 ...

最新文章

  1. 360 您访问的是存在未经证实信息的网站
  2. 高矮不同的人排队问题
  3. CoreCRM 开发实录——开始之新项目的技术选择
  4. 【POJ - 1995】Raising Modulo Numbers(裸的快速幂)
  5. 95-32-010-ChannelPipeline-简介md
  6. 主成分分析(Principal components analysis)(特征降维)(PCA)-最大方差解释
  7. 开博客第一天!!来几句鸡汤
  8. 用数组建立二叉树(LeetCode二叉树测试)
  9. 使用WebViewJavascriptBridge与UIWebView交互
  10. liunx 监控工具sar
  11. 【Android 四大组件之Content Provider】一文吃透 BroadcastReceiver 广播接收器
  12. 【机器学习】朴素贝叶斯对文本分类--对人名国别分类
  13. COPRA RF 2005 SR1
  14. 用友U9【SV】服务
  15. Android系统开发:短信的号码拦截
  16. Linux进程中有xorg,linux – Xorg如何工作?
  17. 2018中国初创企业融资近千亿 人工智能领跑新经济破局
  18. 篮球数据API接口 - 【篮球赛事分析数据】API调用示例代码
  19. 【计算机毕业设计】java+mysql基于SSM的生鲜超市进销存管理系统
  20. 无线网络技术导论笔记(第三讲)

热门文章

  1. GetFirmwareEnvironmentVariable失败返回错误码1314
  2. 为什么计算机关机后自动开机,为何我家的电脑关机后又自动开机?
  3. 华硕主板无盘启动bios设置_华硕主板开机怎么进入bios_华硕主板bios设置U盘启动方法...
  4. windows使用批处理bat控制打印机自动打印测试页(可设置定时自动打印)
  5. 个人记录—— The bean ‘xxx.FeignClientSpecification‘ could not be registered ...
  6. VGG为什么叫做VGG,VGG的由来
  7. docker镜像(第二天)
  8. 《黑白团团队》第六次作业:团队项目系统设计改进与详细设计
  9. 微信可以批量清理僵尸粉啦
  10. html网页设计动态烟花效果