zoukankan      html  css  js  c++  java
  • Codeforces 1324D Pair of Topics

    题目链接

    这种类型题大多都是移项, (a_i + a_j > b_i + b_j (i < j)), 移项得, (a_i - b_i > b_j - a_j (i < j)), 再取反(b_i - a_i < a_j - b_j (i < j)), 则我们可以设新数组(C_i = b_i - a_i, D_i = a_i - b_i (i < j)), 按照顺序统计(D_j > C_i (i < j))的对数即可, 可以用树状数组 (离散化), 也可以直接二分查找

    #include<bits/stdc++.h>
    using namespace std;
    #define ms(x,y) memset(x, y, sizeof(x))
    #define lowbit(x) ((x)&(-x))
    typedef long long LL;
    typedef pair<int,int> pii;
    
    const int maxn = 2e5+5;
    
    LL C[maxn<<1], all[maxn<<1], a[maxn], b[maxn], c[maxn], d[maxn], cnt;
    
    void add(int pos, int val) {
        for(; pos < (maxn<<1); pos += lowbit(pos))
            C[pos] += val;
    }
    
    int getsum(int pos) {
        int ret = 0;
        for(; pos; pos -= lowbit(pos))
            ret += C[pos];
        return ret;
    }
    
    void run_case() {
        int n; cin >> n;
        int allcnt = 0;
        for(int i = 1; i <= n; ++i) {
            cin >> a[i];
        }
        for(int i = 1; i <= n; ++i) {
            cin >> b[i];
        }
        for(int i = 1; i <= n; ++i) {
            d[i] = a[i] - b[i];
            c[i] = b[i] - a[i];
            all[++allcnt] = c[i];
            all[++allcnt] = d[i];
        }
        sort(all+1, all+1+allcnt);
        cnt = unique(all+1, all+1+allcnt) - all - 1;
        for(int i = 1; i <= n; ++i) {
            c[i] = lower_bound(all+1, all+1+cnt, c[i]) - all;
        }
        for(int i = 1; i <= n; ++i) {
            d[i] = lower_bound(all+1, all+1+cnt, d[i]) - all;
        }
        LL ans = 0;
        for(int i = 1; i <= n; ++i) {
            ans += getsum(d[i]-1);
            add(c[i], 1);
        }
        cout << ans;
    }
    
    int main() {
        ios::sync_with_stdio(false), cin.tie(0);
        cout.flags(ios::fixed);cout.precision(2);
        //int t; cin >> t;
        //while(t--)
        run_case();
        cout.flush();
        return 0;
    }
    
    #include<bits/stdc++.h>
    using namespace std;
    #define ms(x,y) memset(x, y, sizeof(x))
    #define lowbit(x) ((x)&(-x))
    typedef long long LL;
    typedef pair<int,int> pii;
    
    const int maxn = 2e5+5;
    
    vector<int> v;
    int a[maxn], b[maxn], x;
    LL ans = 0;
    
    void run_case() {
        int n; cin >> n;
        for(int i = 1; i <= n; ++i) cin >> a[i];
        for(int i = 1; i <= n; ++i) cin >> b[i];
        for(int i = 1; i <= n; ++i) v.push_back(a[i] - b[i]);
        sort(v.begin(), v.end());
        for(int i = 0; i < n-1; ++i) {
            x = upper_bound(v.begin()+i+1, v.end(), -v[i]) - v.begin();
            ans += n - x;
        }
        cout << ans;
    }
    
    int main() {
        ios::sync_with_stdio(false), cin.tie(0);
        cout.flags(ios::fixed);cout.precision(2);
        //int t; cin >> t;
        //while(t--)
        run_case();
        cout.flush();
        return 0;
    }
    
  • 相关阅读:
    服务返返回状态码详解
    LeetCode#28 Implement strStr()
    LeetCode#58 Length of Last Word
    LeetCode#66 Plus One
    spooling技术
    文件的打开与关闭
    DMA方式与通道方式
    中断向量、向量中断、向量地址
    中断响应优先级和中断处理优先级
    I/O接口
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12487610.html
Copyright © 2011-2022 走看看