UVA 514 Rails

来源:互联网 发布:上海大学云计算预约 编辑:程序博客网 时间:2024/06/09 20:38

栈模拟一下,看看输入的顺序能否由正整数序列得到。数组模拟栈或用stl都可以。

如果当前数字比栈顶大,就把之间没用过的都压进栈里,小就return 0,相等就弹栈。

#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<vector>#include<string>#include<queue>#include<cmath>#include<set>///LOOP#define REP(i, n) for(int i = 0; i < n; i++)#define FF(i, a, b) for(int i = a; i < b; i++)#define FFF(i, a, b) for(int i = a; i <= b; i++)#define FD(i, a, b) for(int i = a - 1; i >= b; i--)#define FDD(i, a, b) for(int i = a; i >= b; i--)///INPUT#define RI(n) scanf("%d", &n)#define RII(n, m) scanf("%d%d", &n, &m)#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)#define RFI(n) scanf("%lf", &n)#define RFII(n, m) scanf("%lf%lf", &n, &m)#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)#define RS(s) scanf("%s", s)///OUTPUT#define PN printf("\n")#define PI(n) printf("%d\n", n)#define PIS(n) printf("%d ", n)#define PS(s) printf("%s\n", s)#define PSS(s) printf("%s ", n)///OTHER#define PB(x) push_back(x)#define CLR(a, b) memset(a, b, sizeof(a))#define CPY(a, b) memcpy(a, b, sizeof(b))#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}}using namespace std;typedef long long LL;typedef pair<int, int> P;const int MOD = 100000000;const int INFI = 1e9 * 2;const LL LINFI = 1e17;const double eps = 1e-6;const double pi = acos(-1.0);const int N = 1111;const int M = 22;const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};int a[N], s[N], n, sp;bool vis[N];bool check(){    int m = 1;    sp = 0;    CLR(vis, 0);    REP(i, n)    {        vis[a[i]] = 1;        if(sp)        {            if(s[sp - 1] > a[i])return 0;            else if(s[sp - 1] == a[i])sp--;        }        while(m < a[i])        {            if(!vis[m])s[sp++] = m, vis[m] = 1;            m++;        }    }    return 1;}int main(){    //freopen("input.txt", "r", stdin);    //freopen("output.txt", "w", stdout);    while(RI(n), n)    {        while(RI(a[0]), a[0])        {            FF(i, 1, n)RI(a[i]);            puts(check() ? "Yes" : "No");        }        puts("");    }    return 0;}


原创粉丝点击