POJ(2761)Feed the dogs (树状数组+离散化+贪心)
来源:互联网 发布:php utf8转gbk 编辑:程序博客网 时间:2024/06/06 20:00
#include <iostream>#include <algorithm>#include <stdio.h>#include <string>#include <string.h>#include <stdlib.h>#include <math.h>#include <vector>#include <map>#include <queue>#include <stack>#include <set>using namespace std ;const int maxn = 100002;struct dog{ int value; int id; friend bool operator<(const dog &a,const dog &b){ return a.value<b.value; }}dog[maxn];int has[maxn];int dog_value[maxn];struct feed{ int l,r,k; int id; friend bool operator<(const feed &a,const feed &b){ if(a.l == b.l) return a.r<b.r; return a.l<b.l; }}feed[maxn/2];int n,m;int c[maxn];int ans[maxn/2];int lowbit(int x){ return x&(-x);}void update(int id,int val){ for(int i = id;i < maxn;i += lowbit(i)) c[i] += val;}int find_kth(int k){ int ans = 0,cnt = 0; for(int i = 20;i >= 0;i--){ ans += (1<<i); if(ans >= maxn || cnt + c[ans] >= k) ans -= (1<<i); else cnt += c[ans]; } return ans+1;}int main(){ while(~scanf("%d%d",&n,&m)){ for(int i = 1;i <= n;i++){ scanf("%d",&dog[i].value); dog[i].id = i; } sort(dog+1,dog+n+1); dog_value[1] = dog[1].value; has[dog[1].id] = 1; for(int i = 2,j = 1;i <= n;i++){ if(dog[i].value != dog[i-1].value) dog_value[++j] = dog[i].value; has[dog[i].id] = j; } //for(int i = 1;i <= n;i++) // printf("%d %d\n",has[i],dog_value[i]); for(int i = 1;i <= m;i++){ scanf("%d%d%d",&feed[i].l,&feed[i].r,&feed[i].k); feed[i].id = i; } sort(feed+1,feed+m+1); //for(int i = 1;i <= m;i++) // printf("%d %d %d\n",feed[i].l,feed[i].r,feed[i].k); feed[0].l=1;feed[0].r=0; for(int i=1;i<=m;i++){ for(int j=feed[i-1].l;j<=feed[i-1].r&&j<feed[i].l;j++) update(has[j],-1); for(int j=feed[i].l<feed[i-1].r+1?feed[i-1].r+1:feed[i].l;j<=feed[i].r;j++) update(has[j],1); int num=find_kth(feed[i].k); ans[feed[i].id]=dog_value[num]; } for(int i = 1;i <= m;i++) printf("%d\n",ans[i]); } return 0;}
0 0
- POJ(2761)Feed the dogs (树状数组+离散化+贪心)
- POJ 2761 Feed the dogs Treap+离散处理
- poj 2761 Feed the dogs 平衡树,线段树,树状数组
- POJ 2761 Feed the dogs 树状数组求第k大的数
- POJ-2761-Feed the dogs
- POJ 2761 Feed the dogs
- POJ 2761 Feed the dogs
- POJ 2761 Feed the dogs
- poj 2761 Feed the dogs
- poj 2761 Feed the dogs
- Feed the dogs POJ
- poj 2761 Feed the dogs SBT
- [SBT]POJ 2761 Feed the dogs
- [统计] poj 2761 Feed the dogs
- 划分树 POJ 2761 feed the dogs
- poj 2761 Feed the dogs treap
- poj 2761 Feed the dogs 离线treap
- poj 2761 Feed the dogs 划分树
- ZOJ(1711)Sum It Up (DFS+剪枝+去重复)
- ZOJ(1004)Anagrams by Stack (DFS+stack)
- POJ(2481)Cows 树状数组
- NEU(1262: ASCII Sequence II)动态规划
- linux环境下的rmi常见问题
- POJ(2761)Feed the dogs (树状数组+离散化+贪心)
- FZU Problem 1926 填空(KMP好题一枚,确实好)
- Codeforces Round #183 (Div. 2) B. Calendar
- FZU Problem 2030 括号问题
- HDU(3374) (KMP + 最小表示法)
- HOJ (1042) 整数划分
- HDU(2509) Be the Winner /HDU(1907) John (Nim 博弈)
- HDU(1525)Euclid's Game
- HDU(1847)Good Luck in CET-4 Everybody!