无题 stl 二分

来源:互联网 发布:松下plc编程手册 编辑:程序博客网 时间:2024/06/01 09:17
#include <iostream>#include <cstdio>#include <cstring>#include <math.h>#include <algorithm>#include <cstdlib>using namespace std;int A[100005];int main() {    int T;    scanf("%d",&T);    while(T--) {        int  n,m;        scanf("%d%d",&n,&m);        for(int i=0; i<n; i++) {            scanf("%d",&A[i]);        }        sort(A,A+n,less<int>() );        int pos1,pos2;        pos1=0;        int res=0;        while(pos1<n) {            int data;            data=m-A[pos1];            pos2=upper_bound(A,A+n,data)-A;            pos2=pos2-1;            if(pos2>pos1)            {                res=res+pos2-pos1;            }            pos1++;        }        printf("%d\n",res);    }}//就是练练upper_bound 和lower_bound 而已

原创粉丝点击