1008 simulation game

EDITORIAL

\ (Day1 \) , the exam moderate difficulty, I think starfishTheater .jpg

Rational tree T1 (the SBT)

link

Idea

\(Stern-Brocot\ Tree \to SBT\)

Easily found in each row \ (\ displaystyle \ frac {m } {n} \ lt \ frac {m + m '} {n + n'} \ lt \ frac {m '} {n'} \)

The figure also found that, when the \ (\ displaystyle \ frac {a } {b} \) is less than the current node, go left \ ((L) \) ; is greater than the current node, go right \ ((R & lt) \ )

And found that the situation down the road and there is no title, after removing the extra point, found it to be a complete binary tree (but did not use any eggs ts.png, please ignore this jargonyoul.pngyoul.png

Set the current node is \ (\ DisplayStyle \ FRAC {{n-m}} \) , then

\(\displaystyle \frac{a}{b} \gt \frac{m}{n}\)时,\(a \times n>b \times m\);

\(\displaystyle \frac{a}{b}<\frac{m}{n}\)时,\(a \times n<b \times m\)

According to this, the determination

When will it end?

When \ (a \ times n = b \ times m \) time. the reason is simple

At the end of \ (\ DisplayStyle \ FRAC {m} {n-} = \ FRAC {\ FRAC {A} {GCD (A, B)}} {\ FRAC {B} {GCD (A, B)}} \) , so \ (A \ B = n-Times \ Times m = \ DisplayStyle \ FRAC {A \ B} {Times GCD (A, B)} \) , (it should be true ... ...youl.png

A closer analysis of a wave, \ (A \ Times b \) seems to burst \ (int \) , so to open \ (Long \ Long \) .

Code

signed main(){
//  freopen(File".in","r",stdin);
//  freopen(File".out","w",stdout);
    int a=read(),b=read();
    int x=0,y=1;
    int m=1,n=1;
    int m1=1,n1=0;
    while(a*n!=b*m){         
        if(a*n<b*m){
            putchar('L');
            m1=m; n1=n;
        }
        else{
            putchar('R');
            x=m;y=n;
        }
        m=x+m1; n=y+n1;//按规则计算下一行的分子分母。
    }
    return 0;
}

Problems T2 string (string)

link

Idea

\ (20 \ PTS \) : Searching expected complexity \ (O (T \ ast k ) \!)

\ (50 \ PTS \) : FIG construction, the longest road running, with \ (Bellman-Ford / SPFA \ ) is determined negative ring, complexity \ (O (T \ ast k ^ 4) \)

\ (100 \ PTS \) : Create a new node to all points connected to the right side edge 0, or directly to all points from 0 began to run up the road. Complexity \ (O (T \ ast k ^ 3) \)

Code

int w[maxn][maxn];
int ans,dis[maxn]; 
signed main(){
//  freopen(File".in","r",stdin);
//  freopen(File".out","w",stdout);
    int T=read();
    while(T--){
        mem(dis,0); ans=0; //初始化很重要
        int k=read();
        for(int i=1;i<=k;i++)
        for(int j=1;j<=k;j++)
            w[i][j]=read();
        for(int l=1;l<=k;l++)
        for(int i=1;i<=k;i++)
        for(int j=1;j<=k;j++)
            dis[j]=max(dis[j],dis[i]+w[i][j]);
        bool flag=1;
        for(int i=1;i<=k;i++) 
        for(int j=1;j<=k;j++)
        if(dis[j]<dis[i]+w[i][j]) flag=0;
        if(flag){ 
            for(int i=1;i<=k;i++) 
            ans=max(ans,dis[i]);
            printf("%d\n",ans);
        }
        else puts("-1");
    }
    return 0;
}

T3 sequence (sequence)

link

Idea

Nature is violence on the test done, violent nature is not difficultTheater 1.jpg

For \ (50 \ PTS \) , using \ (ST \) table, the complexity of the \ (O (n ^ 2) \)

For \ (100 \ PTS \) , using the binary + partition idea;

  • To find the global minimum, the minimum processing across the range, and then split into two.

  • After a minimum and endpoint determination, the endpoint the other values ​​satisfy the conditions is determined, then simply count the number of occurrences of the corresponding weight range.

  • For each weight, record all positions in which they appear, and then half of you can know how many times it appears in a certain range.

  • Selecting a shorter side ensure enumeration complexity when enumerating endpoint, because each size is calculated representative of at least half, so a maximum of a calculated position \ (\ log n \) times

Complexity \ (O (n \ log ^ 2 n) \)

We have three strategies:

  • \ (O (n ^ 3) \) enumeration \ (L, R & lt \) , then enumeration \ (\ displaystyle \ min_ {l \ le k \ le r} a_k \)

Our formula is \ (A_L \ Oplus A_R \ Oplus A _ {\} min = D \) , we find the equivalent of a known three

  • Known \ (A_L, A _ {\ min}, D \) , seeking \ (A_R \) ;

  • Known \ (A_R, A _ {\ min}, D \) , seeking \ (A_L \)

More than two equivalent formulas

Code

//暴力,根据题意的暴力T_T
int a[maxn],ans;
inline int find(int l,int r){
    int res=inf;
    for(int i=l;i<=r;i++)
        res=min(res,a[i]);
    return res;
}
signed main(){
//  freopen(File".in","r",stdin);
//  freopen(File".out","w",stdout);
    int n=read(),D=read();  
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<=n;i++)
    for(int j=i;j<=n;j++){
        if((a[i]^a[j]^find(i,j))==D)
        ans++;
    }
    printf("%d",ans);
    return 0;
}
//该暴力复杂度O(n^3),预计20 pts,开O2可以有50pts。。。。
const int N=100005,M=1<<20;
int T,n,D;
int a[N];
int lc[N],rc[N];
int stk[N],tp;
vi bac[M];
LL ans;
void solve(int w,int l,int r){
    if(w-l<r-w){
        for(int i=l;i<=w;++i){
            int v=D^a[i]^a[w];
            ans+=lower_bound(bac[v].begin(),bac[v].end(),r+1)-lower_bound(bac[v].begin(),bac[v].end(),w);
        }
    }else{
        for(int i=w;i<=r;++i){
            int v=D^a[i]^a[w];
            ans+=lower_bound(bac[v].begin(),bac[v].end(),w+1)-lower_bound(bac[v].begin(),bac[v].end(),l);
        }
    }
    if(lc[w])solve(lc[w],l,w-1);
    if(rc[w])solve(rc[w],w+1,r);
}
int main(){
    freopen(File".in","r",stdin);
    freopen(File".out","w",stdout);
    n=read();D=read();ans=0;
    for(int i=0;i<N;++i)bac[i].clear();
    for(int i=1;i<=n;++i){
        a[i]=read();bac[a[i]].push_back(i);
        if(!tp||a[i]>=a[stk[tp]])stk[++tp]=i;
        else{
            for(;tp&&a[i]<a[stk[tp]];--tp)rc[stk[tp-1]]=stk[tp];
            lc[i]=stk[tp+1];stk[++tp]=i;
        }
    }
    for(;tp;--tp)rc[stk[tp-1]]=stk[tp];
    solve(rc[0],1,n);
    printf("%lld\n",ans);
    fclose(stdin);fclose(stdout);
    return 0;
}

\[ The \quad End \]

\ [\ Text {she gradually forgot me, but she does not know; I'm black and blue day are not loved. - "The girl said to me,"} \]

Guess you like

Origin www.cnblogs.com/cbyyc/p/11666978.html