洛谷 NOIP 模拟 DAY1

本文针对两道关于兔子的问题,提供了解决方案。第一题通过预处理斐波那契数列来确定兔子之间的父子关系;第二题则利用vector记录每种颜色兔子的位置,以高效处理颜色交换操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

T1

题目链接
每次新产生的兔子一定是前面的兔子生的。
先预处理出菲波那切数列,然后用a-f[x]就是a的父亲。(f[x]是小于a的最大的)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int N=30077;
const LL M=1e12+5;
int n,m;
LL f[5*N];
LL find(LL a,LL b)
{
    while(a!=b)
    {
        int t1=lower_bound(f+1,f+n+1,a)-f-1;
        int t2=lower_bound(f+1,f+n+1,b)-f-1;
        if(t1==t2)
        {
            a=a-f[t1];
            b=b-f[t2];
        } 
        else
        if(t1>t2)
            a=a-f[t1];
        else 
            b=b-f[t2];
    }
    return a;
}
int main()
{
    scanf("%d",&m);
    f[1]=1;n=2;
    while(f[n-1]<=M)
    {
        f[n]=f[n-1]+f[n-2];
        n++;
    }
    n--;
    //printf("%lld",f[n-1]-M);
    for(int i=1;i<=m;i++)
    {
        LL a,b,ans;
        scanf("%lld%lld",&a,&b);
        ans=find(a,b);
        printf("%lld\n",ans);
    }
    return 0;
} 

T2

题目链接
把每种颜色的兔子的坐标用vector(升序)记下来。
因为修改是x与x+1的交换,所以交换后,在vector数组中的相对位置不会变(降低了代码难度)。
在vector中最好不要自己写二分,容易RE,而且不要随便调用下标。
再者此题注意颜色集合为空的情况。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define LL long long
using namespace std;
const int N=3e5+77;
vector <int> color[N];
int n,m,a[N];
int main()
{
    //freopen("a.in","r",stdin);
    scanf("%d%d",&n,&m);
    for(int x,i=1;i<=n;i++) 
    {
        scanf("%d",&x);
        a[i]=x;
        color[x].push_back(i);
    }
    for(int i=1;i<=m;i++)
    {
        int opt,l,r,c;
        scanf("%d",&opt);
        if(opt==1)
        {
            scanf("%d%d%d",&l,&r,&c);
            int p=lower_bound(color[c].begin(),color[c].end(),l)-color[c].begin();
            int q=upper_bound(color[c].begin(),color[c].end(),r)-color[c].begin();
            printf("%d\n",q-p);
        }
        if(opt==2)
        {
            int x;
            scanf("%d",&x);
            int c1=a[x],c2=a[x+1];
            if(c1==c2) continue;
            int p=lower_bound(color[c1].begin(),color[c1].end(),x)-color[c1].begin();
            int q=lower_bound(color[c2].begin(),color[c2].end(),x+1)-color[c2].begin();
            color[c1][p]=x+1;
            color[c2][q]=x;
            swap(a[x],a[x+1]);
        } 
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值