Codeforces Round #FF

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   

  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\n", ans);        return 0;}

 

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.