Task 11

来源:互联网 发布:linux 匹配文件内容 编辑:程序博客网 时间:2024/06/06 14:25

T1

题目大意:

5元, 10元, 20元排队买书, 给定顺序, 求能否处理

简单分析:

直接模拟

标算:
#include<cstdio>int a[2], n, x;int main() {    scanf("%d", &n);    for(int i = 1; i <= n; ++i) {        scanf("%d", &x);        if(x == 5) a[0]++;        if(x == 10) {            if(a[0]>0) a[0]--;            else {                printf("NO\n");                return 0;            }            a[1]++;        }        if(x == 20) {            if(a[1]>0 && a[0]>0) a[0]--, a[1]--;            else if(a[0]>2) a[0]-=3;            else {                printf("NO\n");                return 0;            }        }    }    printf("YES\n");    return 0;}

T2

题目大意:

n个函数, 给出几个出口的位置, 问是否是一个可能的序列

简单分析:

括号匹配, 直接模拟

标算:

数组z是个栈

#include<cstdio>const int N = 1e6 + 10;int a[N], z[N], n, m, x, p;int main() {    scanf("%d", &n);    for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);    scanf("%d", &m);    for(int i = 1; i <= m; ++i) {        scanf("%d", &x);        if(a[x]>0) a[x] = -a[x];    }    for(int i = n; i >= 1; --i) {        if(a[i] > 0) {            if(z[p] == -a[i]) p--;            else {                a[i] = -a[i];                p++;                z[p] = a[i];            }        } else {            p++;            z[p] = a[i];        }    }    if(p == 0) {        for(int i = 1; i <= n; ++i) {            if(a[i] > 0) printf("+%d ", a[i]);            else printf("%d ", a[i]);        }    } else printf("NO\n");    return 0;}

T3

不可做的题目大意:

迷宫是由 n 个交叉口和 m 条路径构成(路径为无向边),路径上有门和钥匙,对应的门需要用对应的钥匙来开门。他们决定使用贪心的方式走过迷宫,每次路过一条边一定会捡起边上的钥匙(钥匙多不坏事?),并把钥匙存在栈里,钥匙被捡起后会刷新。遇到一个门,他们就会从栈中拿起一个钥匙开门。钥匙开门后会消失。小 l 想知道从 a 是否能够成功的从 A 岔路口走到 B 岔路口,而且到 B 时栈为空。

用命分析:

Floyed
F[i][j][k]表示从i到j能否以k的方式到达。
K = 括号完全匹配 或者 缺少一个右括号

咋都看不懂的标算:
#include<cstdio>const int N = 100 + 10;int n, m, x, y, z, t, g[N][N][40], q[N*N*40][3], sta;int main() {    scanf("%d%d", &n, &m);    t = 0;    for(int i = 1; i <= m; ++i) {        scanf("%d%d%d", &x, &y, &z);        if(z) {            if(z < 0) z = -z;            else z += 10;            g[x][y][z] = g[y][x][z] = 1;        } else {            g[x][y][z] = g[y][x][z] = 1;            ++t;            q[t][0] = x; q[t][1] = y; q[t][2] = 0;            ++t;            q[t][0] = y; q[t][1] = x; q[t][2] = 0;        }    }    for(int i = 1; i <= n; ++i) {        g[i][i][0] = 1;        ++t;        q[t][0] = i; q[t][1] = i; q[t][2] = 0;    }    for(int s = 1; s <= t; ++s) {        x = q[s][0]; y = q[s][1]; sta = q[s][2];        if(sta) {            for(int i = 1; i <= n; ++i) {                if(g[i][x][sta-10] == 1) {                    if(g[i][y][0] == 0) {                        g[i][y][0] = 1;                        ++t;                        q[t][0] = i; q[t][1] = y; q[t][2] = 0;                    }                }            }        } else {            for(int i = 1; i <= n; ++i) {                if(g[i][x][0] == 1 && g[i][y][0] == 0) {                    g[i][y][0] = 1;                    ++t;                    q[t][0] = i; q[t][1] = y; q[t][2] = 0;                }                if(g[y][i][0] == 1 && g[x][i][0] == 0) {                    g[x][i][0] = 1;                    ++t;                    q[t][0] = x; q[t][1] = i; q[t][2] = 0;                }                for(int j = 1; j <= 10; ++j) {                    if(g[y][i][j] == 1 && g[x][i][j+20] == 0) {                        g[x][i][j+20] = 1;                        ++t;                        q[t][0] = x; q[t][1] = i; q[t][2] = j+20;                    }                }            }        }    }    int tt = 0;    scanf("%d", &tt);    while(tt--) {        scanf("%d%d", &x, &y);        if(g[x][y][0] == 1) printf("YES\n");        else printf("NO\n");    }    return 0;}