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(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
随机推荐
- 一个android控件资源网站
http://www.androidviews.net/ 里面有各种常用控件,赞~
- lintcode-103-带环链表 II
带环链表 II 给定一个链表,如果链表中存在环,则返回到链表中环的起始节点的值,如果没有环,返回null. 样例 给出 -21->10->4->5, tail connects to ...
- Packet filtering with Linux & NAT
http://www.linuxfocus.org/ChineseGB/May2003/article289.shtml Gateway, Proxy-Arp 和 Ethernet Bridge ? ...
- Tomcat 中如何给 web 项目配置虚拟目录的方法
为什么要给 web 项目配置虚拟目录? 初学 JavaWeb 时,会发现只要我们把 web 项目放到 Tomcat 的 webapps 目录下,再通过 http://localhost:8080/项目 ...
- springboot 学习笔记(二)--- properties 配置
springboot可以提供了多种方式配置properties. 一.Java System.setProperty(k, v) System.setProperty("myname&quo ...
- [洛谷P3377]【模板】左偏树(可并堆)
题目大意:有$n$个数,$m$个操作: $1\;x\;y:$把第$x$个数和第$y$个数所在的小根堆合并 $2\;x:$输出第$x$个数所在的堆的最小值 题解:左偏树,保证每个的左儿子的距离大于右儿子 ...
- C++——派生类中的访问——可见性问题
C++中派生类对基类成员的访问形式主要有以下两种: 1.内部访问:由派生类中新增成员对基类继承来的成员的访问. 2.对象访问:在派生类外部,通过派生类的对象对从基类继承来的成员的访问.今天给大家介绍在 ...
- CSS3边框会动的信封
<!DOCTYPE html><html> <head> <title>酷炫的CSS3</title> <meta charset=& ...
- JavaScript学习笔记——浅拷贝、深拷贝
参考自:http://www.cnblogs.com/yichengbo/archive/2014/07/10/3835882.html 一.数组的深浅拷贝 在使用JavaScript对数组进行操作的 ...
- 1552: [Cerc2007]robotic sort
这道题用splay写 先离散化数据保证按题目所述顺序来写 按原序作为键值建树 维护区间最小值去跑 每次将i的位置 和 n的位置x和y找出来后 将x旋转到root y旋转到x的有儿子 这时y的左子树就是 ...