hdu 4223 Dynamic Programming? (dp)

//连续的和的绝对值最小
# include <stdio.h>
# include <string.h>
# include <algorithm>
# include <math.h>
using namespace std;
int main()
{
    int t,i,j,num,n,min1;
    int sum[1010],a[1010];
    int cas=0;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d",&n);
            for(i=0; i<n; i++)
                scanf("%d",&a[i]);
            min1=999999999;
            for(i=0; i<n; i++)
            {
                sum[i]=abs(a[i]);
                num=a[i];
                for(j=i-1; j>=0; j--)
                {
                    num=num+a[j];
                    if(sum[i]>abs(num))
                        sum[i]=abs(num);
                }
                if(min1>sum[i])
                    min1=sum[i];
            }
            printf("Case %d: %d\n",++cas,min1);
        }
    }
    return 0;
}

posted @ 2017-08-13 15:52  lytwajue  阅读(308)  评论(0编辑  收藏  举报