Gym 100956 A Random Points on the Circle
二分答案。
对于每次二分后的答案来说,
先倍增序列,通过 two point 来找到 以每个点为起点的最优的符合答案的在哪里。
然后可以DFS树去判断他的前k祖先之间的距离是不是大于k。 常数有点大。
在 ID为 WuHongxun 的代码下学会了, 先判断一下最小一段的跨度, 如果跨度 >= n / k + 1 那么直接就是合法的了。
否则枚举最小跨度内的点作为起点, 直接走k步, 判断一下是否合法。
因为假定最小跨度的区间为 [l, r]。
必定不存在一个合法区间[a, b] 使得 a < l && b > r。 所以因为没有一种区间方式覆盖这个区间, 故在这个区间内一定会有一个点被作为起点。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 2e6 + ;
LL L = << ;
int n, k, seed, add;
LL d[N], s[N];
int nt[N];
inline LL cal(int l, int r){
return (s[r] - s[l+r>>]) - (s[(l+r-)>>] - s[l-]);
}
bool check(LL x){
int mn = * n + , id;
for(int i = , j = ; i <= n * ; ++i){
while(j <= * n && cal(i,j) <= x) ++j;
nt[i] = j;
if(i <= n && j-i < mn){
mn = j - i;
id = i;
}
}
if(mn >= n/k + ) return true;
for(int i = id, ed = nt[i]%n+; i != ed; i = i%n +){
int z = i;
for(int j = ; j <= k; ++j){
z = nt[z];
if(i + n <= z) return true;
}
}
return false; }
void Ac(){
scanf("%d%d", &n, &k);
scanf("%d%d", &seed, &add);
for(int i = ; i <= n; ++i){
seed = (seed * + add) & (L-);
d[i] = seed;
}
sort(d+, d++n);
for(int i = ; i <= n; ++i)
d[i+n] = d[i] + L;
for(int i = ; i <= *n; ++i)
s[i] = s[i-] + d[i];
LL l = , r = L * n / k;
while(l <= r){
LL mid = l+r >> ;
if(!check(mid)) l = mid + ;
else r = mid - ;
}
cout << l << endl;
return ;
}
int main(){
Ac();
return ;
}
Gym 100956 A Random Points on the Circle的更多相关文章
- [Swift]LeetCode478. 在圆内随机生成点 | Generate Random Point in a Circle
Given the radius and x-y positions of the center of a circle, write a function randPoint which gener ...
- [LeetCode] Generate Random Point in a Circle 生成圆中的随机点
Given the radius and x-y positions of the center of a circle, write a function randPoint which gener ...
- 【LeetCode】478. Generate Random Point in a Circle 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/generate ...
- 478. Generate Random Point in a Circle
1. 问题 给定一个圆的半径和圆心坐标,生成圆内点的坐标. 2. 思路 简单说 (1)在圆内随机取点不好做,但是如果画出这个圆的外接正方形,在正方形里面采样就好做了. (2)取两个random确定正方 ...
- [LeetCode] Random Pick with Blacklist 带黑名单的随机选取
Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...
- [LeetCode] Random Flip Matrix 随机翻转矩阵
You are given the number of rows n_rows and number of columns n_cols of a 2D binary matrix where all ...
- [LeetCode] Random Point in Non-overlapping Rectangles 非重叠矩形中的随机点
Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly ...
- 【Checkio Exercise】Three Point Circle
计算三角形外接圆的函数: Three Point Circle If we want to build new silos, then we need to make more formal and ...
- 519. Random Flip Matrix(Fisher-Yates洗牌算法)
1. 问题 给定一个全零矩阵的行和列,实现flip函数随机把一个0变成1并返回索引,实现rest函数将所有数归零. 2. 思路 拒绝采样 (1)先计算矩阵的元素个数(行乘以列),记作n,那么[0, n ...
随机推荐
- Django安装 测试、导入项目以及运行开发服务器
安装Django 下载Django包,解压缩. CMD 进入解压路径下. 执行:python setup.py install 增加环境变量: C:\Python27\Scripts 测试djang ...
- Maven安装和配置环境变量
Maven配置 1.下载 下载maven 3.5.4 先到官网http://maven.apache.org/download.cgi 下载最新版本(目前是3.5.4 ),下载完成后,解压到某个目录( ...
- linux下mysql无法看到3306端口监听
这个问题搞定了,原因是我的my.cnf有话:skip-network注释掉,然后运行netstat -an|grep 3306 就可以看到了
- [Abp vNext 源码分析] - 7. 权限与验证
一.简要说明 在上篇文章里面,我们在 ApplicationService 当中看到了权限检测代码,通过注入 IAuthorizationService 就可以实现权限检测.不过跳转到源码才发现,这个 ...
- 图像反转(一些基本的灰度变换函数)基本原理及Python实现
1. 基本原理 获取像素值在[0, L]范围内的图像的反转图像,即为负片.适用于增强图像中白色或者灰色的区域,尤其当黑色在图片中占主地位时候 $$T(r) = L-r$$ 2. 运行结果 图源自ski ...
- redis缓存介绍以及常见问题浅析
# 没缓存的日子: 对于web来说,是用户量和访问量支持项目技术的更迭和前进.随着服务用户提升.可能会出现一下的一些状况: 页面并发量和访问量并不多,mysql足以支撑自己逻辑业务的发展.那么其实可以 ...
- 【Java例题】2.1复数类
1.定义复数类,包括实部和虚部变量.构造方法. 加减乘除方法.求绝对值方法和显示实部.虚部值的方法. 然后编写一个主类,在其主方法中通过定义两个复数对象来 显示每一个复数的实部值.虚部值和绝对值, 显 ...
- .Net Core in Docker - 使用阿里云Codepipeline及阿里云容器镜像服务实现持续集成(CI)
前面已经介绍过了 .Net Core 程序发布到 Docker 容器的内容.但是每次通过 SSH 链接到服务器敲命令,运行脚本也是挺麻烦的一件事.程序员是最懒的,能让电脑解决的问题绝不手动解决,如果当 ...
- 关于 java中的换行符
java中实现换行有以下3种方法: 1.使用java中的转义符"\r\n": String str="aaa"; str+="\r\n"; ...
- Linux系统与程序监控工具atop教程
引言 Linux以其稳定性,越来越多地被用作服务器的操作系统(当然,有人会较真地说一句:Linux只是操作系统内核:).但使用了Linux作为底层的操作系统,是否我们就能保证我们的服务做到7*24地稳 ...