题目链接:

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. .NET使用QRCodeEncoder生成二维码

    ThoughtWorks.QRCode.dll版本 1.0.2774.19990 代码如下: /// <summary> /// 生成二维码图片 /// </summary> ...

  2. 实例:使用纹理对象创建Sprite对象

    精灵类是Sprite,它的类图如下图所示: Sprite类直接继承了Node类,具有Node基本特征.此外,我们还可以看到Sprite类的派生类有:PhysicsSprite和Skin.Physics ...

  3. 20160126深入浅出obc知识点整理

    1.错误域Error Domain NSMachErrorDomain:系统内核错误 NSPOSIXErrorDomain:系统错误 NSOSStatusErrorDomian:MacOS9之前的错误 ...

  4. iOS中-Qutarz2D详解及使用

    在iOS中Qutarz2D 详解及使用 (一)初识 介绍 Quartz 2D是二维绘图引擎. 能完成的工作有: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成 ...

  5. MIME类型大全

    获取MIME:HttpContext.Current.Request.Files[fileKey].ContentLength MIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该 ...

  6. 位图文件格式及linux下C语言来操作位图文件

    说到图片,位图(Bitmap)当然是最简单的,它是Windows显示图片的基本格式,其文件扩展名为*.BMP.由于没有经过任何的压缩,故BMP图片往往很大.在Windows下,任何格式的图片文件都要转 ...

  7. 8款超酷的HTML5 3D图片动画源码

    1.HTML5移动端图片左右切换动画 今天要给大家分享一款很不错的图片左右切换焦点图动画,并且支持移动端触摸滑动.功能上,这款HTML5图片播放器支持鼠标滑动.手机端触摸滑动以及自动播放.外观上,这款 ...

  8. java中的生产者和消费者的问题

    1----使用Java.util.concurrent.locks包中的lock接口取代synchronized,通过ReentrantLock这个已经实现Lock接口的类, 创建ReentrantL ...

  9. C# 玩家昵称屏蔽敏感字眼

    功能:使用正则  对玩家昵称处理,如果含有 屏蔽字库里的敏感字眼进行屏蔽.已封装成dll 1.屏蔽字库处理成所需要的正则格式:(所需正则表达式格式:".*((XX)|(XX)|(XX)|.. ...

  10. JS判断客户端是手机还是PC

    function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", " ...