题意描述:

给你一个长度为 \(n\) 的序列,让你从中选出 \(k\) 个数组成一个集合,定义这个集合的极限高度为\(a_i...a_k\) 的最大值。

让你求所有的集合极限高度 之和对 \(1000000007\) 的结果。

题解:

套路题 OR 水题

我们还是考虑一个数对答案的贡献,就是他作为区间最大值出现的次数。

假设,比 \(x\) 小的数有 \(m\) 个,那么从这 \(m\) 个数中选出 \(k-1\) 个数在和 \(x\) 这个数拼在一起组合成一个集合的最大值就是 \(x\)

选数的方案数为 \(C_{m}^{k-1}\) ,那么 \(x\) 这个数对答案的贡献就是 \(C_{m}^{k-1} \times x\)

先处理一下阶乘以及阶乘的逆元,在计算每个数对答案的贡献即可。

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define int long long
const int p = 1e9+7;
const int N = 1e5+10;
int n,k,ans,t;
int h[N],jz[N],inv[N];
inline int read()
{ int s = 0,w = 1; char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-') w = -1; ch = getchar();}
while(ch >= '0' && ch <= '9'){s = s * 10 + ch - '0'; ch = getchar();}
return s * w;
}
bool comp(int a,int b)
{
return a > b;
}
int ksm(int a,int b)
{
int res = 1;
for(; b; b >>= 1)
{
if(b & 1) res = res * a % p;
a = a * a % p;
}
return res;
}
void YYCH()
{
jz[0] = inv[0] = 1;
for(int i = 1; i <= N-5; i++) jz[i] = jz[i-1] * i % p;
inv[N-5] = ksm(jz[N-5],p-2);
for(int i = N-6; i >= 1; i--) inv[i] = inv[i+1] * (i+1) % p;
}
int C(int n,int m)
{
if(n < m) return 0;
if(n == 0 || m == 0) return 1;
return jz[n] * inv[n-m] % p * inv[m] % p;
}
signed main()
{
freopen("trees.in","r",stdin);
freopen("trees.out","w",stdout);
n = read(); k = read(); YYCH();
for(int i = 1; i <= n; i++) h[i] = read();
sort(h+1,h+n+1,comp);
for(int i = 1; i <= n; i++)
{
ans = (ans + C(n-i,k-1) * h[i] % p) % p;
}
printf("%lld\n",ans % p);
fclose(stdin); fclose(stdout);
return 0;
}

最新文章

  1. C#把 DataTable转换为Model实体
  2. php CGI、Fastcgi、PHP-FPM的详细介绍与之间的关系
  3. 简述Mesos API–files
  4. Spring MVC BeanNameUrlHandlerMapping example
  5. Apache CXF 3.0: CDI 1.1 Support as Alternative to Spring--reference
  6. 武汉科技大学ACM:1003: 看美女
  7. eclipse工程名出现小红叉的解决办法
  8. 重新认识JavaScript里的数据类型
  9. 在浏览器里点击input输入框输入,会展示默认的历史下拉菜单
  10. Go 语言数据类型
  11. angular 表单验证
  12. 【alpha阶段】第八次Scrum Meeting
  13. Unexpected end of JSON input while parsing near
  14. wordpress +window 走起~
  15. 分频器的verilog设计
  16. 循序渐进学.Net Core Web Api开发系列【16】:应用安全续-加密与解密
  17. 【Python 爬虫系列】从某网站下载小说《鬼吹灯》,正则解析html
  18. 大家都对vertical-align的各说各话
  19. cin中函数的作用
  20. jsp取不到值栈的值

热门文章

  1. 万字长文,一篇文章带你入门Python
  2. GET和POST的本质区别
  3. Unity 移动平台自己编写Shader丢失问题
  4. 敏捷转型谁先动:老总,项目经理or团队
  5. 小花梨判连通 (bfs+思维+map统计数量)
  6. 三年前买的T440p目前淘宝二手价2300左右
  7. 2020BJDCTF
  8. H5游戏定制,4大优势助力企业曝光10W+
  9. 使用SVG symbols建立图标系统
  10. Mybatis注解开发相关