Good Bye 2015 F - New Year and Cleaning

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Good Bye 2015 F - New Year and Cleaning相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/can' title='can'>can   print   for   https       define   lse   def   oid   

F - New Year and Cleaning

杩欓绠€鐩存槸涓у績鐥呯媯鎶樼(鐜嬨€傘€?/p>

鎬濊矾锛氬鏄撴兂鍒拌繖鏍蜂竴涓浆鎹紝鎶婃暣涓煩褰竴璧风Щ鍔紝鐭╁舰绉诲嚭鍘荤殑鏃跺€欑浉褰撲簬涓€琛屾垨鑰呬竴鍒椼€?/p>

涓轰簡浼樺寲鎵惧埌涓嬩竴涓秷鍘荤殑鐐癸紝鎴戝厛鎶婂師鏁扮粍鎵╁ぇ涓ゅ€嶏紝鐢ㄤ簡st琛ㄥ姞浜屽垎鍘绘壘锛岀劧鍚庡氨MLE锛?鎴戝張鎹簡

绾挎鏍慣LE锛屾渶鍚庝笉鎶婃暟缁勬墿澶т袱鍊峉T琛?浜屽垎杩囩殑銆傘€?/p>

 

姣忔娑堝幓鐨勭偣閮芥槸涓嶅彉鐨勶紝鎵€浠ュ彲浠ュ仛鍒扮嚎鎬у鏉傚害銆?/p>

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std;

const int N = 5e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

int k, n, m, X[N], Y[N], Log[N];
char s[N];

struct ST {
    int dp[N][20],ty;
    void build(int n, int b[], int _ty) {
        ty = _ty;
        for(int i = 1; i <= n; i++) dp[i][0] = ty * b[i];
        for(int j = 1; j <= Log[n]; j++)
            for(int i = 1; i+(1<<j)-1 <= n; i++)
                dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
    }
    int query(int x, int y) {
        int k = Log[y - x + 1];
        return ty * max(dp[x][k], dp[y-(1<<k)+1][k]);
    }
} mxx, mnx, mxy, mny;

void walk(int &x, int &y, char c) {
    if(c == 鈥?/span>R鈥?/span>) y++;
    else if(c == 鈥?/span>L鈥?/span>) y--;
    else if(c == 鈥?/span>U鈥?/span>) x--;
    else x++;
}

bool check(int l, int r, int x, int y, int lenr, int lenc) {
    int mxX = mxx.query(l, r) - X[l-1];
    int mnX = mnx.query(l, r) - X[l-1];
    int mxY = mxy.query(l, r) - Y[l-1];
    int mnY = mny.query(l, r) - Y[l-1];
    if(x+mxX+lenr > n || x+mnX < 0 || y+mxY+lenc > m || y+mnY < 0) return true;
    return false;
}

int main() {
    for(int i = -(Log[0]=-1); i < N; i++)
        Log[i] = Log[i - 1] + ((i & (i - 1)) == 0);

    scanf("%d%d%d%s", &k, &n, &m, s + 1);
    int x = 0, y = 0, L = 0, R = 0, D = 0, U = 0, p = 0;
    for(int i = 1; i <= k; i++) {
        walk(x, y, s[i]);
        L = min(L, y); R = max(R, y);
        U = min(U, x); D = max(D, x);
    }
    if(!x && !y) {
        if(R - L + 1 <= m && D - U + 1 <= n) {
            puts("-1");
            return 0;
        }
    }
    x = 0, y = 0;
    for(int i = 1; i <= k; i++) {
        walk(x, y, s[i]);
        X[i] = x; Y[i] = y;
    }

    mxx.build(k, X, 1);
    mnx.build(k, X, -1);
    mxy.build(k, Y, 1);
    mny.build(k, Y, -1);

    x = 0, y = 0;
    int lenr = n, lenc = m;
    LL ans = 0, t = 0;
    while(lenr && lenc) {
        int l = p + 1, r = min(k, p + k), pos = -1;
        while(l <= r) {
            int mid = l + r >> 1;
            if(check(p + 1, mid, x, y, lenr, lenc)) r = mid - 1, pos = mid;
            else l = mid + 1;
        }

        if(pos != -1) {
            x += X[pos] - X[p];
            y += Y[pos] - Y[p];
            t = (t + pos - p) % mod;
            p = pos;
            if(s[p] == 鈥?/span>U鈥?/span>) {
                lenr--; ans = (ans + 1ll*t*lenc) % mod;
                x = 0;
            } else if(s[p] == 鈥?/span>D鈥?/span>) {
                lenr--; ans = (ans + 1ll*t*lenc) % mod;
            } else if(s[p] == 鈥?/span>L鈥?/span>) {
                lenc--; ans = (ans + 1ll*t*lenr) % mod;
                y = 0;
            } else {
                lenc--; ans = (ans + 1ll*t*lenr) % mod;
            }
            if(p == k) p = 0;
        } else {
            x += X[k] - X[p];
            y += Y[k] - Y[p];
            t = (t + k - p) % mod;
            p = 0;
        }

    }
    printf("%lld
", ans);
    return 0;
}

/*
*/

 

以上是关于Good Bye 2015 F - New Year and Cleaning的主要内容,如果未能解决你的问题,请参考以下文章

Good Bye 2016 C. New Year and Rating

Codeforces Good Bye 2017 C. New Year and Curling 几何枚举

Good Bye 2014 E - New Year Domino 单调栈+倍增

Codeforces Good Bye 2017 B. New Year and Buggy Bot 枚举全排列模拟

Good Bye 2015 A

Good Bye 2018 A. New Year and the Christmas Ornament