1. 程式人生 > 實用技巧 >[CF620D]Professor GukiZ and Two Arrays

[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;
}