NA (score:30%)
請問比較排名的算法哪裡錯了嗎?
程式碼(C++):
#include <bits/stdc++.h>
using namespace std;
typedef struct{
string Name;
int k, rank;
priority_queue<int> pq;
}Player;
bool cmp(const Player a, const Player b)
{
if(a.pq.top() != b.pq.top())
return a.pq.top() > b.pq.top();
priority_queue<int> Ap = a.pq, Bp = b.pq;
while(!Ap.empty() && !Bp.empty())
{
if(Ap.top() != Ap.top())
return Ap.top() > Bp.top();
Ap.pop();
Bp.pop();
}
return a.k > b.k;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
string name;
int n;
cin>>n;
Player p[n];
map<string, int> mp;
for(int i=0; i<n; i++)
{
cin>>p[i].Name>>p[i].k;
for(int j=0, tmp; j < p[i].k; j++)
{
cin>>tmp;
p[i].pq.push(tmp);
}
}
sort(p, p+n, cmp);
p[0].rank = 1;
mp.insert(pair<string, int>(p[0].Name, 1));
for(int i = 1, now = 1; i < n; i++)
{
if(p[i].pq.top() < p[i-1].pq.top() || p[i].k < p[i-1].k)
{
p[i].rank = ++now;
mp.insert(pair<string, int>(p[i].Name, now));
continue;
}
priority_queue<int> left = p[i-1].pq, right = p[i].pq;
while(!left.empty() && !right.empty())
{
if(left.top() > right.top())
break;
left.pop();
right.pop();
}
if(!left.empty())
p[i].rank = ++now;
else if(left.empty() && right.empty())
p[i].rank = now;
else
p[i].rank = ++now;
mp.insert(pair<string, int>(p[i].Name, p[i].rank));
}
while(cin>>name)
cout<<mp.at(name)<<'\n';
cout.flush();
return 0;
}