STL

来源:互联网 发布:js登录界面 编辑:程序博客网 时间:2024/04/28 02:19

hpuoj 1193: Interval [STL、双指针、二分]
注意题目下标是从1开始的;

#include <cstdio>//预处理前缀和,枚举最短区间,用单调的数组维护走过的前缀和,用于二分查找>=M的值,则后面的所有区间都满足条件#include <vector>//把所有满足的区间累加起来得到答案#include <map>#include <set>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int x[1000010];void STL(){    map<int,int> m;//键值,存的值    map<int,int>::iterator it1;//map的迭代器    set<int> s;//不允许出现重复的,可去重    s.insert(3);//插入值    set<int>::iterator it2;//set的迭代器    vector<int> v;//锯齿数组    vector<int>::iterator it3;//vector的迭代器    v.insert(it3,2);//要插入的地址,值}int main(){    int ncase;    scanf("%d",&ncase);    while(ncase--)    {        memset(x,0,sizeof(x));        int n,L,M;        scanf("%d%d%d",&n,&L,&M);        for(int i=1;i<=n;i++)        {            scanf("%d",&x[i]);            x[i]+=x[i-1];        }        vector<int> v;        int sum=0;        int l=n-L+1,r=n;        int Size=0;        for(;l>0;l--,r--)        {//lower_bound();以二分查找的第一个出现在数组或者容器里的这个值,返回值为这个数的迭代器,如果没有返回比这个数大的数的迭代器;         //upper_bound();以二分查找的一个出现在数组或者容器里的值比这个数大,返回值为这个大一点的数的迭代器;            vector<int>::iterator it=lower_bound(v.begin(),v.end(),x[r]);//定义迭代器            v.insert(it,x[r]);//在vector里插入数据            Size++;            int tmp=lower_bound(v.begin(),v.end(),M+x[l-1])-v.begin();            sum+=Size-tmp;        }        //begin到end遍历容器        printf("%d\n",sum);    }}
0 0