首页 > 代码库 > ZOJ 3829 Known Notation 贪心

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_backtypedef 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\n", ret);    }    return 0;}

 

ZOJ 3829 Known Notation 贪心