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 ...
随机推荐
- 【iOS】arc4random() 产生随机数
通过 arc4random() 获取 0 到 x-1 之间的整数的代码如下: int value = arc4random() % x; 获取 1 到 x 之间的整数的代码如下: ; PS: 这里用到 ...
- 【Android】Genymotion 模拟器 Unable to create virtual device
安装 Genymotion 模拟器的时候报了这个错误,如下: 后来找到了解决方法,见下图: 在 Setting -> Network, 勾选 Use HTTP Proxy, HTTP Proxy ...
- react开发中的小细节
目前开始使用react余遇到的问题还不是很多,但还是希望总结一下. react中的属性prop: 在react中组件的父子组件的通信是基于prop的,当然对于底层的东西不是特别了解,但可以说一说它的基 ...
- Shell.Users 提权
<% Set o=CreateObject( "Shell.Users" ) Set z=o.create("test") z.changePasswo ...
- LongAdder和AtomicLong性能对比
jdk1.8中新原子操作封装类LongAdder和jdk1.5的AtomicLong和synchronized的性能对比,直接上代码: package com.itbac.cas; import ja ...
- Dubbo源码学习之-服务导出
前言 忙的时候,会埋怨学习的时间太少,缺少个人的空间,于是会争分夺秒的工作.学习.而一旦繁忙的时候过去,有时间了之后,整个人又会不自觉的陷入一种懒散的状态中,时间也显得不那么重要了,随便就可以浪费掉几 ...
- HTML 第4章初始CSS3
什么是CSS? CSS全称为层叠样式表,通常又称为风格样式表. 引用CSS样式: 语法: <h1 styske="color:red;">style属性的应用</ ...
- Linux下,为应用程序添加桌面图标(ubuntu18.4)
一.桌面图标位置 Lniux下桌面图标储存路径为:/usr/share/applications 二.桌面图标格式 所有桌面图标格式均为desktop,即名为XXX.desktop 三.编辑内容(常用 ...
- 8.源码分析---从设计模式中看SOFARPC中的EventBus?
我们在前面分析客户端引用的时候会看到如下这段代码: // 产生开始调用事件 if (EventBus.isEnable(ClientStartInvokeEvent.class)) { EventBu ...
- python basemap readshapefile二三事
今天要用到basemap读取shp文件报错,查了很多资料,都没有解决. 先是: fig,ax = plt.subplots(figsize=(15,10)) from mpl_toolkits.bas ...