QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#335413 | #2697. Exhausting Errands | rageOfThunder | TL | 0ms | 0kb | C++14 | 3.5kb | 2024-02-23 12:30:42 | 2024-02-23 12:30:42 |
answer
#include<bits/stdc++.h>
#define ll long long
#define mk make_pair
#define fi first
#define se second
#define int long long
using namespace std;
inline ll read(){
ll x=0,f=1;char c=getchar();
for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
return x*f;
}
const int mod=998244353;
int ksm(int x,int y,int p=mod){
int ans=1;
for(int i=y;i;i>>=1,x=1ll*x*x%p)if(i&1)ans=1ll*ans*x%p;
return ans%p;
}
int inv(int x,int p=mod){return ksm(x,p-2,p)%p;}
mt19937 rnd(time(0));
int randint(int l,int r){return rnd()%(r-l+1)+l;}
void add(int &x,int v){x+=v;if(x>=mod)x-=mod;}
void Mod(int &x){if(x>=mod)x-=mod;}
int cmod(int x){if(x>=mod)x-=mod;return x;}
void cmax(ll &x,ll v){x=max(x,v);}
void cmin(ll &x,ll v){x=min(x,v);}
const int N=1e6+5;
int n,pre[N],pos[N],op[N],m,len;
int L[N],R[N],a[N],b[N],c[N],d[N];
vector<pair<int,int> >A,B;
signed main(void){
freopen("game.in","r",stdin);
// freopen("sample_game2.in","r",stdin);
// freopen("game.out","w",stdout);
len=read(),n=read();
// for(int i=1;i<=n;i++){
// pos[i]=read();
// op[i]=read();
// if(op[i]==2)pre[i]=read();
// }
// for(int i=1;i<=n;i++)if(op[i]==1)L[i]=R[i]=pos[i];
// for(int i=1;i<=n;i++)if(op[i]==2)cmin(L[pre[i]],pos[i]),cmax(R[pre[i]],pos[i]);
// for(int i=1;i<=n;i++)if(op[i]==1){
// // cout<<"i = "<<i<<" L,R = "<<L[i]<<" "<<R[i]<<endl;
// A.emplace_back(mk(pos[i],R[i]));
// if(L[i]<pos[i])B.emplace_back(mk(L[i],pos[i]));
// }
for(int i=1;i<=n;i++){
int l=read(),r=read();
if(l<r)A.emplace_back(mk(l,r));
else B.emplace_back(mk(r,l));
}
sort(A.begin(),A.end()),sort(B.begin(),B.end());
auto Rec=[&](vector<pair<int,int> >&C){
int tl=-1,tr=-1;
vector<pair<int,int> >R;
for(auto [l,r]:C){
// cout<<"l,r = "<<l<<" "<<r<<" tl,tr = "<<tl<<" "<<tr<<endl;
if(l>tr){
if(tl!=-1)R.emplace_back(mk(tl,tr));
tl=l,tr=r;
}
else cmax(tr,r);
}
if(tl!=-1)R.emplace_back(mk(tl,tr));
C=R;
};
Rec(A),Rec(B);
// cout<<"A = ";for(auto [l,r]:A)cout<<"["<<l<<","<<r<<"] ";puts("");
// cout<<"B = ";for(auto [l,r]:B)cout<<"["<<l<<","<<r<<"] ";puts("");
if(A.size()==0){
cout<<B.back().se-B[0].fi<<endl;
return 0;
}
if(B.size()==0){
cout<<A.back().se-A[0].fi<<endl;
return 0;
}
n=A.size(),m=B.size();
for(int i=1;i<=n;i++)a[i]=A[i-1].fi,b[i]=A[i-1].se;
for(int i=1;i<=m;i++)c[i]=B[i-1].fi,d[i]=B[i-1].se;
// puts("ok");
ll ans=1e18;
vector<ll>sum(max(n,m)+5),vx(max(n,m)+5),vy(max(n,m)+5);
for(int i=1;i<=m;i++)sum[i]=sum[i-1]+d[i]-c[i];
for(int i=1;i<=m;i++)sum[i]*=2;
for(int i=1;i<=m;i++)vx[i]=d[i]-c[1]+abs(c[1]-a[1]),vy[i]=d[m]-c[i]+abs(d[m]-b[n]);
int mn=vx[0]-sum[0];
for(int i=1;i<=m+1;i++){
ans=min(ans,b[n]-a[1]+vy[i]+sum[i-1]+mn);
mn=min(mn,vx[i]-sum[i]);
}
// for(int i=0;i<=m;i++)for(int j=i+1;j<=m+1;j++)ans=min(ans,b[n]-a[1]+vx[i]+vy[j]+sum[j-1]-sum[i]);
fill(sum.begin(),sum.end(),0),fill(vx.begin(),vx.end(),0),fill(vy.begin(),vy.end(),0);
for(int i=1;i<=n;i++)sum[i]=sum[i-1]+b[i]-a[i];
for(int i=1;i<=n;i++)sum[i]*=2;
for(int i=1;i<=n;i++)vx[i]=b[i]-a[1]+abs(a[1]-c[1]),vy[i]=b[n]-a[i]+abs(b[n]-d[m]);
mn=vx[0]-sum[0];
for(int i=1;i<=n+1;i++){
ans=min(ans,d[m]-c[1]+vy[i]+sum[i-1]+mn);
mn=min(mn,vx[i]-sum[i]);
}
// for(int i=0;i<=n;i++)for(int j=i+1;j<=n+1;j++)ans=min(ans,d[m]-c[1]+vx[i]+vy[j]+sum[j-1]-sum[i]);
cout<<ans<<endl;
return 0;
}
详细
Test #1:
score: 0
Time Limit Exceeded
input:
100 2 1 100 100 1