Array Diversity

Time Limit:404MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

 

Description

Here we go!

Let's define the diversity of a list of numbers to be the difference between the largest and smallest number in the list.

For example, the diversity of the list (1, -1, 2, 7) = 7 - (-1) = 8.

A substring of a list is considered a non-empty sequence of contiguous numbers from the list. For example, for the list (1,3,7), the substrings are (1), (3), (7), (1,3), (3,7), (1,3,7). A subsequence of a list is defined to be a non-empty sequence of numbers obtained by deleting some elements from the list. For example, for the list (1,3,7), the subsequences are (1), (3), (7), (1,3), (3,7), (1,7), (1,3,7).

Given a list of length N find the number of substrings and subsequences in this list with the maximum diversity. If a substring/subsequence having maximum diversity occurs multiple times in the list, each of its occurences adds towards the answer.   And tell Harry Potter your answer

 

Input (STDIN):

The first line contains T, the number of test cases. Then follow T test case blocks.

Each blocks starts with the first line containing the number N.

The second line contains a list of numbers in this list.

Output (STDOUT):

For each test case, output the number of substrings and the number of subsequences in this list with the maximum diversity.

Since the answers maybe very large, output them modulo 1000000007.

 

Constraints:

T <= 10

N <= 100,000

Each number in the list is between 1 and 100,000 inclusive.

Sample Input:

3

3

1 2 3

4

1 4 3 4

3

3 2 1

Sample Output:

1 2

3 6

1 2

题解:

  1. 题目大意是求出包含最大的diversity(最大值和最小值之差 )的子序列和子集个数,  其实就是求包含最大值和最小值的子序列和子集个数。

  2. 思路是先求出最大值和最小值以及其位置,然后计算。需要注意的是,如果最大值等于最小值,需要特判,所有的子序列和非空子集都满足题意,所以子序列个数就是1+2+3+……+n=n*(n+1)/2,非空子集个数就是2^n -1。由于数据比较大,所以求幂运算需要另写函数,类似矩阵的快速幂。

  3. 如果最大值不等于最小值,那么子集显然是从从最小值中至少取出一个(2^k1 -1),最大值中至少取出一个(2^k2 -1),剩下的数中取出任意个(即并上任意子集 ),三者相乘即可。

  4. 计算子序列,思路是计算包含不同最大值和最小值的子序列个数之和,注意避免重复。避免重复的方法就是从前向后找最大值和最小值的组合对,前面的组合对可以包含后面的,但是计算后面的组合对时,不能包含前面的。

以下是代码:

#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cctype>
using namespace std; #define ss(x) scanf("%d",&x)
#define ff(i,s,e) for(int i=s;i<e;i++)
#define fe(i,s,e) for(int i=s;i<=e;i++)
#define print(x) printf("%d\n",x)
#define write() freopen("1.in","r",stdin)
#define float double
typedef long long LL; const int N = 100010;
const LL Mod = 1000000007;
int n,T,sm[N],la[N];
LL a[N],ma,mi;
LL ans1,ans2; LL ppow(int n){ //2的n次幂
LL ans = 1,t =2;
while(n){
if(n%2)ans = (ans*t)%Mod;
t = ((t%Mod)*(t%Mod))%Mod;
n = n>> 1;
}
return ans;
}
int main(){
//write();
ss(T);
while(T--){
ss(n);
fe(i,1,n)scanf("%lld",&a[i]);
ma = mi = a[1];
fe(i,2,n){ //求出最大值和最小值
if(a[i]>ma)ma=a[i];
else if(a[i]<mi)mi = a[i];
}
if(ma == mi){ //全部数字相同时,特判
ans1 = n*(n+1)/2 % Mod; //所有子序列满足
ans2 = ppow(n)-1; //所有非空子集均满足
}else{
int k1=0,k2=0;
fe(i,1,n){ //找出最大值和最小值的位置和个数
if(a[i]==mi)sm[++k1]=i;
if(a[i]==ma)la[++k2]=i;
}
int i=1,j=1,pre=1;
ans1=0;
while(i<=k1 && j <=k2){ //算出满足题意的子序列和子集
int t1 = min(sm[i],la[j]);
int t2 = max(sm[i],la[j]);
ans1 = (ans1+(LL)(t1-pre+1)*(n-t2+1))%Mod;
sm[i]<la[j]?i++ : j++;
pre = min(t1+1,min(sm[i],la[j]));
}
ans2 = ((ppow(k1)-1)*(ppow(k2)-1)%Mod * ppow(n-k1-k2))%Mod;
}
printf("%lld %lld\n",ans1,ans2);
}
}

  

