二分(hdu5200)

Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 537    Accepted Submission(s): 177


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]

1 \leq N, Q \leq 50000

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.
 

Source
 

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
int n, m, a[100010], cnt;
struct node
{
    int v, id;
    node() {}
    node(int a,int b):v(a),id(b)
    {}
} c[100010];
int d[50010], ans[100010],cur=1,bin[50010];
bool cmp (const node &a, const node &b)
{
    return a.v < b.v;
}

int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        memset(d,0,sizeof(d));
        for(int i = 1; i <= n; i ++) scanf("%d", &a[i]), bin[i] = a[i], d[i]=1;
        sort(bin + 1, bin + n + 1);
        cnt = unique(bin + 1, bin + n + 1) - bin - 1;
        for(int i = 1; i <= n; i ++) a[i] = lower_bound(bin + 1, bin + cnt + 1, a[i])-bin;
        for(int i = 1; i <= n; i ++) c[i] = node(a[i],i);
        sort(c + 1,c + n + 1,cmp);
        cur=1;
        int j=1;
        ans[0]=1;
        for(int i=1; i<=cnt; i++)
        {
            while(j<=n&&c[j].v==i)
            {
                int p=c[j].id;
                if(!d[p-1] && !d[p+1]) cur--;
                else if(d[p-1] &&d[p+1])cur++;
                j++;
                d[p]=0;
            }
            ans[i]=cur;
        }
        int x;
        while(m--)
        {
            scanf("%d", &x);
            int p = upper_bound(bin +1, bin+cnt+1,x)-bin-1;
            printf("%d\n", ans[p]);
        }

    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值