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

Codeforces Round #675 (Div. 2)

A

直接最長邊

相當於等腰梯形

#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;
 
template<class T1, class T2> bool umin(T1& a, T2 b) { return a > b ? (a = b, true) : false; }
template<class T1, class T2> bool umax(T1& a, T2 b) { return a < b ? (a = b, true) : false; }
template<class T> void clear(T& a) { T().swap(a); }
 
const int N = 1e5 + 5;
 
int n, m, _, k;
int a[4];
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> a[1] >> a[2] >> a[3];
        sort(a + 1, a + 1 + 3);
        cout << a[3] << '\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;
 
template<class T1, class T2> bool umin(T1& a, T2 b) { return a > b ? (a = b, true) : false; }
template<class T1, class T2> bool umax(T1& a, T2 b) { return a < b ? (a = b, true) : false; }
template<class T> void clear(T& a) { T().swap(a); }
 
const int N = 1e5 + 5;
 
int n, m, _, k;
ll a[105][105], b[5];
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> m; ll ans = 0;
        rep (i, 1, n) rep (j, 1, m) cin >> a[i][j];
        rep (i, 1, n + 1 >> 1)
            rep (j, 1, m + 1 >> 1) {
                if (i == n - i + 1 && j == m - j + 1) continue;
                if (i == n - i + 1) { ans += abs(a[i][j] - a[i][m - j + 1]); continue; }
                if (j == m - j + 1) { ans += abs(a[i][j] - a[n - i + 1][j]); continue; }
                b[1] = a[i][j]; b[2] = a[i][m - j + 1];
                b[3] = a[n - i + 1][j]; b[4] = a[n - i + 1][m - j + 1];
                ll mi = 2e18;
                rep (p, 1, 4) {
                    ll res = 0;
                    rep (q, 1, 4) res += abs(b[p] - b[q]);
                    umin(mi, res); 
                }
                ans += mi;
            }
        cout << ans << '\n';
    }
    return 0;
}

C

自閉了, 掛機了1h+, 掉分了≡(▔﹏▔)≡

對於每一位, 分別計算就行了, 看看程式碼就明白了

#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;
 
template<class T1, class T2> bool umin(T1& a, T2 b) { return a > b ? (a = b, true) : false; }
template<class T1, class T2> bool umax(T1& a, T2 b) { return a < b ? (a = b, true) : false; }
template<class T> void clear(T& a) { T().swap(a); }
 
const int N = 1e5 + 5, mod = 1e9 + 7;
 
int n, m, _, k;
ll sum[N], a[N], t[N] = {1};
char s[N];
 
int main() {
    IOS; cin >> s + 1; n = strlen(s + 1);
    rep(i, 1, 1e5) {
        t[i] = t[i - 1] * 10 % mod;
        a[i] = (a[i - 1] + i) % mod;
        sum[i] = i * t[i - 1] % mod;
        sum[i] = (sum[i - 1] + sum[i]) % mod;
    }
    ll ans = 0;
    per(i, n, 1) {
        ans = (ans + (s[i] ^ 48) * sum[n - i] % mod) % mod;
        ans = (ans + (s[i] ^ 48) * a[i - 1] % mod * t[n - i]) % mod;
    }
    cout << ans;
    return 0;
}

D

最短路, 當然你 m * m建邊必t

要貪心的去建邊

對於 傳送站 i\((x_i), y_i\), j\((x_j, y _j)\) \((x_i <= x_j)\)

距離則為min(abs(\(x_i - x_j\)), (\(y_i, y_j\)))

假設 abs(\(x_i - x_j\)) <= (\(y_i, y_j\)), 存在傳送站 k(\(x_k, y_k\)) 且(\(y_k == y_i\) && (\(x_i <= x_k <= x_j\))), 那麼i和j的距離可能會更新(最少不會增加)

所以我們應該直接先去找這樣的 k, 就有了 (i,k) (k, j) 建邊而不是 三個傳送站兩兩建邊, 建邊就被優化了

#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;
 
template<class T1, class T2> bool umin(T1& a, T2 b) { return a > b ? (a = b, true) : false; }
template<class T1, class T2> bool umax(T1& a, T2 b) { return a < b ? (a = b, true) : false; }
template<class T> void clear(T& a) { T().swap(a); }
 
const int N = 1e5 + 5, mod = 1e9 + 7;
const ll inf64 = 1e18;
 
int n, m, _, k;
 
int main() {
    IOS; cin >> n >> m;
    vector<ll[2]> pos(m + 2);
    cin >> pos[m][0] >> pos[m][1] >> pos[m + 1][0] >> pos[m + 1][1];
    rep(i, 0, m - 1) cin >> pos[i][0] >> pos[i][1];
 
    vector<vector<pair<ll, int>>> e(m + 2);
    rep(i, 0, m) e[i].pb({ abs(pos[i][0] - pos[m + 1][0]) + abs(pos[i][1] - pos[m + 1][1]), m + 1 });
 
    rep(k, 0, 1) {
        vector<pair<ll, int>> a(m + 1);
        rep(i, 0, m) a[i] = { pos[i][k], i };
        sort(all(a));
        rep(i, 1, m) {
            ll d = a[i].fi - a[i - 1].fi;
            int x = a[i].se, y = a[i - 1].se;
            e[x].pb({ d, y }); e[y].pb({ d, x });
        }
    }
 
    vector<bool> v(m + 2); vector<ll> d(m + 2, inf64); d[m] = 0;
    priority_queue<pair<ll, int>> q; q.push({ -0, m });
    while (!q.empty()) {
        int x = q.top().se; q.pop();
        if (v[x]) continue; v[x] = 1;
        for (auto& i : e[x]) {
            int y = i.se; ll w = i.fi;
            if (d[y] <= d[x] + w) continue;
            d[y] = d[x] + w;
            q.push({ -d[y], y });
        }
    }
    cout << d[m + 1] << '\n';
    return 0;
}