题目链接:51nod 1065 最小正子段和
房教说用前缀和做,然后看了别人博客懂了后就感觉,这个真有意思...
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N = 50001; 6 const int inf = 0x3f3f3f3f; 7 pair<long long, int> sum[N]; 8 int a[N]; 9 int n; 10 int main(){ 11 int i, j; 12 long long ans = inf; 13 scanf("%d", &n); 14 sum[0] = make_pair(0,0); 15 for(i = 1; i <= n; ++i){ 16 scanf("%d", &a[i]); 17 sum[i].first = a[i] + sum[i-1].first; 18 sum[i].second = i; 19 } 20 sort(sum+1, sum+1+n); 21 for(i = 1; i <= n; ++i){ 22 if(sum[i].first > 0) 23 ans = min(ans, sum[i].first); 24 if(sum[i].second > sum[i-1].second && sum[i].first > sum[i-1].first) 25 ans = min(ans, sum[i].first - sum[i-1].first); 26 } 27 printf("%lld\n", ans); 28 return 0; 29 }
时间: 2024-11-07 05:09:44