跳到正文

个人记录

雅礼兴趣小组总结1

wuweize150815约 15 分钟
原文

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

文章信息与版本

文章信息

发布
2026年9月15日
收录
2026年9月15日
更新
2026年9月20日
篇幅
约 15 分钟

版本

  1. v2当前
  2. v1

雅礼兴趣小组总结1

习题课

Dominant Character

题意

选出一个字串,子串中aa的数量要bb的数量和cc的数量要多,而且长度至少为22。求这个字串的最小长度(无解输出−1-1)

知识点

stringstring,思想

正解

只可能是aa,aba,aca,abca,acba,abbacca,accabbaaa,aba,aca,abca,acba,abbacca,accabba中的一种,长度分别为2,3,3,4,4,7,72,3,3,4,4,7,7,按长度从小往大去findfind看一下是否存在,否则输出−1-1

错因

想到了正解,但是漏了abca,acbaabca,acba两种情况,导致一些测试点要输出44但输出了−1-1

心得

要根据输出发现问题

复杂度
  • 时间复杂度:O(QN)O(QN)
  • 空间复杂度:O(N)O(N)
代码
C++
#include <bits/stdc++.h>
using namespace std;
string s;
int Q , n;
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> Q;
    while (Q--)
    {
        cin >> n >> s;
        if (s.find ("aa")!=-1) cout << "2\n";
        else if (s.find ("aba")!=-1||s.find ("aca")!=-1) cout << "3\n";
        else if (s.find ("abca")!=-1||s.find ("acba")!=-1) cout << "4\n";
        else if (s.find ("abbacca")!=-1||s.find ("accabba")!=-1) cout << "7\n";
        else cout << "-1\n";
    }
    return 0;
}

Codeforces Subsequences

题意

构造一个最短的子序列,且至少有kk个codefocescodefoces,求最短的子序列

知识点

二分答案

正解

构造题。显然将后一个字母插到前面不是最优的。所以就是求: a1×a2×a3×⋯×a8×a9×a10≥ka_1 \times a_2 \times a_3 \times \cdots \times a_8 \times a_9 \times a_{10} \ge k,求(a1+a2+a3+⋯+a8+a9+a10)min(a_1+a_2+a_3+ \cdots +a_8+a_9+a_{10})_{min},并分别输出

错因

我好像没错

心得

??????

复杂度
  • 时间复杂度:O(log2k)O(log_2 k)
  • 空间复杂度:O(1)O(1)
代码
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
char v[11]={' ' , 'c' , 'o' , 'd' , 'e' , 'f' , 'o' , 'r' , 'c' , 'e' , 's'};
long long k;
bool check (int mid)
{
    int res=1;
    int a=mid/10 , b=mid%10;
    if (b>=0)
    {
        for (int i=1;i<=10-b;i++)
        {
            res*=a;
            if (res>=k) return 1;
        }
        for (int i=1;i<=b;i++)
        {
            res*=(a+1);
            if (res>=k) return 1;
        }
    }
    else
    {
        for (int i=1;i<=10;i++)
        {
            res*=a;
            if (res>=k) return 1;
        }
    }
    return (res>=k);
}
signed main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> k;
    int l=10 , r=1000;
    while (l<r)
    {
        int mid=(l+r)/2;
        if (check (mid)==1)
        {
            r=mid;
        }
        else
        {
            l=mid+1;
        }
    }
    int a=l/10 , b=l%10;
    if (b>0)
    {
        for (int i=1;i<=10-b;i++)
        {
            for (int j=1;j<=a;j++)
            {
                 cout << v[i];
            }
        }
        for (int i=10-b+1;i<=10;i++)
        {
            for (int j=1;j<=a+1;j++)
            {
                cout << v[i];
            }
        }
    }
    else
    {
        for (int i=1;i<=10;i++)
        {
            for (int j=1;j<=a;j++)
            {
                cout << v[i];
            }
        }
    }
    return 0;
}

AGAGA XOOORRR

题意

是否能将数组分成若干个(≥2\ge 2)部分,使各个部分的异或值相等

知识点

位运算

正解

第一段一定会从1∼k(k≥1)1 \sim k(k \ge 1),所以直接枚举,只要最后分的完a1⊕a2⊕a3⊕⋯⊕ak−2⊕ak−1⊕aka_1 \oplus a_2 \oplus a_3 \oplus \cdots \oplus a_{k-2} \oplus a_{k-1} \oplus a_{k},就输出yesyes,否则输出nono

