https://www.zybuluo.com/ysner/note/1300791

题面

贝西和她的朋友艾尔西正在玩这个简单的纸牌游戏。游戏有\(2N\)张牌,牌上的数字是\(1\)到\(2N\)。把这些牌分成两份,贝西有\(N\)张,艾尔西有另外\(N\)张。接下来她们进行\(N\)轮出牌,每次各出一张牌。一开始,谁出的牌上的数字大,谁就获得这一轮的胜利。贝西有一个特殊权利,她可以在任意一个时刻把原本数字大的获胜的规则改成数字小的获胜,这个改变将会一直持续到游戏结束。特别的,贝西可以从第一轮开始就使用小牌获胜的规则,也可以直到最后一轮都还不使用大牌获胜的规则。

现在,贝西已经知道了艾尔西出牌的顺序,她想知道她最多能够赢多少轮。

解析

显然前面每次选择比对方大却又尽可能小的牌,后面选择比对方小却又尽可能大的牌。

其实这就是田径赛马的原理。

支持删除和二分查找,显然\(set\)。

然后分别从前后预处理应该出什么牌,最后枚举中介点就行。

问题出在具体实现上。

后一个过程其实可以用一个存负值的\(set\)来搞。

而不是像蒟蒻一样用一个存正值的\(set\)然后\(TLE\)半天。

还有\(lower\_bound\)没找到结果会返回\(end()\)。

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<set>
#define ll long long
#define fp(i,a,b) for(int i=a;i<=b;++i)
#define fq(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
const int N=5e5+100;
int n,a[N],q[N],h[N],ans;
bool vis[N];
set<int>S1,S2;
set<int>::iterator it;
int main()
{
scanf("%d",&n);
fp(i,1,n) scanf("%d",&a[i]),vis[a[i]]=1;
fp(i,1,2*n) if(!vis[i]) S1.insert(i),S2.insert(-i);
fp(i,1,n)
{
it=S1.lower_bound(a[i]);
if(it!=S1.end())
{
q[i]=q[i-1]+1;
S1.erase(it);
}
else q[i]=q[i-1],S1.erase(*S1.begin());
}
fq(i,n,1)
{
it=S2.lower_bound(-a[i]);
if(it!=S2.end())
{
h[i]=h[i+1]+1;
S2.erase(it);
}
else h[i]=h[i+1],S2.erase(*S2.begin());
}
fp(i,0,n) ans=max(ans,q[i]+h[i+1]);
printf("%d\n",ans);
return 0;
}

[USACO15DEC]High Card Low Card (Platinum)的更多相关文章

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

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

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

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

  3. [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 ...

  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. [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 ...

  6. 【dp 贪心】bzoj4391: [Usaco2015 dec]High Card Low Card

    巧妙的贪心 Description Bessie the cow is a huge fan of card games, which is quite surprising, given her l ...

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

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

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

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

  9. bzoj4391 [Usaco2015 dec]High Card Low Card

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

随机推荐

  1. 安装Vmware Tools出现错误

    安装Vmware Tools出现: Before you can compile modules, you need to have the following installed... makegc ...

  2. 第二次 Ubuntu16.04 vi编辑器的方向键和退格问题

    新安装ubuntu后,好多人可能都碰到过这样的问题,vi对文件进行编辑时,上下左右键变成了ABDC,退格键也不管用. 解决办法其实也很简单,首先卸载掉旧的vim-common. apt-get rem ...

  3. python3.x Day5 subprocess模块!!

    subprocess模块: # subprocess用来替换多个旧模块和函数 os.system os.spawn* os.popen* popen2.* commands.* subprocess简 ...

  4. STM32——NVIV:嵌套中断向量控制器

    STM32有43个channel的settable的中断源:AIRC(Application Interrupt and Reset Register)寄存器中有用于指定优先级的4 bits.这4个b ...

  5. UVa 1600 Patrol Robot(BFS)

    题意: 给定一个n*m的图, 有一个机器人需要从左上角(1,1)到右下角(n,m), 网格中一些格子是空地, 一些格子是障碍, 机器人每次能走4个方向, 但不能连续穿越k(0<= k <= ...

  6. 添物不花钱学JavaEE(基础篇)- Java

    Java Java是一面向对象语言 Write Once Run Anywhere Designed for easy Web/Internet applications, Mobile Widesp ...

  7. 2018 & 微信小程序

    2018 & 微信小程序 Wafer2 快速开发 Demo 本仓库是最简版的 Wafer2 开发套件,建议配合腾讯云微信小程序开发者工具解决方案一起使用.适用于想要使用 Wafer SDK 开 ...

  8. springboot 集成日志 yml配置

    原文:https://www.cnblogs.com/bigben0123/p/7895696.html

  9. Flask(4):wtforms组件 & 数据库连接池 DBUtils

    wtforms 组件的作用: --- 生成 HTML 标签 --- form 表单验证 示例代码: app.py from flask import Flask, render_template, r ...

  10. Jquery为DIV添加点击事件,Jquery为a标签超链接添加点击事件

    <div>1</div> <div>2</div> <div>3</div> <div>4</div> ...