SOJ-2511(两次用栈,一次从前往后一次从后往前)

来源:互联网 发布:mysql 停止主从同步 编辑:程序博客网 时间:2024/06/04 18:03
/****************************************************************************************************** ** Copyright (C) 2011.06.12(8:000)-2013.07.01 ** Author: famousDT <13730828587@163.com> ** Edit date: 2011-06-12******************************************************************************************************/#include <stdio.h>#include <stdlib.h>//abs,atof(string to float),atoi,atol,atoll#include <math.h>//atan,acos,asin,atan2(a,b)(a/b atan),ceil,floor,cos,exp(x)(e^x),fabs,log(for E),log10#include <vector>#include <queue>#include <map>#include <set>#include <stack>#include <string>#include <iostream>#include <string.h>//memcpy(to,from,count#include <ctype.h>//character process:isalpha,isdigit,islower,tolower,isblank,iscntrl,isprll#include <algorithm>using namespace std;//typedef long long ll;#define PI acos(-1)#define MAX(a, b) ((a) > (b) ? (a) : (b))#define MIN(a, b) ((a) < (b) ? (a) : (b))#define MALLOC(n, type) ((type *)malloc((n) * sizeof(type)))#define FABS(a) ((a) >= 0 ? (a) : (-(a)))struct my{    int a;    int b;}wo[50005];int main(){    int n;    int i;    int d[50005][2];    while (scanf("%d", &n) == 1) {        int t[50005] = {0};        t[0] = 0;        for (i = 0; i < n; ++i)            scanf("%d%d", &wo[i].a, &wo[i].b);        stack<my> s;        s.push(wo[0]);        for (i = 1; i < n; ++i) {            while (!s.empty() && wo[i].a > s.top().a) {                t[i] += s.top().b;                s.pop();            }            s.push(wo[i]);        }        while (!s.empty())            s.pop();        s.push(wo[n - 1]);        for (i = n - 2; i >= 0; --i) {            while (!s.empty() && wo[i].a > s.top().a) {                t[i] += s.top().b;                s.pop();            }            s.push(wo[i]);        }        int max = -1;        for (i = 0; i < n; ++i)            if (max < t[i])                max = t[i];        printf("%d\n", max);    }    return 0;}

 
原创粉丝点击