Codeforces Round #FF (Div.2 )(C,D)

C. DZY Loves Sequences

DZY has a sequence a, consisting of n integers.

We'll call a sequence ai, ai + 1, ..., aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.

Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.

You only need to output the length of the subsegment you find.

Input

The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

In a single line print the answer to the problem — the maximum length of the required subsegment.

Sample test(s)
Input
6
7 2 3 1 5 6
Output
5
Note

You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.


思路:扫一遍记录下以该节点为终点和起点的序列的长度,然后处理一遍,处理的时候一定要考虑全面

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
int a[maxn],n;
int numi[maxn],numd[maxn];
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    int cnt=1,ans=0;
    memset(numi,0,sizeof(numi));
    memset(numd,0,sizeof(numd));
    numi[n]=1;
    for(int i=n-1;i>0;i--)
    {
        if(a[i]<a[i+1]){cnt++;numi[i]=cnt;}
        else {cnt=1;numi[i]=1;}
    }
    numd[1]=1;
    cnt=1;
    for(int i=2;i<=n;i++)
    {
        if(a[i]>a[i-1]){cnt++;numd[i]=cnt;}
        else{cnt=1;numd[i]=cnt;}
    }
    for(int i=1;i<=n;i++)
    {
        ans=max(ans,max(numi[i],numd[i]));
        if(i>1&&i<n&&a[i-1]<a[i+1]-1)
        {
            ans=max(ans,numi[i+1]+numd[i-1]+1);
        }
        if(i+2<=n&&a[i]<a[i+2]-1)ans=max(ans,numi[i+2]+numd[i]+1);
        if(i<n&&a[i]==a[i+1])ans=max(ans,max(numi[i+1]+1,numd[i]+1));
        if(i>1&&a[i]<a[i-1])ans=max(ans,max(numd[i-1]+1,numi[i]+1));
    }
    cout<<ans<<endl;
    return 0;
}

D. 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.

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
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次,取最大


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN=1004;
const int MAXK=1000005;
typedef long long LL;

int n,m,k,p,a[MAXN][MAXN];
LL row[MAXK], col[MAXK];
priority_queue <LL> q;

int main()
{
    scanf("%d%d%d%d",&n,&m,&k,&p);
    for (int i = 1;i <= n; i ++)
    for (int j = 1;j <= m; j ++)
            scanf("%d",&a[i][j]);

    while (!q.empty()) q.pop();
    for (int i = 1; i <= n; i ++)
    {
        LL sum = 0;
        for (int j = 1; j <= m; j ++)
            sum += a[i][j];
        q.push(sum);
    }
    row[0] = 0;
    for (int i = 1; i <= k; i ++)
    {
        LL now = q.top(); q.pop();

        row[i] = row[i-1] + now;
        q.push(now - 1LL * m * p);
    }
    while (!q.empty()) q.pop();
    for (int i = 1; i <= m; i ++)
    {
        LL sum = 0;
        for (int j = 1; j <= n; j ++)
            sum += a[j][i];
        q.push(sum);
    }
    col[0] = 0;
    for (int i = 1; i <= k; i ++)
    {
        LL now = q.top(); q.pop();
        col[i] = col[i-1] + now;
        q.push(now - 1LL * n * p);
    }

    LL ans = row[0] + col[k];
    for (int i = 1; i <= k;  i ++)
        ans = max(ans, row[i] + col[k - i] - 1LL * i * (k - i) * p);
    cout<<ans<<endl;

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值