CodeForce-803B Distances to Zero(贪心DP)
Distances to Zero
题意:给定一个数列 a0, a1, ..., an - 1。对于数列中的每一项都要求出与该项最近的0与该项的距离。保证数列中有至少一个0。
#include <cstdio> #include <algorithm> #define maxn 200005 #define inf 0x3f3f3f3f using namespace std; int a[maxn], f[maxn], g[maxn], N; int main() { int i; scanf("%d",&N); for(i=1;i<=N;i++)scanf("%d",a+i); f[0]=inf, g[N+1]=inf; for(i=1;i<=N;i++) { if(a[i]==0)f[i]=0; else f[i]=f[i-1]+1; } for(i=N;i;i--) { if(a[i]==0)g[i]=0; else g[i]=g[i+1]+1; } for(i=1;i<=N;i++)printf("%d ",min(f[i],g[i])); return 0; }