ACdream OJ 1099 瑶瑶的第K大 --分治+IO优化
这题其实就是一个求数组中第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优化的更多相关文章
- ACdream 1099——瑶瑶的第K大——————【快排舍半,输入外挂】
瑶瑶的第K大 Time Limit:2000MS Memory Limit:128000KB 64bit IO Format:%lld & %llu Submit Status ...
- [ACdream 1099] 瑶瑶的第K大
瑶瑶的第K大 Time Limit: 4000/2000MS (Java/Others) Memory Limit: 256000/128000KB (Java/Others) Problem Des ...
- ACdream 1099求第k大
题目链接 瑶瑶的第K大 Time Limit: 10000/5000MS (Java/Others)Memory Limit: 512000/256000KB (Java/Others) Submit ...
- ACdream 1104 瑶瑶想找回文串(SplayTree + Hash + 二分)
Problem Description 刚学完后缀数组求回文串的瑶瑶(tsyao)想到了另一个问题:如果能够对字符串做一些修改,怎么在每次询问时知道以某个字符为中心的最长回文串长度呢?因为瑶瑶整天只知 ...
- [ACdream]瑶瑶带你玩激光坦克
题目链接:http://acdream.info/contest?cid=1269#problem-B Problem Description 有一款名为激光坦克的游戏,游戏规则是用一个坦克发出激光来 ...
- ACdream 1103 瑶瑶正式成为CEO(树链剖分+费用流)
Problem Description 瑶瑶(tsyao)是某知名货运公司(顺丰)的老板,这个公司很大,货物运输量极大,因此公司修建了许多交通设施,掌控了一个国家的交通运输. 这个国家有n座城市,公司 ...
- acdream 瑶瑶带你玩激光坦克 (模拟)
瑶瑶带你玩激光坦克 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 256000/128000KB (Java/Others) Submi ...
- 瑶瑶GBK好的,UTF-8卡死
请求地址: 开发环境核心 esb : http://10.15.22.120:8866/0203000007/EmpAndDptRelateInfoSync/V1 用gbk可以马上返回. 用utf-8 ...
- B - 瑶瑶带你玩激光坦克
B - 瑶瑶带你玩激光坦克 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 256000/128000KB (Java/Others) S ...
随机推荐
- sqlite3之基本操作(二)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python自带一个轻量级的关系型数据库SQLite.这一数据库使用SQL语言.S ...
- Window下生成OpenSSL自签证书
:OPenSSL下载地址:https://www.openssl.org/source/ 编译好的OpenSSL下载地址: http://slproweb.com/products/Win32Open ...
- python 学习之电脑的发展历史
电脑的发展历史 电脑的学名叫计算机,电脑是用来做计算的.在古时候,人们最早使用的计算工具可能是手指,英文单词“digit”既有“数字”的意思,又有“手指“的意思.古人用石头打猎,所以还有可能是石头来辅 ...
- 【GPU编解码】GPU硬解码---DXVA
前面介绍利用NVIDIA公司提供的CUVID库进行视频硬解码,下面将介绍利用DXVA进行硬解码. 一.DXVA介绍 DXVA是微软公司专门定制的视频加速规范,是一种接口规范.DXVA规范制定硬件加速解 ...
- The system clock has been set back more than 24 hours
由于破解调试需要,更改了系统时间,打开ArcMap会出现"The system clock has been set back more than 24 hours"的错误,原因是 ...
- SharePoint 2013中Office Web Apps的一次排错
转自http://www.cnblogs.com/awpatp/archive/2013/06/06/3121420.html, 仅供自己查看 笔者尝试在自己的测试环境中为SharePoint 201 ...
- Android Studio 插件的使用
1.GsonFormat https://github.com/zzz40500/GsonFormat 2.Android SelectorChapek http://blog.csdn.ne ...
- 触发layoutSubviews的条件
1. init初始化不会触发layoutSubviews 2. addSubview会触发layoutSubviews 3. 设置view的Frame会触发layoutSubviews,当然前提是fr ...
- Class org.apache.struts2.json.JSONWriter can not access a member of class org.springframework.aop.TruePointcut with modifiers "public"
Spring注入Action使用Json错误:org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: ...
- iOS多线程初见
. 三种创建线程的方法 //第一种 NSThread * thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(doAc ...