HDU 4217 Data Structure? 树状数组
来源:互联网 发布:北航大数据在职研究生 编辑:程序博客网 时间:2024/05/27 09:48
Data Structure?
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2166 Accepted Submission(s): 692
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
首届华中区程序设计邀请赛暨第十届武汉大学程序设计大赛
不知道为什么。。。我用long long 交到GCC编译器上就是一直wa。 然后改__int64才过掉的。。
//// 4217.cpp// ACM_HDU//// Created by ipqhjjybj on 13-9-8.// Copyright (c) 2013年 ipqhjjybj. All rights reserved.//#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>using namespace std;#define lowbit(x) (x&(-x))const int MAXN=333333;int n,k;int a[MAXN];#define LL __int64void Update(int pos){ while(pos<=n){ a[pos]--; pos+=lowbit(pos); }}int Query(int pos){ int sum=0; while(pos>0){ sum+=a[pos]; pos-=lowbit(pos); } return sum;}int Bisearch(int k){ int l=1,r=n,mid; while(l<r){ mid=(l+r)>>1; if(Query(mid)>=k) r=mid; else l=mid+1; } return l;}int main(){ int t,tmp,tt=0; LL sum; scanf("%d",&t); while(t--){ scanf("%d %d",&n,&k); sum=0; for(int i=0;i<=n;i++) a[i]=lowbit(i); while(k--){ scanf("%d",&tmp); tmp=Bisearch(tmp); Update(tmp); sum+=tmp; } cout<<"Case "<<++tt<<": "<<sum<<endl; //printf("Case %d: %lld\n",++tt,sum); } return 0;}
我原本的查找代码:
int Bisearch(int k){ int l=1,r=n,mid; while(l<=r){ mid=(l+r)>>1; int tmp=Query(mid)+mid; if(tmp==k){ return mid; }else if(tmp<k){ l=mid+1; }else r=mid-1; } return l;}
在这题是错误的 。 因为它没有考虑到这样的情况。 假设第8个数据是0,这样前7个数据跟前8个数据和是相等的。 这样我们就可能查找到错误的8了。
所以,我们要保证查找到的是最左边的前tmp项和==k。。 这里我wa了好多次。
- 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 Data Structure? treap
- hdu 4217 Data Structure?/SBT
- HDU 4217 Data Structure?(线段树)
- HDU--4217 -- Data Structure? [线段树]
- hdu-4217-Data Structure? 线段树
- HDU 2217 Data Structure?
- 【HDU 4217】【经典题 树状数组求前k大】 Data Structure?【n个数1-n,k次操作,每次取出第ki小的数。问所有取出数字之和。】
- 队列(Queue),堆栈(Stack)
- DIV+CSS初始化模板
- android 常用控件的使用 TextView EditTextView
- 演示TDE的数据加密示例,并用logminer验证加密效果
- 面试题目收集(4)
- HDU 4217 Data Structure? 树状数组
- 写了个 大家经常会用到的 Tab 切换 效果
- 《数据结构》示范程序\优先队列的堆实现
- 数据类型
- 替换.DATA的COOKIE突破GS
- sth. about SSL (secure socket layer), JSSE (java secure socket extension)
- NAPI处理方式分析
- [Python入门及进阶笔记]Python-基础-列表及列表解析小结
- 一日一码03——插入排序