• 时间在说话


    第一题A,比赛的时候看过题率百分之3没敢动,然后花了一早上一下午的时间竟然搞出来了,真是奇迹。

    首先()是这种摆放的可以消除,所以剩下的情况如果有右括号的就一定在左括号左边,然后我们就可以分两种情况,一种是左括号多的,一种是右括号多的,左括号多的肯定要先输出,这样才能抵消后面的右括号,对左括号多的排序,右括号少的优先,因为后面右括号大的前面多余的左括号可以和他抵消,对右括号多的排序,左括号大的优先,最前面的右括号可以和之前剩下的左括号抵消,然后接下来的左括号越多,就不怕右括号过剩,左括号剩的再和后面的抵消。

    Policeman Anatoliy again monitors a lair of unorganized criminal group spreading prohibited Asian drawings. Presently the criminals are also sharing wireless Internet which can be used anonymously by whoever wants to. The lair still has only one entrance, which is also an exit. When someone enters into the lair, Anatoliy writes an opening round bracket in his notepad, and when someone comes out, he writes a closing round bracket.

    Anatoliy decided to refrain from eating donuts in order not to spoil the records just like the previous time, but nevertheless, when he tore a sheet out of a notepad to file it to a criminal case, accidentally tore it into pieces. He doesn't want his boss to shout on him, so he must restore his records by connecting pieces in the right order. It's good for him that the layout of notepad sheets allows to determine where top and bottom sides of these pieces are. Anatoliy ensured that the lair of criminals was empty before he started the surveillance and after he ended it.

    Input

    The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of pieces of paper with opening and closing round brackets written on them.

    The next n lines contain only characters «(» and «)» — these are the strings written on pieces. The total number of brackets does not exceed 2·105.

    Output

    If the given pieces can be connected so that the resulting string does not contradict the statement, output in the first line «YES» without quotes. In the second line output n integers separated by spaces — the numbers of pieces in the order they must be connected. The pieces are numbered from one in the order they are mentioned in the input.

    If Anatoliy messed up something and the given pieces cannot form the correct record, output in the only line «NO» without quotes.

    Examples

    Input
    3
    (
    ()
    )
    Output
    YES
    1 2 3
    Input
    3
    )))
    ((
    (
    Output
    YES
    2 3 1
    Input
    3
    )))
    ((
    )
    Output
    NO
    #include <iostream>
    #include <cstdio>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    char s[200005];
    int len[200005];
    struct node
    {
        int l;
        int r;
        int id;
    }a[200005],b[200005],c[200005];
    bool cmp1(node x,node y){
        return x.l>y.l;
    }
    bool cmp2(node x,node y)
    {
        return x.r<y.r;
    }
    int main()
    {
    int n,i,j,z=1,x=1;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%s",s+1);
        len[i]=strlen(s+1);
        a[i].l=0;
        a[i].r=0;
        a[i].id=i;
        for(j=1;j<=len[i];j++)
        {
            if(s[j]=='(')
                a[i].l++;
            else
            {
                if(a[i].l)
                {
                    a[i].l--;
                }
                else
                    a[i].r++;
            }
        }
            if(a[i].l>a[i].r)
            b[z++]=a[i];
            else
            c[x++]=a[i];
    }
      sort(b+1,b+z,cmp2);
      sort(c+1,c+x,cmp1);
      int ans=0,flag=0;
      for(i=1;i<z;i++)
      {
          ans-=b[i].r;
          if(ans<0)
          {
              flag=1;
              break;
          }
          ans+=b[i].l;
      }
      for(i=1;i<x;i++)
      {
          ans-=c[i].r;
          if(ans<0)
          {
              flag=1;
              break;
          }
          ans+=c[i].l;
      }
      if(ans!=0)
        flag=1;
        if(flag)
            printf("NO
    ");
        else
        {
            printf("YES
    ");
            for(i=1;i<z;i++)
            {
                if(i!=1)
                    cout<<" ";
                    cout<<b[i].id;
            }
            for(i=1;i<x;i++)
            {
               cout<<" "<<c[i].id;
            }
        }
        return 0;
    }

    下一道是B,当时我做的时候一直想用substr,但是后面又突然想到substr只能返回第一个指向它子串的位置,后面的执行起来比较困难,然后队友过了,他们觉得还觉得这道题特简单,呜呜。

    然后就是如果没有happiness的时候要考虑ahappiness的情况,后来补的时候没考虑就一直卡在第29个样例

    #include <iostream>
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    const int maxn=2e5+10;
    char s[maxn];
    char t[maxn]= {0,'h','a','p','p','i','n','e','s','s'};
    int len;
    int add(int k)
    {
        for(int i=1; i<=9; i++)
        {
            if(s[k+i-1]!=t[i])
                return 0;
        }
        return 1;
    }
    int find()
    {
        for(int i=1; i<=len; i++)
        {
            if(add(i))
                return i;
        }
        return 0;
    }
    
    int main()
    {
        scanf("%s",s+1);
        len=strlen(s+1);
        int t1=find();
        if(!t1)
        {
            swap(s[1],s[2]);
            int kepa=find();
            if(!kepa)
                printf("YES
    1 2
    ");
            else printf("YES
    1 3
    ");
        }
        else
        {
            s[t1]=1;
            int t2=find();
            if(!t2) printf("YES
    %d %d
    ",t1,t1+1);
            else
            {
                s[t2]=1;
                int t3=find();
                if(!t3) printf("YES
    %d %d
    ",t1,t2+1);
                else printf("NO
    ");
            }
        }
        return 0;
    }

    这道题是codeforces上的,好不容易做出来了,第二天起来被hack了,还卡他第46个数据,我的做法绕了半天把我自己都绕昏了,在网上看到用差分数组的,感觉比较好,跟大家分享一下

    B. Segment Occurrences
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given two strings ss and tt, both consisting only of lowercase Latin letters.

    The substring s[l..r]s[l..r] is the string which is obtained by taking characters sl,sl+1,,srsl,sl+1,…,sr without changing the order.

    Each of the occurrences of string aa in a string bb is a position ii (1i|b||a|+11≤i≤|b|−|a|+1) such that b[i..i+|a|1]=ab[i..i+|a|−1]=a (|a||a| is the length of string aa).

    You are asked qq queries: for the ii-th query you are required to calculate the number of occurrences of string tt in a substring s[li..ri]s[li..ri].

    Input

    The first line contains three integer numbers nn, mm and qq (1n,m1031≤n,m≤103, 1q1051≤q≤105) — the length of string ss, the length of string ttand the number of queries, respectively.

    The second line is a string ss (|s|=n|s|=n), consisting only of lowercase Latin letters.

    The third line is a string tt (|t|=m|t|=m), consisting only of lowercase Latin letters.

    Each of the next qq lines contains two integer numbers lili and riri (1lirin1≤li≤ri≤n) — the arguments for the ii-th query.

    Output

    Print qq lines — the ii-th line should contain the answer to the ii-th query, that is the number of occurrences of string tt in a substring s[li..ri]s[li..ri].

    Examples
    input
    Copy
    10 3 4
    codeforces
    for
    1 3
    3 10
    5 6
    5 7
    output
    Copy
    0
    1
    0
    1
    input
    Copy
    15 2 3
    abacabadabacaba
    ba
    1 15
    3 4
    2 14
    output
    Copy
    4
    0
    3
    input
    Copy
    3 5 2
    aaa
    baaab
    1 3
    1 1
    output
    Copy
    0
    0
    Note

    In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.

    #include <iostream>
    #include <map>
    #include <set>
    #include <string>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    using namespace std;
    char s1[1010],s2[1010];
    int b[1010],sum[1010];
    int main()
    {
        int n,m,q,i,l,r,j,k,t;
        cin>>n>>m>>q;
       scanf("%s",s1+1);
       scanf("%s",s2+1);
       for(i=1;i<=n-m+1;i++)
       {
           int flag=0;
           for(k=1,j=i;k<=m;j++,k++)
           {
               if(s2[k]!=s1[j])
               {
                   flag=1;
                   break;
               }
           }
           if(!flag)
            b[i]=1;
       }
       for(i=1;i<=n;i++)
       {
           sum[i]=sum[i-1]+b[i];
       }
       while(q--)
       {
            scanf("%d%d",&l,&r);
            r=r-m+1;
            if(r<l)
                printf("0
    ");
            else
                printf("%d
    ",sum[r]-sum[l-1]);
       }
        return 0;
    }

    这道题贪心做,没想到那去,每次至少移动一个单位的距离,至多移动n-1个单位的距离,所以要想完成上述要求每次决策前后一定要满足条件: 
    k <= s && k*(n-1) >= s

    假设当前决策为移动x单位的距离,所以要满足下述条件: 
    (k-1 <= s-x) && (x <= n-1)

    得:max(x) = min( (s - k + 1) , (n-1) ) 
    因此我们的决策为:每次移动的大小为 s与k的差值 和 n-1 中的较小值,使得s和k尽快的相等。

    There are nn houses in a row. They are numbered from 11 to nn in order from left to right. Initially you are in the house 11.

    You have to perform kk moves to other house. In one move you go from your current house to some other house. You can't stay where you are (i.e., in each move the new house differs from the current house). If you go from the house xx to the house yy, the total distance you walked increases by |xy||x−y| units of distance, where |a||a| is the absolute value of aa. It is possible to visit the same house multiple times (but you can't visit the same house in sequence).

    Your goal is to walk exactly ss units of distance in total.

    If it is impossible, print "NO". Otherwise print "YES" and any of the ways to do that. Remember that you should do exactly kk moves.

    Input

    The first line of the input contains three integers nn, kk, ss (2n1092≤n≤109, 1k21051≤k≤2⋅105, 1s10181≤s≤1018) — the number of houses, the number of moves and the total distance you want to walk.

    Output

    If you cannot perform kk moves with total walking distance equal to ss, print "NO".

    Otherwise print "YES" on the first line and then print exactly kk integers hihi (1hin1≤hi≤n) on the second line, where hihi is the house you visit on the ii-th move.

    For each jj from 11 to k1k−1 the following condition should be satisfied: hjhj+1hj≠hj+1. Also h11h1≠1 should be satisfied.

    Examples
    input
    Copy
    10 2 15
    output
    Copy
    YES
    10 4
    input
    Copy
    10 9 45
    output
    Copy
    YES
    10 1 10 1 2 1 2 1 6
    input
    Copy
    10 9 81
    output
    Copy
    YES
    10 1 10 1 10 1 10 1 10
    input
    Copy
    10 9 82
    output
    Copy
    NO
    #include <bits/stdc++.h>
    
    using namespace std;
    
    int main(){
        long long n, k, s;
        cin >> n >> k >> s;
        if(s<k || s>(n-1)*k){
            cout << "NO" << endl;
            return 0;
        }
        cout << "YES" << endl;
        int st=1;
        while(k--){
            int rm=min(s-k, n-1);
            s-=rm;
            if(st+rm<=n) st+=rm;
            else st-=rm;
            cout << st << " ";
    
        }
        cout << endl;
    }

    今天一个大神送了我八个字:循序渐进,日益精进。

    循序渐进是一种大智慧,日益精进是一种行为准则,也是一种精神。

    二者缺一不可。

    愿诸君共勉。

  • 相关阅读:
    行为型模式之备忘录模式
    行为型模式之中介者模式
    行为型模式之迭代器模式
    行为型模式之解释器模式
    行为型模式之命令模式
    行为型模式之职责链模式
    结构型模式之代理模式
    结构型模式之享元模式
    进入Linux的新世界
    302转向与网址劫持(转)
  • 原文地址:https://www.cnblogs.com/kepa/p/9427671.html
Copyright © 2020-2023  润新知