位运算优先级问题
类别 | 运算符 | 结合性 |
---|---|---|
后缀 | () [] -> . ++ - - | |
一元 | + - ! ~ ++ - - (type)* & sizeof | |
乘除 | * / % | |
加减 | + - | |
移位 | << >> | |
关系 | < <= > >= | |
相等 | == != | |
位运算 | & ^ | | |
逻辑运算 | && || |
注意事项
- 先加减后移位
- 先移位后相等
- 先相等后位运算
- 先与后或
棋盘式(基于连通性)的dp
小国王
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#define int long long
using namespace std;
const int N = 12, M = 1 << 10, K = 110;
int n, m;
vector<int> state;//合法状态
int cnt[M];//每个状态包含的国王数量
vector<int> head[M];//每个状态能够转移到的状态
int f[N][K][M];
int count(int state)
{
int res = 0;
for(int i = 0; i < n; i ++ ) res += state >> i & 1;
return res;
}
main()
{
//所有只摆在前i行,已经摆了j个国王,并且第j行的状态为s的所有方案的集合
//相邻两行不能攻击到,a&b == 0, (a|b) 不能有两个相邻的1
cin >> n >> m;
for(int i = 0 ; i < 1 << n; i ++ )
if(!(i & i >> 1))
{
state.push_back(i);
cnt[i] = count(i);
}
for(int i = 0; i < state.size(); i ++ )
for(int j = 0; j < state.size(); j ++ )
{
int a = state[i], b = state[j];
if((a & b) == 0 && ((a | b) & (a | b) >> 1) == 0)
head[i].push_back(j);
}
f[0][0][0] = 1;
for(int i = 1; i <= n + 1; i ++ )
for(int j = 0; j <= m; j ++ )
for(int a = 0; a < state.size(); a ++ )
for(auto b: head[a])
{
int c = cnt[state[a]];
if(j >= c)
f[i][j][a] += f[i - 1][j - c][b];
}
cout << f[n + 1][m][0] << endl;//n+1行啥都不摆的方案数
}
玉米田
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 14;
const int p = 1e8;
const int M = 1 << 14;
int m, n;
vector<int> state[N];
int dp[N][M];
int main()
{
cin >> m >> n;
for(int k = 1; k <= m; k ++)
{
int ans = 0;
for(int j = 1; j <= n; j ++)
{
int index;
cin >> index;
ans = (ans << 1) + index;
}
for(int i = 0; i < 1 << n; i ++ )
{
if((i | ans) == ans && (i & i >> 1) == 0)//位运算一定要括号起来,不然优先级乱了
state[k].push_back(i);
}
}
state[0].push_back(0);
state[m + 1].push_back(0);
dp[0][0] = 1;
for(int i = 1; i <= m + 1; i ++ )
for(auto a: state[i])
for(auto b: state[i - 1])
{
if((a & b) == 0)//位运算一定要括号起来
dp[i][a] += dp[i - 1][b];
dp[i][a] %= p;
}
cout << dp[m + 1][0];
}
炮兵阵地
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 13, M = 1 << 10;
int n, m;
int g[110];
vector<int> state;
int f[2][M][M];
int cnt[M];
int count(int state)
{
int res = 0;
for(int i = 0; i < m; i ++ ) res += state >> i & 1;
return res;
}
int main()
{
//1. 山地不能部署炮兵
//2. 相互之间不能靠近(两层)
//3. 求最多摆放的数量
//4. 滚动数组:当成不滚动的,然后第一维&1
cin >> n >> m;
for(int i = 1; i <= n; i ++ )
for(int j = 0; j < m; j ++ )
{
char c;
cin >> c;
if(c == 'H') g[i] += 1 << j;//移位优先级不高
}
for(int i = 0; i < 1 << m; i ++ )
if(!((i & i << 1) || (i & i << 2)))
{
state.push_back(i);
cnt[i] = count(i);
}
for(int i = 1; i <= n + 2; i ++ )
for(auto a: state)
for(auto b: state)
for(auto c: state)
{
//c a b
if((a & b) | (b & c) | (a & c)) continue;
if(g[i - 1] & a | g[i] & b) continue;
f[i & 1][a][b] = max(f[i & 1][a][b], f[(i - 1) & 1][c][a] + cnt[b]);
}
cout << f[n + 2 & 1][0][0] << endl;
}
集合式的dp
愤怒的小鸟
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define x first
#define y second
using namespace std;
typedef pair<double, double> PDD;
const int N = 18, M = 1 << 18;
const double eps = 1e-8;
int n, m;
PDD q[N];
int path[N][N];
int f[M];
int cmp(double x, double y)//因为精度问题,比较需要用到cmp
{
if(fabs(x - y) < eps) return 0;
if(x < y) return -1;
return 1;
}
int main()
{
int T;
cin >> T;
while (T --)
{
cin >> n >> m;
for(int i = 0; i < n; i ++ ) cin >> q[i].x >> q[i].y;
memset(path, 0, sizeof path);
for(int i = 0; i < n; i ++ )
{
path[i][i] = 1 << i; //i到i有一条抛物线能够经过1000000...
for(int j = 0; j < n; j ++ )
{
double x1 = q[i].x, y1 = q[i].y;
double x2 = q[j].x, y2 = q[j].y;
if(!cmp(x1, x2)) continue;
double a = (y1 / x1 - y2 / x2) / (x1 - x2);
double b = y1 / x1 - a * x1;
if(a >= 0) continue;
//找到抛物线
int state = 0;//找到抛物线经过的点
for(int k = 0; k < n; k ++ )
{
double x = q[k].x, y = q[k].y;
if(!cmp(a * x * x + b * x, y)) state += 1 << k;
}
path[i][j] = state;//得到path
}
}
memset(f, 0x3f, sizeof f);
f[0] = 0;
for(int i = 0; i + 1 < 1 << n; i ++ )//不必更新最后的状态,到达最后状态跳出即可
{
int x = 0;
for(int j = 0; j < n; j ++ )
if(!(i >> j & 1))
{
x = j;
break;
}
for(int j = 0; j < n; j ++ )
f[i | path[x][j]] = min(f[i | path[x][j]], f[i] + 1);//从状态i跳到状态i|path
}
cout << f[(1 << n) - 1] << endl;//最终会得到经过全部点的抛物线数量
}
}