题目链接:

http://codeforces.com/contest/664/problem/B

题意:

给你一个等式,把等式左边的问号用1到n(n为等式右边的数)的数填好,使得等式成立

题解:

贪心求出最小最大值,如果n在这个范围则有解,否则无解。

构造解: 取最小值或最大值,然后从第一个数开始调整,直到等式成立为止。

代码:

#include<iostream>
#include<cstring>
#include<vector>
using namespace std; const int maxn = ;
const int INF = 1e9 + ; char str[maxn], sig[maxn];
int val[maxn],p1,p2;
int n; void init() {
p1 = , p2 = ;
sig[p1++] = '+';
} int main() {
init();
while (scanf("%s", str) == && str[] != '=') {
if (str[] == '?') { }else{
sig[p1++] = str[];
}
}
sig[p1] = '\0';
scanf("%d", &n);
int mi=, ma=;
vector<int> ans;
for (int i = ; i < p1; i++) {
if (sig[i] == '+') mi += ,ma+=n,ans.push_back();
else mi -= n,ma-=,ans.push_back(n);
}
if (n >= mi&&n <= ma) {
int dis = n-mi;
for (int i = ; i < p1; i++) {
if (sig[i] == '+') {
if (dis >= n - ) {
ans[i] = n;
dis -= (n - );
}
else {
ans[i] += dis;
dis = ;
}
}
else {
if (dis >= n - ) {
ans[i] = ;
dis -= (n - );
}
else {
ans[i] -= dis;
dis = ;
}
}
if (dis == ) break;
}
printf("Possible\n");
for (int i = ; i < p1-; i++) {
printf("%d %c ", ans[i], sig[i + ]);
}
printf("%d = %d\n", ans[p1 - ], n);
}
else {
printf("Impossible\n");
}
return ;
}

Codeforces Round #347 (Div. 2) B. Rebus的更多相关文章

  1. Codeforces Round #347 (Div.2)_B. Rebus

    题目链接:http://codeforces.com/contest/664/problem/B B. Rebus time limit per test 1 second memory limit ...

  2. Codeforces Round #347 (Div. 2)

    unrating的一场CF A - Complicated GCD #include <bits/stdc++.h> const int N = 1e5 + 5; char a[105], ...

  3. Codeforces Round #347 (Div. 2) C. International Olympiad 找规律

    题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...

  4. Codeforces Round #347 (Div.2)_A. Complicated GCD

    题目链接:http://codeforces.com/contest/664/problem/A A. Complicated GCD time limit per test 1 second mem ...

  5. Codeforces Round #347 (Div. 2) (练习)

    A: 题意:找到[a, b]的最大公约数: 思路:相同时为本身,不同时为1. 套路:碰到水题别想太多: 猜想:两个相邻数,必有一奇一偶,如果偶数有因子3或者其他,奇数可不可能有相同的呢? 枚举一些数后 ...

  6. codeforces Round #347 (Div. 2) C - International Olympiad

    思路:从后往前一位一位的模拟,每次判断一下当前枚举的数是否之间枚举过了.或者当前枚举数过小,小于1989. #include<cstdio> #include<cstring> ...

  7. Codeforces Round #347 (Div. 2) A

    Description Greatest common divisor GCD(a, b) of two positive integers a and b is equal to the bigge ...

  8. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. 【学习笔记】【C语言】字符串数组

    1.使用场合 * 一维字符数组中存放一个字符串,比如一个名字char name[20] = "mj" * 如果要存储多个字符串,比如一个班所有学生的名字,则需要二维字符数组,cha ...

  2. python基础:搜索路径

    如何将写好的脚本或者是模块加入python的搜索路径? >>>import sys >>> sys.path ['', '/Library/Frameworks/P ...

  3. 配置PostgreSQL Streaming Replication集群

    运行环境: Primary: 192.168.0.11 Standby: 192.168.0.21, 192.168.0.22 OS: CentOS 6.2 PostgreSQL: 9.1.2 版本以 ...

  4. C++ 11 之学习总结

    感慨时间过的好快,C++ 11出来都5年了,现在才开始学习,但为时也不晚: 主要是网上及身边的朋友大肆宣扬C++ 11的某些优化,弄得别人心里痒痒的,所以就花了3天学习了点基本知识,相对于整个C++ ...

  5. CString使用

    1. 空间分配,如果不是它自己的空间分配方式,需要用函数来手动分配空间,否则大家指向同一块地址,取得内容一样 例子,读取文件到CString ,没有给CString 对象分配空间,而且不是他定义的开拓 ...

  6. 字节的高低位知识,Ascii,GB2312,UNICODE等编码的关系与来历

    很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的,于是他们把这称为"字节". 再后来,他们又做了一些可以处理 ...

  7. 通过Driver获取数据库连接

    先看一下文件,在当前包下有一个properties配置文件,在根目录下有一个lib文件夹,里面放的是mySql的驱动jar包 Driver :是一个接口,数据库厂商必须提供实现的接口,能从其中获取数据 ...

  8. activiti搭建(一)初始化数据库

    转载请注明源地址:http://www.cnblogs.com/lighten/p/5876681.html activiti-engine.jar包中自带了创建activiti工作流数据库表的SQL ...

  9. jQuery学习-----(二)JQuery对象与DOM对象的区别与转换

    1.jQuery对象和DOM对象的区别 DOM对象,即是我们用传统的方法(javascript)获得的对象,jQuery对象即是用jQuery类库的选择器获得的对象; eg: var domObj = ...

  10. align=absMiddle属性设置

    AbsBottom 图像的下边缘与同一行中最大元素的下边缘对齐.AbsMiddle 图像的中间与同一行中最大元素的中间对齐.Baseline 图像的下边缘与第一行文本的下边缘对齐.Bottom 图像的 ...