以下是可测试数据:

sample input:

2

7

1 2 7 5 3 7 1

6

1 7 7 7 1 1

sample output:

10 72

11 49

 

Spring-2-H Array Diversity(SPOJ AMR11H)解题报告及测试数据的更多相关文章

  1. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  2. 【LeetCode】702. Search in a Sorted Array of Unknown Size 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 二分查找 日期 题目地址:https://lee ...

  3. 【LeetCode】548. Split Array with Equal Sum 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

  4. 【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...

  5. 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】922. Sort Array By Parity II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...

  7. 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...

  8. LeetCode 922 Sort Array By Parity II 解题报告

    题目要求 Given an array A of non-negative integers, half of the integers in A are odd, and half of the i ...

  9. 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. Android学习笔记之HttpClient实现Http请求....

    PS:最近光忙着考试了....破组成原理都看吐了....搞的什么也不想干...写篇博客爽爽吧....貌似明天就考试了...sad... 学习笔记: 1.如何实现Http请求来实现通信.... 2.解决 ...

  2. Python 的字符串格式化和颜色控制

    (部分内容源自武神博客和网络收集.) Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两 ...

  3. MvvmLight框架使用入门(一)

    MvvmLight是比较流行的MVVM框架,相对较为简单易用.可能正因为简单,对应的帮助文档不多,对初学者就不够友好了.这里会用几篇随笔,就个人对MvvmLight的使用经验,来做一个入门的介绍. 第 ...

  4. IOS7中自动计算label的宽度和高度的方法

    #import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super vie ...

  5. spring事务与消息队列

    在开发过程中,遇到一个bug,产生bug的原因是spring事务提交晚于消息队列的生产消息,导致消息队列消费消息时获取到的数据不正确.这篇文章介绍问题的产生和一步步的解决过程. 一.问题的产生: 场景 ...

  6. Gradle学习系列之二——创建Task的多种方法

    在本系列的上篇文章中,我们讲到了Gradle入门,在本篇文章中我们将讲到创建Task的多种方法. 请通过以下方式下载本系列文章的Github示例代码: git clone https://github ...

  7. office2016 软件全集 官方下载免费完整版(含破解文件)不含垃圾软件 win10完美激活

    office2016官方下载免费完整版是新一代办公软件,office2016官方下载免费完整版已经分享到下面,office2016官方下载免费完整版包括了Word.Excel.PowerPoint.O ...

  8. URL与图像格式

    绝对/相对URL “绝对URL”是指资源的完整的地址,通常以“http://”打头: “相对URL”是指Internet上资源相对于当前页面的地址,它包含从当前页面指向目标资源位置的路径,不以“htt ...

  9. 解决打印机报错:操作无法完成(错误0x00000709)。

    解决:操作无法完成(错误0x00000709).再次检查打印机名称,并确保打印机已连接到... 上午同时说,网络打印机打印不了,于是首先看一下打印服务器IP是不是给换了,结果没换. 接着尝试重新添加一 ...

  10. [CLR via C#]25. 线程基础

    一.Windows为什么要支持线程 Microsoft设计OS内核时,他们决定在一个进程(process)中运行应用程序的每个实例.进程不过是应用程序的一个实例要使用的资源的一个集合.每个进程都赋予了 ...