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(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
随机推荐
- JSONP跨域jQuery处理整理(附天气数据实例)
写在前面 跨域的解决方案有多种,其中最常见的是使用同一服务器下的代理来获取远端数据,再通过ajax进行读取,而在这期间经过了两次请求过程,使得获取数据的效率大大降低,这篇文章蓝飞就为大家介绍一下解决跨 ...
- k8s第一个实例创建redis集群服务
1.创建redis-master-controller.yaml apiVersion: v1 kind: ReplicationController metadata: name: redis-ma ...
- 【历史】- 一段关于 Unix、Linux 和 Windows 的暗黑史
“SCO在言语上变得越来越好斗,而且还拒绝展示有关诉讼的任何证据,一切都似乎在表明,SCO只不过是在那里拉虎皮做大旗地狂言乱语.但是,微软决不会轻易放弃这么可以一个利用这些狂言乱语的好机会.”2003 ...
- 处理大并发量订单处理的 KafKa部署总结
处理大并发量订单处理的 KafKa部署总结 今天要介绍的是消息中间件KafKa,应该说是一个很牛的中间件吧,背靠Apache 与很多有名的中间件搭配起来用效果更好哦 ,为什么不用RabbitMQ,因为 ...
- BZOJ1037 ZJOI2008生日聚会(动态规划)
设f[i][j][x][y]为安排了i个男孩j个女孩,后缀最大男孩-女孩数为x,最大女孩-男孩数为y的方案数.转移显然. #include<iostream> #include<cs ...
- [Leetcode] The minimum depth of binary tree二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Expect使用小记
By francis_hao May 31,2017 本文翻译了部分Expect的man手册,只选取了个人常用的功能,因此并不完善. Expect是一个可以和交互式程序对话的程序 概述 ...
- word公式编辑中的转义字符
Some of the commonly used symbols: \infty - Infinity \leq - Less then or equal \geq - ...
- 原生toolbar基本使用教程
1.先写布局文件 <android.support.v7.widget.Toolbar android:id="@+id/toolbar" app:title=" ...
- ng websocket
ng使用websocket 1.安装依赖库npm install ws --save 2.安装类型定义文件 npm install @types/ws --save 3.编写服务 import { I ...