跳到正文

个人记录

雅礼兴趣小组总结-4

wuweize150815约 38 分钟
原文
文章信息与版本

文章信息

发布
2026年10月9日
收录
2026年10月9日
更新
2026年11月11日
篇幅
约 38 分钟

版本

  1. v3当前
  2. v2
  3. v1

总结

习题课(兴趣小组)

Not Adjacent Matrix

题意

将一个n×nn \times n的格子分别填上1∼n×n1 \sim n \times n的数,使相邻的两个格子中的数不相邻。如果可以做到,输出矩阵,否则输出−1-1

知识点

构造

正解

设行为xx,列为yy。现将(x+y)mod  2=0(x+y) \mod 2=0的格子依次填 上数字,剩余的接着填。

注意

22是特例,无法做到,输出−1-1

错因

我好像没错

心得

??????

复杂度
  • 时间复杂度:O(N2)O(N^2)
  • 空间复杂度:O(N2)O(N^2)
代码
C++
#include <bits/stdc++.h>
using namespace std;
int a[105][105];
int Q , n;
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> Q;
    while (Q--)
    {
        memset (a , 0 , sizeof (a));
        cin >> n;
        if (n==2)
        {
            cout << -1 << "\n";
            continue;
        }
        int now=0;
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=n;j++)
            {
                if ((i+j)%2==0)
                {
                    now++;
                    a[i][j]=now;
                }
            }
        }
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=n;j++)
            {
                if (a[i][j]==0)
                {
                    now++;
                    a[i][j]=now;
                }
                cout << a[i][j] << " ";
            }
            cout << "\n";
        }
    }
    return 0;
}

Same Differences

题意

求有几对(i,j)(i,j)满足a[i]−a[j]=i−ja[i]-a[j]=i-j

知识点

方程,mapmap

正解

化简:a[i]−i=a[j]−ja[i]-i=a[j]-j,统计每个只出现多少次,再每个的数量选出两个的方案数的总和

错因

我好像没错

心得

??????

复杂度
  • 时间复杂度:O(Nlog⁡2N)O(N \log_2 N)
  • 空间复杂度:O(N)O(N)
代码
C++
#include <bits/stdc++.h>
using namespace std;
map <int , int> a;
int Q , n , v;
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> Q;
    while (Q--)
    {
        a.clear ( );
        cin >> n;
        for (int i=1;i<=n;i++)
        {
            cin >> v;
            a[v-i]++;
        }
        long long cnt=0;
        for (auto i:a)
        {
            cnt+=(long long) i.second*(i.second-1)/2;
        }
        cout << cnt << "\n";
    }
    return 0;
}

Arranging The Sheep

题意

每个*可以往左边或者右边的.挪一个。求至少要挪几下才能使所有的*连在一起

知识点

贪心

正解

有tottot个*那么所有的*全部往第⌈tot2⌉\lceil \frac{tot}{2} \rceil点靠拢,再计算即可

错因

我好像没错

心得

??????

复杂度
  • 时间复杂度:O(N)O(N)
  • 空间复杂度:O(N)O(N)
代码
C++
#include <bits/stdc++.h>
using namespace std;
string s;
int cnt[1000005];
int Q , n;
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> Q;
    while (Q--)
    {
        cin >> n >> s;
        s=" "+s;
        int tot=0;
        for (int i=1;i<=n;i++)
        {
            if (s[i]=='*')
            {
                cnt[++tot]=i;
            }
        }
        long long ans=0 , S=(tot+1)/2;
        int res=cnt[S];
        for (int i=1;i<=S-1;i++)
        {
            ans+=(res-(S-i))-cnt[i];
        }
        for (int i=S+1;i<=tot;i++)
        {
            ans+=cnt[i]-(res+(i-S));
        }
        cout << ans << "\n";
    }
    return 0;
}

Corrupted Array

题意

先有一个aa数组,有nn个数下标分别为1∼n1 \sim n,a[n+1]a[n+1]=∑i=1na[i]\sum_{i=1}^n a[i],a[n+2]a[n+2]为随机抽取。将aa打乱得到bb数组。已知bb数组,求任意一个可能的aa数组

知识点

思想,mapmap

正解

a[n+1]a[n+1]只可能为n+1∼n+2n+1 \sim n+2个数中,所以∑i=1n+1b[i]=a[n+1]×2+a[n+2]\sum_{i=1}^{n+1} b[i]=a[n+1] \times 2+a[n+2],最后判断减去两倍后是否有剩下的那个数

错因

我好像没错

心得

??????

