Poj3250-monotonous Stack

Source: Internet
Author: User
Bad hair day
Time limit:2000 ms   Memory limit:65536 K
Total submissions:10988   Accepted:3705

Description

Some Of Farmer John'sNCows (1 ≤N≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows 'heads.

Each cowIHas a specified heightHi(1 ≤Hi≤ 1,000,000,000) and is standing in a line of cows all facing east (to the right in our diagrams). Therefore, cowICan see the tops of the heads of cows
In front of her (namely cowsI+ 1,I+ 2, and so on), for as long as these cows are strictly shorter than cowI.

Consider this example:

        ==       ==   -   =         Cows facing right -->=   =   == - = = == = = = = =1 2 3 4 5 6 

Cow #1 can see the hairstyle of cows #2, 3, 4
Cow #2 can see no cow's hairstyle
Cow #3 can see the hairstyle of cow #4
Cow #4 can see no cow's hairstyle
Cow #5 can see the hairstyle of cow 6
Cow #6 can see no cows at all!

LetCiDenote the number of cows whose hairstyle is visible from cowI; Please compute the sumC1 throughCN. For this example, the desired is answer 3 + 0 + 1 + 0 + 1 + 0 = 5.

Input

Line 1: The number of cows, N.
Lines 2. n + 1: Line I+ 1 contains a single integer that is the height of cow I.

Output

Line 1: A single integer that is the sum C1 through CN.

Sample Input

610374122

Sample output

5

The question is quite understandable, that is, the number of cows on the right shorter than the left

At first, I wrote a dual loop and timed out.

#include <iostream>#define MAXN 80000int N,height[MAXN+2];int main(){using namespace std;while(cin>>N){for(int i=1;i<=N;++i)cin>>height[i];int ans=0;for(int i=1;i<=N;++i)for(int j=i+1;j<=N;++j)if(height[j]

Then I found this question only by checking the stack information, so I naturally wrote it using the stack. After thinking carefully, I came up with a stack simulation. The idea is as follows:

Traverse the height array from the back to the front. If the index is I, the height is pushed to the stack. If the height at the top of the stack is smaller than the height of the current I, the stack is always pushed out, until the stack is empty or the height of the stack top is relatively small, the number of outgoing Stacks is the number of cows that I can see at this time, and save the number with vist. Next time, if Niu I is pushed out of the stack, the number of cows that the other ox sees must be added to the number of cows that I sees. The Code is as follows:

#include <iostream>#include <stack>#include <cstring>#define MAXN 80000long long N,height[MAXN+2];long long vist[MAXN+2];int main(){using namespace std;cin>>N;for(long long i=1;i<=N;++i)cin>>height[i];long long ans=0;  memset(vist,0,sizeof(vist));stack<long long>s;s.push(height[N]);for(int i=N-1;i>=1;--i){if(!s.empty() && s.top()>=height[i])s.push(height[i]);else{long long temp=0;while (!s.empty() && s.top()

I tested all the test data, and the test was correct. The result was always re. I thought about it carefully. Why was that cow so high? No wonder re, my vist array has a maximum value of 80000. As a result, the size of the vist array is far greater than that of my array. In this case, I have to change the method.

Then, I naturally looked at other people's code. It turns out that others are traversing in order (my previous thought was backward ). Of course, this is not the key. The key is to replace the vist with the number stored in the stack, so sequential traversal is required. Therefore, the code is modified as follows:

11360988 Tserrof 3250 Accepted 2648 K 1204 Ms C ++ 529b 2013-03-17 18:56:51
#include <iostream>#include <stack>#include <cstring>#define MAXN 80000long long N,height[MAXN+2];int main(){using namespace std;while(cin>>N){for(long long i=1;i<=N;++i)cin>>height[i];long long ans=0;  stack<long long>s;s.push(height[1]);for(int i=2;i<=N;++i){while(!s.empty() && s.top()<=height[i])s.pop();if((!s.empty() && s.top()>height[i]) || s.empty()){ans+=s.size();s.push(height[i]);}}cout<<ans<<endl;}return 0;}

If you use your own stack

11361036 Tserrof 3250 Accepted 1488 K 688 Ms C ++ 1578b 2013-03-17 19:03:22

However, I made two contributions to WA. First, I didn't write the size function. I opened a small space and changed maxsize.

#include <iostream>//#include <stack>#include <cstring>#define MAXN 80000long long N,height[MAXN+2];#define MAXSIZE 90000template<typename T>class stack{private:T *STACK;long long TOP;public:stack();~stack();bool pop();bool push(T);T top();bool empty();void show(bool);long long size();};template<typename T> stack<T>::stack(){STACK=new T[MAXSIZE];TOP=-1;}template<typename T> stack<T>::~stack(){delete STACK;}template<typename T> bool stack<T>::pop( ){if(TOP==-1)return false;--TOP;return true;}template<typename T> bool  stack<T>::push(T d){if(TOP==MAXSIZE-1)return false;STACK[++TOP]=d;return true;}template<typename T>T  stack<T>::top(){return STACK[TOP];}template<typename T> bool stack<T>::empty(){if(TOP==-1)return true;return false;}template<typename T> void  stack<T>::show(bool reverse){if(reverse){for(int i=TOP;i>=0;--i)std::cout<<STACK[i]<<" ";}else{for(int i=0;i<=TOP;++i)std::cout<<STACK[i]<<" ";}}template<typename T>long long stack<T>::size(){return TOP+1;}int main(){using namespace std;while(cin>>N){for(long long i=1;i<=N;++i)cin>>height[i];long long ans=0;  stack<long long>s;s.push(height[1]);for(int i=2;i<=N;++i){while(!s.empty() && s.top()<=height[i])s.pop();if((!s.empty() && s.top()>height[i]) || s.empty()){ans+=s.size();s.push(height[i]);}}cout<<ans<<endl;}return 0;}

In fact, there is no need to use long. After changing it to int, the space is reduced a lot.

11361054 Tserrof 3250 Accepted 904 K 688 Ms C ++ 1542b 2013-03-17 19:05:52
#include <iostream>//#include <stack>#include <cstring>#define MAXN 80000int N,height[MAXN+2];#define MAXSIZE 90000template<typename T>class stack{private:T *STACK;int TOP;public:stack();~stack();bool pop();bool push(T);T top();bool empty();void show(bool);int size();};template<typename T> stack<T>::stack(){STACK=new T[MAXSIZE];TOP=-1;}template<typename T> stack<T>::~stack(){delete STACK;}template<typename T> bool stack<T>::pop( ){if(TOP==-1)return false;--TOP;return true;}template<typename T> bool  stack<T>::push(T d){if(TOP==MAXSIZE-1)return false;STACK[++TOP]=d;return true;}template<typename T>T  stack<T>::top(){return STACK[TOP];}template<typename T> bool stack<T>::empty(){if(TOP==-1)return true;return false;}template<typename T> void  stack<T>::show(bool reverse){if(reverse){for(int i=TOP;i>=0;--i)std::cout<<STACK[i]<<" ";}else{for(int i=0;i<=TOP;++i)std::cout<<STACK[i]<<" ";}}template<typename T>int stack<T>::size(){return TOP+1;}int main(){using namespace std;while(cin>>N){for(int i=1;i<=N;++i)cin>>height[i];long long ans=0;  stack<int>s;s.push(height[1]);for(int i=2;i<=N;++i){while(!s.empty() && s.top()<=height[i])s.pop();if((!s.empty() && s.top()>height[i]) || s.empty()){ans+=s.size();s.push(height[i]);}}cout<<ans<<endl;}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.