砍竹子题解(思维+暴力)

题目描述

题目思路

其实就是找到最大的数然后把他周围相同的点和他一起操作

每个点最多操作\(6\)次,那么时间复杂度为\(nlogn\)

代码

#include<cstdio>
#include<map>
#include<set>
#include<iostream>
#include<algorithm>
#include<cstring>
#define fi first
#define se second
//typedef pair<int,int>
typedef long long ll;
const int mod=1000000007,maxn=2e5+5;
using namespace std;
int n;
int a[maxn];
set<pair<ll,ll> > se;
ll h[maxn],x[maxn];
void change(int l,int r){
    for(int i=l;i<=r;i++){
        se.erase({-h[i],i});
        h[i]=sqrt(h[i]/2+1);
        se.insert({-h[i],i});
    }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%lld",&h[i]);
        se.insert({-h[i],i});
    }
    for(int i=1;;i++){
        pair<ll,ll> tmp=*se.begin();
        if(tmp.fi==-1){
            printf("%d\n",i-1);
            return 0;
        }
        int l,r,pos=tmp.se;
        l=r=pos;
        while(l-1>=1&&h[l-1]==h[pos]) l--;
        while(r+1<=n&&h[r+1]==h[pos]) r++;
        change(l,r);
    }
    return 0;
}
posted @ 2022-04-09 18:23  hunxuewangzi  阅读(57)  评论(0编辑  收藏  举报