请问各位神犇我这个为什么WA了。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
inline ll fun(const ll& h) {
return sqrt(h / 2 + 1);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
ll* arr = new ll[n];
int ans = 0;
for (ll i = 0; i != n; ++i) cin >> arr[i];
while (1) {
ll max = *max_element(arr, arr + n);
if (max == 1) break;
int j = 1;
for (int i = 0; i != n; ++i) {
if (arr[i] == max) {
if (arr[i] == 1) continue;
arr[i] = fun(arr[i]);
while (i + j < n && arr[i + j] == max) {
arr[i + j] = fun(arr[i + j]);
++j;
}
++ans;
}
}
}
cout << ans << '\n';
return 0;
}
应该是数据规模的问题,当数组下标超过一百万时,有可能会出错,所以试试用队列priority_queue,看看这个http://t.csdn.cn/27ziN
然后应该就差不多了,(但我只拿了70分)
#include <bits/stdc++.h>
using namespace std;
long long h,hnew,ans,n,rnk;
priority_queue<pair<long long,int>,vector<pair<long long,int>>,less<pair<long long,int>>>q;
int main(){
cin.tie(0),cout.tie(0);
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++){
cin>>h;
if(h!=1)q.push({h,i});//高度为1就不用砍了
}
while(!q.empty()){
h=q.top().first,rnk=q.top().second;
q.pop();
hnew= sqrt(h/2+1);
if(hnew!=1)q.push({hnew,rnk});
while(!q.empty()&&q.top().first==h&&q.top().second==rnk-1){
rnk--;
q.pop();
if(hnew!=1)
q.push({hnew,rnk});
}
ans++;
}
cout<<ans;
return 0;
}
望采纳
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
void fun(int& h) {
h = sqrt(h / 2 + 1);
}
int main() {
int n;
cin >> n;
int *arr=new int[n];
int ans = 0;
for (int i = 0; i != n; ++i) cin >> arr[i];
while (1) {
int max = *max_element(arr, arr + n);
if (max == 1)break;
int j = 1;
for (int i = 0; i != n; ++i) {
if (arr[i] == max) {
fun(arr[i]);
while (i + j < n && arr[i + j] == max) {
fun(arr[i + j]);
++j;
}
++ans;
}
}
}
cout << ans << '\n';
delete arr;
return 0;
}