Gym 101667I Slot Machines
题意:给定n(n≤106)个数,要求将它化为混偱环小数的形式,即前k个数不参与循环,之后所有数以p为循环节长度进行循环。求k和p,要求k+p尽量小,k+p相等时要求p尽量小。
样例1
输入:
6
612534 3157 423 3157 423 3157
输出:
1 2
样例2
输入:
9
1 2 1 3 1 2 1 3 1
输出:
0 4
分析:想了两个晚上,没有想到有什么数据结构可以支持这种操作,想要二分答案又不满足单调性,于是去请教TJW,TJW一语点醒梦中人:
(还是log n的?,不大清楚)。那么直接暴力枚举p,然后均摊O(ln n)验证一下即可。以上是TJW的原话,具体细节还要处理一下:比如如何O(1)比对两段数?哈希即可,具体写法是设一个base数组,base[i]表示哈希常数key的i次方,再开一个hash数组,hash[i]表示前i位的哈希值,具体构造方法是,hash[i]=hash[i-1]*key。然后求l-r的hash值时只要求hash[r]-hash[l-1]*base[r-l+1]。除此以外,还有一个问题:循环节的开头(结尾)不一定是完整的,如何用较小的复杂度计算这段不完整的开头的长度?如果直接一个一个判断,整体复杂度就退化为O(n^2)了。这里我的解决方法是二分(刚好满足单调性),整体复杂度大概是O(nlogn + nlnn)。
/* Gym 101667I Slot Machines
1st Edition:2018.1.13 Saturday
Algorithm:Simulation
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cctype>
using namespace std; #define is_lower(c) (c>='a' && c<='z')
#define is_upper(c) (c>='A' && c<='Z')
#define is_alpha(c) (is_lower(c) || is_upper(c))
#define is_digit(c) (c>='0' && c<='9')
#define stop system("PAUSE")
#define ForG(a,b,c) for(int (a)=c.head[b];(a);(a)=c.E[a].nxt)
#define For(a,b,c) for(int (a)=(b);(a)<=(c);++a)
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define shl(x,y) ((x)<<(y))
#define shr(x,y) ((x)>>(y))
#define mp make_pair
#define pb push_back
#ifdef ONLINE_JUDGE
#define hash rename_hash
#define next rename_next
#define prev rename_prev
#endif
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef double db;
const ll inf=2000000007LL;
const double EPS=1e-;
const ll inf_ll=(ll)1e18;
const ll maxn=1000005LL;
const ll mod=1000000007LL; int n;
int a[maxn]; ull base[maxn];
ull hash[maxn];
const ll hash_base=; inline ull get_hash(int l,int r){
return (hash[r]-hash[l-]*base[r-l+]);
} int main(){
scanf("%d",&n);
base[]=;
For(i,,n) base[i]=base[i-]*hash_base;
For(i,,n){
scanf("%d",a+i);
hash[i]=hash[i-]*hash_base+a[i];
}
/*
For(i,1,n) printf("%llu ",hash[i]);
puts("");
*/
int ansk=inf>>,ansp=inf>>;
For(i,,n){
int pos=n;
ull nhash=get_hash(n-i+,n);
while(pos-i>=){
if(get_hash(pos-i+,pos)!=nhash) break;
pos-=i;
}
int l=,r=i+,mid,res=;
while(l<r){
mid=(l+r)>>;
if(pos-mid+<= || get_hash(pos-mid+,pos)!=get_hash(n-mid+,n)) r=mid;
else{l=mid+;res=mid;}
}
pos-=res;
if(i+pos<ansk+ansp){
ansk=pos;
ansp=i;
}
// printf("%d %d\n",pos,i);
}
printf("%d %d\n",ansk,ansp);
return ;
} /*
6
1 2 3 4 3 4 6
1 2 3 4 5 6 6
1 2 3 4 1 29 6
612534 3157 423 3157 423 3157 9
1 2 1 3 1 2 1 3 1 */
细节还要看代码
UPD.调和级数O(ln n) QwQ
Gym 101667I Slot Machines的更多相关文章
- 用KMP征服循环节问题
以前我还是写过KMP的文章的 现在我们可以求一下循环节啊 Slot Machines Gym - 101667I #include<bits/stdc++.h> using namespa ...
- 【转载】Bandits for Recommendation Systems (Part I)
[原文链接:http://engineering.richrelevance.com/bandits-recommendation-systems/.] [本文链接:http://www.cnblog ...
- 2017-2018 ACM-ICPC, Asia Daejeon Regional Contest Solution
A:Broadcast Stations 留坑. B:Connect3 题意:四个栈,每次放棋子只能放某个栈的栈顶,栈满不能放,现在给出(1, x) 表示黑子放在第x个栈的第一个位置,白子放在第b个栈 ...
- 2017 ACM ICPC Asia Regional - Daejeon
2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...
- 2017-2018 ACM-ICPC, Asia Daejeon Regional Contest
题目传送门 只打了三个小时. A. Broadcast Stations B. Connect3 补题:zz 题解:因为格子是4*4的,而且每次落子的位置最多是只有四个,再加上剪枝,情况不会很多,直接 ...
- 2017-2018 ACM-ICPC, Asia Daejeon Regional Contest PART(10/12)
$$2017-2018\ ACM-ICPC,\ Asia\ Daejeon\ Regional\ Contest$$ \(A.Broadcast\ Stations\) \(B.Connect3\) ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
- PatentTips - Method for network interface sharing among multiple virtual machines
BACKGROUND Many computing systems include a network interface card (NIC) to provide for communicatio ...
随机推荐
- 配置SQL Server on Linux(2)
1. 前言 前一篇配置SQL Server on Linux(1),地址:http://www.cnblogs.com/fishparadise/p/8125203.html ,是关于更改数据库排序规 ...
- Django实战,小网站实现增删改查
直接上代码 视图: from django.shortcuts import render,render_to_response, redirect from submit import models ...
- Mybatis入门(一)之操作数据库
Whats Mybatis 持久层框架, 替代MVC层中DAO,因为DAO 层的需求就是 :能与数据库交互的对象. 能执行SQL语句. 不同于JDBC的connection,MyBatis 中有个Sq ...
- Effective Java 第三版——10. 重写equals方法时遵守通用约定
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- web基础笔记整理(一)
一.程序的分层 1.界面层: 某种类型的应用程序 a.DOS(控制台运行) b.桌面应用程序--独立安装,独立运行 c.web类型--现在流行的 单机版:电脑上要安装,程序升级之后,电脑上也要升级-- ...
- SDP(0):Streaming-Data-Processor - Data Processing with Akka-Stream
再有两天就进入2018了,想想还是要准备一下明年的工作方向.回想当初开始学习函数式编程时的主要目的是想设计一套标准API給那些习惯了OOP方式开发商业应用软件的程序员们,使他们能用一种接近传统数据库软 ...
- MySQL5.6中date和string的转换和比较
Conversion & Comparison, involving strings and dates in MySQL 5.6 我们有张表,表中有一个字段dpt_date,SQL类型为da ...
- Handwritten Parsers & Lexers in Go (Gopher Academy Blog)
Handwritten Parsers & Lexers in Go (原文地址 https://blog.gopheracademy.com/advent-2014/parsers-lex ...
- Java_Date_02_截断日期到日
oracle 的 trunc 函数能很方便的将日期截断.现在有个需求,需要用java实现与 oracle 的 trunc 函数 相同的功能. 1.需求:将日期截断到日 即 将格式为 2018-01-0 ...
- Java设计模式之(一)------单例模式
1.什么是单例模式? 采取一定的办法保证在整个软件系统中,单例模式确保对于某个类只能存在一个实例.有如下三个特点: ①.单例类只能有一个实例 ②.单例类必须自己创建自己的实例 ③.单例类必须提供外界获 ...