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
1 2
题意:给你一串数字,给出一个差异性的定义(这串数字中最大值和最小值的差),问你这串数字的连续字串和非连续字串中差异性和原串相同的各有几个。
思路:连续的字串我们可以这样求
例:3 5 1 4 3 5 2 3
定义a,b分别是当前最小值和最大值的位置
a=0,b=0
for循环从i=1遍历到i=8(n=8),s[是存原串的数组
for(i=1;i<=n;i++){
if(s[i]==mx)//mx是最大值
{
b=i;
}
if(s[i]==mn)//mn是最小值
{
a=i;
}
sum=(sum+min(a,b))%mod;
}
为什么可以这样呢
当i=1时,对于i<=1的所有字串,没有任何字串的差异性可以和原串相等(因为它没有原串的最大值和最小值),所以sum=sum+0;
当i=2时,对于i<=2的所有字串,没有任何字串的差异性可以和原串相等(因为它只有原串的最大值),所以sum=sum+0;此时b=2;
当i=3时,对于i<=3的所有字串,原串的最大值和最小值都可以在i<=3中找到,我们以2<=i<=3为基本串,在它前面再加上连续的数字
可以找到2种情况(5,1[基础串])(3,5,1),此时a=3,b=2;所以sum=sum+min(a,b)=2;
当i=4时,,原串的最大值和最小值都可以在i<=4中找到,而我们可以再i=3的基础上在后面加上连续的数字,又可以找到两种新的情况(5,1,4)
(3,5,1,4);此时a=3,b=2;sum=sum+min(a,b)=4;
当i=5时,,原串的最大值和最小值都可以在i<=5中找到,而我们可以再i=4的基础上在后面加上s[5],又可以找到两种新的情况(5,1,4,3)
(3,5,1,4,3);此时a=3,b=2;sum=sum+min(a,b)=6
当i=6时,,因为s[6]是最原串的最大值,所以我们可以把基础串往后移一些(往后移了以后得到的子串数可以更多(1,4,3,5[基础串])(5,1,4,3,5),(3,5,1,3,4,5)),此时a=3,b=5,我们可以发现每次加的个数等于a,b中最小的那个,因为假设我们的基础串前面有3个数字,
我们可以选择加倒数第一个;选倒数第一个和倒数第二个;选倒数第一,倒数第二,倒数第三个。一共三种情况(因为要连续的),所以基础串前面的数字的个数越多越好。
而算不连续串时,我们可以用到容斥定理来算s【既有最大又有最小的串数】=s【总数】-s【没有最大】-s【没有最小】+s【既没最大又没最小】;
代码:
·
#include<stdio.h>
#define INF 0x7fffffff
#define ll long long
#define mod 1000000007
ll s[100050];
ll bin[100050];
ll min(int a,int b){
if(a>b)
return b;
return a;
}
void init(){
ll i;
ll r=1;
bin[0]=1;
for(i=1;i<=100005;i++){
r=(r*2)%mod;
bin[i]=r;
}
}
int main(){
int t;
init();
int n,i,j;
ll mx1,mn1,mx,mn;
scanf("%d",&t);
while(t--){
mx=mn=1;
mx1=0;
mn1=INF;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%lld",&s[i]);
if(s[i]<mn1){
mn=1;
mn1=s[i];
}
else if(s[i]==mn1)
mn++; //计算最小值的个数
if(s[i]>mx1){
mx=1;
mx1=s[i];
}
else if(s[i]==mx1)
mx++;
}
ll a=0,b=0;
ll sum=0;
for(i=1;i<=n;i++){
if(s[i]==mn1)
a=i;
if(s[i]==mx1)
b=i;
//printf("%d %d\n",a,b);
sum=(min(a,b)+sum)%mod;
}
ll ans=0;
if(mn1!=mx1){//判断特殊情况,如果最大值和最小值相等时,就说明整个数列都是同一个数,那么满足条件的非连续子序列的个数就是2的n次方-1
ans=(bin[n]-bin[n-mn]-bin[n-mx]+bin[n-mx-mn]+mod)%mod;
if(ans<0)//因为可能出现一种极端情况(bin[n-mx]和bin[n-mn]的值都接近与mod,bin[n]和bin[n-mx-mn]的值接近于0,ans的值就可能是负数)
ans+=mod;//在这里wa了七八发,知道真相的我感觉要吐血了
}
else
ans=(bin[n]-1+mod)%mod;
printf("%lld %lld\n",sum,ans);
}
return 0;
}
SPOJ - AMR11H (容斥原理)的更多相关文章
- 排列组合或容斥原理 SPOJ - AMR11H
题目链接: https://vjudge.net/contest/237052#problem/H 这里给你一串数字,让你计算同时拥有这串数字最大值和最小值的子集(连续)和子序列(可以不连续)的数量, ...
- SPOJ - AMR11H
Array Diversity Time Limit: 404MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Submi ...
- Spring-2-H Array Diversity(SPOJ AMR11H)解题报告及测试数据
Array Diversity Time Limit:404MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
- SPOJ - AMR11H Array Diversity (水题排列组合或容斥)
题意:给定一个序列,让你求两种数,一个是求一个子序列,包含最大值和最小值,再就是求一个子集包含最大值和最小值. 析:求子序列,从前往记录一下最大值和最小值的位置,然后从前往后扫一遍,每个位置求一下数目 ...
- SPOJ - AMR11H Array Diversity (排列组合)
题意:给定n个数,求包含最大值和最小值的子集(数字连续)和子序列(数字不连续)的个数. 分析: 1.如果n个数都相同,则子集个数为N * (N + 1) / 2,子序列个数为2N-1. 2.将序列从头 ...
- 2018.11.18 spoj Triple Sums(容斥原理+fft)
传送门 这次fftfftfft乱搞居然没有被卡常? 题目简述:给你nnn个数,每三个数ai,aj,ak(i<j<k)a_i,a_j,a_k(i<j<k)ai,aj,ak( ...
- SPOJ Triple Sums(FFT+容斥原理)
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream& ...
- SPOJ TSUM Triple Sums(FFT + 容斥)
题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct int ...
- SPOJ PGCD (mobius反演 + 分块)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意 :求满足gcd(i , j)是素数(1 &l ...
随机推荐
- Oracle11g温习-第十三章:索引
2013年4月27日 星期六 10:46 1.索引(Index)的功能:对记录进行排序,加快表的查询速度 2.索引的分类 1)B-tree 索引(默认) a.在一个大表上 b.建立在重复值比较少的 ...
- Leetcode 868. 二进制间距
868. 二进制间距 显示英文描述 我的提交返回竞赛 用户通过次数201 用户尝试次数220 通过次数207 提交次数396 题目难度Easy 给定一个正整数 N,找到并返回 N 的二进制表示中 ...
- Oracle Cursor用法总结
cursor分为三种,一是直接声明为cursor变量,二是首先声明类型再声明变量,三是声明为sys_refcursor. (1)直接声明 declare cursor emp_cur is sele ...
- Lucene.Net 学习(搜索部分)(低要求,写给自己看)
1. 搜索 排序:lucene 提供了Sort类对结果进行排序 提供了Filter类对查询条件进行限制 你或许会不自觉地拿它跟SQL语句进行比较:“lucene能执行and.or.order by.w ...
- 如何引用GitHub的静态资源文件 js css
参考:引用GitHub的静态资源文件 有些人说直接用 Github Raw 浏览器不执行是因为返回的 content-type 是 text/plain,这么说不准确.实际上浏览器对 MIME 类型并 ...
- 本地仓库有jar包maven依然报错的原因
本地Maven仓库有所需jar包依然报错,missing……………… 既然有这个jar包为什么还会报错呢? 找到本地仓库后发现里面有一个_remote.repositories文件 问题在_remot ...
- 使用spring-cloud-starter-bus-amqp做微服务配置刷广播,config-client配置 未刷新的 问题
在需要配置刷新的(类或方法)上 加上 @RefreshScope 扩展:spring cloud:config-server中@RefreshScope的"陷阱"
- 【LeetCode】Valid Parentheses合法括号
给定一个仅包含 '('.')'.'{'.'}'.'['.']'的字符串,确定输入的字符串是否合法. e.g. "()"."()[]{}"."[()]( ...
- [CodeForces - 614D] D - Skills
D - Skills Lesha plays the recently published new version of the legendary game hacknet. In this ver ...
- MSSQL优化(TUNING & OPTMIZATION & 优化)之——计划重用(plan reusing)
Oracle中,为了减少系统内的硬解析,从而节省系统资源,有绑定变量.计划共享(通过cursor_sharing参数)等一系列措施.那么,SQL Server作为三大商业关系库之一,是否也存在这样的机 ...