首先要知道选择行列操作时顺序是无关的

用两个数组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 <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
//STL
#define PB push_back
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RS(s) scanf("%s", s) #define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FD(i, b, a) for(int i = (b) - 1; 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)) <= 1e-10)
#define ALL(c) (c).begin(), (c).end()
#define SZ(V) (int)V.size()
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define WI(n) printf("%d\n", n)
#define WS(s) printf("%s\n", s)
#define sqr(x) x * x typedef vector <int> 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 << row[i] + col[k - i] <<endl;
LL ans = -1e18;
FE(i, 0, k)
ans = max(ans, row[i] + col[k - i] - (LL)i * (k - i)* p);
cout << ans << endl;
}
return 0;
}
/*
2 2 4 2
1 2 3 10 2 3 5 2
2 2 2
2 2 2
*/

codeforces#FF(div2) D DZY Loves Modification的更多相关文章

  1. codeforces#FF DIV2C题DZY Loves Sequences(DP)

    题目地址:http://codeforces.com/contest/447/problem/C C. DZY Loves Sequences time limit per test 1 second ...

  2. Codeforces Round #FF/#255 D DZY Loves Modification --贪心+优先队列

    题意:给你一个矩阵,每次选某一行或者某一列,得到的价值为那一行或列的和,然后该行每个元素减去p.问连续取k次能得到的最大总价值为多少. 解法: 如果p=0,即永远不减数,那么最优肯定是取每行或每列那个 ...

  3. Codeforces Round #FF (Div. 1) B. DZY Loves Modification 优先队列

    B. DZY Loves Modification 题目连接: http://www.codeforces.com/contest/446/problem/B Description As we kn ...

  4. Codeforces Round #FF (Div. 2) D. DZY Loves Modification 优先队列

    D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  5. Codeforces Round #FF (Div. 1) B. DZY Loves Modification

    枚举行取了多少次,如行取了i次,列就取了k-i次,假设行列单独贪心考虑然后相加,那么有i*(k-i)个交点是多出来的:dpr[i]+dpc[k-i]-i*(k-i)*p 枚举i取最大值.... B. ...

  6. [CodeForces - 447D] D - DZY Loves Modification

    D - DZY Loves Modification As we know, DZY loves playing games. One day DZY decided to play with a n ...

  7. Codeforces 447D - DZY Loves Modification

    447D - DZY Loves Modification 思路:将行和列分开考虑.用优先队列,计算出行操作i次的幸福值r[i],再计算出列操作i次的幸福值c[i].然后将行取i次操作和列取k-i次操 ...

  8. D. DZY Loves Modification

    D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  9. B. DZY Loves Modification

    B. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...

随机推荐

  1. python基础——13(系统、时间、序列化模块)

    一.时间模块 1.标准库time %y 两位数的年份表示(00-99) %Y 四位数的年份表示(0000-9999) %m 月份(01-12) %d 月中的一天(0-31) %H 24小时制小时数(0 ...

  2. 【LeetCode】ZigZag Conversion(Z 字形变换)

    这道题是LeetCode里的第6道题. 题目要求: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...

  3. E. A Magic Lamp

    E. A Magic Lamp Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 32768KB   64-bit integer IO ...

  4. MySQL5.7多源复制实践

    MySQL5.7开始新增多源复制功能,即允许一个salve同时复制多个主库的事务,slave会创建通往每个主库的管道.多源复制在应用来自多个源的事务的时候不会对有冲突的事务进行检测. 配置实现 主库支 ...

  5. ansible部署

    ansible的特性:基于Python语言实现,由paramiko,PyYAML和jinjia2三个关键模块 部署简单,agentless 默认使用ssh协议        (1) 基于秘钥认证方式  ...

  6. Mysql 主外键与索引之间的区别和联系

    系数据库依赖于主键,它是数据库物理模式的基石.主键在物理层面上只有两个用途: 惟一地标识一行. 作为一个可以被外键有效引用的对象. 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部 ...

  7. CocoaAsyncSocket一个第三方Socket库

    github地址:https://github.com/robbiehanson/CocoaAsyncSocket github上的不完整,cocochina也有demohttp://code4app ...

  8. 【bzoj4605】崂山白花蛇草水 权值线段树套KD-tree

    题目描述 神犇Aleph在SDOI Round2前立了一个flag:如果进了省队,就现场直播喝崂山白花蛇草水.凭借着神犇Aleph的实力,他轻松地进了山东省省队,现在便是他履行诺言的时候了.蒟蒻Bob ...

  9. USACO Longest Prefix

    题目大意:给出一个长字符串,问最长的前缀,使得这个前缀能用给出的一些元素组合而成 思路:暴力dp,dp[i]表示长度为i的前缀能否被表示 /*{ ID:a4298442 PROB:prefix LAN ...

  10. 洛谷P4094 - [TJOI2016]字符串

    Portal Description 给出一个字符串\(s(|s|\leq10^5)\)和\(m\)次询问,每次询问子串\(s[x_1..x_2]\)的所有子串和\(s[y_1..y_2]\)的最长公 ...