SPOJ - AMR11H
Time Limit: 404MS | Memory Limit: 1572864KB | 64bit IO Format: %lld & %llu |
Description
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[] = ;
for(int i = ; i < maxn; i++)
{
_next[i] = _next[i - ] * % mod;
}
int T;
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
long long mmin = 0xfffffff;
long long mmax = ;
for(int i = ; i < n; i++)
{
scanf("%lld", &mmap[i]);
mmin = min(mmin, mmap[i]);
mmax = max(mmax, mmap[i]);
}
long long sum1 = ;
long long sum2 = ;
if(mmax == mmin)
{
sum1 = ((n * (n + )) / ) % mod;
sum2 = _next[n] - ;
printf("%lld %lld\n", sum1, sum2);
continue;
}
int _mmin = ;
int _mmax = ;
int t1 = -, t2 = -;
for(int i = ; i < n; i++)
{
if(mmap[i] == mmin) {
t1 = i;
_mmin ++;
}
if(mmap[i] == mmax) {
t2 = i;
_mmax ++;
}
sum1 = (sum1 + min(t1 + , t2 + )) % mod;
}
sum2 = (_next[n] - _next[n - _mmin] - _next[n - _mmax] + _next[n - _mmax - _mmin]) % mod;
if(sum2 < ) {
sum2 += mod;
}
printf("%lld %lld\n", sum1, sum2);
}
return ;
}
SPOJ - AMR11H的更多相关文章
- Spring-2-H Array Diversity(SPOJ AMR11H)解题报告及测试数据
Array Diversity Time Limit:404MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
- SPOJ - AMR11H (容斥原理)
Enough with this Harry Potter, please! What are we, twelve-year olds? Let's get our teeth into some ...
- 排列组合或容斥原理 SPOJ - AMR11H
题目链接: https://vjudge.net/contest/237052#problem/H 这里给你一串数字,让你计算同时拥有这串数字最大值和最小值的子集(连续)和子序列(可以不连续)的数量, ...
- SPOJ - AMR11H Array Diversity (水题排列组合或容斥)
题意:给定一个序列,让你求两种数,一个是求一个子序列,包含最大值和最小值,再就是求一个子集包含最大值和最小值. 析:求子序列,从前往记录一下最大值和最小值的位置,然后从前往后扫一遍,每个位置求一下数目 ...
- SPOJ - AMR11H Array Diversity (排列组合)
题意:给定n个数,求包含最大值和最小值的子集(数字连续)和子序列(数字不连续)的个数. 分析: 1.如果n个数都相同,则子集个数为N * (N + 1) / 2,子序列个数为2N-1. 2.将序列从头 ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
随机推荐
- winform showDialog() 退出问题
今日发现: 当返回值为Dialog.OK时,会自动退出,不需要this.close().别的返回值仍需要.
- java笔试面试01
今天给大家分享一下小布去广州华南资讯科技公司笔试和面试的过程. 过程:1.HR面试 2.笔试 3.技术面试 小布下午两点到达,进门从前台领了一张申请表,填完之后带上自己的简历到4楼就开始HR面试. ...
- 如何创建LocalDB数据库和数据库实例
LocalDB是SQL Server 2012带来的新特性,它是一个专门为开发人员量身定制的轻量级数据库,下面介绍如何使用它. 创建LocalDB数据库的方法: 打开服务器资源管理器,右键点击“数据连 ...
- 搭建springmvc项目没扫描到mapper和service
严重: Servlet.service() for servlet [spring] in context with path [/springmvc-demo] threw exceptionorg ...
- Java中WeakHashMap实现原理深究
一.前言 我发现Java很多开源框架都使用了WeakHashMap,刚开始没怎么去注意,只知道它里面存储的值会随时间的推移慢慢减少(在 WeakHashMap 中,当某个“弱键”不再正常使用时,会被从 ...
- SpringBoot2.0
建立可执行的Jars和Wars bootJar用于构建可执行的Jar: bootWar用于构建可执行的war. application.properties 不启动web服务器 spring.main ...
- (age|name|sex)+ 脱离顺序控制 并且能添加多个
(age|name|sex)+ 脱离顺序控制 并且能添加多个
- LeetCode--Remove Linked List Element
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- LaTeX的图片插入及排版[转]
LaTeX中一般只直接支持插入eps(Encapsulated PostScript)格式的图形文件, 因此在图片插入latex文档之前应先设法得到图片的eps格式的文件. UNIX下的各种应用软件都 ...
- 2017年研究生数学建模D题(前景目标检测)相关论文与实验结果
一直都想参加下数学建模,通过几个月培训学到一些好的数学思想和方法,今年终于有时间有机会有队友一起参加了研究生数模,but,为啥今年说不培训直接参加国赛,泪目~_~~,然后比赛前也基本没看,直接硬刚.比 ...