Hints of sd0061

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 492    Accepted Submission(s): 106

Problem Description
sd0061, the legend of Beihang University ACM-ICPC Team, retired last year leaving a group of noobs. Noobs have no idea how to deal with m coming contests.sd0061 has left a set of hints for them.

There are n noobs in the team, the i-th of which has a rating ai. sd0061 prepares one hint for each contest. The hint for the j-th contest is a number bj, which means that the noob with the (bj+1)-th lowest rating is ordained by sd0061 for the j-th contest.

The coach asks constroy to make a list of contestants. constroy looks into these hints and finds out: bi+bj≤bk is satisfied if bi≠bj, bi<bk and bj<bk.

Now, you are in charge of making the list for constroy.

 
Input
There are multiple test cases (about 10).

For each test case:

The first line contains five integers n,m,A,B,C. (1≤n≤107,1≤m≤100)

The second line contains m integers, the i-th of which is the number bi of the i-th hint. (0≤bi<n)

The n noobs' ratings are obtained by calling following function n times, the i-th result of which is ai.

unsigned x = A, y = B, z = C;
unsigned rng61() {
  unsigned t;
  x ^= x << 16;
  x ^= x >> 5;
  x ^= x << 1;
  t = x;
  x = y;
  y = z;
  z = t ^ x ^ y;
  return z;
}
 
Output
For each test case, output "Case #x: y1 y2 ⋯ ym" in one line (without quotes), where x indicates the case number starting from 1 and yi (1≤i≤m)denotes the rating of noob for the i-th contest of corresponding case.
 
Sample Input
3 3 1 1 1
0 1 2
2 2 2 2 2
1 1
 
Sample Output
Case #1: 1 1 202755
Case #2: 405510 405510
 
Source
 
 
//题意: n,m,a,b,c abc为3个参数,用这些参数生成n个数,然后,求排好序后的 n 个数中,m 次查询,第 i 的数的值
 
//题解:这种数据下 10e7 ,不能直接用快排,就算快排,还是会超时,但是要利用快排的思想。因为 m 比较小,可以这样,每次快排结束后,前部分一定比分界值小或等,后半部分一定大或等,要查询第 k 大的数时,如果 k 在分界位置左边,就只排左边,在右就只排右,这样,每阶段快排省去一半区间,就行了,还wa了一发,很蛋疼,因为生成数据那部分不能直接用LL,只能用题目给的unsigned ,卡数据,很难玩那!
但是还是差点超时 2300ms ,评测机卡还有可能TLE,我擦,让我再想想怎么优化
 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define LL long long
#define MXN 10000005
#define MXM 105 int num[MXM];
LL prt[MXM];
LL data[MXN];
bool vis[MXN]; int n,m;
unsigned x,y,z;
unsigned rng61(){
LL t;
x ^= x << ;
x ^= x >> ;
x ^= x << ;
t = x;
x = y;
y = z;
z = t ^ x ^ y;
return z;
} void qk_sort(int l,int r,int k)
{
if (l>=r) return;
int a=l, b=r;
while (a<b)
{
while (a<b&&data[b]>=data[l]) b--;
while (a<b&&data[a]<=data[l]) a++;
swap(data[a],data[b]);
}
swap(data[l],data[a]);
vis[a]=;
if (k==a) return;
if (k<a) qk_sort(l,a-,k);
if (k>a) qk_sort(a+,r,k);
} int main()
{
int cnt=;
while (scanf("%d%d%u%u%u",&n,&m,&x,&y,&z)!=EOF)
{
for (int i=;i<m;i++)
scanf("%d",&num[i]);
for (int i=;i<n;i++)
data[i]=(LL)rng61(); memset(vis,,sizeof(vis)); for (int i=;i<m;i++)
{
int l =num[i],r=num[i];
while (l>=&&vis[l]==) l--;
l++;
while (r<n&&vis[r]==) r++;
r--;
qk_sort(l,r,num[i]);
prt[i] = data[num[i]];
}
printf("Case #%d:",cnt++);
for (int i=;i<m;i++)
printf(" %lld",prt[i]);
printf("\n");
}
return ;
}

STL   里面的   nth_element(arr,arr+x,arr+n);

可以将x在[0,n]范围内,将第x小的数字移动到arr[x]上,其余比arr[x]大的,在x后面,比arr[x]小的,在x前面。

1684 ms

STL 用得好省事多啊

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
#define MXN 10000005
#define MXM 105
struct Node
{
int w;
int p;
bool operator <(const Node b) const
{
return w<b.w;
}
}num[MXM];
LL prt[MXM];
LL data[MXN];
bool vis[MXN]; int n,m;
unsigned x,y,z;
unsigned rng61(){
LL t;
x ^= x << ;
x ^= x >> ;
x ^= x << ;
t = x;
x = y;
y = z;
z = t ^ x ^ y;
return z;
} int main()
{
int cnt=;
while (scanf("%d%d%u%u%u",&n,&m,&x,&y,&z)!=EOF)
{
for (int i=;i<m;i++)
{
scanf("%d",&num[i].w);
num[i].p=i;
}
sort(num,num+m); for (int i=;i<n;i++)
data[i]=(LL)rng61(); memset(vis,,sizeof(vis)); for (int i=m-;i>=;i--)
{
if (i==m-)
nth_element(data,data+num[i].w,data+n);
else
nth_element(data,data+num[i].w,data+num[i+].w);
prt[num[i].p] = data[num[i].w];
}
printf("Case #%d:",cnt++);
for (int i=;i<m;i++)
printf(" %lld",prt[i]);
printf("\n");
}
return ;
}

