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取最大值。。。。
2 seconds
256 megabytes
standard input
standard output
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 2
1 3
2 4
11
2 2 5 2
1 3
2 4
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; typedef long long int LL; const int maxn=1100; LL dpr[maxn*maxn],dpc[maxn*maxn];
LL col[maxn],row[maxn];
LL a[maxn][maxn];
priority_queue<LL> qc,qr;
int n,m,k; LL p; int main()
{
scanf("%d%d%d%I64d",&n,&m,&k,&p);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%I64d",&a[i][j]);
col[j]+=a[i][j];
row[i]+=a[i][j];
}
}
for(int i=0;i<n;i++)
qr.push(row[i]);
for(int i=0;i<m;i++)
qc.push(col[i]);
dpr[0]=dpc[0]=0;
for(int i=1;i<=k;i++)
{
LL cc=qc.top(); qc.pop();
LL rr=qr.top(); qr.pop();
dpr[i]=dpr[i-1]+rr;
dpc[i]=dpc[i-1]+cc;
qc.push(cc-p*n);
qr.push(rr-p*m);
}
LL ans=dpr[0]+dpc[k];
for(int i=1;i<=k;i++)
ans=max(ans,dpr[i]+dpc[k-i]-1LL*i*(k-i)*p);
printf("%I64d",ans);
return 0;
}
Codeforces Round #FF (Div. 1) B. DZY Loves Modification的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #FF (Div. 2) D. DZY Loves Modification 贪心+优先队列
链接:http://codeforces.com/problemset/problem/447/D 题意:一个n*m的矩阵.能够进行k次操作,每次操作室对某一行或某一列的的数都减p,获得的得分是这一行 ...
- DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + ...
- Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划
A. DZY Loves Sequences 题目连接: http://www.codeforces.com/contest/446/problem/A Description DZY has a s ...
- Codeforces Round #FF (Div. 2):B. DZY Loves Strings
B. DZY Loves Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
题目链接: http://www.codeforces.com/contest/446/problem/A 题解: dp1[x]表示以x结尾的最大严格升序连续串,dp2[x]表示以x开头的最大严格升序 ...
- Codeforces Round #FF (Div. 2)__E. DZY Loves Fibonacci Numbers (CF447) 线段树
http://codeforces.com/contest/447/problem/E 题意: 给定一个数组, m次操作, 1 l r 表示区间修改, 每次 a[i] + Fibonacci[i-l ...
- Codeforces Round #FF (Div. 2) A. DZY Loves Hash
DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...
随机推荐
- MySQL之查漏补缺
1.TRUNCATE语句和DELETE语句的区别 1.delete语句,是DML语句,truncate语句通常被认为是DDL语句. 2.delete语句,后面可以跟where子句,通常指定where子 ...
- Python 3.6安装教程
0x01 安装Python 1.1 说明 目前,Python有两个版本,一个是2.x版,一个是3.x版,这两个版本是不兼容的. 本教程安装的是python-3.6.1-amd64版本. Python官 ...
- 为nginx配置https并自签名证书
一.把证书准备好. 步骤与使用OpenSSL自签发服务器https证书所述大同小异.在这里再重复一次. 1.制作CA证书: ca.key CA私钥: openssl genrsa -des3 -out ...
- go接口及嵌入类型例子
书上看的.慢慢领会.. package main import ( "fmt" ) type notifier interface { notify() } type user s ...
- HTK训练错误消息意义
在HTK训练线上数据的时候,遇到了ERROR [+6550] LoadHTKLabels: Junk at end of HTK transcription,这个问题,网上查阅是说有空行,结果根本没有 ...
- c++ primer 9 顺序容器
定义: #include <vector> #include <list> #include <deque> vector<int> svec; lis ...
- linux 把ls -R格式化成树状结构
谁能写脚本把linux中的ls -R命令的结果格式化成树状结构? 最好是shell脚本!欢迎讨论! 参与讨论有可能意外获取iPhone6哦~~
- 【转载】Xutils3-Dbutils
Github源码地址:https://github.com/wyouflf/xUtils3 下面是源码中sample关于Dbutils的使用代码: import android.view.View; ...
- bean的singleton(没有看到生命周期范围??)
4.5.1 The singleton scope Only one shared instance of a singleton bean is managed, and all requests ...
- learn_requests
# -*- coding: utf-8 -*- import requests URL_IP = 'http://localhost:8000/ip' URL_GET = 'http://localh ...