- https://www.luogu.com.cn/problem/P6179
- http://usaco.org/index.php?page=viewproblem2&cpid=571
- http://www.lydsy.com/JudgeOnline/problem.php?id=4396
贪心解决。
首先可以发现,出牌顺序并没有什么用。
所以说,我们将读入的牌塞进桶内,并打好标记。
最后,有标记的就是 Elsie 的牌,而没有标记的就是 Bessie 的牌。
紧接着,我们倒序循环每一张牌。
如果当前循环到的是 Bessie 的牌,则说明能大过 Elsie 接下来的牌又多了一张。
如果当前循环到的是 Elsie 的牌,明显我们如果有能大过的牌就大过。
时间复杂度:$O(n)$。
#include<bits/stdc++.h> using namespace std; int n,x,sum,ans; bool pan[100010]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&x); pan[x]=1;//标记已出现过 } for(int i=2*n;i>=1;i--)//倒序寻找 { if(!pan[i]) sum++; if(pan[i]&&sum) sum--,ans++; } printf("%d",ans); }