日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/18 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 99精品久久久久 | 亚洲色欲色欲www | 日韩av视屏 | 最新日韩视频 | 色网站在线播放 | 精品啪啪 | 亚洲精品高清视频 | 中文字幕乱码人妻二区三区 | 蜜臀久久 | 免费污视频 | 都市激情亚洲综合 | av午夜在线 | 在线免费观看欧美大片 | 欧美日韩在线观看一区 | 欧美色欧美 | 人人草人人搞 | 国产v综合v亚洲欧美久久 | 久久毛片基地 | 日韩中文字幕av在线 | 午夜性生活视频 | 国产尤物精品 | 日韩视频国产 | 中国超碰 | 香蕉视频污在线观看 | 香蕉视频成人在线 | 国产一区二区三区四区在线观看 | 欧美日韩一区二区三区四区五区六区 | 姐姐av| 国产黄色在线观看 | 人人揉人人 | 老头把女人躁得呻吟 | 国产伦精品一区二区三区四区视频 | 国产资源站 | 久久精品无码Av中文字幕 | 捆绑无遮挡打光屁股调教女仆 | 欧美jizz19性欧美 | av大片在线观看 | 国产一区二区三区视频免费观看 | 欧美色图3p | 一本之道高清无码视频 | 午夜精品一区二区三区在线视频 | 国产精品理论在线观看 | 国产chinesehd天美传媒 | 精品一区在线看 | 奇米影视777第四色 2019中文字幕在线免费观看 | 成人在线综合 | 国产麻豆成人传媒免费观看 | 手机av片| 国产在线无码精品 | 毛片随便看 | 超碰888 | 国产一区二区三区在线观看视频 | 夜夜嗨av一区二区三区网页 | 99国产在线观看 | 久久人妻一区二区 | 国产一级免费大片 | 特黄一级毛片 | 国产玖玖 | 亚洲一区无 | 人与性动交zzzzbbbb | 奶水旺盛的少妇在线播放 | 麻豆疯狂做受xxxx高潮视频 | 综合网中文字幕 | 国产成人愉拍精品久久 | 亚洲专区在线 | 亚洲精品一区二区三区在线 | 天天躁日日躁狠狠躁免费麻豆 | 99免费国产| 亚洲一区无 | 欧美黄色录像视频 | 欧美另类日韩 | 免费一二三区 | 人人妻人人玩人人澡人人爽 | 美腿丝袜一区二区三区 | 亚洲少妇网 | 男女男网站| 天海翼一区 | 一级爱爱免费视频 | 久久久久久国产精品免费 | 丰满人妻一区二区三区免费 | 天天操导航 | 亚洲天堂h| 激情在线观看视频 | www.亚洲成人 | 色婷婷综合久久久久中文 | 蜜桃视频日韩 | 亚洲图片欧美另类 | 韩国av电影在线观看 | 色牛av | www射 | 亚洲一区二区三区四区五区午夜 | 国产成人综合视频 | 极品91 | 女性裸体视频网站 | 婷婷深爱激情 | aaaaaav| 日本少妇喂奶 | 中文字幕婷婷 | 神马影院午夜伦理 |