复杂度
  • 时间复杂度:O(Nlog⁡2N)O(N \log_2 N)
  • 空间复杂度:O(N)O(N)
代码
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[200005];
map <int , int> mp1 , mp2;
int Q , n;
signed main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> Q;
    while (Q--)
    {
        mp1.clear ( );
        mp2.clear ( );
        cin >> n;
        n+=2;
        long long sum=0;
        for (int i=1;i<=n;i++)
        {
            cin >> a[i];
            sum+=a[i];
        }
        sort (a+1 , a+n+1);
        for (int i=1;i<=n-1;i++)
        {
            mp1[a[i]]++;
        }
        for (int i=1;i<=n-2;i++)
        {
            mp2[a[i]]++;
        }
        mp2[a[n]]++;
        if (mp1.count (sum-a[n]*2))
        {
            mp1[sum-a[n]*2]--;
            for (auto i:mp1)
            {
                for (int j=1;j<=i.second;j++)
                {
                    cout << i.first << " ";
                }
            }
        }
        else if (mp2.count (sum-a[n-1]*2))
        {
            mp2[sum-a[n-1]*2]--;
            for (auto i:mp2)
            {
                for (int j=1;j<=i.second;j++)
                {
                    cout << i.first << " ";
                }
            }
        }
        else
        {
            cout << -1 << " ";
        }
        cout << "\n";
    }
    return 0;
}

Permutation by Sum

题意

构造一个nn的排列使∑i=lr=s\sum_{i=l}^r=s,无法构造输出−1-1

知识点

思想

正解

毕竟n≥500n \ge 500,5002=2.5×105500^2=2.5 \times 10^5,完全可以用O(N2)O(N^2)的方法。如果填最小的还是多了或者填最大的还是小了,输出−1-1,其他情况都能满足。先将l∼rl \sim r中的每一个数分别1,2,⋯r−l+11,2, \cdots r-l+1,ss次操作,每次操作最后一个满足条件的数,就+1+1,最后将没出现的数分配到两边即可

错因

两边都要来一次,我的数没增加。发WAWA了,这时:比赛已经结束了

心得

应该要写出来的⋯\cdots

复杂度
  • 时间复杂度:O(N2)O(N^2)
  • 空间复杂度:O(N)O(N)
代码
C++
#include <bits/stdc++.h>
using namespace std;
bool vis[505];
int b[505];
int Q , n , l , r , s , tot;
int main ( )
{
   ios::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   cin >> Q;
   while (Q--)
   {
       cin >> n >> l >> r >> s;
       long long sum=0;
       for (int i=l;i<=r;i++)
       {
           b[i]=i-l+1;
           sum+=b[i];
       }
       if (sum>s)
       {
           cout << -1 << "\n";
       }
       else
       {
           int now=r;
           int len=r-l+1;
           while (sum<s)
           {
               if (b[now]==n-(r-now)) now--;
               if (now<l) break;
               b[now]++;
               sum++;
           }
           if (sum==s)
           {
               memset (vis , 0 , sizeof (vis));
               for (int i=l;i<=r;i++)
               {
                   vis[b[i]]=1;
               }
               int tot=1;
               for (int i=1;i<=l-1;i++)
               {
                   while (vis[tot]==1)
                   {
                       tot++;
                   }
                   cout << tot << " ";
                   tot++;
               }
               for (int i=l;i<=r;i++)
               {
                   cout << b[i] << " ";
               }
               for (int i=r+1;i<=n;i++)
               {
                   while (vis[tot]==1)
                   {
                       tot++;
                   }
                   cout << tot << " ";
                   tot++;
               }
               cout << "\n";
           }
           else
           {
               cout << -1 << "\n";
           }
       }
   }
   return 0;
}

Education

题意

等级为xx是一天赚a[x]a[x]元,每天可以扣b[x]b[x]升一级,求最少要几天能让手中的钱≥c\ge c

知识点

贪心

正解

记下至少要多少天才能升到ii级和剩余的钱数,再递推取min⁡\min

错因

没有做

心得

每道题都要做到

复杂度
  • 时间复杂度:O(N)O(N)
  • 空间复杂度:O(N)O(N)
