巧妙的贪心

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分

 1 #include<bits/stdc++.h>
 2 const int maxn = 50035;
 3
 4 int n,a[maxn],b[maxn],ans;
 5 int vis[maxn];
 6 bool f[maxn<<1];
 7
 8 inline int read()
 9 {
10     char ch = getchar();
11     int num = 0;
12     bool fl = 0;
13     for (; !isdigit(ch); ch = getchar())
14         if (ch=='-') fl = 1;
15     for (; isdigit(ch); ch = getchar())
16         num = (num<<1)+(num<<3)+ch-48;
17     if (fl) num = -num;
18     return num;
19 }
20 int main()
21 {
22     register int tot,i,k,tt,cg;
23     n = read();
24     for (i=1; i<=n; i++)
25         a[i] = read(), f[a[i]] = 1;
26     for (i=1; i<=2*n; i++)
27         if (!f[i]) b[++b[0]] = i;
28     for (k=0; k<=n; k++)
29     {
30         tot = 0;
31         for (i=1; i<=n; i++)
32         {
33             cg = i > k?-1:1;
34             tt = std::lower_bound(b+1, b+n+1, a[i])-b+cg;
35             if (cg==1) tt--;
36             for (; tt>=1&&tt<=n; tt+=cg)
37                 if (vis[tt]!=k){
38                     vis[tt] = k, tot++;
39                     break;
40                 }
41             if (tot+n-i+1 < ans) break;
42         }
43         ans = tot>ans?tot:ans;
44     }
45     printf("%d\n",ans);
46     return 0;
47 }

$nlogn$的想法

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

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

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

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

然后就显然正确了。

 1 #include<bits/stdc++.h>
 2 const int maxn = 50035;
 3
 4 int n,a[maxn],b[maxn],ans;
 5 int f[maxn],g[maxn];
 6 bool vis[maxn],mp[maxn<<1];
 7
 8 inline int read()
 9 {
10     char ch = getchar();
11     int num = 0;
12     bool fl = 0;
13     for (; !isdigit(ch); ch = getchar())
14         if (ch=='-') fl = 1;
15     for (; isdigit(ch); ch = getchar())
16         num = (num<<1)+(num<<3)+ch-48;
17     if (fl) num = -num;
18     return num;
19 }
20 int main()
21 {
22     n = read();
23     for (int i=1; i<=n; i++) a[i] = read(), mp[a[i]] = 1;
24     for (int i=1; i<=2*n; i++)
25         if (!mp[i]) b[++b[0]] = i;
26     for (int i=1; i<=n; i++)
27     {
28         int tt = std::upper_bound(b+1, b+n+1, a[i])-b;
29         bool fl = 0;
30         for (; tt<=n; tt++)
31             if (!vis[tt]){
32                 fl = 1, vis[tt] = 1;
33                 break;
34             }
35         f[i] = f[i-1];
36         if (fl) f[i]++;
37     }
38     memset(vis, 0, sizeof vis);
39     for (int i=n; i>=1; i--)
40     {
41         int tt = std::lower_bound(b+1, b+n+1, a[i])-b-1;
42         bool fl = 0;
43         for (; tt; tt--)
44             if (!vis[tt]){
45                 fl = 1, vis[tt] = 1;
46                 break;
47             }
48         g[i] = g[i+1];
49         if (fl) g[i]++;
50     }
51     for (int i=0; i<=n; i++)
52         ans = ans < f[i]+g[i+1]?f[i]+g[i+1]:ans;
53     printf("%d\n",ans);
54     return 0;
55 }

END

转载于:https://www.cnblogs.com/antiquality/p/9159505.html

