HDU 4006 The kth great number

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
分析:

①输入 第一个数 p,令res=p

②输入前k个数 若输入的数<res 令res=输入的数

③输入后面的行 若Q 输出res 若输入的数大于或便是res res向后跳一个数 不然res不动

 一二过程中pos=1;

三过程中先跳pos若pos>f[res] 则让res跳到int数组的下一个不为0处 pos=1;

code:

View Code
#include<stdio.h>
#include<string.h>
int q[1000000];
int main()
{
int t,i,j,k,pos,ans,p,res;
char s[2];
while(scanf("%d%d",&t,&k)!=EOF)
{
memset(q,0,sizeof(q));
pos=1;
scanf("%s%d",s,&res);
q[res]++;
for(i=2;i<=k;i++)
{
scanf("%s%d",s,&p);
q[p]++;
res=res<p?res:p;
}
for(i=k+1;i<=t;i++)
{
scanf("%s",s);
if(s[0]=='Q')
printf("%d\n",res);
else
{
scanf("%d",&p);
q[p]++;
if(p>=res)
{
pos++;
if(pos>q[res])
for(j=res+1;j<=100000;j++)
if(q[j]>0)
{
res=j;
pos=1;
break;
}
}
}
}
}
return 0;
}


posted @ 2012-03-14 17:53  'wind  阅读(432)  评论(0编辑  收藏  举报