HDU2852 KiKi's K-Number

题解:

树状求第k大数题目。。二分就行。。。

不过感觉二分是玄学。。。要多试几个不同的二分方式。。。。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define ll long long
#define CLR(x) memset(x,0,sizeof x)
#define SZ(x) ((int)(x).size())
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
typedef pair<int,int> P;
const double eps=1e-9;
const int maxn=100010;
const int N=1000+10;
const int mod=9901;

ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
//-----------------------------------------------------------------------------

int m,op,a,b;
int c[maxn];

int lowbit(int x){return x&-x;}

void add(int x,int v){
    while(x<maxn){
        c[x]+=v;
        x+=lowbit(x);
    }
}

int sum(int x){
    int cnt=0;
    while(x){
        cnt+=c[x];
        x-=lowbit(x);
    }
    return cnt;
}

int bs(int l,int r,int goal){
    int tmp=-1;
    int m,L=l,R=r;
    while(L<R){
        m=(L+R)>>1;
        if(sum(m)-sum(l-1)>=goal){
            tmp=m;
            R=m;
        }
        else L=m+1;
    }
    return tmp;
}

int main(){
    while(~scanf("%d",&m)){
        CLR(c);
        for(int i=1;i<=m;i++){
            scanf("%d",&op);
            if(op==0){
               scanf("%d",&a);
               add(a,1);
            }
            if(op==1){
                scanf("%d",&b);
                if(sum(b)-sum(b-1)==0) printf("No Elment!\n");
                else add(b,-1);
            }
            if(op==2){
                scanf("%d%d",&a,&b);
                if(bs(a+1,maxn,b)==-1) printf("Not Find!\n");
                else printf("%d\n",bs(a+1,maxn,b));
            }
        }
    }
    return 0;
}

HDU2852 KiKi's K-Number

时间: 2024-10-10 09:32:53

HDU2852 KiKi's K-Number的相关文章

hdu2852 KiKi&#39;s K-Number 树状数组求第k大数

//再求第k大数时只需要getsum(b-1)<getsum(a)+k<=getsum(b) //b就是a的第k大数 //又gesum(b-1)<=getsum(b)则可以用二分查找来做 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=100010; int tree[maxn]; int lowbit(int i) {

树状数组求第K小值 (spoj227 Ordering the Soldiers &amp;&amp; hdu2852 KiKi&#39;s K-Number)

题目:http://www.spoj.com/problems/ORDERS/ and http://acm.hdu.edu.cn/showproblem.php?pid=2852 题意:spoj227:告诉每个位置前面有多少个数比当前位置小,求出原序列.hdu2852:设计一个容器,支持几种操作:增加/删除元素,求容器中比a大的数中第k小的数是多少. 分析:两个题思路都是求数组里面的第K小的数.开始一直在找O(N*logN)的方法,后来发现O(N*logN*logN)也是可以过的...两步:和

hdu2852 KiKi&amp;#39;s K-Number

Problem Description For the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. Now Kiki meets a very similar problem, kiki wants to design a container, the container is to support the three operations. Push: Pus

HDU2852 KiKi&#39;s K-Number 树状数组+二分

这题就是给了你三种操作, 1:往容器中一个元素 x 2::把容器中的元素x删除 3:查询比 x大的第k个数 想法:添加元素跟删除元素  直接是以数本身为序号然后以 value值为1和-1即可,相当于计数,至于找比x第k个大的数,那就看看当前往后数k个数的第一个数是哪个就可以了,一开始直接找出来,然后往后暴力的扫了一遍,结果错了,没关系,反应很快,直接改了个二分查找,然后就过了,弄清楚如何建立这个树状数组即可 #include<iostream> #include<cstdio> #

hdu-2852 KiKi&#39;s K-Number---二分+树状数组

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意: 题意:    给出三种操作,    0 在容器中插入一个数.    1 在容器中删除一个数.    2 求出容器中大于a的第k大元素. 解题思路: 用树状数组维护每个值,插入数字是add(x, 1),删除时add(x, -1) 查询第k大时,先判断是否存在,存在的话直接根据树状数组sum值的单调性二分法求解即可 1 #include<iostream> 2 #include&l

347. Top K Frequent Elements

Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: 347. Top K Frequent ElementsYou may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's

[LeetCode]Top K Frequent Elements

题目描述: Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must

347. Top K Frequent Elements [medium] (Python)

题目链接 https://leetcode.com/problems/top-k-frequent-elements/ 题目原文 Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ numbe

[LeetCode] 347. Top K Frequent Elements 解题思路 - Java

Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must be bet