代码
C++
#include <bits/stdc++.h>
using namespace std;
int a[200005] , b[200005];
int Q , n , c;
int CNT (int x , int y)
{
    return (x+y-1)/y;
}
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> Q;
    while (Q--)
    {
        cin >> n >> c;
        long long minn=1e18 , now=0 , sum=0;
        for (int i=1;i<=n;i++)
        {
            cin >> a[i];
        }
        for (int i=1;i<=n-1;i++)
        {
            cin >> b[i];
        }
        for (int i=1;i<=n-1;i++)
        {
            minn=min (minn , now+CNT (c-sum , a[i]));
            int cnt=CNT (b[i]-sum , a[i]);
            now+=cnt+1;
            sum=sum+cnt*a[i]-b[i];
            // cout << now << " " << cnt << " " << sum << "\n";
        }
        minn=min (minn , now+CNT (c-sum , a[n]));
        cout << minn << "\n";
    }
    return 0;
}

Short Task

题意

设f(x)f(x)为xx所有因数和,求最小的xx使得f(x)=cf(x)=c

正解

筛法

正解

用筛法求出每个数的因数,再用minnminn数组记录即可

错因

我好像没错

心得

??????

复杂度
  • 时间复杂度:O(∑i=11071i×107)O(\sum_{i=1}^{10^7} \frac{1}{i} \times 10^7)
  • 空间复杂度:O(107)O(10^7)
代码
C++
#include <bits/stdc++.h>
using namespace std;
const int M=1e7;
int sum[M+5];
int minn[M+5];
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    memset (minn , 0x3f , sizeof (minn));
    for (int i=1;i<=M;i++)
    {
        for (int j=i;j<=M;j+=i)
        {
            sum[j]+=i;
        }
    }
    for (int i=1;i<=10000000;i++)
    {
        if (sum[i]<=M&&minn[sum[i]]>i)
        {
            minn[sum[i]]=i;
        }
    }
    int Q , n;
    cin >> Q;
    while (Q--)
    {
        cin >> n;
        if (minn[n]<=1e9) cout << minn[n] << "\n";
        else cout << "-1\n";
    }
    return 0;
}
///

新课

P3366 【模板】最小生成树

kruskalkruskal或primprim板子题

代码
C++
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int x , y , z;
};
node v[200005];
int fa[5005];
int n , m , x , y , z;
int sum=0 , cnt;
int find (int x)
{
    if (fa[x]==x) return x;
    return fa[x]=find (fa[x]);
}
bool cmp (node x , node y)
{
    return x.z<y.z;
}
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    cnt=n;
    for (int i=1;i<=n;i++)
    {
        fa[i]=i;
    }
    for (int i=1;i<=m;i++)
    {
        cin >> v[i].x >> v[i].y >> v[i].z;
    }
    sort (v+1 , v+m+1 , cmp);
    for (int i=1;i<=m;i++)
    {
        int nx=find (v[i].x) , ny=find (v[i].y);
        if (nx==ny) continue;
        fa[ny]=nx;
        cnt--;
        sum+=v[i].z;
        if (cnt==1)
        {
            cout << sum;
            return 0;
        }
    }
    cout << "orz";
    return 0;
}

P2330 [SCOI2005] 繁忙的都市

kruskalkruskal板子题,再加上maxnmaxn记录最大值即可

代码
C++
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int x , y , z;
};
node v[8008];
int fa[305];
int n , m , x , y , z;
int sum=0 , cnt , p=0;
int find (int x)
{
    if (fa[x]==x) return x;
    return fa[x]=find (fa[x]);
}
bool cmp (node x , node y)
{
    return x.z<y.z;
}
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    cnt=n;
    for (int i=1;i<=n;i++)
    {
        fa[i]=i;
    }
    for (int i=1;i<=m;i++)
    {
        cin >> v[i].x >> v[i].y >> v[i].z;
        p+=v[i].z;
    }
    sort (v+1 , v+m+1 , cmp);
    for (int i=1;i<=m;i++)
    {
        int nx=find (v[i].x) , ny=find (v[i].y);
        if (nx==ny) continue;
        fa[ny]=nx;
        cnt--;
        sum++;
        if (cnt==1)
        {
            cout << sum << " " << v[i].z;
            return 0;
        }
    }
    return 0;
}

/**
* @runId: 67228
* @language: C++
* @author: 伍位泽
* @submitTime: 2026-05-31 14:43:19
*/

P4047 [JSOI2010] 部落划分

因为题目要让剩下的联通块距离尽量大,用kruskalkruskal跑一边,再找两个有边且不在同一联通快的点,输出最小值

