题目详情 - L2-012 关于堆的判断 (25 分) (pintia.cn)
已查出:字符串处理错误,还未能解决。
猜测:题目的测试点不标准,有尾随空格
#include<iostream>
#include<unordered_map>
using namespace std;
const int N = 1010, INF = 0x3f3f3f3f;
unordered_map<int,int> um;
int h[N],sz;
void insert(int x){
h[++sz] = x;
int cur = sz;
while(cur/2 && h[cur/2] > h[cur]){
swap(h[cur/2],h[cur]);
cur /= 2;
}
}
int main(){
int n,m;
cin>>n>>m;
for(int i = 0; i < n; i++){
int x;
cin>>x;
insert(x);
}
for(int i = 1; i <= n; i++) um[h[i]] = i;
while(m--){
string str;
int x = INF, y = INF, op = INF;
char c;
while(cin>>str){
c = getchar();
// cout<<str<<endl;
if( isdigit(str[0]) ) {
if(x == INF)
x = stoi(str);
else if(y == INF)
y = stoi(str);
} else if(str == "root")
op = 1;
else if(str == "siblings")
op = 2;
else if(str == "parent")
op = 3;
else if(str == "child")
op = 4;
if(c == '\n') break;
}
if(op == 1){
if(um[x] == 1) puts("T");
else puts("F");
} else if(op == 2){
if(um[x]/2 == um[y]/2) puts("T");
else puts("F");
} else if(op == 3){
if(um[x] == um[y]/2) puts("T");
else puts("F");
} else if(op == 4){
if(um[x]/2 == um[y]) puts("T");
else puts("F");
}
}
return 0;
}
AC代码
#include<iostream>
#include<unordered_map>
using namespace std;
const int N = 1010, INF = 0x3f3f3f3f;
unordered_map<int,int> mp;
int h[N],sz;
void insert(int x){
h[++sz] = x;
int cur = sz;
while(cur/2 && h[cur/2] > h[cur]){
swap(h[cur/2],h[cur]);
cur /= 2;
}
}
int main(){
int n,m;
cin>>n>>m;
for(int i = 0; i < n; i++){
int x;
cin>>x;
insert(x);
}
for(int i = 1; i <= n; i++) mp[h[i]] = i;
for (int i = 0; i < m; ++i) {
int a;
scanf("%d", &a);
string s1; cin >> s1;
if (s1 == "and") {
int b; scanf("%d", &b);
cin >> s1 >> s1;
if (mp[a] / 2 == mp[b] / 2)puts("T");
else puts("F");
}
else {
cin >> s1;
if (s1 == "a") {
cin >> s1 >> s1;
int b; scanf("%d", &b);
if (mp[a] / 2 == mp[b])puts("T");
else puts("F");
}
else {
cin >> s1;
if (s1 == "root") {
if (mp[a] == 1)puts("T");
else puts("F");
}
else {
cin >> s1;
int b; scanf("%d", &b);
if (mp[b] / 2 == mp[a])puts("T");
else puts("F");
}
}
}
}
return 0;
}