1. 程式人生 > 其它 >Educational Codeforces Round 113 (Rated for Div. 2) C. Jury Meeting

Educational Codeforces Round 113 (Rated for Div. 2) C. Jury Meeting

題目連結

  • 思路:

1.當擁有兩個以上的最大值時,永遠不會發生一個代表反覆發言的情況

2.當有一個最大值時,如果沒有max-1的話,沒有答案,且有max-1的情況中max-1必須在max後面,所以最後的答案和max - 1的個數有關

3.總情況有n!種,減去不合法的情況即為答案,設max-1的個數為k,則有k*(k-1)種排列,放在max後面的有k種,剩下的(k-1)個數有(k-1)!种放法,所以為k!,對於剩下的n-k-1個沒用的數字的位置,有種情況;乘上k!後,得到:,得到答案即為:

  • 程式碼:

#include <bits/stdc++.h>
#define
ll long long using namespace std; typedef pair<ll, ll> PII; const int N = 2e5 + 10, mod = 998244353; int n; ll a[N]; void solve() { cin >> n; ll maxx = 0; for (int i = 1; i <= n; i ++) { cin >> a[i]; maxx = max(a[i], maxx); } int cnt = 0
; int cntm = 0; for (int i = 1; i <= n; i ++) { if(a[i] == maxx - 1) cnt ++; if(a[i] == maxx) cntm ++; } ll ans = 1; ll base = 1; for (ll i = 1; i <= n; i ++) { ans = ans * i % mod; if(i != cnt + 1) base = (base
* i) % mod; } if(cntm == 1) cout << (ans - base + mod) % mod << endl; else cout << ans << endl; } int main() { ios::sync_with_stdio(0); int T; cin >> T; while(T --) { solve(); } return 0; }