代码
C++
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int x , y , z;
};
node v[1000005];
int fa[1005] , x[1005] , y[1005];
int n , k , tot;
int find (int x)
{
    if (fa[x]==x) return x;
    return fa[x]=find (fa[x]);
}
bool cmp (node x , node y)
{
    return x.z<y.z;
}
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for (int i=1;i<=n;i++)
    {
        cin >> x[i] >> y[i];
        fa[i]=i;
        for (int j=1;j<=i-1;j++)
        {
            int dx=abs (x[i]-x[j]) , dy=abs(y[i]-y[j]);
            v[++tot]={i , j , dx*dx+dy*dy};
        }
    }
    sort (v+1 , v+tot+1 , cmp);
    int maxn=0 , cnt=n;
    for (int i=1;i<=tot;i++)
    {
        int nx=find (v[i].x) , ny=find (v[i].y);
        if (nx==ny) continue;
        fa[ny]=nx;
        cnt--;
        if (cnt==k) break;
    }
    for (int i=1;i<=tot;i++)
    {
        int dx=find (v[i].x) , dy=find (v[i].y);
        if (dx==dy) continue;
        maxn=v[i].z;
        break;
    }
    printf ("%.2lf" , sqrt (maxn*1.0));
    return 0;
}

P1546 [USACO3.1] 最短网络 Agri-Net

还是一样的,只不过每两个点都连边了。套kruskalkruskal或primprim板子就行

代码
C++
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int x , y , z;
};
node v[10005];
int fa[2005];
int n , m , x , y , z , tot;
int find (int x)
{
    if (fa[x]==x) return x;
    return fa[x]=find (fa[x]);
}
bool cmp (node x , node y)
{
    return x.z<y.z;
}
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i=1;i<=n;i++) fa[i]=i;
    while (m--)
    {
        cin >> x >> y >> z;
        v[++tot]={x , y , z};
    }
    sort (v+1 , v+tot+1 , cmp);
    long long maxn=0;
    for (int i=1;i<=tot;i++)
    {
        int dx=find (v[i].x) , dy=find (v[i].y);
        if (dx==dy) continue;
        maxn=v[i].z;
        fa[dy]=dx;
    }
    cout << maxn;
    return 0;
}

习题课

Make it Increasing

题意

开始b[i]=0b[i]=0,每次可以b[i]±a[i]b[i] \pm a[i],求最少要多少次才能使b[1]<b[2]<⋯<b[n−1]<b[n]b[1]<b[2]< \cdots <b[n-1]<b[n]

知识点

枚举

正解

枚举最后哪个点还是00,再求最少次数取min⁡\min

错因

我好像没错

心得

??????

复杂度
  • 时间复杂度:O(N2)O(N^2)
  • 空间复杂度:O(N)O(N)
代码
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[5005];
int n , ans=LONG_LONG_MAX;
int CNT (int x , int y)
{
    return x/y+1;
}
int cishu (int now)
{
    int cnt=0 , lst;
    lst=0;
    for (int i=now+1;i<=n;i++)
    {
        int res=CNT (lst , a[i]);
        cnt+=res;
        lst=res*a[i];
    }
    lst=0;
    for (int i=now-1;i>=1;i--)
    {
        int res=CNT (lst , a[i]);
        cnt+=res;
        lst=res*a[i];
    }
    return cnt;
}
signed main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i=1;i<=n;i++)
    {
        cin >> a[i];
    }
    for (int i=1;i<=n;i++)
    {
        ans=min (ans , cishu (i));
    }
    cout << ans << "\n";
    return 0;
}
//

The Enchanted Forest

题意

每次格子上都会+1+1,求最多能拿到几个

知识点

分类讨论

正解

简单分析即可

错因

我好像没错

心得

??????

复杂度
  • 时间复杂度:O(N)O(N)
  • 空间复杂度:O(N)O(N)
代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int s[200005];
int T , n , k , v , ans=0;
signed main ( )
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    cin >> T;
	while (T--)
    {
		cin >> n >> k;
		for (int i=1;i<=n;i++)
        {
            cin >> v;
			s[i]=s[i-1]+v;
		}
		if (k>n)
        {
            cout << ((k-1+k-n)*n/2)+s[n] << '\n';
        }
        else
        {
    	    ans=0;
    		for (int i=k;i<=n;i++)
            {
    		    ans=max (ans , (s[i]-s[i-k])+((k-1)*k/2));
            }
    		cout << ans << "\n";
        }
	} 
	return 0;
}
//

Make Them Equal

题意

a[i]=1a[i]=1,每次可以将其中一个a[i]+=fraca[i]x(x为正整数)a[i]+=frac{a[i]}{x}(x为正整数),每当一个a[i]=b[i]会获得c[i]价值a[i]=b[i]会获得c[i]价值,在操作≤k\le k次的情况下,求最大价值

