HDU P5200 Trees

本文探讨了一道关于树木砍伐的问题,通过分析树木排列及其高度特性,利用贪心算法解决如何计算砍伐特定高度以下树木后形成的树堆数量。介绍了问题背景、输入输出格式、示例解释以及算法实现思路。

Trees

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


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.


BC#36的第三道题,官方做法是线段树,但是其实很多人和我一样用了贪心完成了这题。

题意为n颗树,q条指令,指令为斩掉长度为q[i]及以下的树枝,问我们还剩几个树堆。所谓树堆其实就是挨在一块的树群。

可以想到,一开始是一个树堆,怎么样会增加树堆呢?当一个底谷树被砍掉的时候增加。高峰被砍掉的必减少一个树堆。所以我们可以先把数列中的底谷和高峰排序,并把指令也排序,根据指令来一步步砍掉从矮到高的底谷或高峰,答案就出来了,并且时间复杂度也允许。不过注意一下,当数列中出现3 2 2 2 3即底谷或高峰出现连续相同数字的时候,只能计一个底谷或高峰。


代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<algorithm>
#include<fstream>
#include<queue>
#include<stack> 
#include<vector>
#include<cmath>
#include<iomanip>
#define rep(i,n) for(i=1;i<=n;i++)
#define MM(a,t) memset(a,t,sizeof(a))
#define INF 1e9
typedef long long ll;
#define mod 1000000007
using namespace std;
class node{
  public:
    int op;
	int num;	
}a[100020];
int m[100020];
class qu{
  public:
    int ind;
	int v;	
}q[100020];
int n,m2,n2; 
int res[100020];
bool cmp(node a,node b){
  return a.num<b.num;	
}
bool cmp2(qu a,qu b){
  return a.v<b.v;	
}
int main()
{
	int i,j,cnt;

    while(scanf("%d%d",&n,&m2)!=EOF){
      rep(i,n) scanf("%d",&m[i]);
	  m[0]=-1; m[n+1]=-1;
      i=1; n2=0;
	  while(i<=n){
  	    int pr=i-1,now=m[i];
		while(i<=n && m[i+1]==now) i++;
		if(m[pr]>now && m[i+1]>now){
		  n2++;
		  a[n2].op=0;
		  a[n2].num=now;	
		}  	
		if(m[pr]<now && m[i+1]<now){
		  n2++;
		  a[n2].op=1;
		  a[n2].num=now;	
		}
		i++;
  	  } 	
  	  sort(a+1,a+n2+1,cmp);
  	  rep(i,m2){
  	    q[i].ind=i;
		scanf("%d",&q[i].v);	
  	  }
  	  sort(q+1,q+m2+1,cmp2);
  	  cnt=1; j=1;
  	  rep(i,m2){
  	    int now=q[i].v;
		while(j<=n2 && now>=a[j].num){
		  if(a[j].op==1) cnt--;
		  else           cnt++;
		  j++;	
		}	
		res[q[i].ind]=cnt;
  	  }
  	  rep(i,m2) printf("%d\n",res[i]);
    }
	
	return 0;
}


先看效果: https://renmaiwang.cn/s/jkhfz Hue系列产品将具备高度的个性化定制能力,并且借助内置红、蓝、绿三原色LED的灯泡,能够混合生成1600万种不同色彩的灯光。 整个操作流程完全由安装于iPhone上的应用程序进行管理。 这一创新举措为智能照明控制领域带来了新的启示,国内相关领域的从业者也积极投身于相关研究。 鉴于Hue产品采用WiFi无线连接方式,而国内WiFi网络尚未全面覆盖,本研究选择应用更为普及的蓝牙技术,通过手机蓝牙与单片机进行数据交互,进而产生可调节占空比的PWM信号,以此来控制LED驱动电路,实现LED的调光功能以及DIY调色方案。 本文重点阐述了一种基于手机蓝牙通信的LED灯设计方案,该方案受到飞利浦Hue智能灯泡的启发,但考虑到国内WiFi网络的覆盖限制,故而选用更为通用的蓝牙技术。 以下为相关技术细节的详尽介绍:1. **智能照明控制系统**:智能照明控制系统允许用户借助手机应用程序实现远程控制照明设备,提供个性化的调光及色彩调整功能。 飞利浦Hue作为行业领先者,通过红、蓝、绿三原色LED的混合,能够呈现1600万种颜色,实现了全面的定制化体验。 2. **蓝牙通信技术**:蓝牙技术是一种低成本、短距离的无线传输方案,工作于2.4GHz ISM频段,具备即插即用和强抗干扰能力。 蓝牙协议栈由硬件层和软件层构成,提供通用访问Profile、服务发现应用Profile以及串口Profiles等丰富功能,确保不同设备间的良好互操作性。 3. **脉冲宽度调制调光**:脉冲宽度调制(PWM)是一种高效能的调光方式,通过调节脉冲宽度来控制LED的亮度。 当PWM频率超过200Hz时,人眼无法察觉明显的闪烁现象。 占空比指的...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值