• codeforce 5A-5D(dp)


    5A,5B直接实现,没有什么难度,主要是要读懂题,例如5B...

    5C dp可惜我又没想出来,好伤感...菜鸡的春天在哪里啊...

    看了下官方的tutorial,讲的很清楚,两个数组d[n],c[n]

    d[i],记录i位置‘)’对应的‘(’位置,当然前提是要第i个是‘(’,如果不存在则d[i]=-1;

    c[i],记录以i处的‘)’为结尾的最长子串的第一个‘(’的位置,如果不存在则-1;

    具体实现要维护一个栈,从0开始,如果是‘(,入栈所在位置;如果是')',使得c[i]=d[i]=stack.top();如果stack为空c[i]=d[i]=-1;

    同时,判断d[i]-1处是不是')',如果是,那么判断c[d[i]-1]是不是-1,不是的话,c[i]=c[d[i]-1];

    大概这样子,最后i-c[i]+1的最大值即为最大,记得-1处要舍去啊...

    我解释的不够清楚,就把tutorial 也贴出来

    First of all, for each closing bracket in our string let's define 2 values:

    • d[j] = position of corresponding open bracket, or -1 if closing bracket doesn't belong to any regular bracket sequence.
    •  c[j] = position of earliest opening bracket, such that substring s(c[j], j) (both boundaries are inclusive) is a regular bracket sequence. Let's consider c[j] to be -1 if closing bracket doesn't belong to any regular bracket sequence.

    It can be seen, that c[j] defines the beginning position of the longest regular bracket sequence, which will end in position j. So, having c[j] answer for the problem can be easily calculated.

    Both d[j] and c[j] can be found with following algorithm, which uses stack.

    1. Iterate through the characters of the string.
    2. If current character is opening bracket, put its position into the stack.
    3. If current character is closing bracket, there are 2 subcases:
      • Stack is empty - this means that current closing bracket doesn't have corresponding open one. Hence, both d[j] and c[j] are equal to -1.
      • Stack is not empty - we will have position of the corresponding open bracket on the top of the stack - let's put it to d[j] and remove this position from the stack. Now it is obvious, that c[j] is equal at least to d[j]. But probably, there is a better value for c[j]. To find this out, we just need to look at the position d[j] - 1. If there is a closing bracket at this position, and c[d[j] - 1] is not -1, than we have 2 regular bracket sequences s(c[d[j] - 1], d[j] - 1) and s(d[j], j), which can be concatenated into one larger regular bracket sequence. So we put c[j] to be c[d[j] - 1] for this case.

    5D 纯数学问题,满满算,细心点.

    5A
    #include<iostream>
    #include<list>
    #include<string>
    using namespace std;
    
    int main()
    {
        list<string>strList;
        int traffic=0;
        string str;
        while(getline(cin,str))
        {
            if(str[0]=='+')
                strList.push_back(str.substr(1));
            if(str[0]=='-')
            {
                for(list<string>::iterator it=strList.begin();it!=strList.end();it++)
                {
                    if(str.substr(1)==*it)
                    {
                        strList.erase(it);
                        break;
                    }
                }
            }
            if(str[0]!='+'&&str[0]!='-')
            {
                int i=str.find(':');
                string msg=str.substr(i+1);
                traffic+=strList.size()*msg.length();
    
            }
        }
        cout<<traffic<<endl;
        return 0;
    }
    5B
    #include<iostream>
    #include<string>
    #include<vector>
    using namespace std;
    int main()
    {
        vector<string>vs;
        string str;
        int maxlen=0;
        while(getline(cin,str))
        {A
            if(str.length()>maxlen)
                maxlen=str.length();
            vs.push_back(str);
        }
        for(int i=0;i<maxlen+2;i++)
        {
            cout<<'*';
        }
        cout<<endl;
        int m=0;
        for(int i=0;i<vs.size();i++)
        {
            int l,r,s;
            s=maxlen-vs[i].length();
            if(s%2)
                m++;
            if(m%2==0)
            {
            l=s%2?s/2+1:s/2;
            r=s/2;
            }
            else
            {
                r=s%2?s/2+1:s/2;
                l=s/2;
            }
            cout<<'*';
            for(int j=0;j<l;j++)
                cout<<' ';
            cout<<vs[i];
            for(int j=0;j<r;j++)
                cout<<' ';
            cout<<'*';
            cout<<endl;
    
        }
        for(int i=0;i<maxlen+2;i++)
        {
            cout<<'*';
        }
        cout<<endl;
    
        return 0;
    }
    5C
    #include<iostream>
    #include<fstream>
    #include<stack>
    
    #include<string>
    using namespace std;
    int main()
    {
        stack<int> sta;
        int *c=new int[1000002]();
        int *d=new int[1000002]();
        string str;
        cin>>str;
        for(int i=0;i<str.length();i++)
        {
            if(str[i]=='(')
                sta.push(i);
            else
            {
                if(sta.empty())
                {
                    c[i]=-1;
                    d[i]=-1;
                }
                else
                {
                    d[i]=sta.top();
                    c[i]=sta.top();
                    sta.pop();
                    if(d[i]-1>=0&&str[d[i]-1]==')'&&c[d[i]-1]!=-1)
                    {
                        c[i]=c[d[i]-1];
                    }
                }
            }
        }
    
        int maxlen=0,cnt=0;
        for(int i=0;i<str.length();i++)
        {
            if(c[i]==-1||str[i]=='(')
                continue;
            if(i-c[i]+1>maxlen)
            {
                maxlen=i-c[i]+1;
                cnt=1;
            }
            else
            {
                if(i-c[i]+1==maxlen)
                    cnt++;
            }
    
        }
        if(maxlen==0)
            cout<<0<<" "<<1<<endl;
        else
            cout<<maxlen<<" "<<cnt<<endl;
        return 0;
    }
    //int main()
    //{
    //    int maxlen=0,start=-1,maxcnt=0;
    //    stack<char>sc;
    //    ifstream fin("testdata.txt");
    //    string str;
    //    fin>>str;
    //    for(int i=0;i<str.length();)
    //    {
    //        if(str[i]=='(')
    //        {
    //            int left=1,right=0;
    //            int j=i+1;
    //            int nowmax=0;
    //            int index=0;
    //            while(j<str.length())
    //            {
    //                if(str[j]=='(')
    //                {
    //                    left++;
    //                }
    //                else
    //                {
    //                    right++;
    //                }
    //                j++;
    //                if(right>left)
    //                    break;
    //                if(left>right)
    //                {
    //                    if(right==1)
    //                    {
    //                        left--;
    //                        right=0;
    //                        nowmax+=2;
    //                        index=j-1;
    //                    }
    //                    continue;
    //                }
    //                if(left==right)
    //                {
    //                    nowmax+=2;
    //                    left=0;
    //                    right=0;
    //                    index=j-1;
    //                }
    //            }
    //            if(nowmax>maxlen)
    //            {
    //                maxlen=nowmax;
    //                maxcnt=1;
    //            }
    //            else
    //            {
    //                if(nowmax==maxlen)
    //                {
    //                    maxcnt++;
    //                }
    //            }
    //            i=i+(nowmax>1?index+1:1);
    //
    //        }
    //        else
    //        {
    //            i++;
    //        }
    //    }
    //
    //    if(maxlen==0)
    //    {
    //        cout<<maxlen<<" "<<1<<endl;
    //    }
    //    else
    //    {
    //        cout<<maxlen<<" "<<maxcnt<<endl;
    //    }
    //
    //}
    5D
    #include<iostream>
    #include<cmath>
    #include<iomanip>
    using namespace std;
    int main()
    {
    
        int v,a,l,d,w;
        
        cin>>a>>v>>l>>d>>w;
        if(w>=v)
        {
            double t=(double)v/a;
            if(t*t*a/2>=l)
            {
                cout<<setiosflags(ios_base::fixed)<<setprecision(5)<<sqrt(((double)l*2)/a)<<endl;
            }
            else
            {
                double left=l-t*t*a/2;
                cout<<setiosflags(ios_base::fixed)<<setprecision(5)<<left/v+t<<endl;
            }
    
        }
        else
        {
            double t=(double)v/a;
            double t1=(double)w/a;
            if(t1*t1*a/2>=d)
            {
                if(t*t*a/2>=l)
                    cout<<setiosflags(ios_base::fixed)<<setprecision(5)<<sqrt(((double)l*2)/a)<<endl;
                else
                {
                    double left=l-t*t*a/2;
                    cout<<setiosflags(ios_base::fixed)<<setprecision(5)<<left/v+t<<endl;
                }
            }
            else
            {
                double dleft=d-t1*t1*a/2;
                double dt=(double)(v-w)/a;
                double dmid=(w+v)*dt/2;
                if(dleft/2<=dmid)
                {
                    double t2=sqrt((dleft+(double)w*w/a)/a)-(double)w/a;
                    double acet=(double)(v-w)/a;
                    if((v+w)*acet/2>=l-d)
                    {
                        double t3=sqrt((2*l-2*d+(double)w*w/a)/a)-(double)w/a;
                        cout<<setiosflags(ios_base::fixed)<<setprecision(5)<<t1+2*t2+t3<<endl;
                    }
                    else
                    {
                        double t3=(double)(v-w)/a;
                        double t4=(l-d-(v+w)*t3/2)/v;
                        cout<<setiosflags(ios_base::fixed)<<setprecision(5)<<t1+2*t2+t3+t4<<endl;
                    }
    
                }
                else
                {
                    double t2=(double)(v-w)/a*2+(dleft-2*dmid)/v;
                    double acet=(double)(v-w)/a;
                    if((v+w)*acet/2>=l-d)
                    {
                        double t3=sqrt((2*l-2*d+(double)w*w/a)/a)-(double)w/a;
                        cout<<setiosflags(ios_base::fixed)<<setprecision(5)<<t1+t2+t3<<endl;
                    }
                    else
                    {
                        double t3=(double)(v-w)/a;
                        double t4=(l-d-(v+w)*t3/2)/v;
                        cout<<setiosflags(ios_base::fixed)<<setprecision(5)<<t1+t2+t3+t4<<endl;
                    }
    
                }
    
            }
        }
        return 0;
    }
  • 相关阅读:
    iframe嵌套
    fiddler——一款莱斯的抓包工具
    工程代码规范和更新规范
    新项目新工作空间新仓库新setting文件
    quartz-job实现实时或定时发送短信任务
    quartz-job实现定时任务配置
    quartz开源作业调度框架的配置
    Confluence 6 让一个空间可以公众访问
    Confluence 6 管理和恢复空间管理权限
    Confluence 6 指派和撤销空间权限
  • 原文地址:https://www.cnblogs.com/cavehubiao/p/3446707.html
Copyright © 2020-2023  润新知