巧妙的贪心

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.


题目分析

先从约束最少的情况开始考虑。

游戏规则不改变

如果每一轮都是点数大/小的人获胜,显然一次$O(n)$的贪心就可以了。类似于田忌赛马的道理。

然后从最基础的暴力考虑起。

第一个$n^2$想法

枚举$n$次断点,对于断点的两边分开贪心。这里的贪心思路是上面那种,用最近满足条件的来匹配,如果没有满足的匹配,则用最差的匹配之

这里会出现一个问题:按照这种贪心思路,前一部分贪完之后把一些最小的数用掉了。

对于前一部分来说,这些最小的的确没什么用;但是对于后一部分来说,它需要的就是这些小的数。

换句话说就是“好心没好报”,后一部分并不买前一部分贪心后的帐。

第二个$n^2$想法

Bessie手上的牌只有$n$张,也就是说她最多得分就是$n$。

那我们感性理解一下,把她出牌得分序列看作是一个01串。这里有很普通但是很重要的一点:每张牌最多对答案贡献1。

于是这保证了我们可以先不匹配一些回合,转而进行后面操作的正确性。

然后显然时间复杂度是很不对的(因为要判断最近满足状态所以还要带一个log),于是只有34分

 #include<bits/stdc++.h>
const int maxn = ; int n,a[maxn],b[maxn],ans;
int vis[maxn];
bool f[maxn<<]; inline int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch = getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch = getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
int main()
{
register int tot,i,k,tt,cg;
n = read();
for (i=; i<=n; i++)
a[i] = read(), f[a[i]] = ;
for (i=; i<=*n; i++)
if (!f[i]) b[++b[]] = i;
for (k=; k<=n; k++)
{
tot = ;
for (i=; i<=n; i++)
{
cg = i > k?-:;
tt = std::lower_bound(b+, b+n+, a[i])-b+cg;
if (cg==) tt--;
for (; tt>=&&tt<=n; tt+=cg)
if (vis[tt]!=k){
vis[tt] = k, tot++;
break;
}
if (tot+n-i+ < ans) break;
}
ans = tot>ans?tot:ans;
}
printf("%d\n",ans);
return ;
}

$nlogn$的想法

回顾一下前两个$n^2$的思路,想必很显然的一点是我们可以dp地处理$f[i]$和$g[i]$分别表示从$i$开始向前/向后的最大得分。

对,问题就是出在重复上,这两个最优方案是有重叠的。所以这题不能分类在动态规划里。

深入地剖析一下这个重复的特点,注意到一个事实是如果有重复,则一定会有多余数字。

有多余数字会发生很有趣的事情:假设重复的数字是$k$,$a<k<b$且$a,b$多余,那么$a$可以在断点之后替代$k$;$b$可以在断点之前替代$k$。

然后就显然正确了。

 #include<bits/stdc++.h>
const int maxn = ; int n,a[maxn],b[maxn],ans;
int f[maxn],g[maxn];
bool vis[maxn],mp[maxn<<]; inline int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch = getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch = getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
int main()
{
n = read();
for (int i=; i<=n; i++) a[i] = read(), mp[a[i]] = ;
for (int i=; i<=*n; i++)
if (!mp[i]) b[++b[]] = i;
for (int i=; i<=n; i++)
{
int tt = std::upper_bound(b+, b+n+, a[i])-b;
bool fl = ;
for (; tt<=n; tt++)
if (!vis[tt]){
fl = , vis[tt] = ;
break;
}
f[i] = f[i-];
if (fl) f[i]++;
}
memset(vis, , sizeof vis);
for (int i=n; i>=; i--)
{
int tt = std::lower_bound(b+, b+n+, a[i])-b-;
bool fl = ;
for (; tt; tt--)
if (!vis[tt]){
fl = , vis[tt] = ;
break;
}
g[i] = g[i+];
if (fl) g[i]++;
}
for (int i=; i<=n; i++)
ans = ans < f[i]+g[i+]?f[i]+g[i+]:ans;
printf("%d\n",ans);
return ;
}

END

