第八届福建省大学生程序设计竞赛训练总结【7/12】

本文通过实战案例,详细解析了几道竞赛题目的解题思路与代码实现,涵盖了计算几何、图论及期望等算法知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

训练结果:Rank 32 

Ac题数:6

tot time:1049





A.水题。鸡兔同笼问题,保证有解,小学六年级问题。

被I64d卡了三发= =.


B.计算几何,窝不会,队长会丫。


队长Ac代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
#define LL long long int

const int N = 1e5 + 7;
const int MOD = 1e9+7;

/**********************************/

struct point{
    double x,y;
};

struct trangle{
    point p[3];
}c[2];

double multi(point p1,point p2,point p0){
    return fabs((p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x));
}

double area(point a,point b,point c){
    return multi(a,b,c);
}
int sum;

// 判断c1 在不在c2 里
bool cantian(trangle c1,trangle c2){
    double ac2 =area(c2.p[0],c2.p[1],c2.p[2]);
    int t =0 ;
    for(int i=0;i<=2;i++)
        if(area(c1.p[i],c2.p[0],c2.p[1])+area(c1.p[i],c2.p[1],c2.p[2])+
           area(c1.p[i],c2.p[0],c2.p[2]) > ac2);
        else sum++,t++;
    return t==3;
}

void solve(){
    sum = 0;
    if(cantian(c[0],c[1])||cantian(c[1],c[0])){
        puts("contain");
        return ;
    }
    if(sum == 0){
        puts("disjoint");
        return ;
    }
    puts("intersect");
    return ;
}

int main(){
    int _ = 1,kcase = 0;
    for(scanf("%d",&_);_--;){
        for(int i=0;i<=1;i++)for(int j=0;j<=2;j++)
            scanf("%lf%lf",&c[i].p[j].x,&c[i].p[j].y);
        solve();
    }
    return 0;
}


C.

D.一道挺好的思维KMP的题目,我是萌萌哒D题题解


E.

F.窝不会啊,队长会啊,贼强啊。

队长Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long int
int n;
const int N=3e5+7;
const int MOD=1e9+7;
struct edge{
    int to,next;
}G[N<<1];

int head[N],cntG;

void add(int u,int v){
    G[cntG].to=v,G[cntG].next=head[u],head[u]=cntG++;
    G[cntG].to=u,G[cntG].next=head[v],head[v]=cntG++;
}

LL bit[N][3];
#define lowbit(x) (x&-x)
void update(int i,int v,int x){
    for(;i&&i<=n;i+=lowbit(i)){
        bit[i][x]+=v; bit[i][x]%=MOD;
    }
}
LL getSum(int i,int x){
    LL ans = 0;
    for(;i;i-=lowbit(i))
        ans+=bit[i][x];
    return ans%=MOD;
}

int dep[N],fa[N],son[N],sz[N];
void dfs(int u,int f,int d){
    dep[u]=d,fa[u]=f,son[u]=0,sz[u]=1;
    for(int i=head[u],to;i!=-1;i=G[i].next){
        to=G[i].to;
        if(to == f) continue;
        dfs(to,u,d+1);
        sz[u]+=sz[to];
        if(sz[to]>sz[son[u]]) son[u]=to;
    }
}

int top[N],tree[N],tot;
void dfs2(int u,int tp){
    tree[u]=++tot;top[u]=tp;
    if(son[u]) dfs2(son[u],tp);
    else return ;
    for(int i=head[u],to;i!=-1;i=G[i].next){
        to=G[i].to;
        if(to==fa[u]||to==son[u]) continue;
        dfs2(to,to);
    }
}

