zoukankan      html  css  js  c++  java
  • ZOJ 3829 Known Notation 贪心

    主要的贪心思想就是,如果有一个不合法的*,那么再他前面加1或者2个数字的花费是不可能小于把它和后面的数字交换的,所以把不合法星号尽可能的往后放即可。

    这里我因为懒得特判,把每个情况都算了,不过n只有1000,n^2也是可以接受的。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <climits>
    #include <iostream>
    #include <string>
    
    using namespace std;
     
    #define MP make_pair
    #define PB push_back
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef vector<int> VI;
    typedef pair<int, int> PII;
    typedef pair<double, double> PDD;
    const int INF = INT_MAX / 3;
    const double eps = 1e-8;
    const LL LINF = 1e17;
    const double DINF = 1e60;
    const int maxn = 1024;
    char buf[maxn];
    
    bool isnum(char c) {
        return c >= '0' && c <= '9';
    }
    
    int getval(int len) {
        puts(buf);
        int cnum = 0, cf = 0, ans = 0;
        for(int i = 0; i < len; i++) {
            if(buf[i] >= '0' && buf[i] <= '9') cnum++;
            else {
                cf++;
                while(cf > cnum - 1) {
                    ans++; cnum++;
                }
            }
        }
        if(cf == 0) return -1;
        return ans;
    }
    
    int solve() {
        int len = strlen(buf);
        if(buf[len - 1] >= '0' && buf[len - 1] <= '9') {
            int ans = getval(len) + 1;
            for(int i = len - 2; i >= 0; i--) if(buf[i] == '*') {
                swap(buf[len - 1], buf[i]);
                ans = min(ans, getval(len) + 1);
                swap(buf[len - 1], buf[i]);
            }
            return ans;
        }
        else return getval(len);
    
    }
    
    int main() {
        int T; scanf("%d", &T);
        while(T--) {
            scanf("%s", buf);
            int ret = solve();
            printf("%d
    ", ret);
        }
        return 0;
    }
    
  • 相关阅读:
    反射机制小结
    IO流小节
    C# 利用file打印日志
    JS判断表单内容是否更改过
    input file 样式以及获取选择文件方法集合
    JS判断终端浏览器类型
    Node.js服务的重启与监控
    ASP.NET MVC 与Form表单交互
    JS 更改表单的提交时间和Input file的样式
    My SQL和LINQ 实现ROW_NUMBER() OVER以及Fatal error encountered during command execution
  • 原文地址:https://www.cnblogs.com/rolight/p/4031985.html
Copyright © 2011-2022 走看看