AtCoder Beginner Contest 128 F - Frog Jump

The meaning of problems

There are a frog, with a \ (0, 1, \ cdots , N - 1 \) a lotus leaf. You have the right value on each lotus \ (S_I \) .

  1. Selected \ (A \) , \ (B \) , the initial fraction \ (0 \) .
    The current location is \ (the X-\) :
  2. For \ (X + Y = A \) :
    • If \ (Y = N -. 1 \) , the game ends.
    • If \ (the y-\ neq N - 1 \) , but (y \) \ The lotus leaf is present, the score increased by \ (S_I \) , and this piece of lotus leaf disappears.
    • If \ (Y \ N NEQ -. 1 \) , but \ (Y \) The lotus leaf is not present, then the score is subtracted \ (10 ^ {100} \) , the game ends.
  3. For \ (Y = X - B \) :
    • If \ (Y = N -. 1 \) , the game ends.
    • If \ (the y-\ neq N - 1 \) , but (y \) \ The lotus leaf is present, the score increased by \ (S_I \) , and this piece of lotus leaf disappears.
    • If \ (Y \ N NEQ -. 1 \) , but \ (Y \) The lotus leaf is not present, then the score is subtracted \ (10 ^ {100} \) , the game ends.
      Q. selected the best \ (A \) , \ (B \) in the case of, get the highest score is how much?

Thinking

We consider, selected \ (A \) , \ (B \) , the routes for the frog:
\ [\} * the eqnarray the begin {0, A, A - B, A + (A - B), 2 (A - B), \ cdots
, K (A - B), A + K (A - B) \ end {eqnarray *} \] we let \ (C = A - B \) : \
[\ the begin {the eqnarray *} 0, A, C,
A + C, 2C, \ cdots, KC, A + KC \ end {eqnarray *} \] clearly: \ (A + KC = N -. 1 \) : \
[\ the begin { eqnarray *} 0, N - 1
- KC, C, N - 1 - (K - 1) C, 2C, \ cdots, KC, N - 1 \ end {eqnarray *} \] then when \ (K \) , \ (C \) to determine when walking routes have been identified.
And there is a proviso \ (KC <N \) , then clearly the enumeration \ (K \) , \ (C \) is \ (O (nlogn) \) a.
And we found that when we fix \ (C \), Is incremented \ (K \) when the change in routes is this:
\ [\ the begin {the eqnarray *} && 0, N -. 1 \\ && 0, N -. 1 - C, C, N -. 1 \\ && 0, N - 1 - 2C, C,
n - 1 - C, 2C, N - 1 \\ \ end {eqnarray *} \] increments the \ (N - 1 - KC \ ) and \ (KC \) , these two points, only need to add just fine, and pay attention to whether the judge went on repeating the point.

Code

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define N 100010
int n;
ll s[N];
int used[N];

int main() {
    while (scanf("%d", &n) != EOF) {
        for (int i = 0; i < n; ++i) {
            scanf("%lld", s + i);
        }
        memset(used, 0, sizeof used); 
        ll res = 0;
        for (int C = 1; C <= n; ++C) {
            ll tmp = 0; 
            for (int k = 1; 1ll * k * C < n; ++k) {     
                int a = k * C;
                int b = n - 1 - k * C;
                int A = b, B = b - C;
                if (A <= 0 || B <= 0) break;
                if (a < 0 || a >= n || b < 0 || b >= n || a == b) break; 
                if (used[a] == C || used[b] == C) {
                    break;
                }
                used[a] = C;
                used[b] = C;
                tmp += s[a];
                tmp += s[b];
                res = max(res, tmp);
            }
        }
        printf("%lld\n", res);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Dup4/p/10929996.html