void solve(int x){
    int fx=top[x],t=dep[x];
    LL sum1 = 0,sum2 = 0,sum3 = 0;
    while(fx!=1){
        sum1 = (sum1+getSum(tree[x],0)-getSum(tree[fx]-1,0)+MOD)%MOD;
        sum2 = (sum2+getSum(tree[x],1)-getSum(tree[fx]-1,1)+MOD)%MOD;
        sum3 = (sum3+getSum(tree[x],2)-getSum(tree[fx]-1,2)+MOD)%MOD;
        x=fa[fx],fx=top[x];
    }
    sum1 = (sum1+(getSum(tree[x],0)-getSum(tree[1]-1,0))+MOD)%MOD;
    sum2 = (sum2+(getSum(tree[x],1)-getSum(tree[1]-1,1))+MOD)%MOD;
    sum3 = (sum3+(getSum(tree[x],2)-getSum(tree[1]-1,2))+MOD)%MOD;

    sum1*=t;sum1%=MOD;
    sum2-=sum1;sum2=(sum2%MOD+MOD)%MOD;
    sum2+=sum3;(sum2%=MOD);

    printf("%I64d\n",sum2);
    return ;
}
int read()
{
    int res = 0, ch, flag = 0;

    if((ch = getchar()) == '-')             //判断正负
        flag = 1;

    else if(ch >= '0' && ch <= '9')           //得到完整的数
        res = ch - '0';
    while((ch = getchar()) >= '0' && ch <= '9' )
        res = res * 10 + ch - '0';

    return flag ? -res : res;
}
int main(){
    int _ = 1,kcase = 0;
    for(scanf("%d",&_);_--;){
        memset(head,-1,sizeof(head));
        memset(bit,0,sizeof(bit));

        n=read();cntG=0;
        for(int i=2,x;i<=n;i++){
            x=read();add(i,x);
        }

        dfs(1,0,1);
        tot=0,dfs2(1,1);

        int q,op,v,x,k;
        for(q=read();q--;){
            op=read(),v=read();
            if(1 == op){
                x=read(),k=read();
                update(tree[v],k,0);
                update(tree[v],(LL)k*dep[v]%MOD,1);
                update(tree[v],x,2);
            }
            else solve(v);
        }
    }
    return 0;
}


G.一道很好的期望思维题,将整体拆分成若干个独立事件去求期望,求和的一种解题思路,我是萌萌哒G题题解


H.

I.

J.

K.错排+Cnm,不难。我是萌萌哒K题题解


L.爆搜即可。我是萌萌哒L题题解


队长Ac代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
#define LL long long int

const int N = 1e5 + 7;
const int MOD = 1e9+7;

/**********************************/

int a[10][10];


bool judge(int x){
    if(x==a[1][1]&&a[1][1]==a[1][2]&&a[1][2]==a[1][3])return true;
    if(x==a[2][1]&&a[2][1]==a[2][2]&&a[2][2]==a[2][3])return true;
    if(x==a[3][1]&&a[3][1]==a[3][2]&&a[3][2]==a[3][3])return true;

    if(x==a[1][1]&&a[1][1]==a[2][1]&&a[2][1]==a[3][1])return true;
    if(x==a[1][2]&&a[1][2]==a[2][2]&&a[2][2]==a[3][2])return true;
    if(x==a[1][3]&&a[1][3]==a[2][3]&&a[2][3]==a[3][3])return true;

    if(x==a[1][1]&&a[1][1]==a[2][2]&&a[2][2]==a[3][3])return true;
    if(x==a[1][3]&&a[1][3]==a[2][2]&&a[2][2]==a[3][1])return true;
    return false;
}

bool Kim2(int x){
    int flag = 0;
    for(int i=1;i<=3;i++){
        for(int j=1;j<=3;j++){
            if(a[i][j]==0){
                a[i][j]=x;
                if(judge(x)) flag++;
                a[i][j]=0;
            }
        }
    }
    return flag>=2;
}

bool Kim1(int x){
    for(int i=1;i<=3;i++){
        for(int j=1;j<=3;j++){
            if(a[i][j]==0){
                a[i][j]=x;
                if(judge(x)||Kim2(x))
                    return true;
                a[i][j]=0;
            }
        }
    }
    return false;
}

char s[10];
int main(){
    int _ = 1,kcase = 0;
    for(scanf("%d",&_);_--;){

        for(int i=1;i<=3;i++){
            for(int j=1;j<=3;j++){
                scanf("%s",s);
                if(s[0]=='.') a[i][j]=0;
                else if(s[0]=='x') a[i][j]=1;
                else a[i][j]=2;
            }
        }

        scanf("%s",s);int x;
        if(s[0]=='x')   x=1;
        else            x=2;

        if(judge(3-x)) puts("Cannot win!");
        else if(judge(x)||Kim1(x))
             puts("Kim win!");
        else puts("Cannot win!");
    }
    return 0;
}




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值