码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #690 (Div. 3)

时间:2020-12-21 12:02:47      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:相交   sort   cout   lse   cin   uniq   sub   tuples   个数   

Codeforces Round #690 (Div. 3)

A - Favorite Sequence

按着题意读入, 顺序输出完事

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n;
        rep (i, 1, n) {
            if (i <= (n + 1 >> 1)) cin >> a[i * 2 - 1];
            else cin >> a[n - i + 1 << 1];
        }
        rep (i, 1, n) cout << a[i] << ‘ ‘; cout << ‘\n‘;
    }
    return 0;
}

B - Last Year‘s Substring

模拟, 比A麻烦

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> s + 1;
        bool f = 0;
        if (s[1] == ‘2‘ && s[2] == ‘0‘ && s[3] == ‘2‘ && s[4] == ‘0‘) f = 1;
        else if (s[n - 3] == ‘2‘ && s[n - 2] == ‘0‘ && s[n - 1] == ‘2‘ && s[n] == ‘0‘) f = 1;
        else if (s[1] == ‘2‘ && s[2] == ‘0‘ && s[3] == ‘2‘ && s[n] == ‘0‘) f = 1;
        else if (s[1] == ‘2‘ && s[2] == ‘0‘ && s[n - 1] == ‘2‘ && s[n] == ‘0‘) f = 1;
        else if (s[1] == ‘2‘ && s[n - 2] == ‘0‘ && s[n - 1] == ‘2‘ && s[n] == ‘0‘) f = 1;
        cout << (f ? "YES\n" : "NO\n");
    }
    return 0;
}

C - Unique Number

就9个数, 肯定不用0, 从9各位, 开始放就完事

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n; int i;
        for (i = 1; i <= 9 && n; ++i) a[i] = min(n, 10 - i), n -= a[i];
        if (i == 1 || n)  cout << "-1";
        else while (--i) cout << a[i]; cout << ‘\n‘;
    }
    return 0;
}

D - Add to Neighbour and Remove

无非是把序列分段, 每段的和一样, 直接枚举每段的首尾dp完事, 用unordered_map判断是否能转移

复杂度 \(O(n^2)\) 有个unordered_map的常数, 这次最喜欢的一道题

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n; unordered_map<int, int> st[n + 1];
        rep (i, 1, n) {
            cin >> a[i]; a[i] += a[i - 1]; st[i][a[i]] = i - 1;
            rep (j, 1, i - 1) if (st[j].count(a[i] - a[j])) st[i][a[i] - a[j]] = st[j][a[i] - a[j]] + i - j - 1;
        }
        int ans = n - 1;
        for (auto &i : st[n]) umin(ans, i.se);
        cout << ans << ‘\n‘;
    }
    return 0;
}

E1&E2 - Close Tuples

组合数学

这里只放E2的代码了, E1无非是m,k给定, 不取膜(C用平常约分的方式计算)

int C(int n, int m) {
    if (n < m) return 0;
    return fac[n] * facinv[m] % mod * facinv[n - m] % mod;
}
 
int main() {
    IOS; inv[0] = inv[1] = fac[0] = facinv[0] = 1;
    rep (i, 2, 2e5) inv[i] = (mod - mod / i) * inv[mod % i] % mod;
    rep (i, 1, 2e5) fac[i] = fac[i - 1] * i % mod, facinv[i] = facinv[i - 1] * inv[i] % mod; 
    for (cin >> _; _; --_) {
        cin >> n >> m >> k;
        rep (i, 1, n) cin >> a[i];
        sort(a + 1, a + 1 + n);
        ll ans = 0;
        for (int i = 1, j = 1; i <= n; ++i) {
            while(a[i] - a[j] > k) ++j;
            ans = (ans + C(i - j, m - 1)) % mod;
        }  
        cout << ans << ‘\n‘;
    }
    return 0;
}

F - The Treasure of The Segments

无非就是不相交, 右节点再当前段左节点左边, 左节点再当前段右节点右边 一定不相交

二分找一下就行, 正难则反

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n; VI l(n), r(n);
        rep (i, 1, n) cin >> a[i].fi >> a[i].se, l.pb(a[i].fi), r.pb(a[i].se);
        sort(all(l)); sort(all(r)); 
        int ans = n - 1;
        rep (i, 1, n) umin(ans, lower_bound(all(r), a[i].fi) - r.begin() + n - (upper_bound(all(l), a[i].se) - l.begin()));
        cout << ans << ‘\n‘;
    }
    return 0;
}

Codeforces Round #690 (Div. 3)

标签:相交   sort   cout   lse   cin   uniq   sub   tuples   个数   

原文地址:https://www.cnblogs.com/2aptx4869/p/14143286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!