Codeforces Round #690 (Div. 3)

第25小時發表於2020-12-19

Codeforces Round #690 (Div. 3)

比賽的時候f想到思路,寫了一半比賽就結束了,難受

A.Close Tuples

題意:

題解:

程式碼:

/*
6
7
3 4 5 2 9 1 1
4
9 2 7 1
11
8 4 3 1 2 7 8 7 9 4 2
1
42
2
11 7
8
1 1 1 1 1 1 1 1

*/
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
int const N = 2e5 + 10;
int n, m, T, a[N];

int main() {
    cin >> T;
    while(T--) {
        cin >> n;
        for (int i =1 ; i <= n; ++i) cin >> a[i];
        int i = 1, j = n;
        while(i < j) {
            cout << a[i] << " " << a[j] << " ";
            i++, j--;
        }
        // cout << endl;
        if (i == j) cout << a[i] << endl;
        else cout << endl;
    }   
    return 0;
}

B.Close Tuples

題意:

題解:

程式碼:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
int const N = 2e5 + 10;
int n, m, T;

int main() {
    cin >> T;
    while(T--) {
        cin >> n;
        string s;
        cin >> s;
        if (s == "2020") {
            puts("YES");
            continue;
        }
        int len = s.size();
        int flg = 0;
        for (int i = 0; i < len; ++i) {
            for (int j = i; j < len; ++j) {
                if (n - (j - i + 1) != 4) continue;
                string s1 = s.substr(0, i);
                string s2 = s.substr(j + 1);
                string s3 = s1 + s2;
                if (s3 == "2020") {
                    flg = 1;
                    break;
                }

            }
            if (flg) break;
        }
        if (flg) puts("YES");
        else puts("NO");
    }   
    return 0;
}

C.Close Tuples

題意:

題解:

程式碼:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
int t, n;

int main() {
    LL res[50] = {1,        2,        3,         4,       5,       6,
                  7,        8,        9,         19,      29,      39,
                  49,       59,       69,        79,      89,      189,
                  289,      389,      489,       589,     689,     789,
                  1789,     2789,     3789,      4789,    5789,    6789,
                  16789,    26789,    36789,     46789,   56789,   156789,
                  256789,   356789,   456789,    1456789, 2456789, 3456789,
                  13456789, 23456789, 123456789, - 1,      - 1,      - 1,
                  - 1,       - 1};
    cin >> t;
    while (t--) {
        cin >> n;
        cout << res[n - 1] << endl;   
    }
    return 0;
}

D.Close Tuples

題意:

題解:

程式碼:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int const N = 2e5 + 10;
int a[N];
LL sum[N];
vector<LL> v;
int n, t;

// 試除法求約數
vector<LL> get_divisors(LL x) {
    vector<LL> res;                                // 記錄答案
    for (int i = 1; i <= x / i; ++i) {             // 列舉到sqrtx(x)
        if (x % i == 0) {                          // 如果能夠整除
            res.push_back(i);                      // 放入i
            if (i != x / i) res.push_back(x / i);  // x/i不等於i,也放入答案中
        }
    }
    sort(res.begin(), res.end());  // 排序
    return res;
}

int main() {
    cin >> t;
    while (t--) {
        cin >> n;
        LL average = 0;
        for (int i = 1; i <= n; i++) 
            cin >> a[i], sum[i] = sum[i - 1] + a[i], average += a[i];
        
        v = get_divisors(average);
        LL ans = 1e9;
        for (int k = 0; k < v.size(); k++) {
            LL Sum = 0;
            int flg = 1;
            for (int i = 1; i <= n; i++) {
                Sum += a[i];
                if (Sum == v[k]) Sum = 0;
                
                if (Sum > v[k]) {
                    flg = 0;
                    break;
                }
            }
            if (flg) ans = min(ans, n-average/v[k]);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

E.Close Tuples

題意:

題解:

程式碼:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

int const N = 2e5 + 10, mod = 1e9 + 7;
int fact[N], infact[N];
int t, n, m, k, a[N];

LL qmi(LL a, LL k, LL p) {
    LL res = 1;
    while (k) {
        if (k & 1) res = res * a % p;
        k >>= 1;
        a = a * a % p;
    }
    return res;
}

// 預處理
void init() {
    fact[0] = infact[0] = 1;
    for (int i = 1; i < N; ++i) {
        fact[i] = (LL)fact[i - 1] * i % mod;
        infact[i] = (LL)infact[i - 1] * qmi(i, mod - 2, mod) % mod;
    }
}
LL res;

int main() {
    init();
    cin >> t;
    while (t--) {
        scanf("%d%d%d", &n, &m, &k);
        res = 0;
        for (int i = 0; i < n; i++) scanf("%d", a + i);
        sort(a, a + n);
        for (int i = 0; i < n; i++) {
            int pos = upper_bound(a, a + n, a[i]+k) - a - 1;
            if (pos - i + 1 < m) continue;
            res = (res + (LL)fact[pos - i ] * infact[m-1] % mod *
                   infact[pos - i + 1 - m] % mod) % mod;
        }
        printf("%lld\n", res);
    }
    return 0;
}

F.The Treasure of The Segments

題意: 給出n個區間,問最少需要刪去多少個區間,使得剩下的區間滿足至少有一個區間和其他所有剩下的區間都相交

題解: 和區間i不相交的區間j滿足:j的右區間小於i的左區間 或者 j的左區間大於i的右區間。所以只需要兩次按照端點排序,分別二分即可

程式碼:

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int t, n, temp[N];
struct node {
    int l, r, ln, rn;
} a[N];

bool cmpl(node a, node b) { return a.l < b.l; }
bool cmpr(node a, node b) { return a.r < b.r; }
int main() {
    cin >> t;
    while (t--) {
        cin >> n;
        for (int i = 0; i < n; i++) {
            cin >> a[i].l >> a[i].r;
            a[i].ln = a[i].rn = 0;
        }
        sort(a, a + n, cmpl);
        for (int i = 0; i < n; i++) {
            temp[i] = a[i].l;
        }
        for (int i = 0; i < n; i++) {
            int pos = upper_bound(temp, temp + n, a[i].r) - temp;
            a[i].rn = n-pos;
        }
        sort(a, a + n, cmpr);
        for (int i = 0; i < n; i++) {
            temp[i] = a[i].r;
        }
        for (int i = 0; i <n; i++) {
            int pos = lower_bound(temp, temp + n, a[i].l) - temp-1;
            a[i].ln = pos + 1;
        }
        int res = 0x3f3f3f3f;
        for (int i = 0; i < n; i++) {
            res = min(a[i].ln + a[i].rn, res);
        }
        cout << res << endl;
    }
    return 0;
}

相關文章