比赛题解 【Educational Codeforces Round 9】

· · 个人记录

A. Grandma Laura and Apples

模拟即可

#include<cstdio>
#include<iostream>
typedef long long ll;
const int MAXN = 40 + 5;
using namespace std;

char s[MAXN][10];

int main(void)
{
    int n,p;
    scanf("%d%d",&n,&p);
    for(int i=1; i<=n; ++i) scanf("%s",s[i]+1);

    ll ans=0, now=0;
    for(int i=n; i>=1; --i)
    {
        if(s[i][5])
        {
            ans+=now*p + p/2;
            now=now*2+1;
        }
        else
        {
            ans+=now*p;
            now<<=1ll;
        }
    }
    cout<<ans;
    return 0;
}

B. Alice, Bob, Two Teams

分别处理A和B的前缀和,枚举翻转位置,计算答案

#include<cstdio>
#include<iostream>
typedef long long ll;
const int MAXN = 5e5 + 5;
using namespace std;

char s[MAXN];
ll p[MAXN];
ll a[MAXN],b[MAXN];

int main(void)
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; ++i) cin>>p[i];
    scanf("%s",s+1);

    for(int i=1; i<=n; ++i)
    {
        a[i]=a[i-1];
        b[i]=b[i-1];
        if(s[i]=='A') a[i]+=p[i];
        else b[i]+=p[i];
    }

    ll ans=max(a[n],b[n]);
    for(int i=1; i<=n; ++i)
    {
        ans = max(ans, a[n]-a[i] + b[i]);
        ans = max(ans, b[n]-b[i] + a[i]);
    }
    cout<<ans;
    return 0;
}

C. The Smallest String Concatenation

$|a_i|\leq 50$,所以在比较两个字符串$s_1,s_2$时,比较$s_1 + s_2$与$s_2 + s_1$的字典序即可 ```cpp #include<cstdio> #include<cstring> #include<algorithm> const int MAXN = 5e4 + 5; const int MAXL = 50 + 5; using namespace std; struct String { char s[MAXL*2]; int len; }a[MAXN]; inline bool cmp(String &x,String &y) { int lx=x.len, ly=y.len; for(int i=1; i<=ly; ++i) x.s[lx+i] = y.s[i]; for(int i=1; i<=lx; ++i) y.s[ly+i] = x.s[i]; for(int i=1; i<=lx+ly; ++i) if(x.s[i]!=y.s[i]) return x.s[i]<y.s[i]; return 0; } int main(void) { int n; scanf("%d",&n); for(int i=1; i<=n; ++i) scanf("%s",a[i].s+1), a[i].len=strlen(a[i].s+1); sort(a+1,a+n+1,cmp); for(int i=1; i<=n; ++i) a[i].s[a[i].len+1]=0, printf("%s",a[i].s+1); return 0; } ``` # D. Longest Subsequence [解题报告](https://www.luogu.org/blog/He-Ren/solution-cf632d) 首先,发现所有$a_i > m$可以直接省略,所以$a_i$的范围就缩小到了$10^6

考虑枚举lcm,然后计算序列长度。

用cnt_i表示数i出现的次数,枚举i的所有倍数,将那个倍数的答案加上cnt_i

复杂度是O(\frac{m}{1}+\frac{m}{2}+ \cdots +\frac{m}{m})=O(m\log m)

#include<cstdio>
#include<algorithm>
const int MAXN = 1e6 + 5;
const int MAXM = 1e6 + 5;
using namespace std;

int a[MAXN],b[MAXN],cnt[MAXM],t[MAXM];

int main(void)
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; ++i)
        scanf("%d",&a[i]), b[i]=a[i];

    for(int i=1; i<=n; ++i)
        if(a[i]<=m) ++cnt[a[i]];

    sort(b+1,b+n+1);
    int p = unique(b+1,b+n+1)-b-1;

    for(int i=1; i<=p && b[i]<=m; ++i)
    {
        int x=b[i];
        for(int j=x; j<=m; j+=x)
        {
            t[j]+=cnt[x];
        }
    }

    int ans=t[1], maxt=1;
    for(int i=2; i<=m; ++i)
        if(t[i]>ans)
        {
            ans=t[i];
            maxt=i;
        }

    printf("%d %d\n",maxt,ans);
    for(int i=1; i<=n; ++i)
        if(maxt%a[i]==0) printf("%d ",i);
    return 0;
}