zoukankan      html  css  js  c++  java
  • UVA

    问题分析

    首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个。注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦找到便退出。利用lower_bound,upper_bound比较方便,这两个函数就是用二分实现的,二者之差就是相等的那部分。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    LL T, n;
    const int maxn = 4000 + 10;
    LL a[maxn], b[maxn], c[maxn], d[maxn];
    LL s1[maxn*maxn];
    int cnt, ans;
    int main()
    {
        std::ios::sync_with_stdio(false);
        //freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
        cin >> T;
        for(LL t = 1; t <= T; t++)
        {
            cnt = 0, ans = 0;
            if(t != 1)
                cout << endl;
            cin >> n;
            for(LL i = 0; i < n; i++)
                cin >> a[i] >> b[i] >> c[i] >> d[i];
            memset(s1, 0, sizeof(s1));
            for(LL i = 0; i < n; i++)
                for(LL j = 0; j < n; j++)
                {
                    s1[cnt] = a[i] + b[j];
                    cnt++;
                }
            sort(s1, s1 + cnt);
            for(LL i = 0; i < n; i++)
            {
                for(LL j = 0; j < n; j++)
                {
                    ans += upper_bound(s1, s1 + cnt, -(c[i] + d[j])) - lower_bound(s1, s1 + cnt, -(c[i] + d[j]));
                }
            }
            cout << ans << endl;
        }
    
    }
    
  • 相关阅读:
    Python 冒泡排序
    编程规范之注释篇
    编程规范之变量命名篇
    安装Django
    字典&列表的拓展理解
    小球落地
    Python结合sql登陆案例
    面向对象:两手交换牌
    每日一题(一)
    Web应用之LAMP源码环境部署
  • 原文地址:https://www.cnblogs.com/KeepZ/p/11349202.html
Copyright © 2011-2022 走看看