(Jizhong) 2200. [Training] GDKOI card games (card) [greedy]

(File IO): input: card.in output: card.out
time limit: 1000 ms space constraints: 262144 KB specific restrictions
Goto ProblemSet


Title Description
B e s s i e Bessie is very much like a card cows, though she did not thumb, but she has a near-obsession with the love of playing cards. Unfortunately, other cattle in the herd are not good opponent. Their level is really bad. They are always in a completely predictable manner to play cards! despite this, B e s s i e Bessie still can choose how to win.
B e s s i e Bessie and her friend Elsie playing a simple card game, they have got to pay a 2 n 2n cards cards, digital numbers on the card is 1 2 n 1- 2n , and are divided into two, to a card B e s s i e Bessie and a card to E l s i e Elsie .
Then they began to play cards, a total of conduct n n wheel, in each round, B e s s i e Bessie E l s i e Elsie are playing a card, the cards who have a big points.
magical B e s s i e Bessie can be predicted E l s i e Elsie cards sequence, and the like as in winning. please confirm B e s s i e Bessie the maximum number of points can be earned.


Enter
the first line an integer N ( 1 N 50 , 000 ) N (1≤N≤50,000) .
The next N N line is E l s i e Elsie to round out the continuous game cards. Please note that it is easy to determine from the information B e s s i e Bessie card.

Output
line is given B e s s i e Bessie can score the maximum number of points.


Sample input
. 3
. 1
. 6
. 4

Sample Output
2


Data range limit


Tip
Bessie hands of cards are 2 , 3 5 2,3,5 , according to her 2 3 5 2,3,5 sequential 2 points cards can be obtained.


Problem-solving ideas
greedy.
Prepare two orderly queues, with the principle of Tian Ji's horse.


Code

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
int n,m,a[100010],b[50010],c[50010],x,ans,k,k1,j;
int main(){
 freopen("card.in","r",stdin);
  freopen("card.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        a[x]=1;
    }
    k=0,k1=0;
    for(int i=1;i<=n*2;i++)
        if(!a[i])
            b[++k]=i;
        else
            c[++k1]=i;
    j=1,ans=0;
    for(int i=1;i<=n;i++)
    {
        if(c[j]<b[i])
        {
            j++;
            ans++;
            if(j>n)
            {
                printf("%d",ans);
                return 0;
            }
        }
    }
    printf("%d",ans);
}
Published 119 original articles · won praise 8 · views 4912

Guess you like

Origin blog.csdn.net/kejin2019/article/details/104975652