Codeforces Round #FF (Div. 2)D. DZY Loves Modification
来源:互联网 发布:stackblur 算法 编辑:程序博客网 时间:2024/05/21 18:47
As we know, DZY loves playing games. One day DZY decided to play with a n × m matrix. To be more precise, he decided to modify the matrix with exactly k operations.
Each modification is one of the following:
- Pick some row of the matrix and decrease each element of the row by 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. 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 modifications? Please, help him to calculate this value.
The first line contains four space-separated integers n, m, k and p (1 ≤ n, m ≤ 103; 1 ≤ k ≤ 106; 1 ≤ p ≤ 100).
Then n lines follow. Each of them contains m integers representing aij (1 ≤ aij ≤ 103) — the elements of the current row of the matrix.
Output a single integer — the maximum possible total pleasure value DZY could get.
2 2 2 21 32 4
11
2 2 5 21 32 4
11
For the first sample test, we can modify: column 2, row 2. After that the matrix becomes:
1 10 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
题意:
给一个n*m的矩阵,有两种操作方式:统计某行或者某列的和并将每项减去p。问k个操作后,取的最大值是多少。
题解:
我刚开始想法是每次取行列中和最大的,然后减去多少个p(减多少个p根据取的是行或者列可知,取某一列之和则减去行数*p,同理)。再将其值加入优先队列,一共取k次,再看这k次取了多少行r,多少列w,最后将其和减去r*w*p即可。后来测试发现这种做法是错误的,因为有可能存在一种情况,取两个列比取1行1列的值要小。仔细想一下吧。后来我觉得还是用贪心,取前i行之和+取前k-i列之和再减去i*(k-i)*p的最大值即是答案。有个小地方需要注意,max赋值的时候最小值得很小很小,远超过Int最小值0x7fffffff。所以,嗯,就这样。帖代码。
#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <iostream>#include <cmath>#include <queue>#include <map>#include <stack>#include <list>#include <vector>#include <limits>using namespace std;#define Max(a,b) a>b?a:bstruct node{__int64 v;};__int64 r[1000010],w[1000010];int main(){__int64 n,m,k,p;priority_queue <__int64> q;scanf("%I64d%I64d%I64d%I64d",&n,&m,&k,&p);__int64 i,j,t;memset(r,0,sizeof(r));memset(w,0,sizeof(w));for (i=1;i<=n;i++) //w代表列,r代表行 {for (j=1;j<=m;j++){scanf("%I64d",&t);r[i]+=t;w[j]+=t;}}while (!q.empty()) q.pop();for (i=1;i<=n;i++){q.push(r[i]);r[i]=0;}for (i=1;i<=k;i++){__int64 h=q.top();q.pop();r[i]=r[i-1]+h;h-=p*m;q.push(h);}while (!q.empty())q.pop();for (i=1;i<=m;i++){q.push(w[i]);w[i]=0;}for (i=1;i<=k;i++){__int64 h=q.top();q.pop();w[i]=w[i-1]+h;h-=p*n;q.push(h);}__int64 ans=-1e18;for (i=0;i<=k;i++){ans=Max(ans,r[i]+w[k-i]-(__int64)(i*(k-i)*p));}printf("%I64d\n",ans);return 0;}
- Codeforces Round #FF (Div. 2)D. DZY Loves Modification
- Codeforces Round #FF (Div. 2) D. DZY Loves Modification
- Codeforces Round #FF (Div. 2) D. DZY Loves Modification 贪心+优先队列
- Codeforces Round #FF (Div. 1) B. DZY Loves Modification
- codeforces#FF(div2) D DZY Loves Modification
- Codeforces Round #FF (Div. 2) Problem A DZY Loves Hash
- Codeforces Round #FF (Div. 2) Problem B DZY Loves Strings
- Codeforces Round #FF (Div. 2):Problem A - DZY Loves Hash
- Codeforces Round #FF (Div. 2):B. DZY Loves Strings
- Codeforces Round #FF (Div. 2) B. DZY Loves Strings
- Codeforces Round #FF (Div. 2) A. DZY Loves Hash
- Codeforces Round #FF (Div. 2) A. DZY Loves Hash
- Codeforces Round #FF (Div. 2):C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) Problem C DZY Loves Sequences
- Codeforces Round #FF (Div. 2)C - DZY Loves Sequences
- Codeforces Round #FF (Div. 2)-C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) 447B DZY Loves Strings
- Codeforces Round #FF (Div. 2)C. DZY Loves Sequences
- cheap louis vuitton handbags-882603
- kali linux下的中文输入法安装及iceweasel汉化
- RCP中可以取消的进度条
- Spring AOP Sample
- 单链表的插入排序
- Codeforces Round #FF (Div. 2)D. DZY Loves Modification
- 什么是4P-4C--4R营销理论?
- java SQL语法解释器jsqlparser
- Effective C++ Item 35 考虑 virtual 函数以外的实现
- <img usemap="#Map">的用法
- 单链表的归并排序
- 2500 做一个正气的杭电人
- 2502 月之数
- 2503 a/b + c/d(a除b + c除d)