BZOJ5322 [Jxoi2018]排序问题 【贪心】
题目链接
题解
意思就是使有序的排列尽量少
就是使相同的数尽量少
然后大力贪心即可
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define REP(i,n) for (register int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
#define res register
using namespace std;
const int maxn = 200005,maxm = 10200005,INF = 1000000000,P = 998244353;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,m,l,r,a[maxn];
int fac[maxm];
inline int qpow(int a,int b){
int ans = 1;
for (; b; b >>= 1,a = 1ll * a * a % P)
if (b & 1) ans = 1ll * ans * a % P;
return ans;
}
void init(){
fac[0] = 1;
for (res int i = 1; i < maxm; i++)
fac[i] = 1ll * fac[i - 1] * i % P;
}
int bac[maxm],tail;
int main(){
init();
int T = read(),ans;
while (T--){
n = read(); m = read(); l = read(); r = read(); ans = fac[n + m];
REP(i,n) a[i] = read();
sort(a + 1,a + 1 + n);
tail = 0; int cnt = 0,M = 0;
for (res int i = 1; i <= n; i++){
if (i != 1 && a[i] != a[i - 1]){
if (a[i - 1] >= l && a[i - 1] <= r){
bac[cnt]++,tail++,M = max(M,cnt);
}
else ans = 1ll * ans * qpow(fac[cnt],P - 2) % P;
cnt = 1;
}
else cnt++;
}
if (a[n] >= l && a[n] <= r){
bac[cnt]++,tail++,M = max(M,cnt);
}
else ans = 1ll * ans * qpow(fac[cnt],P - 2) % P;
bac[0] += r - l + 1 - tail;
for (res int i = 0; m; i++,M = max(M,i)){
if (M == i){
int tot = m / bac[i],lef = m - tot * bac[i];
ans = 1ll * ans * qpow(qpow(fac[i + tot],P - 2),bac[i] - lef) % P;
ans = 1ll * ans * qpow(qpow(fac[i + tot + 1],P - 2),lef) % P;
bac[i] = M = 0;
break;
}
if (bac[i] >= m){
bac[i + 1] += m;
bac[i] -= m;
m = 0;
}
else {
m -= bac[i];
bac[i + 1] += bac[i];
bac[i] = 0;
}
}
for (res int i = 0; i <= M; i++){
ans = 1ll * ans * qpow(qpow(fac[i],P - 2),bac[i]) % P;
bac[i] = 0;
}
printf("%d\n",ans);
}
return 0;
}
BZOJ5322 [Jxoi2018]排序问题 【贪心】的更多相关文章
- BZOJ5322: [JXOI2018]排序问题
传送门 不难看出期望就是 \(\frac{(n+m)!}{\prod_{v=1}^{max}(cnt_v!)}\),\(cnt_v\) 表示 \(v\) 这个数出现的次数. 贪心就是直接把 \(m\) ...
- BZOJ5322 JXOI2018排序问题
对于一个序列,重排后有序的概率显然是∏cnti!/n!,其中cnti为第i种数出现次数.要使概率最小,显然应该让各种数字尽量平均分配.剩下的是div2BC左右的大讨论. #include<ios ...
- 【BZOJ5322】[JXOI2018]排序问题(模拟)
[BZOJ5322][JXOI2018]排序问题(模拟) 题面 BZOJ 洛谷 题解 这题就显得很呆. 显然就是每次找到\([l,r]\)中出现次数最小的那个数并且放一个. 然后随便模拟一下就好了Qw ...
- 5322: [Jxoi2018]排序问题
5322: [Jxoi2018]排序问题 链接 分析: 每次选一个出现次数最小的. 代码: #include<cstdio> #include<algorithm> #incl ...
- BZOJ5322:[JXOI2018]排序问题——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5322 https://loj.ac/problem/2543 <-可以看数据,要没有这数据我 ...
- 【JXOI2018】排序问题 贪心
我们令$sum_i$表示数字i在加完数字的数列中出现的次数,那么答案显然为$\dfrac{(n+m)!}{\sum_{i=0}^{\infty}sum_i!}$ 不难发现,当每次添加的数为$[l,r] ...
- 并不对劲的bzoj5322:loj2543:p4561:[JXOI2018]排序问题
题目大意 \(T\)(\(T\leq10^5\))组询问 每次给出\(n,m,l,r\),和\(n\)个数\(a_1,a_2,...,a_n\),要找出\(m\)个可重复的在区间\([l,r]\)的数 ...
- 洛谷P4561 [JXOI2018]排序问题(二分 期望)
题意 题目链接 Sol 首先一种方案的期望等于它一次排好的概率的倒数. 一次排好的概率是个数数题,他等于一次排好的方案除以总方案,也就是\(\frac{\prod cnt_{a_i}!}{(n+m)! ...
- [JXOI2018]排序问题
嘟嘟嘟 这是今天做的第二道九条可怜的题,现在对他的题的印象是:表面清真可做,实则毒瘤坑人. 首先要感谢吉司机,我期望学的特烂,好在样例直接告诉我们期望怎么求了. 令\(b_i\)表示第\(i\)个不同 ...
随机推荐
- PHP计算翻页
function fanye() { if ($total <= $num) { $list['curTotal'] = $total; } else { $offsetA = $start; ...
- Hadoop(19)-MapReduce框架原理-Combiner合并
1. Combiner概述 2. 自定义Combiner实现步骤 1). 定义一个Combiner继承Reducer,重写reduce方法 public class WordcountCombiner ...
- 网络编程之socket的运用
一,socket用法 socket是什么 ? Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐 ...
- 使用source命令解决mysql导入乱码问题
设定编码格式:mysql -u root -p --default-character-set=utf8 use dbname source /root/newsdata.sql
- 1511: [POI2006]OKR-Periods of Words
1511: [POI2006]OKR-Periods of Words https://www.lydsy.com/JudgeOnline/problem.php?id=1511 题意: 对于一个串的 ...
- spring data elasticsearch多索引查询
一次查询多个索引数据 es里可以这样写 GET 索引1,索引2,索引3/_search 也可以这样 给索引创建别名,多个索引可以使用一个别名 POST /_aliases { "action ...
- RTL8195AM开发板使用
1. 本次使用RTL8195AM测试一下,原厂资源地址:https://os.mbed.com/platforms/Realtek-RTL8195AM/ 2. 由于板子支持mbed,所以把CON2连接 ...
- "Cannot open source file "Wire.h" " in Arduino Development
0. Environment Windows 8 x64 Arduino 1.0.5 Visual studio 2012 Visual micro Arduino 1. Steps Add &quo ...
- 用gradle编译任意结构的Android项目
## 需求 * 继续用`Eclipse`项目的结构,但是使用`gradle`编译,或者说任意的项目结构进行编译. ## 解决方案 1. Android studio的项目结构 1. Android S ...
- 监控memcache服务
监控memcache服务是否正常,模拟用户(web客户端)检测. 使用nc命令加上set/get来模拟检测,以及监控响应时间及命中率. #!/bin/bash #################### ...