HDU 4223 Dynamic Programming?(dp)

题意:

思路:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int a[200];
int dp1[1000+10];
int dp2[1000+10];
int main()
{
    int t,n;
    int i,j,k;
    int ans;
    int cas=1;
    scanf("%d",&t);
    while(t--)
    {
        ans=1e7;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            if(ans>abs(a[i])) ans=abs(a[i]);
        }
        for(i=0;i<n;i++)
        {
            dp1[i]=abs(a[i]);
            dp2[i]=a[i];
            for(j=i+1;j<n;j++)
            {
                dp2[j]=dp2[j-1]+a[j];
                dp1[j]=min(abs(dp2[j-1]+a[j]),abs(a[j]));
                if(dp1[j]<ans) ans=dp1[j];
            }
        }
        printf("Case %d: %d\n",cas++,ans);
    }
    return 0;
}

 

posted @   sola94  阅读(173)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示