SlideShare ist ein Scribd-Unternehmen logo
1 von 2
Downloaden Sie, um offline zu lesen
Code in c++ for largest rectangle in histogram
Solution
#include
#define ll long long
#define N_lim 100010
ll max(ll a, ll b)
{
return a>b?a:b;
}
ll largestArea(int h[], int n)
{
ll i, top = 1, area = 0, S[N_lim];
h[0] = S[0] = h[++n] = 0;
for(i=1; i<=n; i++)
{
while(top>1 && h[S[top-1]]>=h[i])
{
area = max(area, (ll)h[S[top-1]]*(ll)(i - S[top-2]-1));
top--;
}
S[top++] = i;
}
return area;
}
int main()
{
int n, i, h[N_lim];
while(scanf("%d", &n)==1 && n)
{
for(i=1; i<=n; i++) scanf("%d", &h[i]);
printf("%lld ", largestArea(h, n));
}
return 0;
}

Weitere ähnliche Inhalte

Mehr von aishwaryaequipment

completely forgot how to do these limits, could i have a extremely d.pdf
completely forgot how to do these limits, could i have a extremely d.pdfcompletely forgot how to do these limits, could i have a extremely d.pdf
completely forgot how to do these limits, could i have a extremely d.pdfaishwaryaequipment
 
Complex logarithmDescribe log z.SolutionThe complex logarith.pdf
Complex logarithmDescribe log z.SolutionThe complex logarith.pdfComplex logarithmDescribe log z.SolutionThe complex logarith.pdf
Complex logarithmDescribe log z.SolutionThe complex logarith.pdfaishwaryaequipment
 
Complete the Income Statement.What was the company’s interest expe.pdf
Complete the Income Statement.What was the company’s interest expe.pdfComplete the Income Statement.What was the company’s interest expe.pdf
Complete the Income Statement.What was the company’s interest expe.pdfaishwaryaequipment
 
College Algebra Can anyone help me If $2500 is invested in the ac.pdf
College Algebra Can anyone help me If $2500 is invested in the ac.pdfCollege Algebra Can anyone help me If $2500 is invested in the ac.pdf
College Algebra Can anyone help me If $2500 is invested in the ac.pdfaishwaryaequipment
 
Complete by hand. Show all work. 14. Information Gathering and E.pdf
Complete by hand. Show all work. 14. Information Gathering and E.pdfComplete by hand. Show all work. 14. Information Gathering and E.pdf
Complete by hand. Show all work. 14. Information Gathering and E.pdfaishwaryaequipment
 
Compensation plans usually include a variety of benefits. Name the t.pdf
Compensation plans usually include a variety of benefits. Name the t.pdfCompensation plans usually include a variety of benefits. Name the t.pdf
Compensation plans usually include a variety of benefits. Name the t.pdfaishwaryaequipment
 
Comparecontrast economies of scale and diseconomies of scale.So.pdf
Comparecontrast economies of scale and diseconomies of scale.So.pdfComparecontrast economies of scale and diseconomies of scale.So.pdf
Comparecontrast economies of scale and diseconomies of scale.So.pdfaishwaryaequipment
 
Comparecontrast explicit and implicit costs.SolutionExplicit .pdf
Comparecontrast explicit and implicit costs.SolutionExplicit .pdfComparecontrast explicit and implicit costs.SolutionExplicit .pdf
Comparecontrast explicit and implicit costs.SolutionExplicit .pdfaishwaryaequipment
 
Compare the Euro downfall with the other major currencies of the wor.pdf
Compare the Euro downfall with the other major currencies of the wor.pdfCompare the Euro downfall with the other major currencies of the wor.pdf
Compare the Euro downfall with the other major currencies of the wor.pdfaishwaryaequipment
 
Compare the difference between using the RTI interrupt to control th.pdf
Compare the difference between using the RTI interrupt to control th.pdfCompare the difference between using the RTI interrupt to control th.pdf
Compare the difference between using the RTI interrupt to control th.pdfaishwaryaequipment
 
