寒假刷题第12天

发布时间:2024年01月23日

PTA甲级

1075 PAT Judge

结构体排序

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

using namespace std;

const int N = 1e5 + 10 , M = 1e4 + 10;
int n , k , m;
int p[N];
struct user
{
    int name , total , rank , cnt , com , sub;
    int score[20] = {0};
    string res;
}users[N];
unordered_set<int>se;
vector<user>v;

bool cmp(user a , user b)
{
    if(a.total != b.total) return a.total > b.total;
    else if(a.cnt != b.cnt) return a.cnt > b.cnt;
    return a.name < b.name;
}

int main()
{
    cin >> n >> k >> m;
    for(int i = 1;i <= k;i ++)
        cin >> p[i];
    for(int i = 1;i <= M;i ++)
        memset(users[i].score , -2 , sizeof users[i].score);
    while(m --)
    {
        int x , id , s;
        cin >> x >> id >> s;
        se.insert(x);
        users[x].score[id] = max(s , users[x].score[id]);
        users[x].name = x;
    }
    for(int i : se)
    {
        int x = 0;
        for(int j = 1;j <= k;j ++)
        {
            if(users[i].score[j] == -1) // 表示编译未通过
                users[i].res += " 0" , users[i].com ++;
            else if(users[i].score[j] >= 0) 
                users[i].res += " " + to_string(users[i].score[j]) , x += users[i].score[j];
            else // 未提交
                users[i].res += " -" , users[i].sub ++;
            if(users[i].score[j] == p[j]) users[i].cnt ++;
        }
        users[i].total = x;
        if(users[i].sub + users[i].com < k) v.push_back(users[i]);
    }
    sort(v.begin() , v.end() , cmp);
    for(int i = 0;i < v.size();i ++)
    {
        if(!i) v[i].rank = 1;
        else 
        {
            if(v[i].total == v[i - 1].total) v[i].rank = v[i - 1].rank;
            else v[i].rank = i + 1;
        }
        printf("%d %05d %d" , v[i].rank , v[i].name , v[i].total);
        cout << v[i].res << endl;
    }
    return 0;
}

1076 Forwards on Weibo

dfs

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

using namespace std;

const int N = 1010;
int n , l , k;
int cnt = 0;
unordered_map<int , vector<int>>mp;
bool st[N];
int level[N];

void dfs(int u , int h)
{
    if(h > l) return ;
    if(!st[u]) cnt ++;
    st[u] = true;
    level[u] = h;
    for(int i : mp[u])
        if(h + 1 < level[i]) dfs(i , h + 1);
}

int main()
{
    cin >> n >> l;
    for(int j = 1;j <= n;j ++)
    {
        int m;
        cin >> m;
        for(int i = 0;i < m;i ++)
        {
            int x;
            cin >> x;
            mp[x].push_back(j);
        }
    }
    cin >> k;
    while(k --)
    {
        memset(st , 0 , sizeof st);
        memset(level , 10 , sizeof level);
        cnt = 0;
        int x;
        cin >> x;
        dfs(x , 0);
        cout << cnt - 1 << endl;
    }
    return 0;
}
// from functools import lru_cache
// N = 1010
// mp = {}
// n , l = map(int , input().split())
// for _ in range(1 , n + 1):
//     mp[_] = []
// for _ in range(1 , n + 1):
//     x = list(map(int , input().split()))
//     x.pop(0)
//     for i in x:
//         mp[i].append(_)
// k = list(map(int , input().split()))
// k.pop(0)
// st = [False] * 1010
// res = 0
// @lru_cache(maxsize = 1000, typed = False)
// def dfs(u , h , l):
//     if h > l:
//         return 0
//     if not st[u]:
//         global res
//         res += 1
//     st[u] = True
//     for i in mp[u]:
//         dfs(i , h + 1 , l)

// for i in range(len(k)):
//     dfs(k[i] , 0 , l)
//     print(res - i - 1)

1079 Total Sales of Supply Chain(突破100题大关)

dfs

#include<iostream>
#include<vector>
#include<cstring>
#include<cmath>

using namespace std;

const int N = 1e5 + 10;
vector<int>v[N];
int n;
double p , r , res;
bool st[N];
int retailer[N];

void dfs(int u , int h)
{
    if(st[u]) return ;
    if(retailer[u]) res += pow((0.01 * r + 1) , h) * p * retailer[u];
    st[u] = true;
    for(int i : v[u])
        dfs(i , h + 1);
}

int main()
{
    memset(st , 0 , sizeof st);
    cin >> n >> p >> r;
    for(int j = 0;j < n;j ++)
    {
        int k;
        cin >> k;
        for(int i = 0;i < k;i ++)
        {
            int x;
            cin >> x;
            v[j].push_back(x);
        }
        if(!k) cin >> retailer[j];
    }
    dfs(0 , 0);
    printf("%.1lf" , res);
    return 0;
}
文章来源:https://blog.csdn.net/xp_xht123/article/details/135767369
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。