舉個例子,5 1 3 2 -11,先假設答案ans=0表示運送所需的單位,那麼
- 可以看成將第一個人要買的酒移到第二個人,並將ans加上5,ans=5
- 那麼現在變成6 3 2 -11,繼續將第二個人移到第三個人,ans=11
- 變成9 2 -11,一樣移到第四個人,ans=20
- 變成11 -11,移到最後一個人,ans=31,得解
再用example作為例子
- 5 -4 1 -3 1 , ans=0
- 1 1 -3 1 , ans=5(+5)
- 2 -3 1 , ans=6(+5+1)
- -1 1 , ans=8(+5+1+2)
- 0 , ans=9(+5+1+2+1) //因為是運送的單位,記得加絕對值
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <cmath> | |
using namespace std; | |
int a[100001]; | |
int main() | |
{ | |
int n; | |
while(scanf("%d",&n)){ | |
if (n==0) break; | |
for(int i=0;i<n;i++) | |
scanf("%d",&a[i]); | |
long long int ans=0; | |
for(int i=0;i<n-1;i++){ | |
ans += abs(a[i]); | |
a[i+1] += a[i]; | |
} | |
printf("%lld\n",ans); | |
} | |
return 0; | |
} |
沒有留言:
張貼留言