【題解】ZeroJudge f446: 1237 – Expert Enough

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=f446 (考生答對率: 23.18%)

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;

int t, n, Q, q, l, r;
vector <pair<int, string>> v;
string s, ans[1000005];

int main() {
    cin >> t;
    while (t--){
        cin >> n;
        v.clear();
        for (int i = 0; i < n; i++){
            cin >> s >> l >> r;
            v.push_back({l, s});
            v.push_back({r+1, s});
        }
        sort(v.begin(), v.end());
        set <string> st;
        int pos = 0;
        for (int i = 1; i <= 1000000; i++){
            while (v[pos].first == i){
                if (st.count(v[pos].second)) st.erase(v[pos].second);
                else st.insert(v[pos].second);
                pos++;
            }
            if (st.size() == 1) ans[i] = *st.begin();
            else ans[i] = "UNDETERMINED";
        }
        cin >> Q;
        while (Q--){
            cin >> q;
            cout << ans[q] << "\n";
        }
    }
}

Python code (credit: Amy Chou)

while True:
    try:
        T = int(input())
        break
    except:
        continue

for _ in range(T):
    D = int(input())
    dic = {}
    for i in range(D):
        name, n1, n2 = map(str, input().split())
        dic[name] = (int(n1), int(n2))

    Q = int(input())
    for i in range(Q):
        P = int(input())
        cnt = 0
        for name, n in dic.items():
            if n[0] <= P and P <= n[1]:
                ans = name
                cnt += 1
                
        if cnt == 1:
            print(ans)
        else:
            print("UNDETERMINED")
分享本文 Share with friends