【dp 贪心】bzoj4391: [Usaco2015 dec]High Card Low Card的更多相关文章

  1. 【BZOJ4391】[Usaco2015 dec]High Card Low Card(贪心)

    [BZOJ4391][Usaco2015 dec]High Card Low Card(贪心) 题面 BZOJ 题解 预处理前缀后缀的结果,中间找个地方合并就好了. #include<iostr ...

  2. 【题解】P3129高低卡(白金)High Card Low Card

    [题解][P3129 USACO15DEC]高低卡(白金)High Card Low Card (Platinum) 考虑贪心. 枚举在第几局改变规则,在改变规则之前,尽量出比它大的最小的牌,在改变规 ...

  3. [BZOJ4391][Usaco2015 dec]High Card Low Card dp+set+贪心

    Description Bessie the cow is a huge fan of card games, which is quite surprising, given her lack of ...

  4. 【刷题】BZOJ 4391 [Usaco2015 dec]High Card Low Card

    Description Bessie the cow is a huge fan of card games, which is quite surprising, given her lack of ...

  5. [USACO15DEC]高低卡(白金)High Card Low Card (Platinum)

    题目描述 Bessie the cow is a hu e fan of card games, which is quite surprising, given her lack of opposa ...

  6. BZOJ4391 High Card Low Card [Usaco2015 dec](贪心+线段树/set库

    正解:贪心+线段树/set库 解题报告: 算辣直接甩链接qwq 恩这题就贪心?从前往后从后往前各推一次然后找一遍哪个地方最大就欧克了,正确性很容易证明 (这里有个,很妙的想法,就是,从后往前推从前往后 ...

  7. [bzoj4391] [Usaco2015 dec]High Card Low Card 贪心 线段树

    ---题面--- 题解: 观察到以决策点为分界线,以点数大的赢为比较方式的游戏都是它的前缀,反之以点数小的赢为比较方式的都是它的后缀,也就是答案是由两段答案拼凑起来的. 如果不考虑判断胜负的条件的变化 ...

  8. bzoj4391 [Usaco2015 dec]High Card Low Card

    传送门 分析 神奇的贪心,令f[i]表示前i个每次都出比对方稍微大一点的牌最多能赢几次 g[i]表示从i-n中每次出比对方稍微小一点的牌最多赢几次 ans=max(f[i]+g[i+1]) 0< ...

  9. [USACO15DEC]High Card Low Card (Platinum)

    https://www.zybuluo.com/ysner/note/1300791 题面 贝西和她的朋友艾尔西正在玩这个简单的纸牌游戏.游戏有\(2N\)张牌,牌上的数字是\(1\)到\(2N\). ...

随机推荐

  1. 字符串匹配,KMP算法

    KMP的详解见:https://segmentfault.com/a/1190000008575379 主要难点在于Next数组的理解,KMP是不需要回溯的匹配算法. #include<iost ...

  2. java数据结构----树

    1.树:树通常结合了有序数组和链表的优点,在树中查找数据项的速度和在有序数组中查找一样快,并且插入数据项和删除数据项的速度也和链表一样快. 2.树由边连接的节点而构成.节点一般代表着一些实体,节点间的 ...

  3. redis配置配置文件

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...

  4. HDU3853(期望)

    题目很水了,但是原地打转的点……虽然不难想到这个坑,但是从数学的角度来讲期望不应该算正无穷嘛……为什么算0啊? ; ; int R, C; struct gird { db ori, right, d ...

  5. Bear and Tower of Cubes Codeforces - 680D

    https://codeforces.com/contest/680/problem/D 一道2D,又是搞两个小时才搞出来...不过好在搞出来了 官方题解:可以证明对于m,设a是满足a^3<=m ...

  6. Function ereg() is deprecated in

    PHP 5.3 ereg() 无法正常使用,提示"Function ereg() is deprecated Error".问题根源是php中有两种正则表示方法,一个是posix, ...

  7. Linux sftp用法

    sftp用法 1. 用sftp如何登录服务器 sftp 是一个交互式文件传输程式.它类似于 ftp, 但它进行加密传输,比FTP有更高的安全性.下边就简单介绍一下如何远程连接主机,进行文件的上传和下载 ...

  8. msyql 死锁

    1.使用 show processlist; 查询当前进程; 找到Command 状态是query 并且Time 时间很长的id kill掉即可 2.select * from information ...

  9. 【转】HashMap,ArrayMap,SparseArray源码分析及性能对比

    HashMap,ArrayMap,SparseArray源码分析及性能对比 jjlanbupt 关注 2016.06.03 20:19* 字数 2165 阅读 7967评论 13喜欢 43 Array ...

  10. php调用c# webservice方法

    第一次用,通过,还没深入了解. 首先在php.ini中启用extension=php_soap.dll,重启apache. $Client=new SoapClient("url?wsdl& ...