知识点

预处理,0101背包dpdp

正解

先处理出11至少要几次才能到b[i]b[i],再套用0101背包dpdp模版

注意

有可能kk较大,那么就可以直接输出价值总和,不用跑dpdp,否则会106×103=10910^6 \times 10^3=10^9严重超时

错因

看错题了,以为xx是输入的一个变量

心得

不是特别难

复杂度
  • 时间复杂度:O(min⁡(nk,3×107))O(\min (nk , 3 \times 10^7))
  • 空间复杂度:O(N)O(N)
代码
C++
#include <bits/stdc++.h>
using namespace std;
int f[100005];
int dp[1000005];
int b[1005] , c[1005];
int Q , n , k;
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    memset (f , 0x3f , sizeof (f));
    f[1]=0;
    for (int i=1;i<=1000;i++)
    {
        for (int j=1;j<=i;j++)
        {
            int pos=i+i/j;
            f[pos]=min (f[pos] , f[i]+1);
        }
    }
    cin >> Q;
    while (Q--)
    {
        memset (dp , 0 , sizeof (dp));
        cin >> n >> k;
        int cnt=0 , sum=0;
        for (int i=1;i<=n;i++)
        {
            cin >> b[i];
            cnt+=f[b[i]];
        }
        for (int i=1;i<=n;i++)
        {
            cin >> c[i];
            sum+=c[i];
        }
        if (cnt<=k)
        {
            cout << sum << "\n";
        }
        else
        {
            for (int i=1;i<=n;i++)
            {
                for (int j=k;j>=f[b[i]];j--)
                {
                    dp[j]=max (dp[j] , dp[j-f[b[i]]]+c[i]);
                }
            }
            cout << dp[k] << "\n";
        }
    }
    return 0;
}

Berpizza

题意

33中输入:

1.1m1 m,新来一个人,数值为mm 2.22给未买单且最早来的顾客买单 3.33给未买单且数值最大顾客买单

依次输出买单的人的编号(第几个来的)

知识点

priorityqueuepriority_queue,模拟

正解

用堆和标记数组模拟过程即可

错因

我好像没错

心得

??????

复杂度
  • 时间复杂度:O(Nlog⁡2N)O(N \log_2 N)
  • 空间复杂度:O(N)O(N)
代码
C++
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int id , w;
    bool operator < (const node &a) const
    {
        if (w!=a.w) return w<a.w;
        return id>a.id;
    }
};
priority_queue <node> q;
bool vis[500005];
int Q , op , m , tot , lst;
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> Q;
    for (int i=1;i<=Q;i++)
    {
        cin >> op;
        if (op==1)
        {
            cin >> m;
            tot++;
            q.push ({tot , m});
        }
        else if (op==2)
        {
            for (int j=lst+1;j<=tot;j++)
            {
                if (vis[j]==0)
                {
                    cout << j << " ";
                    vis[j]=1;
                    lst=j;
                    break;
                }
            }
        }
        else
        {
            while (!(q.empty ( )))
            {
                node p=q.top ( );
                if (vis[p.id]==0)
                {
                    cout << p.id << " ";
                    vis[p.id]=1;
                    break;
                }
                q.pop ( );
            }
        }
    }
    return 0;
}
//

习题课(兴趣小组)

Another Problem About Dividing Numbers

题意

求a,ba,b是否能经过kk次相同

知识点

质因数分解

正解

求出最多最少要几次即可

错因

我好像没错

心得

??????

复杂度
  • 时间复杂度:O(ab)O(\sqrt{ab})
  • 空间复杂度:O(1)O(1)
代码
C++
#include <bits/stdc++.h>
using namespace std;
int Q , a , b , k , x , y;
int minn (int a , int b)
{
    int c=__gcd (a , b) , res=0;
    if (a>c) res++;
    if (b>c) res++;
    return res;
}
int zys (int s)
{
    int cnt=0;
    for (int i=2;i*i<=s;i++)
    {
        while (s%i==0)
        {
            cnt++;
            s/=i;
        }
    }
    if (s>1) cnt++;
    return cnt;
}
int maxn (int a , int b)
{
    return zys (a)+zys (b);
}
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> Q;
    while (Q--)
    {
        cin >> a >> b >> k;
        x=minn (a , b);
        y=maxn (a , b);
        if (a==b&&k==1)
        {
            cout << "No\n";
        }
        else if (k>=x&&k<=y)
        {
            cout << "Yes\n";
        }
        else
        {
            cout << "No\n";
        }
    }
    return 0;
}
//
原文