《挑战程序设计竞赛》1.6 轻松热身 POJ1852
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 12782 | Accepted: 5596 |
Description
the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.
Input
giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace.
Output
time.
Sample Input
2
10 3
2 6 7
214 7
11 12 7 13 176 23 191
Sample Output
4 8
38 207
Source
思路:
大意是蚂蚁在杆子上爬行,方向未知,如果相遇时会各自反向爬回去。计算所有蚂蚁落下杆子所需的最短时间和最长时间。
穷竭算法比较容易想到,枚举所有蚂蚁初始朝向的组合然后验证,复杂度为2^N,显然太高了。
其实这个题有比较巧的思路,蚂蚁相遇时两只蚂蚁若交换,就好像两个蚂蚁仍然按照自己原来的路线走一样。所以压根不需要考虑相遇而进行多次模拟。
那么其实只要检查两端的两只蚂蚁即可。
代码:
Problem: 1852 User: liangrx06
Memory: 740K Time: 141MS
Language: G++ Result: Accepted
Source Code
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; int main(void)
{
int t, n, len;
int pos, minT, maxT; cin >> t;
while (t--)
{
cin >> len >> n;
maxT = 0;
minT = 0;
while (n--)
{
scanf("%d", &pos);
maxT = max(maxT, max(pos, len-pos));
minT = max(minT, min(pos, len-pos));
}
printf("%d %d\n", minT, maxT);
} return 0;
}
《挑战程序设计竞赛》1.6 轻松热身 POJ1852的更多相关文章
- Aizu 2249Road Construction 单源最短路变形《挑战程序设计竞赛》模板题
King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazin ...
- 《挑战程序设计竞赛》2.3 动态规划-优化递推 POJ1742 3046 3181
POJ1742 http://poj.org/problem?id=1742 题意 有n种面额的硬币,面额个数分别为Ai.Ci,求最多能搭配出几种不超过m的金额? 思路 据说这是传说中的男人8题呢,对 ...
- 挑战程序设计竞赛》P345 观看计划
<挑战程序设计竞赛>P345 观看计划 题意:一周一共有M个单位的时间.一共有N部动画在每周si时 ...
- POJ 2386 Lake Counting 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...
- poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...
- 《挑战程序设计竞赛》 4.1.1 矩阵 P286
想写几篇挑战的感悟,也有助于自己理解这本书.但这上面大多贴的是书上的代码,主要是为了用的时候后直接复制就好了,这样就很方便了,就相当于黑盒模板了. 1.线性方程组 /** \brief 高斯消元法 * ...
- poj1182食物链_并查集_挑战程序设计竞赛例题
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65534 Accepted: 19321 Description ...
- 迷宫问题_BFS_挑战程序设计竞赛p34
给定一个N*M的迷宫,求从起点到终点的最小步数. N,M<100: 输入: 10 10#S######.#......#..#.#.##.##.#.#........##.##.####.... ...
- 【网络流#9】POJ 2135 Farm Tour 最小费用流 - 《挑战程序设计竞赛》例题
[题意]给出一张无向图,从1开始到n,求两条没有公共边的最短路,使得路程总和最小 每条边的权值设为费用,最大流量设为1,然后就是从源点到汇点流量为2的最小费用流. 因为是规定了流量,新建一个源点和一个 ...
随机推荐
- SVN学习(二)——SVN 提交、更新、解决冲突等操作步骤
1. 纳入版本控制 ①新建文件abc.txt ②在文件上点右键 ③添加后文件图标发生变化 2. 提交 ①使用TortoiseSVN可以提交具体某一个文件,或某一个目录下的所有改变.方法就是在想要提交的 ...
- android-数据库SQLite相关
android平台下的SQLite数据库是一种轻量级数据库,支持标准的SQL语句. 本文将介绍 android数据库的创建 利用sql语句对数据库增删改查 系统api数据库增删改查 数据库的事务 1, ...
- http Referrer-Policy
Referrer-Policy: no-referrer Referrer-Policy: no-referrer-when-downgrade Referrer-Policy: origin Ref ...
- Mac 上的传奇效率神器 Alfred 3
下载地址:https://www.alfredapp.com/ 第三方教程:https://www.jianshu.com/p/e9f3352c785f 一.内置快捷键介绍 1.默认快捷呼出热键是: ...
- Silverlight实例教程 - Validation客户端同步数据验证(转载)
摘要:在Silverlight 4中,Silverlight Validation有相对的改进,本篇将介绍Silverlight 4中新加入的验证机制功能,IDataErrorInfo客户端同步验证机 ...
- linux-centos7中lnmp服务器编译安装含systemctl启动service(转)
centos7 nginx mysql php 可以分开安装 然后在配置nginx互php的 先安装一些必要的库 ---------------------------------------- ...
- unity, 烘焙lightmap
1,建一个名为_scene的场景,放一个球体. 2,将球体的Static属性勾选. 3,将默认光源Directional light的Baking属性改为Baked. 4,打开Window->L ...
- Atitit.输入法配置说明v1 q229
Atitit.输入法配置说明v1 q229 //------------------------------------------------------ // IME设置 //-------- ...
- 【HDU-5246】超级赛亚ACMer(贪心)
之前用了个nlogn的算法超时了.仅仅能改成n的算法了 大题贪心思路就是 对每一个人的能力值从小到大进行排序,当前能力值为now,那么我们找到一个人的能力使得这个能力值 <= now.now + ...
- C++语言基础(16)-string类
使用 string 类需要包含头文件<string>,下面的例子介绍了几种定义 string 变量(对象)的方法: #include <iostream> #include & ...