Hints of sd0061(快排思想)的更多相关文章

  1. 如何用快排思想在O(n)内查找第K大元素--极客时间王争《数据结构和算法之美》

    前言 半年前在极客时间订阅了王争的<数据结构和算法之美>,现在决定认真去看看.看到如何用快排思想在O(n)内查找第K大元素这一章节时发现王争对归并和快排的理解非常透彻,讲得也非常好,所以想 ...

  2. HDU 5696 ——区间的价值——————【线段树、快排思想】

    区间的价值 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  3. 基于快排思想的第(前)k大(小)

    算法思路就是根据快排的partition,先随机选择一个分隔元素(或a[0]),将数组分为[小于a[p]的元素] a[p] [大于a[p]的元素],如果这时候n-p+1等于k的话,a[p]就是所求的第 ...

  4. java快排思想

    1分治思想 1.1比大小在分区 1.2从数组中取出一个数做基准数 1.3将比他小的数全放在他的左边,比他大的数全放在他的右边 1.4然后递归 左边 和右边 }

  5. 2018.4.24 快排查找第K大

    import java.util.Arrays; /* 核心思想:利用快排思想,先假定从大到小排序,找枢纽,枢纽会把大小分开它的两边,当枢纽下标等于k时, 即分了k位在它左边或右边,也就是最大或最小的 ...

  6. [剑指Offer]39-数组中出现次数超过一半的数字(快排延申,找第k大数同理)

    题目链接 https://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163?tpId=13&tqId=11181&t ...

  7. 215 Kth Largest Element in an Array 快排

    题目:在无序的数组中找到第k大的元素,也就是若长度为n的数组从小到大排列时,下标为n-k的元素. 注意Example2:第4大的元素是4,也就是数组中出现的两个5分别是第2大和第3大的数字. 解法一: ...

  8. 快速排序详解(lomuto划分快排,hoare划分快排,classic经典快排,dualpivot双轴快排源码)

    目录 快速排序(lomuto划分快排,hoare划分快排,classic经典快排,dualpivot双轴快排) 一.快速排序思想 二.划分思想 三.测试用例 快速排序(lomuto划分快排,hoare ...

  9. C++ 由快排学习到的的随机数等知识

    起: 力扣的912题 数组排序 ,想着先用快速排序来写写,在实际用c++编写的时候,有一些之前没注意到的细节问题造成了一些麻烦. 912. 排序数组 - 力扣(LeetCode) 快排思想 每次以数组 ...

随机推荐

  1. ant design pro 表单

    1.Input Enter事件 <input onKeyUp={this.onKeyUp} onPressEnter={this.enter} /> onKeyUp = (e) => ...

  2. zoj How Many Sets I(组合计数)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4535 一个集合s有n个元素,求满足这种集合序列{s1,s2....sk}使S ...

  3. Apache代理80端口

    找到Apache下的conf\extra\httpd-vhosts.conf文件 新增以下内容于合适位置 ↓表示80端口 <VirtualHost *:80>  ServerAdmin * ...

  4. jQuery unbind() 方法

    jQuery 中的 unbind() 方法是 bind() 方法的反向操作,从每一个匹配的元素中删除绑定的事件. 语法结构: unbind([type][, data]); type是事件类型,dat ...

  5. SVN学习(一)——SVN 检出文件步骤、图标显示及含义

    May, I come... 1. 创建一个目录用来存放检出得到的文件,例如MyCRM 2. 直接进入目录MyCRM,点右键 3. 可以看到检出得到的文件 此时文件图标上没有任何标识.可能你会想到通过 ...

  6. Spring Boot整合shiro-登录认证和权限管理

    原文地址:http://www.ityouknow.com/springboot/2017/06/26/springboot-shiro.html 这篇文章我们来学习如何使用Spring Boot集成 ...

  7. pandas 绘图与滑窗

    #import nessary library before start import pandas as pd import numpy as np import matplotlib.pyplot ...

  8. 通过xsd schema结构来验证xml是否合法

    import sys import StringIO import lxml from lxml import etree from StringIO import StringIO # Constr ...

  9. linux学习一个服务(未完)

    学习一个服务的过程 1.了解服务的作用:名字 功能,特点 2.安装 3.配置文件位置,端口 4.服务启动关闭的脚本 5.此服务的使用方法 6.修改配置文件,实战举例 7.排错(从下到上,从内到外)

  10. oracle10g安装,卸载

    一.安装 1.因为oracle的特殊性,笔者选择通过虚拟机安装windows7旗舰版安装数据库,大家的系统假设是windows xp.windows 7,windows 8能够直接安装,windows ...