Codeforces Round #FF

  A.DZY Loves Hash

  hash函数 h(x) = x % p  输出第一次冲突的位置

#include<iostream>
#include<cstdio>
#include<cstdlib>

using namespace std;

const int maxn = 4000;

int p, n;
bool inhash[maxn];

int main()
{
    freopen("447A.in", "r", stdin);
    cin >> p >> n;
    for(int i = 0; i < n; i++) {
        long long t;
        cin >> t;
        if(!inhash[t % p])    inhash[t % p] = 1;
        else {
            cout << i+1 << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}

  

  B.DZY Loves Strings

    对于一个字符串,每个字母有相应的权值,由这个公式计算f(s)

现在能够插入k个任意字母,要求使得新的字符串f(s')值最大

  字符串就是幌子,替换成数字,插入的字符很明显贪心一下取最大的,一开始也没发现规律,瞎搞的,题解的意思大概是:

  插入x到位置p之后,则增加的权值为x*p+Ws(p+1)+Ws(p+2)...(p以后每一个权值) -> x+x+x+x...+Ws(p+1)+Ws(p+2)...容易观察到其实是用x替换前p个Ws,而由于贪心策略,x >= Ws.所以上式要取最大,p应该取|s|,对于剩下k-1个x同理,最终转化为把p个x插入到最后端得到的f(s‘)

(这份代码没删除之前乱搞的部分,比较乱就不贴了)

(B都写不来。。。这绝壁回家养猪节奏!)

  C.DZY Loves Sequences

  咋一眼看像是dp,可是自己死活想不出来思路,O(N),结果是自己没自习审题,ai, ai+1, ai+2 连续的!

  大意是给你一个数列,最多修改一个数字,球得最长的单调递增子串(严格的)(连续的!!!)

  可以设想一下如果x是要修改的数字,a[x+1]-a[x-1]>1(因为是整数,严格单调),以x为界左边是一个以a[x-1]为尾的单调增,右边是a[x+1]为首单调增,我们可以预处理出这两边的长度,然后枚举每一个a[i](2~n-1),取max即可

  预处理过程就算是简单dp吧...以从左往右为例 f[i] = 1 (a[i] <= a[i-1])  或 f[i-1]+1 (a[i] > a[i-1]) 

(审题呐!英文不好容易漏细节...)

  

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

const int maxn = 100000+50;

int n;
int a[maxn], r[maxn], l[maxn], f[maxn];

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", a+i);
    for(int i = 1; i <= n; i++)
        f[i] = 1;
    l[1] = 1;
    for(int i = 2; i <= n; i++)
        l[i] = (a[i] > a[i-1]? l[i-1]+1: 1);
    r[n] = 1;
    for(int i = n-1; i >= 1; i--)
        r[i] = (a[i] < a[i+1]? r[i+1]+1: 1);
    for(int i = 1; i <= n; i++) {
        if(i >= 2)  f[i] = max(f[i], 1+l[i-1]);
        if(i <= n-1)    f[i] = max(f[i], 1+r[i+1]);
        if(a[i+1] - a[i-1] > 1) f[i] = max(f[i], 1+r[i+1]+l[i-1]);
    }
    int ans = 0;
    for(int i = 1; i <= n; i++)
        ans = max(ans, f[i]);
    printf("%d
", ans);    
    return 0;
}

  

原文地址:https://www.cnblogs.com/gemmeg/p/3854371.html