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

· · 题解

P8048 [COCI 2015/2016 #4] ENDOR

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

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

:::info[写完暴力我们发现瓶颈在于:] 对于每个向左走的变色龙都需要把所有在它前面的向右走的变色龙给扫一下来计算答案。 :::

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

Ac Code:

#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;
}