zoukankan      html  css  js  c++  java
  • 【规律】Parentheses

    Parentheses

    题目描述

    Dave loves strings consisting only of '(' and ')'. Especially, he is interested in balanced strings. Any balanced strings can be constructed using the following rules:

    A string "()" is balanced.
    Concatenation of two balanced strings are balanced.
    If T is a balanced string, concatenation of '(', T, and ')' in this order is balanced. For example, "()()" and "(()())" are balanced strings. ")(" and ")()(()" are not balanced strings.
    Dave has a string consisting only of '(' and ')'. It satises the followings:

    You can make it balanced by swapping adjacent characters exactly A times.
    For any non-negative integer B (B<A), you cannot make it balanced by B swaps of adjacent characters.
    It is the shortest of all strings satisfying the above conditions.
    Your task is to compute Dave's string. If there are multiple candidates, output the minimum in lexicographic order. As is the case with ASCII, '(' is less than ')'.

    输入

    The input consists of a single test case, which contains an integer A (1≤A≤109).

    输出

    Output Dave's string in one line. If there are multiple candidates, output the minimum in lexicographic order.

    样例输入

    1
    

    样例输出

    )(
    

    提示

    There are infinitely many strings which can be balanced by only one swap. Dave's string is the shortest of them.


    【题意】

      请大家找到一个通过交换两个位置的操作,经过n次这样操作,能把括号序列变成合法。

      首先保证最短,其次保证字典序最小。

    【题解】

      1:  ")("

      3:  "))(("

      6:  ")))((("

      10:  "))))(((("

      2:  ")()("

      4:  "))()(("

      5:  ")())(("

      

    根据上面的情况找出的规律是:

      等差数列的位置必定是  " )))((("这样的,但是如果不是等差数列则需要在某个位置上交换一下。

      “)())((”

      "))()(("


     1 #pragma GCC optimize(2)
     2 #include <bits/stdc++.h>
     3 using namespace std;
     4 typedef long long ll;
     5 const ll mod = 998244353;
     6 typedef pair<ll,ll> pii;
     7 inline ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();return x*f;}
     8  
     9 const int N=1e7+10;
    10  
    11  
    12 int main()
    13 {
    14     ll n;
    15     scanf("%lld",&n);
    16     int pos = sqrt(2*n);
    17     while(pos*(pos+1) > 2*n) pos--;
    18     //cout<<pos<<endl;
    19     if(pos*(pos+1)/2 == n){
    20         for(int i=1;i<=pos;i++) printf(")");
    21         for(int i=1;i<=pos;i++) printf("(");
    22         printf("
    ");
    23         //main();
    24         return 0;
    25     }
    26     int dis = n - pos*(pos+1)/2;
    27     //cout<<dis<<endl;
    28  
    29     for(int i=1;i<=dis;i++) printf(")");
    30     printf("(");
    31     for(int i=dis;i<=pos;i++) printf(")");
    32     for(int i=1;i<=pos;i++) printf("(");
    33     printf("
    ");
    34     //main();
    35     return 0;
    36 }
    37  
    View Code
  • 相关阅读:
    MySQL 对于千万级的大表要怎么优化?
    mysql数据库优化总结
    php 正则表达式怎么匹配标签里面的style?
    php一行代码获取本周一,本周日,上周一,上周日,本月一日,本月最后一日,上月一日,上月最后一日日期
    PHP过滤常用标签的正则表达式
    php冒泡排序
    PHP中__FUNCTION__与__METHOD__的区别
    关于PHP程序员技术职业生涯规划
    初识DSP
    MATLAB图像处理基础
  • 原文地址:https://www.cnblogs.com/Osea/p/11455852.html
Copyright © 2011-2022 走看看