zoukankan      html  css  js  c++  java
  • Codeforces Round #561 (Div. 2) C. A Tale of Two Lands

    链接:https://codeforces.com/contest/1166/problem/C

    题意:

    The legend of the foundation of Vectorland talks of two integers xx and yy. Centuries ago, the array king placed two markers at points |x||x| and |y||y| on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland. Many years later, the vector king placed markers at points |xy||x−y| and |x+y||x+y| and conquered all the land in between (including the endpoints), which he declared to be Vectorland. He did so in such a way that the land of Arrayland was completely inside (including the endpoints) the land of Vectorland.

    Here |z||z| denotes the absolute value of zz.

    Now, Jose is stuck on a question of his history exam: "What are the values of xx and yy?" Jose doesn't know the answer, but he believes he has narrowed the possible answers down to nnintegers a1,a2,,ana1,a2,…,an. Now, he wants to know the number of unordered pairs formed by two different elements from these nn integers such that the legend could be true if xx and yywere equal to these two values. Note that it is possible that Jose is wrong, and that no pairs could possibly make the legend true.

    思路:

    因为需要满足条件 min(|x-y|, |x+y|) <= |x| <= |y| <= max(|x-y|, |x+y|).

    所以将x变成-x并不影响条件。所以将所有输入全部取绝对值。

    再排序,二分查找。

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    typedef long long LL;
    const int MAXN = 2e5+10;
    
    int a[MAXN];
    
    int main()
    {
        int n;
        cin >> n;
        for (int i = 1;i <= n;i++)
            cin >> a[i], a[i] = abs(a[i]);
        sort(a+1, a+1+n);
        LL res = 0;
        for (int i = 1;i <= n;i++)
            res += (upper_bound(a+1, a+1+n, 2*a[i])-a)-i-1;
        cout << res << endl;
    
        return 0;
    }
    

      

  • 相关阅读:
    osharp3引入事务后操作结果类别的调整
    Code First 迁移
    表达式拼接Expression<Func<IEntityMapper, bool>> predicate
    ASP.NET中Session的sessionState 4种mode模式
    EF Code First Migrations数据库迁移
    啊里大鱼短信发送API
    asp.net服务器控件的生命周期
    osharp3使用经验:整合DbContextScope 文章 1
    关于MarshalByRefObject的解释
    数据库操作事务IsolationLevel 枚举
  • 原文地址:https://www.cnblogs.com/YDDDD/p/10900157.html
Copyright © 2011-2022 走看看