P2915 Mixed Up Cows 题解
Introduction
这题一眼,最多绿题,恶评。
简化题意:给定一个长度为 n n n 的数列 a a a 和常数 k k k,求满足以下条件的排列个数:
- 对于每个 i ∈ { x ∣ x ∈ [ 2 , n ] ∩ Z } i \in \{x \ | \ x \in [2, n] \cap \Z\} i∈{x ∣ x∈[2,n]∩Z},满足 ∣ a i − a i − 1 ∣ > k |a_i - a_{i - 1}| > k ∣ai−ai−1∣>k。
其中, n ≤ 16 n \leq 16 n≤16。
Idea
显然 n ≤ 16 n \leq 16 n≤16,考虑状压 dp。
再考虑状态:设 dp status , j \text{dp}_{\texttt{status}, j} dpstatus,j 为当前选了那些(表示为 status \texttt{status} status),当前选的是什么(表示为 j j j)。
转移方程也就脱口而出了。
dp status , j = ∑ k = 2 n dp status − 2 k , k × [ k ≠ j ] . \text{dp}_{\texttt{status}, j} = \sum^n_{k=2}\text{dp}_{\texttt{status} - 2^k, k} \times [k \neq j]. dpstatus,j=k=2∑ndpstatus−2k,k×[k=j].
,其中 [ expression ] [\texttt{expression}] [expression] 为布尔表达式。
Solution
超长板子求原谅 /kk
#include <bits/stdc++.h>
#define int long long
// FOR templates.
#define rep(i, s, n, k) for(int i = s;i <= n;i += k)
#define repn(i, s, n, k) for(int i = s;i < n;i += k)
#define pre(i, s, n, k) for(int i = s;i >= n;i -= k)
#define pren(i, s, n, k) for(int i = s;i > n;i -= k)
// Abbr for STL.
#define pii pair<int, int>
#define pdd pair<double, double>
#define mpi map<int, int>
#define vc vector<int>
// IO templates, proven very useful.
#define cn(n) int n;cin >> n
#define cm(n) cin >> n
#define debug if(isdebug)cout
// Abbr for funcs.
#define pb push_back
#define mset memset
#define multitst() cn(t);while(t--)
// #define files
using namespace std;
const int MAXN = 0x3f3f3f3f3f3f3f3fLL;
const int MOD1 = 1000000007LL;
const int MOD2 = 998244353LL;
const int isdebug = 0LL;
int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int gcd(int a, int b) {if(b == 0) return a;return gcd(b, a % b);}
inline int lowbit(int x) {return x & (-x);}
inline int lcm(int a, int b) {return a * b / gcd(a, b);}
int dp[1 << 21][21], s[21], n, l;
signed main(){
#ifdef files
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cm(n); cm(l);
repn(i, 0, n, 1) cm(s[i]);
repn(i, 0, n, 1) dp[1 << i][i] = 1;
repn(i, 1, 1 << n, 1){
if(__builtin_popcount(i) == 1) continue;
repn(j, 0, n, 1){
if(!((i >> j) & 1)) continue;
repn(k, 0, n, 1){
if(k != j && (i >> j) & 1 && abs(s[j] - s[k]) > l){
dp[i][j] += dp[i - (1 << j)][k];
}
}
}
}
int ans = 0;
repn(i, 0, n, 1)
ans += dp[(1 << n) - 1][i];
cout << ans << endl;
#ifdef files
fclose(stdin); fclose(stdout);
#endif
return 0;
}
/*
* things to check
* 1. int overflow or long long memory need
* 2. recursion/array/binary search/dp/loop bounds
* 3. precision
* 4. special cases(n=1,bounds)
* 5. delete debug statements
* 6. initialize(especially multi-tests)
* 7. = or == , n or m ,++ or -- , i or j , > or >= , < or <=
* 8. keep it simple and stupid
* 9. do not delete, use // instead
* 10. operator priority
* 11. is there anything extra to output?
* 12. THINK TWICE CODE ONCE, THINK ONCE DEBUG FOREVER
* 13. submit ONCE, AC once. submit twice, WA forever
* 14. calm down and you'll get good rank
* 15. even a bit wrong scores zero
* 16. ...
**/
/*
* something to think about
* 1. greedy? dp? searching? dp with matrix/ segment tree? binary search? ...?
* 2. If it is difficult, why not the opposite?
**/