PAT 1046. Shortest Distance

1046. Shortest Distance (20)

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

Input Specification:

Each input file contains one test case. For each case, the first line contains an integer N (in [3, 105]), followed by N integer distances D1D2... DN, where Diis the distance between the i-th and the (i+1)-st exits, and DNis between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (<=104), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 107.

Output Specification:

For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

Sample Input:

5 1 2 4 14 9

3

1 3

2 5

4 1

Sample Output:

3

10

7


思路:以dis[i]表示1號(hào)節(jié)點(diǎn)按順時(shí)間方向到達(dá)“i號(hào)節(jié)點(diǎn)順時(shí)鐘方向的下一個(gè)節(jié)點(diǎn)”的距離*(1<<i<<N)于是對(duì)每個(gè)查詢left---right,其結(jié)果就是dis(left, right)與sum - dis(left, right)中的最小值。

關(guān)鍵點(diǎn)

此題如果沒有經(jīng)過預(yù)處理dis數(shù)組(把dis數(shù)組做成一個(gè)累加數(shù)組)和sum的做法很容易超時(shí)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 昨天偶然看到了無戒老師的365日更訓(xùn)練營(yíng)挑戰(zhàn),毫不猶豫想要立馬報(bào)個(gè)名。班長(zhǎng)大人說,報(bào)名了就得更文,想想當(dāng)時(shí)沒有半點(diǎn)...
    大Ora小姐閱讀 400評(píng)論 4 11
  • 今天開始學(xué)習(xí)本學(xué)期第四組課文,前言部分是這樣的:有這樣一本書—書中沒有一個(gè)字,卻處處都是學(xué)問;書上沒有作者的姓名,...
    木木屋的故事閱讀 668評(píng)論 0 1
  • 夏日炎炎,繼“心理學(xué)系列”結(jié)束,開啟“自我提升”與“優(yōu)勢(shì)計(jì)劃”系列閱讀。 6.24. 《槍炮、鋼鐵與病菌》(上) ...
    陌上花開wen閱讀 1,399評(píng)論 0 3
  • 黃蘆苦,黃蘆苦,凄風(fēng)冷雨霜兒樹 飛光璞,飛光璞,赭衣帳前魂銷骨 我自遠(yuǎn)方,手秉一盞干燭 來去草莽,進(jìn)退幾分尺度
    檸沐閱讀 228評(píng)論 0 1
  • 這篇文章一如既往還是抄的。特意挑了一篇超簡(jiǎn)單的,里面命令都是最常用的,完全能解決日常使用。什么安裝就不說了。誰(shuí)讓我...
    To_Be_Better閱讀 143評(píng)論 0 0

友情鏈接更多精彩內(nèi)容