P1823 concert waiting, P1823 concert waiting

Source: Internet
Author: User

P1823 concert waiting, P1823 concert waiting
Description

N people are queuing for a concert. People were bored, so they began to turn around and wanted to find their acquaintances in the team. Any two people in the queue, A and B, can see each other if they are adjacent or no one is higher than A or B.

Write a program to calculate how many people can see each other.

Input/Output Format Input Format:

The first line contains an integer N (1 ≤ N ≤ 500 000), indicating that there are N people in the team.

In the next N rows, each line contains an integer indicating the height of a person. In the unit of 10-9 power meters, each person schedules less than 2 ^ 31 m. These heights indicate the heights of the people in the team.

Output Format:

The output contains only one row, containing a number of S, indicating that the total number of S in the team can be seen by people.

Input and Output sample Input example #1:
7 2 4 1 2 2 5 1
Output sample #1:
10
Description

Data production: @ w

This question was first introduced by myself. If there is something bigger than him later, it would be useless,

Then I made 25 points in a mess...

After reading the problem, I found a very good idea.

Is to put the equal and greater values in a loop to judge,

At the beginning, num = 1 ensures that data is read.

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <stack> 6 using namespace std; 7 int read (int & n) 8 {9 char c = '-'; int x = 0; 10 while (c <'0' | c> '9 ') c = getchar (); 11 while (c> = '0' & c <= '9') 12 {13 x = x * 10 + (c-48 ); 14 c = getchar (); 15} 16 n = x; 17} 18 const int MAXN = 500001; 19 stack <int> s; 20 int main () 21 {22 int n, h, ans = 0, flag = 0; 23 read (n); 24 for (int I = 1; I <= n; I ++) 25 {26 flag = 0; 27 read (h); 28 if (I = 1) 29 {30 s. push (h); 31 continue; 32} 33 if (h> s. top () 34 {35 ans ++; 36 s. pop (); 37 while (s. size ()> 0 & h> s. top () 38 {39 ans ++; 40 s. pop (); 41 flag = 1; 42} 43 if (s. size ()! = 0 & h <s. top () 44 ans ++; 45 s. push (h); 46} 47 else if (h = s. top () 48 {49 ans = ans + s. size (); 50 s. push (h); 51} 52 else53 {54 ans ++; 55 s. push (h); 56} 57} 58 printf ("% d", ans); 59}Messing around for 25 minutes
 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<stack> 6 using namespace std; 7 int read(int & n) 8 { 9     char c='-';int x=0;10     while(c<'0'||c>'9')c=getchar();11     while(c>='0'&&c<='9')12     {13         x=x*10+(c-48);14         c=getchar();15     }16     n=x;17 }18 const int MAXN=500001;19 stack<int>s;20 int main()21 {22     int n,h,ans=0,flag=0;23     read(n);24     for(int i=1;i<=n;i++)25     {26         int num=1;27         read(h);28         while(s.size()!=0&&h>=s.top())29         {30         31             if(h==s.top())32             num++;33             ans++;34             s.pop();35         }    36         if(s.size()!=0)37         ans++;38         while(num--)39         s.push(h);40     }41     printf("%d",ans);42 }

 

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.