Hints of sd0061(快排思想)
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
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.
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;
}
#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(快排思想)的更多相关文章
- 如何用快排思想在O(n)内查找第K大元素--极客时间王争《数据结构和算法之美》
前言 半年前在极客时间订阅了王争的<数据结构和算法之美>,现在决定认真去看看.看到如何用快排思想在O(n)内查找第K大元素这一章节时发现王争对归并和快排的理解非常透彻,讲得也非常好,所以想 ...
- HDU 5696 ——区间的价值——————【线段树、快排思想】
区间的价值 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- 基于快排思想的第(前)k大(小)
算法思路就是根据快排的partition,先随机选择一个分隔元素(或a[0]),将数组分为[小于a[p]的元素] a[p] [大于a[p]的元素],如果这时候n-p+1等于k的话,a[p]就是所求的第 ...
- java快排思想
1分治思想 1.1比大小在分区 1.2从数组中取出一个数做基准数 1.3将比他小的数全放在他的左边,比他大的数全放在他的右边 1.4然后递归 左边 和右边 }
- 2018.4.24 快排查找第K大
import java.util.Arrays; /* 核心思想:利用快排思想,先假定从大到小排序,找枢纽,枢纽会把大小分开它的两边,当枢纽下标等于k时, 即分了k位在它左边或右边,也就是最大或最小的 ...
- [剑指Offer]39-数组中出现次数超过一半的数字(快排延申,找第k大数同理)
题目链接 https://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163?tpId=13&tqId=11181&t ...
- 215 Kth Largest Element in an Array 快排
题目:在无序的数组中找到第k大的元素,也就是若长度为n的数组从小到大排列时,下标为n-k的元素. 注意Example2:第4大的元素是4,也就是数组中出现的两个5分别是第2大和第3大的数字. 解法一: ...
- 快速排序详解(lomuto划分快排,hoare划分快排,classic经典快排,dualpivot双轴快排源码)
目录 快速排序(lomuto划分快排,hoare划分快排,classic经典快排,dualpivot双轴快排) 一.快速排序思想 二.划分思想 三.测试用例 快速排序(lomuto划分快排,hoare ...
- C++ 由快排学习到的的随机数等知识
起: 力扣的912题 数组排序 ,想着先用快速排序来写写,在实际用c++编写的时候,有一些之前没注意到的细节问题造成了一些麻烦. 912. 排序数组 - 力扣(LeetCode) 快排思想 每次以数组 ...
随机推荐
- ant design pro 表单
1.Input Enter事件 <input onKeyUp={this.onKeyUp} onPressEnter={this.enter} /> onKeyUp = (e) => ...
- zoj How Many Sets I(组合计数)
http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4535 一个集合s有n个元素,求满足这种集合序列{s1,s2....sk}使S ...
- Apache代理80端口
找到Apache下的conf\extra\httpd-vhosts.conf文件 新增以下内容于合适位置 ↓表示80端口 <VirtualHost *:80> ServerAdmin * ...
- jQuery unbind() 方法
jQuery 中的 unbind() 方法是 bind() 方法的反向操作,从每一个匹配的元素中删除绑定的事件. 语法结构: unbind([type][, data]); type是事件类型,dat ...
- SVN学习(一)——SVN 检出文件步骤、图标显示及含义
May, I come... 1. 创建一个目录用来存放检出得到的文件,例如MyCRM 2. 直接进入目录MyCRM,点右键 3. 可以看到检出得到的文件 此时文件图标上没有任何标识.可能你会想到通过 ...
- Spring Boot整合shiro-登录认证和权限管理
原文地址:http://www.ityouknow.com/springboot/2017/06/26/springboot-shiro.html 这篇文章我们来学习如何使用Spring Boot集成 ...
- pandas 绘图与滑窗
#import nessary library before start import pandas as pd import numpy as np import matplotlib.pyplot ...
- 通过xsd schema结构来验证xml是否合法
import sys import StringIO import lxml from lxml import etree from StringIO import StringIO # Constr ...
- linux学习一个服务(未完)
学习一个服务的过程 1.了解服务的作用:名字 功能,特点 2.安装 3.配置文件位置,端口 4.服务启动关闭的脚本 5.此服务的使用方法 6.修改配置文件,实战举例 7.排错(从下到上,从内到外)
- oracle10g安装,卸载
一.安装 1.因为oracle的特殊性,笔者选择通过虚拟机安装windows7旗舰版安装数据库,大家的系统假设是windows xp.windows 7,windows 8能够直接安装,windows ...