题解:P15263 [USACO26JAN2] Circle of Cows P

· · 题解

思路

首先先想假如当前有一个长度为 2i 的序列,如何匹配是最优的。显然容易发现是对每一个 1 \leq j \leq i,都将 j 与 j+i 匹配。

然后假设当前已经算出来了长度为 2i-2 的答案,考虑怎么往 2i 上扩展。容易发现,这只会新增加两个数,不会做删除操作。证明可以考虑调整法,此时最朴素的实现就是 O(n^4) 的了。

考虑优化。不难发现,当加入两个数过后,一个原本在序列中的数只会与其原本对应的数以及左右各一个匹配。即对于每一个 1 \leq j \leq i-1,匹配的只会是 i-2+j \leq k \leq i+j。所以记录一下往左偏移过后区间最小值和往右偏移过后的区间最小值即可,用 ST 表维护即可做到 O(n^3)。

Code

#include <bits/stdc++.h>
using namespace std;
namespace yhy{
    const int MAXN=1005;
    #define int long long
    const int INF=1e18;
    int n,c;
    int a[MAXN];
    int ans[MAXN];
    int vis[MAXN];
    set<int> st;
    vector<int> vt0,vt1,vt_delta,vt;
    int cnt[MAXN];
    inline int Ask(int x,int y) {
        // cout<<"x:"<<x<<" y:"<<y<<'\n';
        return min(y-x,c-(y-x));
    }
    class RMQ{
        int ST[11][MAXN];
        public:
            inline void Init(int n,vector<int> &vt) {
                for(int i=1;i<=n;i++) {
                    ST[0][i]=vt[i-1];   
                }
                for(int i=1;i<=log2(n);i++) {
                    for(int j=1;j<=n-(1<<i)+1;j++) {
                        ST[i][j]=min(ST[i-1][j],ST[i-1][j+(1<<i-1)]);
                    }
                }
            }
            inline int Ask(int x,int y) {
                if(y<x) return c;
                int len=log2(y-x+1);
                return min(ST[len][x],ST[len][y-(1<<len)+1]);
            }
            inline void Clear(int n) {
                for(int i=0;i<=log2(n);i++) {
                    for(int j=1;j<=n-(1<<i)+1;j++) {
                        ST[i][j]=0;
                    }
                }
            }
    }Minn[3];
    int main() {
        // freopen(".in","r",stdin);
        // freopen(".out","w",stdout);
        std::ios::sync_with_stdio(false);
        cin.tie(0);cout.tie(0);
        cin>>n>>c;
        for(int i=1;i<=n;i++) {
            cin>>a[i];
        }
        for(int i=1;i<=n;i++) {
            for(int j=i+1;j<=n;j++) {
                if(Ask(a[i],a[j])>ans[1]) {
                    st.clear();
                    st.insert(i);
                    st.insert(j);
                    ans[1]=Ask(a[i],a[j]);
                }
            }
        }
        cout<<ans[1]<<" ";
        for(auto v:st) {
            vis[v]=1;
        }

        if(n==2) {
            return 0;
        }

        int now_x=0,now_y=0;
        for(int j=1;j<=n;j++) {
            if(vis[j]) {
                continue;
            }
            for(int k=j+1;k<=n;k++) {
                if(vis[k]) {
                    continue;
                }
                // cout<<"j:"<<j<<" k:"<<k<<'\n';
                st.insert(j);
                st.insert(k);
                int now_ans=c;
                int tot=0;
                for(auto v:st) {
                    tot++;
                    // cout<<"v:"<<v<<" tot:"<<tot<<'\n';
                    if(tot<=2) {
                        vt.push_back(v);
                    }
                    else {
                        now_ans=min(now_ans,Ask(a[vt[tot-2-1]],a[v]));
                    }
                }
                if(now_ans>ans[2]) {
                    ans[2]=now_ans;
                    now_x=j;
                    now_y=k;
                }
                vt.clear();
                st.erase(j);
                st.erase(k);
            }
        }
        st.insert(now_x);
        st.insert(now_y);
        vis[now_x]=vis[now_y]=1;

        // cout<<'\n';

        cout<<ans[2]<<" ";

        // for(auto v:st) {
        //  cout<<v<<" ";
        // }
        // cout<<'\n';

        for(int i=3;i<=n/2;i++) {

            // cout<<"i:"<<i<<'\n';

            // int add_flag=0;

            int tot_id=0;
            auto id=st.begin();

            for(int j=1;j<=n;j++) { // 处理 1 ~ n 在 st 中的相对位置
                if(vis[j]) {
                    continue;
                }
                while(id!=st.end() && *id<j) {
                    id++;
                    tot_id++;
                }
                cnt[j]=tot_id;

                // cout<<"j:"<<j<<" cnt[j]:"<<cnt[j]<<'\n';
            }

            for(auto v:st) {
                if(vt0.size()==i-1) {
                    vt1.push_back(v);
                }
                else {
                    vt0.push_back(v);
                }
            }

            // cout<<"vt0 -> ";
            // for(auto v:vt0) {
            //  cout<<v<<" ";
            // }
            // cout<<'\n';
            // cout<<"vt1 -> ";
            // for(auto v:vt1) {
            //  cout<<v<<" ";
            // }
            // cout<<'\n';

            for(int j=-1;j<=1;j++) { // 处理偏移

                // cout<<"i:"<<i<<" j+1:"<<j+1<<'\n';

                for(int k=1;k<=i-1;k++) {
                    if(k+j<1 || k+j>i-1) {
                        vt_delta.push_back(INF);
                    }
                    else {
                        // cout<<"k:"<<k<<" x:"<<vt0[k-1+j]<<" y:"<<vt1[k-1]<<" Ask(a[vt0[k-1+j]],a[vt1[k-1]]):"<<Ask(a[vt0[k-1+j]],a[vt1[k-1]])<<'\n';
                        vt_delta.push_back(Ask(a[vt0[k-1+j]],a[vt1[k-1]]));
                    }
                }

                // cout<<"vt_delta: ";
                // for(auto v:vt_delta) {
                //  cout<<v<<" ";
                // }
                // cout<<'\n';

                Minn[j+1].Init(i-1,vt_delta);

                // cout<<"j:"<<j<<'\n';
                // for(auto v:vt_delta) {
                //  cout<<v<<" ";
                // }
                // cout<<'\n';

                vt_delta.clear();
            }

            // cout<<"Pass\n";

            int now_x=0;
            int now_y=0;
            for(int j=1;j<=n;j++) {
                if(vis[j]) {
                    continue;
                }
                for(int k=j+1;k<=n;k++) {
                    if(vis[k]) {
                        continue;
                    }

                    int x=cnt[j];
                    int y=cnt[k]+1;

                    // cout<<"i:"<<i<<" j:"<<j<<" k:"<<k<<" x:"<<x<<" y:"<<y<<'\n';

                    int now_ans=ans[i-1];

                    // int now_flag=0;

                    if(x>i-1) { // 在右边加两个

                        // now_flag=0;

                        x-=i-1;
                        y-=i-1;

                        // cout<<"Pass -> add two right\n";

                        // cout<<"x:"<<x<<" y:"<<y<<'\n';

                        now_ans=min(now_ans,Minn[0].Ask(2,x));
                        now_ans=min(now_ans,Ask(a[vt0[x-1]],a[j]));

                        // cout<<"Pass\n";

                        if(y==i) {

                            // cout<<"Pass1 -> y==i\n";

                            now_ans=min(now_ans,Ask(a[vt1[0]],a[k]));
                        }
                        else {

                            // cout<<"Pass1 -> usual situation\n";

                            now_ans=min(now_ans,Ask(a[vt0[y-1]],a[k]));
                            now_ans=min(now_ans,Minn[2].Ask(y,i-1));
                            now_ans=min(now_ans,Ask(a[vt1[0]],a[vt1[i-2]]));

                            // cout<<"y:"<<y<<" i-1:"<<i-1<<" Minn[2].Ask(y,i-1):"<<Minn[2].Ask(y,i-1)<<'\n';
                        }

                        // cout<<"Pass\n";
                    }

                    else if(y<=i-1) { // 在左边加两个

                        // if(j==5 && k==50) {
                        //  cout<<"x:"<<x<<" y:"<<y<<'\n';
                        //  cout<<"y+1:"<<y+1<<" i-1:"<<i-1<<" Minn[2].Ask(y+1,i-1):"<<Minn[2].Ask(y+1,i-1)<<'\n';
                        // }

                        now_ans=min(now_ans,Minn[0].Ask(y+1,i-1));

                        now_ans=min(now_ans,Ask(a[k],a[vt1[y-1]]));

                        // now_flag=1;

                        if(x==0) {
                            now_ans=min(now_ans,Ask(a[j],a[vt0[i-2]]));
                        }
                        else {
                            now_ans=min(now_ans,Ask(a[j],a[vt1[x-1]]));
                            now_ans=min(now_ans,Minn[2].Ask(1,x-1));
                            now_ans=min(now_ans,Ask(a[vt0[0]],a[vt0[i-2]]));
                        }
                    }

                    else { // 一左一右

                        // now_flag=2;

                        y-=i;

                        // if(j==10 && k==62) {
                        //  cout<<"x:"<<x<<" y:"<<y<<'\n';
                        // }

                        if(x==y) {
                            // if(x==1) {
                            //  cout<<"Pass\n";
                            // }

                            // now_flag=3;

                            now_ans=min(now_ans,Ask(a[j],a[k]));
                            // cout<<"now_ans:"<<now_ans<<'\n';
                        }
                        else if(x<y) {

                            // now_flag=4;

                            now_ans=min(now_ans,Ask(a[j],a[vt1[x]]));
                            if(y!=x+1) {
                                now_ans=min(now_ans,Minn[0].Ask(x+2,y));
                            }
                            now_ans=min(now_ans,Ask(a[vt0[y-1]],a[k]));
                        }
                        else {
                            now_ans=min(now_ans,Ask(a[vt0[y]],a[k]));

                            // now_flag=5;

                            // cout<<"now_ans(0):"<<now_ans<<'\n';

                            if(x!=y+1) {
                                now_ans=min(now_ans,Minn[2].Ask(y+1,x-1));

                                // cout<<"now_ans(1):"<<now_ans<<" y+1:"<<y+1<<" x:"<<x<<" Minn[0].Ask(y+1,x):"<<Minn[0].Ask(y+1,x)<<'\n';

                            }
                            now_ans=min(now_ans,Ask(a[j],a[vt1[x-1]]));

                            // cout<<"now_ans(2):"<<now_ans<<'\n';

                        }
                    }

                    if(now_ans>ans[i]) {

                        // add_flag=now_flag;

                        ans[i]=now_ans;
                        now_x=j;
                        now_y=k;
                    }
                }
            }

            // if(i==39) {
            //  cout<<"add_flag:"<<add_flag<<'\n';
            // }

            // cout<<"Pass\n";

            st.insert(now_x);
            st.insert(now_y);

            cout<<ans[i]<<" ";

            // for(auto v:st) {
            //  cout<<v<<" ";
            //  vt.push_back(v);
            // }
            // cout<<'\n';

            // cout<<"vt.size():"<<vt.size()<<'\n';
            // for(int j=1;j<=i;j++) {
            //  cout<<"j:"<<j<<" x:"<<vt[j-1]<<" y:"<<vt[j-1+i]<<" Ask:"<<Ask(a[vt[j-1]],a[vt[j-1+i]])<<'\n';
            //  ans[i]=min(ans[i],Ask(a[vt[j-1]],a[vt[j-1+i]]));
            // }
            // cout<<'\n';
            // cout<<"ans[i]:"<<ans[i]<<'\n';

            vt.clear();

            vis[now_x]=vis[now_y]=1;
            for(int i=0;i<=2;i++) {
                Minn[i].Clear(i-1);
            }
            vt0.clear();
            vt1.clear();
        }
        cout<<'\n';
        return 0;
    }
}
signed main() {
    yhy::main();
    return 0;
}

