2017-3-9校内训练

这次轮到我出题,离上次我出题差不多有半年了,憋出了套质量还勉强的题。

以下是题解,因为自己出的,写的比较详细。题目均为原创。

题解

正解代码之后补。

T1.简单BFS送分题

#include<cstdio>
#define MN 5000
#define MX 25000000
const int o[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
char s[MN+5][MN+5];
int d[MN+5][MN+5],hd,tl;
short qx[MX+5],qy[MX+5];
void work(int x,int y)
{
    int i,xx,yy;
    for(int i=0;i<4;++i)
    {
        xx=x+o[i][0];yy=y+o[i][1];
        if(!s[xx][yy]||d[xx][yy])continue;
        d[xx][yy]=d[x][y];
        if(s[xx][yy]=='0')work(xx,yy);
        else qx[++tl>=MX?tl-=MX:tl]=xx,qy[tl]=yy;
    }
}
int main()
{
    freopen("secret.in","r",stdin);
    freopen("secret.out","w",stdout);
    int n,m,i,x,y;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;++i)scanf("%s",s[i]+1);
    for(qx[0]=qy[0]=d[1][1]=1;!d[n][m];++hd>=MX?hd-=MX:0)
    {
        x=qx[hd];y=qy[hd];
        if(s[x][y]>'0')++d[x][y],--s[x][y];
        if(s[x][y]=='0')work(x,y);
        else qx[++tl>=MX?tl-=MX:tl]=x,qy[tl]=y;
    }
    printf("%d",d[n][m]+s[n][m]-'0'-1);
    fclose(stdin);fclose(stdout);return 0;
}

T2.简单数学和乱搞

