• 8/19 cf思维+马拉车算法


    A. Burenka Plays with Fractions

    看出了结果输出肯定为0、1、2,但是a* c==b* d,这个显而易见的公式迟迟没想到。

    #include
    #define endl '\n'
    #define re register
    #define int long long
    #define ios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
    #define maxn 1000000000LL
    #define ULL unsigned long long
    using namespace std;
    const int N=7e6+10;
    const int inf=0x3f3f3f3f;
    const int mod=1e7+7;
    int a,b,c,d;
    
    void solve()
    {
        cin>>a>>b>>c>>d;
        if(a*d==b*c)
            cout<<0<<endl;
        else if(a==0||c==0||(a*d)%(b*c)==0||(b*c)%(a*d)==0)
            cout<<1<<endl;
        else
            cout<<2<<endl;
    }
    signed main()
    {
        int t;cin>>t;
        while(t--)
            solve();
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    B. Interesting Sum

    差点真去找区间了。不管怎么确定区间,都是前两个大值和前两个小值差值的累加。

    #include
    #define endl '\n'
    #define re register
    #define int long long
    #define ios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
    #define maxn 1000000000LL
    #define ULL unsigned long long
    using namespace std;
    const int N=7e6+10;
    const int inf=0x3f3f3f3f;
    const int mod=1e7+7;
    int n,a[N];
    
    void solve()
    {
        cin>>n;
        int mx=0,mi=inf,g1=0,g2=0;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        sort(a+1,a+n+1);
        cout<<a[n]+a[n-1]-a[1]-a[2]<<endl;
    
    }
    signed main()
    {
        int t;cin>>t;
        while(t--)
            solve();
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    C. Corners

    有时代码问题,思路早就对了,关键是wa了还会去怀疑是不是思路的问题。
    思路:
    如果全是1没有0,则需要浪费两个1;若是只存在被0包裹的1,不存在两个0联通的1,则需要浪费一个1;若是存在两个0联通,则不会浪费,即为1的个数。

    #include
    #define endl '\n'
    #define re register
    #define int long long
    #define ios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
    #define maxn 1000000000LL
    #define ULL unsigned long long
    using namespace std;
    const int N=7e2+10;
    const int inf=0x3f3f3f3f;
    const int mod=1e7+7;
    int n,m;
    char c[N][N];
    void solve()
    {
        cin>>n>>m;
        for(int i=0;i<=n+5;i++)
            for(int j=0;j<=m+5;j++)
            c[i][j]='-1';
        int x=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                cin>>c[i][j];
                if(c[i][j]=='1')
                    x+=1;
            }
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(c[i][j]=='0')
                {
                    flag=1;
                    //cout<
                    if(c[i+1][j]=='0'||c[i][j+1]=='0'||c[i+1][j+1]=='0'||c[i-1][j+1]=='0'||c[i-1][j+1]=='0')
                    {
                        flag=2;break;
                    }
                }
            }
            if(flag==2)
                break;
        }
        if(flag==2)
            cout<<x<<endl;
        else if(flag==1)
            cout<<x-1<<endl;
        else
            cout<<x-2<<endl;
    }
    signed main()
    {
        int t;cin>>t;
        while(t--)
            solve();
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    D1. Xor-Subsequence (easy version)

    通过异或操作,i和j可造成的最大差距为255.
    f[i]:表示以i结尾的最长子序列长度。
    状态转移:f[j]与f[i]+1

    #include
    #define int long long
    #define endl '\n'
    #define For(i,a,b) for(i=(a);i<=(b);++i)
    #define ios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
    using namespace std;
    const int N=7e5+6;;
    const int inf=0x3f3f3f3f;
    int f[N],n,a[N];
    void solve()
    {
        cin>>n;
        for(int i=0;i<n;i++)
            f[i]=1,cin>>a[i];
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<min(n,i+256);j++)
            {
                if((a[i]^j)<(a[j]^i))
                    f[j]=max(f[j],f[i]+1);
            }
        }
        int ans=0;
        for(int i=0;i<n;i++)
            ans=max(ans,f[i]);
        cout<<ans<<endl;
    }
    signed main()
    {
        //ios;
        int t;cin>>t;
        while(t--)
            solve();
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    马拉车算法

    https://www.bilibili.com/video/BV173411V7Ai?spm_id_from=333.999.0.0&vd_source=91973ada1213cf6ba2cbb43a2bebd2e8
    其实早就学过了,但每次都只是理解了个板子,没办法灵活应用,做了几次牛客,发现还是挺长考的,需要精进的学一下。
    一个更简化好理解的板子,按照区间枚举:

    // 构造字符串 @#a#b#c……#
    void get_d(char *s,int n)
    {
        d[1]=1;
        for(int i=2,l,r=1;i<=n;i++)
        {
            if(i<=r) d[i]=min(d[r-i+1],r-i+1);  //盒内加速
            while(s[i-d[i]]==s[i+d[i]]) d[i]++;
            if(i+d[i]-1>r)
                l=i-d[i]+1,r=i+d[i]-1;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    (原创)Lottie动画使用介绍
    Github 仓库代码合并 + 历史记录保留
    Leetcode 1758. 生成交替二进制字符串的最少操作数
    记忆科技携手中国电信,一站式存储打造坚实数字底座
    IMX6ULL移植篇-uboot源码主要目录说明
    java计算机毕业设计华夏球迷俱乐部网站设计与实现MyBatis+系统+LW文档+源码+调试部署
    html:lang属性设置为中文zh-CN
    word文档怎么显示修改痕迹?
    状态压缩dp整理
    nodejs+vue宁夏旅游景点客流量数据分析
  • 原文地址:https://blog.csdn.net/weixin_51934288/article/details/126416877