2019.9.24 simulation game

T1 week

The topic has been hidden over the water
$ O (2 ^ {15}) $ dfs violent enough.

Direct throw Code

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
const int MAXN = 20;
int a[MAXN], b[MAXN], c[MAXN], d[MAXN];
int ans = 0;
void dfs(int day, int oi, int whk) {
    if (day > n) {
        ans = max(ans, oi * whk);
        return;
    }
    dfs(day + 1, max(oi - b[day], 0ll), whk + a[day]);
    dfs(day + 1, oi + c[day], max(whk - d[day], 0ll));
}
signed main() {
    scanf("%lld", &n);
    for (register int i = 1; i <= n; ++i) scanf("%lld%lld%lld%lld", &a[i], &b[i], &c[i], &d[i]);
    dfs(0, 0, 0);
    printf("%lld\n", ans);
    fclose(stdin);
    fclose(stdout);
    return 0;
}

T2 any

A black and white rectangular regions of satisfying such properties on the drawing board: that if the same color blocks can be connected in four directions up and down, then the two black squares or not any communication, but the communication between either only one simple path ( Unique path through a grid of the same).
This rectangular region \ (N \) OK \ (M \) column, from top to bottom for the first \ (1, 2, 3 ... N-1, N \) row, from left to right for the first \ (1, 2, 3 ... M-1, M \) column.
Guo God asks each child a rectangle within this rectangle. When considering only the pixels within the sub-rectangle (i.e., and not beyond the sub-pixels from the interior of the rectangular sub-rectangle communicated), ask the black squares in the number of sub-rectangles communication block.
Any guarantee only up to a simple path between two black pixels.
## ye do
pay attention to moderation
attention moderation
attention moderation
title says China Unicom is part of a tree. So the whole picture is a forest, then converted to the subject of inquiry within the range of how many stars tree. What is the nature of the tree do? There are known tree \ (n-\) points, \ (. 1-n-\) edges. When we know the number of points and edges, the number of trees cut tree is the number of sides of points.
Points of the tree, the number of sides can \ (O (nm) \) pre-treatment, re-use two-dimensional prefix and will be able to do \ (O (1) \) queries. Overall complexity \ (O (nm + Q) \) .

Code

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e3 + 5;
char mp[MAXN][MAXN];
bool v[MAXN][MAXN];
int sum1[MAXN][MAXN], sum2[MAXN][MAXN], sum3[MAXN][MAXN];
int step[][2] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
int n, m, q;

inline void bfs() {
    queue<pair<int, int>> q;
    for (register int i = 1; i <= n; ++i)
        for (register int j = 1; j <= m; ++j) {
            if (mp[i][j] == '1' && !v[i][j]) {
                q.push(make_pair(i, j));
                while (q.size()) {
                    int x = q.front().first, y = q.front().second;
                    int xi, yi;
                    v[x][y] = true;
                    q.pop();
                    for (register int i = 0; i <= 0; ++i) {
                        xi = x;
                        yi = y + step[i][1];
                        if (mp[xi][yi] == '1' && !v[xi][yi] && xi >= 1 && xi <= n && yi >= 1 && yi <= m) {
                            ++sum3[x][y];
                            q.push(make_pair(xi, yi));
                        }
                    }
                }
            }
        }
    memset(v, 0, sizeof(v));
    for (register int i = 1; i <= n; ++i)
        for (register int j = 1; j <= m; ++j) {
            if (mp[i][j] == '1' && !v[i][j]) {
                q.push(make_pair(i, j));
                while (q.size()) {
                    int x = q.front().first, y = q.front().second;
                    int xi, yi;
                    v[x][y] = true;
                    q.pop();
                    for (register int i = 2; i <= 2; ++i) {
                        xi = x + step[i][0];
                        yi = y;
                        if (mp[xi][yi] == '1' && !v[xi][yi] && xi >= 1 && xi <= n && yi >= 1 && yi <= m) {
                            ++sum2[x][y];
                            q.push(make_pair(xi, yi));
                        }
                    }
                }
            }
        }
}
inline void test(int S[][MAXN]) {
    cerr << "-----##-----" << endl;
    for (register int i = 1; i <= n; ++i) {
        for (register int j = 1; j <= m; ++j) cerr << S[i][j] << " ";
        cerr << endl;
    }
}
inline void sum(int S[][MAXN]) {
    for (register int i = 1; i <= n; ++i)
        for (register int j = 1; j <= m; ++j) S[i][j] = S[i - 1][j] + S[i][j - 1] - S[i - 1][j - 1] + S[i][j];
}
inline int calc(int s[][MAXN], const int &a, const int &b, const int &c, const int &d) {
    register int t = s[c][d] + s[a - 1][b - 1] - s[c][b - 1] - s[a - 1][d];
    return t;
}
int main() {
#ifdef lky233
    freopen("duty.in", "r", stdin);
    freopen("duty.out", "w", stdout);
#endif
    scanf("%d %d %d", &n, &m, &q);
    for (register int i = 1; i <= n; ++i) scanf("%s", mp[i] + 1);
    bfs();
    for (register int i = 1; i <= n; ++i)
        for (register int j = 1; j <= m; ++j)
            sum1[i][j] = sum1[i - 1][j] + sum1[i][j - 1] - sum1[i - 1][j - 1] + (mp[i][j] - '0');
    sum(sum2);
    sum(sum3);
    for (register int i = 1, a, b, c, d; i <= q; ++i) {
        scanf("%d %d %d %d", &a, &b, &c, &d);
        printf("%d\n", calc(sum1, a, b, c, d) - calc(sum2, a, b, c - 1, d) - calc(sum3, a, b, c, d - 1));
    }
}