#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
#define ll long long
#define MN 1000000000000LL
#define MX 30000
const ll ex[101]={1000876191844LL,1021057704676LL,1041855028369LL,1063295070244LL,1085407665241LL,1108206765796LL,1131740651889LL,1156035686481LL,1181119024849LL,1207027427904LL,1233803328289LL,1261474907716LL,1290091615684LL,1319700083524LL,1350331845877LL,1382047116025LL,1414888839081LL,1448920171521LL,1484201285284LL,1520779773601LL,1558729777081LL,1598120732224LL,1639027260025LL,1681524253696LL,1725697340964LL,1771632874729LL,1819423299600LL,1869186621124LL,1921015404049LL,1975036729600LL,2031368918121LL,2090143907289LL,2151516908025LL,2215626296004LL,2282649592336LL,2352766380129LL,2426170718689LL,2503062559449LL,2583664034884LL,2668230774784LL,2757024464041LL,2850319747521LL,2948449581025LL,3051736474084LL,3160537284100LL,3275285550625LL,3396398356624LL,3524341646329LL,3659676128784LL,3802968014400LL,3954832232976LL,4115996979264LL,4287222854721LL,4469355387225LL,4663366827289LL,4870301679376LL,5091327421609LL,5327764158025LL,5581061330329LL,5852891525625LL,6145074113476LL,6459730560000LL,6799186625625LL,7166141611225LL,7563644043264LL,7995180380625LL,8464759649476LL,8976974745600LL,9537170709361LL,10151507216449LL,10827232306576LL,11572767123129LL,12398095914649LL,13315047742441LL,14337673126144LL,15482847780625LL,16771007038564LL,18226922490000LL,19881156051241LL,21771388024324LL,23944746995569LL,26460766864009LL,29395448967049LL,32847192175009LL,36944892089361LL,41860589440576LL,47827390590225LL,55167949365169LL,64338911491716LL,76002878869369LL,91155378375125LL,111337613168896LL,139050509313024LL,178563328092081LL,237693046786209LL,331912794888676LL,495709296120900LL,819398697269881LL,1603210729132996LL,4501216504303236LL,23333333333333333LL};
inline ll read()
{
    ll x,u=0;char c;
    while((c=getchar())<'0'||c>'9');
    for(x=c-'0';(c=getchar())>='0'&&c<='9';){x=(x<<3)+(x<<1)+c-'0';if(x>ex[100])u=1;}
    return u?ex[100]:x;
}
struct node
{
    ll z,p;
    friend bool operator<(node a,node b){return a.z>b.z;}
};
priority_queue<node> pq;
ll a[MX+5];int b[MX+5],cnt;
ll cal(double x){return round(x*1e8);}
int main()
{
    freopen("password.in","r",stdin);
    freopen("password.out","w",stdout);
    int T,l,r,mid,ans;ll n,i;double s=0;
    for(i=2;i*i<=MN;++i)pq.push((node){i*i,i});
    while((i=pq.top().z)<=MN)
    {
        while(pq.top().z==i)
        {
            n=pq.top().p;pq.pop();
            pq.push((node){i*n,n});
            s+=1./i;
        }
        if(cal(s)>b[cnt])a[++cnt]=i,b[cnt]=cal(s);
    }
    for(i=0;i<=100;++i)a[++cnt]=ex[i],b[cnt]=b[cnt-1]+1;
    for(T=read();T--;)
    {
        n=read();
        for(l=0,r=cnt;l<=r;)
            if(a[mid=l+r>>1]<=n)ans=mid,l=mid+1;
            else r=mid-1;
        printf("%.8lf
",b[ans]/1e8);
    }
    fclose(stdin);fclose(stdout);return 0;
}

T3.基本数据结构

#include<cstdio>
char B[1<<26],*S=B,C;int X,F;
inline int read()
{
    while((C=*S++)<'0'||C>'9');
    for(X=C-'0';(C=*S++)>='0'&&C<='9';)X=(X<<3)+(X<<1)+C-'0';
    return X;
}
#define MN 500000
#define N 524288
#define L(x) c[x][0]
#define R(x) c[x][1]
#define G(f,x) (R(f)==x)
#define C(f,x) c[f][G(f,x)]
#define INF 0x7FFFFFFF
struct data{int x,s;}t[N*2+5];
data operator+(data a,data b)
{
    if(a.x==b.x)return (data){a.x,a.s+b.s};
    return a.s>b.s?(data){a.x,a.s-b.s}:(data){b.x,b.s-a.s};
}
void change(int k,int x){for(t[k+=N]=(data){x,1};k>>=1;)t[k]=t[k<<1]+t[(k<<1)+1];}
int query(int l,int r)
{
    data res=(data){0,0};
    for(l+=N-1,r+=N+1;l^r^1;l>>=1,r>>=1)
    {
        if(~l&1)res=res+t[l+1];
        if( r&1)res=res+t[r-1];
    }
    return res.x;
}
int fa[MN*2+5],c[MN*2+5][2],s[MN*2+5],p[MN*2+5],a[MN+5];
inline int ran()
{
    static int x=23333;
    return x^=x<<13,x^=x>>17,x^=x<<5;
}
inline void up(int x){s[x]=s[L(x)]+s[R(x)]+1;}
void rotate(int x)
{
    int f=fa[x],ff=fa[f],l=G(f,x),r=l^1;
    fa[f]=x;fa[x]=ff;fa[c[x][r]]=f;
    C(ff,f)=x;c[f][l]=c[x][r];c[x][r]=f;
    up(f);up(x);
}
void ins(int f,int t,int x)
{
    while(c[f][t])++s[f=c[f][t]],t=x>f;
    c[f][t]=x;fa[x]=f;s[x]=1;p[x]=ran();
    while(p[x]>p[fa[x]])rotate(x);
}
void del(int x)
{
    while(L(x)||R(x))rotate(p[L(x)]>p[R(x)]?L(x):R(x));
    C(fa[x],x)=0;for(int i=x;i=fa[i];)up(i);
    fa[x]=L(x)=R(x)=0;
}
int sum(int x,int z)
{
    if(!x)return 0;
    if(x>z)return sum(L(x),z);
    return s[L(x)]+1+sum(R(x),z);
}
int main()
{
    freopen("president.in","r",stdin);
    freopen("president.out","w",stdout);
    fread(B,1,1<<26,stdin);
    int n,m,i,l,r,s;
    n=read();m=read();
    p[0]=-INF;for(i=1;i<=MN;++i)p[MN+i]=INF;
    for(i=1;i<=n;++i)t[i+N]=(data){a[i]=read(),1},ins(MN+a[i],0,i);
    for(i=N;--i;)t[i]=t[i<<1]+t[(i<<1)+1];
    while(m--)
    {
        l=read();r=read();i=query(l,r);s=read();
        if(sum(L(MN+i),r)-sum(L(MN+i),l-1)<=(r-l+1)>>1)i=s;
        printf("%d
",i);
        for(l=read();l--;)change(r=read(),i),del(r),ins(MN+(a[r]=i),0,r);
    }
    l=query(1,n);if(sum(L(MN+l),n)<=n/2)l=-1;
    printf("%d
",l);
    fclose(stdin);fclose(stdout);return 0;
}
原文地址:https://www.cnblogs.com/ditoly/p/20170309C.html