题意:给定一个序列,让你求两种数,一个是求一个子序列,包含最大值和最小值,再就是求一个子集包含最大值和最小值。

析:求子序列,从前往记录一下最大值和最小值的位置,然后从前往后扫一遍,每个位置求一下数目就好。

求子集可以用排列组合解决,很简单,假设最大值个数是 n,最小值的数是 m,总数是 N,答案就是 (2^n-1) * (2^m-1)*2^(N-m-n),

当然要特殊判断最大值和最小值相等的时候。

当然也可以用容斥来求,就是总数 - 不是最大值的数目 - 不是最小值的数目 + 不是最大值也不是最小值的数目,其实也差不多

代码如下:

排列组合:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #define debug() puts("++++");
  18. #define gcd(a, b) __gcd(a, b)
  19. #define lson l,m,rt<<1
  20. #define rson m+1,r,rt<<1|1
  21. #define freopenr freopen("in.txt", "r", stdin)
  22. #define freopenw freopen("out.txt", "w", stdout)
  23. using namespace std;
  24.  
  25. typedef long long LL;
  26. typedef unsigned long long ULL;
  27. typedef pair<int, int> P;
  28. const int INF = 0x3f3f3f3f;
  29. const LL LNF = 1e17;
  30. const double inf = 0x3f3f3f3f3f3f;
  31. const double PI = acos(-1.0);
  32. const double eps = 1e-8;
  33. const int maxn = 1e5 + 10;
  34. const int mod = 1000000007;
  35. const int dr[] = {-1, 0, 1, 0};
  36. const int dc[] = {0, 1, 0, -1};
  37. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  38. int n, m;
  39. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  40. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  41. inline bool is_in(int r, int c){
  42. return r >= 0 && r < n && c >= 0 && c < m;
  43. }
  44.  
  45. LL fast_pow(int n){
  46. LL a = 2, ans = 1;
  47. while(n){
  48. if(n & 1) ans = ans * a % mod;
  49. n >>= 1;
  50. a = a * a % mod;
  51. }
  52. return ans;
  53. }
  54.  
  55. int a[maxn];
  56. vector<int> v1, v2;
  57.  
  58. int main(){
  59. int T; cin >> T;
  60. while(T--){
  61. scanf("%d", &n);
  62. int mmin = mod, mmax = 0;
  63. for(int i = 0; i < n; ++i){
  64. scanf("%d", a+i);
  65. mmin = min(mmin, a[i]);
  66. mmax = max(mmax, a[i]);
  67. }
  68. v1.clear(); v2.clear();
  69. for(int i = 0; i < n; ++i)
  70. if(mmin == a[i]) v1.push_back(i);
  71. else if(mmax == a[i]) v2.push_back(i);
  72. if(v1.size() == n){
  73. LL ans1 = (LL)n * (n+1) / 2 % mod;
  74. LL ans2 = (fast_pow(n) - 1 % mod) % mod;
  75. printf("%lld %lld\n", ans1, ans2);
  76. continue;
  77. }
  78. LL ans2 = (fast_pow(v1.size())-1) * (fast_pow(v2.size())-1) % mod * fast_pow(n-v1.size()-v2.size()) % mod;
  79. ans2 = (ans2 + mod) % mod;
  80. int i = 0, j = 0, pre = 0;
  81. LL ans1 = 0;
  82. while(true){
  83. int t1 = min(v1[i], v2[j]);
  84. int t2 = max(v1[i], v2[j]);
  85. ans1 = (ans1 + (LL)(t1-pre+1) * (n-t2)) % mod;
  86. v1[i] < v2[j] ? ++i : ++j;
  87. if(i == v1.size() || v2.size() == j) break;
  88. pre = min(t1+1, min(v1[i], v2[j]));
  89. }
  90. printf("%lld %lld\n", ans1, ans2);
  91. }
  92. return 0;
  93. }

  

