【贪心】[USACO2016 金组]Circular Barn
来源:互联网 发布:js 0开头数字转字符串 编辑:程序博客网 时间:2024/05/16 15:50
题目描述
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…n1…n around the perimeter of the barn (3≤n≤100,0003≤n≤100,000). 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 cici cows line up outside the door to room ii, so ∑ci=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 d2d2 energy, please determine the minimum amount of energy needed to distribute the cows so one ends up in each room.
INPUT FORMAT (file cbarn.in):
The first line of input contains nn. Each of the remaining nn lines contain c1…cnc1…cn.
OUTPUT FORMAT (file cbarn.out):
Please write out the minimum amount of energy consumed by the cows.
SAMPLE INPUT:
10
1
0
0
2
0
0
1
2
2
2
SAMPLE OUTPUT:
33
题目大意,给定一个环长为n的环上每个点上有
题目解析
可以发现我们如果需要移动
代码
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>using namespace std;const int MAXN = 100000;int a[MAXN+10], c[MAXN+10];char ch;void Read(int &s){ while((ch=getchar()),ch<'0'||ch>'9'); s = ch-'0'; while((ch=getchar()),ch>='0'&&ch<='9') s = s*10+ch-'0'; ungetc(ch, stdin);}int main(){ int n; Read(n); for(int i=1;i<=n;i++){ Read(c[i]); a[i] = i; } queue<int> que; long long ans = 0; for(int i=1;i<=n;i++){ if(!que.empty()){ int u = que.front(); que.pop(); ans += 1LL*(i-u)*(i-u); a[i] = u; c[i] ++; } for(;c[i]>1;c[i]--) que.push(i); } int tn = n*2; for(int i=n+1;i<=tn;i++){ if(!que.empty()){ if(c[i-n]){ que.push(a[i-n]+n); ans -= 1LL*(i-n-a[i-n])*(i-n-a[i-n]); } int u = que.front(); que.pop(); ans += 1LL*(i-u)*(i-u); } } printf("%lld\n", ans); return 0;}
- 【贪心】[USACO2016 金组]Circular Barn
- 【动态规划】[USACO2016 金组]Circular Barn Revisited
- BZOJ4409: [Usaco2016 Feb]Circular barn
- BZOJ 4412/Usaco2016 Feb Circular Barn(构造)
- [BZOJ 4412] [Usaco2016 Feb]Circular Barn
- [BZOJ 4409] [Usaco2016 Feb]Circular barn
- 【贪心】[USACO 2015 February Contest, Gold]Circular Barn
- bzoj4412 Circular Barn 构造
- 洛谷 P3137 [USACO16FEB]圆形谷仓Circular Barn
- USACO/Barn Repair(贪心)
- USACO Barn Repair----贪心
- usaco Barn Repair (贪心)
- USACO 1.3 - Barn Repair(贪心)
- 1.3.2 Barn Repair【贪心】
- 【DP】[USACO 2016 February Contest, Gold]Circular Barn Revisited
- USACO 2016 February Contest, Bronze Problem 2. Circular Barn
- USACO 2016 Feb Circular Barn 二分+树状数组
- 【BZOJ4582】【Usaco2016 open】Diamond Collector 贪心
- Python简单介绍
- Java程序员应该知道的10个Eclipse调试技巧
- Java中处理异常(try/catch)的快捷键
- lamp环境快速搭建
- Qt中文乱码
- 【贪心】[USACO2016 金组]Circular Barn
- JSP 介绍
- 【Music】横屏歌词显示效果不好
- 蓝桥杯 地宫寻宝
- linux sendfile示例
- [从头学数学] 第135节 整式的乘法与因式分解 小结与复习题
- java.util.Timer类可以实现多线程一样的功能
- 如何使用vi处理大文件(GB级别)?
- react-js-04-组件