这道题的第二个测试点怎么弄都过不了,就是起始元素的位置过不了,感觉总是跳过了我的搜索过程直接输出了位置的初始值0,麻烦大佬们帮我看看我这代码哪里有问题。

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int v[N];
int f[N];
 
int main(){
    int n,a = 0,b = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) {
    	cin >> v[i];
	}
	int ans = f[0];
   for(int i = 1; i <= n; i++) {
   			f[i] = max(f[i - 1] , 0) + v[i];
   }
   for(int i = 1; i <= n; i++) {
   	     if(ans < f[i]) {
   	     	ans = f[i];
   	     	b = i;
			}  
   }
   for(int i = b; i >= 1 ;i--)
    {
            if(f[i] == v[i])
            {
                a = i;
                break;
            }
    }
    cout << a << endl << b << endl << ans;
    return 0;
}

1 comments

  • @ 2021-10-17 10:03:26

    考虑全负情况

    eg:

    -5 -4 -1 -7 -8
    
    
    • 1

    Information

    ID
    77
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    8
    Tags
    # Submissions
    326
    Accepted
    47
    Uploaded By