寒假刷题第五天

PTA甲级

1022 Digital Library

大模拟

cpp 复制代码
#include<iostream>
#include<unordered_map>
#include<unordered_set>
#include<vector>
#include<set>

using namespace std;

unordered_map<string , set<int>>ti , au , key , pub , year;
unordered_set<string>se;

void print(set<int>&v)
{
    for(auto i : v)
        printf("%07d\n" , i); // 注意是7位数字
}

vector<string> split(string s)
{
    vector<string>v;
    int last = 0;
    for(int i = 0;i < s.size();i ++)
        if(s[i] == ' ') v.push_back(s.substr(last , i - last)) , last = i + 1;
    v.push_back(s.substr(last));
    return v;
}

int main()
{
    int t;
    cin >> t;
    while(t --)
    {
        int id;
        cin >> id;
        getchar();
        for(int i = 0;i < 5;i ++)
        {
            string s;
            getline(cin , s);
            se.insert(s);
            if(i == 0) ti[s].insert(id);
            else if(i == 1) au[s].insert(id);
            else if(i == 2) 
            {
                vector<string>v = split(s);
                for(auto j : v)
                    key[j].insert(id) , se.insert(j);
            }
            else if(i == 3) pub[s].insert(id);
            else year[s].insert(id);
        }
    }
    int k;
    cin >> k;
    getchar();
    while(k --)
    {
        string s;
        getline(cin , s);
        cout << s << endl;
        int k = s.find(':');
        string t = s.substr(k + 2);
        if(!se.count(t)) puts("Not Found");
        else
        {
            if(ti.count(t)) print(ti[t]);
            else if(au.count(t)) print(au[t]);
            else if(key.count(t)) print(key[t]);
            else if(pub.count(t)) print(pub[t]);
            else if(year.count(t)) print(year[t]);
        }
    }
    return 0;
}

1025PAT Ranking

排序

cpp 复制代码
#include<iostream>
#include<algorithm>
#include<vector>
#include<unordered_map>

using namespace std;

typedef long long ll;
typedef pair<string , int> PII;
const int N = 1e5 + 10;
struct node
{
    string id; // 注意用string
    int score;
    int loc;
    int finalrank;
    int locrank;
};
vector<node>v(N);
int n;
unordered_map<string , int>mp;

bool cmp1(PII a , PII b)
{
    if(a.second != b.second) return a.second > b.second;
    return a.first < b.first;
}

bool cmp2(node a , node b)
{
    if(a.score != b.score) return a.score > b.score;
    return a.id < b.id;
}

int main()
{
    cin >> n;
    int cnt = 0;
    for(int i = 1;i <= n;i ++)
    {
        int m;
        cin >> m;
        vector<PII>t;
        for(int j = 0;j < m;j ++)
        {
            cin >> v[cnt].id >> v[cnt].score;
            v[cnt].loc = i;
            mp[v[cnt].id] = cnt;
            t.push_back({v[cnt].id , v[cnt].score});
            cnt ++;
        }
        sort(t.begin() , t.end() , cmp1);
        for(int j = 0;j < t.size();j ++)
            if(j == 0) v[mp[t[j].first]].locrank = j + 1;
            else
            {
                if(t[j].second == t[j - 1].second) v[mp[t[j].first]].locrank = v[mp[t[j - 1].first]].locrank;
                else v[mp[t[j].first]].locrank = j + 1;
            }
    }
    cout << cnt << endl;
    sort(v.begin() , v.begin() + cnt , cmp2);
    for(int i = 0;i < cnt;i ++)
    {
        if(i == 0) 
            cout << v[i].id << " " << i + 1 << " " << v[i].loc << " " << v[i].locrank << endl , v[i].finalrank = 1;
        else
        {
            if(v[i - 1].score == v[i].score) 
                cout << v[i].id << " " << v[i - 1].finalrank << " " << v[i].loc << " " << v[i].locrank << endl , v[i].finalrank = v[i - 1].finalrank;
            else 
                cout << v[i].id << " " << i + 1 << " " << v[i].loc << " " << v[i].locrank << endl , v[i].finalrank = i + 1;
        }
    }
    return 0;
}

1029 Median

找中位数,直接用python写更快

python 复制代码
A = list(map(int , input().split()))
B = list(map(int , input().split()))
A.pop(0);B.pop(0);
A = A + B
A.sort()
print(A[len(A) // 2 if len(A) % 2 else (len(A) - 1) // 2])
相关推荐
ccLianLian1 小时前
数据结构·ST表
数据结构
大锦终2 小时前
【C++】红黑树
c语言·开发语言·数据结构·c++
似水এ᭄往昔2 小时前
【数据结构】——栈
c语言·数据结构
福居路冥想的草莓3 小时前
逆波兰表达式求值(中等)
数据结构
cdut_suye4 小时前
【Linux系统】从零开始构建简易 Shell:从输入处理到命令执行的深度剖析
java·linux·服务器·数据结构·c++·人工智能·python
yaoshengvalve5 小时前
V型球阀材质性能深度解析:专攻颗粒、料浆与高腐蚀介质的工业利器-耀圣
开发语言·网络·数据结构·c++·安全·材质
evolution_language5 小时前
LintCode第68题-二叉树的前序遍历,第67题-二叉树的后序遍历
数据结构·算法·新手必刷编程50题
是店小二呀5 小时前
【算法-哈希表】常见算法题的哈希表套路拆解
数据结构·c++·算法·散列表
jiunian_cn5 小时前
【c++】多态详解
java·开发语言·数据结构·c++·visual studio
究极无敌暴龙战神X9 小时前
hot100-子串-JS
javascript·数据结构·算法