SPOJ-AMR11H

Enough with this Harry Potter, please! What are we, twelve-year olds? Let's get our teeth into some real pumpkin pasties -- oops, programming problems!

 

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

/**
    题意 :给你一个串,问使得串中的最大值 - 最小值 为 deliver
            然后看有多少个substring 和 subsequence 串 是的 deliver 与
            原串相等
    做法 :
            对于一个串,找到最大值mmax,最小值进行标记mmin,然后看分别由多少个
            mmax 由_mmax记录 ,mmin 由_mmin 记录
            然后符合要求的subsequence是 
(容斥原理 )包含最大值和最小值的子集的个数 = 总的子集个数 - 只有最小值的子集个数 - 只有最大值的子集的个数 + 既没有最小值又没有最大值的子集的个数
            符合要求的substring 是
            从0开始枚举
            有t1 标记离当前位置最近的mmin下标 ,用t2标记离当前位置最近的mmax下标
            然后进行枚举 
            sum = sum + mmin(t1 +1,t2+1);
            PS:当 mmin == mmax 时要特别处理
            substring 是 (n*(n+1))/2;
            subsequence 是 2^n - 1
**/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <stdio.h>
using namespace std;
#define maxn 100000 + 100
#define mod 1000000007
long long  mmap[maxn];
long long _next[maxn];
int main()
{
    _next[0] = 1;
    for(int i = 1; i < maxn; i++)
    {
        _next[i] = _next[i - 1] * 2 % mod;
    }
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n;
        scanf("%d", &n);
        long long  mmin = 0xfffffff;
        long long  mmax = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%lld", &mmap[i]);
            mmin = min(mmin, mmap[i]);
            mmax = max(mmax, mmap[i]);
        }
        long long sum1 = 0;
        long long  sum2 = 0;
        if(mmax == mmin)
        {
            sum1 = ((n * (n + 1)) / 2) % mod;
            sum2 = _next[n] - 1;
            printf("%lld %lld\n", sum1, sum2);
            continue;
        }
        int _mmin = 0;
        int _mmax = 0;
        int t1 = -1, t2 = -1;
        for(int i = 0; i < n; i++)
        {
            if(mmap[i] == mmin) {
                t1 = i;
                _mmin ++;
            }
            if(mmap[i] == mmax) {
                t2 = i;
                _mmax ++;
            }
            sum1 = (sum1 + min(t1 + 1, t2 + 1)) % mod;
        }
        sum2 = (_next[n] - _next[n - _mmin] - _next[n - _mmax] + _next[n - _mmax - _mmin]) % mod;
        if(sum2 < 0) {
            sum2 += mod;
        }
        printf("%lld %lld\n", sum1, sum2);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值