P7231 [COCI 2015/2016 #3] DOMINO 详解
思路
看到题立刻想到弄出所有合法的骨牌放置的位置然后排序贪心,想到贪心立刻发现不对,因为第一大可能会阻挡其他位置的选取。
没有思路直接看数据范围想部分分。
这一看就看出名堂了,
肯定不能直接搜索每一个骨牌的位置和方向,时间无法接受。我们再仔细想想贪心不对的地方,发现其实贪心选取一个骨牌只会最多导致
具体的,我们直接搜
Code
//# pragma GCC optimize("Ofast")
# include <bits/stdc++.h>
# define fr front
# define il inline
# define fir first
# define sec second
# define vec vector
# define it iterator
# define pb push_back
# define lb lower_bound
# define ub upper_bound
# define all(x) x.begin(), x.end()
# define mem(a, b) memset(a, b, sizeof(a))
# define lc (t[p].l)
# define rc (t[p].r)
# define ls(x) (x << 1)
# define rs(x) (x << 1 | 1)
# define lson ls(p), l, mid
# define rson rs(p), mid + 1, r
# define sqr(x) ((x) * (x))
# define bpc __builtin_popcount
# define lowbit(x) ((x) & (-(x)))
# define geti(x, i) (((x) >> (i)) & 1)
# define set1(x, i) ((x) | (1 << (i)))
# define set0(x, i) ((x) & (~(1 << (i))))
# define debug1(x) cerr << #x << " = " << x << " "
# define debug2(x) cerr << #x << " = " << x << "\n"
# define bug cerr << "--------------------------\n"
# define each1(i, x) for(auto (i) : (x))
# define each2(i, x) for(auto (&i) : (x))
# define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
# define pre(i, a, b) for(int i = (a); i >= (b); -- i)
# define G(i, h, u, ne) for(int i = (h[(u)]); i; i = (ne[(i)]))
# define reps(i, a, b, c) for(int i = (a); i <= (b); i += (c))
# define pres(i, a, b, c) for(int i = (a); i >= (b); i -= (c))
using namespace std;
using DB = double;
using LL = long long;
using LDB = long double;
using PII = pair<int, int>;
using ULL = unsigned long long;
const int N = 2e3 + 10;
const int INF1 = 0x3f3f3f3f, INF2 = INT_MAX;
const LL INF3 = (LL)1e18, INF4 = 0x3f3f3f3f3f3f3f3f, INF5 = LLONG_MAX;
int n, m, sz, ans;
int a[N][N], s[60];
LL sum;
bitset<N> mp[N];
struct node{
int val, x1, y1, x2, y2;
};
bool cmp(node a, node b){
return a.val > b.val;
}
vec<node> q;
void dfs(int k, int lst, int num){
ans = max(ans, num);
if(k == m + 1 || num + s[lst + m - k] - s[lst - 1] <= ans) return;//如果加上后面的最大都不行
rep(i, lst, sz){
if(mp[q[i].x1][q[i].y1] || mp[q[i].x2][q[i].y2]) continue;
mp[q[i].x1][q[i].y1] = mp[q[i].x2][q[i].y2] = true;
dfs(k + 1, i + 1, num + q[i].val);
mp[q[i].x1][q[i].y1] = mp[q[i].x2][q[i].y2] = false;
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
q.pb({114514, 0, 0, 0, 0});
cin >> n >> m;
rep(i, 1, n){
rep(j, 1, n){
cin >> a[i][j];
sum += a[i][j];
}
}
rep(i, 1, n - 1)
rep(j, 1, n - 1)
q.pb({a[i][j] + a[i + 1][j], i, j, i + 1, j}), q.pb({a[i][j] + a[i][j + 1], i, j, i, j + 1});
rep(i, 1, n - 1) q.pb({a[n][i] + a[n][i + 1], n, i, n, i + 1});
rep(i, 1, n - 1) q.pb({a[i][n] + a[i + 1][n], i, n, i + 1, n});
sort(all(q), cmp);
sz = min((int)q.size() - 1, 56);
rep(i, 1, sz) s[i] = s[i - 1] + q[i].val;
dfs(1, 1, 0);
cout << sum - ans;
return 0;
}
活像個孤獨患者,自我拉扯,外向的孤獨患者,有何不可?