[CF803B] Distances to Zero(模拟)
题目链接:http://codeforces.com/contest/803/problem/B
题意:给n个数,问每个数和最近的0的位置的最短距离。
正着来一遍,倒着来一遍。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 200200; 5 int a[maxn]; 6 int dp[maxn]; 7 int n; 8 9 int main() { 10 // freopen("in", "r", stdin); 11 while(~scanf("%d", &n)) { 12 memset(dp, 0x7f, sizeof(dp)); 13 for(int i = 1; i <= n; i++) { 14 scanf("%d", &a[i]); 15 } 16 int cnt; 17 int i = 1; 18 while(a[i] != 0) i++; 19 cnt = i; 20 for(; i <= n; i++) { 21 if(a[i] == 0) { 22 dp[i] = 0; 23 cnt = i; 24 } 25 else dp[i] = min(dp[i], abs(cnt-i)); 26 } 27 i = n; 28 while(a[i] != 0) i--; 29 cnt = i; 30 for(; i >= 1; i--) { 31 if(a[i] == 0) cnt = i; 32 else dp[i] = min(dp[i], abs(cnt-i)); 33 } 34 for(int i = 1; i <= n; i++) printf("%d ", dp[i]); 35 printf("\n"); 36 } 37 return 0; 38 }