http://acm.hdu.edu.cn/showproblem.php?pid=5200
Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1285 Accepted Submission(s): 400
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?
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]
1≤N,Q≤50000
0≤h[i]≤1000000000(109)
0≤q[i]≤1000000000(109)
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)
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 2HintIn 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.
Source
Recommend
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define N 50005
int n,q;
struct data
{
int h, num;
}h[N];
bool cmp(data a,data b)
{
return a.h>b.h;
}
int vis[N],ans[N];
int help(int x)
{
int le=1,ri=n;
int mid=(le+ri)/2;
while(le<=ri)
{
mid=(le+ri)/2;
if(x<h[mid].h)
{
le=mid+1;
}
else
ri=mid-1;
}
return le-1;
}
int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&h[i].h);
h[i].num=i;
}
sort(h+1,h+1+n,cmp);
memset(vis,0,sizeof vis);
int sum=0;
for(int i=1;i<=n;i++)
{
int num=h[i].num;
if(vis[num-1]==0&&vis[num+1]==0)
sum++;
else if(vis[num-1]!=0&&vis[num+1]!=0)
sum--;
ans[i]=sum;
vis[num]=1;
}
int x;
for(int i=1;i<=q;i++)
{
scanf("%d",&x);
int pos=help(x);
// printf("pos:%d h[pos].h:%d\n",pos,h[pos].h);
printf("%d\n",ans[pos]);
}
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define N 50005
int n,q;
struct data
{
int h, num;
}h[N];
bool cmp(data a,data b)
{
return a.h>b.h;
}
int vis[N],ans[N];
int help(int x)
{
int le=1,ri=n;
int mid=(le+ri)/2;
while(le<=ri)
{
mid=(le+ri)/2;
if(x<h[mid].h)
{
le=mid+1;
}
else
ri=mid-1;
}
return le-1;
}
int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&h[i].h);
h[i].num=i;
}
sort(h+1,h+1+n,cmp);
memset(vis,0,sizeof vis);
int sum=0;
for(int i=1;i<=n;i++)
{
int num=h[i].num;
if(vis[num-1]==0&&vis[num+1]==0)
sum++;
else if(vis[num-1]!=0&&vis[num+1]!=0)
sum--;
ans[i]=sum;
vis[num]=1;
}
int x;
for(int i=1;i<=q;i++)
{
scanf("%d",&x);
int pos=help(x);
// printf("pos:%d h[pos].h:%d\n",pos,h[pos].h);
printf("%d\n",ans[pos]);
}
}
return 0;
}

本文介绍了一个关于树木切割的问题,通过设定特定的规则来确定被切割树木间的连接方式,并提出了一种算法来解决如何计算切割后剩余树木形成的区块数量。该问题涉及到算法设计与实现,特别是搜索算法的应用。

509

被折叠的 条评论
为什么被折叠?