compare and contrast the change interventions (first and second orde.pdf
compare and contrast the change interventions (first and second orde.pdfcompare and contrast the change interventions (first and second orde.pdf
compare and contrast the change interventions (first and second orde.pdfaishwaryaequipment
 
Compare and contrast the national cultures of the U.S. and India usi.pdf
Compare and contrast the national cultures of the U.S. and India usi.pdfCompare and contrast the national cultures of the U.S. and India usi.pdf
Compare and contrast the national cultures of the U.S. and India usi.pdfaishwaryaequipment
 
COMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdf
COMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdfCOMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdf
COMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdfaishwaryaequipment
 
Compare and contrast the following items, pointing out their key ele.pdf
Compare and contrast the following items, pointing out their key ele.pdfCompare and contrast the following items, pointing out their key ele.pdf
Compare and contrast the following items, pointing out their key ele.pdfaishwaryaequipment
 
Compare and contrast the analysis of variance (ANOVA) with t-test..pdf
Compare and contrast the analysis of variance (ANOVA) with t-test..pdfCompare and contrast the analysis of variance (ANOVA) with t-test..pdf
Compare and contrast the analysis of variance (ANOVA) with t-test..pdfaishwaryaequipment
 
Compare and contrast the theories of liability for insider trading u.pdf
Compare and contrast the theories of liability for insider trading u.pdfCompare and contrast the theories of liability for insider trading u.pdf
Compare and contrast the theories of liability for insider trading u.pdfaishwaryaequipment
 
Compare and contrast the analysis of variance (ANOVA) with a t test.pdf
Compare and contrast the analysis of variance (ANOVA) with a t test.pdfCompare and contrast the analysis of variance (ANOVA) with a t test.pdf
Compare and contrast the analysis of variance (ANOVA) with a t test.pdfaishwaryaequipment
 
Compare and contrast the development of a WBS in traditional project.pdf
Compare and contrast the development of a WBS in traditional project.pdfCompare and contrast the development of a WBS in traditional project.pdf
Compare and contrast the development of a WBS in traditional project.pdfaishwaryaequipment
 
Compare and contrast style and tone in management communicationS.pdf
Compare and contrast style and tone in management communicationS.pdfCompare and contrast style and tone in management communicationS.pdf
Compare and contrast style and tone in management communicationS.pdfaishwaryaequipment
 
Compare and contrast the analysis of variance (ANOVA) with a t test..pdf
Compare and contrast the analysis of variance (ANOVA) with a t test..pdfCompare and contrast the analysis of variance (ANOVA) with a t test..pdf
Compare and contrast the analysis of variance (ANOVA) with a t test..pdfaishwaryaequipment
 

Mehr von aishwaryaequipment (20)

completely forgot how to do these limits, could i have a extremely d.pdf
completely forgot how to do these limits, could i have a extremely d.pdfcompletely forgot how to do these limits, could i have a extremely d.pdf
completely forgot how to do these limits, could i have a extremely d.pdf
 
Complex logarithmDescribe log z.SolutionThe complex logarith.pdf
Complex logarithmDescribe log z.SolutionThe complex logarith.pdfComplex logarithmDescribe log z.SolutionThe complex logarith.pdf
Complex logarithmDescribe log z.SolutionThe complex logarith.pdf
 
Complete the Income Statement.What was the company’s interest expe.pdf
Complete the Income Statement.What was the company’s interest expe.pdfComplete the Income Statement.What was the company’s interest expe.pdf
Complete the Income Statement.What was the company’s interest expe.pdf
 
College Algebra Can anyone help me If $2500 is invested in the ac.pdf
College Algebra Can anyone help me If $2500 is invested in the ac.pdfCollege Algebra Can anyone help me If $2500 is invested in the ac.pdf
College Algebra Can anyone help me If $2500 is invested in the ac.pdf
 
Complete by hand. Show all work. 14. Information Gathering and E.pdf
Complete by hand. Show all work. 14. Information Gathering and E.pdfComplete by hand. Show all work. 14. Information Gathering and E.pdf
Complete by hand. Show all work. 14. Information Gathering and E.pdf
 
Compensation plans usually include a variety of benefits. Name the t.pdf
Compensation plans usually include a variety of benefits. Name the t.pdfCompensation plans usually include a variety of benefits. Name the t.pdf
Compensation plans usually include a variety of benefits. Name the t.pdf
 
Comparecontrast economies of scale and diseconomies of scale.So.pdf
Comparecontrast economies of scale and diseconomies of scale.So.pdfComparecontrast economies of scale and diseconomies of scale.So.pdf
Comparecontrast economies of scale and diseconomies of scale.So.pdf
 
Comparecontrast explicit and implicit costs.SolutionExplicit .pdf
Comparecontrast explicit and implicit costs.SolutionExplicit .pdfComparecontrast explicit and implicit costs.SolutionExplicit .pdf
Comparecontrast explicit and implicit costs.SolutionExplicit .pdf
 
Compare the Euro downfall with the other major currencies of the wor.pdf
Compare the Euro downfall with the other major currencies of the wor.pdfCompare the Euro downfall with the other major currencies of the wor.pdf
Compare the Euro downfall with the other major currencies of the wor.pdf
 
Compare the difference between using the RTI interrupt to control th.pdf
Compare the difference between using the RTI interrupt to control th.pdfCompare the difference between using the RTI interrupt to control th.pdf
Compare the difference between using the RTI interrupt to control th.pdf
 
compare and contrast the change interventions (first and second orde.pdf
compare and contrast the change interventions (first and second orde.pdfcompare and contrast the change interventions (first and second orde.pdf
compare and contrast the change interventions (first and second orde.pdf
 
Compare and contrast the national cultures of the U.S. and India usi.pdf
Compare and contrast the national cultures of the U.S. and India usi.pdfCompare and contrast the national cultures of the U.S. and India usi.pdf
Compare and contrast the national cultures of the U.S. and India usi.pdf
 
COMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdf
COMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdfCOMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdf
COMPARE AND CONTRAST TWO MAIN DIFFERENCES BETWEEN DOMESTIC AND INTER.pdf
 
Compare and contrast the following items, pointing out their key ele.pdf
Compare and contrast the following items, pointing out their key ele.pdfCompare and contrast the following items, pointing out their key ele.pdf
Compare and contrast the following items, pointing out their key ele.pdf
 
Compare and contrast the analysis of variance (ANOVA) with t-test..pdf
Compare and contrast the analysis of variance (ANOVA) with t-test..pdfCompare and contrast the analysis of variance (ANOVA) with t-test..pdf
Compare and contrast the analysis of variance (ANOVA) with t-test..pdf
 
Compare and contrast the theories of liability for insider trading u.pdf
Compare and contrast the theories of liability for insider trading u.pdfCompare and contrast the theories of liability for insider trading u.pdf
Compare and contrast the theories of liability for insider trading u.pdf
 
Compare and contrast the analysis of variance (ANOVA) with a t test.pdf
Compare and contrast the analysis of variance (ANOVA) with a t test.pdfCompare and contrast the analysis of variance (ANOVA) with a t test.pdf
Compare and contrast the analysis of variance (ANOVA) with a t test.pdf
 
Compare and contrast the development of a WBS in traditional project.pdf
Compare and contrast the development of a WBS in traditional project.pdfCompare and contrast the development of a WBS in traditional project.pdf
Compare and contrast the development of a WBS in traditional project.pdf
 
Compare and contrast style and tone in management communicationS.pdf
Compare and contrast style and tone in management communicationS.pdfCompare and contrast style and tone in management communicationS.pdf
Compare and contrast style and tone in management communicationS.pdf
 
Compare and contrast the analysis of variance (ANOVA) with a t test..pdf
Compare and contrast the analysis of variance (ANOVA) with a t test..pdfCompare and contrast the analysis of variance (ANOVA) with a t test..pdf
Compare and contrast the analysis of variance (ANOVA) with a t test..pdf
 

Kürzlich hochgeladen

Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 

Kürzlich hochgeladen (20)

Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

Code in c++ for largest rectangle in histogramSolution#include.pdf

  • 1. Code in c++ for largest rectangle in histogram Solution #include #define ll long long #define N_lim 100010 ll max(ll a, ll b) { return a>b?a:b; } ll largestArea(int h[], int n) { ll i, top = 1, area = 0, S[N_lim]; h[0] = S[0] = h[++n] = 0; for(i=1; i<=n; i++) { while(top>1 && h[S[top-1]]>=h[i]) { area = max(area, (ll)h[S[top-1]]*(ll)(i - S[top-2]-1)); top--; } S[top++] = i; } return area; } int main() { int n, i, h[N_lim]; while(scanf("%d", &n)==1 && n) { for(i=1; i<=n; i++) scanf("%d", &h[i]); printf("%lld ", largestArea(h, n)); } return 0;
  • 2. }