hdu 5200 Trees [ 排序 离线 2指针 ]
Trees
Today CodeFamer is going to cut trees.There are N trees standing in a line. They are numbered from 1 to N. The tree numbered i has height hi. We say that two uncutted trees whose numbers are x and y are in the same block if and only if they are fitting in one of blow rules:
1)x+1=y or y+1=x;
2)there exists an uncutted tree which is numbered z, and x is in the same block with z, while y is also in the same block with z.
Now CodeFamer want to cut some trees whose height is not larger than some value, after those trees are cut, how many tree blocks are there?
Multi test cases (about 15).
For each case, first line contains two integers N and Q separated by exactly one space, N indicates there are N trees, Q indicates there are Q queries.
In the following N lines, there will appear h[1],h[2],h[3],…,h[N] which indicates the height of the trees.
In the following Q lines, there will appear q[1],q[2],q[3],…,q[Q] which indicates CodeFamer’s queries.
Please process to the end of file.
[Technical Specification]
1≤N,Q≤50000
0≤h[i]≤1000000000(109)
0≤q[i]≤1000000000(109)
For each q[i], output the number of tree block after CodeFamer cut the trees whose height are not larger than q[i].
3 2
5
2
3
6
2
0
2
In this test case, there are 3 trees whose heights are 5 2 3. For the query 6, if CodeFamer cuts the tree whose height is not large than 6, the height form of left trees are -1 -1 -1(-1 means this tree was cut). Thus there is 0 block. For the query 2, if CodeFamer cuts the tree whose height is not large than 2, the height form of left trees are 5 -1 3(-1 means this tree was cut). Thus there are 2 blocks.
题解:
窝题目看错了,,悲催啊。。题解以代码均来自小微哥。
对树的高度和查询的高度都分别排序。
然后,两个指针操作,O(n+m)复杂度。
| 13341721 | 2015-04-04 21:14:13 | Accepted | 5200 | 702MS | 4588K | 1496 B | C++ | czy |
代码来自小微哥:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 500005 struct point{
int i, v;
}p[M];
struct node
{
int v;
int cur;
}s[M];
int has[M], res[M]; bool cmp (point a, point b)
{
return a.v > b.v;
}
int S()
{
int ret=,ok=;
char c;
while((c=getchar()))
{
if(c>=''&&c<='')
ret=(ret<<)+ret+ret+c-'',ok=;
else if(ok)
return ret;
}
return ret;
}
bool cmp2(node a,node b)
{
return a.v<b.v;
}
int main()
{
int n, d, i, j, ans;
while (~scanf("%d%d",&n,&d))
{
for (i = ; i < n; i++)
{
p[i].v=S();
p[i].i = i+;
}
sort(p, p+n, cmp);
for (j = ; j < d; j++)
{
s[j].v=S();
s[j].cur=j;
}
sort(s,s+d,cmp2);
memset(has, , sizeof(has));
ans = ;
for (i = , j = d - ; j >= ; j--)
{
for ( ; i < n; i++)
{
if (p[i].v <= s[j].v)
break;
int id = p[i].i;
has[id] = ;
if (!has[id-] && !has[id+]) ++ans;
else if (has[id-] && has[id+]) --ans;
}
res[s[j].cur] = ans;
}
for (i = ; i < d; i++)
{
printf ("%d\n", res[i]);
}
}
return ;
}
hdu 5200 Trees [ 排序 离线 2指针 ]的更多相关文章
- HDU 5200 Trees 二分
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5200 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- HDU 5200 脑洞题 离线
线段树,TLE,各种.唉....我真是笨死了.... 我用的线段树是记录左右区间最长连续棵数的...反正TLE #include <iostream> #include <cstdi ...
- HDU 4857 拓扑排序 优先队列
n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得 ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- 《挑战30天C++入门极限》在c/c++中利用数组名作为函数参数传递排序和用指针进行排序的例子。
在c/c++中利用数组名作为函数参数传递排序和用指针进行排序的例子. 以下两个例子要非常注意,函数传递的不是数组中数组元素的真实值而是数组在内存中的实际地址. #include <std ...
- J - Super Mario HDU - 4417 线段树 离线处理 区间排序
J - Super Mario HDU - 4417 这个题目我开始直接暴力,然后就超时了,不知道该怎么做,直接看了题解,这个习惯其实不太好. 不过网上的思路真的很厉害,看完之后有点伤心,感觉自己应该 ...
- Trees on the level(指针法和非指针法构造二叉树)
Trees on the level Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 4746 莫比乌斯反演+离线查询+树状数组
题目大意: 一个数字组成一堆素因子的乘积,如果一个数字的素因子个数(同样的素因子也要多次计数)小于等于P,那么就称这个数是P的幸运数 多次询问1<=x<=n,1<=y<=m,P ...
- HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...
随机推荐
- [ CQOI 2018 ] 异或序列
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个不为空的子段异或和为 \(k\ ...
- [BZOJ1050][HAOI2006]旅行comf 枚举+并查集
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1050 将边排序,枚举边权最小的边,依次加边直到S和T连通,更新答案. #include&l ...
- js深拷贝与浅拷贝的区别及实现
1. 对于基本数据类型 其值在内存中占据着固定大小的空间,并被保存在栈内存中.当一个变量向另一个变量复制基本类型的值,会创建这个值的副本,并且我们不能给基本数据类型的值添加属性.其为深拷贝. 2. 对 ...
- iOS 动画(基于Lottie封装)
一般app中都会带有动画,而如果是一些复杂的动画,不但实现成本比较高,而且实现效果可能还不能达到UI想要的效果,于是我们可以借助lottie来完成我们想要的动画. lottie动画1.gif ...
- Excel数据导入SQL Server
基本有2种方案,都是无需安装Office的方案 Ole DB读取 + BulkCopy 获取Excel各个SheetName //连接串 string strConn = "Provider ...
- FPGA编程技巧系列之按键边沿检测
抖动的产生: 通常的按键所用开关为机械弹性开关,当机械触点断开.闭合时,由于机械触点的弹性作用,一个按键开关在闭合时不会马上稳定地接通,在断开时也不会一下子断开.因而在闭合及断开的瞬间均伴随有一连串的 ...
- tree 树状构建
/*package ch.util; import com.trm.model.func.FunctionTree; import java.util.HashMap; import java.uti ...
- 阿里P7/P8学习路线图——技术封神之路
一.基础篇 JVM JVM内存结构 堆.栈.方法区.直接内存.堆和栈区别 Java内存模型 内存可见性.重排序.顺序一致性.volatile.锁.final 垃圾回收 内存分配策略.垃圾收集器(G1) ...
- linux下C的建立、编译和运行 gcc (附上Windows下visual c++的用法)
2019/6/24 1. 环境:window10下安装了MobaXterm,这里申请了阿里云的服务账号,可以直接使用linux系统,避免安装虚拟机等. 2. 判断linux下是否有GCC编译工具(我们 ...
- environ - 用户环境(变量)
SYNOPSIS 总览 extern char **environ; DESCRIPTION 描述 变量 environ 指向的是一个叫 'environment'(环境)的字符串数组 (这个变量必须 ...