zoukankan      html  css  js  c++  java
  • [POI2014] KUR-Couriers(洛谷P3567)

    洛谷题目链接:[POI2014]KUR-Couriers

    题目描述

    Byteasar works for the BAJ company, which sells computer games.

    The BAJ company cooperates with many courier companies that deliver the games sold by the BAJ company to its customers.

    Byteasar is inspecting the cooperation of the BAJ company with the couriers.

    He has a log of successive packages with the courier company that made the delivery specified for each package.

    He wants to make sure that no courier company had an unfair advantage over the others.

    If a given courier company delivered more than half of all packages sent in some period of time, we say that it dominated in that period.

    Byteasar wants to find out which courier companies dominated in certain periods of time, if any.

    Help Byteasar out!

    Write a program that determines a dominating courier company or that there was none.

    给一个数列,每次询问一个区间内有没有一个数出现次数超过一半

    输入输出格式

    输入格式:

    The first line of the standard input contains two integers, (n) and (m) ((1 leq n, m leq 50000)), separated by a single space, that are the number of packages shipped by the BAJ company and the number of time periods for which the dominating courier is to be determined, respectively.

    The courier companies are numbered from 1 to (at most) (n).

    The second line of input contains (n) integers, (p_1,p_2,...,p_3,p_n) ((1 leq p_i leq n)), separated by single spaces; (p_i) is the number of the courier company that delivered the (i)-th package (in shipment chronology).

    The (m) lines that follow specify the time period queries, one per line.

    Each query is specified by two integers, (a) and (b) ((1 leq a leq b leq n)), separated by a single space.

    These mean that the courier company dominating in the period between the shipments of the (a)-th and the (b)-th package, including those, is to be determined.

    In tests worth 65% of total score, the condition (n, m leq 50000) holds, and in tests worth 30% of total score (n, m leq 5000)

    输出格式:

    The answers to successive queries should be printed to the standard output, one per line.

    (Thus a total of (m) lines should be printed.) Each line should hold a single integer: the number of the courier company that dominated in the corresponding time period, or 0 if there was no such company.

    输入输出样例

    输入样例#1:

    7 5
    1 1 3 2 3 4 3
    1 3
    1 4
    3 7
    1 7
    6 6

    输出样例#1:

    1
    0
    3
    0
    4

    说明

    给一个数列,每次询问一个区间内有没有一个数出现次数超过一半

    一句话题意: 给出一个数列,每次询问一个区间内有没有一个数出现次数超过一半(严格大于).

    题解: 既然是静态查询区间的第几小有多少个,所以可以用主席树来维护一下区间中的每个元素个数,然后在查询的时候就判断左子树/右子树是否可以满足条件,如果都不能满足条件,就在查询的过程中返回0.其余都是基本操作.

    #include<bits/stdc++.h>
    using namespace std;
    const int N=500000+5;
    
    int n, m, w[N], s[N], rk[N], root[N], size, cnt = 0;
    
    struct president_tree{
        int ls, rs, cnt;
    }t[N*20];
    
    int gi(){
        int ans = 0, f = 1; char i = getchar();
        while(i<'0'||i>'9'){if(i=='-')f=-1;i=getchar();}
        while(i>='0'&&i<='9'){ans=ans*10+i-'0';i=getchar();}
        return ans * f;
    }
    
    void update(int &node,int last,int pos,int l=1,int r=size){
        node = ++cnt; t[node] = t[last]; t[node].cnt++;
        if(l == r) return; int mid = (l+r>>1);
        if(pos <= mid) update(t[node].ls,t[last].ls,pos,l,mid);
        else update(t[node].rs,t[last].rs,pos,mid+1,r);
    }
    
    int query(int node,int last,int sum,int l=1,int r=size){
        if(l == r) return l; int mid = (l+r>>1);
        if(2*(t[t[node].ls].cnt-t[t[last].ls].cnt) > sum)
    		return query(t[node].ls,t[last].ls,sum,l,mid);
        if(2*(t[t[node].rs].cnt-t[t[last].rs].cnt) > sum)
    		return query(t[node].rs,t[last].rs,sum,mid+1,r);
        return 0;
    }
    
    int main(){
        //freopen("data.in","r",stdin);
        int l, r; n = gi(); m = gi();
        for(int i=1;i<=n;i++) w[i] = gi();
        memcpy(s,w,sizeof(s)); sort(s+1 , s+n+1);
        size = unique(s+1 , s+n+1)-s-1;
        for(int i=1;i<=n;i++) rk[i] = lower_bound(s+1 , s+size+1 , w[i])-s;
        for(int i=1;i<=n;i++) update(root[i],root[i-1],rk[i]);
        for(int i=1;i<=m;i++){
    		l = gi(); r = gi();
    		printf("%d
    ",query(root[r],root[l-1],r-l+1));
        }
        return 0;
    }
    
  • 相关阅读:
    单选框radio 选择问题
    用户名、密码等15个常用的js正则表达式
    mysql创建远程用户并授权
    JS,Jquery获取屏幕的宽度和高度
    ThinkPHP模版时间显示
    composer启用国内镜像网站的配置更改办法
    tp6 使用全局中间件配置验证器
    tp3.2查询当前时间大于已有时间三分钟
    jquery判断手机端或者pc端
    html页面引入公共的页首和导航栏
  • 原文地址:https://www.cnblogs.com/BCOI/p/9011425.html
Copyright © 2011-2022 走看看