【考试】9.18

最近这题解是真不想写,

正好这次不太难,就放个代码吧

1>比赛

新奇的方法,但是我不想分析

精度操作很烦人

#include
#include
#include
using namespace std;
int n;
const int N=5e4+3;
long long a[N],b[N];
long long sum[N],sq_sum[N],ans;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    for(int i=1;i<=n;i++) scanf("%lld",&b[i]);
    sort(a+1,a+n+1),sort(b+1,b+n+1);
    
    for(int i=1;i<=n;i++)
        sum[i]=sum[i-1]+b[i],sq_sum[i]=sq_sum[i-1]+b[i]*b[i];
    
    for(int i=1;i<=n;i++)
    {
        int pos=upper_bound(b+pos,b+n+1,a[i])-b-1;
        ans+=a[i]*a[i]*pos +sq_sum[pos] -2*a[i]*sum[pos] ;
        ans-=a[i]*a[i]*(n-pos) +(sq_sum[n]-sq_sum[pos]) -2*a[i]*(sum[n]-sum[pos]);
    }
    
    double t=ans*1.0/n;
    printf("%.1lf\n",t);
    return 0;
} 

2>数字

乘法原理,容斥原理,dp

//dfs递归->超时
//全排列->失败
//递推->0.3s->ok!

//两种条件,本质相似,但是计数会重复(或运算)
//条件只有一个,sum相等,所以就cnt*cnt
//然后*2

//再想交集
//额......  

#include
#include
#include
#include
#include
using namespace std;
int n,len;
char c[15];
const int N=1003,mod=999983;
int d[13],cnt[N][N*10];
long long ans[5];

void prepare()
{
    int mx;
    cnt[0][0]=1;
    for(int i=1;i<=n;i++)
    {
        mx=i*9;
        for(int j=1;j<=len;j++)
            for(int k=d[j];k<=mx;k++)
                cnt[i][k]=(cnt[i][k] +cnt[i-1][k-d[j]])%mod;
    }
}
void work(int pos,int k)
{
    int mx=k*9;
    for(int i=0;i<=mx;i++) ans[pos]=(ans[pos]+1LL*cnt[k][i]*cnt[k][i]%mod)%mod;
}

int main()
{
    scanf("%d%s",&n,c+1) ;
    //clock_t startTime,endTime;
    //startTime = clock();
    len=strlen(c+1);
    for(int i=1;c[i];i++) d[i]=c[i]-'0';
    prepare();
    
    work(0,n);
    ans[0]<<=1;
    work(1,n>>1);
    work(2,n+1>>1);
    
    printf("%lld\n",((ans[0]-ans[1]*ans[2])%mod+mod)%mod);
    //endTime = clock();//计时结束
    //cout << "The run time is: " <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
    return 0;
} 

3>祖孙询问

水啊,LCA

#include
#include
#include
#include
using namespace std;
int n,m,rt,q;
const int N=4e4+3;
int dep[N],fa[N][17];
vector <int> g[N];
void dfs(int u,int ff)
{
    fa[u][0]=ff,dep[u]=dep[ff]+1;
    for(int i=1;i<17 && fa[u][i-1];i++)
        fa[u][i]=fa[fa[u][i-1]][i-1];
    
    int sz=g[u].size() ;
    for(int i=0;i)
        if(g[u][i]!=ff) dfs(g[u][i],u);
}
void query(int u,int v)
{
    if(!dep[u] || !dep[v]) 
    {
        printf("0\n");
        return ;
    }
    
    int flag=2;
    if(dep[u]1;
    int dis=dep[u]-dep[v];
    for(int i=1,j=0;i<=dis;i<<=1,j++)
        if(dis&i) u=fa[u][j];
    if(u==v) printf("%d\n",flag);
    else printf("0\n");
}
int main()
{
    scanf("%d",&n);
    int u,v;
    while(n--)
    {
        scanf("%d%d",&u,&v);
        if(v==-1) rt=u;
        else g[u].push_back(v),g[v].push_back(u);  
    }
    dfs(rt,0);
    
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d%d",&u,&v);
        query(u,v);
    }
    
    return 0;
}
#include
#include
#include
using namespace std ;
int n ;
const int N = 5e4+3 ;
long long a [ N ] , b [ N ] ;
long long sum [ N ] , sq_sum [ N ] , ans ;
 
int main ( )
{
scanf ( " %d " , & n ) ;
for ( int i = 1 ; i <= n ; i ++ ) scanf ( " %lld " , & a [ i ]) ;
for ( int i = 1 ; i <= n ; i ++ ) scanf ( " %lld " , & b [ i ]) ;
sort ( a +1 , a + n +1 ) , sort ( b +1 , b + n +1 ) ;
 
for ( int i = 1 ; i <= n ; i ++ )
sum [ i ] = sum [ i -1 ] + b [ i ] , sq_sum [ i ] = sq_sum [ i -1 ] + b [ i ] * b [ i ] ;
 
for ( int i = 1 ; i <= n ; i ++ )
{
int pos = upper_bound ( b + pos , b + n +1 , a [ i ]) - b -1 ;
ans += a [ i ] * a [ i ] * pos + sq_sum [ pos ] -2 * a [ i ] * sum [ pos ] ;
ans -= a [ i ] * a [ i ] * ( n - pos ) + ( sq_sum [ n ] - sq_sum [ pos ]) -2 * a [ i ] * ( sum [ n ] - sum [ pos ]) ;
}
 
double t = ans * 1.0 / n ;
printf ( " %.1lf\n " , t ) ;
return 0 ;
}

 

你可能感兴趣的:(【考试】9.18)