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 ;
}
最新文章
- chrome谷歌浏览器插件制作简易教程
- vi 的使用
- chrome中hack解决input:-webkit-autofill自定义样式
- Fake chat script for website download
- ScrollView 里的 EditText 与输入法的用例
- poj 1651 Multiplication Puzzle (区间dp)
- REST实战:SeverClient项目+RESTful理论
- 仿花田:内部相亲网站 意中人(Asp.net MVC,Bootstrap2)
- 【分享】Matlab R2015a 发布啦!
- spring中的 classpath* 存在可移植性问题
- poj 2245 Lotto
- 函数lock_rec_set_nth_bit
- delphi 修改Hint的字体和颜色
- 子查询优化成join关联查询时要注意一对多关系
- lc面试准备:Number of 1 Bits
- 【C语言的日常实践(十四)】constkeyword详细解释
- Unix系统使用的地址索引结构有什么特点?
- ConcurrentDictionary与Dictionary 替换
- Winhex数据恢复学习笔记(四)
- hive hbase区别