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 ...
随机推荐
- 第六十七篇、OC_UITableView head下拉图片放大的效果
(一) 布置UITableview 我们首先要通过设置UITableview的内容偏移 self.tableView.contentInset 来为图片视图留出位置,这里我们的图片高度暂定为280 ; ...
- iOS学习笔记--OC系列(1)
前言 从学校毕业进入公司工作已经第3个年头了,回顾这3年的经历,有种迷茫的感觉.在公司我主要是做零售业公司的系统维护,接触的主要是Oracle的Database的东西.但是业务知识和oracle,都没 ...
- OC10_代理反向传值
// // ProtectedDelegate.h // OC10_代理反向传值 // // Created by zhangxueming on 15/6/24. // Copyright (c) ...
- SpringMvc入门五----文件上传
知识点: SpringMvc单文件上传 SpringMvc多文件上传 这里我直接演示多文件上传,单文件的上传就不说了,不过代码都是现成的. 效果预览: DEMO图: 添加文件上传j ...
- OpenGL 回顾——简化版的窗体(包含鼠标控制,普通键位控制,以及镜面反射)
// OpenGLBook.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" //变量声明 static GLfloat spin =0.0; ...
- 大数求模 sicily 1020
Search
- git使用小结
本篇文章主要介绍自己在平时工作中使用git的一些常用命令,之前都是记录在本子上面,现在把他们记录在博客上,便于保存和回顾. 1. 建立自己的git仓库 1.1 在一个新建的repo文件夹里面,执行gi ...
- Debug Intro
The ABAP Debugger is used tool to execute and analyze programs line by line. Using it we can check t ...
- 随便写了一个DAO
package com.java; public class ExamStudent { /** * 流水号 */ private int flowId; /** * 四级.六级 */ private ...
- 【风马一族_Android】造作app的效果图
一.墨刀 官网:https://modao.cc