woj1152 静态维护第K大

http://acm.whu.edu.cn/learn/problem/detail?problem_id=1152

武汉大学 计算机学院 本科生程序设计训练

woj1152

题意是求第K小(第K大)

n比较大,但编号范围比较小<500w,自己用桶排序(计数排数的简化版)

时间复杂度o(n)


另外还是快排修改版、堆的动态维护法


Accepted28688510C++11332016-01-24 15:15:49


/*
 * Author: NICK WONG
 * Created Time:  1/24/2016 14:56:02
 * File Name: woj1152.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
#define out(x) cout<<#x<<": "<<x<<endl
const double eps(1e-8);
const int maxn=5001000;
const long long maxint=-1u>>1;
const long long maxlong=maxint*maxint;
typedef long long lint;
int a[maxn],c[maxn];
int n,k;

void init()
{
    memset(c,0,sizeof(c));
    for (int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        c[a[i]]++;
    }
}

void work()
{
    int num=0;
    for (int i=0; i<=maxn-1; i++)
    {
        num+=c[i];
        if (num>=k)
        {
            printf("%d\n",i);
            break;
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        init();
        work();
    }
    return 0;
}

 

Problem 1152 - Feli的糖果

Time Limit:1000MS  Memory Limit: 65536KB   Difficulty: 3
Total Submit: 1820  Accepted: 473  Special Judge:No

Description

AderFelicia寄来了很多很多的好吃的糖果,Felicia给每一个糖果标记了一个可口度,每天从中选出第K好吃的糖果来吃,但是,Ader寄来的糖果实在是太多太多了,编号后也只能一股脑的放在一个盒子里,Felicia每天都要花巨量的时间来找糖果,这显然是一件折磨人的事,他想写个程序来解决这个问题,但是,万恶的期末考就要来临,Felicia不得不复习E文,只好拜托你来解决这个问题啦!

Input

题目含有多组数据,以EOF结束!
每组数据的第一行都有2个数,NkNN<=5000000)表示AderFelicia的糖果的总数,k(k<=n)Felicia想找到的第K好吃的糖果,接下来的1行里有N个正整数,Felicia把糖果从盒子里倒出来,排成一列,这些就是糖果的编号!

Output

每一组数据输出仅为一行,且只有一个数,为Felicia想找到的第K好吃的糖果的编号。

Sample Input

10 5
5 8 1 4 4 8 3 2 7 10

Sample Output

4

Hint

请大家自己实现算法,不要用STL

Source

masnake @ WHU





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值