• bzoj4165 矩阵 堆维护多路归并


    题目传送门

    https://lydsy.com/JudgeOnline/problem.php?id=4165

    题解

    大概多路归并是最很重要的知识点了吧,近几年考察也挺多的(虽然都是作为签到题的)

    看到题目要求第 (K) 小矩阵,基本上可以想到用堆维护的 (K) 路归并。

    然后我们考虑每一路是以 ((x_2, y_2)) 为右下角的矩形的权值。那么初始的矩形的左上角应该是 ((x_2 - Mina, y_2 - Minb))

    于是我们用一个堆来维护这样的每一路。扩展每一路的话,因为把矩形的左边界或者上边界扩展一个单位以后权值肯定单调升,所以可以直接扩展一下左边界和上边界这两个矩形。但是这样可能会有重复的,所以拿一个 map 来判重一下。


    这样的时间复杂度是 (O(klog nm))

    #include<bits/stdc++.h>
    #include<tr1/unordered_set>
    
    #define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
    #define dbg(...) fprintf(stderr, __VA_ARGS__)
    #define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
    #define fi first
    #define se second
    #define pb push_back
    
    template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
    template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
    
    typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
    
    template<typename I> inline void read(I &x) {
    	int f = 0, c;
    	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    	x = c & 15;
    	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    	f ? x = -x : 0;
    }
    
    const int N = 1000 + 7;
    
    int n, m, mina, minb, k;
    int a[N][N];
    ll s[N][N];
    
    inline ll gsum(int x1, int y1, int x2, int y2) { return s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]; }
    
    struct Matrix {
    	int x1, y1, x2, y2;
    	inline Matrix() {}
    	inline Matrix(const int &x1, const int &y1, const int &x2, const int &y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
    	inline bool operator < (const Matrix &b) const { return gsum(x1, y1, x2, y2) > gsum(b.x1, b.y1, b.x2, b.y2); }
    };
    std::priority_queue<Matrix> q;
    
    std::tr1::unordered_set<ll> mp;
    inline void set_mp(int x1, int y1, int x2, int y2) {
    	ll v = (((((ll)x1 * m) + y1) * n + x2) * m + y2);
    	mp.insert(v);
    }
    inline bool get_mp(int x1, int y1, int x2, int y2) {
    	ll v = (((((ll)x1 * m) + y1) * n + x2) * m + y2);
    	return mp.count(v);
    }
    
    inline void work() {
    	for (int i = mina; i <= n; ++i)
    		for (int j = minb; j <= m; ++j) q.push(Matrix(i - mina + 1, j - minb + 1, i, j)), set_mp(i - mina + 1, j - minb + 1, i, j);
    	while (k--) {
    		Matrix t = q.top();
    		q.pop();
    		if (!k) return (void)printf("%lld
    ", gsum(t.x1, t.y1, t.x2, t.y2));
    		if(t.x1 > 1 && !get_mp(t.x1 - 1, t.y1, t.x2, t.y2)) set_mp(t.x1 - 1, t.y1, t.x2, t.y2), q.push(Matrix(t.x1 - 1, t.y1, t.x2, t.y2));
    		if(t.y1 > 1 && !get_mp(t.x1, t.y1 - 1, t.x2, t.y2)) set_mp(t.x1, t.y1 - 1, t.x2, t.y2), q.push(Matrix(t.x1, t.y1 - 1, t.x2, t.y2));
    	}
    }
    
    inline void init() {
    	read(n), read(m), read(mina), read(minb), read(k);
    	for (int i = 1; i <= n; ++i)
    		for (int j = 1; j <= m; ++j) read(a[i][j]), s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
    }
    
    int main() {
    #ifdef hzhkk
    	freopen("hkk.in", "r", stdin);
    #endif
    	init();
    	work();
    	fclose(stdin), fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    #256 (Div. 2)A. Rewards
    1113 矩阵快速幂
    1108 距离之和最小V2
    1287 加农炮
    1191 消灭兔子
    1051 最大子矩阵
    1086 背包
    1105 第K大的数
    2016 CCPC 网络赛 B 高斯消元 C 树形dp(待补) G 状压dp+容斥(待补) H 计算几何
    Educational Codeforces Round 18 C dp,思维 D lowbit,思维
  • 原文地址:https://www.cnblogs.com/hankeke/p/bzoj4165.html
Copyright © 2020-2023  润新知