PAT甲级官网 刷题(5)

PAT1129 Recommendation System

  需要稍加思考,每次都sort会TLE。

#include<iostream>
#include<algorithm>

#define ac cin.tie(0);cin.sync_with_stdio(0);

using namespace std;
const int MAXN = 50010;
int arr[20], cnt[MAXN];
//按照次数排序,若相同则按照大小排序
bool cmp(int a, int b) {
    if (cnt[a] != cnt[b])
        return cnt[a] > cnt[b];
    else
        return a < b;
}

int main() {
    ac
    int n, m, k = 0, a;

    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> a;
        if (i) {
            cout << a << ":";
            for (int j = 0; j < k; j++)
                cout << " " << arr[j];
            cout << endl;
        }
		//次数+1
        cnt[a]++;
        bool op = false;
        for (int j = 0; j < k; j++)
            if (arr[j] == a) {
                op = true;
                break;
            }
        //没找到,可能有k之外的元素进入前k
        if (!op)
            arr[k++] = a;
        //找到了,那前k个元素肯定还是前k大,只需要内部排序
        sort(arr, arr + k, cmp);
        //k不能超过m
        if (k > m)
            k = m;
    }

    return 0;
}

PAT1121 Damn Single

  签到题,可以用一下unordered_set来优化一下。

#include<iostream>
#include<unordered_set>
#include<algorithm>
#include<cstring>

using namespace std;
const int MAXN = 100010;
//记录着对应couple的编号
int map[MAXN], ans[MAXN];
unordered_set<int> set;

int main() {
    int n, a, b, pos = 0;
	//因为编号从0开始,故有可能出错,赋初值超过100000即可
    memset(map, 0x3f, sizeof(map));

    cin >> n;
    while (n--) {
        cin >> a >> b;
        map[a] = b;
        map[b] = a;
    }

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a;
        set.insert(a);
    }
    for (int i:set)
    	//找不到自己的情侣
        if (!set.count(map[i]))
            ans[pos++] = i;
    sort(ans, ans + pos);
    cout << pos << endl;
    for (int i = 0; i < pos; i++) {
        printf("%05d", ans[i]);
        if (i != pos - 1)
            cout << " ";
    }
    return 0;
}

PAT1055 The World’s Richest

  使用邻接表和优先队列,有一个样例TLE。

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>

#define ac cin.tie(0);cin.sync_with_stdio(0);

using namespace std;

struct node {
    string name;
    int age, wealth;

    node(string a, int b, int c) : name(a), age(b), wealth(c) {};

    node() {};

    //注意优先队列的重载运算符是反的
    bool operator<(const node &a) const {
        if (wealth != a.wealth)
            return wealth < a.wealth;
        else if (age != a.age)
            return age > a.age;
        else
            return name > a.name;
    }
};

vector<node> G[210];
int N, K;

bool cmp(const node &a, const node &b) {
    return a.age < b.age;
}

int main() {
    ac
    string s;
    int a, b, lim;

    cin >> N >> K;
    for (int i = 0; i < N; i++) {
        cin >> s >> a >> b;
        G[a].push_back(node(s, a, b));
    }

    node tmp;
    for (int i = 1; i <= K; i++) {
        cin >> lim >> a >> b;
        priority_queue<node> Q;
		//年龄之间的节点全部加入到优先队列中,这里可以优化
        for (int i = a; i <= b; i++)
            for (node &j:G[i])
                Q.push(j);
        cout << "Case #" << i << ":" << endl;
        if (Q.empty())
            cout << "None" << endl;
        while (lim-- && !Q.empty()) {
            tmp = Q.top();
            Q.pop();
            cout << tmp.name << " " << tmp.age << " " << tmp.wealth << endl;
        }
    }
    return 0;
}

PAT1051 Pop Sequence

  很经典的一题,推荐做一下,提高对栈的理解。

#include<iostream>
#include<stack>

#define ac cin.tie(0);cin.sync_with_stdio(0);
using namespace std;
const int MAXN = 100010;
int arr[MAXN];
int m, n, k;

int main() {
    ac

    cin >> m >> n >> k;
    while (k--) {
        stack<int> ST;
        for (int i = 1; i <= n; i++)
            cin >> arr[i];

        int pos = 1;
        bool op = true;
        for (int i = 1; i <= n && op; i++) {
        	//栈为空或者不等于目标元素
            while (ST.empty() || ST.top() != arr[i]) {
                ST.push(pos++);
                //超过最大容量
                if (ST.size() > m) {
                    op = false;
                    break;
                }
            }
            ST.pop();
        }
        if (op)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

PAT1091 Acute Stroke

  Blood Fill裸题,可以bfs也可dfs,拓展到了三维,只需要稍稍改动fx数组。

#include<iostream>
#include<queue>

#define ac cin.tie(0);cin.sync_with_stdio(0);
using namespace std;
const int M = 1300;
const int N = 130;
const int L = 70;
bool G[L][M][N];
int m, n, l, t;

struct node {
    int l, m, n;

    node(int _x, int _y, int _z) : l(_x), m(_y), n(_z) {};
};
//三维
int fx[][3] = {
        {0,  0,  1},
        {0,  0,  -1},
        {0,  1,  0},
        {0,  -1, 0},
        {1,  0,  0},
        {-1, 0,  0},
};

bool check(const node &a) {
    return a.l >= 0 && a.m >= 0 && a.n >= 0 && a.l < L && a.n < N && a.m < M;
}

int bfs(int l, int m, int n) {
    int ans = 0;
    node tmp(l, m, n);
    G[l][m][n] = false;
    queue<node> Q;
    Q.push(tmp);
    while (!Q.empty()) {
        node cur(Q.front());
        Q.pop();
        ++ans;
        for (int i = 0; i < 6; i++) {
            node nxt(cur);
            //三维方向拓展
            nxt.l += fx[i][0];
            nxt.m += fx[i][1];
            nxt.n += fx[i][2];
            if (check(nxt) && G[nxt.l][nxt.m][nxt.n]) {
                Q.push(nxt);
                //要变为false
                G[nxt.l][nxt.m][nxt.n] = false;
            }
        }
    }
    return ans;
}

int main() {
    ac
    int ans = 0;

    cin >> m >> n >> l >> t;

    for (int i = 0; i < l; i++)
        for (int j = 0; j < m; j++)
            for (int k = 0; k < n; k++)
                cin >> G[i][j][k];

    for (int i = 0; i < l; i++)
        for (int j = 0; j < m; j++)
            for (int k = 0; k < n; k++)
                if (G[i][j][k]) {
                    int cnt = bfs(i, j, k);
                    if (cnt >= t)
                        ans += cnt;
                }
    cout << ans;
    return 0;

}

官网的题快刷完了,考试前一定刷完,奥里给><
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值