/*
10 100
0 4 5 10 30 40 50 65 70 90

50
1 6 7 10 50 
1 4 6 7 8 10 45 
1 2 4 5 6 7 8 10 40
1 2 3 4 5 6 7 8 9 10 40

19 999069114
37662298 133295135 175405990 233971423 356034940 425399084 461558745 639116048 640221783 643625812 719765525 740768029 747754675 759439089 801177770 845021149 953287868 963243312 996628740

497384547 493248201 492272508 488986209 471180330 468219822 397615364 357512692 324127264

499528843 499497044 499073995 498947054 498813655 
498754688 498496641 498307312 498282716 497740995 
497690304 497639313 497595825 497547578 497351573 
497308084 497054873 496854003 496385419 496233402 
495970464 495824809 495637583 495304666 494717688 
494270254 494047004 493931660 493893845 493484357 
492973702 492465673 491698097 491526266

99 999069114
7097712 10609263 19288334 23733901 55834335 58406492 71555741 126266166 129359811 141909946 148643866 151441732 154032128 172124616 187287379 187332193 201081291 204926687 226777440 247487169 264652713 277146028 312285562 314751904 317415785 322092628 331405690 338896626 354963526 363493410 367756721 385244094 389255636 403598371 405572978 416741332 421162528 429133306 449292080 458794297 459073489 460378596 466920700 467509743 490276344 501311787 504693537 509105904 511363565 513219994 524520299 537031459 546548114 547177013 547382496 549096177 550104589 586082682 593428857 597118998 616418330 618078236 622549622 624573478 633124259 655793158 656291363 692354303 726772559 746434223 754647682 778575829 804144198 804751235 820847316 822737201 823955846 825252685 834649995 835130028 847668998 851725168 856977767 863022253 873906192 877795624 881629513 886996631 901145949 908671628 923377639 928705376 929548699 960634055 964272273 972685012 979232546 984981232 987966648

499528843 499497044 499073995 498947054 498813655 498754688 498496641 498307312 498282716 497740995 497690304 497639313 497595825 497547578 497351573 497308084 497054873 496854003 496385419 496233402 495970464 495824809 495637583 495304666 494717688 494270254 494047004 493931660 493893845 493484357 492973702 492465673 491698097 491526266 491214313 489994969 487346311 486654861 484542173 482663532 480256619 475929612 472070944 470754402 469911079 469003984 460999643 459379548 445836880

1 2 3 4 5 
6 8 9 12 13 
16 19 20 22 23 
24 25 26 27 28 
29 30 31 32 33 
34 35 36 37 38 
42 43 44 45 

47 48 50 51 56 
57 64 65 66 67 
68 69 70 72 74 
75 76 77 79 80 
82 83 84 87 88 
89 90 91 92 93 
94 95 96 99 

1 2 3 4 5
6 8 9 10 12 
13 16 19 20 22 
23 24 25 26 27 
28 29 30 31 32 
33 34 35 36 37 
38 42 43 44 45 

47 48 50 51 56 
57 62 64 65 66 
67 68 69 70 72 
74 75 76 77 79
80 82 83 84 87 
88 89 90 91 92 
93 94 95 96 99 


1 2 3 4 5 
6 8 9 10 12 
13 16 19 20 21 
22 23 24 25 26 
27 28 29 30 31 
32 33 34 35 36 
37 38 42 43 44 
45 

47 48 50 51 56 
57 62 64 65 66 
67 68 69 70 71 
72 74 75 76 77 
79 80 82 83 84 
87 88 89 90 91 
92 93 94 95 96 
99 

1 2 3 4 5 6 8 9 12 13 16 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 42 43 44 45 47 48 50 51 56 57 64 65 66 67 68 69 70 71 72 74 75 76 77 79 80 83 84 85 87 88 89 90 91 92 93 94 95 96 99 

1  2  3  4  5  6  7  11 
12 13 15 16 17 18 19 20 

1  2  3  4  5  6  7  8  10 
11 12 13 15 16 17 18 19 20 

499505564 499438675 499286260 499177982 499096883 
499022946 498796194 498570699 498558165 498364246 
498042826 497772161 497744864 497706946 497690301 
497532628 497241287 497231361 497049869 496345836 
496216274 496162757 495812119 495501187 495148372 
494837690 494220680 493914044 493740572 493686807 
493407638 493391315 493366836 492132559 492112393 
492014708 489636776 488879423 488538555 487957818 
486254917 483934497 483917461 482205705 477399167 
475692101 474450785 473985575 449842553 446901811 

499505564 499438675 499286260 499177982 499096883 
499022946 498796194 498570699 498558165 498364246 
498042826 497772161 497744864 497706946 497690301 
497532628 497241287 497231361 497049869 496345836 
496216274 496162757 495812119 495501187 495148372 
494837690 494220680 493914044 493740572 493686807 
493407638 493391315 493366836 492132559 492112393 
492014708 489636776 488879423 487957818 486875239 
486254917 483917461 482205705 477399167 474450785 
473985575 471044833 467006276 449842553 446901811

1 2 3 4 6 
7 9 10 11 13 
16 17 18 19 20 
21 22 25 26 28 
29 30 31 32 35 
36 37 38 39 43 
44 46 47 48 52 
53 54 55 
56 57 58 59 60 
61 62 63 64 65 
66 68 69 70 71 
73 74 77 78 79 
80 81 82 84 85 
86 87 88 89 91 
92 93 94 95 96 
98 99 100 

1 2 3 4 5 
6 7 9 10 11 
13 16 17 18 19 
20 21 22 25 26 
28 29 30 31 32 
35 36 37 38 39 
43 44 46 47 48 
50 52 53 54 
55 56 57 58 59 
60 61 62 63 64 
65 66 68 69 70 
71 73 74 77 78 
79 80 81 82 84 
85 86 87 88 89 
91 92 93 94 95 
96 98 99 100 

*/