• SPOJ


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

     Status

    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 occurances 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.
    Time Limit: 2 s
    Memory Limit: 32 MB
    Sample Input:
    3
    3
    1 2 3
    4
    1 4 3 4
    3
    3 2 1
    Sample Output:
    1 2
    3 6
    12

    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

    1 2

    /**
        题意 :给你一个串,问使得串中的最大值 - 最小值 为 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
    ", 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
    ", sum1, sum2);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    [BZOJ]2132: 圈地计划 最小割
    从最近MySQL的优化工作想到的
    Linux基本操作 9----- 认识与学习bash
    多路径配置vlome group共享存储,VG的更新。
    两位数乘法的速算方法(一)
    请对他有足够的重视——设计!
    ASP.NET中配置应用程序
    flex开发小技巧集锦
    刚制作完的SAP Sybase ASE15.7 [Sybase Central] 客户端
    Static 关键字的 5 种用法,你会几种?
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4743955.html
Copyright © 2020-2023  润新知