High Card Low Card HYSBZ - 4391

Bessie the cow is a huge fan of card games, which is quite surprising, given her lack of opposable thumbs. Unfortunately, none of the other cows in the herd are good opponents. They are so bad, in fact, that they always play in a completely predictable fashion! Nonetheless, it can still be a challenge for Bessie to figure out how to win.

Bessie and her friend Elsie are currently playing a simple card game where they take a deck of 2N cards, conveniently numbered 1…2N, and divide them into N cards for Bessie and N cards for Elsie. The two then play NN rounds, where in each round Bessie and Elsie both play a single card. Initially, the player who plays the highest card earns a point. However, at one point during the game, Bessie can decide to switch the rules so that for the rest of the game, the player who plays the lowest card wins a point. Bessie can choose not to use this option, leaving the entire game in "high card wins" mode, or she can even invoke the option right away, making the entire game follow the "low card wins" rule.

Given that Bessie can predict the order in which Elsie will play her cards, please determine the maximum number of points Bessie can win.

奶牛Bessie和Elsie在玩一种卡牌游戏。一共有2N张卡牌,点数分别为1到2N,每头牛都会分到N张卡牌。

游戏一共分为N轮,因为Bessie太聪明了,她甚至可以预测出每回合Elsie会出什么牌。

每轮游戏里,两头牛分别出一张牌,点数大者获胜。

同时,Bessie有一次机会选择了某个时间点,从那个时候开始,每回合点数少者获胜。

Bessie现在想知道,自己最多能获胜多少轮?

Input

The first line of input contains the value of N (2≤N≤50,000).

The next N lines contain the cards that Elsie will play in each of the successive rounds of the game. Note that it is easy to determine Bessie's cards from this information.

Output

Output a single line giving the maximum number of points Bessie can score.

Sample Input

4 1 8 4 3

Sample Output

3

Hint

Here, Bessie must have cards 2, 5, and 6, and 7 in her hand, and she can use these to win at most 3 points. For example, she can defeat the 1 card and then switch the rules to "low card wins", after which she can win two more rounds.

思路:

  1. 先按点数大者获胜跑一轮,sum1[i]记录的是出完第i张牌时获胜的局数
  2. 先按点数小者获胜跑一轮,sum2[i]记录的是出完第i张牌时获胜的局数

我们再枚举分割点,贪心找一个最大的获胜局数。

注意:有人说这样找的话在前面按点数大的时候用掉了牌a,后面按点数小的时候又用掉了牌a,那么牌a用掉了两遍。这样做看似是错的,其实是对的。

证明:当牌a用两遍时,必有一张牌b没有被使用,若a>b,那么在按点数小的时候用牌b替代牌a是一定可行的,若a<b,那么在按点数大的时候用牌b替代牌a也一定是可行的。

#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
const int maxn=50009;
int a[maxn],book[2*maxn],sum1[maxn],sum2[maxn];
int main()
{
	int n;
	scanf("%d",&n);
	memset(book,0,sizeof(book));
	sum1[0]=sum2[n+1]=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		book[a[i]]=1;
	}
	set<int> s;
	for(int i=1;i<=2*n;i++)
	{
		if(!book[i])
			s.insert(i);
	}
	set<int>::iterator it;
	for(int i=1;i<=n;i++)
	{
		it=s.lower_bound(a[i]);
		if(it!=s.end())
		{
			sum1[i]=sum1[i-1]+1;
			s.erase(it);
		}
		else
			sum1[i]=sum1[i-1];
	}
	s.clear();
	for(int i=1;i<=2*n;i++)
	{
		if(!book[i])
			s.insert(i);
	}
	for(int i=n;i>=1;i--)
	{
		it=s.lower_bound(a[i]);
		if(it!=s.begin())
		{
			it--;
			sum2[i]=sum2[i+1]+1;
			s.erase(it);
		}
		else
			sum2[i]=sum2[i+1];
	}
	int ans=max(sum1[n],sum2[1]);
	for(int i=1;i<n;i++)
		ans=max(ans,sum1[i]+sum2[i+1]);
	printf("%d\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/why932346109/article/details/88397699