码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Codeforces Round #790 (Div. 4) H. Maximum Crossings


    H1. Maximum Crossings (Easy Version)

    H2. Maximum Crossings (Hard Version)

    Let’s look at two wires from i i i → a i a_i ai​ and j j j → a j a_j aj​.

    • If a i a_i ai​ < a j a_j aj​, there can never be any intersection.
    • If a i a_i ai​ > a j a_j aj​, there has to be an intersection.
    • If a i a_i ai​ = a j a_j aj​, it is possible that there is an intersection or not, depending on how we arrange the wires on the bottom terminal.

    In the last case, if there are multiple wires that go to the same segment a i a_i ai​, we can make all pairs of them cross by arranging the points in which they hit this segment from right to left. For example, if a = [ 1 , 1 , 1 , 1 ] a=[1,1,1,1] a=[1,1,1,1], then we can make all pairs of segments cross as shown.

    在这里插入图片描述
    Since we want to maximize the number of intersections, we just need to count the number of pairs ( i , j ) (i,j) (i,j) such that a i a_i ai​ ≥ ≥ ≥ a j a_j aj​. You can brute force all pairs in O ( n 2 ) O(n^2) O(n2).

    #include
    using namespace std;
    
    int main()
    {
        int T;cin>>T;
        while(T--)
        {
            int n;cin>>n;
            vector<int> a(n);
            for(int i=0;i<n;i++) cin>>a[i];
            int res=0;
            for(int i=0;i<n;i++)
                for(int j=i+1;j<n;j++)
                    if(a[i]>=a[j])
                        res++;
            cout<<res<<endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    Read the solution of the easy version.
    We want to count the number of pairs ( i , j ) (i,j) (i,j) such that i < j ii<j and a i a_i ai​ ≥ ≥ ≥ a j a_j aj​. This is a standard problem, and we can do this we can use a segment tree or BIT, for example. Insert the a j a_j aj​ from j j j = = = 1 1 1 to n, and then for each a j a_j aj​ count the number of a i a_i ai​ ≤ ≤ ≤ a j a_j aj​ using a BIT.

    It is also related to the problem of counting inversions, so you can solve it using a modification of merge sort.

    Either way, the solution is O ( n l o g n ) O(nlogn) O(nlogn).

    #include
    using namespace std;
    
    typedef long long LL;
    const int N = 200010;
    
    int tr[N],a[N],n;
    
    void add(int x,int y)
    {
        for(;x<=n;x+=(x&-x))
            tr[x]+=y;
    }
    
    LL query(int x)
    {
        LL res=0;
        for(;x;x-=(x&-x))
            res+=tr[x];
        return res;
    }
    
    int main()
    {
        int T;cin>>T;
        while(T--)
        {
            memset(tr,0,sizeof tr);
            cin>>n;
            for(int i=0;i<n;i++) cin>>a[i];
            LL res=0;
            for(int i=0;i<n;i++)
            {
                res+=query(n)-query(a[i]-1);
                add(a[i],1);
            }
            cout<<res<<endl;
        }
        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
  • 相关阅读:
    从月薪10k到30k的必走之路:自动化测试
    数据保护双保险,一文读懂NVMe协议中的Get LBA Status功能
    centos7 配置coreboot编译环境 以及编译问题解决
    小微信贷传统风控模型的痛点
    小熊家务帮day13-day14 门户管理(ES搜索,Canal+MQ同步,索引同步)
    把握现在,热爱生活
    .NET周刊【1月第1期 2024-01-07】
    【PAT(甲级)】1046 Shortest Distance(距离分析)
    微信小程序渲染层与逻辑层交互原理
    Unity之PUN实现多人联机射击游戏的优化(Section 2)
  • 原文地址:https://blog.csdn.net/qq_52792570/article/details/127782445
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号