寒假刷题第18天

PTA甲级

1127 ZigZagging on a Tree

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

using namespace std;

const int N = 50 , M = 1e5 + 10;
int n;
int in[N] , post[N];
int l[N] , r[N];
int mp[M];

int build(int il , int ir , int pl , int pr)
{
    int root = post[pr];
    int k = mp[root];
    if(il < k) l[root] = build(il , k - 1 , pl , pl + k - 1 - il);
    if(ir > k) r[root] = build(k + 1 , ir , pl + k - il , pr - 1);
    return root;
}

int main()
{
    cin >> n;
    for(int i = 0;i < n;i ++) cin >> in[i] , mp[in[i]] = i;
    for(int i = 0;i < n;i ++) cin >> post[i];

    int root = build(0 , n - 1 , 0 , n - 1);
    queue<pair<int , int>>q;
    q.push({root , 1});
    vector<int>res[N];
    int max_dep = 0;
    while(!q.empty())
    {
        auto [root , dep] = q.front();
        q.pop();

        res[dep].push_back(root);
        max_dep = max(max_dep , dep);
        if(l[root]) q.push({l[root] , dep + 1});
        if(r[root]) q.push({r[root] , dep + 1});
    }
    cout << res[1][0];
    for(int i = 2;i <= max_dep;i ++)
    {
        if(i % 2 == 0)
        {
            for(int j = 0;j < res[i].size();j ++)
                cout << " " << res[i][j];
        }
        else 
        {
            for(int j = res[i].size() - 1;j >= 0;j --)
                cout << " " << res[i][j];
        }
    }
    return 0;
}

1129 Recommendation System

#include<iostream>
#include<set>

using namespace std;

int book[50001];
struct node 
{
    int value , cnt;
    node(int a, int b):value(a), cnt(b){}
    bool operator < (const node &a) const 
    {
        return (cnt != a.cnt) ? cnt > a.cnt : value < a.value;
    }
};
int n , k , num;

int main() 
{
    scanf("%d%d", &n, &k);
    set<node>s;
    for (int i = 0;i < n;i ++) 
    {
        scanf("%d", &num);
        if (i != 0) 
        {
            printf("%d:", num);
            int tempCnt = 0;
            for(auto it = s.begin();tempCnt < k && it != s.end();it ++) 
            {
                printf(" %d", it->value);
                tempCnt++;
            }
            printf("\n");
        }
        auto it = s.find(node(num , book[num]));
        if(it != s.end()) s.erase(it);
        book[num] ++;
        s.insert(node(num, book[num]));
    }
    return 0;
}

1168 Prime Day

n = input()
def prime(x):
    if x <= 1:
        return False
    i = 2
    while i * i <= x:
        if x % i == 0:
            return False
        i += 1
    return True

cnt = len(n)
for i in range(len(n)):
    s = n[i:]
    if prime(int(s)):
        cnt -= 1
        print(s , "Yes")
    else:
        print(s , "No")

if cnt == 0:
    print("All Prime!")

1169 The Judger

#include<iostream>
#include<unordered_map>
#include<unordered_set>
#include<vector>

using namespace std;

const int N = 100 , M = 1e5 + 10;
bool mp[M] = {0} , mp1[M] = {0};
bool se[M] = {0};
int A , B;
int n , k;
int cnt = 0;
vector<int>v[M];
vector<int>pro;

int main()
{
    cin >> A >> B;
    cin >> n >> k;
    pro.push_back(A) , pro.push_back(B);
    mp[A] = 1 , mp[B] = 1;
    mp1[abs(A - B)] = 1;

    for(int i = 1;i <= n;i ++)
    {
        for(int j = 1;j <= k;j ++)
        {
            int x;
            cin >> x;
            v[i].push_back(x);
        }
    }

    for(int i = 1;i <= k;i ++)
    {
        for(int j = 1;j <= n;j ++)
        {
            if(se[j]) continue;
            int x = v[j][i - 1];
            if(mp[x] || !mp1[x]) 
            {
                se[j] = true;
                cnt ++;
                printf("Round #%d: %d is out.\n" , i , j);
                continue;
            }
            mp[x] = true;
            pro.push_back(x);
            for(auto p : pro)
                mp1[abs(x - p)] = true;
        }
    }
    if(cnt == n) puts("No winner.");
    else 
    {
        cout << "Winner(s):";
        for(int i = 1;i <= n;i ++)
            if(!se[i]) cout << " " << i;
    }
    return 0;
}

1170 Safari Park

#include<iostream>
#include<set>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>

using namespace std;

const int N = 510;
int n , m , k;
vector<int>v[N];
bool g[N][N];
int w[N];

bool check()
{
    for(int i = 1;i <= n;i ++)
        for(int j = 1;j <= n;j ++)
            if(g[i][j] && w[i] == w[j]) return false;
    return true;
}

int main()
{
    cin >> n >> m >> k;
    for(int i = 0;i < m;i ++)
    {
        int a , b;
        cin >> a >> b;
        g[a][b] = g[b][a] = true;
    }
    cin >> m;
    while(m --)
    {
        set<int>se;
        for(int i = 1;i <= n;i ++)
        {
            int x;
            cin >> x;
            w[i] = x;
            se.insert(x);
        }
        if(se.size() < k) puts("Error: Too few species.");
        else if(se.size() > k) puts("Error: Too many species.");
        else
        {
            if(check()) puts("Yes");
            else puts("No");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值