銆岄瑙c€岮T4515 [AGC030F] Permutation and Minimum
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆岄瑙c€岮T4515 [AGC030F] Permutation and Minimum相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e4%bb%a3%e7%a0%81' title='浠g爜'>浠g爜
杞Щ include 缁熻 == 閾炬帴 搴忓垪 涓嶅悓 use瀛I浠ユ潵鐨勭涓€閬撻粦棰橈紝鍏堝湪杩欓噷绁竴涓嬨€?/p>
- 绗竴閬撻粦棰樼キ
浜庢槸灏辨墦绠楁潵鍐欓瑙c€?/p>
棰橀潰澶ф剰
鏈変竴涓?(2N) 涓暟鐨勫簭鍒?(A)锛屼粠 (1) 鍒?(2N) 鏍囧彿銆備綘瑕佹妸 (1 sim 2N) 杩欎簺鏁板~杩涘幓锛屼娇瀹冨舰鎴愪竴涓帓鍒椼€?/p>
浣嗘槸宸茬粡鏈変竴浜涗綅缃己鍒跺~浜嗙壒瀹氱殑鏁颁簡锛岃緭鍏ユ椂浼氱粰鍑恒€?/p>
鏈€鍚庝护闀垮害涓?(N) 鐨勫簭鍒?(B) 涓猴細浠?(B_i = min{A_{2 i - 1}, A_{2 i}})銆?/p>
璇㈤棶鎵€鏈夋柟妗堜腑鑳藉緱鍒扮殑涓嶅悓鐨?(B) 鐨勬暟閲忋€?/p>
(1 le N le 300)銆?/p>
鎬濊矾
鎴戜滑瑕佹眰涓€涓暱搴︿负 (N) 鐨勫簭鍒?(B)锛岀劧鍚庢槸搴忓垪 (A) 涓殑鏁颁袱涓ら厤瀵逛腑鐨勬渶灏忓€硷紝鎴戜滑鍏堣€冭檻鎶婁粬鍙樻垚鏈夊簭鐨勶紝鐒跺悗浠庡ぇ鍒板皬鏉ョ粺璁★紝鍥犱负濡傛灉浠庡皬鍒板ぇ鐨勮瘽鎴戜滑鍙兘浼氬宸茬粡缁熻杩囩殑鏁板瓧杩涜鍐嶆缁熻锛屽鑷寸瓟妗堝亸澶э紝鐒跺悗鍥犱负鏁板瓧澶у皬鏄皬浜庣瓑浜?600 鐨勶紝鎵€浠ユ垜浠彲浠ョ洿鎺ヤ笂妗舵潵鎼炪€?/p>
棣栧厛瀵逛簬 (A_{2i-1}) 鍜?(A_{2i}) 閮藉凡缁忕粰鍑虹殑鏁板鑲畾鏄笉浼氬绛旀浣滃嚭璐$尞鐨勶紝鎵€浠ユ垜浠爣璁颁竴涓嬶紝鍦ㄤ箣鍚庣粺璁$殑鏃跺€欎笉璁″叆绛旀涓紝鎵€浠ュ绛旀鑳戒綔鍑鸿础鐚殑鑲畾鏄鍚?锛?-1 , x ) 涓?锛?-1 , -1 锛夌殑褰㈠紡锛屾垜浠О鍓嶈€呬负褰㈠紡 1 锛?鍚庤€呬负褰㈠紡 2 銆傚浜庡悗鑰呮垜浠槸鑳藉闅忔剰鍙樻崲椤哄簭鐨勶紝鎵€浠ユ垜浠氨鍙互缁熻鏁伴噺鐒跺悗鏈€鍚庡绛旀涔樹笂杩欎釜鏁伴噺鐨勯樁涔樸€?/p>
鏄剧劧杩欐槸涓€涓鏁?DP 锛屾垜浠€冭檻濡備綍 DP銆?/p>
璁句竴涓姸鎬?(f_{i,j,k}) 琛ㄧず鎴戜滑宸茬粡缁熻鍒颁簡绗?(i) 涓暟锛岃繕鍓╀笅 (j) 瀵规垜浠凡缁忓~浜嗕竴涓暟鐨勫舰寮?2 鐨勬暟瀵癸紝 杩樻湁 (k) 瀵规病鏈夊~鐨勫舰寮?1 鐨勬暟瀵广€?/p>
閭d箞瀵逛簬褰㈠紡 1 锛屽拰褰㈠紡 2 鐨勬暟瀵癸紝鎴戜滑鍏堥澶勭悊鍑轰粬浠垎鍒湁澶氬皯涓€傜劧鍚庡啀灏嗗叾鏀惧叆涓€涓暟缁勪腑鏂逛究鏌ヨ鍒板簳鏄摢涓€绉嶃€?/p>
棣栧厛瀵逛簬涓€涓凡缁忓瓨鍦ㄤ笌褰㈠紡 1 鐨勬暟瀵逛腑鐨勬暟锛屾垜浠彲浠ュ~鎴栬€呬笉濉紝濡傛灉涓嶅~閭d箞鎴戜滑灏变細浠?(f_{i - 1, j, k}) 杞Щ鍒?(f_{i, j, k + 1}) 锛屽鏋滃~鐨勮瘽锛?鎴戜滑蹇呮媺涓€涓凡缁忓~浜嗕竴鍗婄殑褰㈠紡 2 鐨勬暟瀵硅繃鏉ユ嫾鎴愪竴涓畬鏁寸殑鏁板锛屾墍浠ヨ繖鏃?(j > 0) 锛岀劧鍚庢垜浠氨浼氫粠 (f_{i - 1, j ,k}) 杞Щ鍒?(f_{i, j - 1, k}) 銆?/p>
鐒跺悗灏辨槸瀵逛簬鍓╀笅鐨勬暟锛屼粬浠槸鑷敱鏁帮紝棣栧厛浠栦滑鍙互濉叆涓€涓┖鐨勬暟瀵癸紝閭d箞灏变細浠?(f_{i - 1, j, k}) 杞Щ鑷?(f_{i, j + 1, k}), 鍏舵鍙互濉叆涓€涓凡缁忓畬鎴愪簡涓€鍗婄殑褰㈠紡 2 鐨勬暟瀵癸紝姝ゆ椂 (j > 0) 锛岄偅涔堝氨浼氫粠 (f_{i - 1,j,k}) 杞Щ鑷?(f_{i , j-1, k}), 鏈€鍚庤繕鍙互濉叆涓€涓舰寮?1 鐨勬暟瀵癸紝涓旀鏃?(k > 0)锛岄偅涔堝氨浼氫粠 (f_{i - 1,j,k}) 杞Щ鑷?(f_{i, j, k - 1}) 锛屽張鍥犱负褰㈠紡 1 鐨勬暟瀵圭殑浣嶇疆鏄浐瀹氱殑锛?鑰屾鏃跺張鏈?(k) 涓褰㈠紡鐨勬暟瀵癸紝鎵€浠ユ垜浠湁 (k) 绉嶅~娉曪紝闇€瑕佸 (f_{i - 1, j , k} imes k) 鍚庤浆绉汇€?/p>
杈圭晫涓?(f _ {0 ,0 ,0} = 1) 锛屾敞鎰忓彇妯?/p>
涓嬩负浠g爜
#include <bits/stdc++.h>
using namespace std;
#define R register int
const int N = 300 + 10, mod = 1e9 + 7;
int f[N << 1][N][N], a[N << 1], cnt = 0, tot = 0;
int num = 0, b[N << 1];
bool vis[N << 1], use[N << 1];
signed main() {
int n; scanf("%d", &n);
for(R i = 1; i <= n; i ++) {
int y = i << 1, x = y - 1;
scanf("%d%d", &a[x], &a[y]);
if(a[x] == -1 && a[y] == -1) cnt ++; //瀵瑰舰寮?2 鐨勬暟瀵硅繘琛岀粺璁? else if(a[x] > 0 && a[y] > 0) vis[a[x]] = vis[a[y]] = true; //濡傛灉涓や釜鏁伴兘宸茬粡纭畾鍒欐墦涓婃爣璁? else {
tot ++;
if(a[x] == -1) use[a[y]] = true;
else use[a[x]] = true;
}//瀵逛簬褰㈠紡 1 鐨勬暟瀵硅繘琛岀粺璁★紝骞朵笖缁熻鍑鸿繖绉嶆暟瀵逛腑涓嶄负 -1 鐨勬暟鏄摢浜? }
int n2 = 0; f[0][0][0] = 1; n <<= 1;
for(R i = n; i >= 1; i --)
if(!vis[i]) b[++ n2] = i;
//灏嗕細瀵圭瓟妗堜綔鍑鸿础鐚殑鏁拌繘琛屼粠澶у埌灏忕殑鎺掑簭锛? for(R i = 1; i <= n2; i ++)
for(R j = 0; j <= cnt + tot; j ++) //鑷敱鏁拌兘濉叆鎵€鏈変細瀵圭瓟妗堥€犳垚璐$尞鐨勬暟
for(R k = 0; k <= tot; k ++) {
if(use[b[i]]) {
f[i][j][k + 1] = (f[i][j][k + 1] + f[i - 1][j][k]) % mod;
if(j > 0) f[i][j - 1][k] = (f[i - 1][j][k] + f[i][j - 1][k]) % mod;
}
else {
f[i][j + 1][k] += (f[i - 1][j][k] + f[i][j + 1][k]) % mod;
if(j > 0) f[i][j - 1][k] = (f[i - 1][j][k] + f[i][j - 1][k]) % mod;
if(k > 0) f[i][j][k - 1] = (f[i][j][k - 1] + 1ll * f[i - 1][j][k] * k % mod) % mod;
}
}
int ans = f[n2][0][0];
for(R i = 1; i <= cnt; i ++) ans = (1ll * ans * i) % mod;// 涔樹笂褰㈠锛?-1 , -1 锛夌殑鏁板鐨勯樁涔? printf("%d
", ans);
return 0;
}
以上是关于銆岄瑙c€岮T4515 [AGC030F] Permutation and Minimum的主要内容,如果未能解决你的问题,请参考以下文章