Pair of Numbers
Codeforces Round #209 (Div. 2) D:http://codeforces.com/contest/359/problem/D
题意:给以一个n个数的序列,然后问你最大的区间l,r,在这个区间里面,存在一个数是这个区间所有数的约数,如果这个区间有多个,统计有多少个以及每个区间的左端点。
题解:这一题,别人用了一个很特殊的找发,让我叹为观止。哎,没有数学功底以及很强的推理能力,真的有点力不从心啊。说说别人是怎么找的吧。首先,从第一个数开始,往右边找,然后找到第一个不是a[1]倍数的数,这是第一个区间,然后从这个数让左边找,找到以这个数为约数的左边全歼,然后往右边找,找到不是这个数的倍数的那个数,并把这个数放进队列,因为这个数就是下一次要开始找的数,对于当前这个数,就会得到一个区间,然后进行相关的更新。接着又从队列中数,然后重复上面的 数,直到最后一个数。这里,别人的证明就是每个数最多被找logn次,自己画画图,好像是这样。所以复杂度就是nlogn.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=3e5+;
int a[N],n;
int ans[N],top;
int anss;
int main(){
while(~scanf("%d",&n)){
memset(ans,,sizeof(ans));
memset(a,,sizeof(a));
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
queue<int>Q;
anss=;top=;
Q.push();
while(!Q.empty()){
int x=Q.front();
Q.pop();
int num=,ps=;
for(int i=x-;i>=;i--){
if(a[i]%a[x]==){
num++;
}
else
break;
}
ps=x-num+;
for(int i=x+;i<=n;i++){
if(a[i]%a[x]==)
num++;
else{
Q.push(i);
break;
}
}
if(num>anss){
anss=num;top=;
ans[++top]=ps;
}
else if(num==anss)
ans[++top]=ps;
}
printf("%d %d\n",top,anss-);
for(int i=;i<=top;i++){
if(i==top)printf("%d\n",ans[i]);
else
printf("%d ",ans[i]);
}
}
}
Pair of Numbers的更多相关文章
- Codeforces 395 D.Pair of Numbers
D. Pair of Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #209 (Div. 2) D. Pair of Numbers (模拟)
D. Pair of Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- cf359D Pair of Numbers
Simon has an array a1, a2, ..., an, consisting of n positive integers. Today Simon asked you to find ...
- CodeForces 359D Pair of Numbers (暴力)
题意:给定一个正整数数组,求最长的区间,使得该区间内存在一个元素,它能整除该区间的每个元素. 析:暴力每一个可能的区间,从数组的第一个元素开始考虑,向两边延伸,设延伸到的最左边的点为l, 最右边的点为 ...
- cf D. Pair of Numbers
http://codeforces.com/contest/359/problem/D 题意:给你n个数,然后找出在[l,r]中有一个数a[j],l<=j<=r,在[l,r]中的所有数都是 ...
- CF359D:Pair of Numbers——题解
https://vjudge.net/problem/CodeForces-359D http://codeforces.com/problemset/problem/359/D 题目大意: 给一串数 ...
- [codeforces] 359D Pair of Numbers
原题 RMQ st表棵题 要想让一个区间里的所有数都可以整除其中一个数,那么他一定是这个区间内的最小值,并且同时是这个区间的gcd.然后这个问题就转化成了RMQ问题. 维护两个st表,分别是最小值和g ...
- Codeforces 359D Pair of Numbers | 二分+ST表+gcd
题面: 给一个序列,求最长的合法区间,合法被定义为这个序列的gcd=区间最小值 输出最长合法区间个数,r-l长度 接下来输出每个合法区间的左端点 题解: 由于区间gcd满足单调性,所以我们可以二分区间 ...
- CF359D Pair of Numbers gcd+暴力
利用区间 gcd 个数不超过 log 种来做就可以了~ code: #include <bits/stdc++.h> #define N 300005 #define setIO(s) f ...
随机推荐
- 关于setLayoutParams报错
有两个可能的原因 1.内部view没有用其parent的LayoutParams在继承BaseAdapter的时候,用getView返回View的时候,用代码控制布局,需要用到View.setLay ...
- grep:Binary file (standard input) matches
grep "key" xxx.log时输出 Binary file xxx.log matches 百度了一下:grep觉得这是二进制文件.解决方式:grep -a. grep - ...
- Amoeba实现mysql主从读写分离
Amoeba实现mysql主从读写分离 这段在网上看了下关于amoeba的文章,总体感觉好像要比mysql-proxy好的多,也参考了不少的资料,此文章可能与其他文章作者会有雷同的地方,请谅解,但是此 ...
- [转] Creating a Simple RESTful Web App with Node.js, Express, and MongoDB
You can find/fork the sample project on GitHub Hey! This and all my other tutorials will soon be mov ...
- 『零行代码』解决键盘遮挡问题(iOS)
关注仓库,及时获得更新:iOS-Source-Code-Analyze https://github.com/draveness/iOS-Source-Code-Analyze Follow: Dra ...
- Java基础知识强化之集合框架笔记32:集合之可变参数的概述和使用
1. 可变参数的概述和使用: (1)可变参数:定义方法的时候不知道该定义多少个参数(2)格式: 修饰符 返回值类型 方法名(数据类型… 变量名){ } 注意: 这里的变量其实是一个数 ...
- Objective-C:swift、objective-c、C++、C混合编程
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...
- sql查询过程中 update,insert,delete可视化收影响行数
insert into test_tb output inserted.id,inserted.data values('c'),('d') delete from test_tb output de ...
- 【POJ1823】【线段树】Hotel
Description The "Informatics" hotel is one of the most luxurious hotels from Galaciuc. A l ...
- JS判断终端(Android IOS)
function getMobileOperatingSystem() { var userAgent = navigator.userAgent || navigator.vendor || win ...