hdu4217(求区间k小)
2015-10-03 14:46
351 查看
思路:求k小与k大本质上窝觉得是一样的,可以互相转换。
/article/5032246.html这博客讲的还不错。
/article/5032246.html这博客讲的还不错。
/***************************************** Author :Crazy_AC(JamesQi) Time :2015 File Name : *****************************************/ // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <iomanip> #include <sstream> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <map> #include <set> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <limits.h> using namespace std; #define MEM(a,b) memset(a,b,sizeof a) typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> ii; const int inf = 1 << 30; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; inline int Readint(){ char c = getchar(); while(!isdigit(c)) c = getchar(); int x = 0; while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); } return x; } inline int lowbit(int x){ return x & (-x); } int bit[300000]; int n,m; LL ans; void add(int x,int v){ while(x <= n){ bit[x] += v; x += lowbit(x); } } int sum(int k){ int num = 0; int x = 0; for (int i = 18;i >= 0;i--){ if (x + (1 << i) <= n && bit[x + (1 << i)] + num < k){ x += (1 << i); num += bit[x]; } } return x + 1; } void solve(int kase){ ans = 0LL; memset(bit,0,sizeof bit); scanf("%d%d",&n,&m); for (int i = 1;i <= n;i++) add(i,1); for (int i = 0;i < m;i++){ int x; scanf("%d",&x); int tmp = sum(x); add(tmp,-1); ans += tmp; } printf("Case %d: ",kase); printf("%lld\n",ans); } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int icase = 0; int t; scanf("%d",&t); while(t--){ solve(++icase); } return 0; }
相关文章推荐
- 动手学习TCP:客户端状态变迁
- 数据库 SQL 2005 知识点二
- cobar
- 光于增采样和减采样(一)
- REDIS key notification
- hdu 3068 最长回文(manacher)
- JAVA深入研究——Method的Invoke方法。
- LintCode 删除排序数组中的重复数字
- 一个Sqrt函数引发的血案
- Log4j官方文档翻译(三、配置)
- 如何查看Linux的磁盘空间还有多少
- 贪心+拓扑排序 AOJ 2456 Usoperanto
- 网站伸缩性架构
- python搭建web应用框架以及一些基本语法
- cocos2dx 消除箱子
- 程序性能分析
- MapIterator和MapEnumerator区别
- Android Material Design Library系列教程(五)
- 基数排序
- iOS 多语言支持,应用程序国际化 xcode4.5