NOIP2015普及組 —— 推銷員(salesman)
阿新 • • 發佈:2019-01-08
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("salesman.in");
ofstream fout("salesman.out");
const int MAX_N = 100000, INT_MIN = 1<<31;
struct Tnode
{
int a, id;
} a[MAX_N];
int S[MAX_N], A[MAX_N], rmax[MAX_N];
int Head_a = 0;
bool cmp(const Tnode &, const Tnode &);
void Build_a(int);
void Build_rmax(int);
int LMax();
int RMax();
void Update_a(int);
void Update_rmax(int);
int main()
{
int N;
fin>>N;
for (int i=0; i<N; i++)
fin>>S[i];
for (int i=0; i<N; i++)
fin>>A[i];
Build_a(N);
Build_rmax(N);
int Last_Max = rmax[0];
int Ans = S[Last_Max]<<1+A[Last_Max];
fout<<Ans<<endl;
A[rmax[0]] = INT_MIN;
a[0].a = INT_MIN;
Update_rmax(rmax[0]);
int Left_Max_id, Right_Max_id, Left_Max, Right_Max;
for (int i=1; i<N; i++)
{
while (visit[a[Left_Max_id].id] && Left_Max_id<N)
Left_Max_id++;
Left_Max_id = LMax();
Right_Max_id = RMax();
Left_Max = A[Left_Max_id];
Right_Max = (S[Right_Max_id]-S[Last_Max_id])<<1 + A[Right_Max_id];
if (Left_Max > Right_Max)
{
Ans += Left_Max;
Last_Max = Left_Max_id;
Update_a(Left_Max_id);
}
else
{
Ans += Right_Max;
Last_Max = Right_Max_id;
// Update_rmax(Right_Max_id);
}
fout<<Ans<<endl;
}
return 0;
}
bool cmp(const Tnode &A, const Tnode &B)
{
return A.a < B.a ? true : false;
}
void Build_a(int n)
{
for (int i=0; i<n; i++)
a[i].a = A[i];
sort(a, a+n, cmp);
}
void Build_rmax(int n)
{
rmax[n-1] = n-1;
int Right_Max = S[n-1]<<1 + A[n-1];
int Now_Max;
for (int i=n-2; i>=0; i--)
{
Now_Max = S[i]<<1 + A[i];
if (Now_Max > Right_Max)
{
rmax[i] = i;
Right_Max = Now_Max;
}
else
rmax[i] = rmax[i-1];
}
}
int LMax()
{
return Head_a;
}
void Update_a()
{
Head_a++;
}