BZOJ2620 [Usaco2012 Mar]Haybale Restacking
恩,非常好的题。。。至少思路非常巧妙
首先可以得到性质:对于相邻的两堆A & B,A给B然后B再给A是完全没有意义的。。。也就是说只能单向传递
然后我们记下每个点要给(被给)多少堆干草a[i]
同时可以计算出del[i],表示若第i堆只向右传且第n堆不向第1堆运任何干草的情况下i - 1向i传递干草的数量
del[i] = del[i - 1] + a[i - 1](其实就是前缀和)
现在1可以向右移了,设向右移x堆,则ans = Σabs(del[i] - x)
故x = mid(del + 1, del + n + 1)时,ans最小
更加详细见lrj白书P4。。。
/**************************************************************
Problem: 2620
User: rausen
Language: C++
Result: Accepted
Time:68 ms
Memory:1588 kb
****************************************************************/ #include <cstdio>
#include <algorithm> using namespace std;
typedef long long ll;
const int N = ;
int n;
int a[N], del[N];
ll ans; inline int read(){
int x = , sgn = ;
char ch = getchar();
while (ch < '' || ch > ''){
if (ch == '-') sgn = -;
ch = getchar();
}
while (ch >= '' && ch <= ''){
x = x * + ch - '';
ch = getchar();
}
return sgn * x;
} int main(){
n = read();
int i, m = (n + ) >> ;
for (i = ; i <= n; ++i)
a[i] = read(), a[i] -= read();
for (i = ; i <= n; ++i)
del[i] = a[i - ] + del[i - ];
del[] = a[n] + del[n];
sort(del + , del + n + );
for (i = ; i <= n; ++i)
ans += abs(del[m] - del[i]);
printf("%lld\n", ans);
return ;
}
BZOJ2620 [Usaco2012 Mar]Haybale Restacking的更多相关文章
- 2620: [Usaco2012 Mar]Haybale Restacking
2620: [Usaco2012 Mar]Haybale Restacking Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 201 Solved: ...
- 动态规划(状态压缩):BZOJ 2621 [Usaco2012 Mar]Cows in a Skyscraper
2621: [Usaco2012 Mar]Cows in a Skyscraper Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 303 Sol ...
- BZOJ2621 [Usaco2012 Mar]Cows in a Skyscraper
首先比较容易想到是状态压缩DP 令$f[S]$表示选取了集合$S$以后,已经送了最少次数$cnt$且当前电梯剩下的体积$rest$最大(即$f[S]$是一个二元组$(cnt, rest)$) 于是$f ...
- bzoj2621: [Usaco2012 Mar]Cows in a Skyscraper(状压DP)
第一眼是3^n*n的做法...然而并不可行T T 后来发现对于奶牛的一个状态i,最优情况下剩下那个可以装奶牛的电梯剩下的可用重量是一定的,于是我们设f[i]表示奶牛状态为i的最小电梯数,g[i]为奶牛 ...
- 洛谷3258:[USACO2012 MAR]Flowerpot 花盆——题解
https://www.luogu.org/problemnew/show/P2698#sub 老板需要你帮忙浇花.给出N滴水的坐标,y表示水滴的高度,x表示它下落到x轴的位置. 每滴水以每秒1个单位 ...
- BZOJ-USACO被虐记
bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- USACO March. 2012
Connect the Cows Times17 水题 Landscaping Flowerpot Tractor 广搜 搜到边界就可以终止了 没什么难度 #include <stdio.h&g ...
- USACO2012 Haybale stacking /// 区间表示法 oj21556
题目大意:N个方块 标号1~N K个操作 操作a b 表示标号a~b区间每位多加一个方块 Input * Line 1: Two space-separated integers, N K. * ...
随机推荐
- ubuntu安装conda
https://blog.csdn.net/menghuanbeike/article/details/79138651 你需要前往Anaconda的官网看下目前的下载地址: https://www. ...
- android 错误收集
2. is not translated in Eclipse > Preference > Android > Lint Error Checking的Correctness: M ...
- centos 目录文件管理 mkdir,rm,touch,误删文件extundelete,cp,mv,cat,more,less ,head,tail,chown,chmod ,umask 第四节课
centos 目录文件管理 mkdir,rm,touch,误删文件extundelete,cp,mv,cat,more,less ,head,tail,chown,chmod ,umask 第四节课 ...
- google浏览器插件安装
1:安装本地插件,直接将下载好的crx插件拖入到 chrome://extensions/ 的空白处 http://www.cnplugins.com/tool/outline-instal ...
- HashSet、HashMap、Hashtable、TreeMap循环、区别
HashSet 循环 //可以为null HashSet<Object> hashSet =new HashSet<Object>(); hashSet.add(1); has ...
- mongo数据库连接工具类(C#)
Framework版本:.Net Framework 4 using System; using System.Collections.Generic; using System.Linq; usin ...
- AVAudioFoundation(5):音视频导出
本文转自:AVAudioFoundation(5):音视频导出 | www.samirchen.com 本文主要内容来自 AVFoundation Programming Guide. 要读写音视频数 ...
- 裸眼3D全攻略3:拍摄3D—瞳距、镜距、视角偏转与空间感
http://sd89.blog.163.com/blog/static/356041322014112532958728/ 3D图片的拍摄,与平面有着全新的不同要求,那就是空间感的表现. 简单来说, ...
- ZLYD团队第5周项目总结
ZLYD团队第5周项目总结 项目进展 目前游戏人没有成功运行.初步判断是部分代码有误. 我们采用了两种运行方式,代码未出现明确错误.但问题可能是由于版本问题. 将Wall.java.Gold.java ...
- 实时刷新winform中的某一个控件上的文字
需要注意的是,必须从UI线程,另外启动一个线程才可以. 在新线程调用异步刷新就OK了 Thread thread; private void button1_Click(object sender, ...