文章

5

粉丝

244

获赞

5

访问

6.1k

头像
最大连续子序列 题解:
P1334 浙江大学/中国矿业大学机试题
发布于2023年5月14日 11:09
阅读数 529

用vector来记录一个数的最大最小下标

 

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<string.h>
#include<queue>
#include<math.h>
#include<bits/stdc++.h>
using namespace std;
int main() {
	int k;
	while(cin>>k)
	{
		if(k==0) break;
		vector<int>first(k,-1),last(k,-1);//第i个数最大最小的下标
		int a[k];
		for(int i=0;i<k;i++)
		{
			cin>>a[i];
		}
		
		int dp[k+1];
		
		memset(dp,0,sizeof(dp));
		dp[0]=a[0];
		first[0]=a[0];
		last[0]=a[0];
		int maxindex=0;
		int maxans=dp[0];
		
		bool havepos=false;
		for(int i=0;i<k;i++)
		{
			if(a[i]>=0)
			{
				havepos=true;
			}
		}
		
		if(havepos==false)
		{
			cout<<0<<" "<<a[0]<<" "<<a[k-1]<<endl;
			continue;
		}
		for(int i=1;i<k;i++)
		{
			if(a[i]+dp[i-1]>a[i])
			{
				dp[i]=a[i]+dp[i-1];
				first[i]=first[i-1];
				last[i]=a[i];
			}
			else
	...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发