首页 > 代码库 > HDU 1003 Max Sum

HDU 1003 Max Sum

同上题一样,求连续子序列的最大和

而且比上题还要简单一些,用不到long long了

直接水过

 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6  7 const int maxn = 100000 + 10; 8 int a[maxn]; 9 10 struct Node11 {12     int w;13     int l, r;14 }node[maxn];15 16 int main(void)17 {18     #ifdef LOCAL19         freopen("1003in.txt", "r", stdin);20     #endif21 22     int N, kase;23     scanf("%d", &N);24     for(kase = 1; kase <= N; ++kase)25     {26         if(kase > 1)    printf("\n");27         printf("Case %d:\n", kase);28 29         int n;30         scanf("%d", &n);31         int i;32         for(i = 1; i <= n; ++i)33         {34             scanf("%d", &a[i]);35             node[i].w = a[i];36             node[i].l = node[i].r = i;37         }38 39         for(i = 2; i <= n; ++i)40         {41             if(node[i-1].w+a[i] >= node[i].w)42             {43                 node[i].w = node[i-1].w + a[i];44                 node[i].l = node[i-1].l;45             }46         }47 48         int ans = 1;49         for(i = 2; i <= n; ++i)50             if(node[i].w > node[ans].w)51                 ans = i;52         printf("%d %d %d\n", node[ans].w, node[ans].l, node[ans].r);53     }54     return 0;55 }
代码君