codeforces 232D Fence

敲了1个小时左右,sb错误调了2个小时。。。真是醉了,各种报警

题解的话这儿的写的不错,但是我是看了个大概思路然后自己想的,这种东西还是自己想想比较好呢

点击打开链接

注释上各种错误。。。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;

#define N 200010
#define INF 0x7fffffff

int Last_n,tot,q,t1,t2;
int Ws[N],Wv[N],sa[N],Rank[N],f[N][30];
int Use_a[N],Use_b[N],r[N],height[N],go[N];
char S[N];
struct Node{
    int value,id;
    bool operator < (const Node &a) const{
        return value<a.value;
    }
}A[N*2];
struct Bjzy{
    int L,R;
    int sum;
}t[N*25];    //记得不是*4。。。
int root[N];

inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void Updata(int &i,int l,int r,int pos)
{
    t[++tot]=t[i];i=tot;
    t[i].sum++;
    if(l==r)
        return;
    int mid=(l+r)/2;
    if(pos<=mid)
        Updata(t[i].L,l,mid,pos);
    else Updata(t[i].R,mid+1,r,pos);
}

int Query(int i,int j,int l,int r,int left,int right)
{
    if(l>=left&&r<=right)
    {
        return t[i].sum-t[j].sum;
    }
    int mid=(l+r)/2;
    if(right<=mid)
        return Query(t[i].L,t[j].L,l,mid,left,right);
    else if(left>mid)
        return Query(t[i].R,t[j].R,mid+1,r,left,right);
    else
    {
        return Query(t[i].L,t[j].L,l,mid,left,mid)+Query(t[i].R,t[j].R,mid+1,r,mid+1,right);
    }      //j打成了i真是日了狗
}

bool Cmp(int *r,int a,int b,int len)
{
    return r[a]==r[b]&&r[a+len]==r[b+len];
}

void Suf_work(int n,int m)
{
    int *x=Use_a,*y=Use_b,*t;
    for(int i=0;i<m;i++)Ws[i]=0;
    for(int i=0;i<n;i++)Ws[x[i]=r[i]]++;
    for(int i=1;i<m;i++)Ws[i]+=Ws[i-1];
    for(int i=n-1;i>=0;i--)sa[--Ws[x[i]]]=i;
    for(int j=1,p=1;p<n;j<<=1,m=p)
    {
        p=0;
        for(int i=n-j;i<n;i++) y[p++]=i;
        for(int i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
        for(int i=0;i<n;i++) Wv[i]=x[y[i]];
        for(int i=0;i<m;i++) Ws[i]=0;
        for(int i=0;i<n;i++) Ws[Wv[i]]++;
        for(int i=1;i<m;i++) Ws[i]+=Ws[i-1];
        for(int i=n-1;i>=0;i--) sa[--Ws[Wv[i]]]=y[i];
        t=x;x=y;y=t;p=1;x[sa[0]]=0;
        for(int i=1;i<n;i++)
            x[sa[i]]=Cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
}

void Calheight(int n)
{
    int k=0,j;
    for(int i=1;i<=n;i++)Rank[sa[i]]=i;
    for(int i=0;i<n;height[Rank[i++]]=k)
        for(k?k--:0,j=sa[Rank[i]-1];r[i+k]==r[j+k];k++);
}

void RMQ()
{
    for(int i=1;i<=Last_n;i++)
        f[i][0]=height[i];
    for(int j=1;j<=25;j++)
        for(int i=1;i<=Last_n;i++)
        {
            if(i+(1<<j)-1<=Last_n)
                f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
        }
}

int Get_min(int a,int b)
{
    int x1=Rank[a],x2=Rank[b];
    int l=min(x1,x2),r=max(x1,x2);
    l++;
    int qwer=(int)(log(r-l+1.0)/log(2.0));
    return min(f[l][qwer],f[r-(1<<qwer)+1][qwer]);
}

void Find(int k,int len)
{
    int l,r;
    if(k==1||Get_min(sa[k-1],sa[k])<len)
        t1=k;
    else
    {
        l=1;r=k-1;
        while(l<r)
        {
            int mid=(l+r)/2;
            if(Get_min(sa[mid],sa[k])>=len)
                r=mid;
            else l=mid+1;
        }
        t1=r;
    }
    if(k==Last_n||Get_min(sa[k],sa[k+1])<len)   //特判应该放在或之前
        t2=k;
    else
    {
        l=k+1;r=Last_n+1;    //二分边界错误。。右边界设为Last_n
        while(l<r)
        {
            int mid=(l+r)/2;
            if(Get_min(sa[k],sa[mid])>=len) //把mid打成了k+1
            {
                if(l==mid)
                    break;
                l=mid;
            }
            else r=mid;
        }
        t2=l;
    }
}

int main()
{
    Last_n=read();
    int last=read();Last_n--;
    for(int i=1;i<=Last_n;i++)
    {
        int x=read();
        r[i-1]=x-last;last=x;
        A[++tot].value=r[i-1];A[tot].id=tot;
        A[++tot].value=-r[i-1];A[tot].id=tot;
    }
    sort(A+1,A+tot+1);
    A[0].value=-INF;
    int qwer=0;
    for(int i=1;i<=tot;i++)
    {
        if(A[i].value!=A[i-1].value)
        {
            go[A[i].id]=++qwer;
        }
        else go[A[i].id]=qwer;
    }
    int qqq=Last_n;
    for(int i=1;i<=qqq;i++)
        r[i-1]=go[2*(i-1)+1];
    r[Last_n++]=0;
    for(int i=1;i<=qqq;i++)
        r[Last_n++]=go[2*i];
    Last_n--;
    Suf_work(Last_n+1,qwer+10);
    Calheight(Last_n);
    RMQ();tot=0;
    for(int i=qqq+1;i<=Last_n;i++)
    {
        int now=i-qqq;
        root[now]=root[now-1];
        Updata(root[now],1,Last_n,Rank[i]);
    }
    q=read();
    for(int i=1;i<=q;i++)
    {
        int l=read(),r=read();
        if(l==r)
            printf("%d\n",qqq);
        else
        {
            int Ans=0;int len=r-l;
            Find(Rank[l-1],r-l);
            if(l-len-1>=1)   //len当做1
                Ans+=Query(root[l-len-1],root[0],1,Last_n,t1,t2);
            if(r+1<=qqq)
                Ans+=Query(root[qqq],root[r],1,Last_n,t1,t2);
            printf("%d\n",Ans);
        }
    }
    return 0;
}



### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值