错因

我好像没错

心得

在数据很小的情况下,不用想太多

复杂度
  • 时间复杂度:O(N2)O(N^2)
  • 空间复杂度:O(N)O(N)
代码
C++
#include <bits/stdc++.h>
using namespace std;
int a[2005] , b[2005];
int Q , n , now=0;
bool sf (int k)
{
    int p=0 , cnt=0;
    for (int i=1;i<=n;i++)
    {
        p^=a[i];
        if (p==k) 
        {
            p=0;
            cnt++;
        }
    }
    if (p==0&&cnt>1) return 1;
    return 0;
}
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> Q;
    while (Q--)
    {
        now=0;
        cin >> n;
        for (int i=1;i<=n;i++)
        {
            cin >> a[i];
        }
        bool p=0;
        int k=0;
        for (int i=1;i<=n;i++)
        {
            k^=a[i];
            if (sf (k)==1)
            {
                p=1;
                break;
            }
        }
        if (p==0) cout << "NO\n";
        else cout << "YES\n";
    }
    return 0;
}

Factorial Divisibility

题意

求a1!+a2!+a3!+⋅+an−2!+an−1!+an!(ai≤x)a_1!+a_2!+a_3!+ \cdot +a_{n-2}!+a_{n-1}!+a_n!(a_i \le x)是否整除xx

知识点

!!的运算

正解

i!∗(i+1)i!*(i+1)=(i+1)!(i+1)!,而且kk不大,直接用计数数组来记录,类似进位,如果余下≠0 \ne 0就输出NoNo,否则为YesYes

错因

我好像没错

心得

??????

复杂度
  • 时间复杂度:O(X)O(X)
  • 空间复杂度:O(X)O(X)
代码
C++
#include <bits/stdc++.h>
using namespace std;
int a[500005];
int n , m , k;
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i=1;i<=n;i++)
    {
        cin >> k;
        a[k]++;
    }
    for (int i=1;i<=m-1;i++)
    {
        if (a[i]%(i+1)==0)
        {
            a[i+1]+=a[i]/(i+1);
            a[i]=0;
        }
        else
        {
            cout << "No";
            return 0;
        }
    }
    cout << "Yes";
    return 0;
}

Line Empire

题意

设首都为xx,城市为yy(未被占领)

  • 只占领:∣x−y∣∗a|x-y|*a
  • 占领并迁都(中途城市全被占领):∣x−y∣∗(a+b)|x-y|*(a+b) 求将所有城市都占领耗费的最小价值
知识点

贪心,模拟

正解

一定是从左到右一路占领的。每次比较,选两者中价值决定是否迁都,最后统计

错因

我看成Bit Flipping这道题了,呜呜呜呜呜呜呜

心得

要看清题目,呜呜呜呜呜呜呜

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

Increase Subarray Sums

题意

从i:(1∼n)i:(1 \sim n),每次给任意ii个数+x+x,求加完后的最大字段和(可为空段)

知识点

最大字段和

正解

先预处理出best[i]:best[i]:长度为ii的最大字段和,再在加的时候枚举。设要加ii个数,当前长度为为jj,答案为ansans:

ans=max⁡(ans,best[j]+min⁡(i,j)∗x)ans=\max (ans , best[j]+\min (i , j)*x);

(就是将加上的和原本的加起来)

错因

我看成Anti-Fibonacci Permutation这道题了,呜呜呜呜呜呜呜

心得

要看清题目,呜呜呜呜呜呜呜

复杂度
  • 时间复杂度:O(QN2)O(QN^2)
  • 空间复杂度:O(N)O(N)
代码
C++
#include <bits/stdc++.h>
using namespace std;
int best[5005];
int a[5005];
int Q , n , x;
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> Q;
    while (Q--)
    {
        memset (best , -0x3f , sizeof (best));
        cin >> n >> x;
        for (int i=1;i<=n;i++)
        {
            cin >> a[i];
            a[i]+=a[i-1];
        }
        for (int i=1;i<=n;i++)
        {
            for (int j=i;j<=n;j++)
            {
                best[i]=max (best[i] , a[j]-a[j-i]);
            }
        }
        for (int i=0;i<=n;i++)
        {
            int maxn=0;
            for (int j=0;j<=n;j++)
            {
                maxn=max (maxn , best[j]+min (i , j)*x);
            }
            cout << maxn << " ";
        }
        cout << "\n";
    }
    return 0;
}