小埋再遇a+b+c问题WA90


#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

ll a[100005], l[100005], r[100005];

int main()
{
	ll t;
	cin >> t;
	while(t--)
	{
		ll n, ans = 0;
		cin >> n;
		memset(l, 0, sizeof(l));
		memset(r, 0, sizeof(r));
		for(ll i = 1; i <= n; i++)
			cin >> a[i];
		for(ll i = 1; i <= n; i++)
			l[i] = max(l[i - 1], a[i] + i);
		l[0] = 0;
		a[n + 1] = 0;
		ans = 0;
		for(ll i = n; i >= 1; i--)
			r[i] = max(r[i + 1], a[i] - i);
		for(ll i = 2; i < n; i++)
			ans = max(a[i] + l[i - 1] + r[i + 1], ans);
		cout << ans << endl;
	}
	return 0;
}

WA on #1

1 个赞

初始化是0

memset(r, 0, sizeof(r));

但是a[i]可能小于i,会有负数,比如样例1

r[i] = max(r[i + 1], a[i] - i);

解决:memset改成负数

1 个赞

memset (r,-10e9,sizeof(r))

1 个赞

还没对。。。。。。

1 个赞

用for手写一个

1 个赞

什么意思

1 个赞
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

int a[100005], l[100005], r[100005];

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n, ans = 0;
		cin >> n;
		memset(l, 0, sizeof(l));
		memset(r, 255, sizeof(r));
		for(int i = 1; i <= n; i++)
			cin >> a[i];
		for(int i = 1; i <= n; i++)
			l[i] = max(l[i - 1], a[i] + i);
		l[0] = 0;
		a[n + 1] = 0;
		ans = 0;
		for(int i = n; i >= 1; i--)
			r[i] = max(r[i + 1], a[i] - i);
		for(int i = 2; i < n; i++)
			ans = max(a[i] + l[i - 1] + r[i + 1], ans);
		cout << '/' << ans << endl;
	}
	return 0;
}

这是我现在的代码

1 个赞
for(int i = 1; i <= n; i++)
{
    r[i] = -10e9;
}
1 个赞

不行

1 个赞

你的ll没用啊

1 个赞

本蒟蒻觉的是long long

1 个赞

开了也一样 :sweat_smile:

1 个赞

行吧

1 个赞