跳到正文

题解

题解:P8048 [COCI 2015/2016 #4] ENDOR

ingo_dtw约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年11月20日
篇幅
约 4 分钟
题目
P8048

版本

  1. v2当前
  2. v1

P8048 [COCI 2015/2016 #4] ENDOR

模拟赛写正解发现思路错了,所以来补篇题解。

首先我们考虑 Θ(n2)\Theta(n^2) 的暴力:

  • 我们可以不处理反弹,而是把两个碰撞的变色龙看作互相穿过。
  • 于是在两个变色龙相遇相遇后:
    • 向右的看作不变。
    • 而向左的变色龙颜色变为 (a+b) mod k(a+b) \bmod k。
写完暴力我们发现瓶颈在于:

对于每个向左走的变色龙都需要把所有在它前面的向右走的变色龙给扫一下来计算答案。

我们考虑优化,但是我们发现计算答案的 nn 是必须的,但是 1≤k≤401 \le k \le 40 这个 kk 的范围很小,所以我们不妨大胆猜测一下这个题的时间复杂度为 Θ(n×k)\Theta(n \times k) 所以我们以 kk 来看看怎么实现:

  • 我们发现每一个向左的变色龙总是从右到左依次和在它左边的变色龙碰撞。
  • 在经过同一个向右的变色龙时,相同颜色的变色龙行为相同所以我们尝试统一处理。

Ac Code:

C++
#include <bits/stdc++.h>
using namespace std;
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc _getchar_nolock
#define pc _putchar_nolock
#endif
#define rint register int
#define int long long
#define _ read<int>()
#define R register
inline bool blank(const char &x)
{
    return !(x^10)||!(x^9)||!(x^32)||!(x^13);
}
template<class T>inline T read()
{
    R T r=0,f=1;R char c=gc();
    while(!isdigit(c))
    {
        if(c=='-') f=-1;
        c=gc();
    }
    while(isdigit(c)) r=(r<<1)+(r<<3)+(c^48),c=gc();
    return f*r;
}
inline void out(rint x)
{
    if(x<0) pc('-'),x=-x;
    if(x<10) pc(x+'0');
    else out(x/10),pc(x%10+'0');
}
inline void read(R char &x)
{
    for(x=gc();blank(x)&&(x^-1);x=gc());
}
const int N=1e5+10;
struct oi
{
    int d,cl;
}a[N],b[N];
int cntl,cntr,r[N],lastc,lastd,sd;
double ans[50],cnt[50];
signed main()
{
    rint n=_,k=_,L=_;
    for(rint i=1;i<=n;i++)
    {
        rint d=_,cl=_;
        R char c;
        read(c);
        if(c=='D')
        {
            if(sd) cnt[lastc]+=(d-lastd)*0.5;
            else sd=d;
            ans[cl]+=L-d;
            lastc=(lastc+cl)%k;
            lastd=d;
        }
        else
        {
            ans[cl]+=d-(d+lastd)*0.5;
            ans[(cl+lastc)%k]+=(d+sd)*0.5;
            for(rint j=0;j<=k;j++) ans[j]+=cnt[(cl+lastc-j+k)%k];
        }
    }
    for(rint i=0;i<k;i++)
    {
        printf("%.1lf",ans[i]);
        pc('\n');
    }
    return 0;
}
原文