小蒟蒻求助大佬
查看原帖
小蒟蒻求助大佬
543206
shiroko2008楼主2022/3/16 16:13

调了三个小时我要调疯了啊啊啊啊啊

#include<algorithm>
#include<iostream>
using namespace std;
struct node{
	int hei;
	int num;
	bool operator < (const node& b) {
		return this->hei<b.hei;
	}
	bool operator > (const node &b) {
		return (this->hei>b.hei);
	}
	bool operator == (const node& b) {
		return this->hei==b.hei;
	}
};
stack<node> s;
int n,tot,len;
node h[1000000];
int main()
{
	cin>>n;
	for (int i=0;i<n;i++) {
		cin>>h[i].hei;//[Error] invalid declarator before 's'
		h[i].num=i;
	}
	//for (int i=0;i<n;i++) cout<<h[i].num<<' '<<h[i].hei<<' ';
	//cout<<endl;
	for (int i=0;i<n;i++) {
		if (s.empty()) {
			s.push(h[i]);
			len++;
		}
		else {if (h[i]<s.top()||(h[i]==s.top())) {
			s.push(h[i]);
			len++;
		}
		else while ((!s.empty())&&(h[i]>s.top())) {
			node no=s.top();
			tot+=h[i].num-no.num;
			s.pop();
			len--;
			//cout<<"case"<<i<<": "<<' '<<len<<' '<<h[i].num<<' '<<no.num<<' '<<tot;
			//cout<<' ';
			//if (s.empty()) cout<<"kkk";
			//else if (h[i]>s.top()) cout<<"pinkrabbit";
			//cout<<endl;
		}
		//cout<<tot<<' ';
		}
	}
	node no=s.top();
	s.pop();
	while (!s.empty()) {
	    tot+=no.num-s.top().num;
	    s.pop();
	}
	cout<<tot;
	return 0;
}
2022/3/16 16:13
加载中...