Codeforces Round #209 (Div. 2) D. Pair of Numbers (模拟)
2 seconds
256 megabytes
standard input
standard output
Simon has an array a1, a2, ..., an, consisting of n positive integers. Today Simon asked you to find a pair of integers l, r (1 ≤ l ≤ r ≤ n), such that the following conditions hold:
- there is integer j (l ≤ j ≤ r), such that all integers al, al + 1, ..., ar are divisible by aj;
- value r - l takes the maximum value among all pairs for which condition 1 is true;
Help Simon, find the required pair of numbers (l, r). If there are multiple required pairs find all of them.
The first line contains integer n (1 ≤ n ≤ 3·105).
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 106).
Print two integers in the first line — the number of required pairs and the maximum value of r - l. On the following line print all l values from optimal pairs in increasing order.
5
4 6 9 3 6
1 3
2
5
1 3 5 7 9
1 4
1
5
2 3 5 7 11
5 0
1 2 3 4 5
In the first sample the pair of numbers is right, as numbers 6, 9, 3 are divisible by 3.
In the second sample all numbers are divisible by number 1.
In the third sample all numbers are prime, so conditions 1 and 2 are true only for pairs of numbers (1, 1), (2, 2), (3, 3), (4, 4), (5, 5).
【分析】给定一个正整数数组,求最长的区间,使得该区间内存在一个元素,它能整除该区间的每个元素。
可能我的方法有点复杂吧。我是先从小到大排序,记录所有数的位置,然后从小到大向两边扩展,就行了,详细见代码一。还有种做法类似DP,很短很神奇,见代码二。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
typedef long long ll;
using namespace std;
const int N = 3e5+;
const int M = 1e6+;
int n,m,k,tot=,tim=;
int head[N],vis[N],l[N],r[N];
int a[N],b[N];
vector<pair<int,int> >vec;
vector<int>Ans;
struct man{
int l,r,len;
}ans[N];
bool cmp(man s,man d){
return s.len>d.len;
}
void Find(int p) {
vis[p]=;
int ret=;
int u=b[p];
int ll,rr;
++tot;
for(int i=; i<=n; i++) {
if(!l[tot]) {
ll=p-i;
if(ll>=&&b[ll]%u==) {
vis[ll]=;
} else l[tot]=ll+,ret++;
}
if(!r[tot]) {
rr=p+i;
if(rr<=n&&b[rr]%u==) {
vis[rr]=;
} else r[tot]=rr-,ret++;
}
if(ret==)return;
}
}
int main() {
scanf("%d",&n);
for(int i=; i<=n; i++) {
scanf("%d",&a[i]);
b[i]=a[i];
vec.pb(mp(a[i],i));
}
sort(vec.begin(),vec.end());
for(int i=;i<n;i++){
int p=vec[i].second;
if(!vis[p])Find(p);
}
for(int i=; i<=tot; i++) {
ans[i-].l=l[i];
ans[i-].r=r[i];
ans[i-].len=r[i]-l[i];
}
sort(ans,ans+tot,cmp);
int t=ans[].len,ret=;
for(int i=;i<tot;i++){
if(ans[i].len<t)break;
ret++;
}
printf("%d %d\n",ret,t);
for(int i=;i<ret;i++){
Ans.pb(ans[i].l);
}
sort(Ans.begin(),Ans.end());
for(int i=;i<ret;i++){
printf("%d ",Ans[i]);
}
printf("\n");
return ;
}
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 2e6+;
const int M = 1e6+;
int n,L,R,len,Ans,xllend3;
int a[N],ans_l[N],ans_r[N],ans[N];
void init() {
scanf("%d",&n);
for(int i=; i<=n; ++i)scanf("%d",&a[i]),ans_l[i]=ans_r[i]=i;
}
void work() {
for(int i=; i<=n; ++i)for(; ans_l[i]>&&a[ans_l[i]-]%a[i]==;)ans_l[i]=ans_l[ans_l[i]-];
for(int i=n; i>=; --i)for(; ans_r[i]<n&&a[ans_r[i]+]%a[i]==;)ans_r[i]=ans_r[ans_r[i]+];
for(int i=; i<=n; ++i) {
L=ans_l[i];
R=ans_r[i];
if(R-L==len)ans[++Ans]=L;
if(R-L>len)len=R-L,ans[Ans=]=L;
}
} void outit() {
sort(ans+,ans+Ans+);
for(int i=; i<=Ans; ++i)if(ans[i]!=ans[i-])++xllend3;
printf("%d %d\n%d",xllend3,len,ans[]);
for(int i=; i<=Ans; ++i)if(ans[i]!=ans[i-])printf(" %d",ans[i]);
puts("");
} int main() {
init();
work();
outit();
return ;
}
Codeforces Round #209 (Div. 2) D. Pair of Numbers (模拟)的更多相关文章
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- Codeforces Round #209 (Div. 2)A贪心 B思路 C思路+快速幂
A. Table time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- Codeforces Round #209 (Div. 2) B. Permutation
解题思路: 如果序列a是单调递增的,则序列为1,2,..... 2n,则将给出的式子化简得Σ(a2i - a2i-1) = n 如果序列a是单调递减的,则序列为2n,.........2, 1,则将给 ...
- Codeforces Round #209 (Div. 2) A. Table
#include <iostream> #include <vector> using namespace std; int main(){ int n,m; cin > ...
- Codeforces Round #209 (Div. 2)C
刷了一页的WA ..终于发现了 哪里错了 快速幂模板里一个变量t居然开得long ... 虽然代码写的丑了点 但是是对的 那个该死的long 啊.. #include <iostream&g ...
- Codeforces Round #209 (Div. 2)
A: 要么是两次要么4次,判断是否在边界: #include<cstdio> using namespace std; int main() { int n,m,x; ; scanf(&q ...
- Codeforces Round #209 (Div. 2) C - Prime Number
传送门 题意 给出n个数及x,求 \[\frac{\sum _{i=1}^n x^{a_1+a_2+...+a_{i-1}+a_{i+1}+...a_n}}{\prod_{i=1}^n x^{a_i} ...
- Codeforces Round #627 (Div. 3) D - Pair of Topics(双指针)
题意: 有长为n的a,b两序列,问满足ai+aj>bi+bj(i<j)的i,j对数. 思路: 移项得:(ai-bi)+(aj-bj)>0,i<j即i!=j,用c序列保存所有ai ...
- Codeforces Round #379 (Div. 2) D. Anton and Chess 模拟
题目链接: http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test4 secondsmem ...
随机推荐
- 如何记录MySQL执行过的SQL语句
很多时候,我们需要知道 MySQL 执行过哪些 SQL 语句,比如 MySQL 被注入后,需要知道造成什么伤害等等.只要有 SQL 语句的记录,就能知道情况并作出对策.服务器是可以开启 MySQL 的 ...
- Java的外部类为什么不能使用private、protected进行修饰
对于顶级类(外部类)来说,只有两种修饰符:public和默认(default).因为外部类的上一单元是包,所以外部类只有两个作用域:同包,任何位置.因此,只需要两种控制权限:包控制权限和公开访问权限, ...
- [SDOI2011]消防/[NOIP2007] 树网的核
消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个国家最兴旺的 ...
- [bzoj 1143]最长反链二分图最大匹配
Dilworth定理:偏序集能划分成的最少的全序集的个数与最大反链的元素个数相等. 证明:http://www.cnblogs.com/itlqs/p/6636222.html 题目让求的是最大反链的 ...
- B. Minimum Ternary String (这个B有点狠)
B. Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input sta ...
- maven工程开启jetty调试
转摘自:http://czj4451.iteye.com/blog/1942437 准备工作: a. 在pom.xml中配置jetty插件: <plugins> <plugin> ...
- js实现快速排序的方法
因为面试面到了这个问题,所以写一下,加深印象,有两种方法 第一种是通过两个for循环,每一次对比相邻两个数据的大小,小的排在前面,如果前面的数据比后面的大就交换这两个数的位置,这个方法就是比较次数太多 ...
- 使用Word2010发布博客文章
发布博客可以直接在web页面上面编辑,也可以使用客户端编辑,其中客户端支持windows live writer以及word本身的发布博客功能.个人试用后倾向于使用word发布博客文章. 下面的内容转 ...
- 转:使用 Nginx Upload Module 实现上传文件功能
普通网站在实现文件上传功能的时候,一般是使用Python,Java等后端程序实现,比较麻烦.Nginx有一个Upload模块,可以非常简单的实现文件上传功能.此模块的原理是先把用户上传的文件保存到临时 ...
- BigDecimal精度问题
介绍 1.商业计算使用BigDecimal. 2.使用参数为String的构造函数. 3.BigDecimal都是不可变的,每一步的运算时,都会产生一个新的对象.所以在做加减乘除后千万要保存操作后的值 ...