YL秋季第一次模拟侧

· · 个人记录

20260930模拟赛总结

整体回顾

这次考的不错,继续加油。

我觉得这次考的比较好的原因是因为,我的做题顺序,和时间管理。首先,把每个题花15~20分钟浏览,简单构思一下思路,然后去做自己认为比较简单的第二题。20分钟写完代码,直接去看第一题,发现第一题可以使用图论去模拟找亲戚的过程。35分钟写完代码,美美暴力去了。最后剩下10分钟左右的检查时间。

题目复盘

T1:P10124 [USACO18OPEN] Family Tree B


嗯,这个题是本次比赛中比较良心的题目了。我就讲讲我的思路。
首先,有很多关系,对于我这种搞不清辈分的小蒟蒻还是有点难度,但是好在题目给了图片。

和老师解法的区别:我觉得老师的解法可能跟简洁,但我觉得我的解法的思维难度是更低的,主要就是有点考验代码能力。

:::success[ACcode]

#include <bits/stdc++.h>
using namespace std;
int n;
string a,b;
map<string,string>mm;
int get_d(string hz,string k)
{
    int cnt=0;
    string cur=hz;
    while(mm.count(cur))
    {
        cur=mm[cur];
        cnt++;
        if(cur==k) return cnt;
    }
    return -1;
}
set<string>zuxian(string x)//求所有牛的祖先 
{
    set<string>res;
    string cur=x;
    while(mm.count(cur))
    {
        cur=mm[cur];
        res.insert(cur);
    }
    return res;
}
string get_mm(int x)
{
    string res="";
    for(int i=1;i<=x-2;i++)
    {
        res+="great-";
    } 
    return res;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0); 
    cin >> n;
    cin >> a >> b;
    for(int i=1;i<=n;i++)
    {
        string x,y;
        cin >> x >> y;
        mm[y]=x;
    }
    int disa=get_d(a,b);
    int disb=get_d(b,a);
    //1.直系祖先 
    if(disa!=-1) 
    {
        if(disa==1)
        {
            cout << b << " is the mother of " << a;
        }
        else if(disa==2)
        {
            cout << b << " is the grand-mother of " << a;
        }
        else
        {
            cout << b << " is the " << get_mm(disa) << "grand-mother of " << a;
        }
        return 0;
    }
    if(disb!=-1) 
    {
        if(disb==1)
        {
            cout << a << " is the mother of " << b << "\n";
        }
        else if(disb==2)
        {
            cout << a << " is the grand-mother of " << b;
        }
        else
        {
            cout << a << " is the " << get_mm(disb) << "grand-mother of " << b;
        }
        return 0; 
    }   
    //2.其他关系 
    set<string>seta=zuxian(a);
    set<string>setb=zuxian(b);
    string mma="";
    string mmb="";
    if(mm.count(a)) mma=mm[a];
    if(mm.count(b)) mmb=mm[b];
    if(mma!=""&&mmb!=""&&mma==mmb)//姐妹 
    {
        cout << "SIBLINGS";
        return 0;
    } 
    if(mma!="")
    {
        int d=get_d(b,mma);
        if(d!=-1)
        {
            if(d==1)
            {
                cout << a << " is the aunt of " << b;
            }
            else
            {
                cout << a << " is the " << get_mm(d) << "aunt of " << b;
            }
            return 0;
        }
    } 
    if(mmb!="")
    {
        int d=get_d(a,mmb);
        if(d!=-1)
        {
            if(d==1)
            {
                cout << b << " is the aunt of " << a;
            }
            else
            {
                cout << b << " is the " << get_mm(d) << "aunt of " << a;
            }
            return 0;
        }
    }     
    //3.没有关系 
    bool f=0;
    for(auto t:seta)
    {
        if(setb.count(t))
        {
            f=1;
            break;
        }
    }
    if(f)
    {
        cout << "COUSINS";
    }
    else
    {
        cout << "NOT RELATED";
    }
    return 0;
} 

