X-man

导航

hdu 4223

暴力:

Problem : 4223 ( Dynamic Programming? )     Judge Status : Accepted
RunId : 9293515    Language : C++    Author : yudunfengqing
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define INF 0xfffffff
int a[1010][1010];
int b[1010];
int main()
{
    int t,n;
    int i,j,k;
    scanf("%d",&t);
    int _case=0;
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%d",&b[i]);
        int Min=INF;
        //for()
        for(i=0;i<n;i++)
        {
            a[i][i]=b[i];
            Min=min(abs(a[i][i]),abs(Min));
            for(j=i+1;j<n;j++)
            {
                a[i][j]=a[i][j-1]+b[j];
                Min=min(abs(a[i][j]),abs(Min));
            }
        }
        printf("Case %d: %d\n",++_case,abs(Min));
    }
}

 

预处理:

Problem : 4223 ( Dynamic Programming? )     Judge Status : Accepted
RunId : 9293542    Language : C++    Author : yudunfengqing
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include <iostream>
 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #include<stdlib.h>
 #include<cmath>
 using namespace std;
 #define LL __int64
 #define INF 0xfffffff
 int s[1010];
 int a[1010];
 int main()
 {
     int i,j,k,n,kk=0,t;
     cin>>t;
     while(t--)
     {
         cin>>n;kk++;
         memset(s,0,sizeof(s));
         for(i = 1; i <= n ; i++)
         {
             scanf("%d",&a[i]);
             s[i] = s[i-1]+a[i];
         }
         int minz=INF;
         for(i = 1; i <= n ; i++)
             for(j = 0; j < i ; j++)
             {
                 int x = abs(s[i]-s[j]);
                 minz = min(minz,x);
             }
         printf("Case %d: ",kk);
         cout<<minz<<endl;
     }
     return 0;
 }
View Code

 

posted on 2013-10-07 15:38  雨钝风轻  阅读(308)  评论(0编辑  收藏  举报