poj1785 Binary Search Heap Construction

此题可以先排序再用rmq递归解决。

当然可以用treap。

http://poj.org/problem?id=1785

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 const int maxn = 5e4 + 10;
 6 struct Point{
 7     char str[15];
 8     int pr;
 9     bool operator < (const Point& rhs) const{
10         return strcmp(str, rhs.str) < 0;
11     }
12 }a[maxn];
13
14 struct Data{
15     int p, v;
16 }st[maxn][20];
17
18 int n;
19
20 int query(int l, int r){
21     int len = r - l, d = 0;
22     while((1 << d) <= len) d++;
23     if(st[l][d - 1].v > st[r - (1 << (d - 1))][d - 1].v) return st[l][d - 1].p;
24     else return st[r - (1 << (d - 1))][d - 1].p;
25 }
26
27 void print(int l, int r){
28     if(l > r) return;
29     putchar(‘(‘);
30     int maxp = query(l, r + 1);
31     print(l, maxp - 1);
32     printf("%s", a[maxp].str);
33     print(maxp + 1, r);
34     putchar(‘)‘);
35 }
36
37 void RMQ(){
38     for(int j = 0; j < n; j++) st[j][0].v = a[j].pr, st[j][0].p = j;
39     for(int i = 1; (1 << i) <= n; i++) for(int j = 0; j < n; j++){
40         if(j + (1 << i) <= n){
41             if(st[j][i - 1].v > st[j + (1 << (i - 1))][i - 1].v){
42                 st[j][i].v = st[j][i - 1].v;
43                 st[j][i].p = st[j][i - 1].p;
44             }else{
45                 st[j][i].v = st[j + (1 << (i - 1))][i - 1].v;
46                 st[j][i].p = st[j + (1 << (i - 1))][i - 1].p;
47             }
48         }
49     }
50 }
51
52 int main(){
53     //freopen("in.txt", "r", stdin);
54     //freopen("out.txt", "w", stdout);
55     while(~scanf("%d", &n) && n){
56         for(int i = 0; i < n; i++){
57             scanf("%s", a[i].str);
58             int len = strlen(a[i].str);
59             a[i].pr = 0;
60             for(int j = len - 1, p = 1; a[i].str[j] != ‘/‘; j--, p *= 10)
61                 a[i].pr += p * (a[i].str[j] - ‘0‘);
62         }
63         sort(a, a + n);
64         RMQ();
65         print(0, n - 1);
66         putchar(‘\n‘);
67     }
68     return 0;
69 }

时间: 2024-11-08 10:28:14

poj1785 Binary Search Heap Construction的相关文章

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

POJ 1785 Binary Search Heap Construction (线段树)

题目大意: 给出的东西要求建立一个堆,使得后面的数字满足堆的性质,而且字符串满足搜索序 思路分析: 用线段树的最大询问建树.在建树之前先排序,然后用中序遍历递归输出. 注意输入的时候的技巧... #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define lson num<<1,s,mid #define rson num<<

笛卡尔树 POJ ——1785 Binary Search Heap Construction

相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 9075   Accepted: 2566 Description Read the statement of problem G for the definitions concerning trees. In the following we define the basic

POJ-1785-Binary Search Heap Construction(笛卡尔树)

Description Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal nodes have each assigned a priority (a number) such that the priority of each

Binary search tree system and method

A binary search tree is provided for efficiently organizing values for a set of items, even when values are duplicated. In generating the binary search tree, the value of each item in a set of values is determined. If a particular value is unique and

04-树8. Complete Binary Search Tree (30)

04-树8. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T th

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De