codeforce 3C-3D(Greedy)

3C看起来简单,还是wa的好多次,许多细节没有考虑到。。。

3D没有思路,看了下大牛的解法,感觉比较清楚,写了出来后,最后判断-1时又卡住了,回去看了看大牛,如果leftcnt!=rightcnt及为不可能匹配

#include<iostream>
#include<cmath>
using namespace std;

int cntx=0,cnt0=0,cntdot=0;
int aryx[10][2],ary0[10][2];
char chess[3][3];
int Win(char);

int main()
{


    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        {
            cin>>chess[i][j];
            if(chess[i][j]=='X')
            {
                aryx[cntx][0]=i;
                aryx[cntx][1]=j;
                cntx++;
            }
            if(chess[i][j]=='0')
            {
                ary0[cnt0][0]=i;
                ary0[cnt0][1]=j;
                cnt0++;
            }

            if(chess[i][j]=='.')
            {
                cntdot++;
            }
        }
        if(cntx-cnt0<0||cntx-cnt0>1)
        {
            cout<<"illegal"<<endl;
            return 0;
        }
        if(cntx<3)
        {
            if(cntx>cnt0)
            {
                cout<<"second"<<endl;
                return 0;
            }
            else
            {
                cout<<"first"<<endl;
                return 0;
            }
        }
        if(Win('X')>1||Win('0')>1)
        {
            if(cntdot!=0)
                cout<<"illegal"<<endl;
            else
                cout<<"the first player won"<<endl;
            return 0;
        }
        if(Win('X')==1&&Win('0')==1)
        {
            cout<<"illegal"<<endl;
            return 0;
        }
        if(Win('X')==1&&Win('0')==0)
        {
            if(cntx>cnt0)
            cout<<"the first player won"<<endl;
            else
                cout<<"illegal"<<endl;
            return 0;
        }
        if(Win('X')==0&&Win('0')==1)
        {
            if(cntx==cnt0)
            cout<<"the second player won"<<endl;
            else
            cout<<"illegal"<<endl;
            return 0;
        }
        if(Win('X')==0&&Win('0')==0)
        {
            if(cntdot==0)
            {
                cout<<"draw"<<endl;
                return 0;
            }
            else
            {
                if(cntdot>=2&&cntdot<=4)
                {
                    if(cntx>cnt0)
                    {
                        cout<<"second"<<endl;
                        return 0;
                    }
                    if(cntx==cnt0)
                    {
                        cout<<"first"<<endl;
                        return 0;
                    }
                }

                if(cntdot==1)
                {
                    cout<<"first"<<endl;
                    /*int dotx,doty;
                    for(int i=0;i<3;i++)
                        for(int j=0;j<3;j++)
                        {
                            if(chess[i][j]=='.')
                            {
                                dotx=i;
                                doty=j;
                            }
                        }
                    aryx[cntx][0]=dotx;
                    aryx[cntx][1]=doty;
                    cntx++;
                    chess[dotx][doty]='X';
                    if(Win('X')==1)
                    {
                        cout<<"he first player won"<<endl;
                        return 0;
                    }
                    else
                    {
                        cout<<"draw"<<endl;
                        return 0;
                    }*/
                }
            }
        }

            
    return 0;
}
int Win(char ch)
{
    int xary[3]={0},yary[3]={0};
    for(int i=0;i<(ch=='X'?cntx:cnt0);i++)
    {
        if(ch=='X')
        {
        xary[aryx[i][0]]++;
        yary[aryx[i][1]]++;
        }
        else
        {
            xary[ary0[i][0]]++;
            yary[ary0[i][1]]++;
        }
    }
    int t=0;
    for(int i=0;i<3;i++)
    {
        if(xary[i]==3)
            t++;
        if(yary[i]==3)
            t++;
    }
    if(chess[0][0]==ch&&chess[1][1]==ch&&chess[2][2]==ch)
        t++;
    if(chess[0][2]==ch&&chess[1][1]==ch&&chess[2][0]==ch)
        t++;
    return t;

}
#include<iostream>
#include<stack>
#include<queue>
using namespace std;

char buf[100002];
int weight[100002][3];
int length;
priority_queue<pair<int,int>>pq;
int main()
{
    cin>>buf;
    length=strlen(buf);
    
    int cnt=0;
    long long miniweight=0;
    for(int i=0;i<length;i++)
    {
        if(buf[i]=='?')
        {
            weight[cnt][0]=i;
            cin>>weight[cnt][1]>>weight[cnt][2];
            buf[i]=')';
            miniweight+=weight[cnt][2];
            cnt++;
        }
    }
    int leftcnt=0,rightcnt=0;
    int m=0;
    int n=0;
    while(m<length)
    {
        if(buf[m]=='(')
            leftcnt++;
        else
            rightcnt++;
        if(leftcnt<rightcnt)
        {
            
            while(weight[n][0]<=m&&n<cnt)
            {
                pq.push(make_pair(weight[n][2]-weight[n][1],weight[n][0]));
                n++;
            }
            if(pq.empty())
            {
                break;
            }
            pair<int,int> p=pq.top();
            pq.pop();
            miniweight-=p.first;
            buf[p.second]='(';
            leftcnt++;
            rightcnt--;

        }
        m++;
    }

    if(leftcnt!=rightcnt)
        cout<<-1<<endl;
    else
    {
        cout<<miniweight<<endl;
        cout<<buf<<endl;
    }

    return 0;

}
原文地址:https://www.cnblogs.com/cavehubiao/p/3427903.html