点击打开题目链接

题型就是数据结构。给一个数组,然后又k次操作,每次操作给定一个数ki, 从数组中删除第ki小的数,要求的是k次操作之后被删除的所有的数字的和。 

简单的思路就是,用1标记该数没有被删除,0表示已经被删除,对于找到第ki小的数, 只需要找到标记数组中第一个前缀和为ki的下标,又因为用来标记的数组的前缀和是不减数列,所以可以用二分来加速。这里值得注意的是,被删除后的数,不会第二次或者多次被找到,即每个数最多被找到一次,因为如果该数被删除了,而且该数所在下标的前缀和是ki,那么一定还存在一个更小的下标,使得它的前缀和也是ki, 而我们要找的就是第一次出现前缀和为ki的下标。还需要使用I64.

附上代码:

 /*************************************************************************
> File Name: 4217.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年04月26日 星期六 21时51分19秒
> Propose: HDU 4217
************************************************************************/
//BIT + BinarySearch 复杂度 O(k * logn * logn)
//单点更新,区间求值, 用1表示该数没有被删除,0表示该数已经被删除
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define X first
#define Y second
#define MAX_N (262144 + 5)
typedef long long LL;
typedef pair<int, int> pii;
int n, k;
int c[MAX_N]; int
lowbit(int x) {
return x & (-x);
} LL
get_sum(int x) {
LL s = ;
while (x > ) {
s += c[x];
x -= lowbit(x);
} return s;
} void
update(int x, int v) {
while (x <= n) {
c[x] += v;
x += lowbit(x);
} return ;
} int
main(void) {
int T, cnt = ;
scanf("%d", &T);
while (T--) {
scanf("%d %d", &n, &k);
memset(c, , sizeof(c));
for (int i = ; i <= n; i++) {
update(i, );
}
LL ans = ;
for (int i = ; i < k; i++) {
int ki;
scanf("%d", &ki);
//只需要找到前缀和为ki对应的数,也就是第ki小的数
//此处为不减数列,所以使用二分来找到第一个前缀和为ki对应的数
int L = ki, R = n, tmp = L;
while (L <= R) {
int mid = L + (R - L) / ;
int s = get_sum(mid);
if (s < ki) {
L = mid + ;
} else {
if (s == ki) {
tmp = mid;
}
R = mid;
}
if (tmp == L && tmp == R) {
break;
}
}
ans += tmp;
update(tmp, -);
}
printf("Case %d: %I64d\n", cnt++, ans);
} return ;
}

HDU 4217的更多相关文章

  1. hdu 4217 Data Structure?/treap

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4217 可用线段树写,效率要高点. 这道题以前用c语言写的treap水过了.. 现在接触了c++重写一遍 ...

  2. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. HDU 4217 Hamming Distance 随机化水过去

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  4. HDU 2217 Data Structure?

    C - Data Structure? Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  5. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  6. DNA sequence HDU - 1560

    DNA sequence Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  9. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

随机推荐

  1. 处理iphone的 .play() 不能播放问题

    一.添加音乐 <audio id="Jaudio" src="shake.mp3" preload loop="loop" contr ...

  2. 【DM8168学习笔记4】ezsdk安装过程记录

    安装文件 ezsdk_dm816x-evm_5_05_02_00_setuplinux DM8168-EZSDK文件结构如图所示.               (图片来自:http://process ...

  3. Jmeter环境搭建及目录介绍

    Jmeter环境搭建及目录介绍 运行环境:jmeter是基于java语言的,所有需要准备Java的JDK环境,并添加到环境变量中. 一.环境搭建及配置 1.JMeter的下载: 官网:http://j ...

  4. spring-注解配置-junit整合测试-aop

    1 使用注解配置spring 1.1 步骤 导包4+2+spring-aop 1.为主配置文件引入新的命名空间(约束) 2.开启使用注解代理配置文件 3.在类中使用注解完成配置 1.2 将对象注册到容 ...

  5. webservice作用(优,缺点;作用)

    1其实我们平时的应用,有一方面考虑是部署方便,维护容易~!如果是DLL,部署,更新需要每个应用了这个DLL的应用程序都作相应的引用更新...而如果用了Ws,则不用,因为它通过网络部署,通过网络引用,基 ...

  6. Newtonsoft.json 二次引用出错解决办法

    一.一般在C# 项目中二次引用会出现如下错误: 解决办法:用编辑器打开项目下的文件(*.csproj),可以找到在这个文件中,Newtonsoft.Json的引用,删掉引用,然后在项目中重新引用就可以 ...

  7. npm install时出现error

    今天启动vue前端时,发现依赖没了.于是乎cmd->npm install->更新了部分依赖后,出现error信息,提示更新依赖失败.很奇怪,原来这个项目都是好的,为啥突然更新下来依赖了呢 ...

  8. 2018-2-13-visual-Studio-无法调试,提示程序跟踪已退出

    title author date CreateTime categories visual Studio 无法调试,提示程序跟踪已退出 lindexi 2018-2-13 17:23:3 +0800 ...

  9. [Array]485. Max Consecutive Ones

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  10. 转:Android检查设备是否联网

    public static boolean isConnect(Context context) { ConnectivityManager connectionManager = (Connecti ...