Comet OJ - Contest #8

Comet OJ - Contest #8

傳送門ios

A.殺手皇后

簽到。
c++


Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1005;
vector <string> v;
int n;
string s;
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> s;
        v.push_back(s);
    }
    sort(v.begin(), v.end());
    cout << v[0];
    return 0;
}

B.支援城市

把式子拆開就行。
數組


Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int n;
int w[N];
ll sumv, sum;
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> w[i], sum += w[i];
    for(int i = 1; i <= n; i++) sumv += 1ll * w[i] * w[i];
    for(int i = 1; i <= n; i++) {
        ll ans = sumv + 1ll * n * w[i] * w[i];
        ans -= 2ll * w[i] * sum;
        cout << ans << " \n"[i == n];
    }
    return 0;
}

C.符文能量

手玩一下樣例,發現答案與合併順序無關,而後就能夠愉快的\(dp\)了。
由於最終序列的狀態是有三個階段的,因此就\(dp[i,0/1/2]\)來表示三種狀態,而後分別轉移就行。
spa


Code

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
ll n, k;
ll a[N], b[N], c[N];
ll dp[N][3][2];
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n >> k;
    for(int i = 1; i <= n; i++) cin >> a[i] >> b[i];
    for(int i = 1; i < n; i++) c[i] = a[i + 1] * b[i];
    n--;
    for(int i = 1; i <= n; i++) {
        dp[i][0][0] = dp[i - 1][0][0] + c[i];

        dp[i][0][1] = min(dp[i - 1][0][1], dp[i - 1][1][1]) + c[i];

        dp[i][1][0] = dp[i - 1][0][0] + c[i] * k;
        dp[i][1][1] = min(dp[i - 1][2][1], dp[i - 1][1][0]) + c[i] * k;

        dp[i][2][1] = min(dp[i - 1][2][1], dp[i - 1][1][0]) + c[i] * k * k;
    }
    ll ans = INF;
    ans = min(ans, min(dp[n][0][0], min(dp[n][0][1], min(dp[n][1][0], min(dp[n][1][1], dp[n][2][1])))));
    cout << ans;
    return 0;
}

還有一種前綴和的搞法,感受說不太清楚,見代碼吧:
code


Code

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
ll n, k;
ll a[N], b[N], c[N], d[N];
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n >> k;
    for(int i = 1; i <= n; i++) cin >> a[i] >> b[i];
    for(int i = 1; i < n; i++) c[i] = a[i + 1] * b[i], d[i] = d[i - 1] + c[i];
    n--;
    ll ans = min(d[n], d[n] * k * k), Min = 0;
    for(int i = 1; i <= n; i++) {
        ans = min(ans, c[i] * k + d[i - 1] * k * k + d[n] - d[i] + Min);
        Min = min(Min, -d[i] * k * k + c[i] * k + d[i - 1]);
    }
    cout << ans;
    return 0;
}

D.菜菜種菜

題目給出的詢問都爲連續的區間,考慮離線處理.
將題目所求轉化爲數學語言就是,對於一段區間[l,r],找到全部的點\(u\),知足對於全部的\((u,v)\),不存在\(v\in [l,r]\)
那麼咱們就能夠直接對於全部的點找到一個最大區間[L,R],表示在這個區間中,點\(u\)是不能到達任意點的,那麼咱們對於每一個區間\([l,r]\),其中全部的點\(u\)對答案有貢獻的話就會知足:\(L\leq l,r\leq R\)
以後用樹狀數組進行增刪查詢的操做就行。
代碼以下:
ci


Code

#include <bits/stdc++.h>
#define MP make_pair
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e6 + 5;
int n, m, q;
short a[N];
int l[N], r[N];
vector <pii> v[N];
vector <int> del[N];
int c[N];
int lowbit(int x) {return x & (-x);}
void update(int x, int v) {
    for(; x < N; x += lowbit(x)) c[x] += v;
}
ll query(int x) {
    ll ans = 0;
    for(; x; x -= lowbit(x)) ans += c[x];
    return ans;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n >> m >> q;
    memset(r, INF, sizeof(r));
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        if(v < u) l[u] = max(l[u], v);
        else r[u] = min(r[u], v);
    }
    for(int i = 1; i <= n; i++) {
        l[i]++;
        if(r[i] == INF) continue;
        del[r[i]].push_back(i);
    }
    for(int i = 1; i <= q; i++) {
        int L, R; cin >> L >> R;
        v[R].push_back(MP(L, i));
    }
    ll ans = 0;
    for(int i = 1; i <= n; i++) {
        update(l[i], a[i]);
        update(i + 1, -a[i]);
        for(auto it : del[i]) {
            update(l[it], -a[it]);
            update(it + 1, a[it]);
        }
        for(auto it : v[i]) {
            ans ^= 1ll * it.second * query(it.first);
        }
    }
    cout << ans;
    return 0;
}
相關文章
相關標籤/搜索