FIRS - Hàng cây

Tác giả: skyvn97

Ngôn ngữ: C++

#include<stdio.h>
#include<queue>
#define MAX   100100
using namespace std;
typedef pair<int,int> ii;
bool b[MAX];
int n,c;
priority_queue<ii,vector<ii>,greater<ii> > q;
void init(void) {
	int i,x;
	scanf("%d",&n);
	for (i=1;i<=n;i=i+1) {
		scanf("%d",&x);
		q.push(ii(x,i));
		b[i]=true;
	}
}
void process(void) {
	c=0;
	ii x;
	int i;
	while (!q.empty()) {
		while ((!q.empty()) && (!b[q.top().second])) q.pop();
		if (!q.empty()) {
			x=q.top();
			q.pop();
			c=c+1;
			for (i=-1;i<=1;i=i+1) b[x.second+i]=false;
		}
	}
	printf("%d",c);
}
int main(void) {
	init();
	process();
}

Download