D. Pair of Numbers
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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:

  1. there is integer j (l ≤ j ≤ r), such that all integers al, al + 1, ..., ar are divisible by aj;
  2. 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.

Input

The first line contains integer n (1 ≤ n ≤ 3·105).

The second line contains n space-separated integers a1, a2, ..., an(1 ≤ ai ≤ 106).

Output

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.

Examples
Input
54 6 9 3 6

Output
1 32 

Input
51 3 5 7 9

Output
1 41 

Input
52 3 5 7 11

Output
5 01 2 3 4 5 

Note

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+10;
const int M = 1e6+10;
int n,m,k,tot=0,tim=0;
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]=1;int ret=0;int u=b[p];int ll,rr;++tot;for(int i=1; i<=n; i++) {if(!l[tot]) {ll=p-i;if(ll>=1&&b[ll]%u==0) {vis[ll]=1;} else l[tot]=ll+1,ret++;}if(!r[tot]) {rr=p+i;if(rr<=n&&b[rr]%u==0) {vis[rr]=1;} else r[tot]=rr-1,ret++;}if(ret==2)return;}
}
int main() {scanf("%d",&n);for(int i=1; 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=0;i<n;i++){int p=vec[i].second;if(!vis[p])Find(p);}for(int i=1; i<=tot; i++) {ans[i-1].l=l[i];ans[i-1].r=r[i];ans[i-1].len=r[i]-l[i];}sort(ans,ans+tot,cmp);int t=ans[0].len,ret=0;for(int i=0;i<tot;i++){if(ans[i].len<t)break;ret++;}printf("%d %d\n",ret,t);for(int i=0;i<ret;i++){Ans.pb(ans[i].l);}sort(Ans.begin(),Ans.end());for(int i=0;i<ret;i++){printf("%d ",Ans[i]);}printf("\n");return 0;
}

#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+10;
const int M = 1e6+10;
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=1; i<=n; ++i)scanf("%d",&a[i]),ans_l[i]=ans_r[i]=i;
}
void work() {for(int i=1; i<=n; ++i)for(; ans_l[i]>1&&a[ans_l[i]-1]%a[i]==0;)ans_l[i]=ans_l[ans_l[i]-1];for(int i=n; i>=1; --i)for(; ans_r[i]<n&&a[ans_r[i]+1]%a[i]==0;)ans_r[i]=ans_r[ans_r[i]+1];for(int i=1; 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=1]=L;}
}void outit() {sort(ans+1,ans+Ans+1);for(int i=1; i<=Ans; ++i)if(ans[i]!=ans[i-1])++xllend3;printf("%d %d\n%d",xllend3,len,ans[1]);for(int i=2; i<=Ans; ++i)if(ans[i]!=ans[i-1])printf(" %d",ans[i]);puts("");
}int main() {init();work();outit();return 0;
}

转载于:https://www.cnblogs.com/jianrenfang/p/6479174.html

Codeforces Round #209 (Div. 2) D. Pair of Numbers (模拟)相关推荐

  1. Codeforces Round #743 (Div. 2) D. Xor of 3 模拟 + 构造

    传送门 文章目录 题意: 思路: 题意: 给你一个010101序列aaa,定义一次操作是选择一个[1,n−2][1,n-2][1,n−2]范围内的下表,将ai,ai+1,ai+2a_i,a_{i+1} ...

  2. Codeforces Round #257 (Div. 1) D. Jzzhu and Numbers 高维前缀和 + 容斥

    传送门 文章目录 题意: 思路: 题意: 思路: 完全想不到容斥啊,看了半天也没看懂渍渍渍. 定义f[i]f[i]f[i]表示iii的超集个数,那么选择的方案就是2f[i]−12^{f[i]}-12f ...

  3. Codeforces Round #656 (Div. 3) F. Removing Leaves 贪心 + 模拟

    传送门 文章目录 题意: 思路: 题意: 思路: 首先有一个贪心策略就是每次都找一个叶子节点最多的点,让后删掉他的kkk个叶子节点,现在我们就来考虑如何模拟这个过程. 我们整一个vector<s ...

  4. Codeforces Round #358 (Div. 2) A. Alyona and Numbers 水题

    A. Alyona and Numbers 题目连接: http://www.codeforces.com/contest/682/problem/A Description After finish ...

  5. Codeforces Round #Pi (Div. 2) B. Berland National Library 模拟

    B. Berland National Library Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...

  6. Codeforces Round #552 (Div. 3) —— A. Restoring Three Numbers

    A. Restoring Three Numbers A.恢复三个数字 time limit per test1 second 每次测试的时间限制1秒 memory limit per test256 ...

  7. Codeforces Round #444 (Div. 2) C.Solution for Cube 模拟

    向题解低头,向大佬低头(.﹏.)orz--模拟也不能乱模啊--要好好分析题意,简化简化再简化orz敲黑板 六个面的魔方,能一步还原的情况一定是只有2个面是单色,其余四个面,每个面2种颜色,而且不会出现 ...

  8. Codeforces Round #224 (Div. 2): C. Arithmetic Progression(模拟)

    题意: 给你n个数字,你需要再添加一个数字,使得最后所有数字排序之后任意相邻两个数之差全部相等,问可以添加多少种不同的数字 思路: 一看就是水题但是情况不少,没了 例如所有数字全部相等,只有两个数字, ...

  9. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

最新文章

  1. 第一次作业:艰难的计算机之路
  2. linux 多线程 写日志,rsyslog多线程远程日志记录介绍(lamp+rsyslog)
  3. 在Ubuntu下运行 apt-get update命令后出现错误:
  4. 增值电信服务费是什么意思_增值电信业务IDC许可证要年检吗,流程是什么
  5. Qt笔记-解决QObject::startTimer: Timers cannot be started from another thread
  6. matlab画圆的命令_matlab画半圆
  7. python soup findall_Python之BeautifulSoup常用详细使用
  8. delete优化_深入理解JIT和编译优化
  9. WSGI Application/Framework
  10. Linux就该这么学 20181008(第十三章BIND)
  11. linux打开文件系统调用,Linux2.4打开一个文件的系统调用
  12. 数据库查询——选课系统
  13. 真正优秀的人,为何都那么尊重别人?(非常深刻)
  14. Make it Divisible by 25
  15. js html显示emoji表情,canvas绘制一个常用的emoji表情
  16. 干货 | 区块链项目估值的逻辑和误区
  17. Smart movie Java_智能影院下载-smartmovie智能影院【手机端+PC端+教程+工具】-东坡下载...
  18. ⭐算法入门⭐《动态规划 - 串匹配》困难02 —— LeetCode 72. 编辑距离
  19. 微信小程序实现锚点跳转
  20. Python学海无涯路【第01回】初始Python

热门文章

  1. Unity 配合Wallpaper Engine工具,实现电脑桌面壁纸游戏
  2. 运维开发标准化文档的四项基本原则
  3. 手机安全卫士------主页面(HomeActivity)
  4. lcd4linux 树莓派,树莓派B+运行kali并使用微雪3.5寸LCD电阻屏显示
  5. Java#中间件Tomcat-Typora笔记
  6. AD18的常用操作及快捷键
  7. UOS安装腾讯视频客户端
  8. win10内存占用高达四五十的解决方法
  9. 微信小程序沉浸式布局实现,兼容性强,容易理解
  10. 天翼杯 web APITest