hdu 4217 Data Structure?
2013-11-16 12:12
399 查看
Data Structure?
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2251 Accepted Submission(s): 711
[align=left]Problem Description[/align]
Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data in a computer so that it can be used efficiently. Today let me introduce a data-structure-like
problem for you.
Original, there are N numbers, namely 1, 2, 3...N. Each round, iSea find out the Ki-th smallest number and take it away, your task is reporting him the total sum of the numbers he has taken away.
[align=left]Input[/align]
The first line contains a single integer T, indicating the number of test cases.
Each test case includes two integers N, K, K indicates the round numbers. Then a line with K numbers following, indicating in i (1-based) round, iSea take away the Ki-th smallest away.
Technical Specification
1. 1 <= T <= 128
2. 1 <= K <= N <= 262 144
3. 1 <= Ki <= N - i + 1
[align=left]Output[/align]
For each test case, output the case number first, then the sum.
[align=left]Sample Input[/align]
2 3 2 1 1 10 3 3 9 1
[align=left]Sample Output[/align]
Case 1: 3 Case 2: 14
/*本题用线段树来存储数据,因为树的叶子上数字从1到n顺序排列。
我们可以定义一个变量t记录该树枝中共有多少个数字,
每次判断第K小的数字和左边的树枝上数字个数的大小关系(若K小,则第K小的数字必在左边树枝上;否则必在右边树枝上);
然后进入下一个枝节点,直到找到第K小数字;*/
#include<stdio.h> #include<string.h> #include<math.h> #define N 265000 struct node { int x,y,t; }a[N*3]; void Creat(int t,int x,int y) { a[t].x=x; a[t].y=y; if(x==y) { a[t].t=1; return ; } int temp=t*2,mid=(x+y)/2; Creat(temp,x,mid); Creat(temp+1,mid+1,y); a[t].t=a[temp].t+a[temp+1].t; } int Find(int t,int x,int y,int k) { if(x==y) { a[t].t=0; return x; } int temp=t*2,mid=(x+y)/2,sum; if(k<=a[temp].t) sum=Find(temp,x,mid,k); else sum=Find(temp+1,mid+1,y,k-a[temp].t); //特别注意:第K小数字在右边时则减去左边的的数字个数 a[t].t=a[temp].t+a[temp+1].t; return sum; } int main() { int T,i,cnt=1,n,k; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&k); Creat(1,1,n); __int64 ans=0; while(k--) { scanf("%d",&i); ans+=Find(1,1,n,i); } printf("Case %d: %I64d\n",cnt++,ans); } return 0; }
相关文章推荐
- HDU 4217 Data Structure?
- hdu 4217 Data Structure?/SBT
- 线段树 HDU 4217 Data Structure? 单点更新 区间查询
- hdu-4217-Data Structure? 线段树
- HDU 4217 Data Structure?(线段树)
- hdu 4217 Data Structure?(树状数组)
- hdu 4217 Data Structure? treap
- HDU 4217 Data Structure?(线段树 or 树状数组啊)
- 【HDU 4217】【经典题 树状数组求前k大】 Data Structure?【n个数1-n,k次操作,每次取出第ki小的数。问所有取出数字之和。】
- HDU 4217 Data Structure?(线段树版)
- {HDU}{4217}{Data Structure}{树状数组or线段树}
- HDU 4217 Data Structure?
- HDU 4217 Data Structure?
- hdu 4217 Data Structure? 树状数组求第K小
- HDU 4217 Data Structure?(树状数组求前k大)
- HDU 4217 Data Structure?
- HDU 4217 Data Structure?(线段树的查找和更新)
- hdu 4217 Data Structure?
- HDU 4217 Data Structure?(树状数组+二分)
- hdu 题目4217 Data Structure?(线段树,单点更新)