1. 程式人生 > >【刷題】BZOJ 4391 [Usaco2015 dec]High Card Low Card

【刷題】BZOJ 4391 [Usaco2015 dec]High Card Low Card

set lin 最大化 gis next 從後往前 predict 奶牛 怎麽

Description

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.

Solution

怎麽說,貪心的題都不知道怎麽去講
首先,對於單個數,我們要贏它,那麽肯定是在自己的數的集合中選一個大於它的最小的數,或者是小於它的最大的數(取決在哪一段)
這是單個數的貪心
然後求出 \(f\) 數組,\(f_i\) 代表從前往後到第 \(i\) 個數,贏的方式必須是大於的情況下,最多能贏多少把
同時求出 \(g\) 數組,\(g_i\) 代表從後往前到第 \(i\) 個數,贏的方式必須是小於的情況下,最多能贏多少把
那麽最後的答案就是 \(\max_{i=0}^n\{f_i+g_{i+1}\}\)
為什麽是這樣,這樣有的數不是用了兩次嗎
確實有的數用了兩次,但考慮這樣的事實:

  • 如果一個數用了兩次,那麽肯定有一個數未被使用。
  • 用了兩次的數一定是 \(f\) 中用一次,\(g\) 中用一次,同一段中不可能用兩次

設用了兩次的數為 \(a\) ,未用的數為 \(b\) 。如果 \(a<b\) ,那麽 \(b\) 是可以代替 \(a\) 在比更大的那一段取得勝利的;相反同理。
而我們已經最大化了 \(f\) 數組和 \(g\) 數組,所以這一定是正確的
求兩個數組,就用個set維護就好了

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=50000+10;
int n,vis[MAXN<<1],a[MAXN],f[MAXN],g[MAXN],cnt,ans;
std::set<int> S;
std::set<int>::iterator it;
template<typename T> inline void read(T &x)
{
    T data=0,w=1;
    char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
    if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
int main()
{
    read(n);
    for(register int i=1;i<=n;++i)read(a[i]),vis[a[i]]=1;
    for(register int i=1;i<=(n<<1);++i)
        if(!vis[i])S.insert(i);
    for(register int i=1;i<=n;++i)
        if((it=S.lower_bound(a[i]))!=S.end())f[i]=++cnt,S.erase(it);
        else f[i]=f[i-1];
    S.clear();cnt=0;
    for(register int i=1;i<=(n<<1);++i)
        if(!vis[i])S.insert(i);
    for(register int i=n;i>=1;--i)
        if(((it=S.lower_bound(a[i]))--)!=S.begin())g[i]=++cnt,S.erase(it);
        else g[i]=g[i+1];
    for(register int i=0;i<=n;++i)chkmax(ans,f[i]+g[i+1]);
    write(ans,'\n');
    return 0;
}

【刷題】BZOJ 4391 [Usaco2015 dec]High Card Low Card