codeforces 446B DZY Loves Modification(枚举)
2015-01-09 17:13
531 查看
题目链接
DZY Loves Modification
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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
Output a single integer — the maximum possible total pleasure value DZY could get.
Sample test(s)
Input
Output
Input
Output
题意:n*m的数字矩阵,要刚好操作k次,以及系数p。每次操作可以获得一行或一列的数的和的价值,然后把该行或该列的每个数减p。求获得的价值最大为多少?
题解:先把行和列分开考虑,预处理出单独操作行(1到k次)的最大价值,预处理出单独操作列(1到k次)的最大价值。然后枚举行操作多少次,列操作多少次。若行操作x次的最大价值H[x],列操作x的最大价值为L[x]。若行操作了i次,列操作k-i次,则答案为H[i]+L[k-i]-p*i*(k-i)。
代码如下:
DZY Loves Modification
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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
Output a single integer — the maximum possible total pleasure value DZY could get.
Sample test(s)
Input
2 2 2 2 1 3 2 4
Output
11
Input
2 2 5 2 1 3 2 4
Output
11
题意:n*m的数字矩阵,要刚好操作k次,以及系数p。每次操作可以获得一行或一列的数的和的价值,然后把该行或该列的每个数减p。求获得的价值最大为多少?
题解:先把行和列分开考虑,预处理出单独操作行(1到k次)的最大价值,预处理出单独操作列(1到k次)的最大价值。然后枚举行操作多少次,列操作多少次。若行操作x次的最大价值H[x],列操作x的最大价值为L[x]。若行操作了i次,列操作k-i次,则答案为H[i]+L[k-i]-p*i*(k-i)。
代码如下:
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<string> #include<stack> #include<math.h> #include<vector> #include<set> #include<map> #define nn 1100 #define inff 0x3fffffff #define eps 1e-8 #define mod 1000000007 typedef long long LL; const LL inf64=LL(inff)*inff; using namespace std; int n,m,k; LL p; int a[nn][nn]; multiset<int>se; multiset<int>::iterator it; LL f1[nn*nn],f2[nn*nn]; int main() { int i,j; while(scanf("%d%d%d%I64d",&n,&m,&k,&p)!=EOF) { for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { scanf("%d",&a[i][j]); } } int ix; se.clear(); for(i=1;i<=n;i++) { ix=0; for(j=1;j<=m;j++) { ix+=a[i][j]; } se.insert(ix); } f1[0]=0; for(i=1;i<=k;i++) { it=se.end(); it--; f1[i]=f1[i-1]+(*it); ix=(*it); se.erase(it); se.insert(ix-(int)p*m); } se.clear(); for(j=1;j<=m;j++) { ix=0; for(i=1;i<=n;i++) { ix+=a[i][j]; } se.insert(ix); } f2[i]=0; for(i=1;i<=k;i++) { it=se.end(); it--; f2[i]=f2[i-1]+(*it); ix=*it; se.erase(it); se.insert(ix-(int)p*n); } LL ans=-inf64; for(i=0;i<=k;i++) { ans=max(ans,f1[i]+f2[k-i]-p*i*(k-i)); } printf("%I64d\n",ans); } return 0; }
相关文章推荐
- Codeforces 672C Recycling Bottles【极限思维+贪心枚举】
- codeforces 675B B. Restoring Painting(暴力枚举)
- Codeforces 892 C. Pride (枚举)
- CodeForces - 895A Pizza Separation 简单枚举 区间
- codeforces 158D Ice Sculptures (枚举)
- codeforces 460D Little Victor and Set(构造、枚举)
- Codeforces 379D. New Year Letter【动态规划、暴力枚举】
- CodeForces 253D Table with Letters - 2 减小复杂度的技巧+交替枚举
- Codeforces 593B Anton and Lines 【思维】【枚举】【排序】
- CodeForces 253D Table with Letters - 2(枚举技巧)
- CodeForces 666B World Tour【spfa最短路+枚举】
- Codeforces 846C Four Segments【思维+预处理+前缀和枚举】
- Codeforces 425A Sereja and Swaps(暴力枚举)
- Codeforces 327A-Flipping Game(暴力枚举)
- CodeForces 425B Sereja and Table(枚举)
- codeforces 599D. Spongebob and Squares【推公式+暴力枚举】
- CodeForces 666B World Tour(spfa+枚举)
- Codeforces 466B Wonder Room【思维+暴力枚举】直觉题
- CodeForces 496D Tennis Game (暴力枚举)
- !codeforces 399C Cards-数论&贪心-(暴力枚举)