hdu 5200 Trees [ 排序 离线 2指针 ]

本文详细介绍了如何通过排序和双指针技巧解决树形结构下特定高度限制条件下的树块数量计算问题,包括输入案例解析、算法实现及优化策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

Trees

 
 Accepts: 156
 
 Submissions: 533
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
Problem Description

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?

Input

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]

1N,Q50000

0h[i]1000000000(109)

0q[i]1000000000(109)

Output

For each q[i], output the number of tree block after CodeFamer cut the trees whose height are not larger than q[i].

Sample Input
3 2
5
2
3
6
2
Sample Output
0
2
Hint
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)复杂度。

 

133417212015-04-04 21:14:13Accepted5200702MS4588K1496 BC++czy

 

代码来自小微哥:

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 #define M 500005
 7 
 8 struct point{
 9     int i, v;
10 }p[M];
11 struct node
12 {
13     int v;
14     int cur;
15 }s[M];
16 int has[M],  res[M];
17 
18 bool cmp (point a, point b)
19 {
20     return a.v > b.v;
21 }
22 int S()
23 {
24     int ret=0,ok=0;
25     char c;
26     while((c=getchar()))
27     {
28         if(c>='0'&&c<='9')
29         ret=(ret<<3)+ret+ret+c-'0',ok=1;
30         else if(ok)
31         return ret;
32     }
33     return ret;
34 }
35 bool cmp2(node a,node b)
36 {
37     return a.v<b.v;
38 }
39 int main()
40 {
41     int  n, d, i, j, ans;
42     while (~scanf("%d%d",&n,&d))
43     {
44         for (i = 0; i < n; i++)
45         {
46             p[i].v=S();
47             p[i].i = i+1;
48         }
49         sort(p, p+n, cmp);
50         for (j = 0; j < d; j++)
51         {
52             s[j].v=S();
53             s[j].cur=j;
54         }
55         sort(s,s+d,cmp2);
56         memset(has, 0, sizeof(has));
57         ans = 0;
58         for (i = 0, j = d - 1; j >= 0; j--)
59         {
60             for ( ; i < n; i++) 
61             {
62                 if (p[i].v <= s[j].v)
63                     break;
64                 int id = p[i].i;
65                 has[id] = 1;
66                 if (!has[id-1] && !has[id+1]) ++ans;
67                 else if (has[id-1] && has[id+1]) --ans;
68             }
69             res[s[j].cur] = ans;
70         }
71         for (i = 0; i < d; i++)
72         {
73             printf ("%d\n", res[i]);
74         }
75     }
76     return 0;
77 }

 

转载于:https://www.cnblogs.com/njczy2010/p/4392940.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值