P3078 [USACO13MAR]扑克牌型Poker Hands
Posted five20
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P3078 [USACO13MAR]扑克牌型Poker Hands相关的知识,希望对你有一定的参考价值。
题目描述
Bessie and her friends are playing a unique version of poker involving a deck with N (1 <= N <= 100,000) different ranks, conveniently numbered 1..N (a normal deck has N = 13). In this game, there is only one type of hand the cows can play: one may choose a card labeled i and a card labeled j and play one card of every value from i to j. This type of hand is called a "straight".
Bessie‘s hand currently holds a_i cards of rank i (0 <= a_i <= 100000). Help her find the minimum number of hands she must play to get rid of all her cards.
一个牛有N堆牌,每堆排数量不等。一只牛一次可以将第i张到第j张各打一张出去,问最少几次打完
输入输出格式
输入格式:* Line 1: The integer N.
* Lines 2..1+N: Line i+1 contains the value of a_i.
输出格式:* Line 1: The minimum number of straights Bessie must play to get rid of all her cards.
输入输出样例
5
2
4
1
2
3
6
说明
Bessie can play a straight from 1 to 5, a straight from 1 to 2, a straight from 4 to 5, two straights from 2 to 2, and a straight from 5 to 5, for a total of 6 rounds necessary to get rid of all her cards.
Solution:
本题思路比较巧妙~。
我们可以先画图模拟一下,会发现每次连在一起的区间(不含$0$),肯定是同时去消除最优,即$ans$累加该区间最小值,整个区间减去这段的最小值),按照上述过程去模拟,求出的$ans$是一定正确的。因为我们尽可能的消除了每个连续区间的最小值,假设不消除整段区间,后面至少也会用相同的次数去消除这段区间,所以保证了后面不会有答案比此更优。(说的不够清白,可以画图模拟)。这样其实就可以用线段树维护了,但是可以更加简单。
我们仔细思考上面模拟的过程,会发现,每次让一个数被清$0$的同时去尽可能多的让其它数也有所减少,一定是最优的。那么每读入一个数,我们就让其和前一个数看是否能组成一个区间去整体消除。
也就是意味着开始时我们以第一次读入的数$a_1$作为区间最大值,然后$ans$至少需要累加$a_1$(很显然,消除一个数$a_1$至少要$a_1$次),当读入$a_2$时,若$a_2>a_1$那么要消除$a_2$至少还要$a_2-a_1$次,于是$ans$加上$a_2-a_1$,若$a_2\leq a_1$那么就不需要累加,因为在前面$a_1$的消除中就能同时消去$a_2$。由于要是一段连续的区间,所以每个新读入的值$a_i$只与前一个数$a_{i-1}$有关(再往前就不保证区间一定连续了),于是重复上面的步骤就$OK$了。
整体复杂度$O(n)$,实在是太巧妙了!~。
代码:
1 #include<bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 ll a,b,ans,n; 5 int main(){ 6 scanf("%lld",&n); 7 while(n--){ 8 scanf("%lld",&a); 9 if(a>b)ans+=(a-b); 10 b=a; 11 } 12 cout<<ans; 13 return 0; 14 }
以上是关于P3078 [USACO13MAR]扑克牌型Poker Hands的主要内容,如果未能解决你的问题,请参考以下文章
洛谷 P3079 [USACO13MAR]农场的画Farm Painting
BZOJ 1616 [Usaco2008 Mar]Cow Travelling游荡的奶牛:dp网格型
「USACO13MAR」「LuoguP3080」 牛跑The Cow Run (区间dp