SlideShare une entreprise Scribd logo
1  sur  2
Télécharger pour lire hors ligne
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;
}

Contenu connexe

Plus de 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
 

Plus de 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
 

Dernier

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 

Dernier (20)

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 

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. }