【dp 贪心】bzoj4391: [Usaco2015 dec]High Card Low Card相关推荐

  1. bzoj4396[Usaco2015 dec]High Card Wins*

    bzoj4396[Usaco2015 dec]High Card Wins 题意: 一共有2n张牌,Alice有n张,Bob有n张,每一局点数大的赢.知道Bob的出牌顺序,求Alice最多能赢几局.n ...

  2. 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)

    洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...

  3. 【bzoj 4390】 [Usaco2015 dec]Max Flow(树上差分)

    4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 156  Solved: 100 [Sub ...

  4. bzoj4390[Usaco2015 dec]Max Flow*

    bzoj4390[Usaco2015 dec]Max Flow 题意: 给定一棵有N个点的树,所有节点的权值都为0.有K次操作,每次指定两个点s,t,将s到t路径上所有点的权值都加一.请输出K次操作完 ...

  5. 【BZOJ3174】【codevs25442075】拯救小矮人,DP+贪心

    Time:2016.07.19 Author:xiaoyimi 转载注明出处谢谢 传送门1 传送门2 传送门3 思路: 比较神的DP "贪心确定DP的状态"--reflash 写了 ...

  6. bzoj4393[Usaco2015 Dec]Fruit Feast*

    bzoj4393[Usaco2015 Dec]Fruit Feast 题意: 奶牛一开始饱胀值为0,上限为T.每个柠檬派提供a点饱胀值,每个橘子派提供b点饱胀值,有一次机会喝水,使得饱胀值div2.柠 ...

  7. [sd card] sd card初始化流程

    以SD 3.0为例. 建议先参考<[sd card] SD card初始化时的总线设置>. 一.sd card初始化流程思路说明 通过<SD_Ver3.00_Final_090416 ...

  8. [sd card] SD card初始化时的总线设置

    ============================================================== 零.在sd card初始化过程中,对于bus主要设置以下东西 1.时钟,输 ...

  9. 2018.10.26 NOIP模拟 瓶子 (dp/贪心)

    传送门 正解是dp并不想去想了. 自己yy了一个贪心拿了95pts95pts95pts,唯一没过的点还只有一个地方错了,面向数据变成之后过啦! 所以我讲讲如何贪心. 考虑到最后都只会合并成一种颜色,所 ...

最新文章

  1. Linux查看dmesg日志,Linux中的Printk与dmesg功能
  2. IBM虚拟化石油行业成功案例分享
  3. FileUpload 控件 禁止手动输入
  4. 手机变身IoT设备之地理位置上报
  5. 一段话系列-正则基础
  6. python的dict()字典数据类型的方法详解以及案例使用
  7. 镭神智能C32 ROS Rviz使用教程
  8. 2020高压电工考试及高压电工复审模拟考试
  9. linux 内核参数 pte,Linux下通过线性地址得到页表项pte(X86和龙芯2F下)
  10. NXP JN5169 ZigBee 3.0开发环境搭建
  11. Tumblr,instapaper分享
  12. 201871010134-周英杰《面向对象程序设计(Java)》第四周学习总结
  13. 图森未来:营收增长与亏损扩大并行
  14. 有哪些适合男生的蓝牙耳机?520情人节适合送男生的蓝牙耳机推荐
  15. loglog matlab,MATLAB 函数loglog()
  16. this关键字及 this关键字的应用
  17. Python_模拟登陆新浪微博
  18. 按广义表表示二叉树结构生成二叉树链表的算法
  19. 剑指Offer(29)顺时针打印矩阵
  20. chai3d+haptic device

热门文章

  1. SAP CRM WebUI上Opportunity reason字段的后台配置
  2. 字节是微型计算机中存储容量的度量单位,微型计算机内存容量的基本计量单位...
  3. geoserver 3_SD 2-3/15 PR调速阀德国HAWE哈威
  4. php本地测试还是线上,PHP本地与远程测试(一)
  5. python os.system关闭log_又到牛市!带你学习一个python强大证券数据分析工具
  6. 回文数Python解法
  7. linux c进程和线程脑图,进程和线程
  8. java web 获取根目录_javaweb中获取服务器端跟目录方法总结
  9. linux树莓派连接wifi热点,如何将树莓派设置为WiFi热点
  10. linux服务器上装R语言,R语言 在linux上的安装