1. 程式人生 > 實用技巧 >CF ER92 abc

CF ER92 abc

傳送門

A LCM Problem standard input/output 2 s, 256 MB Submit x14222

看了一下,發現最小的LCM x,y 應該有一個是L, 當連續兩個數 則 LCM為 L*(L+1) 如果是L 2L 那麼LCM為2L,明顯是最小的 如果 L 2L 不在 L R中那麼無解, 否則輸出 L 2L即可
B Array Walk standard input/output 2 s, 256 MB Submit x4503

相當於 每次不往左走的話,能直接從 a1 走到 ak+1, 然後當a1到 ak+1 如果有兩個連續的值和大於 ak+ak+1的話就用最大的和替換掉 ak

+ak+1

然後因為替換了 多走了兩步, 所以最多隻能走到ak-2 以此類推

因為總數小於3e5 所以可以直接列舉

兩種方法列舉,

第一種 列舉向左走的步數0-z,每次能到的位置為 k-2*z+1 然後記錄下所有走過的步值總和 和 最大的相鄰兩步之和,向左的兩步都走最大和 O(zk)

第二種 列舉步數0-k 然後每次記錄每次能向左走的步數 儲存最大值 O(k)

還可以用dp, dp[i][j] 走到第 i 處,向左走了 j 步 O(kz)

狀態轉移方程為dp[i][j] = max(dp[i][j], dp[i-1][j]+a[i]); 向左走一步

if(i!=0 and j+1!=z) dp[i-1][j+1] = max(dp[i-1][j+1], dp[i][j]+a[i-1]); 從向右走一步

C Good String standard input/output 2 s, 256 MB Submit x5918

變換一下, 會發現 一個good string 是這樣的 s2s3s4s5s6s7……sns1 == sns1s2s3s4s5s6s7……sn-1,對應一下也就是 s1 == s3 == sn-1 == s5 == s7

s2 == s4 == s6 == sn, good string 就是一個迴圈節<=2 的字串, 然後字串的字元為 0-9 那麼我們可以直接暴力列舉所有的迴圈節情況 一共 (10*10) 100種, 然後找到最長的good string, 總長度減去最長的good string即可, 程式碼如下

D Segment Intersections standard input/output 2 s, 256 MB Submit x1044 不想補。。。

#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define _for(i,a,b) for(int i = (a); i < (b); i++)
#define _rep(i,a,b) for(int i = (a); i <= (b); i++)

void taskA() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t; cin >> t;
    while(t--) {
        int l, r; cin >> l >> r;
        if(l*2 <= r) cout << l << " " << 2*l << "\n";
        else cout << "-1 -1\n";
    }
    return;
}
View A Code

void taskB1() {
    int t; cin >> t;
    while(t--) {
        int n,k,z; cin >> n >> k >> z;
        vector<int> a(n+1);
        _rep(i,1,n) cin >> a[i];
        int ans = 0;
        _rep(z1,0,z) {
            int x = 1, x1 = 0;
            int y = k+1-2*z1, ma = 0, s = 0;
            if(y < 1) continue;
            _for(i,1,y+1) {
                ma = max(ma, a[i]+a[i+1]), s += a[i];    
            }    
            ans = max(ans, s+ma*z1);
        }
        //ans += a[y];
        cout << ans << "\n";
    }
    return; 
}
View B1 Code

void taskB2() {
    int t; cin >> t;
    while(t--) {
        int n,k,z; cin >> n >> k >> z;
        vector<int> a(n, 0);
        _for(i,0,n) cin >> a[i];
        int s = 0, mx = 0, ans = 0;
        _rep(i,0,k) {
            if(i<n-1) mx = max(mx, a[i]+a[i+1]);
            s += a[i];
            if(i%2 == k%2) {
                int tmp = (k-i)/2;
                if(tmp <= z)
                    ans = max(ans, s+mx*tmp);
            }
        } cout << ans << "\n";
    } return;
}
View B2 Code

void taskB3() {
    int t; cin >> t;
    while(t--) {
        int n,k,z; cin >> n >> k >> z;
        int dp[k+1][z+1] = {};
        vector<int> a(n);
        _for(i,0,n) cin >> a[i];
        _for(i,0,z) dp[0][i] = 0;
        _rep(i,1,k) _rep(j,0,z) {
            int pos = k-i-2*j;
            if(pos<0 or pos>=n) { dp[i][j] = (-1e9); continue; }
            dp[i][j] = max(dp[i][j], dp[i-1][j]+a[pos+1]);
            if(pos!=0 and j!=z) 
                dp[i][j] = max(dp[i][j], dp[i-1][j+1]+a[pos-1]);
        }
        int ans = 0;
        _rep(i,0,z) ans = max(ans, dp[k][i]);
        cout << ans+a[0] << "\n";
    }
    return;
}
View dp Code

void taskB3() {
    int t; cin >> t;
    while(t--) {
        int n,k,z; cin >> n >> k >> z;
        int dp[k+1][z+1] = {};
        vector<int> a(n);
        _for(i,0,n) cin >> a[i];
        _for(i,0,z) dp[0][i] = 0;

        int ans = 0;
        _rep(i,1,k) _rep(j,0,z) {
            dp[i][j] = max(dp[i][j], dp[i-1][j]+a[i]);
            if(i+2*j == k) ans = max(ans, dp[i][j]);
            if(j+1 <= z) dp[i-1][j+1] = max(dp[i-1][j+1], dp[i][j]+a[i-1]);
            if(i-1+2*(j+1) == k) ans = max(ans, dp[i-1][j+1]);
        }
        cout << ans+a[0] << "\n";
    }
    return;
}
View dp2 Code

void taskC() {
    int t; cin >> t;
    while(t--) {
        string s; cin>>s;
        int n = s.size(), ans = 0;
        _for(i,0,10) {
            int ma = 0;
            _for(j,0,10) {
                int x = i, y = j, res = 0;
                for(auto c : s) 
                    if(c-'0' == x)
                        res++, swap(x, y);//妙啊 上一個匹配成功換下一個
                if(x!=y and res%2==1) res--;// 如果總數有多類似 12121 那麼需要-1
                ans = max(ans, res);
            }
        } cout << n-ans << "\n";
    } return;
}
View C Code

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
//    taskB3();
  taskC();
return 0; }