大佬求助 !!! 思路一致但是全WA
查看原帖
大佬求助 !!! 思路一致但是全WA
884432
Y19833288907楼主2024/2/24 19:11
#include<bits/stdc++.h>
using namespace std;
typedef struct{
	long long high; //当前竹子的高度 
	int number; //编号 ,用于判断是否相邻 
}bamboo;
struct cmp{
	bool operator()(bamboo a,bamboo b){
		if(a.high==b.high) 
			return a.number<b.number;
		return a.high<b.high;
	}
};
void print(priority_queue<bamboo,vector<bamboo>,cmp> pq){
	while (!pq.empty()) {
    	bamboo e = pq.top();
    	pq.pop();
//    	cout<<e.high<<" ";
    	cout<<e.number<<" ";
	}
	cout<<endl;
}
int main(){
	int n; cin>>n;
	bamboo temp;
	priority_queue<bamboo,vector<bamboo>,cmp> pq;
	for(int i=0;i<n;++i){
		scanf("%lld",&temp.high);
		temp.number=i;
		if(temp.high>1) pq.push(temp); //如果本身竹子高度就小于1,不必压入队列 
	}
	
	long long ans=0;
	while(!pq.empty()){
//		print(pq);
		temp=pq.top();pq.pop();
		//如果队列里还有与本竹子高度一致且位置相邻的竹子,就合并成一个竹子 
		while(!pq.empty() && temp.high==pq.top().high && abs(temp.number-pq.top().number)==1){
			temp=pq.top();
			pq.pop();
		}
		//砍竹子 
		temp.high=sqrtl((temp.high/2)+1);
		if(temp.high>1) pq.push(temp);
		
		ans++;
	}
	cout<<ans;
	
	return 0;
}
2024/2/24 19:11
加载中...