uva 10714 Ants(貪心)

來源:互聯網
上載者:User

題目串連:10714 - Ants


題目大意:一根棍子上有n只螞蟻, 這些螞蟻開始可以任意朝著一個方向移動一直走,向左或是向右,並且移動速度是相同的,但是一旦螞蟻碰到迎面走來的另一隻螞蟻,那麼這兩隻螞蟻都會馬上改變移動方向,朝反方向移動。求所有螞蟻離開木棍的最小和最長時間。


解題思路:其實題目給出的兩隻螞蟻碰頭的可能是迷惑人的,因為開始所有螞蟻都朝一個方向移動,速度又相同,根本不可能追上,所以不用考慮只一點,這樣的話只要分別計算出向左和向右所要花費的最長時間,大的為最長時間,小的為最小時間(可以和起來計算)


#include <stdio.h>int max(const int &a, const int &b) { return a > b ? a : b;}int min(const int &a, const int &b) { return a > b ? b : a;}int main() {    int cas, n, len, Max, Min, dist;    scanf("%d", &cas);    while (cas--) {Max = Min = 0;scanf("%d%d", &len, &n);for (int i = 0; i < n; i++) {    scanf("%d", &dist);    Max = max(Max, max(dist, len - dist));    Min = max(Min, min(dist, len - dist));}printf("%d %d\n", Min, Max);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.