【16.05%】【codeforces 664B】Rebus
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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
【题解】
一开始问号前面如果是加号则为1,如果是减号则为-1;
这个时候,如果总和小于所需的;
则把大于0的数字递增直到这个数字等于n或者已经达到了要求;小于0的则不能动(前面是负号);
如果总和大于所需的;
则把数字小于0的再减少一点(当然也不能小于-n);
这样可以递减总和。
具体的看代码;
如果以上操作都不能满足总和为n则输出无解信息;
一开始的数字初始化很巧妙;
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define LL long long
using namespace std;
const int MAXN = 2000;
char t;
int a[2000] = {0},now = 0,total = 1,n;
void input_LL(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
a[++now] = 1;
while (cin>>t && t!='=')
{
if (t == '+')
a[++now] = 1,total++;
else
if (t== '-')
a[++now] = -1,total--;
}
input_int(n);
for (int i = 1;i <= now;i++)
if (a[i] > 0)
{
while (a[i]<n && total < n)
a[i]++,total++;
}
else
if (a[i] < 0)
{
while (a[i]>-n && total > n)
a[i]--,total--;
}
if (total!=n)
puts("Impossible");
else
{
puts("Possible");
printf("%d ",a[1]);
for (int i =2;i <= now;i++)
printf("%c %d ",a[i]>0?'+':'-',abs(a[i]));
printf("= %d\n",n);
}
return 0;
}
【16.05%】【codeforces 664B】Rebus的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【2018.05.11 智能驾驶/汽车电子】非技术向:关于Simulink和AutoSar的几种观点
最近看到几篇关于Simulink及AutoSar的Blog和Paper,感觉比较有意思,转载备忘之. 1. 看衰Simulink及AutoSar From:Tumiz的技术天地 https://blo ...
- 【2018.05.10 智能驾驶/汽车电子】AutoSar Database-ARXML及Vector Database-DBC的对比
最近使用python-canmatrix对can通信矩阵进行编辑转换时,发现arxml可以很容易转换为dbc,而dbc转arxml却需要费一番周折,需要额外处理添加一些信息. 注意:这里存疑,还是需要 ...
- codeforces 664B B. Rebus(乱搞题)
题目链接: B. Rebus time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 【16.23%】【codeforces 586C】Gennady the Dentist
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 807D】Dynamic Problem Scoring
[题目链接]:http://codeforces.com/contest/807/problem/D [题意] 给出n个人的比赛信息; 5道题 每道题,或是没被解决->用-1表示; 或者给出解题 ...
- 【codeforces 821E】Okabe and El Psy Kongroo
[题目链接]:http://codeforces.com/problemset/problem/821/E [题意] 一开始位于(0,0)的位置; 然后你每次可以往右上,右,右下3走一步; (x+1, ...
- 【81.82%】【codeforces 740B】Alyona and flowers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- js进阶 13-6 jquery动画效果相关常用函数有哪些
js进阶 13-6 jquery动画效果相关常用函数有哪些 一.总结 一句话总结:animate(),stop(),finish(),delat()四个. 1.stop()方法的基本用法是什么(sto ...
- jmeter--元件的作用域与执行顺序
1.元件的作用域 JMeter中共有8类可被执行的元件(测试计划与线程组不属于元件),这些元件中,取样器是典型的不与其它元件发生交互作用的元件,逻辑控制器只对其子节点的取样器有效,而其它元件(conf ...
- NSDate时间
NSDate 使用 ios时间的秒数 取当前时间的秒数 NSTimeInterval time = [[NSDate date] timeIntervalSince1970]; long long i ...
- JavaFX2 - 文本可复制的Label
背景介绍 我的公司和我个人一直都使用JavaFX2来编写client应用程序,同一时候也作为Applet在浏览器中执行. 我们的客户以前拿我们的产品和网页对照,然后向我们提过两个需求: (1) 希望界 ...
- Android 调用系统邮件,发送邮件到指定邮箱
在项目中,最后有一个联络我们,要求是点击号码还有邮箱地址能够发送邮件,这时候解决的方案其实有两种,一种是调用系统发邮件的软件,可以添加邮箱账号就可以发送邮件:第二种是使用javamail来发送邮件.在 ...
- 总览:SpringCloud基础结构
客户端: 1.连接 配置中心2.连接 服务中心3.连接 链路跟踪3.feign调用4.熔断机制5.连接 熔断监控6.swagger2 生成的RESTful-API7.消息总线-rabbitMQ实现 基 ...
- 使用u盘量产工具修复写保护的u盘
自己的u盘突然提示写保护,而且也没有写保护开关,怎么都写不进文件,试了很多办法都无法去除写保护,最后找了一个u盘量产工具,搞定: 插上u盘后,会检测到u盘,点“开始"后静静等待它完成,u盘又 ...
- stm32四种输入
1. 上拉输入(GPIO_Mode_IPU) 上拉输入就是信号进入芯片后加了一个上拉电阻,再经过施密特触发器转换成0.1信号,读取此时的引脚电平为高电平: 2. ...
- AIR 初步 Javascript学习之cookie操作
//设置cookie的名称,值,过期时间 function setCookie(cookieName,cookieValue,cookieExpire) { v ...
- Satisfying memory ordering requirements between partial reads and non-snoop accesses
A method and apparatus for preserving memory ordering in a cache coherent link based interconnect in ...