[CF620D]Professor GukiZ and Two Arrays
题目
题解
对于不交换和只交换一次这两种情况,我们都可以直接暴力做,前者 \(\mathcal O(n)\) 输入时预处理,后者直接 \(\mathcal O(nm)\) 暴力扫即可。
对于交换两次,我们可以将数组中任意两个数绑在一起,分别组成 \(\frac{n^2}{2}\) 的数组和 \(\frac{m^2}{2}\) 的数组,然后在这两个数组中,将任意一个排序,在另一个中枚举一个 \(x\),在有序数组中二分寻找与 \(x\) 最接近的数,然后与答案进行比较即可。
代码
#include<cstdio>
#include<algorithm>
#include<utility>
using namespace std;
#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
typedef long long LL;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long
#define cg (c=getchar())
template<class T>inline void read(T& x){
char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
if(f)x=-x;
}
template<class T>inline T read(const T sample){
T x=0;char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
if(x<0)return (void)(putchar('-'),fwrit(-x));
if(x>9)fwrit(x/10);
putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}
const int MAXN=2000;
const int MAXM=2000;
int a[MAXN+5],b[MAXN+5];
int n,m,hasswap;
LL ans,suma,sumb;
pair<int,int>swa1,swa2;
int swacnt;
inline void Init(){
n=read(1);
rep(i,1,n)suma+=(a[i]=read(1));
m=read(1);
rep(i,1,m)sumb+=(b[i]=read(1));
if(suma<sumb){
hasswap=true;
swap(n,m);swap(suma,sumb);
rep(i,1,Max(n,m))swap(a[i],b[i]);
}
ans=suma-sumb;
}
inline void Justone(){
LL tmpa,tmpb;
rep(i,1,n)rep(j,1,m){
tmpa=suma-a[i]+b[j];
tmpb=sumb-b[j]+a[i];
if(fab(tmpa-tmpb)<ans){
ans=fab(tmpa-tmpb);
swa1=mp(i,j);
swacnt=1;
}
}
}
pair<int,pii>qa[MAXN*MAXN+5];int sza;
pair<int,pii>qb[MAXM*MAXM+5];int szb;
inline void update(const int i,const int j){
LL tmpa=suma-qa[i].first+qb[j].first;
LL tmpb=sumb-qb[j].first+qa[i].first;
if(fab(tmpa-tmpb)<ans){
ans=fab(tmpa-tmpb);
swa1=mp(qa[i].second.first,qb[j].second.first);
swa2=mp(qa[i].second.second,qb[j].second.second);
swacnt=2;
}
}
inline void Swaptwo(){
rep(i,1,n)rep(j,i+1,n)
qa[++sza]=mp(a[i]+a[j],mp(i,j));
rep(i,1,m)rep(j,i+1,m)
qb[++szb]=mp(b[i]+b[j],mp(i,j));
sort(qb+1,qb+szb+1);
if(!sza || !szb)return;
int l,r,mid;
LL goal;
rep(i,1,sza){
goal=0ll+qa[i].first-((suma-sumb)>>1);
l=1,r=szb;
while(l<r){
mid=l+r>>1;
if(goal<qb[mid].first)r=mid;
else l=mid+1;
}
update(i,l);
if(l>1)update(i,l-1);
}
}
inline void writAns(){
writc(ans,'\n'),writc(swacnt,'\n');
if(hasswap)swap(swa1.first,swa1.second),swap(swa2.first,swa2.second);
if(swa1.first)writc(swa1.first,' '),writc(swa1.second,'\n');
if(swa2.first)writc(swa2.first,' '),writc(swa2.second,'\n');
}
signed main(){
Init();
Justone();
Swaptwo();
writAns();
return 0;
}