hdu 5200 Trees(离线处理,排序)
来源:互联网 发布:java获取file的后缀名 编辑:程序博客网 时间:2024/03/29 10:13
Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1383 Accepted Submission(s): 447
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 numberedz , 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
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 integersN and Q separated by exactly one space, N indicates there are N trees, Q indicates there are Q queries.
In the followingN lines, there will appear h[1],h[2],h[3],…,h[N] which indicates the height of the trees.
In the followingQ 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
In the following
In the following
Please process to the end of file.
[Technical Specification]
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 252362
Sample Output
02HintIn 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.
每个询问给出一个x,输出把高度<=x的树都砍掉之后剩下几个块
思路:因为询问后项可以由前项继续推导获得,所以可以考虑用离线。
把所有的树的高度和询问都保存下来,排序后进行处理即可
过程有点繁琐,要耐心。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 50050struct Node{ int v,id;} p[N],a[N];int flag[N];int ans[N];bool cmp(Node a,Node b){ return a.v<b.v;}int main(){ int n,q; while(~scanf("%d %d",&n,&q)) { memset(flag,0,sizeof(flag)); for(int i=1; i<=n; i++) { scanf("%d",&a[i].v); if(!a[i].v) flag[i]=1; a[i].id=i; } for(int i=1; i<=q; i++) { scanf("%d",&p[i].v); p[i].id=i; } a[0].v=0; int cnt=1,num=0; for(int i=1; i<=n; i++) if(!a[i-1].v&&a[i].v>0) num++; sort(a+1,a+1+n,cmp); sort(p+1,p+1+q,cmp); flag[0]=flag[n+1]=1; for(int i=1; i<=q; i++) { if(p[i].v) { while(cnt<=n&&a[cnt].v<=p[i].v) { int id=a[cnt++].id; if(flag[id]) continue; flag[id]=1; if(!flag[id-1]&&!flag[id+1]) num++; if(flag[id-1]&&flag[id+1]) num--; } } ans[p[i].id]=num; } for(int i=1; i<=q; i++) printf("%d\n",ans[i]); } return 0;}
0 0
- hdu 5200 Trees(离线处理,排序)
- hdu 5200 trees(离线处理)
- Hdu 5200 Trees (离线线段树)
- HDU 5200 - Trees (离线 + 思维)
- HDU 5200 Trees 离线+链表维护.
- hdu5200 Trees(逆向思维+离线处理)
- HDU5200 Trees (离线处理)
- hdu 5200(离线处理)
- hdu 5200 离线处理+思维
- HDU 5200 Trees(线段树 离线应用)经典
- BestCoder Round #36(Trees-离线处理询问)
- HDU - 5200 - Trees (upper_bound)
- hdu 5057(树状数组+离线处理)
- [ACM] HDU 5139 Formula (离线处理)
- HDU--3333(树状数组+离线处理)
- hdu 5139 Formula(离线处理)
- hdu 5139 Formula(离线处理)
- HDU 5139 Formula(数据离线处理)
- 使用Xshell连接Ubuntu
- 写在前面
- 深度学习常用框架介绍
- html5
- 2015CCPC南阳场 D - Pick The Sticks
- hdu 5200 Trees(离线处理,排序)
- 字符串匹配各种算法
- laravel的启动过程
- 小白学Tensorflow之多层神经网络
- Ideal+spring boot+thymeleaf 的热部署问题处理
- 工厂模式:简单工厂模式、工厂方法模式、抽象工厂模式
- CodeForces 289B Polo the Penguin and Matrix
- SDKD 2016 Summer Single Contest #12 .F
- 文本居中