题目

题目描述
As we know, DZY loves playing games. One day DZY decided to play with a n×m n×m matrix. To be more precise, he decided to modify the matrix with exactly k k operations.

Each modification is one of the following:

Pick some row of the matrix and decrease each element of the row by p p . This operation brings to DZY the value of pleasure equal to the sum of elements of the row before the decreasing.
Pick some column of the matrix and decrease each element of the column by p p . This operation brings to DZY the value of pleasure equal to the sum of elements of the column before the decreasing.
DZY wants to know: what is the largest total value of pleasure he could get after performing exactly k k modifications? Please, help him to calculate this value.

输入输出格式
输入格式:
The first line contains four space-separated integers n,m,k n,m,k and p p $ (1<=n,m<=10^{3}; 1<=k<=10^{6}; 1<=p<=100) $ .

Then n n lines follow. Each of them contains m m integers representing $ a_{ij} (1<=a_{ij}<=10^{3}) $ — the elements of the current row of the matrix.

输出格式:
Output a single integer — the maximum possible total pleasure value DZY could get.

输入输出样例
输入样例#1: 复制
2 2 2 2
1 3
2 4
输出样例#1: 复制
11
输入样例#2: 复制
2 2 5 2
1 3
2 4
输出样例#2: 复制
11
说明
For the first sample test, we can modify: column 2, row 2. After that the matrix becomes:

1 1
0 0

For the second sample test, we can modify: column 2, row 2, row 1, column 1, column 2. After that the matrix becomes:

-3 -3
-2 -2

思路

这道题的主要困难在于,行和列之间是相互影响的

但一旦我们知道行操作的次数,列操作的次数也就知道了, 它们相互的影响也可以在常数时间内求得。

我们怎么知道呢?我们不知道,可以枚举

但直接枚举显然是不行的

因此,我们可以做个预处理,求出两个数组,再扫一遍,取个MAX

但这次一定要注意,long long! long long!

代码

#include <bits/stdc++.h>
#define N 1005
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
ll n,m,k,p,a[N][N],res=-INF,sum_r[N],sum_c[N],r[N*N],c[N*N];
priority_queue <ll> rq,cq;
void Init()
{scanf("%I64d%I64d%I64d%I64d",&n,&m,&k,&p);for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){scanf("%I64d",&a[i][j]);}}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){sum_r[i]+=a[i][j];}rq.push(sum_r[i]);}for(int j=1;j<=m;j++){for(int i=1;i<=n;i++){sum_c[j]+=a[i][j];}cq.push(sum_c[j]);}
}
void Solve()
{for(int i=1;i<=k;i++){ll cc=cq.top();cq.pop();c[i]=c[i-1]+cc;cc-=n*p;cq.push(cc);}for(int i=1;i<=k;i++){ll rr=rq.top();rq.pop();r[i]=r[i-1]+rr;rr-=m*p;rq.push(rr);}for(int i=0;i<=k;i++){res=max(res,r[i]+c[k-i]-p*i*(k-i));}
}
void Print()
{printf("%I64d\n",res);
}
int main()
{Init();Solve();Print();return 0;
}

【CF446B】 DZY Loves Modification相关推荐

  1. 【BZOJ3512】DZY Loves Math IV(杜教筛)

    [BZOJ3512]DZY Loves Math IV(杜教筛) https://www.cnblogs.com/cjyyb/p/10165338.html

  2. 【BZOJ3309】DZY Loves Math 解题报告

    [BZOJ3309]DZY Loves Math Description 对于正整数\(n\),定义\(f(n)\)为\(n\)所含质因子的最大幂指数.例如\(f(1960)=f(2^3×5^1×7^ ...

  3. 【BZOJ3309】DZY Loves Math

    3309: DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MB Submit: 411 Solved: 161 [Submit][Status ...

  4. 【BZOJ3569】DZY Loves Chinese II(线性基,图的连通性)

    Description 神校XJ之学霸兮,Dzy皇考曰JC. 摄提贞于孟陬兮,惟庚寅Dzy以降. 纷Dzy既有此内美兮,又重之以修能. 遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图,其上 ...

  5. 【bzoj3309】DZY Loves Math 莫比乌斯反演+线性筛

    Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, f(1)=0. 给定正整数a,b, ...

  6. 【题解】DZY Loves Math

    sol: 卷积函数求通项 + 莫比乌斯函数 . 我们只需要求到 g(n)=∑d∣nf(d)μ(nd)g(n)=\sum_{d|n}f(d)\mu(\frac{n}{d})g(n)=∑d∣n​f(d)μ ...

  7. 【BZOJ3512】DZY Loves Math IV

    题目大意 给出 n , m n,m n,m,求 ∑ i = 1 n ∑ j = 1 m ϕ ( i j ) \sum\limits_{i=1}^n\sum\limits_{j=1}^m\phi(ij) ...

  8. 【BZOJ3512】 DZY Loves Math IV

    题意:(第n+1次)略 首先转换:Ans(n,m)=∑i=1n∑j=1mφ(i⋅j)设s(n,m)=∑i=1mφ(n⋅i)那么Ans(n,m)=∑i=1ns(i,m)每次从1到n枚举i,然后考虑:s( ...

  9. 【BZOJ3309】DZY Loves Math(线性筛)

    题目: BZOJ 3309 分析: 首先,经过一番非常套路的莫比乌斯反演(实在懒得写了),我们得到: \[\sum_{T=1}^n \sum_{d|T}f(d)\mu(\frac{T}{d})\lfl ...

最新文章

  1. 算法----左叶子之和
  2. 最新网众 2490(XP) 客户端 去百度 去劫持 去快车
  3. 打印机打印网页不清晰_打印机墨水:你不知道的秘密
  4. css中小知识点总结
  5. 146. LRU 缓存机制
  6. tcpsyn发生在哪层_必看面试题之计算机网络:来自一位拿到了腾讯和字节双offer的大佬...
  7. 1.4.4 Mother's Mil 母亲的牛奶(DFS)
  8. axure手机页面设计说明_产品经理原型演示——Axure制作手机登陆界面
  9. jquery第三章练习三(制作京东常见问题分类页面)
  10. 单片机课设-中断程序(仿真图,代码全)
  11. 安卓Apk下载以及静默安装
  12. mac 国家税务总局发票不显示验证码
  13. Minecraft多人联机服务器配置
  14. 修改MySQL密码策略
  15. 玩转git之webhook应用初探
  16. 《刨根问底系列》:序言
  17. 字节跳动,跳到哪了?
  18. 声纹验证和声纹识别中的AS-norm、Z-norm、T-norm、ZT-norm、 S-norm操作
  19. gdb工具pwndbg与peda与gef
  20. Centos7下Fluka的安装教程(保姆级)

热门文章

  1. Spark创建hive表报错 ROW FORMAT DELIMITED is only compatible with ‘textfile‘, not ‘orc‘
  2. python最大公约数计算的程序代码_Python实现的求解最大公约数算法示例
  3. C++ Error C2280 尝试引用已删除的函数
  4. 微软编程规范(文档)
  5. Java 线程池及参数动态调节详解
  6. 配置H3C设备无线AP多vlan步骤
  7. iOS如何完成蓝牙打印机功能
  8. 在Composure去除掉对体积云和雾的捕获
  9. 英特尔芯片漏洞比想象中更严重:控制计算机无需密码
  10. 四中矩阵相乘方法对比