bzoj4396[Usaco2015 dec]High Card Wins*

bzoj4396[Usaco2015 dec]High Card Wins

题意:

一共有2n张牌,Alice有n张,Bob有n张,每一局点数大的赢。知道Bob的出牌顺序,求Alice最多能赢几局。n≤50000。

题解:

贪心。将Alice和Bob的牌按点数大小排序,然后如果Alice当前牌能赢Bob当前牌就ans++否则就不断调整Bob的当前牌直到Alice当前牌能赢Bob当前牌。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define inc(i,j,k) for(int i=j;i<=k;i++)
 5 #define maxn 50010
 6 using namespace std;
 7 
 8 inline int read(){
 9     char ch=getchar(); int f=1,x=0;
10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
11     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
12     return f*x;
13 }
14 int a[maxn],b[maxn],ans,n,tot; bool c[maxn*2];
15 int main(){
16     n=read(); inc(i,1,n)a[i]=read(),c[a[i]]=1; inc(i,1,2*n)if(!c[i])b[++tot]=i; sort(a+1,a+n+1);
17     int p=1,q=1;
18     while(1){
19         while(p<=n&&b[p]<a[q])p++; if(p==n+1)break; ans++; p++; q++;
20     }
21     printf("%d",ans); return 0;
22 }

20160908

原文地址:https://www.cnblogs.com/YuanZiming/p/5876058.html