容斥:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #define debug() puts("++++");
  18. #define gcd(a, b) __gcd(a, b)
  19. #define lson l,m,rt<<1
  20. #define rson m+1,r,rt<<1|1
  21. #define freopenr freopen("in.txt", "r", stdin)
  22. #define freopenw freopen("out.txt", "w", stdout)
  23. using namespace std;
  24.  
  25. typedef long long LL;
  26. typedef unsigned long long ULL;
  27. typedef pair<int, int> P;
  28. const int INF = 0x3f3f3f3f;
  29. const LL LNF = 1e17;
  30. const double inf = 0x3f3f3f3f3f3f;
  31. const double PI = acos(-1.0);
  32. const double eps = 1e-8;
  33. const int maxn = 1e5 + 10;
  34. const int mod = 1000000007;
  35. const int dr[] = {-1, 0, 1, 0};
  36. const int dc[] = {0, 1, 0, -1};
  37. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  38. int n, m;
  39. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  40. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  41. inline bool is_in(int r, int c){
  42. return r >= 0 && r < n && c >= 0 && c < m;
  43. }
  44.  
  45. LL fast_pow(int n){
  46. LL a = 2, ans = 1;
  47. while(n){
  48. if(n & 1) ans = ans * a % mod;
  49. n >>= 1;
  50. a = a * a % mod;
  51. }
  52. return ans;
  53. }
  54.  
  55. int a[maxn];
  56. vector<int> v1, v2;
  57.  
  58. int main(){
  59. int T; cin >> T;
  60. while(T--){
  61. scanf("%d", &n);
  62. int mmin = mod, mmax = 0;
  63. for(int i = 0; i < n; ++i){
  64. scanf("%d", a+i);
  65. mmin = min(mmin, a[i]);
  66. mmax = max(mmax, a[i]);
  67. }
  68. v1.clear(); v2.clear();
  69. for(int i = 0; i < n; ++i)
  70. if(mmin == a[i]) v1.push_back(i);
  71. else if(mmax == a[i]) v2.push_back(i);
  72. if(v1.size() == n){
  73. LL ans1 = (LL)n * (n+1) / 2 % mod;
  74. LL ans2 = (fast_pow(n) - 1 % mod) % mod;
  75. printf("%lld %lld\n", ans1, ans2);
  76. continue;
  77. }
  78. LL ans2 = fast_pow(n);
  79. ans2 = (ans2 - fast_pow(n-v1.size()) - fast_pow(n-v2.size()) + fast_pow(n-v1.size()-v2.size())) % mod;
  80. ans2 = (ans2 % mod + mod) % mod;
  81. int i = 0, j = 0, pre = 0;
  82. LL ans1 = 0;
  83. while(true){
  84. int t1 = min(v1[i], v2[j]);
  85. int t2 = max(v1[i], v2[j]);
  86. ans1 = (ans1 + (LL)(t1-pre+1) * (n-t2)) % mod;
  87. v1[i] < v2[j] ? ++i : ++j;
  88. if(i == v1.size() || v2.size() == j) break;
  89. pre = min(t1+1, min(v1[i], v2[j]));
  90. }
  91. printf("%lld %lld\n", ans1, ans2);
  92. }
  93. return 0;
  94. }

  

