--------明天又要上课了,放假啥也没学貌似,竟玩了。
题意:有三种操作,0加数, 1删除数, 2找比这个数大的第k个数。
思路:树状数组的话可以把0~100000范围所有的数做个标记,如果插入了这个数就标记为1,难的地方是2操作
找第k个比a大的数,那就是直接看a这个数后面第k个为1的数,直接从a到100000二分查找就好。
本题代码
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
int c[100005];
int lowbit(int k)
{
return k & (-k);
}
int sum(int x)
{
int ans = 0;
while (x > 0)
{
ans += c[x];
x -= lowbit(x);
}
return ans;
}
void add(int x, int val)
{
while (x <= 100000)
{
c[x] += val;
x += lowbit(x);
}
}
void PRINT(int a, int k)
{
int l = a, r = 100000, tmp;
int now = sum(a);
while (l < r)
{
int mid = (l+r)/2;
tmp = sum(mid);
if (tmp < now + k) l = mid + 1;
else r = mid;
}
if (sum(l) >= now + k) printf("%d\n", l);
else printf("Not Find!\n");
}
int main()
{
int n;
while (scanf("%d", &n) == 1)
{
memset(c, 0, sizeof c);
int ope, aa, kk;
while (n--)
{
scanf("%d%d", &ope, &aa);
if (ope == 0)
{
add(aa, 1);
}
else if (ope == 1)
{
if (sum(aa) - sum(aa-1) == 0) printf("No Elment!\n");
else add(aa, -1);
}
else
{
scanf("%d", &kk);
PRINT(aa, kk);
}
}
}
return 0;
}
HDU2852题解
本文介绍了解决HDU2852问题的方法,该问题涉及三种操作:增加数值、删除数值以及查找大于指定数值的第k个元素。通过使用树状数组来标记数值范围内的状态,并实现高效的查找算法。
1515

被折叠的 条评论
为什么被折叠?



