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. Dancing Link 详解(转载)

    Dancing Link详解: http://www.cnblogs.com/grenet/p/3145800.html Dancing Link求解数独: http://www.cnblogs.co ...

  2. P6 EPPM R16.1安装与配置指南(三)

    P6 EPPM R16.1安装与配置指南(三) 解压:V137390-01.zip 修改 D:\P6_R161\p6suite\database\dbsetup.bat   的行 SET JAR_FI ...

  3. 重构第28 天 重命名bool方法(Rename boolean method)

    详解:本文中的”为布尔方法命名”是指如果一个方法带有大量的bool 参数时,可以根据bool 参数的数量,提取出若干个独立的方法来简化参数. 理解: 我们现在要说的重构并不是普通字面意义上的重构,它有 ...

  4. C#设计模式——外观模式(Facade Pattern)

    一.概述 在系统设计中,某一个系统可能非常庞大,用户要使用该系统就不得不掌握大量的接口,造成使用的不便.这时可以考虑将该系统细分成一系列子系统并使子系统间的耦合降到最低,利用外观模式提供一个外观对象, ...

  5. 循序渐进开发WinForm项目(4)--Winform界面模块的集成使用

    随笔背景:在很多时候,很多入门不久的朋友都会问我:我是从其他语言转到C#开发的,有没有一些基础性的资料给我们学习学习呢,你的框架感觉一下太大了,希望有个循序渐进的教程或者视频来学习就好了. 其实也许我 ...

  6. 创建Google网站地图Sitemap.xml

    Sitemap.xml是google搞出来的,也就是网站地图,不过这个网站地图是用xml写的,而且要按google的标准来写,并且要将写出来的这个文件sitemap.xml上传到自己的服务器空间中去. ...

  7. PHP实现过滤各种HTML标签

    首先分享一些比较常见的 $str=preg_replace("/<s*imgs+[^>]*?srcs*=s*(''|")(.*?)\1[^>]*?/?s*> ...

  8. ActiveReports 报表控件官方中文入门教程 (3)-如何选择页面报表和区域报表

    本篇文章将介绍区域报表和页面报表的常见使用场景.区别和选择报表类型的一些建议,两种报表的模板设计.数据源(设计时和运行时)设置.和浏览报表的区别. ActiveReports 报表控件官方中文入门教程 ...

  9. Spring RMI Example

    一: 提供服务的远程一端 1-1. applicationContext.xml <?xml version="1.0" encoding="UTF-8" ...

  10. ahjesus 创建msdn一样的帮助文档

    转载自http://www.cnblogs.com/DotNetNuke/archive/2009/04/23/1441899.html 使用SandCastle创建.Net帮助文档 Sandcast ...