zoukankan      html  css  js  c++  java
  • CF446B DZY Loves Modification 优先队列

    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:

    1. 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.
    2. 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.

    Examples
    Input
    Copy
    2 2 2 2
    1 3
    2 4
    Output
    Copy
    11
    Input
    Copy
    2 2 5 2
    1 3
    2 4
    Output
    Copy
    11
    Note

    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
    貌似行和列不太好处理?
    假设先对行进行处理了 i 次,那么列自然就是 k-i 次处理;
    对行操作结束后:
    sum-=m*p*i;
    此时对列的就是 -= (k-i)*p*i;
    那么我们枚举行和列的操作次数即可;
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    #include<bitset>
    #include<ctime>
    #include<deque>
    #include<stack>
    #include<functional>
    #include<sstream>
    //#include<cctype>
    //#pragma GCC optimize(2)
    using namespace std;
    #define maxn 2000005
    #define inf 0x7fffffff
    //#define INF 1e18
    #define rdint(x) scanf("%d",&x)
    #define rdllt(x) scanf("%lld",&x)
    #define rdult(x) scanf("%lu",&x)
    #define rdlf(x) scanf("%lf",&x)
    #define rdstr(x) scanf("%s",x)
    typedef long long  ll;
    typedef unsigned long long ull;
    typedef unsigned int U;
    #define ms(x) memset((x),0,sizeof(x))
    const long long int mod = 1e9 + 7;
    #define Mod 1000000000
    #define sq(x) (x)*(x)
    #define eps 1e-3
    typedef pair<int, int> pii;
    #define pi acos(-1.0)
    //const int N = 1005;
    #define REP(i,n) for(int i=0;i<(n);i++)
    typedef pair<int, int> pii;
    inline ll rd() {
    	ll x = 0;
    	char c = getchar();
    	bool f = false;
    	while (!isdigit(c)) {
    		if (c == '-') f = true;
    		c = getchar();
    	}
    	while (isdigit(c)) {
    		x = (x << 1) + (x << 3) + (c ^ 48);
    		c = getchar();
    	}
    	return f ? -x : x;
    }
    
    ll gcd(ll a, ll b) {
    	return b == 0 ? a : gcd(b, a%b);
    }
    ll sqr(ll x) { return x * x; }
    
    /*ll ans;
    ll exgcd(ll a, ll b, ll &x, ll &y) {
    	if (!b) {
    		x = 1; y = 0; return a;
    	}
    	ans = exgcd(b, a%b, x, y);
    	ll t = x; x = y; y = t - a / b * y;
    	return ans;
    }
    */
    
    
    priority_queue<ll>r, c;
    ll n, m;
    ll maxc[maxn], maxr[maxn];
    ll a[2000][2000];
    
    int main() {
    	//ios::sync_with_stdio(0);
    	ll k, p;
    	cin >> n >> m >> k >> p;
    	ll sumr = 0, sumc = 0;
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < m; j++) {
    			rdllt(a[i][j]);
    			sumr += a[i][j];
    		}
    		r.push(sumr); sumr = 0;
    	}
    	for (int i = 0; i < m; i++) {
    		for (int j = 0; j < n; j++) {
    			sumc += a[j][i];
    		}
    		c.push(sumc); sumc = 0;
    	}
    	ll maxx = -1e17 - 4;
    	for (int i = 1; i <= k; i++) {
    		ll tmp = r.top(); r.pop();
    		maxr[i] = maxr[i - 1] + tmp;
    		r.push(tmp - m * p);
    	}
    	for (int i = 1; i <= k; i++) {
    		ll tmp = c.top(); c.pop();
    		maxc[i] = maxc[i - 1] + tmp;
    		c.push(tmp - n * p);
    	}
    	for (int i = 0; i <= k; i++) {
    		ll ans = maxc[i] + maxr[k - i] - (ll)i*(k - i)*p;
    		maxx = max(maxx, ans);
    	}
    	cout << maxx << endl;
    	return 0;
    }
    


    EPFL - Fighting
  • 相关阅读:
    POJ-2253 Frogger---最短路变形&&最大边的最小值
    POJ-2263 Heavy Cargo---最短路变形&&最小边的最大值
    POJ-2570 Fiber Network---Floyd+二进制表示集合
    POJ-3259 Wormholes---SPFA判断有无负环
    POJ-3268 Silver Cow Party---正向+反向Dijkstra
    Floyd算法
    View相关面试问题-ListView缓存面试问题讲解
    View相关面试问题-事件分发面试问题讲解
    Android基础相关面试问题-binder面试问题详解
    Android异常与性能优化相关面试问题-其他优化面试问题详解
  • 原文地址:https://www.cnblogs.com/zxyqzy/p/10215498.html
Copyright © 2011-2022 走看看