L2-012. 关于堆的判断

https://www.patest.cn/contests/gplt/L2-012

题解:1.sstream读入一行

  2.make_heap,find函数直接建堆

坑:cin输入挂 下不能puts。。。

#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<fstream>
#include<iostream>
#include<functional>
#include<algorithm>
#include<memory.h>
//#define INF 0x3f3f3f3f
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n)  for(int i =(t);i<=(n);++i)
#define per(i,n,t)  for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
#define mmm(a,b) memset(a,b,sizeof(a))
//std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void smain();
#define ONLINE_JUDGE
int main() {
    //ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    FILE *myfile;
    myfile =freopen("C:\\Users\\SuuTT\\Desktop\\test\\in.txt", "r", stdin);
    if (myfile == NULL)
        fprintf(stdout, "error on input freopen\n");
    /*FILE *outfile;
    outfile= freopen("C:\\Users\\SuuTT\\Desktop\\test\\out.txt", "w", stdout);
    if (outfile == NULL)
        fprintf(stdout, "error on output freopen\n");*/
    long _begin_time = clock();
#endif
    smain();
#ifndef ONLINE_JUDGE
    long _end_time = clock();
    printf("time = %ld ms.", _end_time - _begin_time);
#endif
    return 0;
}
int dir[4][2] = { 1,0,0,1,-1,0,0,-1 };
const int maxn = 2e2 + 5;
int n, m;
vector<int>heap;
int find(int a) {
    return distance(heap.begin(), find(heap.begin(), heap.end(), a));
}
void smain() {
    cin >> n >> m;
    while (n--) {
        int x;
        cin >> x;
        heap.push_back(x);
        make_heap(heap.begin(), heap.end(), greater<int>());
    }
    getchar();
    while (m--) {
        string line;
        getline(cin, line);

        

        int rlen = line.length() - 1;

        if (line[rlen] == 't') {
            int x; stringstream ss(line); ss >> x;
            if (heap[0] == x)puts("T");else puts("F");
        }
        else if (line[rlen] == 's') {
            string t;
            int a, b; stringstream ss(line); ss>> a >> t >> b;
            int posa = find(a);
            int posb = find(b);
            posa++, posb++;
            if(posa/2==posb/2)puts("T"); else puts("F");
        }
        else {
            stringstream ss(line);
            int a, b;
            string t1, t2;
            ss >> a >> t1 >> t2;
            if (t2[0] == 't') {//parent
                ss >> t1 >> t2 >> b;
                int posb = find(b); posb--;
                int posa = find(a);
                if(posa==posb/2)puts("T"); else puts("F");
            }
            else {//child
                ss >> t1 >> t2 >> b;
                swap(a, b);
                int posb = find(b); posb--;
                int posa = find(a);
                if (posa == posb / 2)puts("T"); else puts("F");
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/SuuT/p/9006855.html