SPOJ - AMR11H Array Diversity (水题排列组合或容斥)的更多相关文章

  1. SPOJ - AMR11H Array Diversity (排列组合)

    题意:给定n个数,求包含最大值和最小值的子集(数字连续)和子序列(数字不连续)的个数. 分析: 1.如果n个数都相同,则子集个数为N * (N + 1) / 2,子序列个数为2N-1. 2.将序列从头 ...

  2. SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  3. 2018 湖南网络比赛题 HDU - 6286 (容斥)

    题意:不说了. 更加偏向于数学不好的小可爱来理解的. 这篇博客更加偏重于容斥的讲解.用最直观的数学方法介绍这个题. 思路: 在a<=x<=b. c<=y<=d 中满足  x*y ...

  4. 【BZOJ4927】第一题 双指针+DP(容斥?)

    [BZOJ4927]第一题 Description 给定n根直的木棍,要从中选出6根木棍,满足:能用这6根木棍拼 出一个正方形.注意木棍不能弯折.问方案数. 正方形:四条边都相等.四个角都是直角的四边 ...

  5. 【HDU 5532 Almost Sorted Array】水题,模拟

    给出一个序列(长度>=2),问去掉一个元素后是否能成为单调不降序列或单调不增序列. 对任一序列,先假设其可改造为单调不降序列,若成立则输出YES,不成立再假设其可改造为单调不增序列,若成立则输出 ...

  6. Eugeny and Array(水题,注意题目描述即可)

    Eugeny has array a = a1, a2, ..., an, consisting of n integers. Each integer ai equals to -1, or to ...

  7. Educational Codeforces Round 69 (Rated for Div. 2) C. Array Splitting 水题

    C. Array Splitting You are given a sorted array

  8. Distinct Substrings SPOJ - DISUBSTR(后缀数组水题)

    求不重复的子串个数 用所有的减去height就好了 推出来的... #include <iostream> #include <cstdio> #include <sst ...

  9. [CTS2019]珍珠(NTT+生成函数+组合计数+容斥)

    这题72分做法挺显然的(也是我VP的分): 对于n,D<=5000的数据,可以记录f[i][j]表示到第i次随机有j个数字未匹配的方案,直接O(nD)的DP转移即可. 对于D<=300的数 ...

随机推荐

  1. Java模版引擎:jsp、freemarker、velocity区别

    在java领域,表现层技术主要有三种:jsp.freemarker.velocity. jsp是大家最熟悉的技术优点:1.功能强大,可以写java代码2.支持jsp标签(jsp tag)3.支持表达式 ...

  2. 流畅的python之序列

    python对开发者友好的根源在于:1.序列的泛型操作2.内置的元组和映身类型3.用缩进来架构的源码4.无需变量声明的强类型 序列数据共用的一套丰富的操作:迭代.切片.排序和拼接.内置序列类型:1.容 ...

  3. 安装成功的nginx如何添加未编译模块?

    在重启nginx后发生了错误,错误如下: nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /u ...

  4. Pager分页

    分页组件: /// <summary> /// 分页组件 /// </summary> public class PagerHelper { /// <summary&g ...

  5. iOS 【资源篇】

    iOS9开发入门教程索引 Objective-C视频教程 O-c Blog 社区 畅游  http://www.9ria.com/ 苹果中文开发社区  http://www.cocoachina.co ...

  6. Rails的静态资源管理(二)—— 如何使用 Asset Pipeline

    官方文档:http://guides.ruby-china.org/asset_pipeline.html http://guides.rubyonrails.org/asset_pipeline.h ...

  7. SQL基础(1)

    1.SQL简介 (1)什么是SQL? SQL指结构化查询语言 SQL使我们有能力访问数据库 SQL是一种 ANSI 的标准计算机语言 (2)SQL 能做什么? SQL面向数据库执行查询 SQL可从数据 ...

  8. CentOS7 线上环境的一些 配置

    上周服务器被攻击导致上面收回了我们服务器的IP,所以这周重新安装部署了服务器,使用centos7系统.为了防止服务器再次被攻击,所以建议以下几点: 1. root密码要复杂一点,尽量字母数字特殊字符都 ...

  9. appium_python_android测试环境搭建

    第一步  安装appium •Appium是由.NET 开发的,所以,它会依赖 .NET framework相关组件,所以先安装.net framework 4.5,备注: Appium最低支持.ne ...

  10. redux使用教程详细介绍

    本文介绍redux的使用 安装 cnpm install redux --save cnpm install react-redux --save cnpm install redux-devtool ...