T3 fly

Space constraints \ (32MB \)
liu_runda decided to raise the level of knowledge about, so he went to ask God Guo Guo God readily gave liu_runda a question of God, liu_runda and will not do, so put this question to throw in the High School entrance exam do.
Guo God \ (n-\) bar is located in the first quadrant segments, and each segment is given \ (X \) axis and \ (Y \) axes intersection, so obviously you can uniquely identify each segment.
\ (n-\) line segments and \ (Y \) axis intersection of the ordinate are \ (1,2,3,4 ... n-\) . We remember and \ (Y \) axis intersection ordinate \ (I \) and the lines (X \) \ axis intersection of the abscissa is \ (x [i] + 1 \) generated in such a manner:
\ ( x [1] \) is given by the input. \ [x [i] = ( x [i-1] + a), 2 \ leq i \ leq n \] That is: If \ (X [. 3] =. 4 \) , then the \ (Y \) axis intersection ordinate \ (3 \) parabolic and (X \) \ axis of abscissa intersection \ (4 + 1 = 5 \).
We guarantee given \ (n, x [1] , a, mod \) such that all \ (x [1] \) different from each other.
For all points in the first quadrant (point of horizontal and vertical coordinates may be any real number), if a dot is \ (X \) line segments through its Kichiku value is \ (\ frac {x \ times (x-1 )} {2} \) .
The first demand value Kichiku all points within a quadrant and.

How to do it

Firstly, some of the characteristics of this equation. We found that if two lines intersect, will have contributed 1, and if the three lines intersect at one point, will have contributed 3, respectively, equivalent to three lines intersect, and so on, for the sake of this question is converted there have been several the case where two lines intersect.
Since each line plus a y-axis, each line will intersect the line is larger than its x. So far, this problem is converted to a one-dimensional problem.
After reasoning, there are two ideas.


According to insert the time to sort the x, we found that when x and x remaining reverse order, will have contributed the number of lines in front of him, so the problem becomes a question of seeking to reverse.
But \ (MOD \ Leq 10 ^ 8 \) , is directly do not fit. Discrete? \ (the n-\ Leq 10 ^ 7 \) , if open a \ (1e7 \) array space still explode.
Analysis \ (X \) the number of columns in the \ (X \) before the mold is taken, this period interval is incremented, the contribution does not occur during this interval, the number of which will be after an interval corresponding to the upper position generating contribution for each number in the interval, this contribution to the arithmetic sequence, can be quickly processed.
Contribution generated corresponding position, since each segment is the regular interval, so we only recorded the start position of each segment interval \ (X \) is formed on the reverse number, each contribution will have the reverse of the interval length. Because of \ (X1 \) limit, the first segment is not complete, needs to be calculated separately. \ (a \) in the range of \ (1E5 \) , this problem can be easily passed.
Thanks \ (@ jiqimao \) Gangster provides this excellent algorithm.


This is my bad algorithm:
According to \ (x \) to sort of position, we find that each (x \) \ contribution generated Behind him is the number of x. The same before modulo this period, \ (X \) is incremented, and every time \ (x + a \) , the row (X \) \ Number interval period before the number will be subtracted. Therefore, paragraph \ (x \) contribution is the arithmetic series, each cycle period, you can get an answer.

Poor quality codes

Arithmetic sequence boundary process is too cumbersome it done recurrence. This violence is running

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 1e5 + 5;
int A[MAXN];
int a, mod, x1, n;
long long ans;
inline void add(int x) {
    for (; x <= a; x += x & (~x + 1)) A[x] += 1;
}
inline int ask(int x) {
    int ans = 0;
    for (; x; x -= x & (~x + 1)) ans += A[x];
    return ans;
}
inline int calc(int a1, int d, int n) { return a1 * n + (n * (n - 1)) * d; }
signed main() {
    cin >> n >> x1 >> a >> mod;

    for (register int i = 1, now = x1, cnt = 0, lun = 0; i <= n; ++i) {
        if (now >= a) {
            cnt -= lun;
            if (x1 > now)
                ++cnt;
            ans += cnt;
        } else
            cnt = i - 1 - ask(now + 1), ans += cnt, add(now + 1);
        now += a;
        if (now >= mod)
            now -= mod, ++lun;
    }
    cout << ans << endl;
}

Guess you like

Origin www.cnblogs.com/Shiina-Rikka/p/11584458.html