zoukankan      html  css  js  c++  java
  • codeforce 1476D D. Journey 画图 逆推 C

    codeforce 1476D D. Journey 画图 逆推 C
    https://codeforces.com/contest/1476/problem/D
    D. Journey
    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    There are n+1n+1 cities, numbered from 00 to nn. nn roads connect these cities, the ii-th road connects cities i1i−1 and ii (i[1,n]i∈[1,n]).

    Each road has a direction. The directions are given by a string of nn characters such that each character is either L or R. If the ii-th character is L, it means that the ii-th road initially goes from the city ii to the city i1i−1; otherwise it goes from the city i1i−1 to the city ii.

    A traveler would like to visit as many cities of this country as possible. Initially, they will choose some city to start their journey from. Each day, the traveler must go from the city where they currently are to a neighboring city using one of the roads, and they can go along a road only if it is directed in the same direction they are going; i. e., if a road is directed from city ii to the city i+1i+1, it is possible to travel from ii to i+1i+1, but not from i+1i+1 to ii. After the traveler moves to a neighboring city, all roads change their directions to the opposite ones. If the traveler cannot go from their current city to a neighboring city, their journey ends; it is also possible to end the journey whenever the traveler wants to.

    The goal of the traveler is to visit as many different cities as possible (they can visit a city multiple times, but only the first visit is counted). For each city ii, calculate the maximum number of different cities the traveler can visit during exactly one journey if they start in the city ii.

    Input

    The first line contains one integer tt (1t1041≤t≤104) — the number of test cases.

    Each test case consists of two lines. The first line contains one integer nn (1n31051≤n≤3⋅105). The second line contains the string ss consisting of exactly nn characters, each character is either L or R.

    It is guaranteed that the sum of nn over all test cases does not exceed 31053⋅105.

    Output

    For each test case, print n+1n+1 integers. The ii-th integer should be equal to the maximum number of different cities the traveler can visit during one journey if this journey starts in the ii-th city.

    Example
    input
    Copy
    2
    6
    LRRRLL
    3
    LRL
    
    output
    Copy
    1 3 2 3 1 3 2
    1 4 1 4

    分析

    题目给出的背景有点迷,我们设为上下两部分来算

    b 0 1 2 3 4 5 6

    c 0 1 2 3 4 5 6

    由于LRRRLL

    b1链接c0,c2连接b1,这样每次想走某一条边,就只能通过已经连的进行

    接着就是类似并查集的操作,倒着把连接的边数算到前面

    然后再从前往后依次输出并更新

    可惜就差那么几秒

    就写完了

    代码

    https://codeforces.com/contest/1476/submission/105938876

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <math.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <iostream>
    #include <time.h>
    #include <queue>
    #include <list>
    #include <map>
    #include <set>
    #include <vector>
    #include <stack>
    #include <string.h>
    #include <bitset>
    #define sf scanf
    #define pf printf
    #define lf double
    #define p123 printf("123
    ");
    #define pn printf("
    ");
    #define pk printf(" ");
    #define p(n) printf("%d",n);
    #define pln(n) printf("%d
    ",n);
    #define s(n) scanf("%d",&n);
    #define ss(n) scanf("%s",n);
    #define ps(n) printf("%s",n);
    #define sld(n) scanf("%lld",&n);
    #define pld(n) printf("%lld",n);
    #define slf(n) scanf("%lf",&n);
    #define plf(n) printf("%lf",n);
    #define sc(n) scanf("%c",&n);
    #define pc(n) printf("%c",n);
    #define gc getchar();
    #define ll long long
    #define re(n,a) memset(n,a,sizeof(n));
    #define len(a) strlen(a)
    #define eps 1e-13
    #define zero(x) (((x) > 0? (x):(-x)) < eps)
    using namespace std;
    char a[300005];
    int b[300005];
    int c[300005];
    int bv[300005];
    int cv[300005];
    int main(){
        int t;
        s(t)
        int n;
        while(t --){
            s(n) gc;
            for(int i = 0; i <= n;i++){
                b[i] = i;
                c[i] = i;
                bv[i] = 1;
                cv[i] = 1;
            }
            for(int i = 1; i <= n; i ++){
                a[i] = gc;
                if(a[i] == 'L'){
                    b[i] = i-1;
                }else if(a[i] == 'R'){
                    c[i] = i-1;
                }
            }
            for(int i = n; i >= 1; i --){
                if(b[i] == i){
                    cv[i] ++;
                    bv[i-1] = cv[i];
                }else{
                    bv[i] ++;
                    cv[i-1] = bv[i];
                }
            }
            /*for(int i = 0; i <= n; i ++){
                p(b[i]) pk
            }
            pn
            for(int i = 0; i <= n; i ++){
                p(c[i]) pk
            }
            pn*/
            for(int i = 0; i <= n-1; i ++){
                p(bv[i]) pk
                if(b[i+1] == i){
                    bv[i+1] = cv[i];
                }else{
                   cv[i+1] = bv[i];
                }
            }
            p(bv[n]) pk pn
        }
        return 0;
    }
    作者:kidgzz
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    初识Spring框架IOC属性注入
    JSP:在本地获取图片后立即展示选择的图片
    JavaWeb手机短信实现前台利用JS获取随机验证码,倒计时效果
    通过form表单上传文件,后台接收的方法
    封装数据库方法
    JavaWeb无限级分销结构分析
    JavaWeb忘记密码后通过邮箱进入修改密码的界面
    JavaWeb通过快递单号展示物流信息转JSON显示(servlet)
    markdown 语法测试
    example数据库
  • 原文地址:https://www.cnblogs.com/Kidgzz/p/14347799.html
Copyright © 2011-2022 走看看