2020 Winter Holiday [gmoj2200] [GDKOI Training] [Card Game] [greedy]

Title description

Bessie is a cow who likes playing cards very much. Although she does not have a thumb, she has an almost obsessive love for playing cards. Unfortunately, none of the other cattle in the herd are good opponents. Their level is really poor. They always play cards in a completely predictable way! Nevertheless, Bessie can still choose how to win.
Bessie and her friend Elsie are playing a simple card game. They took a card with 2n cards. The number on the card is 1-2n, and they are divided into two. One card is for Bessie and A card to Elsie.
Then the two started playing cards for a total of n rounds. In each round, Bessie and Elsie both played a card, and whoever had the highest card scored one point.
The magical Bessie can predict Elsie's playing order and try to win as much as possible. Please determine the maximum points that Bessie can win.

Input

An integer N in the first row (1≤N≤50,000). The
next N rows are the cards that Elsie will play in each round of consecutive matches. Please note that it is easy to determine Bessie's card from this information.

Output

One line gives the maximum number of points that Bessie can score.

Sample input

3
1
6
4

Sample output

2

analysis

It is very simple to find out which cards Bessie has, with the opponent holding 1 and Bessie holding 0.
The optimal solution is easy to draw: the smallest card among the cards larger than the opponent's hand is the optimal one.
So from a small to a large cycle, record how many cards are still not crushed, and then, if you encounter a crush that can be crushed, even if you win one, and deal with the number of existing cards that are not crushed, it's done!

Code on

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,ff[100001],ans;
int main()
{
	freopen("card.in","r",stdin);
	freopen("card.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    	int x;
    	cin>>x;
    	ff[x]=1;
	}
	int x=0;
	for(int i=1;i<=2*n;i++)//从小到大直接 
	{
		if(ff[i]==1) x++;//别人打出一张
		if(ff[i]==0&&x>0)
		{
			x--;//从小到大第一个比它小的就压死 
			ans++;//赢一把 
		} 	
	}
	cout<<ans; 
	fclose(stdin);
	fclose(stdout);
    return 0;
}

Published 110 original articles · 100 praises · views 8019

Guess you like

Origin blog.csdn.net/dglyr/article/details/105035592