牛客多校(2020第三场)A Clam and Fish
题目链接:https://ac.nowcoder.com/acm/contest/5668/A
题意:
- 小月有n个单位时间都在钓鱼,每个单位时间有4种状态,有蛤蜊/没蛤蜊,有鱼/没鱼。小月事先知道这n个时间点的状态。每个事件点有四种可能的动作:
- 若该时间点有鱼,则可以直钓鱼
- 若该时间点有蛤蜊,则可以利用蛤蜊制造一袋鱼饵。
- 若该时间点至少有一个鱼饵,则可以利用这个鱼饵钓一条鱼,之后鱼饵数量-1
- 什么事都不做
- 问小月最多可以获得多少条鱼
题解:
- 平台有鱼就钓鱼
- 如果有蛤蜊就拿来做鱼饵,没蛤蜊就用鱼饵钓鱼,如果最后还有x包鱼饵,那么就把制作比较晚的x/2包鱼饵的时间拿来钓鱼
1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<string> 5 using namespace std; 6 7 const int MAX_N = 2 * 10E6 + 1; 8 int n; 9 int num_01[MAX_N]; 10 string s; 11 12 void solve() { 13 int sum_fish = 0, sum_food = 0; 14 15 for (int i = 0; i < n; i++) { 16 switch(s[i]) { 17 case '0': { 18 if (sum_food != 0) { 19 sum_food--; 20 sum_fish++; 21 } 22 break; 23 } 24 case '1': { 25 sum_food++; 26 break; 27 } 28 case '2': 29 case '3': { 30 sum_fish++; 31 break; 32 } 33 } 34 } 35 if (sum_food) { 36 sum_fish += sum_food/2; 37 } 38 cout << sum_fish << "\n"; 39 return; 40 } 41 42 int main() { 43 int t; 44 cin >> t; 45 while (t--) { 46 cin >> n; 47 cin >> s; 48 solve(); 49 } 50 return 0; 51 }
附上之前傻傻ac做法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<string> 5 using namespace std; 6 7 const int MAX_N = 2 * 10E6 + 1; 8 int n; 9 int num_01[MAX_N]; 10 string s; 11 12 void solve() { 13 int sum_fish = 0, sum_food = 0; 14 int num_3 = count(s.begin(), s.end(),'3'); 15 int num_2 = count(s.begin(), s.end(),'2'); 16 sum_fish = sum_fish + num_2 + num_3; 17 18 for (int i = 0; i < n; i++) { 19 switch(s[i]) { 20 case '0': { 21 if (sum_food != 0) { 22 sum_food--; 23 sum_fish++; 24 } 25 break; 26 } 27 case '1': { 28 if (sum_food >= num_01[i] - 1 && sum_food != 0) { 29 sum_fish++; 30 sum_food--; 31 } 32 else { 33 sum_food++; 34 } 35 break; 36 } 37 default : break; 38 } 39 } 40 41 cout << sum_fish << "\n"; 42 return; 43 } 44 45 int main() { 46 int t; 47 cin >> t; 48 while (t--) { 49 memset(num_01, 0, sizeof(num_01)); 50 cin >> n; 51 s = ""; 52 cin >> s; 53 num_01[0] = count(s.begin(), s.end(),'0'); 54 num_01[0] += count(s.begin(), s.end(), '1'); 55 int temp = 1; 56 for (int i = 1; i < n; i++) { 57 if (s[i] == '0' || s[i] == '1') { 58 num_01[i] = num_01[0] - temp; 59 temp++; 60 } 61 } 62 solve(); 63 } 64 return 0; 65 }