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

原创 2016年06月01日 09:53:17

题目链接:点击打开链接

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Codeforces Round #347 (Div. 2) B Rebus (贪心构造)

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

Codeforces Round #347 (Div. 2)-B. Rebus(模拟)

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

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

You are given a rebus of form ? + ? - ? + ? = n, consisting of only question marks, separated by ari...

Codeforces Round #347 (Div. 2) B. Rebus (codeforces 664b) (构造)

题目链接:点这里!!!! 题意:给你一个式子:? + ? - ? + ? ... + ? = n, 问你能不能构成这个等式,将?换成[1,n]中任意中的一个数,能构成就输出任意满足条件的解。 比如...

CF #347 (Div.2) B. Rebus(贪心)

题目链接: CF #347 (Div.2) B. Rebus 题意: 给一串只含加号和减号的运算式,以及运算结果为n,运算数全是’?’,问能否用不大于n的正整数代替所有的’?’,使得等式成立?如...
  • Ramay7
  • Ramay7
  • 2016年04月17日 09:21
  • 267

Codeforces Round #347 (Div. 1) B. International Olympiad

International Abbreviation Olympiad takes place annually starting from 1989. Each year the competiti...

Codeforces Round #365 (Div. 2) A(暴力) B(数学技巧) C(二分)D(线段树+离散)E(乘除法DP+约数分解+map映射)

传送门:A. Mishka and Game 暴力记录两人赢的次数,最后将次数再比较一次即可 #include using namespace std; int n; int main()...

Codeforces Round #365 (Div. 2) 703B Mishka and trip 水题

Mishka and trip time limit per test 1 second memory limit per test 256 megabytes in...
  • kyoma
  • kyoma
  • 2016年08月05日 12:59
  • 267

Codeforces Round #340 (Div. 2)-B. Chocolate(组合数学的乘法计数)

B. Chocolate time limit per test 1 second memory limit per test 256 megabytes input...

Codeforces Round 280 Div2 B Vanya and Lanterns(binary serch)

B. Vanya and Lanterns time limit per test 1 second memory limit per test 256 megabytes ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #347 (Div. 2) B. Rebus
举报原因:
原因补充:

(最多只允许输入30个字)