Hdu 4217 Data Structure?【二分+树状数组】
来源:互联网 发布:nginx pathinfo 编辑:程序博客网 时间:2024/04/30 02:14
Data Structure?
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3359 Accepted Submission(s): 1077
Problem Description
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.
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.
Input
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
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
Output
For each test case, output the case number first, then the sum.
Sample Input
23 21 110 33 9 1
Sample Output
Case 1: 3Case 2: 14
Author
iSea@WHU
Source
首届华中区程序设计邀请赛暨第十届武汉大学程序设计大赛
题目大意:
给你N个数,从1~N,每次从中拿第ki小的数,然后去掉这个数,一共拿K次,问累加的和是多少。
思路:
一开始我们让数组都设定为1,表示各个数都没有被拿过,然后我们每一次拿的时候,二分一下树上剩余的第k个数的位子就行了。
过程维护一下即可。
Ac代码:
#include<stdio.h>#include<string.h>using namespace std;int tree[400005];int n,k;int lowbit(int x){ return x&(-x);}int sum(int x){ int sum=0; while(x>0) { sum+=tree[x]; x-=lowbit(x); } return sum;}void add(int x,int c){ while(x<=n) { tree[x]+=c; x+=lowbit(x); }}int main(){ int t; int kase=0; scanf("%d",&t); while(t--) { memset(tree,0,sizeof(tree)); scanf("%d%d",&n,&k); for(int i=1;i<=n;i++)add(i,1); long long int output=0; for(int i=0;i<k;i++) { int x;scanf("%d",&x); int l=1; int r=n; int pos=-1; while(r-l>=0) { int mid=(l+r)/2; if(sum(mid)>=x) { if(sum(mid)==x)pos=mid; r=mid-1; } else l=mid+1; } if(pos==-1)continue; else output+=pos,add(pos,-1); } printf("Case %d: %lld\n",++kase,output); }}
阅读全文
0 0
- Hdu 4217 Data Structure?【二分+树状数组】
- HDU 4217 Data Structure? 树状数组
- hdu 4217 Data Structure?(树状数组)
- HDU 4217 Data Structure? 线段树 OR 树状数组
- 【Data Structure】树状数组
- HDU 4217 Data Structure?(树状数组求前k大)
- HDU 4217 Data Structure?(线段树 or 树状数组啊)
- hdu-4217Data Structure?
- hdu 4217 Data Structure?
- HDU 4217 Data Structure?
- hdu 4217Data Structure?
- HDU 4217 树状数组+二分查找
- hdu 4217 Data Structure? treap
- hdu 4217 Data Structure?/SBT
- HDU 4311 树状数组+二分
- HDU 2852 树状数组+二分
- HDU 5493(树状数组+二分)
- hdu 5592 树状数组+二分
- Java中分割字符串
- 二叉树三种遍历非递归实现【每日一题】
- ES基础
- iOS崩溃日志解析脚本
- 机器学习实战之K-邻近算法
- Hdu 4217 Data Structure?【二分+树状数组】
- strcasecmp函数
- Python Number(数字)
- J2EE学习中一些值得研究的开源项(转)
- Java导出excel
- Storm实时大数据处理(三)
- Java Float类型 减法运算时精度丢失问题
- Spring创建Bean的三种方式
- Exception处理