zoukankan      html  css  js  c++  java
  • 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 = 111;
    const int INF = 1e9 + 7;
    
    char str[maxn], sig[maxn];
    int val[maxn],p1,p2;
    int n;
    
    void init() {
        p1 = 0, p2 = 0;
        sig[p1++] = '+';
    }
    
    int main() {
        init();
        while (scanf("%s", str) == 1 && str[0] != '=') {
            if (str[0] == '?') {
                
            }else{
                sig[p1++] = str[0];
            }
        }
        sig[p1] = '';
        scanf("%d", &n);
        int mi=0, ma=0;
        vector<int> ans;
        for (int i = 0; i < p1; i++) {
            if (sig[i] == '+') mi += 1,ma+=n,ans.push_back(1);
            else mi -= n,ma-=1,ans.push_back(n);
        }
        if (n >= mi&&n <= ma) {
            int dis = n-mi;
            for (int i = 0; i < p1; i++) {
                if (sig[i] == '+') {
                    if (dis >= n - 1) {
                        ans[i] = n;
                        dis -= (n - 1);
                    }
                    else {
                        ans[i] += dis;
                        dis = 0;
                    }
                }
                else {
                    if (dis >= n - 1) {
                        ans[i] = 1;
                        dis -= (n - 1);
                    }
                    else {
                        ans[i] -= dis;
                        dis = 0;
                    }
                }
                if (dis == 0) break;
            }
            printf("Possible
    ");
            for (int i = 0; i < p1-1; i++) {
                printf("%d %c ", ans[i], sig[i + 1]);
            }
            printf("%d = %d
    ", ans[p1 - 1], n);
        }
        else {
            printf("Impossible
    ");
        }
        return 0;
    }
  • 相关阅读:
    pymoo: Multi-objective Optimization in Python
    读代码——NSGAII
    读论文——A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II
    神经网络入门00
    梯度下降pthon实现
    在线加解密工具
    安恒杯-一张谍报
    漏洞挖掘学习记录
    安恒杯-元数据存储
    安恒杯-babysql
  • 原文地址:https://www.cnblogs.com/fenice/p/5547586.html
Copyright © 2011-2022 走看看