题目链接

BZOJ5322

题解

意思就是使有序的排列尽量少

就是使相同的数尽量少

然后大力贪心即可

#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]排序问题 【贪心】的更多相关文章

  1. BZOJ5322: [JXOI2018]排序问题

    传送门 不难看出期望就是 \(\frac{(n+m)!}{\prod_{v=1}^{max}(cnt_v!)}\),\(cnt_v\) 表示 \(v\) 这个数出现的次数. 贪心就是直接把 \(m\) ...

  2. BZOJ5322 JXOI2018排序问题

    对于一个序列,重排后有序的概率显然是∏cnti!/n!,其中cnti为第i种数出现次数.要使概率最小,显然应该让各种数字尽量平均分配.剩下的是div2BC左右的大讨论. #include<ios ...

  3. 【BZOJ5322】[JXOI2018]排序问题(模拟)

    [BZOJ5322][JXOI2018]排序问题(模拟) 题面 BZOJ 洛谷 题解 这题就显得很呆. 显然就是每次找到\([l,r]\)中出现次数最小的那个数并且放一个. 然后随便模拟一下就好了Qw ...

  4. 5322: [Jxoi2018]排序问题

    5322: [Jxoi2018]排序问题 链接 分析: 每次选一个出现次数最小的. 代码: #include<cstdio> #include<algorithm> #incl ...

  5. BZOJ5322:[JXOI2018]排序问题——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5322 https://loj.ac/problem/2543 <-可以看数据,要没有这数据我 ...

  6. 【JXOI2018】排序问题 贪心

    我们令$sum_i$表示数字i在加完数字的数列中出现的次数,那么答案显然为$\dfrac{(n+m)!}{\sum_{i=0}^{\infty}sum_i!}$ 不难发现,当每次添加的数为$[l,r] ...

  7. 并不对劲的bzoj5322:loj2543:p4561:[JXOI2018]排序问题

    题目大意 \(T\)(\(T\leq10^5\))组询问 每次给出\(n,m,l,r\),和\(n\)个数\(a_1,a_2,...,a_n\),要找出\(m\)个可重复的在区间\([l,r]\)的数 ...

  8. 洛谷P4561 [JXOI2018]排序问题(二分 期望)

    题意 题目链接 Sol 首先一种方案的期望等于它一次排好的概率的倒数. 一次排好的概率是个数数题,他等于一次排好的方案除以总方案,也就是\(\frac{\prod cnt_{a_i}!}{(n+m)! ...

  9. [JXOI2018]排序问题

    嘟嘟嘟 这是今天做的第二道九条可怜的题,现在对他的题的印象是:表面清真可做,实则毒瘤坑人. 首先要感谢吉司机,我期望学的特烂,好在样例直接告诉我们期望怎么求了. 令\(b_i\)表示第\(i\)个不同 ...

随机推荐

  1. PHP解决跨域问题

    在做项目的过程中经常需要跨域访问.这里主要介绍一下 PHP 中怎么解决跨域问题. 1.允许所有域名访问 header('Access-Control-Allow-Origin: *'); 2.允许单个 ...

  2. 【GUI】一、Swing外观框架BeautyEye使用

    一.Swing外观框架BeautyEye使用 1.1 导包 BeautyEye.jar 1.2 使用BeautyEye L&F public static void main(String[] ...

  3. 函数:引用file类对象及io类对象作为参数打印文本及显示文本

    #include <iostream> #include <fstream> #include <cstdlib> using namespace std; voi ...

  4. STL 入门 (17 暑假集训第一周)

    快速全排列的函数 头文件<algorithm> next_permutation(a,a+n) ---------------------------------------------- ...

  5. 【Java】Spring MVC 扩展和SSM框架整合

    开发web项目通常很多地方需要使用ajax请求来完成相应的功能,比如表单交互或者是复杂的UI设计中数据的传递等等.对于返回结果,我们一般使用JSON对象来表示,那么Spring MVC中如何处理JSO ...

  6. SAPの販売管理で、価格設定をするまでの関連カスタマイズ画面

    この記事ではSAP SDで.価格を決めるまでに必要な設定画面について述べています. condition table (条件テーブル) 条件レコードのキー項目を定義したもの.3桁の数字で名前がついている ...

  7. java加密用到了BASE64Decoder时报错信息:Access restriction: The type BASE64Encoder is not accessible due to restrict

    在Eclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示: Access restriction : ...

  8. Uber CEO博鳌论坛采访:看好中国市场共享经济的发展模式

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. kafka监听类

    package com.datad.dream.service; import com.alibaba.fastjson.JSON; import com.datad.dream.dao.KafkaI ...

  10. mysql连接jdbc查询代码

    package com.answer.test; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.S ...