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[];
int n,ans=1e9;
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
a[n+i]=a[i];
}
for(int i=;i<=n;i++){
int flag=,cnt=;
for(int j=i;j<=i+n-;j++){
if(a[j]==&&que.size()==){ flag=; break; }
int x=a[j];
while(x){ que.push(j);x--; }
int now=que.top();que.pop();
cnt+=(j-now)*(j-now);
}
if(flag==) continue;
ans=min(ans,cnt);
}
cout<<ans;
}

 

洛谷 P3137 [USACO16FEB]圆形谷仓Circular Barn_Silver的更多相关文章

  1. 圆形谷仓Circular Barn_Silver---(DP优化 / )队列 + 贪心(复杂度O(2n))---DD(XYX)​​​​​​​的博客

    目录 小数据 大数据 小数据 题目描述 农夫约翰有一个圆形的谷仓,谷仓分成了环形的n(3≤n≤1000)个房间,编号为1 , 2 , -- .每个房间有三个门,两个门通往两个相邻的房间,第三个门朝外. ...

  2. 「 Luogu P3137 」X 「 USACO16FEB 」 圆形谷仓

    # 题目大意 管理大大给修下 $\text{Markdown}$ 吧,严重影响做题体验啊. 这道题的意思很简单就是给你一个长度是 $n$ 的环,这个环上不均匀的分布着 $n$ 头奶牛.一头奶牛移动要花 ...

  3. 洛谷P3268 [JLOI2016]圆的异或并(扫描线)

    扫描线还不是很熟啊--不管是从想的方面还是代码实现的方面-- 关于这题,考虑一条平行于\(y\)轴的扫描线从左到右扫描每一个圆,因为只有相离和内含两种关系,只用在切线处扫描即可 我们设上半圆为1,下半 ...

  4. 洛谷P3138 [USACO16FEB]负载平衡Load Balancing_Silver

    P3138 [USACO16FEB]负载平衡Load Balancing_Silver 题目描述 Farmer John's NN cows are each standing at distinct ...

  5. 洛谷P2508 [HAOI2008]圆上的整点

    题目描述 求一个给定的圆$ (x^2+y^2=r^2) $,在圆周上有多少个点的坐标是整数. 输入格式 \(r\) 输出格式 整点个数 输入输出样例 输入 4 输出 4 说明/提示 \(n\le 20 ...

  6. [bzoj1041] [洛谷P2508] [HAOI2008] 圆上的整点

    Description 求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数. Input 只有一个正整数n,n<=2000 000 000 Output 整点个数 Samp ...

  7. 洛谷P4606 [SDOI2018]战略游戏 【圆方树 + 虚树】

    题目链接 洛谷P4606 双倍经验:弱化版 题解 两点之间必经的点就是圆方树上两点之间的圆点 所以只需建出圆方树 每次询问建出虚树,统计一下虚树边上有多少圆点即可 还要讨论一下经不经过根\(1\)的情 ...

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

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

  9. 【洛谷P1352】没有上司的舞会

    [洛谷P1352]没有上司的舞会 x舷售 锚」翅θ 但是 拙臃 蓄ⅶ榔 暄条熨卫 翘ヴ馇 表现无愧于雪月工作室的核心管理 爸惚扎掬 颇瓶 芟缆肝 貌痉了 洵┭笫装 嗝◇裴腋 褓劂埭 ...

随机推荐

  1. R in action读书笔记(19)第十四章 主成分和因子分析

    第十四章:主成分和因子分析 本章内容 主成分分析 探索性因子分析 其他潜变量模型 主成分分析(PCA)是一种数据降维技巧,它能将大量相关变量转化为一组很少的不相关变量,这些无关变量称为主成分.探索性因 ...

  2. 触发器deleted 表和 inserted 表详解

    摘要:触发器语句中使用了两种特殊的表:deleted 表和 inserted 表. create trigger updateDeleteTimeon userfor updateasbegin  u ...

  3. Android(java)学习笔记195:ContentProvider使用之添加数据到联系人(掌握)

    1.添加联系人逻辑思路 (1)首先在raw_contacts创建一个新的id (2)在data表里面添加这个id对应的数据 2.下面通过一个案例,说明一下如何添加一条数据到联系人: (1)首先我们关注 ...

  4. bootstrap 整理

    form-control   输入框,独占一行,占满. form-group  增加下边距 <label> 字体会加粗,不是独占一行 .col-xs-  超小屏 .col-sm- 平板 . ...

  5. A10. JVM 对象

    [概述] 首先需要了解对象在内存中的存储布局,其次需要了解对对象的访问定位. [对象的内存布局] 在 HotSpot 虚拟机中,对象在内存中存储的布局可以分为 3 块区域:对象头(Header).实例 ...

  6. python 删除/查找重复项

    l = [1,2,3,2,1] # l = ['你','我','他','她','你'] for i in l: print("the %s has found %s" % (i, ...

  7. seq2seq(1)- EncoderDecoder架构

    零 seq2seq是从序列到序列的学习过程,最重要的是输入序列和输出序列是可变长的,这种方式就非常灵活了,典型的机器翻译就是这样一个过程. 一 最基本的seq2seq网络架构如下所示: 可以看到,en ...

  8. android 获取application和activity下meta-data中的值

    meta-data在AndroidManifest中是以键值对的形式存在的,可以将meta-data放在application根节点下,也可以放在某个activity节点下.因为存放的位置不同,因此获 ...

  9. [Python3网络爬虫开发实战] 1.8.3-Scrapy-Splash的安装

    Scrapy-Splash是一个Scrapy中支持JavaScript渲染的工具,本节来介绍它的安装方式. Scrapy-Splash的安装分为两部分.一个是Splash服务的安装,具体是通过Dock ...

  10. Django 模版语法 二

    变量的过滤器(filter)的使用 过滤器:upper, lower, first, capfirst 在 views.py 中修改 from django.shortcuts import rend ...