| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 958 人关注过本帖
标题:大家帮忙看一下这道题目,总是tle
取消只看楼主 加入收藏
枫叶无痕
Rank: 2
等 级:论坛游民
帖 子:80
专家分:30
注 册:2011-2-10
结帖率:73.91%
收藏
 问题点数:0 回复次数:2 
大家帮忙看一下这道题目,总是tle
The kth great number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)

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

Hint
Xiao  Ming  won't  ask  Xiao  Bao  the  kth  great  number  when  the  number  of  the  written number is smaller than k. (1=<k<=n<=1000000).
搜索更多相关主题的帖子: game Because feeling playing several 
2011-09-03 15:34
枫叶无痕
Rank: 2
等 级:论坛游民
帖 子:80
专家分:30
注 册:2011-2-10
收藏
得分:0 
回复 2楼 落叶深蓝色
你是怎么的思路??
2011-09-03 16:02
枫叶无痕
Rank: 2
等 级:论坛游民
帖 子:80
专家分:30
注 册:2011-2-10
收藏
得分:0 
我做出来了,给你们代码吧
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int maxn=1000002;
int num[maxn];
int n,k;
int cmp(const void *x,const void *y)
{
    return *(int*)y-*(int*)x;
}
void wp(int p,int q)
{
    int i;
    for(i=k-1;i>=p;i--)
    {
        num[i+1]=num[i];
    }
    num[p]=q;
}
int main()
{
    int i,j,s,f,q;
    char str;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        j=0;
        getchar();
        memset(num,0,sizeof(num));
        while((str=getchar())!=EOF)
        {
            if(str=='I')
            {
                scanf("%d",&q);
                num[j]=q;
                j++;
            }
            else
            {
                qsort(num,j,sizeof(num[0]),cmp);
                s=num[k-1];
                printf("%d\n",s);
                getchar();
                break;
            }
            getchar();
        }
        for(i=j+1;i<n;i++)
        {
            str=getchar();
            if(str=='I')
            {
                scanf("%d",&q);
                if(q>s)
                {
                    if(q>=num[0])
                    {
                        wp(0,q);
                    }
                    else
                    {
                        for(f=k-1;f>0;f--)
                        {
                            if(q>num[f]&&q<=num[f-1])
                            {
                                wp(f,q);
                            }
                        }
                    }
                    s=num[k-1];
                }
            }
            else
            printf("%d\n",s);
            getchar();
        }
    }
    return 0;
}
2011-09-03 16:22
快速回复:大家帮忙看一下这道题目,总是tle
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030041 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved