首页 > 其他 > 详细

HDU --- 4006

时间:2014-03-12 22:43:58      阅读:355      评论:0      收藏:0      [点我收藏+]

The kth great number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 6014    Accepted Submission(s): 2434


Problem Description
Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too much, Xiao Bao is feeling giddy. Now, try to help Xiao Bao.
 

 

Input
There are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed by a number that Xiao Ming will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number. 
 

 

Output
The output consists of one integer representing the largest number of islands that all lie on one line. 
 

 

Sample Input
8 3
I 1
I 2
I 3
Q
I 5
Q
I 4
Q
 

 

Sample Output
1 2 3
 
思路:多次更新,多次询问第K大的数,用线段树超时了,至今不知道为什么超时,最后用优先队列过的。
优先队列:
bubuko.com,布布扣
 1 #include<cstdio>

 2 #include<queue>
 3 #include<vector>
 4 using namespace std;
 5 struct cmp
 6 {
 7     bool operator()(int x, int y)
 8     {
 9         return x > y;
10     }
11 };
12 priority_queue<int, vector<int>, cmp>q;
13 int main(int argc, char const *argv[]) 
14 {
15     int n, k, temp;
16     char str[2];
17     //freopen("in.c", "r", stdin);
18     while(~scanf("%d%d", &n, &k))
19     {
20         while(!q.empty())
21             q.pop();
22         for(int i = 0;i < n;i ++)
23         {
24             scanf("%s", str);
25             if(str[0] == I)
26             {
27                 scanf("%d", &temp);
28                 q.push(temp);
29                 if(q.size() > k)
30                     q.pop();
31             }
32             else
33                 printf("%d\n", q.top());
34         }
35     }
36     return 0;
37 }
bubuko.com,布布扣

线段树:

bubuko.com,布布扣
 1 #include<stdio.h>
 2 #define MAX 1000005
 3 typedef struct
 4 {
 5     int left, right, mid, num, l_cnt, r_cnt;
 6 }NodeTree;
 7 NodeTree node[4*MAX];
 8 void BuildTree(int k, int l, int r)
 9 {
10     node[k].left = l;
11     node[k].right = r;
12     node[k].l_cnt = node[k].r_cnt = 0;
13     node[k].mid = (l + r) >> 1;
14     if(l == r)
15         return;
16     int mid = (l + r) >> 1;
17     BuildTree(k << 1, l, mid);
18     BuildTree(k << 1|1, mid+1, r);
19 }
20 
21 void UpdateTree(int k, int num)
22 {
23     if(node[k].left == node[k].right)
24     {
25         node[k].num = num;
26         return ;
27     }
28     if(node[k].mid < num)
29     {
30         node[k].r_cnt ++;
31         UpdateTree(k << 1|1, num);
32     }
33     else
34     {
35         node[k].l_cnt ++;
36         UpdateTree(k << 1, num);
37     }
38 }
39 
40 int GetRusult(int k, int pos)
41 {
42     if(node[k].left == node[k].right)
43         return node[k].num;
44     if(node[k].r_cnt >= pos)
45         GetRusult(k << 1|1, pos);
46     else
47         GetRusult(k << 1, pos - node[k].r_cnt);
48 }
49 
50 int main(int argc, char const *argv[]) 
51 {
52     int n, k, a, i;
53     char str[2];
54     //freopen("in.c", "r", stdin);
55     while(~scanf("%d%d", &n, &k))
56     {
57         BuildTree(1, 1, n);
58         for(i = 0;i < n;i ++)
59         {
60             scanf("%s", str);
61             if(str[0] == I)
62             {
63                 scanf("%d%d", &a);
64                 UpdateTree(1, a);
65             }
66             else
67             {
68                 printf("%d\n", GetRusult(1, k));
69             }
70         }
71     }
72     return 0;
73 }
bubuko.com,布布扣

 

 

HDU --- 4006,布布扣,bubuko.com

HDU --- 4006

原文:http://www.cnblogs.com/anhuizhiye/p/3597312.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!