这题其实就是一个求数组中第K大数的问题,用快速排序的思想可以解决。结果一路超时。。原来要加输入输出优化,具体优化见代码。

顺便把求数组中第K大数和求数组中第K小数的求法给出来。

代码:

/*
* this code is made by whatbeg
* Problem: 1099
* Verdict: Accepted
* Submission Date: 2014-06-15 00:13:53
* Time: 4340 MS
* Memory: 21212 KB
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <iomanip>
using namespace std;
#define N 1007 //复杂度O(n)
int Max_K(int a[],int low,int high,int k)
{
if(k <= || k > high-low+)
return -;
int flag = low + abs(rand())%(high-low+); //随机选择一个基准点
swap(a[low],a[flag]);
int mid = low;
int cnt = ;
for(int i=low+;i<=high;i++)
{
if(a[i] > a[low]) //遍历,把较大的数放在数组左边
{
swap(a[++mid],a[i]);
cnt++;
}
}
//比基准点大的数的个数为cnt-1
swap(a[mid],a[low]); //将基准点放在左、右两部分的分界处
if(cnt > k)
return Max_K(a,low,mid-,k);
else if(cnt < k)
return Max_K(a,mid+,high,k-cnt);
else
return mid;
} int Min_K(int a[],int low,int high,int k)
{
if(k <= || k > high-low+)
return -;
int flag = low + abs(rand())%(high-low+);
swap(a[low],a[flag]);
int mid = low;
int cnt = ;
for(int i=low+;i<=high;i++)
{
if(a[i] < a[low])
{
swap(a[++mid],a[i]);
cnt++;
}
}
swap(a[mid],a[low]);
if(k < cnt)
return Min_K(a,low,mid-,k);
else if(k > cnt)
return Min_K(a,mid+,high,k);
else
return mid;
} inline int in()
{
char ch;
int a = ;
while((ch = getchar()) == ' ' || ch == '\n');
a += ch - '';
while((ch = getchar()) != ' ' && ch != '\n')
{
a *= ;
a += ch - '';
}
return a;
} inline void out(int a)
{
if(a >= )
out(a / );
putchar(a % + '');
} int a[]; int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
getchar();
for(int i=;i<n;i++)
a[i] = in();
int res = Max_K(a,,n-,k);
out(a[res]);
puts("");
}
return ;
}

ACdream OJ 1099 瑶瑶的第K大 --分治+IO优化的更多相关文章

  1. ACdream 1099——瑶瑶的第K大——————【快排舍半,输入外挂】

    瑶瑶的第K大 Time Limit:2000MS     Memory Limit:128000KB     64bit IO Format:%lld & %llu Submit Status ...

  2. [ACdream 1099] 瑶瑶的第K大

    瑶瑶的第K大 Time Limit: 4000/2000MS (Java/Others) Memory Limit: 256000/128000KB (Java/Others) Problem Des ...

  3. ACdream 1099求第k大

    题目链接 瑶瑶的第K大 Time Limit: 10000/5000MS (Java/Others)Memory Limit: 512000/256000KB (Java/Others) Submit ...

  4. ACdream 1104 瑶瑶想找回文串(SplayTree + Hash + 二分)

    Problem Description 刚学完后缀数组求回文串的瑶瑶(tsyao)想到了另一个问题:如果能够对字符串做一些修改,怎么在每次询问时知道以某个字符为中心的最长回文串长度呢?因为瑶瑶整天只知 ...

  5. [ACdream]瑶瑶带你玩激光坦克

    题目链接:http://acdream.info/contest?cid=1269#problem-B Problem Description 有一款名为激光坦克的游戏,游戏规则是用一个坦克发出激光来 ...

  6. ACdream 1103 瑶瑶正式成为CEO(树链剖分+费用流)

    Problem Description 瑶瑶(tsyao)是某知名货运公司(顺丰)的老板,这个公司很大,货物运输量极大,因此公司修建了许多交通设施,掌控了一个国家的交通运输. 这个国家有n座城市,公司 ...

  7. acdream 瑶瑶带你玩激光坦克 (模拟)

    瑶瑶带你玩激光坦克 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 256000/128000KB (Java/Others) Submi ...

  8. 瑶瑶GBK好的,UTF-8卡死

    请求地址: 开发环境核心 esb : http://10.15.22.120:8866/0203000007/EmpAndDptRelateInfoSync/V1 用gbk可以马上返回. 用utf-8 ...

  9. B - 瑶瑶带你玩激光坦克

    B - 瑶瑶带你玩激光坦克 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 256000/128000KB (Java/Others) S ...

随机推荐

  1. Represent code in math equations

    Introduce The article shows a way to use math equations to represent code's logical. Key ideas logic ...

  2. [python拾遗]列表

    python列表拾遗 1.列表可以修改,使用 ‘+’ 将一个新列表附加在原列表的尾部: >>> a = [1,'a'] >>> b = a + [2,'b'] &g ...

  3. quartz使用(一)

    在项目中经常会碰到定时任务,quartz是一款非常优秀的开源框架, 提供了定时任务的支持,还支持任务的持久化,并且提供了对数据库的支持.下面首先对quartz做一个简单介绍,并附上一个小例子. 1.下 ...

  4. 总结一下SQL的全局变量

    SQL Server 2008中的全局变量及其用法 T-SQL程序中的变量分为全局变量和局部变量两类,全局变量是由SQL Server系统定义和使用的变量.DBA和用户可以使用全局变量的值,但不能自己 ...

  5. How to Install Hadoop on Ubuntu

    安装教程,https://www.digitalocean.com/community/tutorials/how-to-install-hadoop-on-ubuntu-13-10

  6. TSQL生成Combguid

    Nhibernate实现combguid /// <summary> /// Generate a new <see cref="Guid"/> using ...

  7. [ javascript canvas toDataURL() clip() ] javascript canvas toDataURL() clip() 属性及方法演示

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  8. UWP开发中的流媒体

    写这篇的目的只是为了记住这个东西, win10原生支持HLS了 AdaptiveMediaSourceCreationResult amsResult = await AdaptiveMediaSou ...

  9. Lucene总体架构

    Lucene总的来说是:• 一个高效的,可扩展的,全文检索库.• 全部用Java实现,无须配置.• 仅支持纯文本文件的索引(Indexing)和搜索(Search).• 不负责由其他格式的文件抽取纯文 ...

  10. Swift Standard Library: Documented and undocumented built-in functions in the Swift standard library – the complete list with all 74 functions

    Swift has 74 built-in functions but only seven of them are documented in the Swift book (“The Swift ...