跳到正文

个人记录

雅礼兴趣小组总结-4

wuweize150815约 14 分钟
原文

这是 2026年10月15日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

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

版本

  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

题意