Codeforces Round #347 (Div. 2) B. Rebus

題目鏈接:點擊打開鏈接

B. Rebus
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a rebus of form ? + ? - ? + ? = n, consisting of only question marks, separated by arithmetic operation '+' and '-', equality and positive integer n. The goal is to replace each question mark with some positive integer from 1 to n, such that equality holds.

Input

The only line of the input contains a rebus. It's guaranteed that it contains no more than 100 question marks, integer n is positive and doesn't exceed 1 000 000, all letters and integers are separated by spaces, arithmetic operations are located only between question marks.

Output

The first line of the output should contain "Possible" (without quotes) if rebus has a solution and "Impossible" (without quotes) otherwise.

If the answer exists, the second line should contain any valid rebus with question marks replaced by integers from 1 to n. Follow the format given in the samples.

Examples
input
? + ? - ? + ? + ? = 42
output
Possible
9 + 13 - 39 + 28 + 31 = 42
input
? - ? = 1
output
Impossible
input
? = 1000000
output
Possible
1000000 = 1000000

 題意: 給你 ? + ? - ? + ? = n,包含多個?和+、-,其中?是1-n的整數,問是否存在這樣的等式,如果存在輸出“Possible"和任意一組,不存在輸出”Impossile"。

思路:個人覺得字符串的輸入是個很頭疼的問題,最終用s[1010][1010]來解決,s[i]就等於"+"或“-”或“=”或“?"或"n",最後一個字符串是n,將它轉換爲數字n,遍歷一個s數組,得到“+”的個數cnt1和“-”的個數cnt2;

     如果cnt1==0&&cnt2==0,意味着等式是? = n,輸出n = n,即可; 

     第一個數肯定是被加起來的,所以cnt1+1個數被加起來,有cnt2個數被減掉,如果(cnt1+1)*n-cnt2*1<n的話肯定無解,同理(cnt1+1)-cnt2*n>n的話也無解;

     反之一定有解,將cnt1+1個數保存在a[1010]中,將cnt2個數保存在b[1010]中,接下來是貪心的做法,先初始化a[1~cnt1+1]=1,b[1~cnt2]=1;

     如果cnt1+1-cnt2>n,證明要減的更多,將他們儘可能均分給每一個減數:

     

cnt3=(cnt1+1-cnt2-n)/cnt2;
            for(h=1;h<=cnt2;h++)
            b[h]+=cnt3;
            cnt3=(cnt1+1-cnt2-n)%cnt2;
            if(cnt3>=1)
            for(h=1;h<=cnt3;h++) b[h]++;
    如果 cnt1+1-cnt2<n,證明要加的更多,同樣的處理方法,這樣可以保證每一個數都大於等於1且不會超過n:

   

  cnt3=(n-(cnt1+1-cnt2))/(cnt1+1);
            for(h=1;h<=cnt1+1;h++)
            a[h]+=cnt3;
            cnt3=n-(cnt1+1-cnt2)-cnt3*(cnt1+1);
            for(h=1;h<=cnt3;h++) a[h]++;
   下面附上AC代碼:這題畢竟卡了很久,凌晨兩點半睡不着又下牀想了這種寫法才過掉的

   

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
    char s[1010][1010];int i=0,j,n=0,cnt1=0,cnt2=0,cnt3=1,a[1010],b[1010];
    while(scanf("%s",s[i])!=EOF)
    {
        if(strcmp(s[i],"?")==0||strcmp(s[i],"+")==0||strcmp(s[i],"-")==0||strcmp(s[i],"=")==0) i++;
        else break;
    }
    for(j=0;j<i;j++)
    {
        if(strcmp(s[j],"+")==0) cnt1++;
        if(strcmp(s[j],"-")==0) cnt2++;
    }
    int len=strlen(s[i]);
    for(j=len-1;j>=0;j--)
    {
        n+=(s[i][j]-'0')*cnt3;
        cnt3*=10;
    }
    //printf("%d %d\n",cnt1,cnt2);
    if(cnt1==0&&cnt2==0) printf("Possible\n%d = %d\n",n,n);
    else if(n+n*cnt1-cnt2<n||1+cnt1-cnt2*n>n) printf("Impossible\n");
    else
    {
        int h;
        printf("Possible\n");
        for(h=1;h<=cnt1+1;h++)
        a[h]=1;
        for(h=1;h<=cnt2;h++)
        b[h]=1;
        if(cnt1+1-cnt2>n)
        {
            cnt3=(cnt1+1-cnt2-n)/cnt2;
            for(h=1;h<=cnt2;h++)
            b[h]+=cnt3;
            cnt3=(cnt1+1-cnt2-n)%cnt2;
            if(cnt3>=1)
            for(h=1;h<=cnt3;h++) b[h]++;
        }
        else if(cnt1+1-cnt2<n)
        {
            cnt3=(n-(cnt1+1-cnt2))/(cnt1+1);
            for(h=1;h<=cnt1+1;h++)
            a[h]+=cnt3;
            cnt3=n-(cnt1+1-cnt2)-cnt3*(cnt1+1);
            for(h=1;h<=cnt3;h++) a[h]++;
        }
        //for(h=1;h<=cnt1+1;h++) printf("%d ",a[h]);
        printf("%d ",a[1]);
        int m,x;m=2;x=1;
        for(h=1;h<i-1;h++)
        {
            if(strcmp(s[h],"+")==0) printf("+ %d ",a[m++]);
            if(strcmp(s[h],"-")==0) printf("- %d ",b[x++]);
        }
        printf("= %d\n",n);

    }
}
//? + ? - ? - ? - ? - ? - ? = 5
//? + ? + ? + ? + ? + ? + ? + ? - ? = 5

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章