Cattle passenger Challenge 38 (A - C)

 

A - polygon circle

The meaning of problems: given radius of the circle and the coordinates of a polygon, the polygon can roll within the circle, a point Q becomes a path length through the center of the rotation during the rotation center to become lower.

Thinking: Enumeration point 2 - n be the case from the rotational center point of the system 1 is not trained computational geometry, the code may be improved.


view code


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inc(i, l, r) for (int i = l; i <= r; i++)

const int maxn = 1e2 + 5;

int n, r;
double x[maxn], y[maxn], res;

double dis(int i, int j) {
    return sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
}
double aatan(int i, int j) {
    if (x[i] == x[j]) {
        if (y[i] > y[j]) return M_PI / 2;
        return 3 * M_PI / 2;
    }
    return atan((y[i] - y[j]) / (x[i] - x[j]));
}
double alp(int i) {
    double r = aatan(i, i - 1) - aatan(i, i + 1);
    while (r < 0) r += M_PI;
    while (r > M_PI) r -= M_PI;
    return r;
}

int main() {
    cin >> n >> r;
    inc(i, 1, n) cin >> x[i] >> y[i];
    x[n + 1] = x[1], y[n + 1] = y[1];
    inc(i, 1, n - 1) {
        double d = dis(i, i + 1), d2 = dis(i + 1, i + 2), p = dis(1, i + 1);
        double a = asin(d / 2 / r), a2 = asin(d2 / 2 / r);
        res += (M_PI - a - a2 - alp(i + 1)) * p;
    }
    printf("%.12f\n", res);
}


 

B - flip substring

The meaning of problems: given a string of length n with a set of q operations, one is to select the length of the interval m. \ ([X_i, m x_i + -. 1] \) flipping, one is on a query string character positions each time the reversing operation start coordinates \ (x_i \) is strictly diminished. n ≤ 1e6, q ≤ 1e6 .

Thinking: monotonic operation coordinates, using a sliding window to maintain flipped section was a sliding window of length m interval with. STD :: the deque stored inside the element, it scans from left to right again interval encountered flipping in operation, flip marked mark (or marks flip cancellation) without actually changing the internal element when in the inverted state, the movement of the sliding window, the team would have to discard the first element, but in fact the element in the queue tail, so execution. pop_front(); would have to go the tail adding elements, but this interval is reversed state, it should be added to the first team to correctly respond to the query. consider the location of the marker to flip the current sliding window query. the time complexity of O (n).


view code


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inc(i, l, r) for (int i = l; i <= r; i++)

const int maxn = 1e6 + 5;

int n, m, q, x, y, f, top;
char a[maxn];
deque<char> deq;

int main() {
    scanf("%d %d %d", &n, &m, &q);
    scanf("%s", a + 1);
    inc(i, 1, m) deq.push_back(a[i]);
    top = 1;
    while (q--) {
        scanf("%d %d", &x, &y);
        if (x == 1) {
            while (top < y) {
                if (f) {
                    a[top] = deq.back();
                    deq.pop_back();
                    deq.push_front(a[top + m]);
                } else {
                    a[top] = deq.front();
                    deq.pop_front();
                    deq.push_back(a[top + m]);
                }
                top++;
            }
            f = 1 - f;
        } else {
            if (y >= top && y < top + m) {
                if (f)
                    printf("%c", deq[m - 1 - (y - top)]);
                else
                    printf("%c", deq[y - top]);
            } else
                printf("%c", a[y]);
        }
    }
    printf("\n");
}


 

C - Round Table Dinner

Meaning of the questions: A and B Chinese people have to eat at a round table n positions, A people does not sit next to someone's location, B people will not sit within 2 position from the beginning was a round table on nobody. , then there is a probability p / (p + q) is a people into a probability q / (p + q) of B into a people; he will randomly pick a location, if not directly meet his requirements away. Finally I asked again when not receiving the desired number of people, 998,244,353 modulo. n ≤ 3e5, 0 ≤ p , q <998244353.

Ideas: First, without any restrictions when the first person to sit down, sit down, and after everyone is indifferent so the question becomes to consider the remaining n - 1 consecutive location can accommodate the number of people defined dp [i.. ] of i successive positions can accommodate people expect, p does not have dp is 0 [1] = dp [2 ] = 0, dp [3] = dp [4] = 1 (p is 0 dp [3] = dp [4] = 0) , the enumeration state transition to the spacing position. deduced \ [dp [j] = \ frac {p × \ sum_ {i = 2} ^ {j-1} {(dp [ i - 1] + dp [j - i])} + q × \ sum_ {i = 3} ^ {j-2} {(dp [i - 1] + dp [j - i])}} {p × (j - 2) + q × (j - 4)} = \ frac {2p × sdp [j - 2] + 2q × sdp [j - 3]} {p × (j - 2) + q × (j - . 4)} \]
(SDP [] is DP [] and prefix)


view code


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inc(i, l, r) for (int i = l; i <= r; i++)

const int maxn = 3e5 + 5;
const int mod = 998244353;

ll dp[maxn], s[maxn];
int n;
ll p, q;

inline ll ksm(ll a, ll n) {
    ll r = 1;
    while (n) {
        if (n & 1) r = r * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return r;
}

int main() {
    scanf("%d %lld %lld", &n, &p, &q);
    if (p) dp[3] = dp[4] = s[3] = 1, s[4] = 2;
    inc(i, 5, n - 1) {
        dp[i] = ((2 * p * s[i - 2] + 2 * q * s[i - 3]) % mod *
                     ksm((p * (i - 2) + q * (i - 4)) % mod, mod - 2LL) +
                 1) %
                mod;
        s[i] = (s[i - 1] + dp[i]) % mod;
    }
    printf("%lld\n", (dp[n - 1] + 1) % mod);
}


 

Guess you like

Origin www.cnblogs.com/hs-zlq/p/12537451.html