This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#define MAXN 100005
struct Stick
{
int t, d, s;
};
Stick m[MAXN];
int cmp(const Stick &a, const Stick &b)
{
return a.t<b.t || (a.t==b.t && a.d<b.d);
}
long long dp[MAXN][2], out;
int n, h, tx[MAXN], dx[MAXN];
int bst(int x)
{
int st, fn, mid;
st=1, fn=n;
while(st<=fn)
{
mid=(st+fn)/2;
if(tx[mid]>x) fn=mid-1;
else if(tx[mid]<x) st=mid+1;
else if(tx[mid]==x) break;
}
return mid;
}
int bsd(int x)
{
int st, fn, mid;
st=1, fn=n;
while(st<=fn)
{
mid=(st+fn)/2;
if(dx[mid]>x) fn=mid-1;
else if(dx[mid]<x) st=mid+1;
else if(dx[mid]==x) break;
}
return mid;
}
void Init()
{
int i;
scanf("%d %d",&n,&h);
for(i=1 ; i<=n ; i++)
{
scanf("%d %d",&m[i].t,&m[i].d);
tx[i]=m[i].t, dx[i]=m[i].d;
m[i].s=h+abs(m[i].t-m[i].d);
}
std::sort(tx+1,tx+1+n);
std::sort(dx+1,dx+1+n);
}
void Change()
{
int i;
for(i=1 ; i<=n ; i++)
{
m[i].t=bst(m[i].t);
m[i].d=bsd(m[i].d);
}
std::sort(m+1,m+1+n,cmp);
}
void dynamic()
{
int i;
long long v1, v2;
for(i=1 ; i<=n ; i++)
{
v1=dp[m[i].t][0]+m[i].s;
v2=dp[m[i].d][1]+m[i].s;
if(dp[m[i].d][1]<v1)
{
dp[m[i].d][1]=v1;
if(out<dp[m[i].d][1]) out=dp[m[i].d][1];
}
if(dp[m[i].t][0]<v2)
{
dp[m[i].t][0]=v2;
if(out<dp[m[i].t][0]) out=dp[m[i].t][0];
}
}
}
int main()
{
Init();
Change();
dynamic();
printf("%lld",out);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |