BZOJ-1621-[Usaco2008 Open]Roads Around The Farm分岔路口
来源:互联网 发布:ubuntu iptables启用 编辑:程序博客网 时间:2024/05/01 07:28
Description
约翰的N(1≤N≤1,000,000,000)只奶牛要出发去探索牧场四周的土地.她们将沿着一条路走,一直走到三岔路口(可以认为所有的路口都是这样的).这时候,这一群奶牛可能会分成两群,分别沿着接下来的两条路继续走.如果她们再次走到三岔路口,那么仍有可能继续分裂成两群继续走. 奶牛的分裂方式十分古怪:如果这一群奶牛可以精确地分成两部分,这两部分的牛数恰好相差K(1≤K≤1000),那么在三岔路口牛群就会分裂.否则,牛群不会分裂,她们都将在这里待下去,平静地吃草. 请计算,最终将会有多少群奶牛在平静地吃草.
Input
两个整数N和K.
Output
最后的牛群数.
Sample Input
6 2
INPUT DETAILS:
There are 6 cows and the difference in group sizes is 2.
Sample Output
3
OUTPUT DETAILS:
There are 3 final groups (with 2, 1, and 3 cows in them).
6
/ \
2 4
/ \
1 3
HINT
6只奶牛先分成2只和4只.4只奶牛又分成1只和3只.最后有三群奶牛.
题解:
维护一个队列,首先将N放入,每次对于队首的tmp,若是(tmp-k)%2==0,则分别将(tmp-k)/2+k和(tmp-k)/2入队,否则Out++。
最后输出Out。
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <string>#include <queue>#include <vector>#include <algorithm>#include <cmath>using namespace std;long long int n,k;int main(){ long long int out=0,tmp; cin >> n >> k; queue<long long int>q; q.push(n); while(!q.empty()) { tmp=q.front(); q.pop(); if(tmp<=k || (tmp-k)%2) out++; else { q.push((tmp-k)/2); q.push((tmp-k)/2+k); } } cout << out << endl; return 0;}
0 0
- BZOJ 1621: [Usaco2008 Open]Roads Around The Farm分岔路口
- BZOJ 1621: [Usaco2008 Open]Roads Around The Farm分岔路口
- 【BZOJ 1621】 [Usaco2008 Open]Roads Around The Farm分岔路口
- BZOJ-1621-[Usaco2008 Open]Roads Around The Farm分岔路口
- BZOJ 1621: [Usaco2008 Open]Roads Around The Farm分岔路口
- BZOJ 1621 [Usaco2008 Open]Roads Around The Farm分岔路口 模拟
- [BZOJ1621][Usaco2008 Open]Roads Around The Farm分岔路口
- BZOJ1621: [Usaco2008 Open]Roads Around The Farm分岔路口
- 1621: [Usaco2008 Open]Roads Around The Farm分岔路口(记忆化搜索)
- bzoj1621【Usaco2008 Open】Roads Around The Farm
- [Usaco08Open] Roads Around The Farm
- BZOJ 1605 [Usaco2008 Open]Crisis on the Farm 牧场危机 DP
- 麻烦的DP-BZOJ-1605-[Usaco2008 Open]Crisis on the Farm 牧场危机
- bzoj 1605: [Usaco2008 Open]Crisis on the Farm 牧场危机(DP)
- luogu2907 [USACO08OPEN]农场周围的道路Roads Around The Farm
- [Usaco2008 Open]Crisis on the Farm 牧场危机
- BZOJ 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场
- Bzoj 1619 [Usaco2008 Nov]Guarding the Farm 保卫牧场
- Android总结 - 网络请求总结
- iOS单元测试GHUnit + OCMock(不使用cocoapods版)
- 单例设计模式
- linux关于bashrc与profile的区别(转)
- dfs总结
- BZOJ-1621-[Usaco2008 Open]Roads Around The Farm分岔路口
- 查缺补漏
- break,continue,return in function
- 【个人记录】使APP在user版本隐藏图标,在eng版本显示图标
- 【剑指offer-Java版】30最小的K个数
- iOS中UIWebView中添加长按和双击事件
- Unity中鼠标左键按下,拖拽物体移动
- iOS重绘机制drawRect
- Java代码通过JDBC连接Hiveserver2