版本比较
题解:P3992 [BJOI2017] 开车
正文
增加 8 行删除 4 行
1123345674 行未改动1213141516161718192045 行未改动66676869707172737475767778798081827983848586875 行未改动> Computers are fast nowadays, so we can solve this problem in $O(nq)$。> $\text{Computers are fast nowadays, so we can solve this problem in}\ O(nq)\text{.}$>> It is important to make sure your program uses SIMD instructions.> $\text{It is important to make sure your program uses SIMD instructions.}$
考虑朴素暴力怎么做。显然是把 $a,b$ 排序后,计算 $\sum_{i=1}^n |a_i-b_i|$。
接下来考虑带修的情况。由于单点修,我们不需要每次重新排序,只需要修改后移动即可。4 行未改动:::success[code]```#include<bits/stdc++.h>using namespace std;// #define int long long#define int long long#define ui unsigned int#define i128 __int128_t#define u128 __uint128_t#define i16 short45 行未改动 for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<n;i++) cin>>b[i],ta[i]=a[i]; sort(a.begin(),a.end()); sort(b.begin(),b.end()); int ans=0; for(int i=0;i<n;i++) ans+=abs(a[i]-b[i]); cout<<ans<<'\n'; int q; cin>>q; while(q--) { int i,x; cin>>i>>x; i--; auto p=lower_bound(a.begin(),a.end(),ta[i]); a.erase(p); ta[i]=x; p=lower_bound(a.begin(),a.end(),ta[x]); p=lower_bound(a.begin(),a.end(),ta[i]); a.insert(p,x); int ans=0; for(int i=0;i<n;i++) ans+=abs(a[i]-b[i]); cout<<ans<<'\n';5 行未改动