55-53: 模拟赛

处理处两个数组

离散化

树状数组

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>

using namespace std;

#define gc getchar()
inline int read() {
    int x = 0; char c = gc;
    while(c < '0' || c > '9') c = gc;    
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
    return x;
}

#define LL long long

const int N = 1e5 + 10;

LL n, Mod;
LL a[N];
LL f[N], g[N], B[N << 1];
int len;

LL Ksm(LL a, LL b) {
    LL ret = 1;
    while(b) {
        if(b & 1) ret = ret * a % Mod;
        a = a * a % Mod;
        b >>= 1;
    }
    return ret;
}

LL Tree[N << 1];

int Lowbit(int x) {
    return x & (-x);
}

void Add(int x) {
    while(x <= len) Tree[x] ++, x += Lowbit(x);
}

LL Ask(int x) {
    LL ret = 0;
    while(x) {
        ret += Tree[x];
        x -= Lowbit(x);
    }
    return ret;
}

int main() {
    freopen("calc.in", "r", stdin);
    freopen("calc.out", "w", stdout);
    n = read(), Mod = read();
    for(int i = 1; i <= n; i ++) a[i] = read();
    for(int i = 1; i <= n; i ++) f[i] = Ksm(i, a[i]);
    for(int i = 1; i <= n; i ++) g[i] = Ksm(a[i], i);
    for(int i = 1; i <= n; i ++) B[++ len] = f[i];
    for(int i = 1; i <= n; i ++) B[++ len] = g[i];
    sort(B + 1, B + len + 1);
    len = unique(B + 1, B + len + 1) - B - 1;
    for(int i = 1; i <= n; i ++) f[i] = lower_bound(B + 1, B + len + 1, f[i]) - B;
    for(int i = 1; i <= n; i ++) g[i] = lower_bound(B + 1, B + len + 1, g[i]) - B;
    LL Ans = 0;
    for(int i = n; i >= 1; i --) {
        Ans += Ask(f[i] - 1);
        Add(g[i]);
    }
    cout << Ans;
    return 0;
}

二分答案

贪心判断

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>


using namespace std;

#define LL long long

#define gc getchar()
inline LL read() {
    LL x = 0; char c = gc;
    while(c < '0' || c > '9') c = gc;    
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
    return x;
}

const int N = 5e5 + 10;

LL n, R, K;
LL a[N];
LL sum[N], b[N];
LL Sum[N];

inline bool See(LL x) {
    for(int i = 0; i <= n; i ++) Sum[i] = sum[i], b[i] = 0;
    LL tot = 0;
    for(int i = 1; i <= n; i ++) {
        b[i] += b[i - 1];
      Sum[i] += b[i];
        if(Sum[i] < x) {
            LL del = x - Sum[i];
            b[i] += del;
            tot += del;
            if(tot > K) return 0;
            int r = min(n, i + R + R);
            b[r + 1] -= del;
        }
    }
    return 1;
}

int main() {
    freopen("game.in", "r", stdin);
    freopen("game.out", "w", stdout);
    
    n = read(), R = read(), K = read();
    for(int i = 1; i <= n; i ++) a[i] = read();
    for(int i = 1; i <= n; i ++) {
        int ll = max(1 * 1ll, i - R), rr = min(n * 1ll, i + R);
        sum[ll] += a[i], sum[rr + 1] -= a[i];
    }
    for(int i = 1; i <= n; i ++) sum[i] += sum[i - 1];
    LL Ans;
    LL l = 0, r = 1e18 + 5e14;
    while(l <= r) {
        LL mid = (l + r) >> 1;
        if(See(mid)) l = mid  + 1, Ans = mid;
        else r = mid - 1;
    }
    cout << Ans;
    
    return 0;
}

for循环

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>

using namespace std;

#define gc getchar()
inline int read() {
    int x = 0; char c = gc;
    while(c < '0' || c > '9') c = gc;    
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
    return x;
}

#define LL long long

const int N = 1e5 + 10;

int fa[N];

struct Node {
    int u, v, w;
    bool operator < (const Node a) const {
        return this->w < a.w;
    }
} G[N];
int n, m, p;

int Get(int x) {
    return fa[x] == x ? x : fa[x] = Get(fa[x]);
}

LL Ans[N], B[N];
int len;
LL Size[N];


int main() {
    freopen("graph.in", "r", stdin);
    freopen("graph.out", "w", stdout);
    
    n = read(), m = read(), p = read();
    for(int i = 1; i <= m; i ++) {
        G[i] = (Node) {
            read(), read(), read()
        };
    }
    sort(G + 1, G + m + 1);
    for(int i = 1; i <= n; i ++) fa[i] = i;
    LL Nowmax = -1;
    for(int i = 1; i <= n; i ++) {
        int w = G[i].w;
        while(G[i].w == w) {
            int u = G[i].u, v = G[i].v;
            int fu = Get(u), fv = Get(v);
            if(fu == fv) {
                Size[fu] += G[i].w;
                if(Size[fu] > Nowmax) {
                    Nowmax = Size[fu];
                }
            } else {
                if(Size[fu] > Size[fv]) {
                    fa[fv] = fu;
                    Size[fu] += Size[fv] + G[i].w;
                    if(Size[fu] > Nowmax) {
                        Nowmax = Size[fu];
                    }
                 } else {
                    fa[fu] = fv;
                    Size[fv] += Size[fu] + G[i].w;
                    if(Size[fv] > Nowmax) {
                        Nowmax = Size[fv];
                    }
                }
            }
            i ++;
        }
        i --;
        B[++ len] = G[i].w;
        Ans[len] = Nowmax;
    }
    for(int i = 1; i <= p; i ++) {
        int w_ = read();
        int w = lower_bound(B + 1, B + len + 1, w_) - B;
        if(B[w] > w_) w --;
        if(w > len) w = len;
        cout << Ans[w] << "\n";
    }
    
    return 0;
}

 

posted @ 2018-11-06 15:16  xayata  阅读(231)  评论(2编辑  收藏  举报