Codeforces Round #347 (Div. 2) B. Rebus
题目链接:
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的更多相关文章
- Codeforces Round #347 (Div.2)_B. Rebus
题目链接:http://codeforces.com/contest/664/problem/B B. Rebus time limit per test 1 second memory limit ...
- Codeforces Round #347 (Div. 2)
unrating的一场CF A - Complicated GCD #include <bits/stdc++.h> const int N = 1e5 + 5; char a[105], ...
- Codeforces Round #347 (Div. 2) C. International Olympiad 找规律
题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...
- 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 ...
- Codeforces Round #347 (Div. 2) (练习)
A: 题意:找到[a, b]的最大公约数: 思路:相同时为本身,不同时为1. 套路:碰到水题别想太多: 猜想:两个相邻数,必有一奇一偶,如果偶数有因子3或者其他,奇数可不可能有相同的呢? 枚举一些数后 ...
- codeforces Round #347 (Div. 2) C - International Olympiad
思路:从后往前一位一位的模拟,每次判断一下当前枚举的数是否之间枚举过了.或者当前枚举数过小,小于1989. #include<cstdio> #include<cstring> ...
- 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 ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- shell中使用echo命令改变输出显示样式
文本终端的颜色可以使用“ANSI非常规字符序列”来生成.举例:echo -e "\033[44;37;5m ME \033[0m COOL" 以上命令设置背景成为蓝色,前景白色,闪 ...
- 20150323--MVC
MVC: Model view(视图层,模板) Control(控制层) 三层:数据访问,商业逻辑,用户界面(Webform,MVC). 服务端无状态:接受请求,返回页面,每次请求并返回界面,前后不是 ...
- 10款基于jquery的web前端特效及源码下载
1.jQuery时间轴插件:jQuery Timelinr 这是一款可用于展示历史和计划的时间轴插件,尤其比较适合一些网站展示发展历程.大事件等场景.该插件基于jQuery,可以滑动切换.水平和垂直滚 ...
- Codevs 2833 奇怪的梦境
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Aiden陷入了一个奇怪的梦境:他被困在一个小房子中,墙上有很多按钮,还 ...
- TCP通信三次握手的过程
过程 编辑 第一次 第一次握手:建立连接时,客户端 发送syn包(syn=j)到服务器,并进入SYN_SENT状态,等待服务器确认:SYN:同步序列编号(Synchronize Sequence Nu ...
- C++ 嵌入汇编 获取CPU信息
#include "windows.h" #include "iostream" #include "string" using names ...
- Error: An App ID with identifier "*****" is not avaliable. Please enter a different string.
Error: An App ID with identifier "*****" is not avaliable. Please enter a different string ...
- “==”,比较的是引用 “equals方法”比较的是具体内容
package com.java1234.chap03.sec08; public class Demo2 { public static void main(String[] args) { //“ ...
- Python的类变量和对象变量声明解析
Python的类和C++一样,也都是存在两种类型的变量,类变量和对象变量!前者由类拥有,被所有对象共享,后者由每个对象独有.这里我主要想讨论一下他们的声明办法. 首先说的是对象变量: 只要是声明在类的 ...
- 锋利的jquery-选择器
1 jquery $(document).ready(function(){}) 可以简写成 $(function(){}) 2 jquery 对象和DOM对像 ① jquery对象和DOM对象不 ...