Description:ios
非 * 號的地方能夠放A或B,不能AA或BB,一共有a個A,b個B,問你最多放幾個spa
Solution:
1.模擬一下,找連續空位長度,若是長度爲奇數,則我能夠有一個位置聽任意一個,不然擺放消耗必定,最後放完了判斷一下是否是還有剩下的,有剩下的就都放到任意位置
Codecode
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 2e5 + 2e2; char s[maxn]; int main() { int n,a,b; int numa,numb; while(~scanf("%d%d%d",&n,&a,&b)) { numa = a; numb = b; scanf("%s",s); s[n] = '*'; s[n+1]='\0'; int last = -1; int use = 0; for(int i = 0;i < n + 1;++i) { if(s[i] == '*') { int len = i - last - 1; if(len & 1)use++; a -= len / 2; b -= len / 2; last = i; } } int ret = 0; if(a <= 0) { ret += numa; a = 0; } else ret += numa - a; if(b <= 0) { ret += numb; b = 0; } else ret += numb - b; ret += min(a+b,use); printf("%d\n",ret); } return 0; }
2.小貪心,按位置模擬,能放的時候就先放個數最多,直到結束blog
codeip
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 2e5 + 2e2;char s[maxn];int main(){ int n,a,b; while(~scanf("%d%d%d",&n,&a,&b)) { scanf("%s",s); int ret = 0; for(int i = 0;i < n;++i) { if(a < b) swap(a,b); while(s[i] == '.' && i < n) { if(a > 0)ret++; a--; swap(a,b); i++; } } printf("%d\n",ret); } return 0;}