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

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

求子集可以用排列组合解决,很简单,假设最大值个数是 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;
}

  

最新文章

  1. echarts饼图
  2. 如何获取ResultSet的行数和列数
  3. 将文本文件的内容存储在DataSet中的方法总结
  4. Activity、Task、应用和进程
  5. SAP 关于标准成本、计划成本、目标成本、实际成本
  6. 《day14---多线程入门_进阶》
  7. html5 touch事件实现触屏页面上下滑动(一)
  8. [Effective C++ --015]在资源管理类中提供对原始资源的访问
  9. Mybatis 打开连接池和关闭连接池性能对比
  10. Linux内核中的list用法和实现分析
  11. Codeforces Gym10008E Harmonious Matrices(高斯消元)
  12. 64位WIN7+oracle11g+plsql
  13. 玩玩RMI
  14. kafka 集群部署 多机多broker模式
  15. Dijango学习_01_pycharm创建应用
  16. Bioconductor软件安装与升级
  17. luogu P1486 [NOI2004]郁闷的出纳员
  18. LeetCode112.路径总和
  19. tomcat7修改tomcat-users.xml文件,但服务器重启后又自动还原了。
  20. .net图表之ECharts随笔05-不同01的语法步骤

热门文章

  1. mysql_union all 纵向合并建表_20170123
  2. [TopCoder12727]FoxAndCity
  3. 【转】浅谈Java中的equals和==
  4. POJ3111(最大化平均值)
  5. (转)C#用Linq实现DataTable的Group by数据统计
  6. Linux python安装
  7. 机器学习:模型泛化(LASSO 回归)
  8. python第十一天-----补:缓存操作
  9. Mysql教程:[1]下载安装配置详细教程
  10. [Apache]架设Apache服务器