Codeforces Round #347 (Div. 2) B

【题意】给一个式子,让你选[1,n]中间的数把问号替换掉,使这个式子成立;

【分析】贪心乱搞!

【AC代码】

#include <queue>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10010;
char s[maxn];
int answer[maxn];
string solve(int x)
{
    string temp="";
    while(x)
    {
        temp+=(x%10+'0');
        x/=10;
    }
    reverse(temp.begin(),temp.end());
    return temp;
}
int main()
{
    int ans=0;
    gets(s);
//    scanf("%s",s);
    int len = strlen(s);
    int p1=1,p2=0;
    int i,j,sum=0;
    for(i=0; i<len&&s[i]!='='; i++){
        if(s[i]=='+')p1++;
        if(s[i]=='-')p2++;
    }
    for(j=i+1; j<len; j++){
        if(isdigit(s[j])) ans = ans*10 + s[j]-'0';
    }
    int tmp = p1-p2;
    //cout<<tmp<<endl;
    if(tmp>ans)
    {
        int cnt=0;
        int xx=tmp-ans;
        for(i=0; i<len&&s[i]!='='; i++){
            if(s[i]=='+'||i==0)answer[cnt++] = 1;
            else if(s[i]=='-')
            {
                if(xx>(ans-1))answer[cnt++] = ans,xx-=(ans-1);
                else if(xx!=0&&xx<=(ans-1))answer[cnt++] = 1+xx,xx=0;
                else answer[cnt++]=1;
            }
        }
        if(xx!=0)
        {
            puts("Impossible");
            return 0;
        }
        printf("Possible\n");
        int t=0;
        string ss="";
        for(int i=0; i<len; i++)
        {
            if(s[i]=='?') ss+=solve(answer[t++]);
            else ss+=s[i];
        }
        cout<<ss<<endl;
    }
    else
    {
        int cnt=0;
        int xx=ans-tmp;
        for(i=0; i<len&&s[i]!='='; i++){
            if(s[i]=='-')answer[cnt++] = 1;
            else if(i==0||s[i]=='+')
            {
                if(xx>(ans-1))answer[cnt++] = ans,xx-=(ans-1);
                else if(xx!=0&&xx<=(ans-1))answer[cnt++] = 1+xx,xx=0;
                else answer[cnt++]=1;
            }
        }
        if(xx!=0)
        {
            puts("Impossible");
            return 0;
        }
        printf("Possible\n");
        int t=0;
        string ss="";
        for(int i=0; i<len; i++)
        {
            if(s[i]=='?') ss+=solve(answer[t++]);
            else ss+=s[i];
        }
        cout<<ss<<endl;
    }
    return 0;
}




你可能感兴趣的:(Codeforces Round #347 (Div. 2) B)