1. 程式人生 > 實用技巧 >Codeforces Round #671 (Div. 2)

Codeforces Round #671 (Div. 2)

A

人wa傻了, 審題不清, 兩者選過的數字互不影響

#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
 
const int N = 1e5 + 5;
 
int n, m, _, k;
char s[N];
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> s + 1;
        int x = 0, y = 0, a = 0, b = 0;
        for (int i = 1; s[i]; ++i)
            if (i & 1) x += (s[i] - '0') & 1, y += !((s[i] - '0') & 1);
            else a += (s[i] - '0') & 1, b += !((s[i] - '0') & 1);
        if (x + y > a + b) 
            if (x) cout << 1 << '\n';
            else cout << 2 << '\n';
        else 
            if (b) cout << 2 << '\n';
            else cout << 1 << '\n';
    }
    return 0;
}

B

找規律, 下一個是由 上一個梯子放左下角, 上一個梯子放右上角, 和一個正方形組成

#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
 
const int N = 1e5 + 5;
 
int n, m, _, k, x, y;
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        ll n, ls = 1; cin >> n; m = 0;
        while (n >= ls) {
            ++m; n -= ls;
            ls = (ls << 1) + (1ll << (m << 1));
        }
        cout << m << '\n';
    }
    return 0;
}

C

貪心?

#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
 
const int N = 1e5 + 5;
 
int n, m, _, k, z;
int a[1005];
ll q, p;
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> m; q = p = z = 0;
        rep (i, 1, n) {
            cin >> a[i];
            if (a[i] == m) ++z;
            p += m - a[i];
        }
        if (z == n) cout << 0 << '\n';
        else if (!p || z) cout << 1 << '\n';
        else cout << 2 << '\n';
    }
    return 0;
}

D_1 & D_2

貪心模擬一下

#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
 
const int N = 1e5 + 5;
 
int n, m, _, k, x, y;
int a[N], b[N];
 
int main() {
    IOS; cin >> n;
    rep (i, 1, n) cin >> a[i];
    sort(a + 1, a + 1 + n);
    int l = 1;
    for (int i = 2; i <= n; i += 2) b[i] = a[l++];
    for (int i = 1; i <= n; i += 2) b[i] = a[l++];
    for (int i = 2; i <= n - 1; i += 2)
        m += (b[i - 1] > b[i] && b[i + 1] > b[i]);
    cout << m << '\n';
    rep (i, 1, n) cout << b[i] << ' ';
    return 0;
}

E

構造

\(n = p_1^c_1 * p_2^c_2 * ... * p_k^c_k\)

構造成 含有p_1的所有除數, p_1 * p_2, 含有p_2的所有除數, .... n

#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
 
const int N = 1e6 + 5;
 
int n, m, _, k;
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n; VI cnt, e; m = n;
        rep (i, 2, n / i)
            if (n % i == 0) {
                e.pb(i); cnt.pb(0);
                while (n % i == 0) ++cnt.back(), n /= i;
            }
        if (n > 1) e.pb(n), cnt.pb(1);
        if (e.size() == 1) {
            int res = e.back();
            while (cnt.back()--) cout << res << ' ', res *= e.back();
            cout << '\n' << 0 << '\n';
        }
        else if (e.size() == 2 && cnt[0] == 1 && cnt[1] == 1)
            cout << e[0] << ' ' << e[1] << ' ' << m << '\n' << 1 << '\n';
        else {
            vector<VI> ans(e.size()); unordered_set<int> factor;
            rep (i, 2, m / i) if (m % i == 0) factor.insert(i), factor.insert(m / i);
            per (i, e.size() - 1, 1) ans[i].pb(e[i] * e[i - 1]), factor.erase(e[i] * e[i - 1]);
            for (auto &i : factor)
                per (j, e.size() - 1, 0)
                    if (i % e[j] == 0) { ans[j].pb(i); break; }
            for (auto &i : ans) for (auto &j : i) cout << j << ' ';
            cout << m << '\n' << 0 << '\n';
        }
    }
    return 0;
}