SPOJ - AMR11H Array Diversity (水题排列组合或容斥)
题意:给定一个序列,让你求两种数,一个是求一个子序列,包含最大值和最小值,再就是求一个子集包含最大值和最小值。
析:求子序列,从前往记录一下最大值和最小值的位置,然后从前往后扫一遍,每个位置求一下数目就好。
求子集可以用排列组合解决,很简单,假设最大值个数是 n,最小值的数是 m,总数是 N,答案就是 (2^n-1) * (2^m-1)*2^(N-m-n),
当然要特殊判断最大值和最小值相等的时候。
当然也可以用容斥来求,就是总数 - 不是最大值的数目 - 不是最小值的数目 + 不是最大值也不是最小值的数目,其实也差不多
代码如下:
排列组合:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} LL fast_pow(int n){
LL a = 2, ans = 1;
while(n){
if(n & 1) ans = ans * a % mod;
n >>= 1;
a = a * a % mod;
}
return ans;
} int a[maxn];
vector<int> v1, v2; int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
int mmin = mod, mmax = 0;
for(int i = 0; i < n; ++i){
scanf("%d", a+i);
mmin = min(mmin, a[i]);
mmax = max(mmax, a[i]);
}
v1.clear(); v2.clear();
for(int i = 0; i < n; ++i)
if(mmin == a[i]) v1.push_back(i);
else if(mmax == a[i]) v2.push_back(i);
if(v1.size() == n){
LL ans1 = (LL)n * (n+1) / 2 % mod;
LL ans2 = (fast_pow(n) - 1 % mod) % mod;
printf("%lld %lld\n", ans1, ans2);
continue;
}
LL ans2 = (fast_pow(v1.size())-1) * (fast_pow(v2.size())-1) % mod * fast_pow(n-v1.size()-v2.size()) % mod;
ans2 = (ans2 + mod) % mod;
int i = 0, j = 0, pre = 0;
LL ans1 = 0;
while(true){
int t1 = min(v1[i], v2[j]);
int t2 = max(v1[i], v2[j]);
ans1 = (ans1 + (LL)(t1-pre+1) * (n-t2)) % mod;
v1[i] < v2[j] ? ++i : ++j;
if(i == v1.size() || v2.size() == j) break;
pre = min(t1+1, min(v1[i], v2[j]));
}
printf("%lld %lld\n", ans1, ans2);
}
return 0;
}
容斥:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} LL fast_pow(int n){
LL a = 2, ans = 1;
while(n){
if(n & 1) ans = ans * a % mod;
n >>= 1;
a = a * a % mod;
}
return ans;
} int a[maxn];
vector<int> v1, v2; int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
int mmin = mod, mmax = 0;
for(int i = 0; i < n; ++i){
scanf("%d", a+i);
mmin = min(mmin, a[i]);
mmax = max(mmax, a[i]);
}
v1.clear(); v2.clear();
for(int i = 0; i < n; ++i)
if(mmin == a[i]) v1.push_back(i);
else if(mmax == a[i]) v2.push_back(i);
if(v1.size() == n){
LL ans1 = (LL)n * (n+1) / 2 % mod;
LL ans2 = (fast_pow(n) - 1 % mod) % mod;
printf("%lld %lld\n", ans1, ans2);
continue;
}
LL ans2 = fast_pow(n);
ans2 = (ans2 - fast_pow(n-v1.size()) - fast_pow(n-v2.size()) + fast_pow(n-v1.size()-v2.size())) % mod;
ans2 = (ans2 % mod + mod) % mod;
int i = 0, j = 0, pre = 0;
LL ans1 = 0;
while(true){
int t1 = min(v1[i], v2[j]);
int t2 = max(v1[i], v2[j]);
ans1 = (ans1 + (LL)(t1-pre+1) * (n-t2)) % mod;
v1[i] < v2[j] ? ++i : ++j;
if(i == v1.size() || v2.size() == j) break;
pre = min(t1+1, min(v1[i], v2[j]));
}
printf("%lld %lld\n", ans1, ans2);
}
return 0;
}
SPOJ - AMR11H Array Diversity (水题排列组合或容斥)的更多相关文章
- SPOJ - AMR11H Array Diversity (排列组合)
题意:给定n个数,求包含最大值和最小值的子集(数字连续)和子序列(数字不连续)的个数. 分析: 1.如果n个数都相同,则子集个数为N * (N + 1) / 2,子序列个数为2N-1. 2.将序列从头 ...
- SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)
#include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...
- 2018 湖南网络比赛题 HDU - 6286 (容斥)
题意:不说了. 更加偏向于数学不好的小可爱来理解的. 这篇博客更加偏重于容斥的讲解.用最直观的数学方法介绍这个题. 思路: 在a<=x<=b. c<=y<=d 中满足 x*y ...
- 【BZOJ4927】第一题 双指针+DP(容斥?)
[BZOJ4927]第一题 Description 给定n根直的木棍,要从中选出6根木棍,满足:能用这6根木棍拼 出一个正方形.注意木棍不能弯折.问方案数. 正方形:四条边都相等.四个角都是直角的四边 ...
- 【HDU 5532 Almost Sorted Array】水题,模拟
给出一个序列(长度>=2),问去掉一个元素后是否能成为单调不降序列或单调不增序列. 对任一序列,先假设其可改造为单调不降序列,若成立则输出YES,不成立再假设其可改造为单调不增序列,若成立则输出 ...
- Eugeny and Array(水题,注意题目描述即可)
Eugeny has array a = a1, a2, ..., an, consisting of n integers. Each integer ai equals to -1, or to ...
- Educational Codeforces Round 69 (Rated for Div. 2) C. Array Splitting 水题
C. Array Splitting You are given a sorted array
- Distinct Substrings SPOJ - DISUBSTR(后缀数组水题)
求不重复的子串个数 用所有的减去height就好了 推出来的... #include <iostream> #include <cstdio> #include <sst ...
- [CTS2019]珍珠(NTT+生成函数+组合计数+容斥)
这题72分做法挺显然的(也是我VP的分): 对于n,D<=5000的数据,可以记录f[i][j]表示到第i次随机有j个数字未匹配的方案,直接O(nD)的DP转移即可. 对于D<=300的数 ...
随机推荐
- Agc007_C Pushing Balls
传送门 题目大意 在一条直线上有$N$个球和$N+1$个洞,每两个球之间有一个洞,每两个洞之间有一个球,最左端和最右端都是洞,其中产生的$2N$个间隔满足从左到右是等差数列.你每次随机选择一个未被推进 ...
- C#异步编程(二)用户模式线程同步
基元线程同步构造 多个线程同时访问共享数据时,线程同步能防止数据损坏.不需要线程同步是最理想的情况,因为线程同步存在许多问题. 第一个问题就是它比较繁琐,而且很容易写错. 第二个问题是,他们会损害性能 ...
- ubuntu nfs server config
(1)#sudo apt-get install nfs-kernel-server 打开/etc/exports文件,在末尾加入: /home/hyq *(rw,sync,no_root_squas ...
- Spring基础知识之装配Bean
装配(wiring):创建应用对象之间协作关系的行为.这是依赖注入的本质. Spring配置的可选方案 Spring提供了三种装配机智: 1)在XML中进行显示装配 2)在java中进行显示装配 3) ...
- Pix mesa 自动化测试
最近在准备PIX的认证, 需要进行mesa测试. 但是Mesa的标准测试工具中没有针对PIX的TestCase, 只是提到NIST的web测试.路径为:http://pixpdqtests.nist. ...
- 蓝桥杯 算法训练 ALGO-156 表达式计算
算法训练 表达式计算 时间限制:1.0s 内存限制:256.0MB 问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输 ...
- Python内置函数:read()
文章转载于:http://blog.csdn.net/sxingming/article/details/51337768(博主:快递小哥) 1> >>> f=open(r&q ...
- Django基础(四)
Form表单 Admin Django Form表单 django 中的form 一般有两种功能: 输入html 验证用户输入 1,先写一个form import re from django ...
- 2016.5.30实现透明Panel及控件置顶的方法
想放置一个透明Panel在某控件上端,实现效果是可透过此Panel看见下面控件,但鼠标点击却无任何反应. 1.新建置自定义Panel类 using System; using System.Colle ...
- javascipt——对象的概念——数组
一.Array 特点: 数组的长度是可变的: 数组的索引可以是数字.字符串: 数组的内容可以是任意内容: 可以通过索引获取之前不存在的一个位置,其值为undefined: 1.构造函数: new Ar ...