【bzoj4412】[Usaco2016 Feb]Circular Barn

先看成一条链

for一遍找位置

在for一遍算答案

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long LL;

#define N 100010

int n;
int cnt=1;

int a[N<<2],s[N<<2];

int main()
{
freopen("a.in","r",stdin);freopen("a.out","w",stdout);
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*2;i++)
{
if (i==cnt+n)
break;
s[i]=s[i-1]+a[i]-1;
while (s[i]<s[cnt-1])
cnt++;
}
LL ans=0;
int l=cnt+n-1,r=cnt+n-1;
while (r>=cnt)
{
while (!a[l])
l--;
ans+=(LL)(r-l)*(r-l);
a[l]--;
r--;
}
printf("%lld\n",ans);
return 0;
}

时间: 2025-01-11 10:42:45

【bzoj4412】[Usaco2016 Feb]Circular Barn的相关文章

bzoj 4412: [Usaco2016 Feb]Circular Barn

4412: [Usaco2016 Feb]Circular Barn Description 有一个N个点的环,相邻两个点距离是1.点顺时针标号为1..N.每一个点有ci头牛,保证∑ci=N.每头牛都可以顺时针走.设一头牛走了d个单位停下了,将耗费d^2的能量.请设计一种牛的走法,使得每一个点上都正好有一头牛,且最小化耗费的能量. Input 第一行一个数N.N <= 100000接下来N行,每行一个数ci. Output 输出一个数表示耗费能量的最小值 Sample Input 10 1 0

【bzoj3940】[Usaco2015 Feb]Censoring

[题目描述] FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S.他有一个包含n个单词的列表,列表里的n个单词 记为t_1...t_N.他希望从S中删除这些单词. FJ每次在S中找到最早出现的列表中的单词(最早出现指该单词的开始位置最小),然后从S中删除这个单词.他重复这个操作直到S中 没有列表里的单词为止.注意删除一个单词后可能会导致S中出现另一个列表中的单词 FJ注意到列表中的单词不会出现一个单词是另一个单词子串的情况,这意味着每个列表中的单词在S中出现的开始位置是

【bzoj1733】[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+网络流最大流

题目描述 Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within his farm and needs to be able to get to the machine without being detected. He must make a total of T (1 <= T <=

树的直径 【bzoj3363】[Usaco2004 Feb]Cow Marathon 奶牛马拉松

3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 Description ? 最近美国过度肥胖非常普遍,农夫约翰为了让他的奶牛多做运动,举办了奶牛马拉松.马拉 松路线要尽量长,所以,告诉你农场的地图(该地图的描述与上题一致),请帮助约翰寻找两个 最远农场间的距离. Input ? 第1行:两个分开的整数N和M. ? 第2到M+1行:每行包括4个分开的内容,Fi,F2,L,D分别描述两个农场的编号,道路的长 度,F1到F2的方向N,E,S,W. Output ? 一个

【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp

题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but this has

【bzoj1782】[Usaco2010 Feb]slowdown 慢慢游

题目描述 每天Farmer John的N头奶牛(1 <= N <= 100000,编号1-N)从粮仓走向他的自己的牧场.牧场构成了一棵树,粮仓在1号牧场.恰好有N-1条道路直接连接着牧场,使得牧场之间都恰好有一条路径相连.第i条路连接着A_i,B_i,(1 <= A_i <= N; 1 <= B_i <= N).奶牛们每人有一个私人牧场P_i (1 <= P_i <= N).粮仓的门每次只能让一只奶牛离开.耐心的奶牛们会等到他们的前面的朋友们到达了自己的私人

【bzoj4994】[Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组

题目描述 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 样例输入 4 3 2 4 4 1 3 2 1 样例输出 3 题解 树状数组 WH说是CDQ分治直接把我整蒙了... 把所有数按照第一次出现位置从小到大排序,然后扫一遍.此时一定是满足$a_j>a_i$的. 那么只需要求出$(a_j,b_j)$中以前出现过的$b_i$的数目.使用树状数组维护即可. 时间复杂度$O(n\log n)$ #include &l

【SummaryPlan】Summary of Feb &amp; Plan of March——How to &#39;just do it&#39;?

Why I choose to be a graduate student from an undergraduate student? It’s time to applying for internships. Recalling this time last year, how I beg for an ideal job. A job means the way of life, not just a screw that change the world, not even a che

【BZOJ3011】[Usaco2012 Dec]Running Away From the Barn 可并堆

[BZOJ3011][Usaco2012 Dec]Running Away From the Barn Description It's milking time at Farmer John's farm, but the cows have all run away! Farmer John needs to round them all up, and needs your help in the search. FJ's farm is a series of N (1 <= N <=