//#include<iostream>
//#include<algorithm>
//#include<cstring>
//using namespace std;
//
//int a[5] = { 5,3,10,7,1 };
//int sum[200005];
//#define mem(a) memset(a, 0, sizeof(a))
//int main() {
// int min = -1000, min2 = -1000;
// for (int i = 1; i <= 4; i++) {
// if (a[i-1] > min)min = a[i-1];
// if (min + a[i] > min2)min2 = min + a[i];
// }
// cout << min2;
//}
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int b[200005];
int a[200005];
int sum[200005];
#define mem(a) memset(a, 0, sizeof(a))
int main() {
int T;
cin >> T;
int n, k;
while (T--) {
memset(b, 0, sizeof(b));
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
int cnt = 1;
for (int i = k; i <= n; i++) {
b[cnt++] = sum[i] - sum[i - k];
}
long long MAX1 = -1e18, MAX2 = -1e18;
for (int i = k + 1; i <= n - k + 1; i++) {
if (b[i - k] > MAX1)
MAX1 = b[i - k];
if (MAX1 + b[i] > MAX2)
MAX2 = b[i] + MAX1;
}
cout << MAX2 << endl;
}
}
上面不对下面的对?????!!!!!!!!!!!!!!!!!!
#include<iostream>
#include<algorithm>
#include<cstring>
typedef long long ll;
using namespace std;
ll a[200005], b[200005];
#define mem(a) memset(a, 0, sizeof(a))
int main() {
int T;
cin >> T;
ll n, k;
while (T--) {
memset(b, 0, sizeof(b));
memset(a, 0, sizeof(a));
cin >> n >> k;
for (ll i = 1; i <= n; i++) {
cin >> a[i];
a[i] = a[i - 1] + a[i];
}
ll cnt = 1;
for (ll i = k; i <= n; i++) {
b[cnt++] = a[i] - a[i - k];
}
//b数组相当于所有区间和枚举
ll MAX1 = -1e18, ans = -1e18;
for (int i = k + 1; i <= n - k + 1; i++) {
MAX1 = max(MAX1, b[i - k]);
ans = max(ans, MAX1 + b[i]);
}
cout << ans << endl;
}
}
焯!懂了 看数据200000*100000是一个11位数,而int最大10位数,数据一大直接溢出炸了。就是int和long long的区别就能导致AC与否!
与
if (b[i - k] > MAX1)
MAX1 = b[i - k];
if (MAX1 + b[i] > ans)
ans = b[i] + MAX1;
和
MAX1 = max(MAX1, b[i - k]);
ans = max(ans, MAX1 + b[i]);
没一点关系,这两个代码同等效力!