0%

CCCC初赛 关于堆的判断

https://www.patest.cn/contests/gplt/L2-012
题意就不用我说了
比赛中写了个丑的不行的程序。。。
于是后来又写了一个非常STL风格的
想了想还是丢到博客了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;

vector<int> a;
map<int,int> pos;

int getint(string s)
{
stringstream ss;
ss<<s;
int res;
ss>>res;
return res;
}

bool right(string &s)
{
if (s.find("root")!=string::npos)
return (pos[getint(s)]==1);
int p = s.find_first_of("-1234567890",s.find(" "));
int x = getint(s);
int y = getint(s.substr(p,s.size()-p));
if (s.find("parent")!=string::npos)
return (pos[x]==pos[y]>>1);
if (s.find("child")!=string::npos)
return (pos[x]>>1==pos[y]);
//contains "siblings"
return (pos[x]>>1==pos[y]>>1);
}

int main()
{
int n,m,i,x;
scanf("%d%d",&n,&m);
for (i=0;i<n;i++)
{
scanf("%d",&x);
a.push_back(x);
push_heap(a.begin(),a.end(),greater<int>());
}
for (i=0;i<n;i++) pos[a[i]]=i+1;
scanf("\n");
while (m--)
{
string s;
getline(cin,s);
if (right(s)) puts("T");
else puts("F");
}
return 0;
}
咖啡,亦我所需也