zoukankan      html  css  js  c++  java
  • HDU 5101

    hdoj5101 lower_bound函数;
    题意:
    从两个不同集合拿出两个数,加的和大于k的可行的方案数

    思路: 答案=从所有数中选择的两个加和大于k的数的方案数-在同一个集合中选择的两个加和大于k的数的方案数
    对于同一个集合中选择的两个加和大于k的方案数,
    直接排序,
    然后利用单调性快速统计出来的。
    ————————————————————————。

    
    #include<iostream>
    #include<cstdio>
    #include<stdlib.h>
    #include<vector>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    #define INF 0x3f3f3f3f
    
    
    
    int main()
    {
        int T,x;
        int n,k,m;
        scanf("%d",&T);
        while(T--)
        {
            vector<int>q[1010];
            scanf("%d%d",&n,&k);
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&m);
                for(int j=1;j<=m;j++)
                {
                    scanf("%d",&x);
                    q[i].push_back(x);
                    q[0].push_back(x);
                }
                sort(q[i].begin(),q[i].end());
            }
            sort(q[0].begin(),q[0].end());
            LL ans=0;
            LL num1,num2;
            int v;
            for(int i=1;i<=n;i++)
            {
                for(int j=0;j<(int)q[i].size();j++)
                {
                    v=q[i][j];
                    num1=q[0].end()-lower_bound(q[0].begin(),q[0].end(),k-v+1); //最低插入地址;
                    num2=q[i].end()-lower_bound(q[i].begin(),q[i].end(),k-v+1);
                    ans+=num1-num2;
                }
            }
            printf("%lld
    ",ans/2);
        }
        return 0;
    }
  • 相关阅读:
    c++ this *this
    名称空间
    c++ 静态持续变量
    c++ 数组
    c++ 头文件
    实例化和具体化详解
    在linux下安装eclipse以及运行c++程序的安装步骤
    在centos (linux) 搭建 eclipse c++开发分环境
    Linux上使用Qt Creator进行C/C++开发
    使用Qt Creator 2.60编写C/C++程序
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934551.html
Copyright © 2011-2022 走看看