P3083 [USACO13OPEN]豪华游船Luxury River Cruise

Posted wsmrxc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P3083 [USACO13OPEN]豪华游船Luxury River Cruise相关的知识,希望对你有一定的参考价值。

瞎模拟.
模拟的时候顺便把每次指令完成后到达的点记下来, 假如之前经过说明遇到了循环, 直接mod就ok.
时间复杂度 (O(NM))

#include <vector>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 1e3 + 10;
const int MAXM = 5e2 + 10;
inline int read()
{
    int x = 0; char ch = getchar();
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
    return x;
}

struct Node
{
    bool vis;
    int l, r;
}node[MAXN];

int N, M, K;
char dir[MAXM];

vector<int> pos;
int main()
{
    // freopen("p3083.in", "r", stdin);
    cin>>N>>M>>K;
    for(int i = 1; i <= N; i++){
        int l = read(), r = read();
        node[i].l = l, node[i].r = r;
    }
    for(int i = 0; i < M; i++)
        scanf(" %c", dir + i);

    int p = 1; bool flag = false;
    pos.push_back(1), node[1].vis = true;
    for(int i = 1; i <= K; i++){
        for(int j = 0; j < M; j++){
            if(dir[j] == 'L') p = node[p].l;
            else p = node[p].r;
        }
        if(node[p].vis) flag = true;
        node[p].vis = true; pos.push_back(p);
        if(flag) break;
    }
    if(!flag) cout<<*pos.rbegin()<<endl;
    else{
        K -= pos.size();
        pos.erase(pos.begin(), find(pos.begin(), pos.end(), *pos.rbegin()) + 1);
        cout<<pos[K % pos.size()]<<endl;
    }
    return 0;
}

以上是关于P3083 [USACO13OPEN]豪华游船Luxury River Cruise的主要内容,如果未能解决你的问题,请参考以下文章

[USACO13OPEN]照片Photo [动态规划 单调队列]

洛谷 P2209 [USACO13OPEN]燃油经济性Fuel Economy

DP专题——[USACO13OPEN]照片Photo

P3084 [USACO13OPEN]照片Photo

P3084 [USACO13OPEN]照片Photo (dp+单调队列优化)

Luugu 3084 [USACO13OPEN]照片Photo