题目描述

Given an array A with length n  a[1],a[2],...,a[n] where a[i] (1<=i<=n) is positive integer.
Count the number of pair (l,r) such that a[l],a[l+1],...,a[r] can be rearranged to form a geometric sequence.
Geometric sequence is an array where each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio. i.e. A = [1,2,4,8,16] is a geometric sequence.

输入描述:

The first line is an integer n (1 <= n <= 100000).
The second line consists of n integer a[1],a[2],...,a[n] where a[i] <= 100000 for 1<=i<=n.

输出描述:

An integer answer for the problem.
示例1

输入

5
1 1 2 4 1

输出

11

说明

The 11 pairs of (l,r) are (1,1),(1,2),(2,2),(2,3),(2,4),(3,3),(3,4),(3,5),(4,4),(4,5),(5,5).
示例2

输入

10
3 1 1 1 5 2 2 5 3 3

输出

20

备注:

The answer can be quite large that you may use long long in C++ or the similar in other languages.

题解

暴力。

先统计公比为$1$的区间有几种,接下来,无论公比为多少,区间长度不会超过$17$,因此只要枚举区间起点,验证$17$个区间即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 100000 + 10;
int n;
long long a[maxn];
long long b[maxn];
int sz; int check() {
if(b[0] == b[sz - 1]) return 0;
if(sz == 2) return 1;
for(int i = 2; i < sz; i ++) {
if(b[1] * b[i - 1] != b[0] * b[i]) return 0;
}
return 1;
} int main() {
while(~scanf("%d", &n)) {
for(int i = 1; i <= n; i ++) {
scanf("%lld", &a[i]);
}
long long ans = 1;
long long sum = 1;
for(int i = 2; i <= n; i ++) {
if(a[i] == a[i - 1]) sum ++;
else sum = 1;
ans = ans + sum;
}
for(int i = 1; i <= n; i ++) {
sz = 0;
for(int j = i; j <= min(n, i + 20); j ++) {
b[sz ++] = a[j];
int p = sz - 1;
while(p && b[p] < b[p - 1]) {
swap(b[p], b[p - 1]);
p --;
}
ans = ans + check();
}
}
printf("%lld\n", ans);
}
return 0;
}

  

湖南大学ACM程序设计新生杯大赛(同步赛)A - Array的更多相关文章

  1. 湖南大学ACM程序设计新生杯大赛(同步赛)J - Piglet treasure hunt Series 2

    题目描述 Once there was a pig, which was very fond of treasure hunting. One day, when it woke up, it fou ...

  2. 湖南大学ACM程序设计新生杯大赛(同步赛)L - Liao Han

    题目描述 Small koala special love LiaoHan (of course is very handsome boys), one day she saw N (N<1e1 ...

  3. 湖南大学ACM程序设计新生杯大赛(同步赛)B - Build

    题目描述 In country  A, some roads are to be built to connect the cities.However, due to limited funds, ...

  4. 湖南大学ACM程序设计新生杯大赛(同步赛)I - Piglet treasure hunt Series 1

    题目描述 Once there was a pig, which was very fond of treasure hunting. The treasure hunt is risky, and ...

  5. 湖南大学ACM程序设计新生杯大赛(同步赛)E - Permutation

    题目描述 A mod-dot product between two arrays with length n produce a new array with length n. If array ...

  6. 湖南大学ACM程序设计新生杯大赛(同步赛)D - Number

    题目描述 We define Shuaishuai-Number as a number which is the sum of a prime square(平方), prime cube(立方), ...

  7. 湖南大学ACM程序设计新生杯大赛(同步赛)H - Yuanyuan Long and His Ballons

    题目描述 Yuanyuan Long is a dragon like this picture?                                     I don’t know, ...

  8. 湖南大学ACM程序设计新生杯大赛(同步赛)G - The heap of socks

    题目描述 BSD is a lazy boy. He doesn't want to wash his socks, but he will have a data structure called ...

  9. 湖南大学ACM程序设计新生杯大赛(同步赛)C - Do you like Banana ?

    题目描述 Two endpoints of two line segments on a plane are given to determine whether the two segments a ...

随机推荐

  1. Hbuider制作app升级包的简单办法 (升级官方提供的案例)

    源文档:http://ask.dcloud.net.cn/question/11795 http://ask.dcloud.net.cn/article/199 一.生成移动App资源升级包 5+应用 ...

  2. REST式的web服务

    “REST”是罗伊·菲尔丁(Roy Fielding)在他的博士论文中创造的缩写.菲尔丁论文的第5章勾画出了被称为REST风格或REST式的Web服务的知道原则.他是HTTP1.1规范的主要作者和Ap ...

  3. 去除UITableView多余的seperator

    UIView *v = [[UIView alloc] initWithFrame:CGRectZero]; [tableView setTableFooterView:v]; [v release] ...

  4. 【leetcode 简单】第九题 移除元素

    给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...

  5. [转载]Android中Bitmap和Drawable

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

  6. python基础===100盏灯的问题

    闪存里有人这样提问这样: 第一轮操作所有电灯,第二轮操作第2盏,第4盏开关,以此类推,第三轮改变编号为3的倍数的电灯,第3盏,第6盏,如果原来那盏灯是亮的,就熄灭它,如果原来是灭的,就点亮它,以此类推 ...

  7. NEERC2014

    NEERC2014 A - Alter Board 题目描述:给出一个\(n \times m\)的国际象棋棋盘,每次选定一个矩形,使得矩形中的每个格子的颜色翻转,求出最少次数的方案使得最终棋盘只有一 ...

  8. MySQL三种备份

    一)备份分类 1 2 3 4 5 6 7 8 9 10 11 12 冷备:cold backup数据必须下线后备份 温备:warm backup全局施加共享锁,只能读,不能写 热备:hot backu ...

  9. 使AJAX可缓存——基于flask

    主流浏览器都有缓存机制,主要基于HTTP协议定义的缓存策略.对于一定时间内不发生变动的文档缓存起来,对于下次请求,就可以直接返回缓存的结果.使用缓存有以下好处: 1.减少冗余的数据传输,节省网络流量成 ...

  10. Oracle常用sql语句(三)之子查询

    子查询 子查询要解决的问题,不能一步求解 分为: 单行子查询 多行子查询 语法: SELECT select_list FROM table WHERE expr operator (SELECT s ...