[POJ1852] Ants(思维题)
题干
An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know 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
The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers 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
For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.
Example
In:
2
10 3
2 6 7
214 7
11 12 7 13 176 23 191
Out:
4 8
38 207
Ideas
题意大致概括为:
有n只蚂蚁在木棍上爬行,每只蚂蚁的速度都是每秒1单位长度,现在给你所有蚂蚁初始的位置(蚂蚁运动方向未定),蚂蚁相遇会掉头反向运动,让你求出所有蚂蚁都掉下木棍的最短时间和最长时间。
因为能掉头,数据又足足一百万,暴搜显然是不现实的。那么怎么处理这个掉头的问题呢?让我们画个图来理解一下,看完你就会发现这都是吓唬人的。

我们以找最长时间为例,最长的时间可能出现在谁身上呢?通过看图我们知道1号蚂蚁早早下场了,所以最长时间只能出现在2或3身上。2号的路径为2、4、6(标红部分);3号的路径为3、5、7,那么,由于蚂蚁们的速度都相同,我们会发现一个很神奇的规律:
路径1、2、3相等,路程4、5相等,但注意路程6、7不一定相等。所以呢,3号的路程可以写成1、4、7,刚好是1号蚂蚁到右端的距离对不对?同理,2号蚂蚁的路程也可以表示为3,5,6,刚好是3号蚂蚁到左端的距离对不对?以此类推,我们发现其实1,2,3号蚂蚁每个蚂蚁掉下去所走的总距离(即总时间),都能用原来的某一个蚂蚁到两端的距离表示出来(其实第二行中1号蚂蚁到左端还能走一段,但我不想改图了…),无论初始时各个蚂蚁的初方向怎样,规律是不变的。那么当蚂蚁的数量再多一点,这个规律还适用吗?
答案是肯定的:


大家可以试一试,规律依然是适用的。
道理懂了,那么这道题就是白给了。我们直接遍历每个蚂蚁到两端的距离,根据需要取较小或较大的那个,再在所有蚂蚁中找到一个最大的(因为要求所有蚂蚁都掉下去),就是我们的答案。
ps:这题不用开longlong,一百万的数据看着很吓人,但因为没有累加,int就够用了
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1000000+10;
int a[maxn],len,n;
int main(){
int t;
cin>>t;
while(t--){
memset(a,0,sizeof(a));
len=0;
scanf("%d%d",&len,&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
int minn=0,maxx=0;
for(int i=1;i<=n;i++){
//minn=min(minn,min(a[i],len-a[i])); 注意千万不要这么写,因为我们要保证所有蚂蚁都掉下去,所以括号外是取max的
minn=max(minn,min(a[i],len-a[i]));
maxx=max(maxx,max(a[i],len-a[i]));
}
printf("%d %d\n",minn,maxx);
}
}
[POJ1852] Ants(思维题)的更多相关文章
- UVA.10881 Piotr's Ants (思维题)
UVA.10881 Piotr's Ants (思维题) 题意分析 有一根长度为L cm的木棍,上有n只蚂蚁,蚂蚁要么向左爬,要么向右,速度均为1cm/s,若2只蚂蚁相撞,则蚂蚁同时调头.求解第T秒时 ...
- poj1852 Ants ——想法题、水题
求最短时间和最长时间. 当两个蚂蚁相遇的时候,可以看做两个蚂蚁穿过,对结果没有影响.O(N)的复杂度 c++版: #include <cstdio> #define min(a, b) ( ...
- 思维题 UVA 10881 Piotr's Ants
题目传送门 /* 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态 思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了 关键2:蚂蚁的相对位置不变 关键3:o ...
- zoj 3778 Talented Chef(思维题)
题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...
- cf A. Inna and Pink Pony(思维题)
题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)
思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记
PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
随机推荐
- repo 导出本地 git tag 给他人
背景 使用 repo 管理了多个 git 仓库,有时需要将本地仓库的tag同步给其他人,但又不能直接推到远程(例如权限问题). 实际场景举例 本地复现了一个问题,需要让其他人回退到相同环境来排查. 本 ...
- php symfony/var-dumper 打印插件
$records = array( array( 'id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe', ), arr ...
- chattr +i 用户也没法随意删除
root用户也没法用rm随意删除文件? 前言 在你的印象中,是不是root用户就可以为所欲为呢?随便一个rm -rf *,一波骚操作走人?可能没那么容易. 先来个示例,创建一个文本文件test.t ...
- 拉勾网 + selenium
目录 方式一 selenium 方式二普通方法 方式一 selenium from selenium import webdriver import time from selenium.webdri ...
- 利用 Powershell 编写简单的浏览器脚本
生活中有很多事情是低效益,重复性.比如每天上某些网站,先登录再签到打卡,比如每隔一段时间清理回收站的文件等等.一个成熟的软件工程师应该想到用软件解决他. 对于这些简单的小任务,一般用脚本实现.比如Py ...
- @uoj - 164@ 【清华集训2015】V
目录 @description@ @solution@ @accepted code@ @details@ @description@ Picks博士观察完金星凌日后,设计了一个复杂的电阻器.为了简化 ...
- matlab 梯度法(最速下降法)
norm(A,p)当A是向量时norm(A,p) Returns sum(abs(A).^zhip)^(/p), for any <= p <= ∞.norm(A) Returns nor ...
- gradle的搭建和配置
下载 官方网站:https://gradle.org/install/#manually 提供了两种下载方式,Binary-only是只下载二进制源码,Complete, with docs and ...
- SpringBoot中注入ApplicationContext对象的三种方式
[本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 在项目中,我们可 ...
- 爬取B站弹幕并且制作词云
目录 爬取弹幕 1. 从手机端口进入网页爬取找到接口 2.代码 制作词云 1.文件读取 2.代码 爬取弹幕 1. 从手机端口进入网页爬取找到接口 2.代码 import requests from l ...