CodeForces 446B DZY Loves Modification 经典+贪心+更换遍历对象
来源:互联网 发布:微信领取淘宝优惠券 编辑:程序博客网 时间:2024/06/07 04:36
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
遍历对象更换为k,在k确定的情况下求解
#include <iostream> #include <cstdio>#include <cstdlib>#include <cmath>#include <fstream>#include <algorithm>#include <queue>#include <climits>#include <cstring>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <list>const int maxn=5e2+10;using namespace std;typedef long long ll;typedef unsigned long long ull; const int N = 1e6+5;int a[1010][1010];ll n,m,k,p;ll sumx[1010];ll sumy[1010];ll dpx[1000010];ll dpy[1000010];priority_queue <ll> sx,sy;//有序数组 int main(){ios::sync_with_stdio(false); cin>>n>>m>>k>>p;for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin >> a[i][j];sumy[j] += a[i][j];sumx[i] += a[i][j];}}//行与列分开求 for(int i=0;i<n;i++)sx.push(sumx[i]);for(int j=0;j<m;j++)sy.push(sumy[j]);dpx[0] =dpy[0] = 0; for(int i=1;i<=k;i++){ll t1 = sx.top();sx.pop();ll t2 = sy.top();sy.pop();dpx[i] = dpx[i-1] + t1;dpy[i] = dpy[i-1] + t2;sx.push(t1-m*p);sy.push(t2-n*p);}//最后利用k遍历 ll re = dpx[0]+dpy[k];for(int i=1;i<=k;i++){re = max(re,(dpx[i]+dpy[k-i]-(ll)(i*(k-i)*p)));}cout <<re<<endl;return 0;}
- CodeForces 446B DZY Loves Modification 经典+贪心+更换遍历对象
- 【杂题】 codeforces 446B - DZY Loves Modification
- CodeForces 446B DZY Loves Modification
- CodeForces 446-B. DZY Loves Modification
- Codeforces 446B DZY Loves Modification
- DZY Loves Modification CodeForces 447D 贪心
- codeforces 446B DZY Loves Modification(枚举)
- DZY Loves Modification CodeForces
- DZY Loves Modification CodeForces
- Codeforces Round #FF (Div. 1) B. DZY Loves Modification
- Codeforces 446B DZY Loves Modification 矩阵行列分开考虑 优先队列+构造
- ACM篇:CF 446B -- DZY Loves Modification
- CF 446B DZY Loves Modification 优先队列
- codeforces#FF(div2) D DZY Loves Modification
- codeforces 255#D. DZY Loves Modification
- Codeforces Round #FF (Div. 2) D. DZY Loves Modification 贪心+优先队列
- D. DZY Loves Modification
- DZY Loves Modification
- Mybatis加载属性的优先级
- 自己想转型其他技术方向,搜集到课程学习资料.
- Tomcat容器等级
- Vim插件管理利器——Vundle
- 自定义View高级知识点(一)
- CodeForces 446B DZY Loves Modification 经典+贪心+更换遍历对象
- 祝大家2017鸡年愉快幸福
- 1280. Permutation
- 腐都两年小记
- Java反射实例
- 类型和运算
- android动画_属性动画
- 安卓微信自动抢红包插件优化和实现
- Android 出现错误: Caused by: java.lang.NumberFormatException: Expected an int but was 0.02 at line 1 c