ProblemI. 整数反应
题目:
思路:
想到了要用二分写,但一直时间超限,原因是二分里面还要用set函数或multiset函数,如果不用函数用循环写的话,就会时间超限,赛后学长将=讲了之后才明白,补题的时候用了multise函数(优点是元素可以重复)set函数元素不能重复,其他的两个函数基本一样。
代码:
#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int N=2e5+20;
int a[N],b[N];
int n;
bool check(int x)
{
multiset<int> s1,s2;
for(int i=1;i<=n;i++)
{
if(b[i]==0)
{
if(s1.empty())
s2.insert(a[i]);
else
{
auto t=s1.lower_bound(x-a[i]);
if(t==s1.end())
return false;
else
s1.erase(t);
}
}
else
{
if(s2.empty())
s1.insert(a[i]);
else
{
auto t=s2.lower_bound(x-a[i]);
if(t==s2.end())
return false;
else
s2.erase(t);
}
}
}
return true;
}
void solve()
{
cin>>n;
int ma=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
ma=max(ma,a[i]);
}
for(int i=1;i<=n;i++)
cin>>b[i];
int l=0,r=2*ma+1;
while(l<r)
{
int mid=l+r+1>>1;
if(check(mid)) l=mid;
else
r=mid-1;
}
cout<<l<<endl;
return ;
}
signed main()
{
IOS
int t=1;
while(t--)
solve();
return 0;
}