:::

T2:P1694 [USACO19FEB] The Great Revegetation B

本场最简单的题目,我第一个做出来的题目。

接下来就是代码,我觉得代码更好理解些。 :::success[ACcode]

#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int>ve[105];
int a[105];
int t[5];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n,m;
    cin >> n >> m;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        cin >> x >> y;
        ve[x].push_back(y);
        ve[y].push_back(x);
    } 
    for(int i=1;i<=n;i++)
    {
        t[1]=t[2]=t[3]=t[0]=0;
        for(int j=0;j<ve[i].size();j++)
        {
            t[a[ve[i][j]]]++;
        }
        for(int j=1;j<=4;j++)
        {
            if(!t[j]) 
            {
                cout << j;
                a[i]=j;
                break;
            }
        }
    }
    return 0;
}

:::

T3:P6148 [USACO20FEB] Swapity Swapity Swap S

这个题我本来是可以拿到暴力分的,但是格式错误,哎算了。

想过很多解法但都没写,最后去写另外有思路的题目去了,就骗了点分。 那就说说正解吧。
算出平面上所有符合两条直角边分别平行于X轴和Y轴的直角三角形之和的两倍。直角三角形最特殊的顶点是什么?那肯定就是直角顶点,我们可以去枚举所有的直角点去连其他两点,其他两点要满足:一个同x坐标,一个同y坐标。我们算出两条直角边长不就可以算面积了吗?然后,这样一个一个去找太麻烦了,我们可以优化一下:前缀和——首先把所有的点进行排序,以x坐标为第一关键字,y坐标为第二关键字,这样,所有x坐标相同的点就排在一起,这样我们就对y坐标进行前缀和,这样就可以快速算出任意一个点到同列其他所有点的距离之和。(反过来同理)。
这样算下来,其实时间最多的就是排序了。O(n·log n)
:::success[ACcode]

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
const int MAXN=100005;
struct Node
{
    int x,y,sum_x,sum_y;
}p[MAXN];
bool cmpx(const Node &a,const Node &b)
{
    if(a.x!=b.x) return a.x<b.x;
    return a.y<b.y;
}
bool cmpy(const Node &a,const Node &b)
{
    if(a.y!=b.y) return a.y<b.y;
    return a.x<b.x;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    for(int i=1;i<=n;i++) cin >> p[i].x >> p[i].y;
    sort(p+1,p+n+1,cmpx);
    for(int i=1;i<=n;)
    {
        int j=i;
        while(j<=n&&p[j].x==p[i].x)
        {
            j++;
        }
        int pre=0,s=0;
        for (int k=i;k<j;k++)
        {
            p[k].sum_y=(k-i)*p[k].y-pre;
            pre+=p[k].y;
        }
        for(int k=j-1;k>=i;k--)
        {
            p[k].sum_y+=s-(j-1-k)*p[k].y;
            s+=p[k].y;
        }
        i=j;
    }
    sort(p+1,p+n+1,cmpy);
    for(int i=1;i<=n;)
    {
        int j=i;
        while(j<=n&&p[j].y==p[i].y)
        {
            j++;
        }
        int pre=0,s=0;
        for(int k=i;k<j;k++)
        {
            p[k].sum_x=(k-i)*p[k].x-pre;
            pre+=p[k].x;
        }
        for(int k=j-1;k>=i;k--)
        {
            p[k].sum_x+=s-(j-1-k)*p[k].x;
            s+=p[k].x;
        }
        i=j;
    }
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        int tot=(p[i].sum_y%mod)*(p[i].sum_x%mod)%mod;
        ans=(ans+tot)%mod;
    }
    cout << (ans+mod)%mod;
    return 0;
}

:::

整体反思

对于时间:我觉得会比之前做得更好,每个题都看了、想了,优先做了有把握能拿分的题目。继续努力。
审题方面与检查:我觉得有待提升,主要是第三题,如果再细心仔细一点可能会拿到更好的成绩。