ZOJ 3829 Known Notation 贪心
2014-10-17 20:22
351 查看
主要的贪心思想就是,如果有一个不合法的*,那么再他前面加1或者2个数字的花费是不可能小于把它和后面的数字交换的,所以把不合法星号尽可能的往后放即可。
这里我因为懒得特判,把每个情况都算了,不过n只有1000,n^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\n", ret); } return 0; }
相关文章推荐
- ZOJ 3829 Known Notation(贪心)
- ZOJ 3829 Known Notation【贪心】【好题】
- ZOJ 3829 Known Notation 贪心
- ZOJ 3829 Known Notation --贪心+找规律
- ZOJ3829-贪心|模拟-Known Notation
- zoj 3829 Known Notation(贪心)
- zoj 3829 Known Notation (贪心)
- zoj 3829 Known Notation (The 2014 ACM-ICPC Asia Mudanjiang Regional Contest K题) 贪心
- ZOJ 3829 Known Notation --贪心+找规律
- 【贪心+一点小思路】Zoj - 3829 Known Notation
- ZOJ 3829 Known Notation 贪心 难度:0
- ZOJ 3829 Known Notation / 2014牡丹江区域赛 K (模拟, 贪心)
- K - Known Notation ZOJ - 3829 贪心
- ZOJ 3829 Known Notation(贪心)
- zoj 3829 Known Notation The 2014 ACM-ICPC Asia Mudanjiang Regional Contest K 贪心
- ZOJ 3829 Known Notation
- zoj 3829 Known Notation(2014年牡丹江区域赛k题)
- Known Notation ZOJ - 3829
- ZOJ 3829 Known Notation 乱搞
- ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)