Codeforces Round #616 (Div. 2)題解

A. Even But Not Even

鏈接

https://codeforces.com/contest/1291/problem/A

題意

尋找特殊偶數,即本身不能被2整數但是數各位置上的數字之和能被2整除。

現給你一個數字s,你可以進行刪除某個位置上的數字的操作,但不可改變數字相對順序,可以操作多次,求解是否能的到特殊偶數若能則輸出操作後的偶數,若不能則輸出-1

解法

注意Note的內容

In the first test case of the example, 12271227 is already an ebne number (as 1+2+2+7=121+2+2+7=12, 1212 is divisible by 22, while in the same time, 12271227 is not divisible by 22) so we don’t need to delete any digits. Answers such as 127127 and 1717 will also be accepted.

也就是說17就行,那麼我只需要取兩位奇數就行,這樣一定無法被2整數,且數位之和爲偶數

代碼

int main(){
    int t; RD(t);
    while(t--){
        int n; RD(n);
        LL sum = 0;
        LL ans[3] = {0};
        int k = 0;
        string s; cin >> s;
        for(int i = 0; i < n; i++)  {
            int temp = s[i] - '0';
            if (temp%2 == 1 && k < 2){ans[k++] = temp;}
        }
        if(k < 2){ printf("-1\n");}
        else  cout << ans[0] << ans[1] << '\n';
    }
}

B. Array Sharpening

鏈接

https://codeforces.com/contest/1291/problem/B

題意

有一個長度爲n的數組,第i位上的數字爲ai,可以對每個位置上的數字進行ai = ai – 1;操作,但是要確保操作的數字目前 滿足ai > 0

解法

舉個例子

當3個數字的時候3個位置最小值是0 1 0,5個時候5個位置最小值是0 1 2 1 0
你判斷下這個位置的數是否大於等於這個最小值

也就是說你要確保每個位置的數大於一個數,偶數的話就是需要判斷中間兩位是否是否相等

  • n = 2,最小 1 1 (0, 1 | 1, 0)
  • n = 4,最小 0 2 2 0 (0,1,2,0 | 0,2,1,0)
  • ….應該很好理解
  • 就是說你n等於2的術後中間如果相等要a[i] >= n/2
  • 那麼其實你只需要判斷0 ~ n-1能否走完就行——>看代碼

代碼

const int maxn = 3e5 + 50;
LL a[maxn];
int main(){
    //freopen("in.txt", "r", stdin);
    int t; RD(t);
    while(t--){
        int n; RD(n);
        bool jud = true;
        for(int i = 0; i < n; i++){
            RD(a[i]);
        }
        int i = 0, j;
        for(i = 0; i + 1 < n && a[i + 1] >= i + 1; ++i);
        for(j = i; j < n &&a[j] >= (n - j - 1); ++j);
        if (j != n) jud = false;
        if (jud) printf("Yes\n");
        else printf("No\n");
    }
}

C

鏈接

https://codeforces.com/contest/1291/problem/C

題意

你和你的n-1個朋友排成一個隊伍,選擇ai,排在前面的優先選擇,但只能選擇第一個或者最後一個,每個ai只能被選擇一次。

在開始之前你可以選擇k個人說服他們選擇第一個還是最後一個元素,而剩餘的朋友的選擇是不受控制的,輸出您至少獲得的ai

解法

首先可以知道排在你身後的朋友對你的選擇是沒有影響的。

代碼

const int maxn = 1e5+50;
int a[maxn];
int main(){
    int t; RD(t);
    while(t--){
        int n, m, k; RD(n, m, k);
        REP(i, n){RD(a[i]);}
        k = min(m-1, k);
        int ans = 0;
        for(int i = 0; i <= k; i++){
            int tmp = 1e9;
            for(int j = 0; j < m-k; j++){
                tmp = min(tmp, max(a[i+j], a[n-1-(k-i)-(m-k-j-1)]));

            }
            ans = max(ans, tmp);
        }
        cout << ans << '\n';
    }
}

D

鏈接

題意

解法

代碼

E

鏈接

題意

解法

代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章