vector在oj和IDE上的区别:
cout << "front capcity : " << front.capacity() << " size : " << front.size() << endl;
cout << "back capcity : " << back.capacity() << " size : " << back.size() << endl;
样例:
5
1 3 1 1 4
IDE(clion):
front capcity : 4 size : 4
back capcity : 4 size : 3
OJ(acwing):
front capcity : 8 size : 5
back capcity : 8 size : 5
#include <iostream>
#include <vector>
#include <algorithm>
typedef long long ll;
using namespace std;
const int maxn = 2e5 + 5;
ll n, sum = 0;
ll num[maxn]{0};
vector<ll> front, back;
int main()
{
cin >> n;
front.push_back(0ll);
back.push_back(0ll);
for (int i = 1; i <= n; i++)
{
scanf("%lld", &num[i]);
sum += num[i];
}
ll t = sum / 2;
bool m1 = true, m2 = true;
for (int i = 1; i <= n; i++)
{
//注意vector的size!
if(m1)
{
if (front[i - 1] + num[i] <= t)
{
front.push_back(front[i - 1] + num[i]);
}
else
{
m1 = false;
}
}
if(m2)
{
if (back[i - 1] + num[n - i + 1] <= t)
{
back.push_back(back[i - 1] + num[n - i + 1]);
}
else
{
m2 = false;
}
}
}
reverse(front.begin(), front.end());
reverse(back.begin(), back.end());
int i = 0, j = 0;
bool flag = false;
while (i < front.size() && j < back.size())
{
if (front[i] == back[j])
{
cout << front[i] << endl;
flag = true;
break;
}
else if (front[i] > back[j])
{
i++;
}
else
{
j++;
}
}
if (!flag)
{
cout << 0 << endl;
}
return 0;
}