首先要知道选择行列操作时顺序是无关的
用两个数组row[i],col[j]分别表示仅选择i行能得到的最大值和仅选择j列能得到的最大值
这个用优先队列维护,没选择一行(列)后将这行(列)的和减去相应的np (mp)重新加入队列
枚举选择行的次数为i,那么选择列的次数为k - i次,ans = row[i] + col[k - i] - (k - i) * i * p;
既然顺序无关,可以看做先选择完i次行,那么每次选择一列时都要减去i * p,选择k - i次列,即减去(k - i) * i * p
//#pragma comment(linker, "/STACK:102400000,102400000")//HEAD#include <cstdio>#include <cstring>#include <vector>#include <iostream>#include <algorithm>#include <queue>#include <string>#include <set>#include <stack>#include <map>#include <cmath>#include <cstdlib>using namespace std;//LOOP#define FE(i, a, b) for(int i = (a); i = (a); --i)#define REP(i, N) for(int i = 0; i = (a); --i)#define CPY(a, b) memcpy(a, b, sizeof(a))#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)#define EQ(a, b) (fabs((a) - (b)) VI;typedef unsigned long long ULL;typedef long long LL;const int INF = 0x3f3f3f3f;const int maxn = 1010;const double eps = 1e-10;const LL MOD = 1e9 + 7;int ipt[maxn][maxn];LL row[maxn * maxn], col[maxn * maxn];LL rtol[maxn], ctol[maxn];int main(){ int n, m, k, p; while (~RIV(n, m, k, p)) { priority_queue<ll> r, c; int radd = 0, cadd = 0; CLR(rtol, 0), CLR(ctol, 0); FE(i, 1, n) FE(j, 1, m) { RI(ipt[i][j]); rtol[i] += ipt[i][j]; ctol[j] += ipt[i][j]; } FE(i, 1, n) r.push(rtol[i]); FE(j, 1, m) c.push(ctol[j]); row[0] = 0, col[0] = 0; FE(i, 1, k) { LL x = r.top(), y = c.top(); r.pop(), c.pop(); r.push(x - m * p); c.push(y - n * p); row[i] = row[i - 1] + x; col[i] = col[i - 1] + y; }// FE(i, 0, k)// cout <br> <br> <p></p> </ll></cstdlib></cmath></map></stack></set></string></queue></algorithm></iostream></vector></cstring></cstdio>